Jump to content

Wrappers with loadstring?


Recommended Posts

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

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

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

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

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

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...