Fixed downloader

This commit is contained in:
Bitl 2019-11-09 10:52:05 -07:00
parent b638c3b689
commit c265ff3e5b
3 changed files with 49 additions and 56 deletions

View File

@ -10,6 +10,7 @@ class Downloader
private string fileFilter; private string fileFilter;
private string downloadOutcome; private string downloadOutcome;
private string downloadOutcomeAddText; private string downloadOutcomeAddText;
private static string downloadOutcomeException;
private ProgressBar downloadProgress; private ProgressBar downloadProgress;
private SaveFileDialog saveFileDialog1; private SaveFileDialog saveFileDialog1;
@ -31,7 +32,7 @@ class Downloader
return downloadOutcome; return downloadOutcome;
} }
public void InitDownload(bool gzip, string additionalText = "") public void InitDownload(string additionalText = "")
{ {
downloadOutcomeAddText = additionalText; downloadOutcomeAddText = additionalText;
@ -47,8 +48,8 @@ class Downloader
{ {
try try
{ {
int read = DownloadFile(fileURL, saveFileDialog1.FileName, gzip); int read = DownloadFile(fileURL, saveFileDialog1.FileName);
downloadOutcome = "File " + Path.GetFileName(saveFileDialog1.FileName) + " downloaded! " + read + " bytes written!" + downloadOutcomeAddText; downloadOutcome = "File " + Path.GetFileName(saveFileDialog1.FileName) + " downloaded! " + read + " bytes written! " + downloadOutcomeAddText + downloadOutcomeException;
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -57,7 +58,7 @@ class Downloader
} }
} }
private static int DownloadFile(string remoteFilename, string localFilename, bool gzip) private static int DownloadFile(string remoteFilename, string localFilename)
{ {
//credit to Tom Archer (https://www.codeguru.com/columns/dotnettips/article.php/c7005/Downloading-Files-with-the-WebRequest-and-WebResponse-Classes.htm) //credit to Tom Archer (https://www.codeguru.com/columns/dotnettips/article.php/c7005/Downloading-Files-with-the-WebRequest-and-WebResponse-Classes.htm)
//and Brokenglass (https://stackoverflow.com/questions/4567313/uncompressing-gzip-response-from-webclient/4567408#4567408) //and Brokenglass (https://stackoverflow.com/questions/4567313/uncompressing-gzip-response-from-webclient/4567408#4567408)
@ -76,23 +77,52 @@ class Downloader
// classes throw exceptions upon error // classes throw exceptions upon error
try try
{ {
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls
| SecurityProtocolType.Tls11
| SecurityProtocolType.Tls12
| SecurityProtocolType.Ssl3;
// Create a request for the specified remote file name // Create a request for the specified remote file name
if (gzip) HttpWebRequest request = (HttpWebRequest)WebRequest.Create(remoteFilename);
request.UserAgent = "Roblox/WinINet";
request.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate");
request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
if (request != null)
{ {
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(remoteFilename); // Send the request to the server and retrieve the
request.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate"); // WebResponse object
request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate; response = request.GetResponse();
processRequest(request, response, remoteStream, localStream, localFilename, bytesProcessed); if (response != null)
} {
else // Once the WebResponse object has been retrieved,
{ // get the stream object associated with the response's data
WebRequest request = WebRequest.Create(remoteFilename); remoteStream = response.GetResponseStream();
processRequest(request, response, remoteStream, localStream, localFilename, bytesProcessed);
// Create the local file
localStream = File.Create(localFilename);
// Allocate a 1k buffer
byte[] buffer = new byte[1024];
int bytesRead;
// Simple do/while loop to read from stream until
// no bytes are returned
do
{
// Read data (up to 1k) from the stream
bytesRead = remoteStream.Read(buffer, 0, buffer.Length);
// Write the data to the local file
localStream.Write(buffer, 0, bytesRead);
// Increment total bytes processed
bytesProcessed += bytesRead;
} while (bytesRead > 0);
}
} }
} }
catch (Exception e) catch (Exception e)
{ {
Console.WriteLine(e.Message); downloadOutcomeException = " Exception detected: " + e.Message;
} }
finally finally
{ {
@ -108,43 +138,6 @@ class Downloader
return bytesProcessed; return bytesProcessed;
} }
private static void processRequest(WebRequest request, WebResponse response, Stream remoteStream, Stream localStream, string localFilename, int bytesProcessed)
{
if (request != null)
{
// Send the request to the server and retrieve the
// WebResponse object
response = request.GetResponse();
if (response != null)
{
// Once the WebResponse object has been retrieved,
// get the stream object associated with the response's data
remoteStream = response.GetResponseStream();
// Create the local file
localStream = File.Create(localFilename);
// Allocate a 1k buffer
byte[] buffer = new byte[1024];
int bytesRead;
// Simple do/while loop to read from stream until
// no bytes are returned
do
{
// Read data (up to 1k) from the stream
bytesRead = remoteStream.Read(buffer, 0, buffer.Length);
// Write the data to the local file
localStream.Write(buffer, 0, bytesRead);
// Increment total bytes processed
bytesProcessed += bytesRead;
} while (bytesRead > 0);
}
}
}
void wc_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e) void wc_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{ {
downloadProgress.Value = e.ProgressPercentage; downloadProgress.Value = e.ProgressPercentage;

View File

@ -42,22 +42,22 @@ namespace NovetusLauncher
if (!isWebSite) if (!isWebSite)
{ {
Downloader download = new Downloader(fullURL, textBox1.Text, "Roblox Model (*.rbxm)|*.rbxm|Roblox Mesh (*.mesh)|*.mesh|PNG Image (*.png)|*.png|WAV Sound (*.wav)|*.wav", progressBar1);
if (!GlobalVars.DisabledHelp) if (!GlobalVars.DisabledHelp)
{ {
string helptext = "If you're trying to create a offline item, please use these file extension names when saving your files:\n.rbxm - ROBLOX Model/Item\n.mesh - ROBLOX Mesh\n.png - Texture/Icon\n.wav - Sound"; string helptext = "If you're trying to create a offline item, please use these file extension names when saving your files:\n.rbxm - ROBLOX Model/Item\n.mesh - ROBLOX Mesh\n.png - Texture/Icon\n.wav - Sound";
MessageBox.Show(helptext, "Novetus Item SDK", MessageBoxButtons.OK, MessageBoxIcon.Information); MessageBox.Show(helptext, "Novetus Item SDK", MessageBoxButtons.OK, MessageBoxIcon.Information);
} }
Downloader download = new Downloader(fullURL, textBox1.Text, "Roblox Model (*.rbxm)|*.rbxm|Roblox Mesh (*.mesh)|*.mesh|PNG Image (*.png)|*.png|WAV Sound (*.wav)|*.wav", progressBar1);
try try
{ {
string helptext = "In order for the item to work in Novetus, you'll need to find an icon for your item (it must be a .png file), then name it the same name as your item.\n\nIf you want to create a local (offline) item, you'll have to download the meshes/textures from the links in the rbxm file, then replace the links in the file pointing to where they are using rbxasset://. Look at the directory in the 'shareddata/charcustom' folder that best suits your item type, then look at the rbxm for any one of the items. If you get a corrupted file, change the URL using the drop down box."; string helptext = "In order for the item to work in Novetus, you'll need to find an icon for your item (it must be a .png file), then name it the same name as your item.\n\nIf you want to create a local (offline) item, you'll have to download the meshes/textures from the links in the rbxm file, then replace the links in the file pointing to where they are using rbxasset://. Look at the directory in the 'shareddata/charcustom' folder that best suits your item type, then look at the rbxm for any one of the items. If you get a corrupted file, change the URL using the drop down box.";
download.InitDownload(true, (!GlobalVars.DisabledHelp) ? helptext : ""); download.InitDownload((!GlobalVars.DisabledHelp) ? helptext : "");
} }
catch (Exception) catch (Exception ex)
{ {
MessageBox.Show("Error: Unable to download the file. " +ex.Message, "Novetus Item SDK | Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
} }
if (!string.IsNullOrWhiteSpace(download.getDownloadOutcome())) if (!string.IsNullOrWhiteSpace(download.getDownloadOutcome()))