diff --git a/PS2_Manager/AddGameWindow.axaml b/PS2_Manager/AddGameWindow.axaml index ff0b1f2..f61bbc0 100644 --- a/PS2_Manager/AddGameWindow.axaml +++ b/PS2_Manager/AddGameWindow.axaml @@ -10,7 +10,6 @@ Title="Install Game" WindowStartupLocation="CenterScreen" Loaded="Control_OnLoaded" - SizeChanged="Control_OnSizeChanged" Background="#201c29" SystemDecorations="None"> diff --git a/PS2_Manager/AddGameWindow.axaml.cs b/PS2_Manager/AddGameWindow.axaml.cs index 17c9eb0..0f5e888 100644 --- a/PS2_Manager/AddGameWindow.axaml.cs +++ b/PS2_Manager/AddGameWindow.axaml.cs @@ -26,6 +26,7 @@ public partial class AddGameWindow : Window private void RefreshPreview() { + Console.Info("Refreshing AddGameWindow Preview"); IsoPathBox.Text = this.newGame.GamePath; SerialBox.Text = this.newGame.GameID; GameNameBox.Text = this.newGame.Name; @@ -36,33 +37,7 @@ public partial class AddGameWindow : Window { RefreshPreview(); } - - private void Control_OnSizeChanged(object? sender, SizeChangedEventArgs e) - { - var newSize = e.NewSize; - Console.WriteLine($"[SizeChanged] New size: {newSize.Width} x {newSize.Height}"); - } - public void LoadPs2Cover(string gameId, Image imageControl) - { - //gameId = "SCPS-15110"; - string url = $"https://github.com/xlenore/ps2-covers/blob/main/covers/default/{gameId}.jpg?raw=true"; - - try - { - using HttpClient client = new(); - byte[] data = client.GetByteArrayAsync(url).GetAwaiter().GetResult(); - - using MemoryStream stream = new(data); - var bitmap = new Bitmap(stream); - - imageControl.Source = bitmap; - } - catch (Exception ex) - { - Console.WriteLine($"[Error] Could not load cover for {gameId}: {ex.Message}"); - } - } private void WindowDrag(object? sender, PointerPressedEventArgs e) @@ -90,7 +65,7 @@ public partial class AddGameWindow : Window }; newGame.InstallationFinished += (o, args) => { - Console.WriteLine("Installation finished"); + Console.WriteLine("Installation for " + newGame + " finished"); this.Close(); }; } @@ -116,8 +91,13 @@ public partial class AddGameWindow : Window if (files.Count >= 1) { + Console.Info("Updating " + Artwork.Type.Front + " Artwork for " + this.newGame.GameID); this.newGame.ArtworkFront = new Bitmap(files[0].Path.LocalPath); RefreshPreview(); } + else + { + Console.Error("OpenFilePicker was called but Canceled or no Files selected"); + } } } \ No newline at end of file diff --git a/PS2_Manager/App.axaml.cs b/PS2_Manager/App.axaml.cs index f2435f4..2038100 100644 --- a/PS2_Manager/App.axaml.cs +++ b/PS2_Manager/App.axaml.cs @@ -41,7 +41,7 @@ public partial class App : Application public override void OnFrameworkInitializationCompleted() { SetExceptionHandler(); - Console.Info("App started at " + DateTime.Now.ToString("dddd, dd MMMM yyyy HH:mm:ss")); + Console.SetLogFile("output.log"); Globals.LoadSettings(); if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop) { diff --git a/PS2_Manager/Core/Console.cs b/PS2_Manager/Core/Console.cs index 4be4c7e..a2d4648 100644 --- a/PS2_Manager/Core/Console.cs +++ b/PS2_Manager/Core/Console.cs @@ -6,14 +6,26 @@ using System.IO; using System.Runtime.CompilerServices; using System.Text; - - namespace PS2_Manager.Core { - - public static class Console { + private static StreamWriter logWriter = null; + private static readonly object logLock = new(); + + public static void SetLogFile(string path) + { + if (logWriter != null) + { + logWriter.Dispose(); + } + + logWriter = new StreamWriter(path, append: true) + { + AutoFlush = true + }; + } + public static class Data { public static class Colors @@ -34,6 +46,7 @@ namespace PS2_Manager.Core public static string writeline_char = "•"; } } + private static void ConfiguredWriteline( string text, ConsoleColor color, @@ -43,104 +56,129 @@ namespace PS2_Manager.Core { VanillaConsole.OutputEncoding = Encoding.UTF8; } - catch (Exception ex) - { - - } + catch {} + + // Save current colors ConsoleColor prevColor = VanillaConsole.BackgroundColor; ConsoleColor prevForeColor = VanillaConsole.ForegroundColor; + + // Timestamp + DateTime currentTime = DateTime.Now; + string timestamp = currentTime.ToString("[HH:mm:ss] "); + if (Data.Formatting.timestamp_prefix) + text = timestamp + text; + + // Apply console colors if (Data.Colors.colored_output) { VanillaConsole.BackgroundColor = color; VanillaConsole.ForegroundColor = foregroundColor; } - DateTime currentTime = DateTime.Now; - if (Data.Formatting.timestamp_prefix) - text = currentTime.ToString("[HH:mm:ss]") + text; + VanillaConsole.Write(text + " "); + if (Data.Colors.colored_output) { - VanillaConsole.BackgroundColor = prevColor; VanillaConsole.ForegroundColor = prevForeColor; } + VanillaConsole.WriteLine(""); + + // Log to file (if enabled) + if (logWriter != null) + { + lock (logLock) + { + try + { + logWriter.WriteLine(timestamp + text); + } + catch (Exception ex) + { + VanillaConsole.WriteLine("Log file write failed: " + ex.Message); + } + } + } } public static void WriteLine(string text, [CallerLineNumber] int lineNumber = 0, [CallerMemberName] string caller = null) { - ConfiguredWriteline(" " + Data.Formatting.writeline_char + " (" + lineNumber + "|" + caller + ") " + text,VanillaConsole.BackgroundColor, ConsoleColor.White); + ConfiguredWriteline(" " + Data.Formatting.writeline_char + " (" + lineNumber + "|" + caller + ") " + text, + VanillaConsole.BackgroundColor, ConsoleColor.White); } + public static void WriteLine(float text, [CallerLineNumber] int lineNumber = 0, [CallerMemberName] string caller = null) { - ConfiguredWriteline(" " + Data.Formatting.writeline_char + " (" + lineNumber + "|" + caller + ") " + text,Data.Colors.float_color, ConsoleColor.White); + ConfiguredWriteline(" " + Data.Formatting.writeline_char + " (" + lineNumber + "|" + caller + ") " + text, + Data.Colors.float_color, ConsoleColor.White); } + public static void WriteLine(double text, [CallerLineNumber] int lineNumber = 0, [CallerMemberName] string caller = null) { - ConfiguredWriteline(" " + Data.Formatting.writeline_char + " (" + lineNumber + "|" + caller + ") " + text,Data.Colors.double_color, ConsoleColor.White); + ConfiguredWriteline(" " + Data.Formatting.writeline_char + " (" + lineNumber + "|" + caller + ") " + text, + Data.Colors.double_color, ConsoleColor.White); } + public static void WriteLine(int text, [CallerLineNumber] int lineNumber = 0, [CallerMemberName] string caller = null) { - ConfiguredWriteline(" " + Data.Formatting.writeline_char + " (" + lineNumber + "|" + caller + ") " + text,Data.Colors.int_color, ConsoleColor.White); + ConfiguredWriteline(" " + Data.Formatting.writeline_char + " (" + lineNumber + "|" + caller + ") " + text, + Data.Colors.int_color, ConsoleColor.White); } + public static void Success(string text, [CallerLineNumber] int lineNumber = 0, [CallerMemberName] string caller = null) { - ConfiguredWriteline(" " + Data.Formatting.success_char + " (" + lineNumber + "|" + caller + ") " + text, ConsoleColor.Green, - ConsoleColor.Black); + ConfiguredWriteline(" " + Data.Formatting.success_char + " (" + lineNumber + "|" + caller + ") " + text, + ConsoleColor.Green, ConsoleColor.Black); } public static void Info(string text, [CallerLineNumber] int lineNumber = 0, [CallerMemberName] string caller = null) { - ConfiguredWriteline(" " + Data.Formatting.info_char + " (" + lineNumber + "|" + caller + ") " + text, ConsoleColor.Blue, - ConsoleColor.Black); + ConfiguredWriteline(" " + Data.Formatting.info_char + " (" + lineNumber + "|" + caller + ") " + text, + ConsoleColor.Blue, ConsoleColor.Black); } public static void Error(string text, [CallerLineNumber] int lineNumber = 0, [CallerMemberName] string caller = null) { - ConfiguredWriteline(" " + Data.Formatting.error_char + " (" + lineNumber + "|" + caller + ") " + text, ConsoleColor.DarkRed, - ConsoleColor.Black); + ConfiguredWriteline(" " + Data.Formatting.error_char + " (" + lineNumber + "|" + caller + ") " + text, + ConsoleColor.DarkRed, ConsoleColor.Black); } public static void Warning(string text, [CallerLineNumber] int lineNumber = 0, [CallerMemberName] string caller = null) { - ConfiguredWriteline(" " + Data.Formatting.warning_char + " (" + lineNumber + "|" + caller + ") " + text, ConsoleColor.DarkYellow, - ConsoleColor.Black); + ConfiguredWriteline(" " + Data.Formatting.warning_char + " (" + lineNumber + "|" + caller + ") " + text, + ConsoleColor.DarkYellow, ConsoleColor.Black); } - + public static void WriteLine(List List, bool verbose = true) { - ConfiguredWriteline("List contains " + typeof(T) + "(" + List.Count + ")", ConsoleColor.DarkMagenta, - ConsoleColor.Black); + ConfiguredWriteline("List contains " + typeof(T) + "(" + List.Count + ")", + ConsoleColor.DarkMagenta, ConsoleColor.Black); + if (!verbose) return; for (int i = 0; i < List.Count; i++) { - if (i % 2 == 0) - { - ConfiguredWriteline("(" + i + "): " + List[i], ConsoleColor.DarkGray); - } - else - { - ConfiguredWriteline("(" + i + "): " + List[i], ConsoleColor.Black); - } + var color = (i % 2 == 0) ? ConsoleColor.DarkGray : ConsoleColor.Black; + ConfiguredWriteline("(" + i + "): " + List[i], color); } } } -} \ No newline at end of file +} diff --git a/PS2_Manager/Core/Game.cs b/PS2_Manager/Core/Game.cs index 9b37333..869aa51 100644 --- a/PS2_Manager/Core/Game.cs +++ b/PS2_Manager/Core/Game.cs @@ -46,11 +46,20 @@ public class Game public void ChangeName(string newName) { - this.Name = newName; - string targetDirectory = settings.library_path.GetValue(); - string newFileName = $"{this.GameID}.{this.Name}.iso"; - string destPath = Path.Combine(Path.Combine(targetDirectory, "DVD"), newFileName); - File.Move(this.GamePath, destPath); + Console.Info("Changing name of " + this.Name + " to " + newName); + try + { + this.Name = newName; + string targetDirectory = settings.library_path.GetValue(); + string newFileName = $"{this.GameID}.{this.Name}.iso"; + string destPath = Path.Combine(Path.Combine(targetDirectory, "DVD"), newFileName); + File.Move(this.GamePath, destPath); + Console.Success(this.Name + " renamed successfully"); + } + catch (Exception e) + { + Console.Error(e.Message); + } } public string GetGameTitle() @@ -75,7 +84,7 @@ public class Game } catch (Exception ex) { - Console.WriteLine($"Error: {ex.Message}"); + Console.Error($"{ex.Message}"); return ""; } } @@ -85,7 +94,9 @@ public class Game private string ParseFormattedFilename(string filename) { string[] parts = filename.Split('.'); - return parts[^2]; + string parsedName = parts[^2]; + Console.Info(parsedName + " parsed from " + filename); + return parsedName; } public async void Install() @@ -96,7 +107,7 @@ public class Game private Bitmap DownloadCover(Artwork.Type type) { - + Console.Info("Downloading " + type + " Artwork for " + this.GameID); Bitmap cover = null; string url = ""; switch (type) @@ -124,8 +135,10 @@ public class Game } catch (Exception ex) { - Console.WriteLine($"[Error] Could not load cover for {this.GameID}: {ex.Message}"); + Console.Error($"Could not load cover for {this.GameID}: {ex.Message}"); } + + Console.Success(type + " Artwork for " + this.GameID + " downloaded successfully"); return cover; } @@ -137,6 +150,7 @@ public class Game public async Task CopyIsoWithProgressAsync() { + Console.Info("Copying ISO file for " + this + "..."); string targetDirectory = settings.library_path.GetValue(); Util.CheckDir(Path.Combine(targetDirectory, "DVD")); @@ -161,15 +175,13 @@ public class Game this.InstallProgress.Value = (double)bytesCopied / totalBytes * 100; } } + Console.Success("ISO File copied successfully for " + this); - //this.InstallCover(); this.SaveCover(Artwork.Type.Front); this.SaveCover(Artwork.Type.Back); this.SaveCover(Artwork.Type.Disc); this.InstallationFinished?.Invoke(this, EventArgs.Empty); MainWindow.RefreshGamesListTrigger?.Invoke(this, EventArgs.Empty); - - Console.WriteLine($"Copied ISO to: {destPath}"); } @@ -178,6 +190,7 @@ public class Game public Bitmap? LoadCover(Artwork.Type artworkType) { + Console.Info("Loading " + artworkType + " Artwork for " + this.GameID); string targetDirectory = settings.library_path.GetValue(); Util.CheckDir(Path.Combine(targetDirectory, "ART")); try @@ -228,6 +241,7 @@ public class Game ).Save(Path.Combine(Path.Combine(targetDirectory, "ART"), this.GameID + "_ICO.png")); break; } + Console.Success("Saved " + artworkType + " Artwork for " + this.GameID); } public override string ToString() diff --git a/PS2_Manager/GameInfo.axaml.cs b/PS2_Manager/GameInfo.axaml.cs index 8bd4b8a..011c605 100644 --- a/PS2_Manager/GameInfo.axaml.cs +++ b/PS2_Manager/GameInfo.axaml.cs @@ -102,7 +102,7 @@ public partial class GameInfo : UserControl this.game.ArtworkDVD = new Bitmap(files[0].Path.LocalPath); break; } - Console.Error("Updating " + ArtworkType.Value + " Artwork for " + this.game.GameID); + Console.Info("Updating " + ArtworkType.Value + " Artwork for " + this.game.GameID); this.game.SaveCover(ArtworkType.Value); UpdateArtworks(); MainWindow.RefreshGamesListTrigger?.Invoke(null, EventArgs.Empty); diff --git a/PS2_Manager/MainWindow.axaml.cs b/PS2_Manager/MainWindow.axaml.cs index 9926442..9cbc835 100644 --- a/PS2_Manager/MainWindow.axaml.cs +++ b/PS2_Manager/MainWindow.axaml.cs @@ -104,7 +104,7 @@ public partial class MainWindow : Window { if (GamesList.SelectedItem is Game selectedGame) { - Console.Info("Uninstalling " + selectedGame); + Console.Warning("Uninstalling " + selectedGame); selectedGame.Uninstall(); RefreshGamesListTrigger?.Invoke(sender, e); }