add lua table serialization and deserialization so abstract out loading config files
This commit is contained in:
52
main.lua
52
main.lua
@@ -55,6 +55,58 @@ TransmissionTypes = {
|
|||||||
Config = {}
|
Config = {}
|
||||||
Config.ConfigPath = "/caero-attitude-control/config/config.txt"
|
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
|
-- Monitor Configuration
|
||||||
Config.Monitors = {}
|
Config.Monitors = {}
|
||||||
Config.Monitors.InstrumentPanelMonitor = nil
|
Config.Monitors.InstrumentPanelMonitor = nil
|
||||||
|
|||||||
Reference in New Issue
Block a user