diff --git a/PS2_Manager/Core/Game.cs b/PS2_Manager/Core/Game.cs index dc7f0a2..20a4081 100644 --- a/PS2_Manager/Core/Game.cs +++ b/PS2_Manager/Core/Game.cs @@ -1,4 +1,5 @@ using System; +using System.ComponentModel; using System.IO; using System.Linq; using System.Net.Http; @@ -7,6 +8,7 @@ using Avalonia; using Avalonia.Controls; using Avalonia.Media.Imaging; using Avalonia.Threading; +using Microsoft.VisualBasic.CompilerServices; namespace PS2_Manager.Core; @@ -33,7 +35,31 @@ public class Game this.Cover = new Bitmap(Path.Combine(Path.Combine(settings.library_path.GetValue(), "ART"), this.GameID + "_COV.png")); } } - + + public void GetChecksum() + { + string filePath = this.GamePath; + + BackgroundWorker md5Worker = new BackgroundWorker(); + BackgroundWorker sha1Worker = new BackgroundWorker(); + + md5Worker.DoWork += (s, e) => + { + string md5 = Checksum.GetMD5Hash(filePath); + Console.WriteLine("MD5: " + md5); + }; + + sha1Worker.DoWork += (s, e) => + { + string sha1 = Checksum.GetSHA1Hash(filePath); + Console.WriteLine("SHA1: " + sha1); + }; + + md5Worker.RunWorkerAsync(); + sha1Worker.RunWorkerAsync(); + + Console.WriteLine("Hashing started..."); + } private string ParseFormattedFilename(string filename) { @@ -74,8 +100,7 @@ public class Game public async Task CopyIsoWithProgressAsync() { string targetDirectory = settings.library_path.GetValue(); - if(!Directory.Exists(Path.Combine(targetDirectory, "DVD"))) - Directory.CreateDirectory(Path.Combine(targetDirectory, "DVD")); + Util.CheckDir(Path.Combine(targetDirectory, "DVD")); string newFileName = $"{this.GameID}.{this.Name}.iso"; string destPath = Path.Combine(Path.Combine(targetDirectory, "DVD"), newFileName); @@ -113,9 +138,7 @@ public class Game public void InstallCover() { string targetDirectory = settings.library_path.GetValue(); - if(!Directory.Exists(Path.Combine(targetDirectory, "ART"))) - Directory.CreateDirectory(Path.Combine(targetDirectory, "ART")); - //this.Cover.Save(Path.Combine(Path.Combine(targetDirectory, "ART"), this.GameID + "_COV.png")); + Util.CheckDir(Path.Combine(targetDirectory, "ART")); this.Cover.CreateScaledBitmap( new PixelSize(353, 500), BitmapInterpolationMode.HighQuality diff --git a/PS2_Manager/Core/Util.cs b/PS2_Manager/Core/Util.cs index 82bd122..4f1e10b 100644 --- a/PS2_Manager/Core/Util.cs +++ b/PS2_Manager/Core/Util.cs @@ -1,7 +1,11 @@ +using System; using System.Collections.Generic; +using System.IO; using System.Linq; +using System.Security.Cryptography; using Avalonia; using Avalonia.Controls; +using Avalonia.Controls.Shapes; using Avalonia.Media.Imaging; namespace PS2_Manager.Core; @@ -24,6 +28,12 @@ public static class Util var result = dialog.ShowAsync(parent).GetAwaiter().GetResult(); return result?.FirstOrDefault(); } + + public static void CheckDir(string path) + { + if(!Directory.Exists(path)) + Directory.CreateDirectory(path); + } public static string FormatFileSize(long bytes) { string[] sizes = { "B", "KB", "MB", "GB", "TB" }; @@ -39,4 +49,29 @@ public static class Util // Format to 1 decimal place return string.Format("{0:0.0} {1}", len, sizes[order]); } +} + + +public static class Checksum +{ + public static string GetMD5Hash(string filePath) + { + using (var md5 = MD5.Create()) + using (var stream = File.OpenRead(filePath)) + { + var hash = md5.ComputeHash(stream); + return BitConverter.ToString(hash).Replace("-", "").ToLowerInvariant(); + } + } + + public static string GetSHA1Hash(string filePath) + { + using (var sha1 = SHA1.Create()) + using (var stream = File.OpenRead(filePath)) + { + var hash = sha1.ComputeHash(stream); + return BitConverter.ToString(hash).Replace("-", "").ToLowerInvariant(); + } + } + } \ No newline at end of file diff --git a/PS2_Manager/EditGame.axaml b/PS2_Manager/EditGame.axaml index 1a22835..5b79035 100644 --- a/PS2_Manager/EditGame.axaml +++ b/PS2_Manager/EditGame.axaml @@ -8,5 +8,6 @@ + diff --git a/PS2_Manager/EditGame.axaml.cs b/PS2_Manager/EditGame.axaml.cs index de668ba..20b332a 100644 --- a/PS2_Manager/EditGame.axaml.cs +++ b/PS2_Manager/EditGame.axaml.cs @@ -1,6 +1,7 @@ using System; using Avalonia; using Avalonia.Controls; +using Avalonia.Interactivity; using Avalonia.Markup.Xaml; using PS2_Manager.Core; @@ -20,4 +21,9 @@ public partial class EditGame : UserControl this.game.Name = DisplayNameBox.Text; MainWindow.RefreshGamesListTrigger.Invoke(null, EventArgs.Empty); } + + private void Button_OnClick(object? sender, RoutedEventArgs e) + { + this.game.GetChecksum(); + } } \ No newline at end of file diff --git a/PS2_Manager/Setup.axaml b/PS2_Manager/Setup.axaml index 237768b..fed40ec 100644 --- a/PS2_Manager/Setup.axaml +++ b/PS2_Manager/Setup.axaml @@ -9,7 +9,8 @@ Title="Setup" Height="349" Width="598" - Background="#201c29"> + Background="#201c29" + Closing="Window_OnClosing"> { this.Close(); @@ -59,4 +61,9 @@ public partial class Setup : Window } FinishSetup(); } + + private void Window_OnClosing(object? sender, WindowClosingEventArgs e) + { + if(!this.SetupFinished) e.Cancel = true; + } } \ No newline at end of file