# HG changeset patch # User Tess Snider # Date 2021-07-21 17:47:38 # Node ID 6919080271d55f3f138660bb37dccd116249d13e # Parent 6cd1ceb3031b31e19171878516b1211bf5630843 Implemented a system that will allow ingredients to be safely renamed, in the future, without invalidating profiles. Standardized to PP's "FalconBait." diff --git a/Data/ingredients.txt b/Data/ingredients.txt --- a/Data/ingredients.txt +++ b/Data/ingredients.txt @@ -8,7 +8,7 @@ Carrot | Carrot | 224, 112 Clay | Clay | 128, 96, 32 Dead Tongue | DeadTongue | 112, 64, 64 Toad Skin | ToadSkin | 48, 96, 48 -Falcons Bait | FalconsBait | 128, 240, 224 +Falcons Bait | FalconBait | 128, 240, 224 Red Sand | RedSand | 144, 16, 24 Lead | Lead | 80, 80, 96 Silver Powder | Silver | 16, 16, 32 diff --git a/Data/renames.txt b/Data/renames.txt new file mode 100644 --- /dev/null +++ b/Data/renames.txt @@ -0,0 +1,8 @@ +# PracticalPaint + +EarthLight | FalconBait +FalconsBait | FalconBait + +# Internal + +Earth Light | Falcons Bait \ No newline at end of file diff --git a/Data/template/ingredients.txt b/Data/template/ingredients.txt --- a/Data/template/ingredients.txt +++ b/Data/template/ingredients.txt @@ -9,7 +9,7 @@ Carrot | 224, 112, 32 | 8 | Y | Clay | 128, 96, 32 | 10 | Y | bulk | 20 DeadTongue | 112, 64, 64 | 500 | Y | normal | 4 ToadSkin | 48, 96, 48 | 500 | Y | normal | 4 -FalconsBait | 128, 240, 224 | 10000 | Y | normal | 4 +FalconBait | 128, 240, 224 | 10000 | Y | normal | 4 RedSand | 144, 16, 24 | 4 | Y | bulk | 20 Lead | 80, 80, 96 | 50 | Y | normal | 6 Silver | 16, 16, 32 | 50 | Y | normal | 6 diff --git a/DesertPaintCodex.csproj b/DesertPaintCodex.csproj --- a/DesertPaintCodex.csproj +++ b/DesertPaintCodex.csproj @@ -17,6 +17,9 @@ + + PreserveNewest + diff --git a/Models/Reagent.cs b/Models/Reagent.cs --- a/Models/Reagent.cs +++ b/Models/Reagent.cs @@ -12,6 +12,8 @@ namespace DesertPaintCodex.Models public string Name { get; } public string PracticalPaintName { get; } public bool Enabled { get; set; } + + public int Index { get; private set; } public uint Cost { @@ -32,7 +34,7 @@ namespace DesertPaintCodex.Models } // catalyst - public Reagent(string name, string ppName) + public Reagent(string name, string ppName, int index) { Name = name; PracticalPaintName = ppName; @@ -40,9 +42,10 @@ namespace DesertPaintCodex.Models Enabled = true; RecipeMax = 1; IsCatalyst = true; + Index = index; } - public Reagent(string name, string ppName, byte red, byte green, byte blue) + public Reagent(string name, string ppName, byte red, byte green, byte blue, int index) { Color = new PaintColor(red, green, blue); Name = name; @@ -51,6 +54,7 @@ namespace DesertPaintCodex.Models RecipeMax = 10; Enabled = true; IsCatalyst = false; + Index = index; } public override string ToString() diff --git a/Services/ReagentService.cs b/Services/ReagentService.cs --- a/Services/ReagentService.cs +++ b/Services/ReagentService.cs @@ -10,42 +10,51 @@ namespace DesertPaintCodex.Services { internal static class ReagentService { - // PP format - private static readonly Regex _reagentRegex = new(@"(?\w+)\s*\|\s*(?\d+),\s*(?\d+),\s*(?\d+)\s*\|\s*(?\d+)\s*\|\s*(?[YN])\s*\|\s*(?(bulk|normal))\s*\|\s*(?\d+).*"); - private static readonly Regex _catalystRegex = new(@"(?\w+)\s*\|\s*catalyst\s*\|\s*(?\d+)\s*\|\s*(?[YN])\s*\|\s*(?(bulk|normal)).*"); + private static readonly Regex _ppReagentRegex = new(@"(?\w+)\s*\|\s*(?\d+),\s*(?\d+),\s*(?\d+)\s*\|\s*(?\d+)\s*\|\s*(?[YN])\s*\|\s*(?(bulk|normal))\s*\|\s*(?\d+).*"); + private static readonly Regex _ppCatalystRegex = new(@"(?(\w*\s)*\w+\w+)\s*\|\s*catalyst\s*\|\s*(?\d+)\s*\|\s*(?[YN])\s*\|\s*(?(bulk|normal)).*"); private static readonly Regex _internalReagentRegex = new(@"(?(\w*\s)*\w+)\s*\|\s*(?\w+)\s*\|\s*(?\d+),\s*(?\d+),\s*(?\d+).*"); private static readonly Regex _internalCatalystRegex = new(@"(?(\w+\s)*\w+)\s*\|\s*(?\w+)\s*\|\s*catalyst.*"); + private static readonly Regex _renameHeader = new(@"#\s*(?
\w*)"); + private static readonly Regex _renameRegex = new(@"(?(\w+\s)*\w+)\s*\|\s*(?(\w+\s)*\w+)"); private static readonly Dictionary _reagents = new(); - private static readonly Dictionary _nameLookup = new(); // pp name to our name - - private static string _lastReagentsFile = string.Empty; - + private static readonly Dictionary _ppToInternalName = new(); // pp name to our name + private static readonly Dictionary _ppRenames = new(); + private static readonly Dictionary _internalRenames = new(); + private static bool _initialized = false; public static List Names { get; } = new(); - public static void Initialize() + private static void Initialize() { if (_initialized) return; string? reagentsPath = FileUtils.FindApplicationResourceFile("ingredients.txt"); Debug.Assert(reagentsPath != null); - + Load(reagentsPath); + + string? renamesPath = FileUtils.FindApplicationResourceFile("renames.txt"); + + if (renamesPath != null) + { + LoadAliases(renamesPath); + } + + _initialized = true; } // Loads reagent name/colors - public static void Load(string file) + private static void Load(string file) { - if (_lastReagentsFile == file) return; - _lastReagentsFile = file; - _reagents.Clear(); - _nameLookup.Clear(); + _ppToInternalName.Clear(); Names.Clear(); + int index = 0; + using StreamReader reader = new(file); string? line; while ((line = reader.ReadLine()) != null) @@ -55,14 +64,16 @@ namespace DesertPaintCodex.Services { string name = match.Groups["name"].Value; string ppname = match.Groups["ppname"].Value; + Debug.WriteLine("Adding reagent[" + name + "] from line: [" + line + "]"); _reagents.Add(name, new Reagent(name, ppname, byte.Parse(match.Groups["red"].Value), byte.Parse(match.Groups["green"].Value), - byte.Parse(match.Groups["blue"].Value))); + byte.Parse(match.Groups["blue"].Value), + index++)); // nameStore.AppendValues(name); Names.Add(name); - _nameLookup.Add(ppname, name); + _ppToInternalName.Add(ppname, name); } else { @@ -72,14 +83,49 @@ namespace DesertPaintCodex.Services string name = match.Groups["name"].Value; string ppname = match.Groups["ppname"].Value; - _reagents.Add(name, new Reagent(ppname, ppname)); + _reagents.Add(name, new Reagent(ppname, ppname, index++)); // nameStore.AppendValues(name); Names.Add(name); - _nameLookup.Add(ppname, name); + _ppToInternalName.Add(ppname, name); } } } + private static void LoadAliases(string file) + { + _ppRenames.Clear(); + _internalRenames.Clear(); + + Dictionary? renameDict = null; + + using StreamReader reader = new(file); + string? line; + while ((line = reader.ReadLine()) != null) + { + Match match = _renameHeader.Match(line); + if (match.Success) + { + string header = match.Groups["header"].Value; + renameDict = header switch + { + "PracticalPaint" => _ppRenames, + "Internal" => _internalRenames, + _ => renameDict + }; + } + + if (renameDict == null) continue; + + match = _renameRegex.Match(line); + + if (!match.Success) continue; + + string oldName = match.Groups["oldName"].Value; + string newName = match.Groups["newName"].Value; + renameDict.Add(oldName, newName); + } + } + public static void LoadProfileReagents(string file) { Initialize(); @@ -88,11 +134,16 @@ namespace DesertPaintCodex.Services string? line; while ((line = reader.ReadLine()) != null) { - Match match = _reagentRegex.Match(line); + Match match = _ppReagentRegex.Match(line); if (match.Success) { string ppname = match.Groups["name"].Value; - if (_nameLookup.TryGetValue(ppname, out string? name)) + if (_ppRenames.TryGetValue(ppname, out string? rename)) + { + ppname = rename; // Apply rename. + } + + if (_ppToInternalName.TryGetValue(ppname, out string? name)) { Reagent reagent = GetReagent(name); if (reagent.IsCatalyst) continue; @@ -100,19 +151,20 @@ namespace DesertPaintCodex.Services reagent.Cost = uint.Parse(match.Groups["cost"].Value); reagent.RecipeMax = uint.Parse(match.Groups["max"].Value); } - else - { - // bad name? - } } else { - match = _catalystRegex.Match(line); + match = _ppCatalystRegex.Match(line); if (!match.Success) continue; string ppname = match.Groups["name"].Value; - if (_nameLookup.TryGetValue(ppname, out string? name)) + if (_ppRenames.TryGetValue(ppname, out string? rename)) + { + ppname = rename; // Apply rename. + } + + if (_ppToInternalName.TryGetValue(ppname, out string? name)) { Reagent reagent = GetReagent(name); @@ -121,10 +173,6 @@ namespace DesertPaintCodex.Services reagent.Enabled = match.Groups["enabled"].Value.Equals("Y"); reagent.Cost = uint.Parse(match.Groups["cost"].Value); } - else - { - // bad name? - } } } } @@ -188,17 +236,43 @@ namespace DesertPaintCodex.Services public static Reagent GetReagent(string reagentName) { Initialize(); + + Reagent? returnVal = GetReagentByInternalName(reagentName); + + if (returnVal != null) return returnVal; + + returnVal = GetReagentByPPName(reagentName); + + if (returnVal != null) return returnVal; - if (_reagents.TryGetValue(reagentName, out Reagent? returnVal)) return returnVal; - // convert pp name to our internal name - if (_nameLookup.TryGetValue(reagentName, out string? otherName)) + string? rename = null; + if (_internalRenames.TryGetValue(reagentName, out rename)) { - _reagents.TryGetValue(otherName, out returnVal); + returnVal = GetReagentByInternalName(rename); + if (returnVal != null) return returnVal; + } + + if (_ppRenames.TryGetValue(reagentName, out rename)) + { + returnVal = GetReagentByPPName(rename); + if (returnVal != null) return returnVal; } Debug.Assert(returnVal != null); return returnVal; } + + private static Reagent? GetReagentByInternalName(string internalName) + { + return _reagents.TryGetValue(internalName, out Reagent? returnVal) ? returnVal : null; + } + + private static Reagent? GetReagentByPPName(string ppName) + { + if (!_ppToInternalName.TryGetValue(ppName, out string? otherName)) return null; + + return _reagents.TryGetValue(otherName, out Reagent? returnVal) ? returnVal : null; + } } } diff --git a/Util/Constants.cs b/Util/Constants.cs --- a/Util/Constants.cs +++ b/Util/Constants.cs @@ -18,9 +18,9 @@ namespace DesertPaintCodex.Util { if (_stubReactionTest == null) { - _stubReactionTest = new( - new Reagent("Toad Skin", "ToadSkin", 48, 96, 48), - new Reagent("Falcons Bait", "FalconsBait", 128, 240, 224), + _stubReactionTest = new ReactionTest( + new Reagent("Toad Skin", "ToadSkin", 48, 96, 48, 0), + new Reagent("Falcons Bait", "FalconBait", 128, 240, 224, 1), null, ClipType.None, true); }