table.save relative to Mudlet home directory

Post Reply
Wyd
Posts: 54
Joined: Wed Mar 24, 2010 11:56 pm

table.save relative to Mudlet home directory

Post by Wyd »

Because of the ability to now register the sysExitEvent and sysLoadEvent functions, I've created table.LoadRelative() and table.saveRelative() to easily save and load tables relative to the mudlet home directory:
Code: [show] | [select all] lua
function table.SaveRelative(relPath, saveTable)
	if string.char(getMudletHomeDir():byte()) == "/" then _sep = "/" else  _sep = "\\" end
	local saveFile = getMudletHomeDir() ..  _sep .. relPath

	table.save(saveFile, saveTable)
end
Is it worth having something similar added to the lua globals file?

User avatar
Vadi
Posts: 4854
Joined: Sat Mar 14, 2009 3:13 pm

Re: table.save relative to Mudlet home directory

Post by Vadi »

Seems useful, but I'm not sure about the name. Maybe table.saveRelative

Wyd
Posts: 54
Joined: Wed Mar 24, 2010 11:56 pm

Re: table.save relative to Mudlet home directory

Post by Wyd »

]Yeah, I do have it lowercase to go fit with convention:
Code: [show] | [select all] lua
function table.saveRelative(relPath, saveTable)
	if string.char(getMudletHomeDir():byte()) == "/" then _sep = "/" else  _sep = "\\" end
	local saveFile = getMudletHomeDir() ..  _sep .. relPath

	table.save(saveFile, saveTable)
end

function table.loadRelative(relPath)
	if string.char(getMudletHomeDir():byte()) == "/" then _sep = "/" else  _sep = "\\" end
	local saveFile = getMudletHomeDir() ..  _sep .. relPath

	local loadTable = {}
	table.load(saveFile, loadTable)

	return loadTable
end
Example usage:
Code: [show] | [select all] lua

function antitheft_save_tracked()
	local saveTable = {
			tracked = Antitheft.tracked,
			container = Antitheft.container
	}

	table.saveRelative("antitheft.lua", saveTable)
end

function antitheft_load_tracked()
	local loaded = table.loadRelative("antitheft.lua")
	
	if loaded.tracked then
		Antitheft.tracked = loaded.tracked
	end

	if loaded.container then
		Antitheft.container = loaded.container
	end
end


Post Reply