Remove a whole lot of unnecessary checks before creating/deleting dirs

pull/45/head
Miepee 3 years ago
parent d1419c6a08
commit 2b138a8f15

@ -33,8 +33,7 @@ internal static class MainClass
string launcherDataPath = CrossPlatformOperations.CurrentPath; string launcherDataPath = CrossPlatformOperations.CurrentPath;
// Make sure first, ~/.local/share/AM2RLauncher exists // 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. // Now, see if log4netConfig exists, if not write it again.
if (!File.Exists($"{launcherDataPath}/log4net.config")) if (!File.Exists($"{launcherDataPath}/log4net.config"))

@ -29,8 +29,7 @@ internal static class MainClass
string launcherDataPath = CrossPlatformOperations.CurrentPath; string launcherDataPath = CrossPlatformOperations.CurrentPath;
// Make sure first, ~/.local/share/AM2RLauncher exists // 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. // Now, see if log4netConfig exists, if not write it again.
if (!File.Exists($"{launcherDataPath}/log4net.config")) if (!File.Exists($"{launcherDataPath}/log4net.config"))

@ -28,8 +28,7 @@ internal static class MainClass
string launcherDataPath = CrossPlatformOperations.CurrentPath; string launcherDataPath = CrossPlatformOperations.CurrentPath;
// Make sure first, that the path exists // 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. // Now, see if log4netConfig exists, if not write it again.
if (!File.Exists($"{launcherDataPath}/log4net.config")) if (!File.Exists($"{launcherDataPath}/log4net.config"))

@ -245,11 +245,8 @@ public partial class MainForm : Form
try try
{ {
// Cleanup invalid PatchData directory if it exists // Cleanup invalid PatchData directory if it exists
if (Directory.Exists(Core.PatchDataPath)) log.Info("Deleting PatchData if it already exists.");
{ HelperMethods.DeleteDirectory(Core.PatchDataPath);
log.Info("PatchData directory already exists, cleaning up...");
HelperMethods.DeleteDirectory(Core.PatchDataPath);
}
// Separate thread so launcher doesn't get locked // Separate thread so launcher doesn't get locked
await Task.Run(() => Repository.Clone(currentMirror, Core.PatchDataPath, cloneOptions)); 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); 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); 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; successful = false;
} }
@ -284,9 +280,8 @@ public partial class MainForm : Form
{ {
log.Error(ex.Message + "\n*****Stack Trace*****\n\n" + ex.StackTrace); 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); 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; successful = false;
} }
@ -445,7 +440,7 @@ public partial class MainForm : Form
{ {
await Task.Run(() => Profile.RunGame(profile, createDebugLogs)); 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); 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."); log.Error(modFile.Name + " does not contain profile.xml! Cancelling mod import.");
MessageBox.Show(this, HelperMethods.GetText(Text.ModIsInvalidMessage, modFileName), Text.ErrorWindowTitle, MessageBoxType.Error); MessageBox.Show(this, HelperMethods.GetText(Text.ModIsInvalidMessage, modFileName), Text.ErrorWindowTitle, MessageBoxType.Error);
Directory.Delete(extractedModDir, true); HelperMethods.DeleteDirectory(extractedModDir);
return; return;
} }
@ -799,8 +794,9 @@ public partial class MainForm : Form
updateModButton.ToolTip = HelperMethods.GetText(Text.UpdateModButtonToolTip, profileName); updateModButton.ToolTip = HelperMethods.GetText(Text.UpdateModButtonToolTip, profileName);
} }
desktopShortcutButton.Enabled = Directory.Exists(Core.ProfilesPath + "/" + profileName); bool doesProfilePathExist = Directory.Exists(Core.ProfilesPath + "/" + profileName);
profileButton.Enabled = Directory.Exists(Core.ProfilesPath + "/" + profileName); desktopShortcutButton.Enabled = doesProfilePathExist;
profileButton.Enabled = doesProfilePathExist;
profileButton.ToolTip = HelperMethods.GetText(Text.OpenProfileFolderToolTip, profileName); profileButton.ToolTip = HelperMethods.GetText(Text.OpenProfileFolderToolTip, profileName);
saveButton.Enabled = true; saveButton.Enabled = true;
saveButton.ToolTip = HelperMethods.GetText(Text.OpenSaveFolderToolTip, profileName); saveButton.ToolTip = HelperMethods.GetText(Text.OpenSaveFolderToolTip, profileName);
@ -994,8 +990,7 @@ public partial class MainForm : Form
string extractedModDir = Core.ModsPath + "/" + extractedName; string extractedModDir = Core.ModsPath + "/" + extractedName;
// If for some reason old files remain, delete them so that extraction doesn't throw // If for some reason old files remain, delete them so that extraction doesn't throw
if (Directory.Exists(extractedModDir)) HelperMethods.DeleteDirectory(extractedModDir);
Directory.Delete(extractedModDir, true);
// Directory doesn't exist -> extract! // Directory doesn't exist -> extract!
ZipFile.ExtractToDirectory(fileFinder.FileName, extractedModDir); 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."); log.Error(fileFinder.FileName + " does not contain profile.xml! Cancelling mod update.");
MessageBox.Show(this, HelperMethods.GetText(Text.ModIsInvalidMessage, extractedName), Text.ErrorWindowTitle, MessageBoxType.Error); MessageBox.Show(this, HelperMethods.GetText(Text.ModIsInvalidMessage, extractedName), Text.ErrorWindowTitle, MessageBoxType.Error);
Directory.Delete(extractedModDir, true); HelperMethods.DeleteDirectory(extractedModDir);
return; return;
} }

