From 2b138a8f15dfe411b9f7de8d6ba7fbbb96de843c Mon Sep 17 00:00:00 2001 From: Miepee Date: Sat, 14 Jan 2023 02:06:55 +0100 Subject: [PATCH] Remove a whole lot of unnecessary checks before creating/deleting dirs --- AM2RLauncher/AM2RLauncher.Gtk/Program.cs | 3 +- AM2RLauncher/AM2RLauncher.Mac/Program.cs | 3 +- AM2RLauncher/AM2RLauncher.Wpf/Program.cs | 3 +- .../AM2RLauncher/MainForm/MainForm.Events.cs | 29 +++++------ .../AM2RLauncher/MainForm/MainForm.Methods.cs | 13 ++--- .../CrossPlatformOperations.cs | 8 ++-- AM2RLauncher/AM2RLauncherLib/Profile.cs | 48 +++++++------------ 7 files changed, 41 insertions(+), 66 deletions(-) diff --git a/AM2RLauncher/AM2RLauncher.Gtk/Program.cs b/AM2RLauncher/AM2RLauncher.Gtk/Program.cs index bfff06f..223c7a6 100644 --- a/AM2RLauncher/AM2RLauncher.Gtk/Program.cs +++ b/AM2RLauncher/AM2RLauncher.Gtk/Program.cs @@ -33,8 +33,7 @@ internal static class MainClass string launcherDataPath = CrossPlatformOperations.CurrentPath; // Make sure first, ~/.local/share/AM2RLauncher exists - if (!Directory.Exists(launcherDataPath)) - Directory.CreateDirectory(launcherDataPath); + Directory.CreateDirectory(launcherDataPath); // Now, see if log4netConfig exists, if not write it again. if (!File.Exists($"{launcherDataPath}/log4net.config")) diff --git a/AM2RLauncher/AM2RLauncher.Mac/Program.cs b/AM2RLauncher/AM2RLauncher.Mac/Program.cs index 628edb2..0f31465 100644 --- a/AM2RLauncher/AM2RLauncher.Mac/Program.cs +++ b/AM2RLauncher/AM2RLauncher.Mac/Program.cs @@ -29,8 +29,7 @@ internal static class MainClass string launcherDataPath = CrossPlatformOperations.CurrentPath; // Make sure first, ~/.local/share/AM2RLauncher exists - if (!Directory.Exists(launcherDataPath)) - Directory.CreateDirectory(launcherDataPath); + Directory.CreateDirectory(launcherDataPath); // Now, see if log4netConfig exists, if not write it again. if (!File.Exists($"{launcherDataPath}/log4net.config")) diff --git a/AM2RLauncher/AM2RLauncher.Wpf/Program.cs b/AM2RLauncher/AM2RLauncher.Wpf/Program.cs index 78821e5..2baa765 100644 --- a/AM2RLauncher/AM2RLauncher.Wpf/Program.cs +++ b/AM2RLauncher/AM2RLauncher.Wpf/Program.cs @@ -28,8 +28,7 @@ internal static class MainClass string launcherDataPath = CrossPlatformOperations.CurrentPath; // Make sure first, that the path exists - if (!Directory.Exists(launcherDataPath)) - Directory.CreateDirectory(launcherDataPath); + Directory.CreateDirectory(launcherDataPath); // Now, see if log4netConfig exists, if not write it again. if (!File.Exists($"{launcherDataPath}/log4net.config")) diff --git a/AM2RLauncher/AM2RLauncher/MainForm/MainForm.Events.cs b/AM2RLauncher/AM2RLauncher/MainForm/MainForm.Events.cs index b32e840..3cb571a 100644 --- a/AM2RLauncher/AM2RLauncher/MainForm/MainForm.Events.cs +++ b/AM2RLauncher/AM2RLauncher/MainForm/MainForm.Events.cs @@ -245,11 +245,8 @@ public partial class MainForm : Form try { // Cleanup invalid PatchData directory if it exists - if (Directory.Exists(Core.PatchDataPath)) - { - log.Info("PatchData directory already exists, cleaning up..."); - HelperMethods.DeleteDirectory(Core.PatchDataPath); - } + log.Info("Deleting PatchData if it already exists."); + HelperMethods.DeleteDirectory(Core.PatchDataPath); // Separate thread so launcher doesn't get locked await Task.Run(() => Repository.Clone(currentMirror, Core.PatchDataPath, cloneOptions)); @@ -274,8 +271,7 @@ public partial class MainForm : Form { log.Error("LibGit2SharpException: " + ex.Message + "\n*****Stack Trace*****\n\n" + ex.StackTrace); MessageBox.Show(this, ex.Message + "\n*****Stack Trace*****\n\n" + ex.StackTrace, Text.ErrorWindowTitle, MessageBoxType.Error); - if (Directory.Exists(Core.PatchDataPath)) - HelperMethods.DeleteDirectory(Core.PatchDataPath); + HelperMethods.DeleteDirectory(Core.PatchDataPath); } successful = false; } @@ -284,9 +280,8 @@ public partial class MainForm : Form { log.Error(ex.Message + "\n*****Stack Trace*****\n\n" + ex.StackTrace); MessageBox.Show(this, ex.Message + "\n*****Stack Trace*****\n\n" + ex.StackTrace, Text.ErrorWindowTitle, MessageBoxType.Error); - - if (Directory.Exists(CrossPlatformOperations.CurrentPath + " / PatchData")) - HelperMethods.DeleteDirectory(Core.PatchDataPath); + + HelperMethods.DeleteDirectory(Core.PatchDataPath); successful = false; } @@ -445,7 +440,7 @@ public partial class MainForm : Form { await Task.Run(() => Profile.RunGame(profile, createDebugLogs)); } - catch(Exception ex) + catch (Exception ex) { MessageBox.Show($"{Text.UnhandledException}\n*****Stack Trace*****\n\n{ex}", Text.ErrorWindowTitle, MessageBoxType.Error); } @@ -735,7 +730,7 @@ public partial class MainForm : Form { log.Error(modFile.Name + " does not contain profile.xml! Cancelling mod import."); MessageBox.Show(this, HelperMethods.GetText(Text.ModIsInvalidMessage, modFileName), Text.ErrorWindowTitle, MessageBoxType.Error); - Directory.Delete(extractedModDir, true); + HelperMethods.DeleteDirectory(extractedModDir); return; } @@ -799,8 +794,9 @@ public partial class MainForm : Form updateModButton.ToolTip = HelperMethods.GetText(Text.UpdateModButtonToolTip, profileName); } - desktopShortcutButton.Enabled = Directory.Exists(Core.ProfilesPath + "/" + profileName); - profileButton.Enabled = Directory.Exists(Core.ProfilesPath + "/" + profileName); + bool doesProfilePathExist = Directory.Exists(Core.ProfilesPath + "/" + profileName); + desktopShortcutButton.Enabled = doesProfilePathExist; + profileButton.Enabled = doesProfilePathExist; profileButton.ToolTip = HelperMethods.GetText(Text.OpenProfileFolderToolTip, profileName); saveButton.Enabled = true; saveButton.ToolTip = HelperMethods.GetText(Text.OpenSaveFolderToolTip, profileName); @@ -994,8 +990,7 @@ public partial class MainForm : Form string extractedModDir = Core.ModsPath + "/" + extractedName; // If for some reason old files remain, delete them so that extraction doesn't throw - if (Directory.Exists(extractedModDir)) - Directory.Delete(extractedModDir, true); + HelperMethods.DeleteDirectory(extractedModDir); // Directory doesn't exist -> extract! ZipFile.ExtractToDirectory(fileFinder.FileName, extractedModDir); @@ -1005,7 +1000,7 @@ public partial class MainForm : Form { log.Error(fileFinder.FileName + " does not contain profile.xml! Cancelling mod update."); MessageBox.Show(this, HelperMethods.GetText(Text.ModIsInvalidMessage, extractedName), Text.ErrorWindowTitle, MessageBoxType.Error); - Directory.Delete(extractedModDir, true); + HelperMethods.DeleteDirectory(extractedModDir); return; } diff --git a/AM2RLauncher/AM2RLauncher/MainForm/MainForm.Methods.cs b/AM2RLauncher/AM2RLauncher/MainForm/MainForm.Methods.cs index 1422b1e..4914db3 100644 --- a/AM2RLauncher/AM2RLauncher/MainForm/MainForm.Methods.cs +++ b/AM2RLauncher/AM2RLauncher/MainForm/MainForm.Methods.cs @@ -139,11 +139,9 @@ public partial class MainForm : Form XML.LauncherConfigXML launcherConfig = new XML.LauncherConfigXML(); // If folder doesn't exist, create it and the config file - if (!Directory.Exists(launcherConfigPath) || !File.Exists(launcherConfigFilePath)) - { - Directory.CreateDirectory(launcherConfigPath); + Directory.CreateDirectory(launcherConfigPath); + if (!File.Exists(launcherConfigFilePath)) File.WriteAllText(launcherConfigFilePath, Serializer.Serialize(launcherConfig)); - } // Deserialize the config xml into launcherConfig launcherConfig = Serializer.Deserialize(File.ReadAllText(launcherConfigFilePath)); @@ -186,11 +184,10 @@ public partial class MainForm : Form XML.LauncherConfigXML launcherConfig = new XML.LauncherConfigXML(); // If folder doesn't exist, create it and the config file - if (!Directory.Exists(launcherConfigPath) || !File.Exists(launcherConfigFilePath)) - { - Directory.CreateDirectory(launcherConfigPath); + Directory.CreateDirectory(launcherConfigPath); + if (!File.Exists(launcherConfigFilePath)) File.WriteAllText(launcherConfigFilePath, Serializer.Serialize(launcherConfig)); - } + // Deserialize the config xml into launcherConfig launcherConfig = Serializer.Deserialize(File.ReadAllText(launcherConfigFilePath)); diff --git a/AM2RLauncher/AM2RLauncherLib/CrossPlatformOperations.cs b/AM2RLauncher/AM2RLauncherLib/CrossPlatformOperations.cs index bccf2c5..4695df7 100644 --- a/AM2RLauncher/AM2RLauncherLib/CrossPlatformOperations.cs +++ b/AM2RLauncher/AM2RLauncherLib/CrossPlatformOperations.cs @@ -114,11 +114,9 @@ public static class CrossPlatformOperations // And on Nix systems, we want to replace ~ with its corresponding env var string realPath = OS.IsWindows ? Environment.ExpandEnvironmentVariables(path).Replace("/", "\\") : path.Replace("~", Home); - if (!Directory.Exists(realPath)) - { - log.Info($"{realPath} did not exist and was created"); - Directory.CreateDirectory(realPath); - } + + log.Info($"Creating {realPath} if it did not exist before"); + Directory.CreateDirectory(realPath); // Needs quotes otherwise paths with space wont open if (OS.IsWindows) diff --git a/AM2RLauncher/AM2RLauncherLib/Profile.cs b/AM2RLauncher/AM2RLauncherLib/Profile.cs index 1350538..a991a60 100644 --- a/AM2RLauncher/AM2RLauncherLib/Profile.cs +++ b/AM2RLauncher/AM2RLauncherLib/Profile.cs @@ -100,8 +100,7 @@ public static class Profile string tmpPath = Path.GetTempPath() + "/" + Path.GetFileNameWithoutExtension(zipPath); // Clean up in case folder exists already - if (Directory.Exists(tmpPath)) - Directory.Delete(tmpPath, true); + HelperMethods.DeleteDirectory(tmpPath); Directory.CreateDirectory(tmpPath); // Open archive @@ -144,7 +143,7 @@ public static class Profile // Clean up - Directory.Delete(tmpPath, true); + HelperMethods.DeleteDirectory(tmpPath); // If we didn't exit before, everything is fine log.Info("AM2R_11 check successful!"); @@ -217,8 +216,7 @@ public static class Profile } // Safety check to generate the Mods folder if it does not exist - if (!Directory.Exists(Core.ModsPath)) - Directory.CreateDirectory(Core.ModsPath); + Directory.CreateDirectory(Core.ModsPath); // Get Mods folder info DirectoryInfo modsDir = new DirectoryInfo(Core.ModsPath); @@ -307,8 +305,7 @@ public static class Profile log.Info($"Attempting to delete profile {profile.Name}..."); // Delete folder in Mods - if (Directory.Exists(CrossPlatformOperations.CurrentPath + profile.DataPath)) - HelperMethods.DeleteDirectory(CrossPlatformOperations.CurrentPath + profile.DataPath); + HelperMethods.DeleteDirectory(CrossPlatformOperations.CurrentPath + profile.DataPath); // Delete the zip file in Mods if (File.Exists($"{CrossPlatformOperations.CurrentPath}{profile.DataPath}.zip")) @@ -319,8 +316,7 @@ public static class Profile } // Delete folder in Profiles - if (Directory.Exists($"{Core.ProfilesPath}/{profile.Name}")) - HelperMethods.DeleteDirectory($"{Core.ProfilesPath}/{profile.Name}"); + HelperMethods.DeleteDirectory($"{Core.ProfilesPath}/{profile.Name}"); log.Info($"Successfully deleted profile {profile.Name}."); } @@ -339,12 +335,10 @@ public static class Profile string tempPath = $"{Path.GetTempPath()}/AM2RLauncherProfileTemp/"; // Failsafe for Profiles directory - if (!Directory.Exists(Core.ProfilesPath)) - Directory.CreateDirectory(Core.ProfilesPath); + Directory.CreateDirectory(Core.ProfilesPath); // Delete temp if it exists + create it - if (Directory.Exists(tempPath)) - Directory.Delete(tempPath, true); + HelperMethods.DeleteDirectory(tempPath); Directory.CreateDirectory(tempPath); // Switch profilePath on Linux and Mac, as they need special handling @@ -507,10 +501,10 @@ public static class Profile Console.SetError(cliError); // Clean files - Directory.Delete(assetsPath, true); + HelperMethods.DeleteDirectory(assetsPath); File.Delete($"{tempPath}/{exe}"); - Directory.Delete($"{tempPath}/AM2R.AppDir", true); - if (File.Exists($"{tempPath}/AM2R.AppImage")) File.Delete($"{tempPath}/AM2R.AppImage"); + HelperMethods.DeleteDirectory($"{tempPath}/AM2R.AppDir"); + File.Delete($"{tempPath}/AM2R.AppImage"); File.Move($"{tempPath}/AM2R-x86_64.AppImage", $"{tempPath}/AM2R.AppImage"); log.Info("AppImage created!"); #endif @@ -526,8 +520,7 @@ public static class Profile } // Loading custom fonts crashes on Mac, so we delete those if they exist - if (Directory.Exists($"{tempPath}/lang/fonts")) - Directory.Delete($"{tempPath}/lang/fonts", true); + HelperMethods.DeleteDirectory($"{tempPath}/lang/fonts"); // Move Frameworks, Info.plist and PkgInfo over HelperMethods.DirectoryCopy($"{Core.PatchDataPath}/data/Frameworks", tempPath.Replace("Resources", "Frameworks")); @@ -546,8 +539,7 @@ public static class Profile File.Copy($"{dataPath}/profile.xml", $"{tempPath}/profile.xml"); // This failsafe should NEVER get triggered, but Miepee's broken this too much for me to trust it otherwise. - if (Directory.Exists(profilePath)) - Directory.Delete(profilePath, true); + HelperMethods.DeleteDirectory(profilePath); HelperMethods.DirectoryCopy(tempPath, profilePath); log.Info("Moved from temp path into the profile path!"); @@ -606,8 +598,7 @@ public static class Profile string dataPath = CrossPlatformOperations.CurrentPath + profile.DataPath; // Clean up in case Directory exists already - if (Directory.Exists(tempDir)) - Directory.Delete(tempDir, true); + HelperMethods.DeleteDirectory(tempDir); Directory.CreateDirectory(tempDir); log.Info("Cleanup, variables, and working directory created."); @@ -670,8 +661,8 @@ public static class Profile File.Delete($"{workingDir}/modifiers.ini"); File.Delete($"{workingDir}/readme.txt"); File.Delete($"{workingDir}/data.win"); - Directory.Delete($"{workingDir}/mods", true); - Directory.Delete($"{workingDir}/lang/headers", true); + HelperMethods.DeleteDirectory($"{workingDir}/mods"); + HelperMethods.DeleteDirectory($"{workingDir}/lang/headers"); if (OS.IsLinux) File.Delete($"{workingDir}/icon.png"); // Modify apktool.yml to NOT compress ogg files @@ -731,8 +722,7 @@ public static class Profile log.Info($"Performing logging setup for profile {profile.Name}."); // Check if directory exists, and roll log file over if necessary - if (!Directory.Exists(logDir.FullName)) - Directory.CreateDirectory(logDir.FullName); + Directory.CreateDirectory(logDir.FullName); if (File.Exists(logFile)) HelperMethods.RecursiveRollover(logFile, 5); @@ -802,8 +792,7 @@ public static class Profile log.Info($"Performing logging setup for profile {profile.Name}."); // Check if directory exists, and roll log file over if necessary - if (!Directory.Exists(logDir.FullName)) - Directory.CreateDirectory(logDir.FullName); + Directory.CreateDirectory(logDir.FullName); if (File.Exists(logFile)) HelperMethods.RecursiveRollover(logFile, 5); @@ -839,8 +828,7 @@ public static class Profile log.Info($"Performing logging setup for profile {profile.Name}."); // Check if directory exists, and roll log file over if necessary - if (!Directory.Exists(logDir.FullName)) - Directory.CreateDirectory(logDir.FullName); + Directory.CreateDirectory(logDir.FullName); if (File.Exists(logFile)) HelperMethods.RecursiveRollover(logFile, 5);