# HG changeset patch # User Jason Maltzen # Date 2021-07-23 01:34:22 # Node ID 9f255f7e44228ecc551cf957b325d7a2c2beb08d # Parent 665daa360de162b397e0c0f3d7f53fcdb7774133 Fix a crash when importing a Practical Paint reactions.txt file with empty (not-recorded) entries. diff --git a/Models/PlayerProfile.cs b/Models/PlayerProfile.cs --- a/Models/PlayerProfile.cs +++ b/Models/PlayerProfile.cs @@ -129,36 +129,69 @@ namespace DesertPaintCodex.Models { using StreamReader reader = new(ppFile); using StreamWriter writer = new(dpFile, false); - string? line; + string? line; while ((line = reader.ReadLine()) != null) { string[] tokens = line.Split('|'); //if ((tokens.Length > 0) && (tokens [0] != "//")) if ((tokens.Length == 5) && (tokens[0].Trim() != "//")) { - string reagent1 = tokens[0].Trim(); - string reagent2 = tokens[1].Trim(); + string reagent1Name = tokens[0].Trim(); + string reagent2Name = tokens[1].Trim(); string colorCode = tokens[2].Trim(); string change1 = tokens[3].Trim(); string change2 = tokens[4].Trim(); + + Reagent reagent1 = ReagentService.GetReagent(reagent1Name); + Reagent reagent2 = ReagentService.GetReagent(reagent2Name); + + if (reagent1 == null || reagent2 == null) continue; + + bool hasChange1 = int.TryParse(change1, out _); + bool hasChange2 = int.TryParse(change2, out _); + // Write reaction. switch (colorCode) { case "W": - WriteReaction(writer, reagent1, reagent2, change1, change1, change1); - WriteReaction(writer, reagent2, reagent1, change2, change2, change2); + if (hasChange1) + { + WriteReaction(writer, reagent1Name, reagent2Name, change1, change1, change1); + } + if (hasChange2) + { + WriteReaction(writer, reagent2Name, reagent1Name, change2, change2, change2); + } break; case "R": - WriteReaction(writer, reagent1, reagent2, change1, "0", "0"); - WriteReaction(writer, reagent2, reagent1, change2, "0", "0"); + if (hasChange1) + { + WriteReaction(writer, reagent1Name, reagent2Name, change1, "0", "0"); + } + if (hasChange2) + { + WriteReaction(writer, reagent2Name, reagent1Name, change2, "0", "0"); + } break; case "G": - WriteReaction(writer, reagent1, reagent2, "0", change1, "0"); - WriteReaction(writer, reagent2, reagent1, "0", change2, "0"); + if (hasChange1) + { + WriteReaction(writer, reagent1Name, reagent2Name, "0", change1, "0"); + } + if (hasChange2) + { + WriteReaction(writer, reagent2Name, reagent1Name, "0", change2, "0"); + } break; case "B": - WriteReaction(writer, reagent1, reagent2, "0", "0", change1); - WriteReaction(writer, reagent2, reagent1, "0", "0", change2); + if (hasChange1) + { + WriteReaction(writer, reagent1Name, reagent2Name, "0", "0", change1); + } + if (hasChange2) + { + WriteReaction(writer, reagent2Name, reagent1Name, "0", "0", change2); + } break; } }