From 1a0d0657fdb813fb1ac0c7f1defba5f071dbf8de Mon Sep 17 00:00:00 2001 From: Miepee Date: Sun, 11 Dec 2022 15:41:10 +0100 Subject: [PATCH] Use lookup tables for generic checkbox/button events Co-Authored-By: Jim Hobbs <106978449+General-Ridley@users.noreply.github.com> --- AM2RModPacker/ModPacker.Designer.cs | 41 ++++++++++++++++++++ AM2RModPacker/ModPacker.cs | 60 +++++++++-------------------- 2 files changed, 60 insertions(+), 41 deletions(-) diff --git a/AM2RModPacker/ModPacker.Designer.cs b/AM2RModPacker/ModPacker.Designer.cs index 0e6b2d4..5e73aad 100644 --- a/AM2RModPacker/ModPacker.Designer.cs +++ b/AM2RModPacker/ModPacker.Designer.cs @@ -1,7 +1,9 @@ +using System.Collections.Generic; using Eto.Forms; using Eto.Drawing; using AM2RModPackerLib.XML; using System.IO; +using System.Reflection; using AM2RModPackerLib; namespace AM2RModPacker; @@ -10,6 +12,45 @@ public partial class ModPacker : Form { public ModPacker() { + // Fill in lookup tables: + #region Lookup Table filling + labelLookupTable = new Dictionary() + { + { ProfileOperatingSystems.Windows, windowsLabel }, + { ProfileOperatingSystems.Linux, windowsLabel }, + { ProfileOperatingSystems.Mac, windowsLabel }, + { ProfileOperatingSystems.Android, windowsLabel }, + }; + buttonLookupTable = new Dictionary() + { + { ProfileOperatingSystems.Windows, windowsButton }, + { ProfileOperatingSystems.Linux, linuxButton }, + { ProfileOperatingSystems.Mac, macButton }, + { ProfileOperatingSystems.Android, apkButton }, + }; + checkboxLookupTable = new Dictionary() + { + { ProfileOperatingSystems.Windows, windowsCheckBox }, + { ProfileOperatingSystems.Linux, linuxCheckBox }, + { ProfileOperatingSystems.Mac, macCheckBox }, + { ProfileOperatingSystems.Android, apkCheckBox }, + }; + modPathLookupTable = new Dictionary() + { + { ProfileOperatingSystems.Windows, modInfo.GetType().GetField(nameof(modInfo.WindowsModPath)) }, + { ProfileOperatingSystems.Linux, modInfo.GetType().GetField(nameof(modInfo.LinuxModPath)) }, + { ProfileOperatingSystems.Mac, modInfo.GetType().GetField(nameof(modInfo.MacModPath)) }, + { ProfileOperatingSystems.Android, modInfo.GetType().GetField(nameof(modInfo.ApkModPath)) }, + }; + isModLoadedLookupTable = new Dictionary() + { + { ProfileOperatingSystems.Windows, modInfo.GetType().GetProperty(nameof(modInfo.IsWindowsModLoaded)) }, + { ProfileOperatingSystems.Linux, modInfo.GetType().GetProperty(nameof(modInfo.IsLinuxModLoaded)) }, + { ProfileOperatingSystems.Mac, modInfo.GetType().GetProperty(nameof(modInfo.IsMacModLoaded)) }, + { ProfileOperatingSystems.Android, modInfo.GetType().GetProperty(nameof(modInfo.IsApkModLoaded)) }, + }; + #endregion + Title = "AM2R ModPacker " + version; // TODO: Currently broken as I don't know how to do this from Rider //Icon = Icon.FromResource("icon64.ico"); diff --git a/AM2RModPacker/ModPacker.cs b/AM2RModPacker/ModPacker.cs index 298c3fc..649bd12 100644 --- a/AM2RModPacker/ModPacker.cs +++ b/AM2RModPacker/ModPacker.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.IO; using System.IO.Compression; using System.Linq; @@ -14,6 +15,13 @@ public partial class ModPacker : Form private const string version = Core.Version; private ModCreationInfo modInfo = new ModCreationInfo(); + + // Lookup dictionaries, filled in Constructor + private Dictionary labelLookupTable = new Dictionary(); + private Dictionary buttonLookupTable = new Dictionary(); + private Dictionary checkboxLookupTable = new Dictionary(); + private Dictionary modPathLookupTable = new Dictionary(); + private Dictionary isModLoadedLookupTable = new Dictionary(); private readonly FileFilter zipFileFilter = new FileFilter("zip archives (*.zip)", ".zip"); private readonly FileFilter apkFileFilter = new FileFilter("Android application packages (*.apk)", ".apk"); @@ -316,21 +324,16 @@ public partial class ModPacker : Form private void OSCheckboxChanged(ProfileOperatingSystems os) { - CheckBox osCheckbox = null; - Button osButton = null; - switch(os) - { - case ProfileOperatingSystems.Windows: osCheckbox = windowsCheckBox; osButton = windowsButton; break; - case ProfileOperatingSystems.Linux: osCheckbox = linuxCheckBox; osButton = linuxButton; break; - case ProfileOperatingSystems.Mac: osCheckbox = macCheckBox; osButton = macButton; break; - case ProfileOperatingSystems.Android: osCheckbox = apkCheckBox; osButton = apkButton; break; - }; - osButton.Enabled = osCheckbox.Checked.Value; + CheckBox osCheckbox = checkboxLookupTable[os]; + Button osButton = buttonLookupTable[os]; + Label osLabel = labelLookupTable[os]; + FieldInfo osModPath = modPathLookupTable[os]; // If it was disabled, clean the appropriate attributes + osButton.Enabled = osCheckbox.Checked.Value; if (!osCheckbox.Checked.Value) { - windowsLabel.Visible = false; - modInfo.WindowsModPath = null; + osLabel.Visible = false; + osModPath.SetValue(modInfo, null); } UpdateCreateButton(); } @@ -338,37 +341,12 @@ public partial class ModPacker : Form private void OSButtonClicked(ProfileOperatingSystems os) { string pickerMessage = $"Please select your custom {os.ToString()} AM2R .{(os == ProfileOperatingSystems.Android ? "apk" : "zip")}"; - Label osLabel = null; - FieldInfo osModPathPropertyField = null; - PropertyInfo isOsModLoaded = null; - - switch (os) - { - case ProfileOperatingSystems.Windows: - osLabel = windowsLabel; - osModPathPropertyField = modInfo.GetType().GetField(nameof(modInfo.WindowsModPath)); - isOsModLoaded = modInfo.GetType().GetProperty(nameof(modInfo.IsWindowsModLoaded)); - break; - case ProfileOperatingSystems.Linux: - osLabel = linuxLabel; - osModPathPropertyField = modInfo.GetType().GetField(nameof(modInfo.LinuxModPath)); - isOsModLoaded = modInfo.GetType().GetProperty(nameof(modInfo.IsLinuxModLoaded)); - break; - case ProfileOperatingSystems.Mac: - osLabel = macLabel; - osModPathPropertyField = modInfo.GetType().GetField(nameof(modInfo.MacModPath)); - isOsModLoaded = modInfo.GetType().GetProperty(nameof(modInfo.IsMacModLoaded)); - break; - case ProfileOperatingSystems.Android: - osLabel = apkLabel; - osModPathPropertyField = modInfo.GetType().GetField(nameof(modInfo.ApkModPath)); - isOsModLoaded = modInfo.GetType().GetProperty(nameof(modInfo.IsApkModLoaded)); - break; - } - + Label osLabel = labelLookupTable[os]; + FieldInfo osModPath = modPathLookupTable[os]; + PropertyInfo isOsModLoaded = isModLoadedLookupTable[os]; // Open window to select modded file string selectedFile = SelectFile(pickerMessage, os == ProfileOperatingSystems.Android ? apkFileFilter : zipFileFilter); - osModPathPropertyField.SetValue(modInfo, selectedFile); + osModPath.SetValue(modInfo, selectedFile); osLabel.Visible = (bool)isOsModLoaded.GetValue(modInfo); UpdateCreateButton(); }