Migration for new helpers library

This commit is contained in:
Cooldude2606
2024-10-06 16:34:37 +01:00
parent 67a169ce0a
commit 377fb69152
10 changed files with 51 additions and 26 deletions

View File

@@ -9,13 +9,15 @@ System command which sends an object to the clustorio api, should be used for de
local Commands = require("modules/exp_commands")
local Clustorio = require("modules/clusterio/api")
local json_to_table = helpers.json_to_table
Commands.new("_ipc", "Send an IPC message on the selected channel")
:add_flags{ "system_only" }
:enable_auto_concatenation()
:argument("channel", "string")
:argument("message", "string")
:register(function(_, channel, message)
local tbl = game.json_to_table(message)
local tbl = json_to_table(message)
if tbl == nil then
return Commands.status.invalid_input("Invalid json string")
else

View File

@@ -1,5 +1,8 @@
local Async = require("modules/exp_util/async")
local table_to_json = helpers.table_to_json
local json_to_table = helpers.json_to_table
--- Top level module table, contains event handlers and public methods
local Groups = {}
@@ -233,13 +236,13 @@ function Groups.get_actions_json()
rtn_i = rtn_i + 1
end
return game.table_to_json(rtn)
return table_to_json(rtn)
end
--- Convert a json string array into an array of input actions
--- @param json string A json string representing a string array of actions
function Groups.json_to_actions(json)
local tbl = game.json_to_table(json)
local tbl = json_to_table(json)
assert(tbl, "Invalid Json String")
--- @cast tbl string[]
return names_to_actions(tbl)
@@ -262,15 +265,15 @@ function Groups._prototype:to_json(raw)
end
if allow_i >= disallow_i then
return raw and { true, disallow } or game.table_to_json{ true, disallow }
return raw and { true, disallow } or table_to_json{ true, disallow }
end
return raw and { false, allow } or game.table_to_json{ false, allow }
return raw and { false, allow } or table_to_json{ false, allow }
end
--- Restores this group to the state given in a json string
--- @param json string The json string to restore from
function Groups._prototype:from_json(json)
local tbl = game.json_to_table(json)
local tbl = json_to_table(json)
assert(tbl and type(tbl[1]) == "boolean" and type(tbl[2]) == "table", "Invalid Json String")
if tbl[1] then

View File

@@ -154,6 +154,9 @@ local Datastore = {}
local Data = {}
local copy = table.deep_copy
local trace = debug.traceback
local table_to_json = helpers.table_to_json
local json_to_table = helpers.json_to_table
local write_file = helpers.write_file
--- Save datastores in the global table
Storage.register(Data, function(tbl)
@@ -242,11 +245,11 @@ function DatastoreManager.ingest(action, datastore_name, key, value_json)
if action == "remove" then
datastore:raw_set(key)
elseif action == "message" then
local success, value = pcall(game.json_to_table, value_json)
local success, value = pcall(json_to_table, value_json)
if not success or value == nil then value = tonumber(value_json) or value_json end
datastore:raise_event("on_message", key, value)
elseif action == "propagate" or action == "request" then
local success, value = pcall(game.json_to_table, value_json)
local success, value = pcall(json_to_table, value_json)
if not success or value == nil then value = tonumber(value_json) or value_json end
local old_value = datastore:raw_get(key)
value = datastore:raise_event("on_load", key, value, old_value)
@@ -389,9 +392,9 @@ self:write_action('save', 'TestKey', 'Foo')
function Datastore:write_action(action, key, value)
local data = { action, self.name, key }
if value ~= nil then
data[4] = type(value) == "table" and game.table_to_json(value) or value
data[4] = type(value) == "table" and table_to_json(value) or value
end
game.write_file("ext/datastore.out", table.concat(data, " ") .. "\n", true, 0)
write_file("ext/datastore.out", table.concat(data, " ") .. "\n", true, 0)
end
----- Datastore Local

View File

@@ -47,6 +47,9 @@ local Datastore = require("modules.exp_legacy.expcore.datastore") --- @dep expco
local Commands = require("modules.exp_legacy.expcore.commands") --- @dep expcore.commands
require("modules.exp_legacy.config.expcore.command_general_parse") --- @dep config.expcore.command_general_parse
local table_to_json = helpers.table_to_json
local write_file = helpers.write_file
--- Common player data that acts as the root store for player data
local PlayerData = Datastore.connect("PlayerData", true) -- saveToDisk
PlayerData:set_serializer(Datastore.name_serializer) -- use player name
@@ -83,7 +86,7 @@ Commands.new_command("preference", "Shows you what your current data saving pref
Commands.new_command("save-data", "Writes all your player data to a file on your computer")
:register(function(player)
player.print{ "expcore-data.get-data" }
game.write_file("expgaming_player_data.json", game.table_to_json(PlayerData:get(player, {})), false, player.index)
write_file("expgaming_player_data.json", table_to_json(PlayerData:get(player, {})), false, player.index)
end)
--- Async function called after 5 seconds with no player data loaded

View File

@@ -7,11 +7,13 @@ local Roles = require("modules.exp_legacy.expcore.roles") --- @dep expcore.roles
local format_number = require("util").format_number --- @dep util
local config = require("modules.exp_legacy.config.deconlog") --- @dep config.deconlog
local write_file = helpers.write_file
local filepath = "log/decon.log"
local seconds_time_format = ExpUtil.format_time_factory{ format = "short", hours = true, minutes = true, seconds = true }
local function add_log(data)
game.write_file(filepath, data .. "\n", true, 0) -- write data
write_file(filepath, data .. "\n", true, 0) -- write data
end
local function get_secs()
@@ -36,7 +38,7 @@ local function print_to_players(admin, message)
end
Event.on_init(function()
game.write_file(filepath, "\n", false, 0) -- write data
write_file(filepath, "\n", false, 0) -- write data
end)
if config.decon_area then

View File

@@ -3,17 +3,19 @@
local Event = require("modules/exp_legacy/utils/event") --- @dep utils.event
local write_file = helpers.write_file
-- Clear the file on startup to minimize its size
Event.on_init(function()
game.write_file("fagc-actions.txt", "", false, 0)
write_file("fagc-actions.txt", "", false, 0)
end)
Event.add(defines.events.on_player_banned, function(e)
local text = "ban;" .. e.player_name .. ";" .. (e.by_player or "") .. ";" .. (e.reason or "") .. "\n"
game.write_file("fagc-actions.txt", text, true, 0)
write_file("fagc-actions.txt", text, true, 0)
end)
Event.add(defines.events.on_player_unbanned, function(e)
local text = "unban;" .. e.player_name .. ";" .. (e.by_player or "") .. ";" .. (e.reason or "") .. "\n"
game.write_file("fagc-actions.txt", text, true, 0)
write_file("fagc-actions.txt", text, true, 0)
end)

View File

@@ -6,9 +6,11 @@ local Event = require("modules/exp_legacy/utils/event") --- @dep utils.event
local config = require("modules.exp_legacy.config.logging") --- @dep config.logging
local config_res = require("modules.exp_legacy.config.research") --- @dep config.research
local write_file = helpers.write_file
local function add_log(data)
game.write_file(config.file_name, data, true, 0)
game.write_file(config.file_name, "\n", true, 0)
write_file(config.file_name, data, true, 0)
write_file(config.file_name, "\n", true, 0)
end
Event.add(defines.events.on_rocket_launched, function(event)

View File

@@ -4,6 +4,8 @@ local statics = require("modules.exp_legacy.modules.graftorio.statics")
local general = require("modules.exp_legacy.modules.graftorio.general")
local forcestats = nil
local table_to_json = helpers.table_to_json
if config.modules.forcestats then
forcestats = require("modules.exp_legacy.modules.graftorio.forcestats")
end
@@ -22,6 +24,6 @@ Commands.new_command("collectdata", "Collect data for RCON usage")
forcestats.collect_production()
forcestats.collect_loginet()
end
rcon.print(game.table_to_json(general.data.output))
rcon.print(table_to_json(general.data.output))
return Commands.success()
end)

View File

@@ -8,6 +8,9 @@ local Event = require("modules/exp_legacy/utils/event") --- @dep utils.event
local Roles = require("modules.exp_legacy.expcore.roles") --- @dep expcore.roles
local config = require("modules.exp_legacy.config.research") --- @dep config.research
local table_to_json = helpers.table_to_json
local write_file = helpers.write_file
local research = {}
Storage.register(research, function(tbl)
research = tbl
@@ -57,7 +60,7 @@ local function research_add_log()
result_data[res["disp"][i]["raw_name"]] = research.time[i]
end
game.write_file(config.file_name, game.table_to_json(result_data) .. "\n", true, 0)
write_file(config.file_name, table_to_json(result_data) .. "\n", true, 0)
end
local function research_res_n(res_)

View File

@@ -8,6 +8,9 @@ local traceback = debug.traceback
local floor = math.floor
local concat = table.concat
local table_to_json = helpers.table_to_json
local write_file = helpers.write_file
local Common = {
--- A large mapping of colour rgb values by their common name
color = require("modules/exp_util/include/color"),
@@ -82,7 +85,7 @@ function Common.assert_argument_type(arg_value, type_name, arg_index, arg_name)
end
end
--- Write a luu table to a file as a json string, note the defaults are different to game.write_file
--- Write a luu table to a file as a json string, note the defaults are different to write_file
--- @param path string The path to write the json to
--- @param tbl table The table to write to file
--- @param overwrite boolean? When true the json replaces the full contents of the file
@@ -90,9 +93,9 @@ end
--- @return nil
function Common.write_json(path, tbl, overwrite, player_index)
if player_index == -1 then
return game.write_file(path, game.table_to_json(tbl) .. "\n", not overwrite)
return write_file(path, table_to_json(tbl) .. "\n", not overwrite)
end
return game.write_file(path, game.table_to_json(tbl) .. "\n", not overwrite, player_index or 0)
return write_file(path, table_to_json(tbl) .. "\n", not overwrite, player_index or 0)
end
--- Clear a file by replacing its contents with an empty string
@@ -101,9 +104,9 @@ end
--- @return nil
function Common.clear_file(path, player_index)
if player_index == -1 then
return game.write_file(path, "", false)
return write_file(path, "", false)
end
return game.write_file(path, "", false, player_index or 0)
return write_file(path, "", false, player_index or 0)
end
--- Same as require but will return nil if the module does not exist, all other errors will propagate to the caller
@@ -214,7 +217,7 @@ function Common.format_any(value, options)
local formatted, is_locale_string = Common.safe_value(value)
if type(formatted) == "table" and (not is_locale_string or options.no_locale_strings) then
if options.as_json then
local success, rtn = pcall(game.table_to_json, value)
local success, rtn = pcall(table_to_json, value)
if success then return rtn end
end
if options.max_line_count ~= 0 then