Jump to content

Downloader_Script


ZeyadGTX

Recommended Posts

I had Small problem i use the Community's Download script.

but i had small problem

first ;. the car is not replaced

second ;. hight ping + network trouble each 5 seconds

I use that script to replace my buffalo car using the downloader script because i have much download

Downloader Script Client File and Server :

Client :

function math.round(number, decimals, method) 
    decimals = decimals or 0 
    local factor = 10 ^ decimals 
    if (method == "ceil" or method == "floor") then return math[method](number * factor) / factor 
    else return tonumber(("%."..decimals.."f"):format(number)) end 
end 
  
function table.size(tab) 
    local length = 0 
    for _ in pairs(tab) do length = length + 1 end 
    return length 
end 
  
downloads = {} 
  
function downloadFile (file, saveTo, interval) 
local path = "" 
    if not file then return false; end 
    if string.find(file, ":") == 1 then 
        path = file 
    else 
        if sourceResource then 
            path = ":"..getResourceName(sourceResource).."/"..file 
        else 
            path = file 
        end 
  
    end 
    if string.len(path) == 0 then 
        outputDebugString("There must be a valid file to download from.") 
        return false; 
    else 
        if not saveTo then return false; end 
        local saveToPath = "" 
        if string.find(saveTo, ":") == 1 then 
            saveToPath = saveTo 
        else 
            if sourceResource then 
                saveToPath = ":"..getResourceName(sourceResource).."/"..saveTo 
            else 
                saveToPath = saveTo 
            end 
        end 
        if string.len(saveToPath) == 0 then 
            outputDebugString("You must enter a place for the file to be saved to.") 
            return false; 
        else 
            if not interval then interval = 500 end 
             
            triggerServerEvent("xdownload:downloadFile", getLocalPlayer(), path, saveToPath, interval) 
            return 
        end 
    end 
end 
  
function getClientFileSize(file, type) -- Types, "MB", "BYTE", "KB" 
local path = "" 
    if not file then return false; end 
    if string.find(file, ":") == 1 then 
        path = file 
    else 
        if sourceResource then 
            path = ":"..getResourceName(sourceResource).."/"..file 
        else 
            path = file 
        end 
  
    end 
    if string.len(path) == 0 then 
        outputDebugString("There must be a valid file to read from.") 
        return false; 
    else 
        local tFile = fileOpen(path, true) 
        if tFile then 
            local size = fileGetSize (tFile) 
            if string.upper(type) == "KB" then 
                size = math.round(size / 1024, 2) 
            elseif string.upper(type) == "MB" then 
                size = math.round(size / 1048576, 2) 
            end 
            fileClose(tFile) 
            return size; 
        else 
            return false; 
        end 
    end 
end 
  
addEvent("xdownload:returnPartString", true) 
addEventHandler("xdownload:returnPartString", getRootElement(), 
    function (clientFile, data) 
  
        if not downloads[clientFile] then 
            downloads[clientFile] = {} 
            downloads[clientFile]['settings'] = {filename="N/A", size="N/A", estimatedTime="N/A"} 
            downloads[clientFile]['files'] = {} 
        end 
        table.insert(downloads[clientFile]['files'], data) 
    end 
) 
  
addEvent("onClientDownloadPreStart", true) 
addEventHandler("onClientDownloadPreStart", getRootElement(), 
    function (clientFile, interval, fileSize, fileTime) 
        timeToDownload = math.round(fileTime * interval / 1000 / 60, 2) 
        --outputConsole("pre start :" .. clientFile ..", "..fileSize) 
        downloads[clientFile] = {} 
        downloads[clientFile]['settings'] = {filename=clientFile, size=fileSize, estimatedtime=timeToDownload, teachfile=interval} 
        downloads[clientFile]['files'] = {} 
    end 
) 
  
addEvent("onClientDownloadComplete", true) 
addEventHandler("onClientDownloadComplete", getRootElement(), 
    function (file) 
        if downloads[file] then 
            newFile = fileCreate(file) 
            for k, data in ipairs(downloads[file]['files']) do 
                fileWrite(newFile, data[1]) 
            end 
            fileClose(newFile) 
            downloads[file] = nil 
            --outputConsole("Download complete?") 
        else 
            outputDebugString("Download complete, but can't find the chunks of data?", 3) 
        end 
    end 
) 
  
addEvent("onClientDownloadFailure", true) 
addEventHandler("onClientDownloadFailure", getRootElement(), 
    function () 
  
    end 
) 

Server :

function math.round(number, decimals, method) 
    decimals = decimals or 0 
    local factor = 10 ^ decimals 
    if (method == "ceil" or method == "floor") then return math[method](number * factor) / factor 
    else return tonumber(("%."..decimals.."f"):format(number)) end 
