Changeset - 6919080271d5
[Not reviewed]
Tess Snider (Malkyne) - 3 years ago 2021-07-21 17:47:38
this@malkyne.org
Implemented a system that will allow ingredients to be safely renamed, in the
future, without invalidating profiles. Standardized to PP's "FalconBait."
7 files changed with 129 insertions and 40 deletions:
0 comments (0 inline, 0 general)
Data/ingredients.txt
Show inline comments
...
 
@@ -10,3 +10,3 @@ Dead Tongue     | DeadTongue  | 112, 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
Data/renames.txt
Show inline comments
 
new file 100644
 
# PracticalPaint
 

	
 
EarthLight | FalconBait
 
FalconsBait | FalconBait
 

	
 
# Internal
 

	
 
Earth Light | Falcons Bait
...
 
\ No newline at end of file
Data/template/ingredients.txt
Show inline comments
...
 
@@ -11,3 +11,3 @@ DeadTongue	| 112, 64, 64    | 500    | Y
 
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
DesertPaintCodex.csproj
Show inline comments
...
 
@@ -19,2 +19,5 @@
 
    <None Remove="CodexTheme.xaml" />
 
    <None Update="Data\renames.txt">
 
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
 
    </None>
 
  </ItemGroup>
Models/Reagent.cs
Show inline comments
...
 
@@ -15,2 +15,4 @@ namespace DesertPaintCodex.Models
 

	
 
        public int Index { get; private set; }
 

	
 
        public uint Cost
...
 
@@ -34,3 +36,3 @@ namespace DesertPaintCodex.Models
 
        // catalyst
 
        public Reagent(string name, string ppName)
 
        public Reagent(string name, string ppName, int index)
 
        {
...
 
@@ -42,5 +44,6 @@ namespace DesertPaintCodex.Models
 
            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)
 
        {
...
 
@@ -53,2 +56,3 @@ namespace DesertPaintCodex.Models
 
            IsCatalyst = false;
 
            Index = index;
 
        }
Services/ReagentService.cs
Show inline comments
...
 
@@ -12,12 +12,13 @@ namespace DesertPaintCodex.Services
 
    {
 
        // PP format
 
        private static readonly Regex _reagentRegex          = new(@"(?<name>\w+)\s*\|\s*(?<red>\d+),\s*(?<green>\d+),\s*(?<blue>\d+)\s*\|\s*(?<cost>\d+)\s*\|\s*(?<enabled>[YN])\s*\|\s*(?<bulk>(bulk|normal))\s*\|\s*(?<max>\d+).*");
 
        private static readonly Regex _catalystRegex         = new(@"(?<name>\w+)\s*\|\s*catalyst\s*\|\s*(?<cost>\d+)\s*\|\s*(?<enabled>[YN])\s*\|\s*(?<bulk>(bulk|normal)).*");
 
        private static readonly Regex _ppReagentRegex        = new(@"(?<name>\w+)\s*\|\s*(?<red>\d+),\s*(?<green>\d+),\s*(?<blue>\d+)\s*\|\s*(?<cost>\d+)\s*\|\s*(?<enabled>[YN])\s*\|\s*(?<bulk>(bulk|normal))\s*\|\s*(?<max>\d+).*");
 
        private static readonly Regex _ppCatalystRegex       = new(@"(?<name>(\w*\s)*\w+\w+)\s*\|\s*catalyst\s*\|\s*(?<cost>\d+)\s*\|\s*(?<enabled>[YN])\s*\|\s*(?<bulk>(bulk|normal)).*");
 
        private static readonly Regex _internalReagentRegex  = new(@"(?<name>(\w*\s)*\w+)\s*\|\s*(?<ppname>\w+)\s*\|\s*(?<red>\d+),\s*(?<green>\d+),\s*(?<blue>\d+).*");
 
        private static readonly Regex _internalCatalystRegex = new(@"(?<name>(\w+\s)*\w+)\s*\|\s*(?<ppname>\w+)\s*\|\s*catalyst.*");
 
        private static readonly Regex _renameHeader          = new(@"#\s*(?<header>\w*)");
 
        private static readonly Regex _renameRegex           = new(@"(?<oldName>(\w+\s)*\w+)\s*\|\s*(?<newName>(\w+\s)*\w+)");
 

	
 
        private static readonly Dictionary<string, Reagent> _reagents   = new();
 
        private static readonly Dictionary<string, string>  _nameLookup = new(); // pp name to our name
 

	
 
        private static string _lastReagentsFile = string.Empty;
 
        private static readonly Dictionary<string, string>  _ppToInternalName = new(); // pp name to our name
 
        private static readonly Dictionary<string, string> _ppRenames = new();
 
        private static readonly Dictionary<string, string> _internalRenames = new();
 

	
...
 
@@ -28,3 +29,3 @@ namespace DesertPaintCodex.Services
 

	
 
        public static void Initialize()
 
        private static void Initialize()
 
        {
...
 
@@ -36,2 +37,11 @@ namespace DesertPaintCodex.Services
 
            Load(reagentsPath);
 

	
 
            string? renamesPath = FileUtils.FindApplicationResourceFile("renames.txt");
 

	
 
            if (renamesPath != null)
 
            {
 
                LoadAliases(renamesPath);
 
            }
 

	
 
            _initialized = true;
 
        }
...
 
@@ -39,10 +49,9 @@ namespace DesertPaintCodex.Services
 
        // Loads reagent name/colors
 
        public static void Load(string file)
 
        private static void Load(string file)
 
        {
 
            if (_lastReagentsFile == file) return;
 
            _lastReagentsFile = file;
 
            _reagents.Clear();
 
            _ppToInternalName.Clear();
 
            Names.Clear();
 
            
 
            _reagents.Clear();
 
            _nameLookup.Clear();
 
            Names.Clear();
 
            int index = 0;
 

	
...
 
@@ -57,2 +66,3 @@ namespace DesertPaintCodex.Services
 
                    string ppname = match.Groups["ppname"].Value;
 
                    Debug.WriteLine("Adding reagent[" + name + "] from line: [" + line + "]");
 
                    _reagents.Add(name,
...
 
@@ -61,6 +71,7 @@ namespace DesertPaintCodex.Services
 
                            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);
 
                }
...
 
@@ -74,7 +85,42 @@ namespace DesertPaintCodex.Services
 
                    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<string, string>? 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);
 
            }
