diff --git a/GeneticAlgorithmBot.code-workspace b/GeneticAlgorithmBot.code-workspace new file mode 100644 index 0000000..220f2e3 --- /dev/null +++ b/GeneticAlgorithmBot.code-workspace @@ -0,0 +1,7 @@ +{ + "folders": [ + { + "path": "..\\BizHawk\\src\\BizHawk.Client.EmuHawk" + } + ] +} \ No newline at end of file diff --git a/GeneticAlgorithmBot.sln.lnk b/GeneticAlgorithmBot.sln.lnk new file mode 100644 index 0000000..c10fef9 Binary files /dev/null and b/GeneticAlgorithmBot.sln.lnk differ diff --git a/src/GeneticAlgorithmBot.cs b/src/GeneticAlgorithmBot.cs index d5741b0..acf4706 100644 --- a/src/GeneticAlgorithmBot.cs +++ b/src/GeneticAlgorithmBot.cs @@ -3,6 +3,7 @@ using BizHawk.Client.EmuHawk.ToolExtensions; using BizHawk.Common; using BizHawk.Emulation.Common; +using Newtonsoft.Json; using System; using System.Collections; using System.Collections.Generic; @@ -636,11 +637,20 @@ private bool LoadBotFile(string path) { try { try { // Attempts to load GeneticAlgorithmBot .BOT file save data. - botData = (BotData) ConfigService.LoadWithType(json); + var data = ConfigService.LoadWithType(json); + // Deserializing a serialized object works. + botData = JsonConvert.DeserializeObject(JsonConvert.SerializeObject(data)); } catch (InvalidCastException) { - // If exception is thrown, attempt to load BasicBot .BOT file save data instead. - botData = Utils.BotDataReflectionCopy(ConfigService.LoadWithType(json)); + // Loading the data alternatively. + try { + // Deserializing a serialized object works. + botData = JsonConvert.DeserializeObject(json); + } + catch (InvalidCastException) { + // If exception is thrown, attempt to load BasicBot .BOT file save data instead. + botData = Utils.BotDataReflectionCopy(ConfigService.LoadWithType(json)); + } } } catch (InvalidCastException e) { @@ -713,9 +723,11 @@ private void LoadBotFileInner(BotData botData, string path) { .OfType() .ToList(); - foreach (var (button, p) in botData.ControlProbabilities) { - var control = probabilityControls.Single(c => c.ButtonName == button); - control.Probability = p; + if (botData.ControlProbabilities != null) { + foreach (var (button, p) in botData.ControlProbabilities) { + var control = probabilityControls.Single(c => c.ButtonName == button); + control.Probability = p; + } } MaximizeAddress = botData.Maximize; @@ -1249,7 +1261,7 @@ public static BotAttempt BotAttemptReflectionCopy(object source) { object value = p.GetValue(source); PropertyInfo targetInfo = typeof(BotAttempt).GetProperty(p.Name); if (value.GetType() == typeof(int)) { - targetInfo!.SetValue(target, Convert.ToUInt32(value)); + targetInfo!.SetValue(target, Convert.ToInt32(value)); } else if (p.Name.Equals("Log")) { List logs = (List) targetInfo.GetValue(target); @@ -1558,7 +1570,7 @@ public void DeepCopy(FrameInput other) { public class BotAttempt { public long Attempt { get; set; } public long Generation { get; set; } - public int Fitness { get; set; } + public long Fitness { get; set; } public int Maximize { get; set; } public int TieBreak1 { get; set; } public int TieBreak2 { get; set; }