Jump to content

Questions about fetchRemote


Bilal135

Recommended Posts

  • Moderators
10 hours ago, Lynch said:

Can you elaborate the process

Yes, but in parts, as every part has to be build with precision.

 

First try to get an understanding of the resource. https://wiki.multitheftauto.com/wiki/XmlData#Simplifying_the_export

 

Then create a table format that could be used for the file management. For example:

{
  [470] = {
   txd = "files/470/mod.txd",
   dff = "files/470/mod.dff"
  },
  [...] = {
   ...
  }
}

You must create some functions that can create and update your format.

 

I will give you more steps when this part is finished.

 

Note, you shouldn't save your mods inside of XML. The file will get too big too quickly.

 

 

Link to comment

I was working on it. Before your previous reply, this is the best I could come up with. It didn't work, nor did I expect it to. But I'd still like to know where am I going wrong with this. I'll look into the table format that you provided an example of.

local xml = exports.xmlData

addEvent("downloadNotDownloadedVehicles", true)
addEventHandler ( "downloadNotDownloadedVehicles" , resourceRoot, 
    function (modelDataList)
        for model, v in pairs(modelDataList) do
            print(model)
            local col = v.col
            if col then
                print(col)
                local col_loaded = engineLoadCOL (col)
                if col_loaded then
                    engineReplaceCOL (col_loaded, model )
                    outputChatBox("replacing col")
                    local colfile = fileCreate(model..".col")
                    fileWrite(colfile, col_loaded)
                end
            end
            
            local txd = v.txd
            if txd then
                print(txd)
                  local txd_loaded = engineLoadTXD(txd)
                  if txd_loaded then
                    engineImportTXD(txd_loaded, model)
                    outputChatBox("replacing txd")
                    local txdfile = fileCreate(model..".txd")
                    fileWrite(txdfile, txd_loaded)
                end
            end
            
            local dff = v.dff
            if dff then
                print(dff)
                local dff_loaded = engineLoadDFF(dff)
                if dff_loaded then
                    engineReplaceModel(dff_loaded, model)
                    outputChatBox("replacing dff")
                    local dfffile = fileCreate(model..".dff")
                    fileWrite(dfffile, dff_loaded)
                end
            end
            xml.xmlSaveData("DownloadedFiles", modelDataList, true)
        end
    end
)

function difference(a,b)
	-- This should put everything that is on table A and it isn't on table B into a new indexed table
	local ret = {}
	for k,v in pairs(a) do
		local insert = true
		for k2,v2 in pairs(b) do
			if v == v2 then
				insert = false
				break;
			end
		end
		if insert then
			table.insert(ret, v)
		end
	end
  	return ret
end

addEvent("downloadVehicle", true)
addEventHandler("downloadVehicle", resourceRoot,

function (notDownloadedFilesTableData)

    local downloadedFilesXML = xml.xmlLoadData("DownloadedFiles", true)
    local downloadedFilesTable = difference(downloadedFilesXML, modelDataList)
    local notDownloadedFilesTable = difference(downloadedFilesTable, modelDataList)
    local notDownloadedFilesXML = xml.xmlSaveData("notDownloadedFiles", true)

    for model, fileType in pairs(notDownloadedFilesTableData) do
        local col = v.col
        if col then
            print(col)
            local col_loaded = engineLoadCOL(model..".col")
            if col_loaded then
                engineReplaceCOL (col_loaded, model)
                outputChatBox("[downloadedFiles] replacing col")
            end
        end
        
        local txd = v.txd
        if txd then
            print(txd)
              local txd_loaded = engineLoadTXD(model..".txd")
              if txd_loaded then
                engineImportTXD(txd_loaded, model)
                outputChatBox("[downloadedFiles] replacing txd")
            end
        end
        
        local dff = v.dff
        if dff then
            print(dff)
            local dff_loaded = engineLoadDFF(model..".dff")
            if dff_loaded then
                engineReplaceModel(dff_loaded, model)
                outputChatBox("[downloadedFiles] replacing dff")
            end
        end

        triggerServerEvent("notDownloadedFilesData", source, notDownloadedFilesTable)

    end
end
)
local links = {
	{
		model = 560,
		txd = "https://drive.google.com/uc?export=download&id=1XIlsWiphME11AXW94cQGtOGJl4hMEX9V", 
		dff = "https://drive.google.com/uc?export=download&id=1exS-vr_kYp-tnD4xiCs8qW53zEAqT_S3"
	}
}

