Jump to content

Sending Files Server Side to Client Side


deltamta

Recommended Posts

Hello guys,

I was just wondering if there is a way to transmit Server Side files within maps to Client Side Cache. I already looped through a map's file but I can't seem to understand how to transfer the actual file to client side. The reason I wanna do that is cause I'm running clientside scripts on 'pcall ( loadstring )' and no meta file from the actual map is read. So I can't play the music files etc etc. I tried something like that :

Server Side :

for Index, MapFile in ipairs( g_RaceFiles ) do -- For all the scripts read them completely. 
        g_RaceFiles[Index] = { } 
        g_RaceFiles[Index].names = MapFile 
        g_RaceFiles[Index].files = readMapFile ( readMapFile ( ":"..MapResourceName.."/"..MapFile ) ) 
end 
triggerLatentClientEvent ( Player, 'onClientReceiveMap', 9999999999999, false, Player, g_Vehicles, g_Spawnpoints, g_Checkpoints, g_Objects, g_Pickups, g_RaceScriptFiles, g_RaceFiles ) 

ReadMapFile Server Side Function :

function readMapFile ( MapFile ) 
    local file = fileOpen( MapFile )  
    if ( file ) then 
        local scriptInformation = fileRead( file, fileGetSize( file ) )  
        fileClose( file ) 
        return scriptInformation 
    end 
    return false 
end 

Client Side :

for i, file in ipairs ( g_RaceFiles ) do 
    outputChatBox ( file.files ) 
    local RaceFile = fileCreate( file.names ) 
    if ( RaceFile ) then 
        local RaceWrite = fileWrite ( RaceFile, file.files ) 
        if ( RaceWrite ) then 
            fileClose ( RaceFile ) 
            outputChatBox ( "#ff8c00* #ffffffSuccessfully have written inside the file.", 255, 255, 255, true ) 
        end 
    end 
end 

This though doesn't seem to work... I would appreciate some help :wink:

Regards,

Delta

Link to comment
triggerLatentClientEvent ( Player, 'onClientReceiveMap', 9999999999999, false, Player, g_Vehicles, g_Spawnpoints, g_Checkpoints, g_Objects, g_Pickups, g_RaceScriptFiles, g_RaceFiles ) 

Doing this doesn't help much, triggerLatentClientEvent is useless the way you're using it.

And server scripts aren't supposed to be downloaded to the client's cache, they're run from the server.

Link to comment
And server scripts aren't supposed to be downloaded to the client's cache, they're run from the server.

Not if you want to do what I'm trying to succeed. I want to make a server with many gamemodes inside and to succeed that I have to trigger the map elements, scripts and files Client Side to load them for the specific player that has joinned the specific gamemode. All I want to know is how to trigger a file from server side to client side so I can stick it in cache.

Regards,

Delta.

Link to comment

I don't wanna save a freakin' script I wanna save a file. For example lets say a music file. In order to be ran I need to save it in the client cache. Since I'm using 'pcall ( loadstring )' I have to transfer everything manually. And even if you don't understand can't you just tell me how to move a file from serverside to clientside. I know why I wanna do that.

Regards,

Delta.

Link to comment

Would you like to cache client-side scripts? If so, you first need to get script name's and their location and send them to client. Put them in a table. (You don't need latent event.)

local scriptsTable = {} 
    for index, node in ipairs( xmlNodeGetChildren(meta) ) do 
        local ntype = xmlNodeGetName(node) 
        if ntype == 'script' then 
            local filePath = xmlNodeGetAttribute(node,'src') 
            if filePath then 
                table.insert(scriptsTable, filePath) 
            end 
        end 
    end 
    triggerClientEvent(a, "checkCache", a, resName, scriptsTable) 

Then you need to check if script is already cached. Do that by:

  
function checkCache(resName, tScripts) 
    for i, v in ipairs(tScripts) do 
        if not fileExists("mapscripts/"..resName.."/"..v) then 
            triggerServerEvent("requestDownload", source, resName, v) 
        else 
                 
        end 
    end 
end 
addEvent("checkCache", true) 
addEventHandler("checkCache", root, checkCache) 

On the server-side, you need to read that file and send it back to client.

addEvent("requestDownload", true) 
addEventHandler("requestDownload", root, 
    function(resName, scriptName) 
        if fileExists(":"..resName.."/"..scriptName) then 
            file = fileOpen(":"..resName.."/"..scriptName) 
            if file then 
                fileSize = fileGetSize(file) 
                fileContent = fileRead(file, fileSize) 
                triggerLatentClientEvent(source, "onClientDownloadRecieve", source, fileContent, resName, scriptName) 
            end 
        end 
    end 
) 

When server will send the code back, client will cache it.

function cacheFiles(content, resName, scriptName) 
    local newFile = fileCreate("mapscripts/"..resName.."/"..scriptName) 
    if newFile then 
        fileWrite(newFile, content) 
        fileClose(newFile) 
    end 
end 
addEvent("onClientDownloadRecieve", true) 
addEventHandler("onClientDownloadRecieve", root, cacheFiles) 

That's it :) It works fine for me!

EDIT: Do the same for files like music. You can stream music also.

Link to comment

It works awesome and well and thanks GTX. But when I run the scripts shouldn't it like play the music and load the files etc. ? Cause it doesn't although the files are downloaded. If you could help me with that guys would be awesome.

Regards,

Delta.

Link to comment

You're welcome.

You must use loadstring and do the wrappers. Like:

  
-- Override the function. 
_playSound = playSound 
  
sounds = {} 
  
res = "" 
  
function playSound(filepath, looped) 
    local music = _playSound("cache/"..res.."/"..filepath, looped) -- Filepath is song (music.mp3) 
    if music then 
        table.insert(sounds, music) -- You can unload it later. Just loop through this table and use stopSound. 
    end 
end 
  
function loadScript(content, resourceName) 
    res = resourceName -- Your map name. 
    local loadit = loadstring(content) 
    local loaded = pcall(loadit) 
    if loaded then 
        -- ? 
    end 
end 
  

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