Skip to content

Instantly share code, notes, and snippets.

@QZLin
Last active August 29, 2021 16:06
Show Gist options
  • Save QZLin/6b786035911cff1fa86ed1023a3b3fab to your computer and use it in GitHub Desktop.
Save QZLin/6b786035911cff1fa86ed1023a3b3fab to your computer and use it in GitHub Desktop.
Access factorio data by folder/file path liked string
local function split(inputstr, sep)
if sep == nil then sep = "%s" end
local result_table = {}
for str in string.gmatch(inputstr, "([^" .. sep .. "]+)") do
table.insert(result_table, str)
end
return result_table
end
function Ptable(table_, path)
-- example path: action/{type=direct}/action_delivery/target_effects
local last_table = table_
local i = 0
for node in string.gmatch(path, "[^/]+") do
i = i + 1
if string.match(node, "{.*=.*}") then
local key_value = split(string.sub(node, 2, -2), "=")
assert(#key_value == 2,
string.format("Node <%s> should be {key=value}", node))
local key = key_value[1]
local value = key_value[2]
for _, v in pairs(last_table) do
if v[key] == value then last_table = v end
end
else
last_table = last_table[node]
end
assert(last_table ~= nil,
string.format("Read node <%s:%d> of `%s` fail", node, i, path))
end
return last_table
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment