Jump to content

fileOpen while downloadFile


XJMLN

Recommended Posts

Hi, 

I tried to use fileOpen (c-side) when downloadFile is started, but it doesn't work in db3: Bad usage @ 'fileOpen' [unable to load file 'dff/vehicles/542.dff'] etc. but file is in this dir.

Someone maybe know where problem is? :D 

 





function replac_getAllSize(d)
    local size = 0  
    for i,v in ipairs(d) do 
        local t 
        local t2
        if v[2] == 1 then 
            t = "txd/vehicles/"..v[1]..".txd"
            t2 = "dff/vehicles/"..v[1]..".dff"
        elseif v[2] == 2 then
            t = "txd/skins/"..v[1]..".txd"
            t2 = "dff/skins/"..v[1]..".dff"
        end
        if fileExists(t2) then
            local openFile = fileOpen(t2,true)
            if openFile then 
                size = size + fileGetSize(openFile)
                fileClose(openFile)
            else 
            end
        else
        end
        if fileExists(t) then 
            local openFile2 = fileOpen(t,true) 
            if openFile2 then 
                size = size + fileGetSize(openFile2)
                fileClose(openFile2)
            else 
            end
        else
        end
    end
    return size/1048576 
end



function calcProg(w, p)
    if w and p then 
        local w = w -2 
        local width = ((w*p)/100)
        return width 
    end
    return 0 
end

function replac_render()
    if (not r.rendered) then return end 
    local size = replac_getAllSize(r.installed)
    dxDrawRectangle(224, 512, 362, 36, tocolor(0, 0, 0, 209), false)
    dxDrawRectangle(230, 517, 350, 25, tocolor(255, 255, 255, 255), false)
    local progress = ((size*100)/r.sizeAll)
    local x,y,w,h = (230/800)*sw, (517/600)*sh, (350/800)*sw, (25/600)*sh 
    local width = calcProg(w,progress)
    dxDrawRectangle(x,y,width,h, tocolor(104, 215, 89, 255), false)
    dxDrawText("Trwa pobieranie "..string.format("%.02f",size).."MB/"..string.format("%.02f",r.sizeAll).."MB.. ("..math.floor(progress).."%)", 230, 517, 580, 542, tocolor(0, 0, 0, 254), 1.00, "clear", "center", "center", false, false, false, false, false)
end

function replac_applyChanges(installed,uninstalled)
    r.rendered = true 
    r.sizeAll = 0
    for i,v in ipairs(installed) do
        if v[2] == 1 then 
            t = "vehicles"
        elseif v[2] == 2 then
            t = "skins"
        end
        r.sizeAll = r.sizeAll + v[3]
        local txd = downloadFile("txd/"..t.."/"..v[1]..".txd")
        local dff = downloadFile("dff/"..t.."/"..v[1]..".dff")
    end
    r.installed = installed 
    addEventHandler("onClientRender",root,replac_render)
end
Link to comment

So the only way I could work around is creating a server-sided script. On startUp calculating the filesizes, saving it into a variable. Then whenever the client joins, the client-side requests that variable (via events). If you need an example of the script.

local fileList = {"test.dff"}
local allSizes = 0

function calcAll()
	for i, k in pairs(fileList) do
		if fileExists(k) then
			allSizes = allSizes + fileGetSize(fileOpen(k))
		end
	end
end
calcAll()

function requestAll(requestClient)
	triggerClientEvent(requestClient, "startDownload", root, allSizes)
end
addEvent("requestSize", true)
addEventHandler("requestSize", root, requestAll)
local fileList = {"test.dff"}

function startDownload(fileSizes)
	outputChatBox("All size: " .. fileSizes)
	for i, k in pairs(fileList) do
		downloadFile(k)
	end
end
addEvent("startDownload", true)
addEventHandler("startDownload", root, startDownload)

triggerServerEvent("requestSize", root, localPlayer)

 

Link to comment

Yup, i use that version to get size of file on server :/ , but now i need size from client file to show something like 60MB/120MB (50%). It's something weird because i use (probably) the same method from this resource and in that resource it works perfectly..

Edited by XJMLN
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...