fix(inferium-server): create persisted configs file if don't exist

This commit is contained in:
Guillaume ARM 2024-05-22 19:34:54 +02:00
parent baba577b62
commit 43c936bf3b

View File

@ -1,7 +1,7 @@
local net = require('libs/net') local net = require('libs/net')
local inferium = require('config/inferium') local inferium = require('config/inferium')
local VERSION = "1.1.0" local VERSION = "1.1.1"
local INFERIUM_SERVER = 'inferium.com' local INFERIUM_SERVER = 'inferium.com'
local PERSISTED_CONFIGS = '/data/inferium-configs' local PERSISTED_CONFIGS = '/data/inferium-configs'
@ -9,19 +9,6 @@ local UPGRADE_SCRIPT = '/upgrade.lua'
local defaultConfig = inferium.defaultConfig or error('no default config provided in config', 0) local defaultConfig = inferium.defaultConfig or error('no default config provided in config', 0)
local readPersistedConfigs = function()
local file = fs.open(PERSISTED_CONFIGS, 'r')
if not file then
return nil
end
local serializedConfigs = file.readAll()
file.close()
return textutils.unserialize(serializedConfigs)
end
local savePersistedConfigs = function(configs) local savePersistedConfigs = function(configs)
local file = fs.open(PERSISTED_CONFIGS, 'w') local file = fs.open(PERSISTED_CONFIGS, 'w')
@ -33,7 +20,22 @@ local savePersistedConfigs = function(configs)
file.close() file.close()
end end
local LOCAL_CONFIGS = readPersistedConfigs() or {} local readPersistedConfigs = function()
local file = fs.open(PERSISTED_CONFIGS, 'r')
if not file then
local initialConfig = {}
savePersistedConfigs(initialConfig)
return initialConfig
end
local serializedConfigs = file.readAll()
file.close()
return textutils.unserialize(serializedConfigs)
end
local LOCAL_CONFIGS = readPersistedConfigs()
local function getConfigForComputer(computerId) local function getConfigForComputer(computerId)
return LOCAL_CONFIGS[tostring(computerId)] return LOCAL_CONFIGS[tostring(computerId)]