From 3c8e7f51c19b21f456b441f730674239800f493a Mon Sep 17 00:00:00 2001 From: tom_mai78101 Date: Wed, 2 Aug 2023 23:00:31 -0400 Subject: [PATCH] Fixed loading BOT file from failing after saving to the same BOT file. --- GeneticAlgorithmBot.code-workspace | 7 +++++++ GeneticAlgorithmBot.sln.lnk | Bin 0 -> 1064 bytes src/GeneticAlgorithmBot.cs | 28 ++++++++++++++++++++-------- 3 files changed, 27 insertions(+), 8 deletions(-) create mode 100644 GeneticAlgorithmBot.code-workspace create mode 100644 GeneticAlgorithmBot.sln.lnk 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 0000000000000000000000000000000000000000..c10fef9861177b66d5d3eb570b72856e3fc78dee GIT binary patch literal 1064 zcmeZaU|?VrVFHp23Kv8~HYH~?2Lj(gS0}I3ZefL6QbUnka3F-mWYk~DZ9E>o_V!bwl4?`kD5kopd zDuX*iCPN8B216-B5<>t`ET16@D4GluD+X#&VweQA`ESTpP5}m|%&LsU@@!rA)V$P^ z%w)%$^!y^AJ93@!L1sW)@HynqEYGk@!XOtkUS|f9q6o{utj*K<8JvLjRsr=SGL$o9 zGw1@{k;;$<#3exSWCllu9H8s+f$o90E*GdiA1DX%lNZ!Q|3Z#22{05FB{NvSZEm0K z8FmY7^Sx^zn-#$JA&8f+#en`Q0@}d9AjHtdV8j55px+@88x$tMh`|#rij#^`7=B#wZWNpC;~NZpyerU5P6mbrmO$DtI3ReVSgy<3-U5&19u9g-Y%6+Xf#MlJ%-{jU Y94bphq~$!^x3)=rR(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; }