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
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
public static List<string> FilesToDownload(string pathToEZPP)
|
|
|
|
|
{
|
|
|
|
|
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 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();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!directoryExists)
|
|
|
|
|
{
|
|
|
|
|
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 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);
|
|
|
|
|
|
|
|
|
|
foreach(string filename in files)
|
|
|
|
|
{
|
|
|
|
|
await DownloadFile(filename, path);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static async Task DownloadFile(string filename, string path)
|
2021-05-25 09:04:08 +00:00
|
|
|
|
{
|
2021-05-25 11:14:20 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
using (WebClient wc = new WebClient())
|
|
|
|
|
{
|
|
|
|
|
wc.DownloadProgressChanged += (sender, value) => MainWindow.updateProgress(filename + " - " + value.ProgressPercentage + "% - " + Math.Round((value.BytesReceived / 1024000D), 2) + "MB/" + Math.Round((value.TotalBytesToReceive / 1024000D), 2) + "MB", value.ProgressPercentage);
|
|
|
|
|
await wc.DownloadFileTaskAsync(@"https://new.ez-pp.farm/ezppclient?file=" + filename, path + @"\" + filename);
|
|
|
|
|
}
|
|
|
|
|
}catch(WebException)
|
|
|
|
|
{
|
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
}
|