Files
@ 4778889395e8
Branch filter:
Location: ATITD-Tools/Desert-Paint-Codex/Services/ReagentService.cs
4778889395e8
10.9 KiB
text/x-csharp
Change the displayed permeutation count to display using locale-specific number formatting (with comma / dot separators). Fix a crash when starting a new round of recipe generation after recipe generation completed during a previous run.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 | using System;
using System.IO;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text.RegularExpressions;
using DesertPaintCodex.Models;
using DesertPaintCodex.Util;
namespace DesertPaintCodex.Services
{
internal static class ReagentService
{
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> _ppToInternalName = new(); // pp name to our name
private static readonly Dictionary<string, string> _ppRenames = new();
private static readonly Dictionary<string, string> _internalRenames = new();
private static bool _initialized = false;
public static List<string> Names { get; } = new();
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
private static void Load(string file)
{
_reagents.Clear();
_ppToInternalName.Clear();
Names.Clear();
int index = 0;
using StreamReader reader = new(file);
string? line;
while ((line = reader.ReadLine()) != null)
{
Match match = _internalReagentRegex.Match(line);
if (match.Success)
{
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),
index++));
// nameStore.AppendValues(name);
Names.Add(name);
_ppToInternalName.Add(ppname, name);
}
else
{
match = _internalCatalystRegex.Match(line);
if (!match.Success) continue;
string name = match.Groups["name"].Value;
string ppname = match.Groups["ppname"].Value;
_reagents.Add(name, new Reagent(ppname, ppname, index++));
// nameStore.AppendValues(name);
Names.Add(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);
}
}
public static void LoadProfileReagents(string file)
{
Initialize();
using StreamReader reader = new(file);
string? line;
while ((line = reader.ReadLine()) != null)
{
Match match = _ppReagentRegex.Match(line);
if (match.Success)
{
string ppname = match.Groups["name"].Value;
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;
reagent.Enabled = match.Groups["enabled"].Value.Equals("Y");
reagent.Cost = uint.Parse(match.Groups["cost"].Value);
reagent.RecipeMax = uint.Parse(match.Groups["max"].Value);
}
}
else
{
match = _ppCatalystRegex.Match(line);
if (!match.Success) continue;
string ppname = match.Groups["name"].Value;
if (_ppRenames.TryGetValue(ppname, out string? rename))
{
ppname = rename; // Apply rename.
}
if (_ppToInternalName.TryGetValue(ppname, out string? name))
{
Reagent reagent = GetReagent(name);
if (reagent is not {IsCatalyst: true}) continue;
reagent.Enabled = match.Groups["enabled"].Value.Equals("Y");
reagent.Cost = uint.Parse(match.Groups["cost"].Value);
}
}
}
}
public static void SaveProfileReagents(string file)
{
Initialize();
using StreamWriter writer = new(file);
writer.WriteLine("// Ingredients are in the form:");
writer.WriteLine("// Name | RGB values | cost | enabled (Y/N) | bulk/normal | max items per paint (1-20)");
writer.WriteLine("//");
writer.WriteLine("// It is recommended to only change the cost value");
writer.WriteLine("// It is not recommended to set many of the ingredients above 10 per paint");
List<Reagent> sortedReagents = new(_reagents.Count);
sortedReagents.AddRange(_reagents.Values);
sortedReagents.Sort((x, y) =>
((x.IsCatalyst && !y.IsCatalyst) ?
1 : ((y.IsCatalyst && !x.IsCatalyst) ?
-1 : string.Compare(x.PracticalPaintName, y.PracticalPaintName, StringComparison.InvariantCulture))));
foreach (Reagent reagent in sortedReagents)
{
if (!reagent.IsCatalyst)
{
writer.WriteLine("{0,-10} | {1,3}, {2,3}, {3,3} | {4,7} | {5} | {6} | {7}",
reagent.PracticalPaintName,
reagent.Color?.Red, reagent.Color?.Blue, reagent.Color?.Green,
reagent.Cost,
reagent.Enabled ? "Y" : "N",
reagent.RecipeMax >= 10 ? " bulk" : "normal",
reagent.RecipeMax);
}
else
{
writer.WriteLine("{0,-10} | catalyst | {1,7} | {2} | normal | 1",
reagent.PracticalPaintName,
reagent.Cost,
reagent.Enabled ? "Y" : "N");
}
}
}
public static void InitializeReactions(ReactionSet reactions)
{
Initialize();
foreach (KeyValuePair<string, Reagent> pair1 in _reagents)
{
foreach (KeyValuePair<string, Reagent> pair2 in _reagents)
{
if (pair1.Key != pair2.Key)
{
reactions.Set(pair1.Value, pair2.Value, null);
}
}
}
}
public static Reagent GetReagent(string reagentName)
{
Initialize();
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))
{
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;
}
}
}
|