From a62d649d50441ad5dadba6550371fc74644ea247 Mon Sep 17 00:00:00 2001 From: Miepee Date: Sun, 11 Dec 2022 12:58:34 +0100 Subject: [PATCH] reduce copy pasted logic in create click button --- AM2RModPacker/ModPacker.Designer.cs | 4 +- AM2RModPacker/ModPacker.cs | 143 ++++++++++------------------ AM2RModPackerLib/Core.cs | 2 - 3 files changed, 54 insertions(+), 95 deletions(-) diff --git a/AM2RModPacker/ModPacker.Designer.cs b/AM2RModPacker/ModPacker.Designer.cs index 2c44186..22633d4 100644 --- a/AM2RModPacker/ModPacker.Designer.cs +++ b/AM2RModPacker/ModPacker.Designer.cs @@ -12,9 +12,9 @@ public partial class ModPacker : Form profile = new ModProfileXML(); Title = "AM2R ModPacker " + version; - //TODO: Currently broken as I don't know how to do this from Rider + // TODO: Currently broken as I don't know how to do this from Rider //Icon = Icon.FromResource("icon64.ico"); - + MinimumSize = new Size(550, 400); var mainContent = new DynamicLayout() { Spacing = new Size(15, 15) }; diff --git a/AM2RModPacker/ModPacker.cs b/AM2RModPacker/ModPacker.cs index 0c8fa5c..d37cff6 100644 --- a/AM2RModPacker/ModPacker.cs +++ b/AM2RModPacker/ModPacker.cs @@ -203,7 +203,7 @@ public partial class ModPacker : Form { if (nameTextBox.Text == "" || authorTextBox.Text == "" || versionTextBox.Text == "") { - MessageBox.Show("Text field missing! Mod packaging aborted.", "Error", MessageBoxButtons.OK, MessageBoxType.Error); + MessageBox.Show("Mod name, author or version field missing! Mod packaging aborted.", "Error", MessageBoxButtons.OK, MessageBoxType.Error); return; } @@ -224,64 +224,62 @@ public partial class ModPacker : Form createLabel.Visible = true; createLabel.Text = "Packaging mod(s)... This could take a while!"; - - string output; - if (windowsCheckBox.Checked.Value) + bool PromptAndSaveOSMod(ProfileOperatingSystems os) { - var windowsZip = ZipFile.Open(windowsPath, ZipArchiveMode.Read); - if (windowsZip.Entries.All(f => f.FullName != "AM2R.exe")) - { - var result = MessageBox.Show("Modded game not found, make sure it's not placed in any subfolders.\nCreated profile will likely not be installable, are you sure you want to continue?", "WARNING", MessageBoxButtons.YesNo, MessageBoxType.Warning); - if (result != DialogResult.Yes) - { - AbortPatch(); - return; - } - } - - if (windowsZip.Entries.Any(f => f.Name == "profile.xml")) + string modZipPath = os switch { - var result = MessageBox.Show("profile.xml found. This file is used by the AM2RLauncher to determine profile stats and its inclusion may make the profile uninstallable. Are you sure you want to continue?", "WARNING", MessageBoxButtons.YesNo, MessageBoxType.Warning); - if (result != DialogResult.Yes) - { - AbortPatch(); - return; - } - } + ProfileOperatingSystems.Windows => windowsPath, + ProfileOperatingSystems.Linux => linuxPath, + ProfileOperatingSystems.Mac => macPath, + _ => null + }; + string output; - using (var saveFile = new SaveFileDialog { Title = "Save Windows mod profile", Filters = { zipFileFilter } }) + using (var saveFile = new SaveFileDialog { Title = $"Save {os.ToString()} mod profile", Filters = { zipFileFilter } }) { if (saveFile.ShowDialog(this) == DialogResult.Ok) output = saveFile.FileName; else { createLabel.Text = "Mod packaging aborted!"; - return; + return false; } } // Some filepickers don't automatically set the file extension if (!output.ToLower().EndsWith(".zip")) output += ".zip"; - LoadProfileParameters(ProfileOperatingSystems.Windows); + LoadProfileParameters(os); try { - Core.CreateModPack(profile, originalPath, windowsPath, apkPath, output); + Core.CreateModPack(profile, originalPath, modZipPath, apkPath, output); } catch (Exception exception) { MessageBox.Show(exception.ToString(), "Error", MessageBoxButtons.OK, MessageBoxType.Error); AbortPatch(); - return; + return false; } + return true; } - if (linuxCheckBox.Checked.Value) + bool CheckForProfileXML(ZipArchive zipfile) { - var linuxZip = ZipFile.Open(linuxPath, ZipArchiveMode.Read); - if (linuxZip.Entries.All(f => f.FullName != "AM2R") && linuxZip.Entries.All(f => f.FullName != "runner")) - { - var result = MessageBox.Show("Modded Linux game not found, make sure it's not placed in any subfolders.\nCreated profile will likely not be installable, are you sure you want to continue?", "WARNING", MessageBoxButtons.YesNo, MessageBoxType.Warning); + if (zipfile.Entries.All(f => f.Name != "profile.xml")) + return true; + var result = MessageBox.Show("profile.xml found. This file is used by the AM2RLauncher to determine profile stats and its inclusion may make the profile uninstallable. Are you sure you want to continue?", "WARNING", MessageBoxButtons.YesNo, MessageBoxType.Warning); + if (result == DialogResult.Yes) + return true; + AbortPatch(); + return false; + } + + if (windowsCheckBox.Checked.Value) + { + var windowsZip = ZipFile.Open(windowsPath, ZipArchiveMode.Read); + if (windowsZip.Entries.All(f => f.FullName != "AM2R.exe")) + { + var result = MessageBox.Show("Modded game not found, make sure it's not placed in any subfolders.\nCreated profile will likely not be installable, are you sure you want to continue?", "WARNING", MessageBoxButtons.YesNo, MessageBoxType.Warning); if (result != DialogResult.Yes) { AbortPatch(); @@ -289,39 +287,31 @@ public partial class ModPacker : Form } } - if (linuxZip.Entries.Any(f => f.Name == "profile.xml")) - { - var result = MessageBox.Show("profile.xml found. This file is used by the AM2RLauncher to determine profile stats and its inclusion may make the profile uninstallable. Are you sure you want to continue?", "WARNING", MessageBoxButtons.YesNo, MessageBoxType.Warning); if (result != DialogResult.Yes) + if (!CheckForProfileXML(windowsZip)) + return; + + if (!PromptAndSaveOSMod(ProfileOperatingSystems.Windows)) + return; + } + + if (linuxCheckBox.Checked.Value) + { + var linuxZip = ZipFile.Open(linuxPath, ZipArchiveMode.Read); + if (linuxZip.Entries.All(f => f.FullName != "AM2R") && linuxZip.Entries.All(f => f.FullName != "runner")) + { + var result = MessageBox.Show("Modded Linux game not found, make sure it's not placed in any subfolders.\nCreated profile will likely not be installable, are you sure you want to continue?", "WARNING", MessageBoxButtons.YesNo, MessageBoxType.Warning); + if (result != DialogResult.Yes) { AbortPatch(); return; } } - using (var saveFile = new SaveFileDialog { Title = "Save Linux mod profile", Filters = { zipFileFilter } }) - { - if (saveFile.ShowDialog(this) == DialogResult.Ok) - output = saveFile.FileName; - else - { - createLabel.Text = "Mod packaging aborted!"; - return; - } - } - // Some filepickers don't automatically set the file extension - if (!output.ToLower().EndsWith(".zip")) - output += ".zip"; - LoadProfileParameters(ProfileOperatingSystems.Linux); - try - { - Core.CreateModPack(profile, originalPath, linuxPath, apkPath, output); - } - catch (Exception exception) - { - MessageBox.Show(exception.ToString(), "Error", MessageBoxButtons.OK, MessageBoxType.Error); - AbortPatch(); + if (!CheckForProfileXML(linuxZip)) + return; + + if (!PromptAndSaveOSMod(ProfileOperatingSystems.Linux)) return; - } } if (macCheckBox.Checked.Value) { @@ -333,37 +323,11 @@ public partial class ModPacker : Form AbortPatch(); } - if (macZip.Entries.Any(f => f.Name == "profile.xml")) - { - var result = MessageBox.Show("profile.xml found. This file is used by the AM2RLauncher to determine profile stats and its inclusion may make the profile uninstallable. Are you sure you want to continue?", "WARNING", MessageBoxButtons.YesNo, MessageBoxType.Warning); - if (result != DialogResult.Yes) - AbortPatch(); - } + if (!CheckForProfileXML(macZip)) + return; - using (SaveFileDialog saveFile = new SaveFileDialog { Title = "Save Mac mod profile", Filters = { zipFileFilter } }) - { - if (saveFile.ShowDialog(this) == DialogResult.Ok) - output = saveFile.FileName; - else - { - createLabel.Text = "Mod packaging aborted!"; - return; - } - } - // Some filepickers don't automatically set the file extension - if (!output.ToLower().EndsWith(".zip")) - output += ".zip"; - LoadProfileParameters(ProfileOperatingSystems.Mac); - try - { - Core.CreateModPack(profile, originalPath, macPath, apkPath, output); - } - catch (Exception exception) - { - MessageBox.Show(exception.ToString(), "Error", MessageBoxButtons.OK, MessageBoxType.Error); - AbortPatch(); + if (!PromptAndSaveOSMod(ProfileOperatingSystems.Mac)) return; - } } createLabel.Text = "Mod package(s) created!"; } @@ -398,9 +362,6 @@ public partial class ModPacker : Form { // Set labels createLabel.Text = "Mod packaging aborted!"; - originalZipLabel.Visible = false; - apkLabel.Visible = false; - linuxLabel.Visible = false; // Remove temp directory if (Directory.Exists(Path.GetTempPath() + "/AM2RModPacker")) diff --git a/AM2RModPackerLib/Core.cs b/AM2RModPackerLib/Core.cs index a54ff79..d905008 100644 --- a/AM2RModPackerLib/Core.cs +++ b/AM2RModPackerLib/Core.cs @@ -44,7 +44,6 @@ public static class Core string tempModPath = Directory.CreateDirectory(tempPath + "/mod").FullName; string tempProfilePath = Directory.CreateDirectory(tempPath + "/profile").FullName; - // Extract 1.1 and modded AM2R to their own directories in temp work ZipFile.ExtractToDirectory(originalZipPath, tempOriginalPath); ZipFile.ExtractToDirectory(modZipPath, tempModPath); @@ -172,7 +171,6 @@ public static class Core } // Copy datafiles (exclude .ogg if custom music is not selected) - var dirInfo = new DirectoryInfo(tempModPath); if (profile.OperatingSystem == "Linux") dirInfo = new DirectoryInfo(tempModPath + "/assets");