sorted some classes

This commit is contained in:
2021-05-27 08:59:45 +02:00
parent a4f060debe
commit c14665d8a8
7 changed files with 7 additions and 8 deletions

View File

@@ -0,0 +1,166 @@
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 async Task<List<string>> FilesToDownload(string pathToEZPP, bool reinstall)
{
bool directoryExists = Directory.Exists(pathToEZPP);
List<string> downloadList = new List<string>();
List<MD5File> md5Files = new List<MD5File>();
try
{
using (WebClient wc = new WebClient())
{
wc.Headers.Add("user-agent", "EZPPClientInstaller");
var data = await wc.DownloadStringTaskAsync("https://new.ez-pp.farm/ezppclient?list&release=" + MainWindow.RELEASESTREAM.ToLower());
string[] dataList = data.Split('\n');
foreach (string datastring in dataList)
{
if (string.IsNullOrWhiteSpace(datastring) || !datastring.Contains("#"))
continue;
string[] se = datastring.Split('#');
string md5 = StripHTML(se[0]);
string name = StripHTML(se[1]);
md5Files.Add(new MD5File(name, md5));
}
}
}
catch (WebException error)
{
_ = new ContentDialog()
{
Title = "Oops...",
Content = "A error occurred while trying to fetch all needed Files.\n" + error.Message,
PrimaryButtonText = "Okay"
}.ShowAsync();
}
if (!directoryExists || reinstall)
{
#if DEBUG
MessageBox.Show("Folder does not exist: " + pathToEZPP);
#endif
foreach (MD5File md5File in md5Files)
downloadList.Add(md5File.Name);
}
else
{
foreach (MD5File md5file in md5Files)
{
string file = pathToEZPP + @"\" + md5file.Name;
bool doesExistOnPC = File.Exists(file);
if (!doesExistOnPC)
{
downloadList.Add(md5file.Name);
}
else
{
string onlineHash = md5file.MD5;
string pcHash = MD5Util.CalculateMD5(file);
#if DEBUG
MessageBox.Show("Filename: " + md5file.Name + "\nOnline-Hash: " + onlineHash + "\n PC-Hash: " + pcHash);
#endif
if (!string.Equals(onlineHash, pcHash))
{
downloadList.Add(md5file.Name);
}
}
}
}
#if DEBUG
MessageBox.Show(downloadList.Count + "/" + md5Files.Count + " Files need for update");
#endif
return downloadList;
}
public static async Task DownloadFiles(List<string> files, string path)
{
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
foreach (string file in files)
if (File.Exists(path + @"\" + file))
File.Delete(path + @"\" + file);
foreach (string filename in files)
{
await DownloadFile(filename, path);
}
}
private static async Task DownloadFile(string filename, string path)
{
long last = -1;
long lastTransferRate = -1;
List<long> byteTransfer = new List<long>();
try
{
using (WebClient wc = new WebClient())
{
wc.Headers.Add("user-agent", "EZPPClientInstaller");
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);
};
await wc.DownloadFileTaskAsync(@"https://new.ez-pp.farm/ezppclient?file=" + filename + "&release=" + MainWindow.RELEASESTREAM.ToLower(), path + @"\" + filename); ;
}
}
catch (WebException)
{
}
}
private static string StripHTML(string input)
{
return input.Replace("\r\n", "").Replace("\r", "").Replace("\n", "");
}
}
}

View File

@@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace EZPPClient_Installer
{
class MD5Util
{
public static string CalculateMD5(string filename)
{
using (var md5 = MD5.Create())
{
using (var stream = File.OpenRead(filename))
{
var hash = md5.ComputeHash(stream);
return BitConverter.ToString(hash).Replace("-", "").ToUpperInvariant();
}
}
}
}
}

View File

@@ -0,0 +1,54 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Path = System.IO.Path;
using File = System.IO.File;
using Microsoft.Win32;
namespace EZPPClient_Installer
{
class OsuUtil
{
public static string osuInstallDir()
{
string path = "";
string registry_key = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
string second_registry_key = @"SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall";
using (Microsoft.Win32.RegistryKey key = Registry.LocalMachine.OpenSubKey(registry_key))
{
foreach (string subkey_name in key.GetSubKeyNames())
{
using (RegistryKey subkey = key.OpenSubKey(subkey_name))
{
if ((string)subkey.GetValue("DisplayName") == "osu!")
{
path = Path.GetDirectoryName(subkey.GetValue("DisplayIcon").ToString());
}
}
}
}
if (string.IsNullOrEmpty(path))
{
using (Microsoft.Win32.RegistryKey key = Registry.LocalMachine.OpenSubKey(second_registry_key))
{
foreach (string subkey_name in key.GetSubKeyNames())
{
using (RegistryKey subkey = key.OpenSubKey(subkey_name))
{
if ((string)subkey.GetValue("DisplayName") == "osu!")
{
path = Path.GetDirectoryName(subkey.GetValue("DisplayIcon").ToString());
}
}
}
}
}
return path;
}
}
}

View File

@@ -0,0 +1,33 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EZPPClient_Installer
{
class ProcessUtil
{
public static bool ProgramIsRunning(string FullPath)
{
string FilePath = Path.GetDirectoryName(FullPath);
string FileName = Path.GetFileNameWithoutExtension(FullPath).ToLower();
bool isRunning = false;
Process[] pList = Process.GetProcessesByName(FileName);
foreach (Process p in pList)
{
if (p.MainModule.FileName.StartsWith(FilePath, StringComparison.InvariantCultureIgnoreCase))
{
isRunning = true;
break;
}
}
return isRunning;
}
}
}