0
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

Egor Skriptunoff
  • 23,359
  • 2
  • 34
  • 64
Birk
  • 191
  • 2
  • 12
  • 2
    `code:match"client_script%s*'([^\n]+)'%s*%-%-Test"` – Egor Skriptunoff Feb 18 '21 at 17:28
  • 2
    next time it would be nice if you'd share some own ideas first. this is not a coding service or online Lua pattern generator. a solution to this problem can easily be achieved with the Lua reference manual within minutes even if you know nothing about patterns befor – Piglet Feb 19 '21 at 09:25

1 Answers1

1

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.

IS4
  • 11,945
  • 2
  • 47
  • 86