denny199 Posted May 26, 2013 Share Posted May 26, 2013 Hi guys, I was busy with creating some gamemode like my own deathrace gamemode, and my own survival gamemode in one server with different arenas, but now I need to use loadstring to load those client side scripts, because i'm running to some own made single-player games only client side for the player. But the unloading is a big problem. I have readed serveral posts about wrappers, but I still don't understand it exactly how it works within loadstring. How does a wrapper work? like overwriting eventHandlers,etc. Do I just need to make wrappers inside the loading function with loadstring like this: _playSound = playSound function playSound(filepath, looped) _playSound ( "denr.mp3", looped ) end function loadscript(content) local loading = loadstring(content) end addCommandHandler ( "loadme", loadscript ) Does this wrapper overwrite the functions in loadstring too what uses playSound? Kindly regards, Danny Link to comment
ixjf Posted May 26, 2013 Share Posted May 26, 2013 You should sandbox each file's code. Make an environment with wrapped functions for each and load the code on it. http://lua-users.org/wiki/SandBoxes http://pgl.yoyo.org/luai/i/2.9+Environments Link to comment
denny199 Posted May 26, 2013 Author Share Posted May 26, 2013 Hm, I readed very carfully, but now I'm stuck in some point what do I need to do next my code for now is: --server cache the file to client addCommandHandler ( "gvd", function(player) local hFile = fileOpen("test.denr") local data = fileRead(hFile, fileGetSize(hFile)) triggerClientEvent ( player, "get:data", player, data ) fileClose(hFile) end) --Client saving and sandbox thingys and i'm stuck here addEvent ( "get:data", true ) addEventHandler ( "get:data", root, function(data) local newFile = fileCreate("test.me") if (newFile) then fileWrite(newFile, data) fileClose(newFile) end loadFile(data) end) local sandBox = {} sandBox.chatbox = {} _outputChatBox = outputChatBox function outputChatBox(...) sandBox.chatbox["test.me"] = {...} return _outputChatBox(...) end function loadFile (data) local loaded = loadstring(data) setfenv(loaded, sandBox) return pcall(loaded) end The test.me contains: outputChatBox ( "Chinees" ) Errors: None What do I have to do now? I'm lost ;p Link to comment
ixjf Posted May 26, 2013 Share Posted May 26, 2013 What is the purpose of a wrapper of outputChatBox? Link to comment
denny199 Posted May 27, 2013 Author Share Posted May 27, 2013 It could be anything.... I just need the help, I know outputChatBox is a shitty example. It even could be: local sandBox = {} sandBox.eventHandlers = {} _addEventHandler = addEventHandler function addEventHandler(...) sandBox.eventHandlers["denr.me"] = {...} return _addEventHandler(...) end Can you give me a example? Link to comment
Blaawee Posted May 27, 2013 Share Posted May 27, 2013 Here is an example : local gEvents = {}; local _addEventHandler = addEventHandler; function addEventHandler ( eventName, attachedTo, handlerFunction, getPropagated, priority ) if type( eventName ) == 'string' and type( handlerFunction ) == 'function' then table.insert ( gEvents, { eventName, attachedTo, handlerFunction } ); -- Store all events in table end return _addEventHandler ( eventName, attachedTo, handlerFunction, getPropagated, priority ); end function removeTheEvents( ) -- use this function to remove the events for _, theEvent in ipairs( gEvents ) do removeEventHandler ( theEvent[ 1 ], theEvent[ 2 ], theEvent[ 3 ] ); -- Get all events of the resource then remove it end end Link to comment
denny199 Posted May 27, 2013 Author Share Posted May 27, 2013 Thanks, but how can I export those functions to the loadstring function still with setfenv? Link to comment
denny199 Posted May 28, 2013 Author Share Posted May 28, 2013 Thanks, it all works, but I have one problem, onClientResourceStart isn't triggered while it's storing it in the table? For example this is my code which will be loaded: outputChatBox ( "File loaded?" ) function Boosters () outputChatBox ( "Markers loaded?" ) -- Markers marker1 = createMarker(3284.22, -1774.68, 7.46, "corona", 3, 255, 0, 0, 0) --etc theres more code here end addEventHandler( "onClientResourceStart", getResourceRootElement(getThisResource()), Boosters ) It doesn't work because "markers loaded" isn't outputting anything. My load code: function loadfile (data) local loadit = loadstring(data) loaded = pcall(loadit) if loaded then outputChatBox ( "loaded" ) end end And yes, it's outputting : "File loaded?" Link to comment
Moderators IIYAMA Posted May 28, 2013 Moderators Share Posted May 28, 2013 removed. (reason: edited post before) Link to comment
ixjf Posted May 28, 2013 Share Posted May 28, 2013 Because onClientResourceStart event is called by the Multi Theft Auto SA when a resource is loaded, you're just loading code within a resource. Link to comment
denny199 Posted May 28, 2013 Author Share Posted May 28, 2013 ait, thanks for the help! Just added this line: if ( eventName == "onClientResourceStart" ) then load(handlerFunction) end and everything works. Thanks for all your help everyone! 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