Files
@ 7117d2e703c8
Branch filter:
Location: ATITD-Tools/Desert-Paint-Codex/Services/ReagentService.cs
7117d2e703c8
8.0 KiB
text/x-csharp
Updated scanner for Tale 10, and fixed several bugs.
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 | 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
{
// 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 _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 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 bool _initialized = false;
public static List<string> Names { get; } = new();
public static void Initialize()
{
if (_initialized) return;
string? reagentsPath = FileUtils.FindApplicationResourceFile("ingredients.txt");
Debug.Assert(reagentsPath != null);
Load(reagentsPath);
}
// Loads reagent name/colors
public static void Load(string file)
{
if (_lastReagentsFile == file) return;
_lastReagentsFile = file;
_reagents.Clear();
_nameLookup.Clear();
Names.Clear();
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;
_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)));
// nameStore.AppendValues(name);
Names.Add(name);
_nameLookup.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));
// nameStore.AppendValues(name);
Names.Add(name);
_nameLookup.Add(ppname, name);
}
}
}
public static void LoadProfileReagents(string file)
{
Initialize();
using StreamReader reader = new(file);
string? line;
while ((line = reader.ReadLine()) != null)
{
Match match = _reagentRegex.Match(line);
if (match.Success)
{
string ppname = match.Groups["name"].Value;
if (_nameLookup.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
{
// bad name?
}
}
else
{
match = _catalystRegex.Match(line);
if (!match.Success) continue;
string ppname = match.Groups["name"].Value;
if (_nameLookup.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);
}
else
{
// bad name?
}
}
}
}
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();
if (_reagents.TryGetValue(reagentName, out Reagent? returnVal)) return returnVal;
// convert pp name to our internal name
if (_nameLookup.TryGetValue(reagentName, out string? otherName))
{
_reagents.TryGetValue(otherName, out returnVal);
}
Debug.Assert(returnVal != null);
return returnVal;
}
}
}
|