Use lookup tables for generic checkbox/button events

Co-Authored-By: Jim Hobbs <106978449+General-Ridley@users.noreply.github.com>
pull/9/head
Miepee 4 years ago
parent b3ae23ba43
commit 1a0d0657fd

@ -1,7 +1,9 @@
using System.Collections.Generic;
using Eto.Forms; using Eto.Forms;
using Eto.Drawing; using Eto.Drawing;
using AM2RModPackerLib.XML; using AM2RModPackerLib.XML;
using System.IO; using System.IO;
using System.Reflection;
using AM2RModPackerLib; using AM2RModPackerLib;
namespace AM2RModPacker; namespace AM2RModPacker;
@ -10,6 +12,45 @@ public partial class ModPacker : Form
{ {
public ModPacker() public ModPacker()
{ {
// Fill in lookup tables:
#region Lookup Table filling
labelLookupTable = new Dictionary<ProfileOperatingSystems, Label>()
{
{ ProfileOperatingSystems.Windows, windowsLabel },
{ ProfileOperatingSystems.Linux, windowsLabel },
{ ProfileOperatingSystems.Mac, windowsLabel },
{ ProfileOperatingSystems.Android, windowsLabel },
};
buttonLookupTable = new Dictionary<ProfileOperatingSystems, Button>()
{
{ ProfileOperatingSystems.Windows, windowsButton },
{ ProfileOperatingSystems.Linux, linuxButton },
{ ProfileOperatingSystems.Mac, macButton },
{ ProfileOperatingSystems.Android, apkButton },
};
checkboxLookupTable = new Dictionary<ProfileOperatingSystems, CheckBox>()
{
{ ProfileOperatingSystems.Windows, windowsCheckBox },
{ ProfileOperatingSystems.Linux, linuxCheckBox },
{ ProfileOperatingSystems.Mac, macCheckBox },
{ ProfileOperatingSystems.Android, apkCheckBox },
};
modPathLookupTable = new Dictionary<ProfileOperatingSystems, FieldInfo>()
{
{ 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, PropertyInfo>()
{
{ 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; 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"); //Icon = Icon.FromResource("icon64.ico");

@ -1,4 +1,5 @@
using System; using System;
using System.Collections.Generic;
using System.IO; using System.IO;
using System.IO.Compression; using System.IO.Compression;
using System.Linq; using System.Linq;
@ -15,6 +16,13 @@ public partial class ModPacker : Form
private ModCreationInfo modInfo = new ModCreationInfo(); private ModCreationInfo modInfo = new ModCreationInfo();
// Lookup dictionaries, filled in Constructor
private Dictionary<ProfileOperatingSystems, Label> labelLookupTable = new Dictionary<ProfileOperatingSystems, Label>();
private Dictionary<ProfileOperatingSystems, Button> buttonLookupTable = new Dictionary<ProfileOperatingSystems, Button>();
private Dictionary<ProfileOperatingSystems, CheckBox> checkboxLookupTable = new Dictionary<ProfileOperatingSystems, CheckBox>();
private Dictionary<ProfileOperatingSystems, FieldInfo> modPathLookupTable = new Dictionary<ProfileOperatingSystems, FieldInfo>();
private Dictionary<ProfileOperatingSystems, PropertyInfo> isModLoadedLookupTable = new Dictionary<ProfileOperatingSystems, PropertyInfo>();
private readonly FileFilter zipFileFilter = new FileFilter("zip archives (*.zip)", ".zip"); private readonly FileFilter zipFileFilter = new FileFilter("zip archives (*.zip)", ".zip");
private readonly FileFilter apkFileFilter = new FileFilter("Android application packages (*.apk)", ".apk"); 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) private void OSCheckboxChanged(ProfileOperatingSystems os)
{ {
CheckBox osCheckbox = null; CheckBox osCheckbox = checkboxLookupTable[os];
Button osButton = null; Button osButton = buttonLookupTable[os];
switch(os) Label osLabel = labelLookupTable[os];
{ FieldInfo osModPath = modPathLookupTable[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;
// If it was disabled, clean the appropriate attributes // If it was disabled, clean the appropriate attributes
osButton.Enabled = osCheckbox.Checked.Value;
if (!osCheckbox.Checked.Value) if (!osCheckbox.Checked.Value)
{ {
windowsLabel.Visible = false; osLabel.Visible = false;
modInfo.WindowsModPath = null; osModPath.SetValue(modInfo, null);
} }
UpdateCreateButton(); UpdateCreateButton();
} }
@ -338,37 +341,12 @@ public partial class ModPacker : Form
private void OSButtonClicked(ProfileOperatingSystems os) private void OSButtonClicked(ProfileOperatingSystems os)
{ {
string pickerMessage = $"Please select your custom {os.ToString()} AM2R .{(os == ProfileOperatingSystems.Android ? "apk" : "zip")}"; string pickerMessage = $"Please select your custom {os.ToString()} AM2R .{(os == ProfileOperatingSystems.Android ? "apk" : "zip")}";
Label osLabel = null; Label osLabel = labelLookupTable[os];
FieldInfo osModPathPropertyField = null; FieldInfo osModPath = modPathLookupTable[os];
PropertyInfo isOsModLoaded = null; PropertyInfo isOsModLoaded = isModLoadedLookupTable[os];
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;
}
// Open window to select modded file // Open window to select modded file
string selectedFile = SelectFile(pickerMessage, os == ProfileOperatingSystems.Android ? apkFileFilter : zipFileFilter); string selectedFile = SelectFile(pickerMessage, os == ProfileOperatingSystems.Android ? apkFileFilter : zipFileFilter);
osModPathPropertyField.SetValue(modInfo, selectedFile); osModPath.SetValue(modInfo, selectedFile);
osLabel.Visible = (bool)isOsModLoaded.GetValue(modInfo); osLabel.Visible = (bool)isOsModLoaded.GetValue(modInfo);
UpdateCreateButton(); UpdateCreateButton();
} }

Loading…
Cancel
Save