-- OUTPUT
local modelDataList = {}


-- 
local fileTypeLoadOrder = {
	"col",
	"txd",
	"dff"
}

do
	-- This is a temporary function. Only used for the initial state.
		local fileRequest = function (data, err, linkIndex, fileType) 
			if not err.success then print(err.statusCode) end
		
		outputChatBox("Download started", root)
		
		local link = links[linkIndex]
		local model = link.model
		
		-- make a new container if not exist 
		local modelData = modelDataList[model]
		if not modelData then
			modelData = {
				fileCountRemaining = link.fileCount
			}
			modelDataList[model] = modelData
		end
		
		-- save the data
		modelData.model = model
		modelData[fileType] = data
		
		modelData.fileCountRemaining = modelData.fileCountRemaining - 1
		
		-- clean up
		if modelData.fileCountRemaining == 0 then
			modelData.fileCountRemaining = nil
			modelData.ready = true
		end
	end
	
	
	-- Loop through all the links
	for i=1, #links do
		local link = links[i]
		
		-- Keep track of the amount of files that need to be downloaded
		local fileCount = 0
		
		-- Loop through all the file types in order.
		for j=1, #fileTypeLoadOrder do
			local fileType = fileTypeLoadOrder[j]
			local url = link[fileType]
			if url then -- If file type has an URL, then try to download it.
				fetchRemote(url, {}, fileRequest, {i, fileType})
				fileCount = fileCount + 1 -- + file
			end
		end
		link.fileCount = fileCount
	end
end

function difference(a,b)
	-- This should put everything that is on table A and it isn't on table B into a new indexed table
	local ret = {}
	for k,v in pairs(a) do
		local insert = true
		for k2,v2 in pairs(b) do
			if v == v2 then
				insert = false
				break;
			end
		end
		if insert then
			table.insert(ret, v)
		end
	end
  	return ret
end

addEvent("notDownloadedFilesData", true)
addEventHandler("notDownloadedFilesData", resourceRoot, function(notDownloadedFilesTable)
	 
	local notDownloadedFilesTableData = difference(notDownloadedFilesTable, modelDataList)

	triggerLatentClientEvent(source, "downloadVehicle", 10^7, true, resourceRoot, notDownloadedFilesTableData)
end)
	
	

addEventHandler('onPlayerJoin', root, function()
	triggerLatentClientEvent(source, 'downloadNotDownloadedVehicles', 10^7, true, resourceRoot, modelDataList) 
end) 

 

Edited by Lynch
Link to comment

What about this?

filePaths = {

	[560] = {
		txd = "files/470/sultan.txd",
		dff = "files/470/sultan.dff"
	},
	
	[411] = {
		txd = "files/411/infernus.txd",
		dff = "files/411/infernus.dff"
	}

}

function createFilePath(id, txdpath, dffpath)

	filePaths[id] = {
		txd = txdpath,
		dff = dffpath
	}

end

function updateFilePath(id, txdpath, dffpath)

	for k, v in ipairs(filePaths) do

		filePath[id].txd = txdpath
		filePath[id].dff = dffpath

	end

end

 

Link to comment
  • Moderators
5 hours ago, Lynch said:

What about this?

Loop in line 26 is not required.

Keep in mind that you might want to update 1 of the files instead of both.

When you update/create the filepaths you also need to save the files. As well as saving the filepaths in the xml saving resource.

 

Link to comment

@IIYAMA I did my best and this is what I came up with. Does not work. Outputs one error in debug. I added the mod files in meta.xml as well, should I do that or not?

Client.Lua:

----------------------- Tables ----------------------------------

filePaths = {

	[560] = {
        col = "files/560/mod.col",
		txd = "files/560/mod.txd",
		dff = "files/560/mod.dff"
	},
	
	[411] = {
        col = "files/411/mod.col",
		txd = "files/411/mod.txd",
		dff = "files/411/mod.dff"
	}

}

----------------------- File Functions --------------------------

function txdCreateFilePath(id, txdpath)

	filePaths[id] = {
		txd = txdpath
	}

end

function dffCreateFilePath(id, dffpath)

	filePaths[id] = {
		dff = dffpath
	}

end

function colCreateFilePath(id, colpath)

    filePaths[id] = {
        col = colpath
    }

end

function txdUpdateFilePath(id, txdpath)

	filePath[id].txd = txdpath

end

