diff --git a/PS2_Manager/App.axaml.cs b/PS2_Manager/App.axaml.cs index a4cfdcc..f2435f4 100644 --- a/PS2_Manager/App.axaml.cs +++ b/PS2_Manager/App.axaml.cs @@ -1,15 +1,38 @@ using System; +using System.IO; +using System.Runtime.InteropServices.JavaScript; using System.Text.Json; using Avalonia; using Avalonia.Controls; using Avalonia.Controls.ApplicationLifetimes; using Avalonia.Markup.Xaml; using PS2_Manager.Core; +using Tmds.DBus.Protocol; namespace PS2_Manager; public partial class App : Application { + + private void SetExceptionHandler() + { + AppDomain currentDomain = default(AppDomain); + currentDomain = AppDomain.CurrentDomain; + currentDomain.UnhandledException += GlobalUnhandledExceptionHandler; + } + private static void GlobalUnhandledExceptionHandler(object sender, UnhandledExceptionEventArgs e) + { + Exception ex = (Exception)e.ExceptionObject; + Console.Error("Exception: " + ex.Message); + DateTime currentDateTime = DateTime.Now; + string formattedDateTime = currentDateTime.ToString("dddd, dd MMMM yyyy HH:mm:ss"); + using (StreamWriter writer = new StreamWriter("error_log.txt", append: true)) + { + writer.WriteLine(formattedDateTime); + writer.WriteLine(ex.ToString()); + writer.WriteLine("---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------"); + } + } public override void Initialize() { AvaloniaXamlLoader.Load(this); @@ -17,6 +40,8 @@ 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")); Globals.LoadSettings(); if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop) { diff --git a/PS2_Manager/Core/Console.cs b/PS2_Manager/Core/Console.cs new file mode 100644 index 0000000..4be4c7e --- /dev/null +++ b/PS2_Manager/Core/Console.cs @@ -0,0 +1,146 @@ +global using Console = PS2_Manager.Core.Console; +global using VanillaConsole = System.Console; +using System; +using System.Collections.Generic; +using System.IO; +using System.Runtime.CompilerServices; +using System.Text; + + + +namespace PS2_Manager.Core +{ + + + public static class Console + { + public static class Data + { + public static class Colors + { + public static bool colored_output = true; + public static ConsoleColor int_color = ConsoleColor.Blue; + public static ConsoleColor double_color = ConsoleColor.Cyan; + public static ConsoleColor float_color = ConsoleColor.DarkCyan; + } + + public static class Formatting + { + public static bool timestamp_prefix = false; + public static string success_char = "✓"; + public static string warning_char = "⌬"; + public static string info_char = "◈"; + public static string error_char = "☓"; + public static string writeline_char = "•"; + } + } + private static void ConfiguredWriteline( + string text, + ConsoleColor color, + ConsoleColor foregroundColor = ConsoleColor.White) + { + try + { + VanillaConsole.OutputEncoding = Encoding.UTF8; + } + catch (Exception ex) + { + + } + ConsoleColor prevColor = VanillaConsole.BackgroundColor; + ConsoleColor prevForeColor = VanillaConsole.ForegroundColor; + 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(""); + } + + 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); + } + 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); + } + 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); + } + 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); + } + 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); + } + + 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); + } + + 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); + } + + 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); + } + + public static void WriteLine(List List, bool verbose = true) + { + 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); + } + } + } + } +} \ No newline at end of file diff --git a/PS2_Manager/Core/Game.cs b/PS2_Manager/Core/Game.cs index 3fece8f..9b37333 100644 --- a/PS2_Manager/Core/Game.cs +++ b/PS2_Manager/Core/Game.cs @@ -229,6 +229,11 @@ public class Game break; } } + + public override string ToString() + { + return this.Name + " -> " + this.GameID; + } } diff --git a/PS2_Manager/EditGame.axaml b/PS2_Manager/EditGame.axaml index 11f81e5..eeb49b7 100644 --- a/PS2_Manager/EditGame.axaml +++ b/PS2_Manager/EditGame.axaml @@ -9,7 +9,7 @@ - + diff --git a/PS2_Manager/EditGame.axaml.cs b/PS2_Manager/EditGame.axaml.cs index 8370992..edd6bed 100644 --- a/PS2_Manager/EditGame.axaml.cs +++ b/PS2_Manager/EditGame.axaml.cs @@ -23,7 +23,7 @@ public partial class EditGame : UserControl { Dispatcher.UIThread.Invoke(() => { - Console.WriteLine("MD5Hash.ValueChanged"); + Console.Info("MD5 Hash calculated"); MD5TextBlock.Text = "MD5: " + MD5Hash.Value; }); }; @@ -31,17 +31,12 @@ public partial class EditGame : UserControl { Dispatcher.UIThread.Invoke(() => { - Console.WriteLine("SHA1Hash.ValueChanged"); + Console.Info("SHA1 Hash calculated"); SHATextBlock.Text = "SHA1: " + SHA1Hash.Value; }); }; } - - private void DisplayNameBox_OnTextChanged(object? sender, TextChangedEventArgs e) - { - - } - + private void ChecksumButtonOnClick(object? sender, RoutedEventArgs e) { this.GetChecksum(); @@ -69,9 +64,9 @@ public partial class EditGame : UserControl }; md5Worker.RunWorkerAsync(); + Console.Info("MD5 Hashing started..."); sha1Worker.RunWorkerAsync(); - - Console.WriteLine("Hashing started..."); + Console.Info("SHA1 Hashing started..."); } private void Save_OnClick(object? sender, RoutedEventArgs e) diff --git a/PS2_Manager/GameInfo.axaml.cs b/PS2_Manager/GameInfo.axaml.cs index bce5e0f..8bd4b8a 100644 --- a/PS2_Manager/GameInfo.axaml.cs +++ b/PS2_Manager/GameInfo.axaml.cs @@ -37,6 +37,8 @@ public partial class GameInfo : UserControl private void UpdateArtworks() { + + Console.Info("Showing: " + ArtworkType.Value.ToString()); Bitmap? tempBitmap = null; switch (ArtworkType.Value) { @@ -64,6 +66,7 @@ public partial class GameInfo : UserControl CoverContainer.Background = Brushes.Transparent; CoverTextHint.IsVisible = false; } + CoverTextHint.Text = ArtworkType.Value.ToString(); @@ -99,11 +102,15 @@ 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); this.game.SaveCover(ArtworkType.Value); UpdateArtworks(); MainWindow.RefreshGamesListTrigger?.Invoke(null, EventArgs.Empty); } + else + { + Console.Error("OpenFilePicker was called but Canceled or no Files selected"); + } } private void NextButton(object? sender, RoutedEventArgs e) diff --git a/PS2_Manager/MainWindow.axaml.cs b/PS2_Manager/MainWindow.axaml.cs index d9ff494..9926442 100644 --- a/PS2_Manager/MainWindow.axaml.cs +++ b/PS2_Manager/MainWindow.axaml.cs @@ -26,6 +26,7 @@ public partial class MainWindow : Window GameShowcaseGrid.IsVisible = true; InfoWindow.Child = new GameInfo(game); GameEdit.Child = new EditGame(game); + Console.Info(game + " set as Showcase"); } private void WindowDrag(object? sender, PointerPressedEventArgs e) @@ -38,6 +39,7 @@ public partial class MainWindow : Window private void WindowClose(object? sender, PointerPressedEventArgs e) { + Console.Success("Window closing"); this.Close(); } @@ -57,9 +59,13 @@ public partial class MainWindow : Window if (files.Count >= 1) { - //Console.WriteLine(ISO.GetSerial()); + Console.Info("AddGameWindow was called for " + files[0].Path.LocalPath); new AddGameWindow(files[0].Path.LocalPath).Show(); } + else + { + Console.Error("OpenFilePicker was called but Canceled or no Files selected"); + } } private async void Control_OnLoaded(object? sender, RoutedEventArgs e) @@ -71,16 +77,16 @@ public partial class MainWindow : Window public void FetchGamesFromLibrary(object? sender = null, EventArgs? e = null) { + Console.Info("Fetching games from library..."); List Games = new List(); - Console.WriteLine("Loading library..."); Util.CheckDir(Path.Combine(settings.library_path.GetValue(), "DVD")); string[] files = Directory.GetFiles(Path.Combine(settings.library_path.GetValue(), "DVD")); foreach (var file in files) { - Console.WriteLine(file); Game newGame = new Game(file, true); Games.Add(newGame); + Console.Success("Successfully fetched " + newGame); } Games = Games.OrderBy(game => game.Name).ToList(); GamesList.ItemsSource = Games; @@ -90,6 +96,7 @@ public partial class MainWindow : Window { if (GamesList.SelectedItem is Game selectedGame) { + Console.Info("Selected " + selectedGame + " from ListView"); this.ShowcaseGame(selectedGame); } } @@ -97,6 +104,7 @@ public partial class MainWindow : Window { if (GamesList.SelectedItem is Game selectedGame) { + Console.Info("Uninstalling " + selectedGame); selectedGame.Uninstall(); RefreshGamesListTrigger?.Invoke(sender, e); } diff --git a/PS2_Manager/MessageBox.axaml b/PS2_Manager/MessageBox.axaml new file mode 100644 index 0000000..cdb1879 --- /dev/null +++ b/PS2_Manager/MessageBox.axaml @@ -0,0 +1,31 @@ + + + + + + + + + + + + + + + + + + + + diff --git a/PS2_Manager/MessageBox.axaml.cs b/PS2_Manager/MessageBox.axaml.cs new file mode 100644 index 0000000..87c9677 --- /dev/null +++ b/PS2_Manager/MessageBox.axaml.cs @@ -0,0 +1,22 @@ +using Avalonia; +using Avalonia.Controls; +using Avalonia.Input; +using Avalonia.Markup.Xaml; + +namespace PS2_Manager; + +public partial class MessageBox : Window +{ + public MessageBox(string messageContent) + { + InitializeComponent(); + ErrorDump.Content = "Exception: " + messageContent; + } + private void WindowDrag(object? sender, PointerPressedEventArgs e) + { + if (e.GetCurrentPoint(this).Properties.IsLeftButtonPressed) + { + BeginMoveDrag(e); + } + } +} \ No newline at end of file diff --git a/PS2_Manager/Setup.axaml.cs b/PS2_Manager/Setup.axaml.cs index 63183e0..7fcde28 100644 --- a/PS2_Manager/Setup.axaml.cs +++ b/PS2_Manager/Setup.axaml.cs @@ -27,6 +27,7 @@ public partial class Setup : Window private void FinishSetup() { + Console.Success("Setup Complete"); InfoText1.Text = "Setup Finished"; InfoText2.Text = "You can now exit to the Main Application"; OpenLibraryButton.Content = "Exit"; @@ -40,6 +41,7 @@ public partial class Setup : Window private async void OpenLibraryButton_OnClick(object? sender, RoutedEventArgs e) { + Console.Info("Open Library Button Clicked"); settings.library_path.SetValue(""); while (String.IsNullOrEmpty(settings.library_path.GetValue())) { diff --git a/PS2_Manager/Welcome.axaml.cs b/PS2_Manager/Welcome.axaml.cs index eeb3760..9fd5cf1 100644 --- a/PS2_Manager/Welcome.axaml.cs +++ b/PS2_Manager/Welcome.axaml.cs @@ -14,7 +14,7 @@ public partial class Welcome : UserControl } private void OnRepositoryLinkClicked(object sender, RoutedEventArgs e) { - // Open the link in the default web browser + Console.Info("Repository Link Clicked"); Process.Start(new ProcessStartInfo { FileName = "https://git.weexnes.dev/WeeXnes/ps2_manager",