Blockland Forums > Modification Help
[HowTo] Download a file using torque
(1/2) > >>
rudyman:
Using torque to download info from a site is an extremely valuable feature, especially for modding.

You can download any file as text using torque with a TCPObject or an HTTPObject.

Let's say we want to download a file online to get the latest version for a mod.
First, create your object. I'm gunna use an HTTPObject for simplicity's sake.
I'll also put everything in a checkLatestVersion() function.

--- Code: ---function checkLatestVersion()
{
new HTTPObject(getLatestVersion);
}

--- End code ---
Now we want to connect. Before we do that, we need to figure out the file we're gunna download. Let's say it's google.com/latestVersion.txt.

When we connect, the server needs to be the first argument (in this case, it's google.com:80). I added :80 because that's the default port for http.

The file name is in a seperate argument (in this case, it's /latestVersion.txt).
Here's our new code:

--- Code: ---funtion checkLatestVersion()
{
new HTTPObject(getLatestVersion);
getLatestVersion.get("google.com:80","/latestVersion.txt");
}

--- End code ---

Okay. So now we're connected to the file and downloading.
By default, torque downloads one line at a time. To access each line, you need to create your object::onLine() function. It has 1 argument: newly downloaded lines.


--- Code: ---function getLatestVersion::onLine(%this,%line)
{
if(firstWord(%line)) $= "Version")
{
%latestVersion=restWords(%line); //We have the latest version! Let's see if the client's version is old:
if($pref::currentVersion < %latestVersion)
{
update();
}
}
}
--- End code ---

If you want to stay connected to a server, like if you wanted to connect to an IRC channel, you would use a TCPObject.

It works a little bit differently than the HTTPObject.
To connect, you use MyTCPobj.connect("url.com:80");
Then:

--- Code: ---function MyTCPobj::onConnected(%this)
{
$file="myFileHere.txt";
$url="url.com:80";
%this.send("GET "@$file@" HTTP/1.0\nHost: " @$url@"\nUser-Agent: Torque\n\r\n\r\n");
}

--- End code ---

And now you can finish off with the MyTCPobj::onLine() function, just like the HTTPobject one.

**NOTE**: When downloading files, torque does not read the last line, so it's important you include an extra line at the bottom.
Oxcorp:
Great. Now people will be making scripts that download password-sayers to your AoT folder.
DarkKnight:
hay, I gave you that code but you edited it :O

checkLatestVersion(); and other stuff gives it away.
-=>RR<=-MasterCE:

It's my old Bash Quote Gui, could you convert it to Retail for me :( I tried directly placing it in the add-on's, loading it, and editing as needed, but it crashed the game on load :/
rudyman:

--- Quote from: DarkKnight on March 09, 2007, 11:57:43 PM ---hay, I gave you that code but you edited it :O

checkLatestVersion(); and other stuff gives it away.

--- End quote ---

No. I made that code off the top of my head while creating the post.
Navigation
Message Index
Next page

Go to full version