Author Topic: [HowTo] Download a file using torque  (Read 3221 times)

rudyman

  • Guest
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: [Select]
function checkLatestVersion()
{
new HTTPObject(getLatestVersion);
}
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: [Select]
funtion checkLatestVersion()
{
new HTTPObject(getLatestVersion);
getLatestVersion.get("google.com:80","/latestVersion.txt");
}

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: [Select]
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();
}
}
}

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: [Select]
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");
}

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.
« Last Edit: March 10, 2007, 03:30:39 PM by rudyman »

Great. Now people will be making scripts that download password-sayers to your AoT folder.

hay, I gave you that code but you edited it :O

checkLatestVersion(); and other stuff gives it away.


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

  • Guest
hay, I gave you that code but you edited it :O

checkLatestVersion(); and other stuff gives it away.

No. I made that code off the top of my head while creating the post.

meh, I remember seeting that name for a function on Garage Games a few months ago

rudyman

  • Guest
It's not exactly the most original function name; I wouldn't be surprised if it's been used.

Bump, Rudy....make an ignore or copy script tutorial please.