function dffUpdateFilePath(id, dffpath)

	filePath[id].dff = dffpath

end

function colUpdateFilePath(id, colpath)

    filePath[id].col = colpath

end

-----------------------------------------------------------------

local xml = exports.xmlData

addEventHandler("onClientResourceStart", resourceRoot,
    function ()

        triggerServerEvent("alreadyDownloadedFiles", source, filePaths)

        local xmlFilePaths = xml:xmlLoadData("downloadedFiles", true)

        for model, v in pairs(xmlFilePaths) do

            local col = v.col
            if col then
                local col_loaded = engineLoadCOL("files/"..model.."/mod.col")
                if col_loaded then
                    engineReplaceCOL(col_loaded, model)
                end
            end
            
            local txd = v.txd
            if txd then
                local txd_loaded = engineLoadTXD("files/"..model.."/mod.txd")
                if txd_loaded then
                    engineImportTXD(txd_loaded, model)
                end
            end
            
            local dff = v.dff
            if dff then
                local dff_loaded = engineLoadDFF("files/"..model.."/mod.dff")
                if dff_loaded then
                    engineReplaceModel(dff_loaded, model)
                end
            end

        end
    end
)

addEvent("downloadVehicle", true)
addEventHandler("downloadVehicle", resourceRoot,

function (modelDataList)

    for model, v in pairs(modelDataList) do

        local col = v.col
        if col then
            local col_loaded = engineLoadCOL("files/"..model.."/mod.col")
            if col_loaded then
                engineReplaceCOL (col_loaded, model)
                local file = fileCreate("files/"..model.."/mod.col")
                fileWrite(file, col_loaded)
                fileClose(file)
                colCreateFilePath(model, "files/"..model.."/mod.col")
                outputChatBox("[downloadedFiles] replacing col")
            end
        end
        
        local txd = v.txd
        if txd then
              local txd_loaded = engineLoadTXD("files/"..model.."/mod.txd")
              if txd_loaded then
                engineImportTXD(txd_loaded, model)
                local file = fileCreate("files/"..model.."/mod.txd")
                fileWrite(file, txd_loaded)
                fileClose(file)
                txdCreateFilePath(model, "files/"..model.."/mod.txd")
                outputChatBox("[downloadedFiles] replacing txd")
            end
        end
        
        local dff = v.dff
        if dff then
            local dff_loaded = engineLoadDFF("files/"..model.."/mod.dff")
            if dff_loaded then
                engineReplaceModel(dff_loaded, model)
                local file = fileCreate("files/"..model.."/mod.dff")
                fileWrite(file, dff_loaded)
                fileClose(file)
                txdCreateFilePath(model, "files/"..model.."/mod.dff")
                outputChatBox("[downloadedFiles] replacing dff")
            end
        end

        xml:xmlSaveData("downloadedFiles", filePaths, true)

    end
end
)

server.Lua:

local links = {
	{
		model = 560,
		txd = "https://drive.google.com/uc?export=download&id=1XIlsWiphME11AXW94cQGtOGJl4hMEX9V", 
		dff = "https://drive.google.com/uc?export=download&id=1exS-vr_kYp-tnD4xiCs8qW53zEAqT_S3"
	}
}

-- OUTPUT
local modelDataList = {}


-- 
local fileTypeLoadOrder = {
	"col",
	"txd",
	"dff"
}

do
	-- This is a temporary function. Only used for the initial state.
		local fileRequest = function (data, err, linkIndex, fileType) 
			if not err.success then print(err.statusCode) end
		
		outputChatBox("Download started", root)
		
		local link = links[linkIndex]
		local model = link.model
		
		-- make a new container if not exist 
		local modelData = modelDataList[model]
		if not modelData then
			modelData = {
				fileCountRemaining = link.fileCount
			}
			modelDataList[model] = modelData
		end
		
		-- save the data
		modelData.model = model
		modelData[fileType] = data
		
		modelData.fileCountRemaining = modelData.fileCountRemaining - 1
		
		-- clean up
		if modelData.fileCountRemaining == 0 then
			modelData.fileCountRemaining = nil
			modelData.ready = true
		end
	end
	
	
	-- Loop through all the links
	for i=1, #links do
		local link = links[i]
		
		-- Keep track of the amount of files that need to be downloaded
		local fileCount = 0
		
		-- Loop through all the file types in order.
		for j=1, #fileTypeLoadOrder do
			local fileType = fileTypeLoadOrder[j]
			local url = link[fileType]
			if url then -- If file type has an URL, then try to download it.
				fetchRemote(url, {}, fileRequest, {i, fileType})
				fileCount = fileCount + 1 -- + file
			end
		end
		link.fileCount = fileCount
	end
