From 0f8c65c61c46421a6c32307e8e665a15c904708f Mon Sep 17 00:00:00 2001 From: WeeXnes Date: Mon, 21 Apr 2025 21:14:53 +0200 Subject: [PATCH] Working Game Artworks (Front, Back, DVD) --- PS2_Manager/AddGameWindow.axaml | 3 +- PS2_Manager/AddGameWindow.axaml.cs | 31 +++++++++- PS2_Manager/Core/Game.cs | 89 +++++++++++++++++++++++++---- PS2_Manager/Core/Globals.cs | 4 ++ PS2_Manager/Core/Util.cs | 6 ++ PS2_Manager/GameInfo.axaml | 16 +++++- PS2_Manager/GameInfo.axaml.cs | 92 +++++++++++++++++++++++++++++- 7 files changed, 224 insertions(+), 17 deletions(-) diff --git a/PS2_Manager/AddGameWindow.axaml b/PS2_Manager/AddGameWindow.axaml index cebadc7..ff0b1f2 100644 --- a/PS2_Manager/AddGameWindow.axaml +++ b/PS2_Manager/AddGameWindow.axaml @@ -27,7 +27,8 @@ - + diff --git a/PS2_Manager/AddGameWindow.axaml.cs b/PS2_Manager/AddGameWindow.axaml.cs index 7cfc234..c769bb1 100644 --- a/PS2_Manager/AddGameWindow.axaml.cs +++ b/PS2_Manager/AddGameWindow.axaml.cs @@ -9,6 +9,7 @@ using Avalonia.Input; using Avalonia.Interactivity; using Avalonia.Markup.Xaml; using Avalonia.Media.Imaging; +using Avalonia.Platform.Storage; using PS2_Manager.Core; namespace PS2_Manager; @@ -23,11 +24,16 @@ public partial class AddGameWindow : Window InitializeComponent(); } - private void Control_OnLoaded(object? sender, RoutedEventArgs e) + private void RefreshPreview() { IsoPathBox.Text = this.newGame.GamePath; SerialBox.Text = this.newGame.GameID; - CoverImage.Source = this.newGame.Cover; + CoverImage.Source = this.newGame.ArtworkFront; + } + + private void Control_OnLoaded(object? sender, RoutedEventArgs e) + { + RefreshPreview(); } private void Control_OnSizeChanged(object? sender, SizeChangedEventArgs e) @@ -92,4 +98,25 @@ public partial class AddGameWindow : Window { this.newGame.Name = GameNameBox.Text ?? ""; } + + private async void CoverImage_OnPointerPressed(object? sender, PointerPressedEventArgs e) + { + var topLevel = TopLevel.GetTopLevel(this); + + var files = await topLevel.StorageProvider.OpenFilePickerAsync(new FilePickerOpenOptions + { + Title = "Select a Cover for the PS2 Game", + AllowMultiple = false, + FileTypeFilter = new [] + { + Globals.FileDialogTypes.PS2Cover + } + }); + + if (files.Count >= 1) + { + this.newGame.ArtworkFront = new Bitmap(files[0].Path.LocalPath); + RefreshPreview(); + } + } } \ No newline at end of file diff --git a/PS2_Manager/Core/Game.cs b/PS2_Manager/Core/Game.cs index 20a4081..fadd2ec 100644 --- a/PS2_Manager/Core/Game.cs +++ b/PS2_Manager/Core/Game.cs @@ -17,7 +17,9 @@ public class Game public string Name { get; set; } public string GameID { get; set; } public string GamePath { get; set; } - public Bitmap Cover { get; set; } + public Bitmap? ArtworkFront { get; set; } + public Bitmap? ArtworkBack { get; set; } + public Bitmap? ArtworkDVD { get; set; } public EventHandler? InstallationFinished { get; set; } public UpdateVar InstallProgress { get; private set; } @@ -27,12 +29,14 @@ public class Game this.GameID = ISO.GetSerial(isoPath); if (!installed) { - this.Cover = this.GetCover(); + this.ArtworkFront = this.GetCover(); } else { this.Name = ParseFormattedFilename(Path.GetFileName(isoPath)); - this.Cover = new Bitmap(Path.Combine(Path.Combine(settings.library_path.GetValue(), "ART"), this.GameID + "_COV.png")); + this.ArtworkFront = this.LoadCover(Artwork.Type.Front); + this.ArtworkBack = this.LoadCover(Artwork.Type.Back); + this.ArtworkDVD = this.LoadCover(Artwork.Type.Disc); } } @@ -124,24 +128,89 @@ public class Game } } - this.InstallCover(); + //this.InstallCover(); this.InstallationFinished?.Invoke(this, EventArgs.Empty); MainWindow.RefreshGamesListTrigger?.Invoke(this, EventArgs.Empty); Console.WriteLine($"Copied ISO to: {destPath}"); } - - public void InstallCover() + + public Bitmap? LoadCover(Artwork.Type artworkType) { string targetDirectory = settings.library_path.GetValue(); Util.CheckDir(Path.Combine(targetDirectory, "ART")); - this.Cover.CreateScaledBitmap( - new PixelSize(353, 500), - BitmapInterpolationMode.HighQuality - ).Save(Path.Combine(Path.Combine(targetDirectory, "ART"), this.GameID + "_COV.png")); + try + { + switch (artworkType) + { + case Artwork.Type.Front: + return new Bitmap(Path.Combine(Path.Combine(targetDirectory, "ART"), this.GameID + "_COV.png")); + break; + case Artwork.Type.Back: + return new Bitmap(Path.Combine(Path.Combine(targetDirectory, "ART"), this.GameID + "_COV2.png")); + break; + case Artwork.Type.Disc: + return new Bitmap(Path.Combine(Path.Combine(targetDirectory, "ART"), this.GameID + "_ICO.png")); + break; + default: + return null; + break; + } + } + catch + { + return null; + } + } + public void SaveCover(Artwork.Type artworkType) + { + string targetDirectory = settings.library_path.GetValue(); + Util.CheckDir(Path.Combine(targetDirectory, "ART")); + switch (artworkType) + { + case Artwork.Type.Front: + this.ArtworkFront?.CreateScaledBitmap( + new PixelSize(353, 500), + BitmapInterpolationMode.HighQuality + ).Save(Path.Combine(Path.Combine(targetDirectory, "ART"), this.GameID + "_COV.png")); + break; + case Artwork.Type.Back: + this.ArtworkBack?.CreateScaledBitmap( + new PixelSize(353, 500), + BitmapInterpolationMode.HighQuality + ).Save(Path.Combine(Path.Combine(targetDirectory, "ART"), this.GameID + "_COV2.png")); + break; + case Artwork.Type.Disc: + this.ArtworkDVD?.CreateScaledBitmap( + new PixelSize(353, 353), + BitmapInterpolationMode.HighQuality + ).Save(Path.Combine(Path.Combine(targetDirectory, "ART"), this.GameID + "_ICO.png")); + break; + } + } +} + + +public static class Artwork +{ + public enum Type + { + Front, + Back, + Disc + } + + public static Type NextType(Type _type) + { + return (Type)(((int)_type + 1) % Enum.GetValues(typeof(Type)).Length); + } + + public static Type PrevType(Type _type) + { + return (Type)(((int)_type - 1 + Enum.GetValues(typeof(Type)).Length) % Enum.GetValues(typeof(Type)).Length); } } \ No newline at end of file diff --git a/PS2_Manager/Core/Globals.cs b/PS2_Manager/Core/Globals.cs index 263ff4d..2b76533 100644 --- a/PS2_Manager/Core/Globals.cs +++ b/PS2_Manager/Core/Globals.cs @@ -25,6 +25,10 @@ public static class Globals { Patterns = new[] { "*.iso" } }; + public static FilePickerFileType PS2Cover { get; } = new("PS2 Cover") + { + Patterns = new[] { "*.png", "*.jpg" } + }; } } diff --git a/PS2_Manager/Core/Util.cs b/PS2_Manager/Core/Util.cs index 4f1e10b..fb4d1f7 100644 --- a/PS2_Manager/Core/Util.cs +++ b/PS2_Manager/Core/Util.cs @@ -49,6 +49,11 @@ public static class Util // Format to 1 decimal place return string.Format("{0:0.0} {1}", len, sizes[order]); } + + public static bool IsBitmapEmpty(Bitmap? bitmap) + { + return bitmap == null; + } } @@ -73,5 +78,6 @@ public static class Checksum return BitConverter.ToString(hash).Replace("-", "").ToLowerInvariant(); } } + } \ No newline at end of file diff --git a/PS2_Manager/GameInfo.axaml b/PS2_Manager/GameInfo.axaml index 5c4964b..3134e06 100644 --- a/PS2_Manager/GameInfo.axaml +++ b/PS2_Manager/GameInfo.axaml @@ -5,9 +5,19 @@ mc:Ignorable="d" x:Class="PS2_Manager.GameInfo"> - - - + + +