@ -139,11 +139,9 @@ public partial class MainForm : Form
XML.LauncherConfigXML launcherConfig = new XML.LauncherConfigXML(); XML.LauncherConfigXML launcherConfig = new XML.LauncherConfigXML();
// If folder doesn't exist, create it and the config file // If folder doesn't exist, create it and the config file
if (!Directory.Exists(launcherConfigPath) || !File.Exists(launcherConfigFilePath)) Directory.CreateDirectory(launcherConfigPath);
{ if (!File.Exists(launcherConfigFilePath))
Directory.CreateDirectory(launcherConfigPath);
File.WriteAllText(launcherConfigFilePath, Serializer.Serialize<XML.LauncherConfigXML>(launcherConfig)); File.WriteAllText(launcherConfigFilePath, Serializer.Serialize<XML.LauncherConfigXML>(launcherConfig));
}
// Deserialize the config xml into launcherConfig // Deserialize the config xml into launcherConfig
launcherConfig = Serializer.Deserialize<XML.LauncherConfigXML>(File.ReadAllText(launcherConfigFilePath)); launcherConfig = Serializer.Deserialize<XML.LauncherConfigXML>(File.ReadAllText(launcherConfigFilePath));
@ -186,11 +184,10 @@ public partial class MainForm : Form
XML.LauncherConfigXML launcherConfig = new XML.LauncherConfigXML(); XML.LauncherConfigXML launcherConfig = new XML.LauncherConfigXML();
// If folder doesn't exist, create it and the config file // If folder doesn't exist, create it and the config file
if (!Directory.Exists(launcherConfigPath) || !File.Exists(launcherConfigFilePath)) Directory.CreateDirectory(launcherConfigPath);
{ if (!File.Exists(launcherConfigFilePath))
Directory.CreateDirectory(launcherConfigPath);
File.WriteAllText(launcherConfigFilePath, Serializer.Serialize<XML.LauncherConfigXML>(launcherConfig)); File.WriteAllText(launcherConfigFilePath, Serializer.Serialize<XML.LauncherConfigXML>(launcherConfig));
}
// Deserialize the config xml into launcherConfig // Deserialize the config xml into launcherConfig
launcherConfig = Serializer.Deserialize<XML.LauncherConfigXML>(File.ReadAllText(launcherConfigFilePath)); launcherConfig = Serializer.Deserialize<XML.LauncherConfigXML>(File.ReadAllText(launcherConfigFilePath));