end

function difference(a,b)
	-- This should put everything that is on table A and it isn't on table B into a new indexed table
	local ret = {}
	for k,v in pairs(a) do
		local insert = true
		for k2,v2 in pairs(b) do
			if v == v2 then
				insert = false
				break;
			end
		end
		if insert then
			table.insert(ret, v)
		end
	end
  	return ret
end

-- Server call back / client trigger

addEvent("alreadyDownloadedFiles", true)
addEventHandler("alreadyDownloadedFiles", resourceRoot,
	function(filePaths)

		local notDownloadedFiles = difference(modelDataList, filePaths)

        modelDataList = notDownloadedFiles

		triggerLatentClientEvent(source, "downloadVehicle", 10^7, true, resourceRoot, modelDataList)

	end
) 

meta.xml:

<meta>

<script src="server.Lua" type="server"/>
<script src="client.Lua" type="client"/>

<file src="files/560/mod.txd" download="false"/> -- I later removed these two files, still no effect.
<file src="files/560/mod.dff" download="false"/>

<min_mta_version>1.5.4-9.11342</min_mta_version>

</meta>

Error in debug:

vOfdah.png

I do not know where to use the update functions. Not sure what I did above is even correct. 

Edited by Lynch
Link to comment
  • Moderators

 

3 hours ago, Lynch said:

Error in debug:

Compare filesizes, best way to validate if the file is incorrect.

 

3 hours ago, Lynch said:

Not sure what I did above is even correct. 

There is only 1 way to figure that out and that is to debug every step. And every variable that plays an important role.

 

 

The update function is used to update the file, when you have replaced the old one with with a new one. This is something for the future.

 

 

  • Line 70 is using the filepaths that are the old ones. Line 72 is delivering the new ones.
  • Meta doesn't make use of include tag.
    <include resource="xmlData" />
  • Line 65 it's value should be set after onClientResourceStart.

 

Edited by IIYAMA
Link to comment

I removed the files from the meta. Added the update functions.

addEvent("alreadyDownloadedFiles", true)
addEventHandler("alreadyDownloadedFiles", resourceRoot,
	function(filePaths)

		local notDownloadedFiles = difference(modelDataList, filePaths)

		modelDataList = notDownloadedFiles

		triggerLatentClientEvent(source, "downloadVehicle", 10^7, true, resourceRoot, modelDataList)

	end
) 

Tried debugging this code. 'notDownloadedFiles' does not return a usable value. Therefore, nothing is sent to the client with the triggerLatentClientEvent. That results in the files not being created in the first place, and thus, the error in debug, 'Error loading DFF' and 'Error loading TXD'.

Its probably because modelDataList contains data and not the file paths while filePaths contains the file paths, and comparing them with 'difference' function does not return the values that are needed. So, I assume that another filePaths table has to be created server side, containing all the file paths of the files which are going to be downloaded. Then, use xmlLoadData to retrieve the client sided filePaths in server side. Comparing the two of them would give us the file paths of the files which are not downloaded in the client side, exactly what we need. But how does one use those file paths to trigger their download from links table? That is what I can't figure out.

Is my approach correct? @IIYAMA 

Link to comment
  • Moderators
14 hours ago, Lynch said:

Comparing the two of them would give us the file paths of the files which are not downloaded in the client side, exactly what we need. But how does one use those file paths to trigger their download from links table? That is what I can't figure out.

Is my approach correct? @IIYAMA 

You do not have to use filepaths serverside, as there are URL's. But you do need something to compare the downloaded files with the (serverside) URL's. That is when you need an identifier. You could use the URL for that, as it is always unique.

Your approach sounds correctly.

Link to comment

@IIYAMA getting an error here,

local txd = v.txd
        if txd then
              local txd_loaded = engineLoadTXD(txd)
              if txd_loaded then
                engineImportTXD(txd_loaded, model)
                local file = fileCreate("files/"..model.."/mod.txd")
                fileWrite(file, txd_loaded)
                fileClose(file)
                txdCreateFilePath(model, "files/"..model.."/mod.txd")
                outputChatBox("[downloadedFiles] replacing txd")
            end
        end

