AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Module Coding (https://forums.alliedmods.net/forumdisplay.php?f=9)
-   -   Module: Download (https://forums.alliedmods.net/showthread.php?t=151777)

Black Rose 02-27-2011 14:23

Module: Download
 
4 Attachment(s)
Update 2013-07-31: I removed the binary. It was a shitty module. If anyone is interested I'll leave the source, but have in mind it's much better to use sockets for three reasons. 1. Everyone has it. 2. It won't crash like this do. 3. It's easier to work with.

Please refer to my HTTP2 include instead of this module.

Download 1.0

Enables plugins to download files from http sources.
Basically a wrapper for URLDownloadToFile.
More info on that function:
http://msdn.microsoft.com/en-us/library/ms775123.aspx

Could enable, for example, auto-updating of plugins. (Not alone though)
Known problem:
When I placed 6+ downloads at server startup it would crash on me at restart. (Not first start) Reason: unknown. I'm not that good at C++... Either way, it basically works.

PHP Code:

/*
* native DownloadStart(const url[], const file[]);
*     Start the download of a file from http source.

* Arguments:

*     const url[]:
*         Source URL to download from.

*     const file[]:
*         Local filename to save to.
*         Can be used both with "C:\file.ext" or "/file.ext".
*         The latest will save the file under moddir.

* Returns:

*     On success:
*         Download Id
*             Integer 0-9
*     On fail:
*         -1
*             Error message displayed in console.

*     Note:    0-9 does not guarantee success.
*             If I was to make the module wait for the
*             status of the file, it will freeze the server.
*             You need to use the DownloadFailed forward.
*             If that forward is never called, successful.
* */
native DownloadStart(const url[], const file[]);


/*
* native DownloadAbort(Id);
*     Aborts a download.

* Arguments:

*     Id:
*         Download Id to abort.


* Returns:
*     0
* */
native DownloadAbort(Id);

/*
* forward DownloadDone(Id, KBDone, KBTotal);
*     This forward is called when any file is finished.

* Arguments:

*     Id:
*         Current Download Id.

*     KBDone:
*         KiBiBytes downloaded.

*     KBTotal
*         KiBiBytes total.

* Possible return values:
*     Return value is ignored.
* */
forward DownloadDone(IdKBDoneKBTotal);

/*
* forward Downloading(Id, BDone, BTotal);
*     This forward is called as long as any file is downloading.

* Arguments:

*     Id:
*         Current Download Id.

*     KBDone:
*         KiBiBytes downloaded.

*     KBTotal
*         KiBiBytes total.

* Possible return values:
*     Return value is ignored.
* */
forward Downloading(IdKBDoneKBTotal);

/*
* forward DownloadFailed(Id);
*     This forward is called when a download fail.
*     Reason is specified in console.

* Arguments:

*     Id:
*         The download Id that failed.

*     ErrorId:
*         The Id of the error, more information:
*         http://msdn.microsoft.com/en-us/library/ms775145.aspx

* Possible return values:
*     Return value is ignored.
* */
forward DownloadFailed(IdErrorId); 

Kind regards
Black Rose, also known here as [ --<-@ ] Black Rose or DivinityX

ot_207 02-27-2011 15:14

Re: Module: Download
 
Just lol, and gj.
Will test this.
Maybe a port to linux?

fmfs10 02-27-2011 15:26

Re: Module: Download
 
You can make the possibility of compress files and uncompress this. Or just the ability of uncompress files after download a compressed files. It would help a lot servers when downloading big files (like models)

EDIT@

You should add the ability to read file version url side too =P

fysiks 02-27-2011 16:25

Re: Module: Download
 
Is this supposed to be an alternative to FTP? I think I would always prefer FTP.

fmfs10 02-27-2011 17:29

Re: Module: Download
 
Quote:

Originally Posted by fysiks (Post 1425081)
Is this supposed to be an alternative to FTP? I think I would always prefer FTP.

I don't know but I think it can change the file. FTP can't delete a data =x

Black Rose 02-27-2011 17:37

Re: Module: Download
 
Quote:

Originally Posted by ot_207 (Post 1425041)
Just lol, and gj.
Will test this.
Maybe a port to linux?

I don't know anything about linux.
All my modules are based on Windows functions.

Quote:

Originally Posted by fmfs10 (Post 1425052)
You can make the possibility of compress files and uncompress this. Or just the ability of uncompress files after download a compressed files. It would help a lot servers when downloading big files (like models)

uncompress files is easily done with my ExecuteX plugin, combined win rar.exe (command line WinRAR). I might add it, but I wouldn't count on it.
As stated, I'm not that good at C++.
Quote:

Originally Posted by fmfs10 (Post 1425052)
You should add the ability to read file version url side too =P

I don't quite understand what you mean. You can download a website and parse it like any file.

Quote:

Originally Posted by fysiks (Post 1425081)
Is this supposed to be an alternative to FTP? I think I would always prefer FTP.

I don't understand. This works on ftp aswell.

fmfs10 02-27-2011 18:19

Re: Module: Download
 
Quote:

Originally Posted by Black Rose (Post 1425119)
I don't quite understand what you mean. You can download a website and parse it like any file.

If you want to update a file, you need to know the file version from the url (the updated) and the server (the possible outdated). So you need to compare the file version from url and from the server...

Black Rose 02-27-2011 18:54

Re: Module: Download
 
Quote:

Originally Posted by fmfs10 (Post 1425139)
If you want to update a file, you need to know the file version from the url (the updated) and the server (the possible outdated). So you need to compare the file version from url and from the server...

Nothing is impossible. There's several ways.
Start downloading file, check total file size, abort and then decide if you wanna download it is one way.
The best way of course would be if there was a php generated site to tell you only the information you want, download that. If that matches your current version, don't download the new. If it differs, download.
You could also download the file, load it as a separate plugin and then compare versions. Seems very unpractical though with so much downloading.



I made a small autoupdate example just to prove the point. This would not be good enough for a plugin to use. (So don't!) It just shows the method.

Current plugin:
PHP Code:

#include <amxmodx>
#include <Download>

#define MyPluginUrl "http://DivinityX.net/test2.amxx"

new g_downloadTo[260];

public 
plugin_init() {
    
    
register_plugin("Test 2""1.0""DivinityX");
    
    new 
filename[64];
    new 
pluginsdir[260];
    new 
modname[32];
    
    
// First we get the modname
    
get_modname(modname31);
    
    
// Next, we need plugins filename.
    
get_plugin (-1filenamecharsmax(filename), pluginsdircharsmax(pluginsdir), pluginsdircharsmax(pluginsdir), pluginsdircharsmax(pluginsdir), pluginsdircharsmax(pluginsdir));
    
    
// We also want to know where that file is stored. NO HARDCODING!
    
get_localinfo("amxx_pluginsdir"pluginsdircharsmax(pluginsdir))
    
    
// Format it up to fit into 1 param.
    
formatex(g_downloadTocharsmax(g_downloadTo), "%s/%s/%s"modnamepluginsdirfilename);
    
    
// Just wait.
    
set_task(10.0"UpdateMyPluginPlz");
    
server_print("The anticipation is killing me...");
}

public 
UpdateMyPluginPlz() {
    
// Download the file, overwriting the original.
    
    // If the server changed level while downloading
    // the original will be destroyed.
    
    // The safe way would be to download it to
    // another location and then move it.
    
DownloadStart(MyPluginUrlg_downloadTo);
    
    
server_print("Done");
    
// Well, not actually done.
    // You have to wait for it to download.
    // I used a small plugin for this example.


test2.amxx:
PHP Code:

#include <amxmodx>

public plugin_init() {
    
register_plugin("Test 2""1.0""DivinityX");
    
server_print("What do you know about auto-update?");


Running first plugin:
http://DivinityX.net/au1.png
After restart (mapchange would have the same effect):
http://DivinityX.net/au2.png

fmfs10 02-27-2011 18:57

Re: Module: Download
 
Yeah you are right =P

Sorry '-'

Exolent[jNr] 02-27-2011 19:04

Re: Module: Download
 
Advantages/disadvantages of using this instead of using the sockets module to do this manually, or even the HTTP Downloader plugin?


All times are GMT -4. The time now is 15:15.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.