add lua table serialization and deserialization so abstract out loading config files

This commit is contained in:
2026-06-25 15:14:24 -05:00
parent 59e3b5a266
commit 0776699c68

View File

@@ -55,6 +55,58 @@ TransmissionTypes = {
Config = {}
Config.ConfigPath = "/caero-attitude-control/config/config.txt"
-- File serializatino and deserialization
-- taken from https://gist.github.com/yuhanz/6688d474a3c391daa6d6
function tableToString(table)
return "return"..serializeTable(table)
end
-- loadstring is deprecated, so modified to run without it
function stringToTable(str)
local f = {}
local chunk, err = load(str)
if chunk then
f = chunk()
else
print("Error loading config string: "..err)
end
return f
end
function serializeTable(val, name, skipnewlines, depth)
skipnewlines = skipnewlines or false
depth = depth or 0
local tmp = string.rep(" ", depth)
if name then
if not string.match(name, '^[a-zA-z_][a-zA-Z0-9_]*$') then
name = string.gsub(name, "'", "\\'")
name = "['".. name .. "']"
end
tmp = tmp .. name .. " = "
end
if type(val) == "table" then
tmp = tmp .. "{" .. (not skipnewlines and "\n" or "")
for k, v in pairs(val) do
tmp = tmp .. serializeTable(v, k, skipnewlines, depth + 1) .. "," .. (not skipnewlines and "\n" or "")
end
tmp = tmp .. string.rep(" ", depth) .. "}"
elseif type(val) == "number" then
tmp = tmp .. tostring(val)
elseif type(val) == "string" then
tmp = tmp .. string.format("%q", val)
elseif type(val) == "boolean" then
tmp = tmp .. (val and "true" or "false")
else
tmp = tmp .. "\"[inserializeable datatype:" .. type(val) .. "]\""
end
return tmp
end
-- Monitor Configuration
Config.Monitors = {}
Config.Monitors.InstrumentPanelMonitor = nil