Bad argument fileWrite, expected string at argument 2, got txd. Getting errors for cols, txds and dffs.

Link to comment

I've changed a few things. It outputs the following error,

'Error: \client.Lua:50: Error loading TXD @ 'engineLoadTXD' [files/560/mod.txd]'

Client.Lua:

----------------------- Tables ----------------------------------

filePaths = {}

----------------------- File Functions --------------------------

function txdCreateFilePath(id, txdpath)
	filePaths[id] = {
		txd = txdpath
	}
end

function dffCreateFilePath(id, dffpath)
	filePaths[id] = {
		dff = dffpath
	}
end

function colCreateFilePath(id, colpath)
    filePaths[id] = {
        col = colpath
    }
end

function txdUpdateFilePath(id, txdpath)
	filePath[id].txd = txdpath
end

function dffUpdateFilePath(id, dffpath)
	filePath[id].dff = dffpath
end

function colUpdateFilePath(id, colpath)
    filePath[id].col = colpath
end

-----------------------------------------------------------------

addEventHandler("onClientResourceStart", resourceRoot,
    function ()
        local xml = exports.xmlData
        local xmlFilePaths = xml:xmlLoadData("downloadedFiles", true)
        if not xmlFilePaths then
            xml:xmlSaveData("downloadedFiles", filePaths, true)
            xmlFilePaths = xml:xmlLoadData("downloadedFiles", true)
        end
        for model, v in pairs(xmlFilePaths) do 
            local txd = v.txd
            if txd then
                local txd_loaded = engineLoadTXD("files/"..model.."/mod.txd")
                if txd_loaded then
                    engineImportTXD(txd_loaded, model)
                    txdUpdateFilePath(model, "files/"..model.."/mod.txd")
                end
            end
            local dff = v.dff
            if dff then
                local dff_loaded = engineLoadDFF("files/"..model.."/mod.dff")
                if dff_loaded then
                    engineReplaceModel(dff_loaded, model)
                    txdUpdateFilePath(model, "files/"..model.."/mod.dff")
                end
            end
            local col = v.col
            if col then
                local col_loaded = engineLoadCOL("files/"..model.."/mod.col")
                if col_loaded then
                    engineReplaceCOL(col_loaded, model)
                    colUpdateFilePath(model, "files/"..model.."/mod.col")
                end
            end
        end
        triggerServerEvent("alreadyDownloadedFiles", resourceRoot, xmlFilePaths)
    end
)

addEvent("downloadVehicle", true)
addEventHandler("downloadVehicle", resourceRoot,
function (modelDataList)
    for model, v in pairs(modelDataList) do  
        local txd = v.txd
        if txd then
              local txd_loaded = engineLoadTXD(txd)
              if txd_loaded then
                engineImportTXD(txd_loaded, model)
                local file = fileCreate("files/"..model.."/mod.txd")
                fileClose(file)
                txdCreateFilePath(model, "files/"..model.."/mod.txd")
                outputChatBox("[downloadedFiles] replacing txd")
            end
        end
        
        local dff = v.dff
        if dff then
            local dff_loaded = engineLoadDFF(dff)
            if dff_loaded then
                engineReplaceModel(dff_loaded, model)
                local file = fileCreate("files/"..model.."/mod.dff")
                fileClose(file)
                txdCreateFilePath(model, "files/"..model.."/mod.dff")
                outputChatBox("[downloadedFiles] replacing dff")
            end
        end
        local col = v.col
        if col then
            local col_loaded = engineLoadCOL(col)
            if col_loaded then
                engineReplaceCOL (col_loaded, model)
                local file = fileCreate("files/"..model.."/mod.col")
                fileClose(file)
                colCreateFilePath(model, "files/"..model.."/mod.col")
                outputChatBox("[downloadedFiles] replacing col")
            end
        end
        exports.xmlData:xmlSaveData("downloadedFiles", filePaths, true)
    end
end
)

Server.Lua:

local links = {
	{
		model = 560,
		txd = "https://drive.google.com/uc?export=download&id=1XIlsWiphME11AXW94cQGtOGJl4hMEX9V", 
		dff = "https://drive.google.com/uc?export=download&id=1exS-vr_kYp-tnD4xiCs8qW53zEAqT_S3"
	}
}

-- OUTPUT
local modelDataList = {}

--
local fileTypeLoadOrder = {
	"txd",
	"dff",
	"col"
}