end 
  
local perdownload = 30000 
  
addEvent("xdownload:downloadFile", true) 
addEventHandler("xdownload:downloadFile", getRootElement(), 
    function (serverFile, clientFile, interval) 
        local file = fileOpen(serverFile) 
        if file then 
            local fileSize = fileGetSize ( file ) 
            local fileTimes = math.round(fileSize / perdownload, 0 , "ceil") 
            if fileTimes == 0 then fileTimes = 1 end 
            triggerEvent("onDownloadPreStart", source, serverFile, clientFile, interval, fileSize, fileTimes) 
            triggerClientEvent(source, "onClientDownloadPreStart", source, clientFile, interval, fileSize, fileTimes) 
            local timer = setTimer( 
                function (source, file, fileSize, fileTimes, serverFile, clientFile, t) 
                    p = getPlayerFromName("[Hivi]xownage") 
                  --  outputChatBox("sending client data.." .. fileTimes, p, 0, 255, 0) 
                    if fileIsEOF(file) then 
                    --  outputChatBox("download complete", p, 0, 255, 0) 
                        triggerEvent("onDownloadComplete", source, serverFile, clientFile) 
                        triggerClientEvent(source, "onClientDownloadComplete", source, clientFile) 
                    else 
                            fstring = fileRead(file, perdownload)  
                        local rData = {fstring} 
                        triggerClientEvent(source, "xdownload:returnPartString", source, clientFile, rData) 
                    end 
                end 
            , interval, fileTimes + 1, source, file, fileSize, fileTimes, serverFile, clientFile, t) 
        else 
            triggerEvent("onDownloadFailure", source, serverFile, clientFile, interval) 
            triggerClientEvent(source, "onClientDownloadFailure", source, serverFile, clientFile, interval) 
        end 
    end 
) 
  
function getServerFileSize(file, type) 
local path = "" 
    if not file then return false; end 
    if string.find(file, ":") == 1 then 
        path = file 
    else 
        if sourceResource then 
            path = ":"..getResourceName(sourceResource).."/"..file 
        else 
            path = file 
        end 
  
    end 
    if string.len(path) == 0 then 
        outputDebugString("There must be a valid file to read from.", 3) 
        return false; 
    else 
        local tFile = fileOpen(path, true) 
        if tFile then 
            local size = fileGetSize (tFile) 
            if string.upper(type) == "KB" then 
            size = math.round(size / 1024, 2, "ceil") 
            elseif string.upper(type) == "MB" then 
            size = math.round(size / 1048576, 2, "ceil" 
           ) 
            end 
            fileClose(tFile) 
            return size; 
        else 
            return false; 
        end 
    end 
end 
  
addEvent("onDownloadFailure", true) 
addEvent("onDownloadComplete", true) 
addEvent("onDownloadPreStart", true) 

My Car Script :

-- Generated using GM2MC ( GTA:SA Models To MTA:SA Converter ) by SoRa 
mods = { 
{"buffalo.txd", 565, "txd"}, 
{"buffalo.dff", 565, "dff"} 
} 
  
addEventHandler('onClientResourceStart',resourceRoot,function () 
        for k, mod in ipairs(mods) do 
                if mod[3] == "txd" then 
                        if engineImportTXD( mod[1], mod[2]) == false then 
                                exports.download:downloadFile(mod[1], mod[1], 1000) 
                        end 
                elseif mod[3] == "dff" then 
                        if engineReplaceModel( mod[1], mod[2] ) == false then 
                                exports.download:downloadFile(mod[1], mod[1], 1000) 
                        end 
                end 
        end 
  
end) 
  
addEventHandler("onClientDownloadComplete", getRootElement(), 
        function (theFile) 
                for k, mod in ipairs(mods) do 
                        if string.find(theFile, mod[1]) then 
                                if mod[3] == "txd" then 
                                        engineImportTXD( mod[1], mod[2]) 
                                elseif mod[3] == "dff" then 
                                        engineReplaceModel( mod[1], mod[2] ) 
                                end 
                        end 
                end 
        end 
) 

And This is the Car Script's Meta

<?xml version="1.0" encoding="utf-8" standalone="yes"?> 

     "sss" type="misc" name="Aston" description="Aston" /> 
     

Link to comment

It doesn't have to be local, you can specify ip or even domain names. The only thing you have to worry about is the ping difference, but since mods are downloaded by request from the user it doesn't matter if it takes ~100 ms longer time to download the mod, it won't lock your thread or affect the game play anyway. I never tested it, just read it somewhere.

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