Jump to content

loadstring


dugasz1

Recommended Posts

Hello!

So i want to load external lua files in my resource and i do it with loadstring (Link).
Like this:
 

function loadLuaFile( path )
    local file = fileOpen(path, true)
    if (not file) then
        outputChatBox("Failed")
        return
    else
        local string = ""
        local buffer
        while not fileIsEOF(file) do
            buffer = fileRead(file, 100)
            if (buffer) then 
                string = string .. buffer
            end
        end
        loadstring(string)()
    end
    fileClose(file)
end

Loading a few script:

loadLuaFile(":shared/oop.lua")
loadLuaFile(":shared/test1.lua")
loadLuaFile(":shared/test2.lua")

 

And i found a wierd thing. It doesn't matter how many lua files i load in, the memory usage in performancebrowser doesn't change. Is it a bug? Or somehow its recognize this code was loaded in another resource?

Link to comment

I used the performancebrowser online interface. Maybe because of that. How can i open it ingame?

I don't really get setfenv(0,newENV). Can you please explane it for me? Kindy get how Lua's environments work but what is this 0 means:
"As a special case, when f is 0 setfenv changes the environment of the running thread. In this case, setfenv returns no values."

Why is it good? What is it for?

Link to comment
26 minutes ago, dugasz1 said:

I used the performancebrowser online interface. Maybe because of that. How can i open it ingame?

I don't really get setfenv(0,newENV). Can you please explane it for me? Kindy get how Lua's environments work but what is this 0 means:
"As a special case, when f is 0 setfenv changes the environment of the running thread. In this case, setfenv returns no values."

Why is it good? What is it for?

I will try to explain how it works. Every function has it's own environment containing variables to which function has access. But as you're using loadstring it returns function that could be called by func() or pcall(func) and this function's environment would be set to global state(_G) by default. Because of that, if you're loading scripts from maps made by people, they will be able to call any function or trigger any event, finally, ruin your server. Function environment won't let do that, because you can manage what functions can script use. For instance,

local code = "a = 6 outputChatBox(a)"
local loadedCode = loadstring(code) -- returns function. loadedCode() will also work to call this code
local newEnv = {outputChatBox = outputChatBox} -- we put function to use it in code

setfenv(loadedCode, newEnv) -- now this code is safe and can't access _G 
local executedCode = pcall(loadedCode)
if executedCode then
  outputDebugString("Code loaded!", 3)
end

About perfomancebrowser, run ipb resource and use /ipb to open ingame browser.

 

Also useful posts by Arezu:

 

Hope, I helped a bit.

  • Like 1
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...