function difference(a,b)
	-- This should put everything that is on table A and it isn't on table B into a new indexed table
	local ret = {}
	for k,v in pairs(a) do
		local insert = true
		for k2,v2 in pairs(b) do
			if k == k2 then
				insert = false
				break;
			end
		end
		if insert then
			table.insert(ret, a[k].model)
			table.insert(ret, a[k].txd)
			table.insert(ret, a[k].dff)
		end
	end
  	return ret
end

-- Server call back / client trigger

addEvent("alreadyDownloadedFiles", true)
addEventHandler("alreadyDownloadedFiles", resourceRoot,
	function(xmlFilePaths)
		local notDownloadedFiles = difference(links, xmlFilePaths)
		if notDownloadedFiles then
			local fileRequest = function(data, err, linkIndex, fileType) 
				if not err.success then
					print(err.statusCode)
				end
			    outputChatBox("Download started", root)
				local link = notDownloadedFiles[linkIndex]
				local model = link.model
				local modelData = modelDataList[model]
				if not modelData then
					modelData = {
						fileCountRemaining = link.fileCount
					}
					modelDataList[model] = modelData
				end
				modelData.model = model
		        modelData[fileType] = data
		        modelData.fileCountRemaining = modelData.fileCountRemaining - 1
		        if modelData.fileCountRemaining == 0 then
			        modelData.fileCountRemaining = nil
			        modelData.ready = true
		        end
		    end
	        for i=1, #notDownloadedFiles do
		        local link = notDownloadedFiles[i]
		        local fileCount = 0
		        for j=1, #fileTypeLoadOrder do
			        local fileType = fileTypeLoadOrder[j]
			        local url = link[fileType]
			        if url then
				        fetchRemote(url, {}, fileRequest, {i, fileType})
				        fileCount = fileCount + 1
			        end
		        end
		        link.fileCount = fileCount
	        end
		else return
		end
	end
)

addEventHandler("onPlayerJoin", root,
	function()
		triggerLatentClientEvent(source, "downloadVehicle", 10^7, true, resourceRoot, modelDataList)
	end
)

 

Link to comment

Now its downloading already downloaded vehicles and all vehicles every time. I changed quite a lot of stuff.

local links = {
	{
		model = 560,
		txd = "https://drive.google.com/uc?export=download&id=1XIlsWiphME11AXW94cQGtOGJl4hMEX9V", 
		dff = "https://drive.google.com/uc?export=download&id=1exS-vr_kYp-tnD4xiCs8qW53zEAqT_S3"
	},
	{
		model = 596,
		txd = "https://drive.google.com/uc?export=download&id=15zbo6U9wxVBzZHTkrM8wTlDDMfasWJn1",
		dff = "https://drive.google.com/uc?export=download&id=1qyJC2wAD7QAmvS29IDgIXMwf7vqVeFVy"
    }
}

-- OUTPUT
local modelDataList = {}
local modelDataListAll = {}

--
local fileTypeLoadOrder = {
	"txd",
	"dff",
	"col"
}

function difference(a,b)
	-- This should put everything that is on table A and it isn't on table B into a new indexed table
	local ret = {}
	for k,v in pairs(a) do
		local insert = true
		for k2,v2 in pairs(b) do
			if k == k2 then
				insert = false
				break;
			end
		end
		if insert then
			table.insert(ret, v)
		end
	end
  	return ret
end

-- Server call back / client trigger

addEvent("alreadyDownloadedFiles", true)
addEventHandler("alreadyDownloadedFiles", resourceRoot,
	function(xmlFilePaths)
		local notDownloadedFiles = difference(links, xmlFilePaths)
		for k, v in pairs(notDownloadedFiles) do
			outputChatBox(""..k.."", root)
		end
		if notDownloadedFiles then
			local fileRequest = function(data, err, linkIndex, fileType) 
				if not err.success then
					print(err.statusCode)
				end
			    outputChatBox("Download started", root)
				local link = notDownloadedFiles[linkIndex]
				local model = link.model
				local modelData = modelDataList[model]
				if not modelData then
					modelData = {
						fileCountRemaining = link.fileCount
					}
					modelDataList[model] = modelData
				end
				modelData.model = model
		        modelData[fileType] = data
		        modelData.fileCountRemaining = modelData.fileCountRemaining - 1
		        if modelData.fileCountRemaining == 0 then
			        modelData.fileCountRemaining = nil
			        modelData.ready = true
		        end
		    end
	        for i=1, #notDownloadedFiles do
		        local link = notDownloadedFiles[i]
		        local fileCount = 0
		        for j=1, #fileTypeLoadOrder do
			        local fileType = fileTypeLoadOrder[j]
			        local url = link[fileType]
			        if url then
				        fetchRemote(url, {}, fileRequest, {i, fileType})
				        fileCount = fileCount + 1
			        end
		        end
		        link.fileCount = fileCount
	        end
		else return outputChatBox("Function is being returned", root)
		end
	end
)