@ -114,11 +114,9 @@ public static class CrossPlatformOperations
// And on Nix systems, we want to replace ~ with its corresponding env var // And on Nix systems, we want to replace ~ with its corresponding env var
string realPath = OS.IsWindows ? Environment.ExpandEnvironmentVariables(path).Replace("/", "\\") string realPath = OS.IsWindows ? Environment.ExpandEnvironmentVariables(path).Replace("/", "\\")
: path.Replace("~", Home); : path.Replace("~", Home);
if (!Directory.Exists(realPath))
{ log.Info($"Creating {realPath} if it did not exist before");
log.Info($"{realPath} did not exist and was created"); Directory.CreateDirectory(realPath);
Directory.CreateDirectory(realPath);
}
// Needs quotes otherwise paths with space wont open // Needs quotes otherwise paths with space wont open
if (OS.IsWindows) if (OS.IsWindows)

@ -100,8 +100,7 @@ public static class Profile
string tmpPath = Path.GetTempPath() + "/" + Path.GetFileNameWithoutExtension(zipPath); string tmpPath = Path.GetTempPath() + "/" + Path.GetFileNameWithoutExtension(zipPath);
// Clean up in case folder exists already // Clean up in case folder exists already
if (Directory.Exists(tmpPath)) HelperMethods.DeleteDirectory(tmpPath);
Directory.Delete(tmpPath, true);
Directory.CreateDirectory(tmpPath); Directory.CreateDirectory(tmpPath);
// Open archive // Open archive
@ -144,7 +143,7 @@ public static class Profile
// Clean up // Clean up
Directory.Delete(tmpPath, true); HelperMethods.DeleteDirectory(tmpPath);
// If we didn't exit before, everything is fine // If we didn't exit before, everything is fine
log.Info("AM2R_11 check successful!"); 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 // 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 // Get Mods folder info
DirectoryInfo modsDir = new DirectoryInfo(Core.ModsPath); DirectoryInfo modsDir = new DirectoryInfo(Core.ModsPath);
@ -307,8 +305,7 @@ public static class Profile
log.Info($"Attempting to delete profile {profile.Name}..."); log.Info($"Attempting to delete profile {profile.Name}...");
// Delete folder in Mods // 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 // Delete the zip file in Mods
if (File.Exists($"{CrossPlatformOperations.CurrentPath}{profile.DataPath}.zip")) if (File.Exists($"{CrossPlatformOperations.CurrentPath}{profile.DataPath}.zip"))
@ -319,8 +316,7 @@ public static class Profile
} }
// Delete folder in Profiles // 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}."); log.Info($"Successfully deleted profile {profile.Name}.");
} }
@ -339,12 +335,10 @@ public static class Profile
string tempPath = $"{Path.GetTempPath()}/AM2RLauncherProfileTemp/"; string tempPath = $"{Path.GetTempPath()}/AM2RLauncherProfileTemp/";
// Failsafe for Profiles directory // Failsafe for Profiles directory
if (!Directory.Exists(Core.ProfilesPath)) Directory.CreateDirectory(Core.ProfilesPath);
Directory.CreateDirectory(Core.ProfilesPath);
// Delete temp if it exists + create it // Delete temp if it exists + create it
if (Directory.Exists(tempPath)) HelperMethods.DeleteDirectory(tempPath);
Directory.Delete(tempPath, true);
Directory.CreateDirectory(tempPath); Directory.CreateDirectory(tempPath);
// Switch profilePath on Linux and Mac, as they need special handling // Switch profilePath on Linux and Mac, as they need special handling
@ -507,10 +501,10 @@ public static class Profile
Console.SetError(cliError); Console.SetError(cliError);
// Clean files // Clean files
Directory.Delete(assetsPath, true); HelperMethods.DeleteDirectory(assetsPath);
File.Delete($"{tempPath}/{exe}"); File.Delete($"{tempPath}/{exe}");
Directory.Delete($"{tempPath}/AM2R.AppDir", true); HelperMethods.DeleteDirectory($"{tempPath}/AM2R.AppDir");
if (File.Exists($"{tempPath}/AM2R.AppImage")) File.Delete($"{tempPath}/AM2R.AppImage"); File.Delete($"{tempPath}/AM2R.AppImage");
File.Move($"{tempPath}/AM2R-x86_64.AppImage", $"{tempPath}/AM2R.AppImage"); File.Move($"{tempPath}/AM2R-x86_64.AppImage", $"{tempPath}/AM2R.AppImage");
log.Info("AppImage created!"); log.Info("AppImage created!");
#endif #endif
@ -526,8 +520,7 @@ public static class Profile
} }
// Loading custom fonts crashes on Mac, so we delete those if they exist // Loading custom fonts crashes on Mac, so we delete those if they exist
if (Directory.Exists($"{tempPath}/lang/fonts")) HelperMethods.DeleteDirectory($"{tempPath}/lang/fonts");
Directory.Delete($"{tempPath}/lang/fonts", true);
// Move Frameworks, Info.plist and PkgInfo over // Move Frameworks, Info.plist and PkgInfo over
HelperMethods.DirectoryCopy($"{Core.PatchDataPath}/data/Frameworks", tempPath.Replace("Resources", "Frameworks")); 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"); 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. // This failsafe should NEVER get triggered, but Miepee's broken this too much for me to trust it otherwise.
if (Directory.Exists(profilePath)) HelperMethods.DeleteDirectory(profilePath);
Directory.Delete(profilePath, true);
HelperMethods.DirectoryCopy(tempPath, profilePath); HelperMethods.DirectoryCopy(tempPath, profilePath);
log.Info("Moved from temp path into the profile path!"); log.Info("Moved from temp path into the profile path!");
@ -606,8 +598,7 @@ public static class Profile
string dataPath = CrossPlatformOperations.CurrentPath + profile.DataPath; string dataPath = CrossPlatformOperations.CurrentPath + profile.DataPath;
// Clean up in case Directory exists already // Clean up in case Directory exists already
if (Directory.Exists(tempDir)) HelperMethods.DeleteDirectory(tempDir);
Directory.Delete(tempDir, true);
Directory.CreateDirectory(tempDir); Directory.CreateDirectory(tempDir);
log.Info("Cleanup, variables, and working directory created."); log.Info("Cleanup, variables, and working directory created.");
@ -670,8 +661,8 @@ public static class Profile
File.Delete($"{workingDir}/modifiers.ini"); File.Delete($"{workingDir}/modifiers.ini");
File.Delete($"{workingDir}/readme.txt"); File.Delete($"{workingDir}/readme.txt");
File.Delete($"{workingDir}/data.win"); File.Delete($"{workingDir}/data.win");
Directory.Delete($"{workingDir}/mods", true); HelperMethods.DeleteDirectory($"{workingDir}/mods");
Directory.Delete($"{workingDir}/lang/headers", true); HelperMethods.DeleteDirectory($"{workingDir}/lang/headers");
if (OS.IsLinux) File.Delete($"{workingDir}/icon.png"); if (OS.IsLinux) File.Delete($"{workingDir}/icon.png");
// Modify apktool.yml to NOT compress ogg files // 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}."); log.Info($"Performing logging setup for profile {profile.Name}.");
// Check if directory exists, and roll log file over if necessary // 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)) if (File.Exists(logFile))
HelperMethods.RecursiveRollover(logFile, 5); HelperMethods.RecursiveRollover(logFile, 5);
@ -802,8 +792,7 @@ public static class Profile
log.Info($"Performing logging setup for profile {profile.Name}."); log.Info($"Performing logging setup for profile {profile.Name}.");
// Check if directory exists, and roll log file over if necessary // 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)) if (File.Exists(logFile))
HelperMethods.RecursiveRollover(logFile, 5); HelperMethods.RecursiveRollover(logFile, 5);
@ -839,8 +828,7 @@ public static class Profile
log.Info($"Performing logging setup for profile {profile.Name}."); log.Info($"Performing logging setup for profile {profile.Name}.");
// Check if directory exists, and roll log file over if necessary // 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)) if (File.Exists(logFile))
HelperMethods.RecursiveRollover(logFile, 5); HelperMethods.RecursiveRollover(logFile, 5);

Loading…
Cancel
Save