Page 1 of 1

table.save relative to Mudlet home directory

Posted: Mon May 23, 2011 11:57 am
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?

Re: table.save relative to Mudlet home directory

Posted: Mon May 23, 2011 11:19 pm
by Vadi
Seems useful, but I'm not sure about the name. Maybe table.saveRelative

Re: table.save relative to Mudlet home directory

Posted: Mon May 23, 2011 11:31 pm
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