addEvent("downloadAllFiles", true)
addEventHandler("downloadAllFiles", root,
	function()
		local fileRequest = function(data, err, linkIndex, fileType) 
			if not err.success then
				print(err.statusCode)
			end
			outputChatBox("[No previous files found] Download started", root)
			local link = links[linkIndex]
			local model = link.model
			local modelData = modelDataListAll[model]
			if not modelData then
				modelData = {
					fileCountRemaining = link.fileCount
				}
				modelDataListAll[model] = modelData
			end
			modelData.model = model
			modelData[fileType] = data
			modelData.fileCountRemaining = modelData.fileCountRemaining - 1
			if modelData.fileCountRemaining == 0 then
				modelData.fileCountRemaining = nil
				modelData.ready = true
			end
		end
		for i=1, #links do
			local link = links[i]
			local fileCount = 0
			for j=1, #fileTypeLoadOrder do
				local fileType = fileTypeLoadOrder[j]
				local url = link[fileType]
				if url then
					fetchRemote(url, {}, fileRequest, {i, fileType})
					fileCount = fileCount + 1
				end
			end
			link.fileCount = fileCount
		end
	end
)


addEventHandler("onPlayerJoin", root,
	function()
		if not modelDataList or not modelDataListAll then return end
		if modelDataList then
		    triggerLatentClientEvent(source, "downloadVehicle", 10^7, true, resourceRoot, modelDataList)
		elseif modelDataListAll then
			triggerLatentClientEvent(source, "downloadVehicle_2", 10^7, true, resourceRoot, modelDataListAll)
		end
	end
)
----------------------- Tables ----------------------------------

filePaths = {

	[560] = {
        col = "files/560/mod.col",
		txd = "files/560/mod.txd",
		dff = "files/560/mod.dff"
    }

}

----------------------- File Functions --------------------------

function txdCreateFilePath(id, txdpath)
	filePaths[id] = {
		txd = txdpath
	}
end

function dffCreateFilePath(id, dffpath)
	filePaths[id] = {
		dff = dffpath
	}
end

function colCreateFilePath(id, colpath)
    filePaths[id] = {
        col = colpath
    }
end

function txdUpdateFilePath(id, txdpath)
	filePaths[id].txd = txdpath
end

function dffUpdateFilePath(id, dffpath)
	filePaths[id].dff = dffpath
end

function colUpdateFilePath(id, colpath)
    filePaths[id].col = colpath
end

-----------------------------------------------------------------

addEventHandler("onClientResourceStart", resourceRoot,
    function ()
        local xml = exports.xmlData
        local xmlFilePaths = xml:xmlLoadData("downloadedFiles_10", true)
        if not xmlFilePaths then 
            return triggerEvent("downloadAllFiles_c", resourceRoot)
        end
        for model, v in pairs(xmlFilePaths) do 
            local txd = v.txd
            if txd then
                local txd_loaded = engineLoadTXD("files/"..model.."/mod.txd")
                if txd_loaded then
                    engineImportTXD(txd_loaded, model)
                    txdUpdateFilePath(model, "files/"..model.."/mod.txd")
                    outputChatBox("[old files] replacing txd")
                end
            end
            local dff = v.dff
            if dff then
                local dff_loaded = engineLoadDFF("files/"..model.."/mod.dff")
                if dff_loaded then
                    engineReplaceModel(dff_loaded, model)
                    txdUpdateFilePath(model, "files/"..model.."/mod.dff")
                end
            end
            local col = v.col
            if col then
                local col_loaded = engineLoadCOL("files/"..model.."/mod.col")
                if col_loaded then
                    engineReplaceCOL(col_loaded, model)
                    colUpdateFilePath(model, "files/"..model.."/mod.col")
                end
            end
        end
        triggerServerEvent("alreadyDownloadedFiles", resourceRoot, xmlFilePaths)
    end
)

