Jump to content

[HELP] Downloading multiple files from HTTP server


HandBuzz

Recommended Posts

Posted

 

Hey guys, so I created a script for my server so that players can download mod files from a HTTP server. I have a considerable amount of files and this is a more comfortable way of downloading them.

 Now, I figured it out so players can download a single file, with the following structure (stripped down):

function downloadMod ( name ) --name is correspondent to a clientside list
	if ( name and moddinglist[name] ) then
		local dff = moddinglist[name][1]
		local txd = moddinglist[name][2]
		if ( fileExists ( "mods/"..dff ) ) then			--overwrite old files
			fileDelete ( "mods/"..dff )
		end if ( fileExists ( "mods/"..txd ) ) then		--overwrite old files
			fileDelete ( "mods/"..txd )
		end
		
		downloadingFiles[name] = true  -- a little output so i let the player know what he downloads
		local f = fetchRemote ( httpModDirectory.."/"..dff, function ( data, err, name, dff, txd  )  
			if ( err == 0 ) then
			
				local f = fileCreate ( "mods/"..dff )
				fileWrite (  f, data ) 
				fileClose ( f )
				
				if ( moddinglist[name][4] ) then
					fetchRemote ( httpModDirectory.."/"..txd, function ( data, err, name, name2 ) 
						if ( err == 0 ) then
							local f = fileCreate ( "mods/"..txd )
							fileWrite ( f , data ) 
							fileClose ( f )
                  			-- some more error handling code here, unnecesary
                  			-- and so on

 

Now, as you can figure out i use an external file index with all the values and all that, so that a list can be created with each individual name and file names, and looks something like this

moddinglist = {
  	-- ["Name"] = { "file", "txdfile", vehID, isTXD }
 }

 Finally, I have a question. I want to setup the script so that players could download all the files listed all at once, how could I do that, without eating out all my server's resources?

  • Moderators
Posted
1 hour ago, HandBuzz said:

without eating out all my server's resources?

What do you mean with that? Are you saying that your scripts are also located in that direction?

 

Do you want to improve your Lua programming skills and make less mistakes?   Start with Lua Language Server!   🙀

 

  Useful functions  3x 

  Tutorials  4x 

 

Posted (edited)

I wanted to say something that wouldn't cause so much CPU usage, I know some methods may load all the mods at the time and are very laggy for that server as well as the players

I am not very good at efficient scripting, help is welcome

Edited by HandBuzz
Posted
33 minutes ago, IIYAMA said:

What do you mean with that? Are you saying that your scripts are also located in that direction?

 

I wanted to say something that wouldn't cause so much CPU usage, I know some methods may load all the mods at the time and are very laggy for that server as well as the players

I am not very good at efficient scripting, help is welcome

  • Moderators
Posted (edited)
27 minutes ago, HandBuzz said:

I wanted to say something that wouldn't cause so much CPU usage, I know some methods may load all the mods at the time and are very laggy for that server as well as the players

I am not very good at efficient scripting, help is welcome

If you are worrying about the internet usage. Get yourself a second server. Which can be a cloud server. This will reduce the impact on your main server.

 

 

As for the clients. The most laggy part is loading the mods.

You can queue processes like this:

(untested)

local nextModLoading = 0

local function processMods (modsInQueue)
  	local timeNow = getTickCount()
	if timeNow > nextModLoading then
		local newMod = modsInQueue[1]
		-- load mods
    	
		table.remove(modsInQueue, 1)
		nextModLoading = timeNow + 300
	end
	if #modsInQueue == 0 then
		removeRenderEvent(processMods)
	end
end

do 
	local modsInQueue = {}

	function addModToLoader (newMod)
		modsInQueue[#modsInQueue + 1] = newMod
		addRenderEvent(processMods, nil, modsInQueue)
	end
end

 

 

 

Edited by IIYAMA

Do you want to improve your Lua programming skills and make less mistakes?   Start with Lua Language Server!   🙀

 

  Useful functions  3x 

  Tutorials  4x 

 

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