ExperimentalGear/scripts/common/gameconfig.lua

37 lines
917 B
Lua

require("core.game")
GameConfig = {}
---Find patterns in file
---@param path string # relative path to game file
---@param pattern string # search pattern
---@return table? matches # {{group1, group2, ...}, ...}
---@return string? errmsg
local function match_all(path, pattern)
local matches = {}
local text, err = game.file.open(path, "r")
if not text then
return nil, err
end
for line in text:lines() do
if line:match(pattern) then
table.insert(matches, {line:match(pattern)})
end
end
return matches
end
function GameConfig.refresh()
local config, err = match_all("Main.cfg", "(%w*)%s*=%s*\"?([^\"%s]*)\"?")
if not config then
game.Log("Unable to refresh GameConfig, " .. err, game.LOGGER_ERROR)
return
end
for _, match in ipairs(config) do
GameConfig[match[1]] = match[2]
end
end
GameConfig.refresh()