addEvent("downloadAllFiles_c", true)
addEventHandler("downloadAllFiles_c", root,
    function()
        exports.xmlData:xmlSaveData("downloadedFiles_10", filePaths, true)
        triggerServerEvent("downloadAllFiles", resourceRoot)
    end
)

addEvent("downloadVehicle", true)
addEventHandler("downloadVehicle", resourceRoot,
function (modelDataList)
    for model, v in pairs(modelDataList) do  
        local txd = v.txd
        if txd then
            local file = fileCreate("files/"..model.."/mod.txd")
            fileWrite(file, txd)
            fileClose(file)
            local txd_loaded = engineLoadTXD(txd)
            if txd_loaded then
                engineImportTXD(txd_loaded, model)
                txdCreateFilePath(model, "files/"..model.."/mod.txd")
                outputChatBox("[downloadedFiles] replacing txd")
            end
        end
        
        local dff = v.dff
        if dff then
            local file = fileCreate("files/"..model.."/mod.dff")
            fileWrite(file, dff)
            fileClose(file)
            local dff_loaded = engineLoadDFF(dff)
            if dff_loaded then
                engineReplaceModel(dff_loaded, model)
                txdCreateFilePath(model, "files/"..model.."/mod.dff")
                outputChatBox("[downloadedFiles] replacing dff")
            end
        end
        local col = v.col
        if col then
            local file = fileCreate("files/"..model.."/mod.col")
            fileWrite(file, col)
            fileClose(file)
            local col_loaded = engineLoadCOL(col)
            if col_loaded then
                engineReplaceCOL (col_loaded, model)
                colCreateFilePath(model, "files/"..model.."/mod.col")
                outputChatBox("[downloadedFiles] replacing col")
            end
        end
        exports.xmlData:xmlSaveData("downloadedFiles_10", filePaths, true)
    end
end
)

addEvent("downloadVehicle_2", true)
addEventHandler("downloadVehicle_2", resourceRoot,
function (modelDataListAll)
    for model, v in pairs(modelDataListAll) do  
        local txd = v.txd
        if txd then
            local file = fileCreate("files/"..model.."/mod.txd")
            fileWrite(file, txd)
            fileClose(file)
            local txd_loaded = engineLoadTXD(txd)
            if txd_loaded then
                engineImportTXD(txd_loaded, model)
                txdCreateFilePath(model, "files/"..model.."/mod.txd")
                outputChatBox("[downloadedFiles] replacing txd")
            end
        end
        
        local dff = v.dff
        if dff then
            local file = fileCreate("files/"..model.."/mod.dff")
            fileWrite(file, dff)
            fileClose(file)
            local dff_loaded = engineLoadDFF(dff)
            if dff_loaded then
                engineReplaceModel(dff_loaded, model)
                txdCreateFilePath(model, "files/"..model.."/mod.dff")
                outputChatBox("[downloadedFiles] replacing dff")
            end
        end
        local col = v.col
        if col then
            local file = fileCreate("files/"..model.."/mod.col")
            fileWrite(file, col)
            fileClose(file)
            local col_loaded = engineLoadCOL(col)
            if col_loaded then
                engineReplaceCOL (col_loaded, model)
                colCreateFilePath(model, "files/"..model.."/mod.col")
                outputChatBox("[downloadedFiles] replacing col")
            end
        end
        exports.xmlData:xmlSaveData("downloadedFiles_10", filePaths, true)
    end
end
)




--[[local txd = v.txd
local dff = v.dff
engineImportTXD(engineLoadTXD(txd), model) 
engineReplaceModel(engineLoadDFF(dff), model)

        if data == "txd" then
            engineImportTXD(engineLoadTXD(txd), model) 
        elseif data == "dff" then
            engineReplaceModel(engineLoadDFF(dff), model)

--]]

 

Link to comment
  • Moderators
59 minutes ago, Lynch said:

Now its downloading already downloaded vehicles and all vehicles every time

 

Don't use "onPlayerJoin", for downloading.

  1. Client checks for files.
  2. client > server Tells the server which files there are already.
  3. The server compares the files.
  4. The server downloads the files from the external server.  (Caching files for a 1 minute?)
  5. server > client The server sends the files back.
  6. Client loads the mods either from already downloaded files or directly from the RAW data.
    Best to load the mods that are already downloaded at this moment. As you might want to update mods in the future.

 

There is no onPlayerJoin in that cycle.

 

 

 

  • Thanks 1
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...