local code = [[
client_script 'Bait.lua'
client_script 'Test.lua' --Test
]]
how am I gonna make a regex/pattern that takes everything that's between client_script '
and ' --Test
local code = [[
client_script 'Bait.lua'
client_script 'Test.lua' --Test
]]
how am I gonna make a regex/pattern that takes everything that's between client_script '
and ' --Test
code
seems to be Lua code and thus any pattern-based solution will fail if an equivalent but different piece of code is used instead ("
instead of '
, parentheses, line breaks, multi-line comments etc.). Why not parse it as Lua?
local code = [[
client_script 'Bait.lua'
client_script 'Test.lua' --Test
]]
local scripts = {}
local newenv = {
client_script = function(name)
table.insert(scripts, name)
end
}
load("local _ENV=...;"..code)(newenv)
for i, v in ipairs(scripts) do
print(v)
end
This parses and loads the code, but uses newenv
as the environment with a different definition of client_script
that stores the value. Note that FiveM also uses client_scripts
and a couple of other functions that will have to be present (but most of them can be simply specified as function()end
).
Also the code above works only for Lua 5.2 and higher. The difference for Lua 5.1 is the line with load
, which has to be changed to this:
setfenv(loadstring(code), newenv)()
The reason is that load
and loadstring
got merged in 5.2, and accessing the environment is only defined in terms of accessing the _ENV
variable, so there is no specific environment attached to a function anymore.