Bonsai Posted January 21, 2014 Share Posted January 21, 2014 Hey Peeps, I did a lot of research before, but I didn't find any good answer to my question. When I'm loading a script file by loadstring, where do I put my wrapper functions? It obviously does not work to just have them in the same resource. Do I have to edit the loaded script string before executing? Bonsai Link to comment
Gallardo9944 Posted January 21, 2014 Share Posted January 21, 2014 i just created another resource which handles that. The code looks like: local objects = { } local _createObject = createObject -- save the original function function createObject(id,x,y,z,rx,ry,rz,islod) -- replace the original one if not id or not x or not y or not z then return false end local rx = rx or 0 local ry = ry or 0 local rz = rz or 0 local object = _createObject(id,x,y,z,rx,ry,rz,islod) -- use the old function if object then table.insert(objects,object) return object -- Don't forget to return the info of the original one end end function loadCode(content) local loaded = loadstring(content) local ex = pcall(loaded) if ex then outputChatBox("loaded the code") end end function unloadCode() for i,v in ipairs(objects) do if isElement(v) then destroyElement(v) end end end Link to comment
Bonsai Posted January 21, 2014 Author Share Posted January 21, 2014 Okay, I tested yours and it works... My code did pretty much look like that, don't know why it wasn't working. Anyway, now I can finally get started. Thank you. Link to comment
ixjf Posted January 21, 2014 Share Posted January 21, 2014 I'd rather run the code in a secure, separate environment (sandbox): http://lua-users.org/wiki/SandBoxes Link to comment
Bonsai Posted January 21, 2014 Author Share Posted January 21, 2014 Something is not working well with this way of loading code. I get an error if my script looks e.g. like: grav1 = createMarker ( 5595.1923828125, -1091.1322021484, 91.135231018066 , "corona" , 7, 0, 0, 0, 0) addEventHandler( "onClientMarkerHit", grav1, markerHit2) It says element attached to Handler is nil... EDIT: Nevermind, I've found the problem. Thanks anyway. Link to comment
Gallardo9944 Posted January 22, 2014 Share Posted January 22, 2014 setfenv doesn't seem to work with MTA, does it? Link to comment
Bonsai Posted January 22, 2014 Author Share Posted January 22, 2014 setfenv doesn't seem to work with MTA, does it? It didn't work for me. Don't know if I did it right. Is there a way to get rid of variables without restarting the resource? Bonsai Link to comment
Gallardo9944 Posted January 22, 2014 Share Posted January 22, 2014 I use _EVN changes and that seems to work though. The only thing I have to do is to move _G stuff into the environment table. Seems to help. Then just empty the environment and it should be fine. At least that's how it works at my server. Link to comment
Bonsai Posted January 22, 2014 Author Share Posted January 22, 2014 Don't I need setfenv to get that done? Link to comment
ixjf Posted January 22, 2014 Share Posted January 22, 2014 setfenv doesn't seem to work with MTA, does it? setfenv should work just fine. setfenv doesn't seem to work with MTA, does it? It didn't work for me. Don't know if I did it right. Is there a way to get rid of variables without restarting the resource? Bonsai If you use separate environments, just clear it (or delete it completely) - it's all just a table. Link to comment
Bonsai Posted January 22, 2014 Author Share Posted January 22, 2014 local env = {} local func = loadstring(data) setfenv(func, env) pcall(func) It tells me "bad argument #1 to setfenv (number expected, got nil)" Link to comment
Gallardo9944 Posted January 22, 2014 Share Posted January 22, 2014 Yep, i had the same issue with setfenv. I suppose debug.setfenv should be used. But no use though. Link to comment
ixjf Posted January 22, 2014 Share Posted January 22, 2014 local env = {} local func = loadstring(data) setfenv(func, env) pcall(func) It tells me "bad argument #1 to setfenv (number expected, got nil)" I'm unable to reproduce this. Are you sure the string passed to loadstring is valid code and that the function is returning the correct result? Also, is this code being executed on the client or server? This is the code I used on the server side: local env = {} env.outputServerLog = outputServerLog local func = loadstring ( "outputServerLog ( \"hey\" )" ) local _func = loadstring ( "var = 1" ) outputServerLog ( tostring ( func ) ) outputServerLog ( tostring ( _func ) ) setfenv ( func, env ) setfenv ( _func, env ) outputServerLog ( tostring ( pcall ( func ) ) ) outputServerLog ( tostring ( pcall ( _func ) ) ) outputServerLog ( "var: " .. env.var ) Output: function: 01C06C78 function: 01C06CB8 hey true true var: 1 Link to comment
Gallardo9944 Posted January 22, 2014 Share Posted January 22, 2014 Confirmed, it works. I had a problem of moving functions into the new env table: -- DOESN'T WORK for i,v in ipairs(_G) do if not (v == "cacheFiles" or v == "loadData" or v == "unloadAll") then --table.insert(RUNCODE,v) -- Neither this RUNCODE[#RUNCODE+1] = v -- Nor that end end -- WORKS setmetatable(RUNCODE,{__index = _G}) Link to comment
Bonsai Posted January 22, 2014 Author Share Posted January 22, 2014 Yeah, that works. But thats not suitable for loading complete scripts sadly. Link to comment
Gallardo9944 Posted January 22, 2014 Share Posted January 22, 2014 that is suitable though. You can test it on my server, just moved it to this structure. Link to comment
ixjf Posted January 22, 2014 Share Posted January 22, 2014 Confirmed, it works. I had a problem of moving functions into the new env table: -- DOESN'T WORK for i,v in ipairs(_G) do if not (v == "cacheFiles" or v == "loadData" or v == "unloadAll") then --table.insert(RUNCODE,v) -- Neither this RUNCODE[#RUNCODE+1] = v -- Nor that end end -- WORKS setmetatable(RUNCODE,{__index = _G}) viewtopic.php?f=91&t=69031#p644203 Read from there on. And, for k,v in pairs ( _G ) do if ( not ( k == "cacheFiles" or k == "loadData" or k == "unloadAll" or k == "_G" ) ) then RUNCODE[k] = v end end Link to comment
Gallardo9944 Posted January 23, 2014 Share Posted January 23, 2014 Unfortunately, the way of copying _G causes problems for some scripts. I'd stick to metatables with index. Link to comment
Bonsai Posted January 23, 2014 Author Share Posted January 23, 2014 This seems to be the hardest part when making wrappers. Its easy to destroy created elements or set back world functions to normal, but getting rid of set variables is complicated. Easiest way seems to be just to restart the wrapper resource, but it turned out restart takes too long so its not up again when server side scripts ask for execution. PS: When I want to remove an EventHandler, but the attached Element doesn't exist anymore at that time, could I just use root? Or isn't it necesssary to remove that handler at all? Link to comment
ixjf Posted January 23, 2014 Share Posted January 23, 2014 Unfortunately, the way of copying _G causes problems for some scripts. I'd stick to metatables with index. Can you enumerate them? This seems to be the hardest part when making wrappers.Its easy to destroy created elements or set back world functions to normal, but getting rid of set variables is complicated. It's as simple as clearing a table, what's your problem? PS: When I want to remove an EventHandler, but the attached Element doesn't exist anymore at that time, could I just use root? Or isn't it necesssary to remove that handler at all? Event handlers are removed automatically when the element is deleted. Link to comment
Bonsai Posted January 23, 2014 Author Share Posted January 23, 2014 Event handlers are removed automatically when the element is deleted. Okay, thanks for that. It's as simple as clearing a table, what's your problem? I'm loading a complete script by loadstring. Everything works fine. Wrapper do their job etc. But when I put setfenv on my loadstring it outputs an error. "bad argument #1 to setfenv (number expected, got nil)" Simple functions like in your example work fine. But a script does not. Link to comment
ixjf Posted January 23, 2014 Share Posted January 23, 2014 Mind showing your loader code? Link to comment
Bonsai Posted January 23, 2014 Author Share Posted January 23, 2014 Mind showing your loader code? function loadScript(file, dim) dimension = dim local file = fileOpen(file) local content = fileRead(file, fileGetSize(file)) local loaded = loadstring(content) pcall(loaded) end _addEventHandler("onLoadScript", root, loadScript) Link to comment
ixjf Posted January 23, 2014 Share Posted January 23, 2014 That snippet of code is irrelevant. You talk about an issue on setfenv call but you show nothing about it. Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now