Jump to content

[REL]Download functions for scripters


knash94

Recommended Posts

Download functions

Ever wanted to make background downloads? Such as a car model but without making your client downloading that model as soon as he joins the server and delay his play? With these download functions you can make it possible to download such models in the background without any need of any meta config, just place the file into the resource and make a script using these new functions to make this possible.

Resource requires permissions in the admin acl so it can read the files, simple add object "resource.download" into your admin group acl.

Client functions:

downloadFile (string fileFromServer, string saveAs, int interval) 
-- Interval is how long between each time the server sends the client some data, I'd reccomend this being at about 200 to avoid lagging the server. 
  
getClientFileSize (string theFile, string type) 
-- Types: "MB", "KB", if blank will return bytes. 

Server functions:

getServerFileSize (string theFile, string type) 
-- Types: "MB", "KB", if blank will return bytes. 

Server events:

onDownloadFail 
--Parameters: String serverFile, String clientFile 
  
onDownloadComplete 
--Parameters: String serverFile, String clientFile 
  
onDownloadPreStart 
--Parameters: string clientFile, int interval, bytes fileSize, int transferTimes 

Client events:

onClientDownloadPreStart 
--Parameters: string clientFile, int interval, bytes fileSize, int transferTimes 
  
onClientDownloadComplete 
--Parameters: string clientFile 
  
onClientDownloadFail 
--Parameters: String serverFile, String clientFile 

Example of usage.

  
addEventHandler("onClientResourceStart", getResourceRootElement(getThisResource()), 
function () 
exports.download:downloadFile("sometexture.txd", "infernus.txd", 500) 
end 
  
addEventHandler("onClientDownloadComplete", getRootElement(), 
function (theFile) 
-- Right now I made a mistake which I'll fix later which makes it so you see the whole path of the file, for example :/your-res/infernus.txd instead of infernus.txd 
if theFile == ":/your-res/infernus.txd" then 
     local theTXD = engineLoadTXD ( theFile ) 
     engineImportTXD ( theTXD, 411 ) 
end 
end 
  

Please report any issues you have with my script here please, I'll try as quick as I can to fix it ! :)

Download link.

Edited by Guest
Link to comment

Thanks for the comments guys, please let me know if you come across any errors. Also on the return of onClientDownloadComplete do you think it should return something like ":/your-res/infernus.txd" or "infernus.txd", in my opinion with it returning the resource name within it could be handy.

Link to comment

I have some questions.

1. I need place download.zip in my resource.zip?

2. I need in my resource.zip add lua file with this code :

addEventHandler("onClientResourceStart", getResourceRootElement(getThisResource()), 
function () 
exports.download:downloadFile("sometexture.txd", "infernus.txd", 500) 
end 
  
addEventHandler("onClientDownloadComplete", getRootElement(), 
function (theFile) 
-- Right now I made a mistake which I'll fix later which makes it so you see the whole path of the file, for example :/your-res/infernus.txd instead of infernus.txd 
if theFile == ":/your-res/infernus.txd" then 
     local theTXD = engineLoadTXD ( theFile ) 
     engineImportTXD ( theTXD, 411 ) 
end 
end 

?

Sure i will change this code for my script :)

Link to comment

Thanks Daniel,

Eht1, Yeah place it in resources and start it up. Whatever code you make, it doesn't have to be used in this resource. You can make a seperate resource and call the functions via that by simple putting exports.download:functionName(args), this was just an example but you can use it as a reference.

Link to comment

This not work there is code for my car :

addEventHandler("onClientResourceStart", getResourceRootElement(getThisResource()), 
function () 
exports.download:downloadFile("infernus.txd", "infernus.txd", 411) 
exports.download:downloadFile("infernus.dff", "infernus.dff", 411) 
end 
  
addEventHandler("onClientDownloadComplete", getRootElement(), 
function (theFile) 
if theFile == ":/infernus_play4/infernus.txd" then 
     local theTXD = engineLoadTXD ("infernus.txd", 411 ) 
     engineImportTXD ( theTXD, 411 ) 
if theFile == ":/infernus_play4/infernus.dff" then 
     local theTXD = engineLoadTXD ("infernus.dff", 411 ) 
     engineImportTXD ( theTXD, 411 ) 
end 
addEventHandler ( "onClientResourceStart", getResourceRootElement(getThisResource()), replaceModel) 
  

Link to comment
I had got the same problem but now it's giving a bad argumant @ "fileOpen", in the resource download

mayby you could make a example script and upload it on the community ;-)

you will need to add object "resource.download" into the admin group acl, tell me if it isn't still working after this.

I just realised the event might be a bit dodgey (onDownloadComplete), I'll fix it up tommorow, I should of tested this out a bit more before releasing tbh :roll:

Link to comment
  • 11 months later...
  • 1 year later...

Sorry for posting in an old thread but can somebody see what's wrong here?

  
addEventHandler ("onClientResourceStart", getResourceRootElement(getThisResource ()), 
function () 
 exports.download:downloadFile ("infernus.txd", "infernus.txd", 500) 
 exports.download:downloadFile ("infernus.dff", "infernus.dff", 500) 
end) 
  
addEventHandler ("onClientDownloadComplete", getRootElement(), 
function (theFile) 
if theFile == ":/test/cars/infernus.txd" then 
  local theTXD = engineLoadTXD ( theFile ) 
  engineImportTXD ( theTXD, 411 ) 
elseif theFile == ":/test/cars/infernus.dff" then 
  local theDFF = engineLoadDFF ( theFile, 411 ) 
  engineReplaceModel ( theDFF, 411) 
end 
 end) 

Link to comment
Guest
This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...