STS ... Simple Table Storage

Want a small and quick way to save data that doesn’t need something like DefSave but still don’t want to use the builtin save functions directly?

sts.lua (744 Bytes)

-- STS ... Simple Table Storage

local M = {}
M.appname = "SimpleTableStorage"
M.sysinfo = sys.get_sys_info()

local function get_appname()
	local appname = M.appname
	if M.sysinfo.system_name == "Linux" then
		-- For Linux we must modify the default path to make Linux users happy
		appname = "config/" .. tostring(M.appname)
	end
	return appname
end

function M.save(filename, filetable)
	local path = sys.get_save_file(get_appname(), filename)
	return sys.save(path, filetable)
end

function M.load(filename)
	local path = sys.get_save_file(get_appname(), filename)
	local file = sys.load(path) or {}
	return file
end

function M.delete(filename)
	local path = sys.get_save_file(get_appname(), filename)
	return os.remove(path)
end

return M

I have used this in some small utilities for saving / loading stand alone config data. Below is an example of how it might be used. For bigger projects something like DefSave or something more custom can be better.

local sts = require("imports.sts")

...

function M.load_manifest()
	M.manifest = sts.load("manifest")
end

function M.save_manifest()
	sts.save("manifest", M.manifest)
end

...

function M.delete_entire_cache()
	M.load_manifest()
	for k,v in pairs(M.manifest) do
		local confirm, error = M.delete(k)
		if error then print(error) end
	end
	M.save_manifest()
end

...
14 Likes