Author Topic: [RESOURCE] Simple File Downloader  (Read 1202 times)

This is a multipurpose downloader I have created, I apologize about all the "Swol_" prefixes I do that so I don't have any conflicts

to download a file
Code: [Select]
swolDownloader_downloadFile(%server,%dir,%port,%saveTo,%showGUI,%type);%showGUI is for showing the default downloader gui
%type represents your download, which will be utilized in callbacks

the call backs:
Code: [Select]
function Swol_DownloaderChunkCallBack(%chunk,%length,%timeStarted,%type) {}
Code: [Select]
function Swol_DownloaderCompleteCallBack(%type) {}
Code: [Select]
function Swol_DownloaderErrorCallBack(%errorType,%type) {}%errorType:
0 = dns fail
1 = connection fail
2 = could not write to file

if you use the callbacks, parent them so you don't mess up anything else that uses this

https://dl.dropboxusercontent.com/u/50959273/Blockland%20Private/FileDownloader.zip
Code: [Select]
if(!isObject(SwolFileDownloaderGui))
exec("./SwolFileDownloaderGui.gui");
package swol_TCPFileDownloader
{
function Swol_FileDownloaderTCP::onConnected(%this)
{
%this.send("GET" SPC %this.dir SPC "HTTP/1.0\r\nHost:" SPC %this.server @ "\r\n\r\n");

if(%this.showGui)
{
canvas.pushDialog(SwolFileDownloaderGui);
SwolFileDownloaderWindow.setText("Downloading " @ %this.fileName);
SwolFileDownloaderProgress.setValue(0);
SwolFileDownloaderProgressText.setText("Loading...");
SwolFileDownloaderTextTop.setText("Done:   0KB");
SwolFileDownloaderTextBottom.setText("Speed: N/A");
}
%this.timeStarted = getSimTime();
}
function Swol_FileDownloaderTCP::onConnectFailed(%this)
{
Swol_DownloaderErrorCallBack(1,%this.type);
%this.delete();
}

function Swol_FileDownloaderTCP::onDNSFailed(%this)
{
Swol_DownloaderErrorCallBack(0,%this.type);
%this.delete();
}
function Swol_FileDownloaderTCP::onLine(%this,%line)
{
if(strPos(%line,"Content-Length:") >= 0)
%this.length = getWords(%line,1,5);

if(%line $= "")
%this.setBinarySize(%this.length);
}
function Swol_FileDownloaderTCP::onBinChunk(%this,%chunk)
{
Swol_DownloaderChunkCallBack(%chunk,%this.length,%this.timeStarted,%this.type);
if(%this.showGui)
{
SwolFileDownloaderProgress.setValue(%chunk/%this.length);
SwolFileDownloaderProgressText.setText("Downloaded: " @ mFloor((%chunk/%this.length)*100) @ "%");
SwolFileDownloaderTextBottom.setText("Speed: " @ mFloatLength(%chunk/(getSimTime()-%this.timeStarted),2) @ "kb/s");
if(!swol_chooseByteRoundMthd(%this.length))
SwolFileDownloaderTextTop.setText("Done:   " @ swol_byteRoundToKB(%chunk) @ "/" @ swol_byteRoundToKB(%this.length));
else
SwolFileDownloaderTextTop.setText("Done:   " @ swol_byteRoundToMB(%chunk) @ "/" @ swol_byteRoundToMB(%this.length));
}
if(%chunk < %this.length)
{
return;
}
if(!isWriteableFilename(%this.saveTo))
{
Swol_DownloaderErrorCallBack(2,%this.type);
}
else
{
%this.saveBufferToFile(%this.saveTo);
}
%this.disconnect();

Swol_DownloaderCompleteCallBack(%this.type);

if(%this.showGui)
%this.closeSched = %this.schedule(500,closeGui);
}
function Swol_FileDownloaderTCP::closeGui(%this)
{
cancel(%this.closeSched);
canvas.popDialog(SwolFileDownloaderGui);
}
function Swol_FileDownloaderTCP::downloadFile(%this,%server,%dir,%port,%saveTo,%showGUI,%type)
{
if(!isWriteableFilename(%saveTo))
{
Swol_DownloaderErrorCallBack(2,%type);
return;
}

if(isFile(%saveTo))
fileDelete(%saveTo);

%this.server = %server;
%this.port = %port;
%this.dir = %dir;
%this.saveTo = %saveTo;
%this.fileName = fileName(%this.saveTo);
%this.length = "";
%this.showGui = %showGui;
%this.type = %type;
%this.connect(%this.server @ ":" @ %this.port);
}
function Swol_DownloaderChunkCallBack(%chunk,%length,%timeStarted,%type)
{

}
function Swol_DownloaderCompleteCallBack(%type)
{

}
function Swol_DownloaderErrorCallBack(%errorType,%type)
{
if(%errorType == 0)
%errorMsg = "DNS Fail";
if(%errorType == 1)
%errorMsg = "Connection Fail";
if(%errorType == 2)
%errorMsg = "Not Writeable Filename";
}
function swolDownloader_downloadFile(%server,%dir,%port,%saveTo,%showGUI,%type)
{
new TCPObject(Swol_FileDownloaderTCP);
Swol_FileDownloaderTCP.downloadFile(%server,%dir,%port,%saveTo,%showGUI,%type);
}
function swol_chooseByteRoundMthd(%bytes)
{
if(%bytes > 1048576)
%result = 1;
else
%result = 0;

return %result;
}
function swol_byteRoundToBest(%bytes)
{
if(%bytes > 1048576)
%result = swol_byteRoundToMB(%bytes);
else
%result = swol_byteRoundToKB(%bytes);

return %result;
}
function swol_byteRoundToMB(%bytes)
{
%result = mFloatLength(%bytes/1048576, 2) @ "MB";
return %result;
}
function swol_byteRoundToKB(%bytes)
{
%result = mFloatLength(%bytes/1024, 2) @ "KB";
return %result;
}
};
ActivatePackage(swol_TCPFileDownloader);

Err.. why don't the callbacks give any way to make sure that it's actually the file your add-on requested to download and not some other add-on?

Err.. why don't the callbacks give any way to make sure that it's actually the file your add-on requested to download and not some other add-on?
the %type value, when you download a file set the %type to some sort of identifier string, for my brick texture selector I use "BTS"

the %type value, when you download a file set the %type to some sort of identifier string, for my brick texture selector I use "BTS"

Oh yeah, didn't see that. Why not let the user specify their own callback methods? Seems more practical than needing to package a method.