From f8fde6c07969e55c80b965a3aef724d8469634f2 Mon Sep 17 00:00:00 2001 From: Peterson Fernandes Date: Sat, 30 Mar 2024 23:48:40 -0300 Subject: [PATCH] Refactor code to improve error handling and game button creation This commit introduces better error handling for file loading operations such as the system.xml and mame.xml. If these files are missing or corrupted, the application now shuts down, advising the user to reinstall the software. In addition, the game button creation was optimized to prioritize the machine's description as the search term, if it exists, for the YouTube video and info link functionalities. The system's edit page now creates an image folder for new systems, if not specified by the user. --- SimpleLauncher/EditSystem.xaml.cs | 43 +++++++++++++++++++++++++++++ SimpleLauncher/GameButtonFactory.cs | 35 +++++++++++------------ SimpleLauncher/MainWindow.xaml.cs | 2 +- SimpleLauncher/MameConfig.cs | 30 +++++++++++++++----- SimpleLauncher/SystemConfig.cs | 29 +++++++++++++------ 5 files changed, 106 insertions(+), 33 deletions(-) diff --git a/SimpleLauncher/EditSystem.xaml.cs b/SimpleLauncher/EditSystem.xaml.cs index 29bc5c6..1bc7a71 100644 --- a/SimpleLauncher/EditSystem.xaml.cs +++ b/SimpleLauncher/EditSystem.xaml.cs @@ -2,6 +2,7 @@ using System.Diagnostics; using System.IO; using System.Linq; +using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Forms; @@ -341,6 +342,10 @@ private void AddSystemButton_Click(object sender, RoutedEventArgs e) { ClearFields(); AdjustPlaceholderVisibility(); + + SaveSystemButton.IsEnabled = true; + DeleteSystemButton.IsEnabled = false; + MessageBox.Show("You can add a new system now.", "Info", MessageBoxButton.OK, MessageBoxImage.Information); } @@ -535,6 +540,44 @@ orderby system.Element("SystemName")?.Value MessageBox.Show("System saved successfully.", "Info", MessageBoxButton.OK, MessageBoxImage.Information); + + // Create a folder inside images folder with the same name as SystemName + // only do that if user does not provide the SystemImageFolder + if (string.IsNullOrWhiteSpace(systemImageFolder)) + { + // Get the application directory + string applicationDirectory = AppDomain.CurrentDomain.BaseDirectory; + + // Construct the path to the 'images' directory + string imagesDirectory = Path.Combine(applicationDirectory, "images"); + + // Check if 'images' directory exists + if (!Directory.Exists(imagesDirectory)) + { + Directory.CreateDirectory(imagesDirectory); + } + + // Use SystemName as the name for the new folder inside 'images' + string newFolderPath = Path.Combine(imagesDirectory, systemName); + + try + { + // Check if the folder exists, and create it if it doesn't + if (!Directory.Exists(newFolderPath)) + { + Directory.CreateDirectory(newFolderPath); + MessageBox.Show($"I have also created a folder for this System within the 'images' folder at {newFolderPath}.\n\nYou may place the cover images for this System inside this folder.", "Info", MessageBoxButton.OK, MessageBoxImage.Information); + } + } + catch (Exception exception) + { + string formattedException = $"The Simple Launcher failed to create an image folder for the newly created system.\n\nException detail: {exception}"; + Task logTask = LogErrors.LogErrorAsync(exception, formattedException); + logTask.Wait(TimeSpan.FromSeconds(2)); + throw; + } + + } } private static void AddEmulatorToXml(XElement emulatorsElement, string name, string location, string parameters) diff --git a/SimpleLauncher/GameButtonFactory.cs b/SimpleLauncher/GameButtonFactory.cs index bea37cc..aad5580 100644 --- a/SimpleLauncher/GameButtonFactory.cs +++ b/SimpleLauncher/GameButtonFactory.cs @@ -29,15 +29,14 @@ internal class GameButtonFactory( public async Task