...
 
@@ -90,3 +136,3 @@ namespace DesertPaintCodex.Services
 
            {
 
                Match match = _reagentRegex.Match(line);
 
                Match match = _ppReagentRegex.Match(line);
 
                if (match.Success)
...
 
@@ -94,3 +140,8 @@ namespace DesertPaintCodex.Services
 
                    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))
 
                    {
...
 
@@ -102,6 +153,2 @@ namespace DesertPaintCodex.Services
 
                    }
 
                    else
 
                    {
 
                        // bad name?
 
                    }
 
                }
...
 
@@ -109,3 +156,3 @@ namespace DesertPaintCodex.Services
 
                {
 
                    match = _catalystRegex.Match(line);
 
                    match = _ppCatalystRegex.Match(line);
 
                    
...
 
@@ -114,3 +161,8 @@ namespace DesertPaintCodex.Services
 
                    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))
 
                    {
...
 
@@ -123,6 +175,2 @@ namespace DesertPaintCodex.Services
 
                    }
 
                    else
 
                    {
 
                        // bad name?
 
                    }
 
                }
...
 
@@ -191,7 +239,21 @@ namespace DesertPaintCodex.Services
 
            
 
            if (_reagents.TryGetValue(reagentName, out Reagent? returnVal)) return returnVal;
 
            // convert pp name to our internal name
 
            if (_nameLookup.TryGetValue(reagentName, out string? otherName))
 
            Reagent? returnVal = GetReagentByInternalName(reagentName);
 

	
 
            if (returnVal != null) return returnVal;
 

	
 
            returnVal = GetReagentByPPName(reagentName);
 

	
 
            if (returnVal != null) return returnVal;
 
            
 
            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;
 
            }
...
 
@@ -202,3 +264,15 @@ namespace DesertPaintCodex.Services
 
        }
 

	
 
        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;
 
    }
 
}
 
}
Util/Constants.cs
Show inline comments
...
 
@@ -20,5 +20,5 @@ namespace DesertPaintCodex.Util
 
                {
 
                    _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);
0 comments (0 inline, 0 general)