feat(robot): add config.onSave function

This commit is contained in:
Guillaume ARM 2024-05-25 23:43:35 +02:00
parent f6ab6d6f77
commit ffccce4700

View File

@ -10,11 +10,16 @@ local DEFAULT_CONFIG = {
stateFilePath = ROBOT_DEFAULT_STATE_FILE stateFilePath = ROBOT_DEFAULT_STATE_FILE
} }
local function identity(x)
return x
end
local function getConfig(givenConfig) local function getConfig(givenConfig)
return { return {
autosave = givenConfig.autosave or DEFAULT_CONFIG.autosave, autosave = givenConfig.autosave or DEFAULT_CONFIG.autosave,
autoload = givenConfig.autoload or DEFAULT_CONFIG.autoload, autoload = givenConfig.autoload or DEFAULT_CONFIG.autoload,
stateFilePath = givenConfig.stateFilePath or DEFAULT_CONFIG.stateFilePath stateFilePath = givenConfig.stateFilePath or DEFAULT_CONFIG.stateFilePath,
onSave = givenConfig.onSave or identity
} }
end end
@ -28,8 +33,7 @@ local function createDefaultState()
} }
end end
local saveRobotState = function(stateFile, state) local saveRobotState = function(stateFile, onSave, state)
stateFile = stateFile or ROBOT_DEFAULT_STATE_FILE
state = state or createDefaultState() state = state or createDefaultState()
local file = fs.open(stateFile, 'w') local file = fs.open(stateFile, 'w')
@ -37,18 +41,17 @@ local saveRobotState = function(stateFile, state)
error('saveRobotState: cannot open ' .. stateFile .. ' file!') error('saveRobotState: cannot open ' .. stateFile .. ' file!')
end end
file.write(textutils.serializeJSON(state)) file.write(textutils.serializeJSON(onSave(state)))
file.close() file.close()
return state return state
end end
local loadRobotState = function(stateFile) local loadRobotState = function(stateFile, onSave)
stateFile = stateFile or ROBOT_DEFAULT_STATE_FILE
local file = fs.open(stateFile, 'r') local file = fs.open(stateFile, 'r')
if not file then if not file then
return robot.saveRobotState(stateFile) return robot.saveRobotState(stateFile, onSave)
end end
local serializedRobotState = file.readAll() local serializedRobotState = file.readAll()
@ -57,7 +60,7 @@ local loadRobotState = function(stateFile)
local parsedResult = textutils.unserializeJSON(serializedRobotState) local parsedResult = textutils.unserializeJSON(serializedRobotState)
if not parsedResult then if not parsedResult then
return robot.saveRobotState(stateFile) return robot.saveRobotState(stateFile, onSave)
end end
return parsedResult return parsedResult
@ -68,7 +71,7 @@ local getAutoSave = function(config, state)
if config.autosave then if config.autosave then
autoSave = function() autoSave = function()
saveRobotState(config.stateFilePath, state) saveRobotState(config.stateFilePath, config.onSave, state)
end end
end end
@ -192,11 +195,11 @@ api.create = function(state, givenConfig)
end end
robot.saveState = function() robot.saveState = function()
return saveRobotState(config.stateFilePath, state) return saveRobotState(config.stateFilePath, config.onSave, state)
end end
robot.loadState = function(stateFile) robot.loadState = function(stateFile)
return loadRobotState(config.stateFilePath) return loadRobotState(config.stateFilePath, config.onSave)
end end
return robot return robot