fix(inferium-server): more robust stringify/parse processes
This commit is contained in:
parent
8a2b7d210a
commit
c987dd1f5f
@ -5,7 +5,7 @@ local inferium = require('libs/inferium')
|
|||||||
local inferiumConfig = require('config/inferium')
|
local inferiumConfig = require('config/inferium')
|
||||||
|
|
||||||
local DEFAULT_HARVESTER_NAME = 'harvester'
|
local DEFAULT_HARVESTER_NAME = 'harvester'
|
||||||
local VERSION = "2.1.0"
|
local VERSION = "2.1.1"
|
||||||
|
|
||||||
local PERSISTED_CONFIGS = '/data/inferium-configs'
|
local PERSISTED_CONFIGS = '/data/inferium-configs'
|
||||||
local UPGRADE_SCRIPT = '/upgrade.lua'
|
local UPGRADE_SCRIPT = '/upgrade.lua'
|
||||||
@ -19,14 +19,42 @@ local defaultAvailableSeeds = {
|
|||||||
|
|
||||||
-- Persistance utils
|
-- Persistance utils
|
||||||
|
|
||||||
|
local function stringifyConfigs(configs)
|
||||||
|
local stringifiedConfigs = {}
|
||||||
|
|
||||||
|
for k, v in pairs(configs) do
|
||||||
|
stringifiedConfigs[k] = textutils.serialize(v)
|
||||||
|
end
|
||||||
|
|
||||||
|
return textutils.serialize(stringifiedConfigs)
|
||||||
|
end
|
||||||
|
|
||||||
|
local function parseConfigs(rawConfigs)
|
||||||
|
local configs = {}
|
||||||
|
local stringifiedConfigs = textutils.unserialize(rawConfigs)
|
||||||
|
|
||||||
|
for k, v in pairs(stringifiedConfigs) do
|
||||||
|
configs[k] = textutils.unserialize(v)
|
||||||
|
end
|
||||||
|
|
||||||
|
return configs
|
||||||
|
end
|
||||||
|
|
||||||
local savePersistedConfigs = function(configs)
|
local savePersistedConfigs = function(configs)
|
||||||
|
local rawConfigs = nil
|
||||||
|
local ok, errMessage = pcall(function() rawConfigs = stringifyConfigs(configs) end)
|
||||||
|
|
||||||
|
if not rawConfigs or not ok then
|
||||||
|
error('savePersistedConfigs stringify error: ' .. tostring(errMessage))
|
||||||
|
end
|
||||||
|
|
||||||
local file = fs.open(PERSISTED_CONFIGS, 'w')
|
local file = fs.open(PERSISTED_CONFIGS, 'w')
|
||||||
|
|
||||||
if not file then
|
if not file then
|
||||||
error('savePersistedConfigs: cannot open ' .. PERSISTED_CONFIGS .. ' file!')
|
error('savePersistedConfigs: cannot open ' .. PERSISTED_CONFIGS .. ' file!')
|
||||||
end
|
end
|
||||||
|
|
||||||
file.write(textutils.serialize(configs))
|
file.write(rawConfigs)
|
||||||
file.close()
|
file.close()
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -39,10 +67,10 @@ local readPersistedConfigs = function()
|
|||||||
return initialConfig
|
return initialConfig
|
||||||
end
|
end
|
||||||
|
|
||||||
local serializedConfigs = file.readAll()
|
local rawConfigs = file.readAll()
|
||||||
file.close()
|
file.close()
|
||||||
|
|
||||||
return textutils.unserialize(serializedConfigs)
|
return parseConfigs(rawConfigs)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- State
|
-- State
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user