2021-05-25 09:04:08 +00:00
|
|
|
|
using ModernWpf.Controls;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Security.Cryptography;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
|
|
|
|
|
namespace EZPPClient_Installer
|
|
|
|
|
{
|
|
|
|
|
public class DownloadUtil
|
|
|
|
|
{
|
|
|
|
|
|
2021-05-26 06:09:30 +00:00
|
|
|
|
public static List<string> FilesToDownload(string pathToEZPP, bool reinstall)
|
2021-05-25 09:04:08 +00:00
|
|
|
|
{
|
|
|
|
|
bool directoryExists = Directory.Exists(pathToEZPP);
|
|
|
|
|
|
|
|
|
|
List<string> downloadList = new List<string>();
|
|
|
|
|
List<MD5File> md5Files = new List<MD5File>();
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
using (WebClient wc = new WebClient())
|
|
|
|
|
{
|
2021-05-25 16:29:54 +00:00
|
|
|
|
wc.Headers.Add("user-agent", "EZPPClientInstaller");
|
2021-05-25 11:14:20 +00:00
|
|
|
|
var data = wc.DownloadString("https://new.ez-pp.farm/ezppclient?list");
|
2021-05-25 09:04:08 +00:00
|
|
|
|
string[] dataList = data.Split('\n');
|
|
|
|
|
foreach (string datastring in dataList)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrWhiteSpace(datastring))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
string[] se = datastring.Split('#');
|
2021-05-25 11:14:20 +00:00
|
|
|
|
string md5 = StripHTML(se[0]);
|
|
|
|
|
string name = StripHTML(se[1]);
|
2021-05-25 09:04:08 +00:00
|
|
|
|
md5Files.Add(new MD5File(name, md5));
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-05-25 11:14:20 +00:00
|
|
|
|
}
|
|
|
|
|
catch (WebException error)
|
2021-05-25 09:04:08 +00:00
|
|
|
|
{
|
|
|
|
|
new ContentDialog()
|
|
|
|
|
{
|
|
|
|
|
Title = "Oops...",
|
|
|
|
|
Content = "A error occurred while trying to fetch all needed Files.\n" + error.Message,
|
|
|
|
|
PrimaryButtonText = "Okay"
|
|
|
|
|
}.ShowAsync();
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-26 06:09:30 +00:00
|
|
|
|
if (!directoryExists || reinstall)
|
2021-05-25 09:04:08 +00:00
|
|
|
|
{
|
2021-05-25 15:59:35 +00:00
|
|
|
|
#if DEBUG
|
|
|
|
|
MessageBox.Show("Folder does not exist: " + pathToEZPP);
|
|
|
|
|
#endif
|
2021-05-25 09:04:08 +00:00
|
|
|
|
foreach (MD5File md5File in md5Files)
|
|
|
|
|
downloadList.Add(md5File.getName());
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2021-05-25 11:14:20 +00:00
|
|
|
|
foreach (MD5File md5file in md5Files)
|
2021-05-25 09:33:38 +00:00
|
|
|
|
{
|
|
|
|
|
string file = pathToEZPP + @"\" + md5file.getName();
|
|
|
|
|
bool doesExistOnPC = File.Exists(file);
|
|
|
|
|
if (!doesExistOnPC)
|
|
|
|
|
{
|
|
|
|
|
downloadList.Add(md5file.getName());
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
string onlineHash = md5file.getMD5();
|
|
|
|
|
string pcHash = MD5Util.CalculateMD5(file);
|
2021-05-25 15:59:35 +00:00
|
|
|
|
#if DEBUG
|
|
|
|
|
MessageBox.Show("Filename: " + md5file.getName() + "\nOnline-Hash: " + onlineHash + "\n PC-Hash: " + pcHash);
|
|
|
|
|
#endif
|
2021-05-25 11:14:20 +00:00
|
|
|
|
if (!string.Equals(onlineHash, pcHash))
|
2021-05-25 09:33:38 +00:00
|
|
|
|
{
|
|
|
|
|
downloadList.Add(md5file.getName());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-05-25 09:04:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-25 11:14:20 +00:00
|
|
|
|
#if DEBUG
|
2021-05-25 09:33:38 +00:00
|
|
|
|
MessageBox.Show(downloadList.Count + "/" + md5Files.Count + " Files need for update");
|
2021-05-25 11:14:20 +00:00
|
|
|
|
#endif
|
2021-05-25 09:04:08 +00:00
|
|
|
|
return downloadList;
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-25 11:14:20 +00:00
|
|
|
|
public static async Task DownloadFiles(List<string> files, string path)
|
|
|
|
|
{
|
|
|
|
|
if (!Directory.Exists(path))
|
|
|
|
|
Directory.CreateDirectory(path);
|
|
|
|
|
|
2021-05-26 06:09:30 +00:00
|
|
|
|
foreach (string file in files)
|
2021-05-25 15:59:35 +00:00
|
|
|
|
if (File.Exists(path + @"\" + file))
|
|
|
|
|
File.Delete(path + @"\" + file);
|
|
|
|
|
|
|
|
|
|
foreach (string filename in files)
|
2021-05-25 11:14:20 +00:00
|
|
|
|
{
|
|
|
|
|
await DownloadFile(filename, path);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-26 06:09:30 +00:00
|
|
|
|
|
|
|
|
|
|
2021-05-25 11:14:20 +00:00
|
|
|
|
private static async Task DownloadFile(string filename, string path)
|
2021-05-25 09:04:08 +00:00
|
|
|
|
{
|
2021-05-26 06:09:30 +00:00
|
|
|
|
|
|
|
|
|
long last = -1;
|
|
|
|
|
long lastTransferRate = -1;
|
|
|
|
|
|
|
|
|
|
List<long> byteTransfer = new List<long>();
|
|
|
|
|
|
2021-05-25 11:14:20 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
using (WebClient wc = new WebClient())
|
|
|
|
|
{
|
2021-05-25 16:29:54 +00:00
|
|
|
|
wc.Headers.Add("user-agent", "EZPPClientInstaller");
|
2021-05-26 06:09:30 +00:00
|
|
|
|
wc.DownloadProgressChanged += (sender, value) =>
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
var bytesReceived = value.BytesReceived;
|
|
|
|
|
var totalBytes = value.TotalBytesToReceive;
|
|
|
|
|
var currentMillis = DateTimeOffset.Now.ToUnixTimeMilliseconds();
|
|
|
|
|
var diffMillis = Math.Abs(currentMillis - last);
|
|
|
|
|
if (diffMillis >= 1000 || last == -1)
|
|
|
|
|
{
|
|
|
|
|
if (lastTransferRate != -1)
|
|
|
|
|
{
|
|
|
|
|
long transfer = Math.Abs(lastTransferRate - bytesReceived);
|
|
|
|
|
byteTransfer.Add(transfer);
|
|
|
|
|
if (byteTransfer.Count > 5)
|
|
|
|
|
byteTransfer.RemoveAt(0);
|
|
|
|
|
}
|
|
|
|
|
lastTransferRate = bytesReceived;
|
|
|
|
|
last = DateTimeOffset.Now.ToUnixTimeMilliseconds();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
long averageTransferRate = 0;
|
|
|
|
|
if (byteTransfer.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
foreach (long tr in byteTransfer)
|
|
|
|
|
{
|
|
|
|
|
averageTransferRate += tr;
|
|
|
|
|
}
|
|
|
|
|
averageTransferRate /= byteTransfer.Count;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MainWindow.updateProgress(filename + " - " + value.ProgressPercentage + "% - " + Math.Round((averageTransferRate / 1024000D) * 1.8, 2) + "MBit/s", value.ProgressPercentage);
|
|
|
|
|
};
|
2021-05-25 11:14:20 +00:00
|
|
|
|
await wc.DownloadFileTaskAsync(@"https://new.ez-pp.farm/ezppclient?file=" + filename, path + @"\" + filename);
|
|
|
|
|
}
|
2021-05-26 06:09:30 +00:00
|
|
|
|
}
|
|
|
|
|
catch (WebException)
|
2021-05-25 11:14:20 +00:00
|
|
|
|
{
|
|
|
|
|
}
|
2021-05-25 09:04:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-25 11:14:20 +00:00
|
|
|
|
private static string StripHTML(string input)
|
|
|
|
|
{
|
|
|
|
|
return input.Replace("\r\n", "").Replace("\r", "").Replace("\n", "");
|
|
|
|
|
}
|
2021-05-25 09:04:08 +00:00
|
|
|
|
}
|
|
|
|
|
}
|