RynthSuite as of 2026-10-04 (internal c94715f): RynthOracle (based on Oracle of Dereth by Advis Eveldan) and RynthInventory, RynthAi patrol doorways and rooms, healing priority, strict monster priority, finish the wounded target, slayer weapons, arcs when clear, blasts like rings, learn unknown spells, looting fixes, release 2026.10.4.6
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
c33e24a610
commit
12ca25f911
426 changed files with 108239 additions and 7521 deletions
403
Tools/RynthCore.LootSdkTests/LootEditTests.cs
Normal file
403
Tools/RynthCore.LootSdkTests/LootEditTests.cs
Normal file
|
|
@ -0,0 +1,403 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using RynthCore.Loot.Editing;
|
||||
using RynthCore.Loot.VTank;
|
||||
|
||||
namespace RynthCore.LootSdkTests;
|
||||
|
||||
// LootEditSession: the plugin-side logic behind the in-game Loot Editor
|
||||
// (RynthCore docs/IMGUI_LOOT_EDITOR.md). Load, the edit commands, validation,
|
||||
// the no-clobber save, and above all that a round trip (open, edit through the
|
||||
// wire DTOs, save) loses no rule data. Files go to a temp folder only.
|
||||
internal static partial class Program
|
||||
{
|
||||
// Every shape the editor must carry: v1 header, custom expression, priority,
|
||||
// Keep # with its count line, a DisabledRule node in the middle, typed nodes,
|
||||
// raw nodes (SpellMatch, colours), an unknown node type, odd length codes,
|
||||
// and a SalvageCombine block.
|
||||
private static readonly string EditFixture = string.Join("\n", new[]
|
||||
{
|
||||
"UTL", "1", "5",
|
||||
"Keep Coins", "", "3;1;7", "5", "7",
|
||||
"Keep 25 Peas", "expr here", "0;10;1;12", "25", "0", "Pea$", "1", "0", "5", "19",
|
||||
"Disabled Junk", "", "0;3;2;9999;4", "0", "250", "28", "0", "true", "0", "0.5", "5",
|
||||
"Unknown Node", "", "0;4;4242", "0", "mystery 1", "mystery 2",
|
||||
"Spells and Colours", "", "7;2;9;14;1000", "12", "Legendary", "Epic", "2", "0", "10", "20", "30", "0.1", "0.2",
|
||||
"0", "300", "34",
|
||||
"SalvageCombine", "0", "1", "1-6, 7-8, 9, 10", "1", "60", "1-9, 10", "0",
|
||||
}) + "\n";
|
||||
|
||||
private static string _tempDir = string.Empty;
|
||||
|
||||
private static void RunLootEditTests(string[] args)
|
||||
{
|
||||
_tempDir = Path.Combine(Path.GetTempPath(), "RynthLootEditTests_" + Guid.NewGuid().ToString("N"));
|
||||
Directory.CreateDirectory(_tempDir);
|
||||
try
|
||||
{
|
||||
TestEditUneditedSaveIsByteIdentical();
|
||||
TestEditEncodingsPreserved();
|
||||
TestEditWireRoundTripEveryRule();
|
||||
TestEditCommands();
|
||||
TestEditValidation();
|
||||
TestEditNoClobber();
|
||||
TestEditDiskWatch();
|
||||
TestEditReadOnlyCases();
|
||||
TestEditNewProfile();
|
||||
TestEditVocab();
|
||||
TestEditValueNames();
|
||||
TestEditDeployedProfile(args);
|
||||
}
|
||||
finally
|
||||
{
|
||||
try { Directory.Delete(_tempDir, recursive: true); } catch { }
|
||||
}
|
||||
}
|
||||
|
||||
private static string TempFile(string name, byte[] bytes)
|
||||
{
|
||||
string path = Path.Combine(_tempDir, name);
|
||||
File.WriteAllBytes(path, bytes);
|
||||
return path;
|
||||
}
|
||||
|
||||
private static LootEditSession OpenOk(string path)
|
||||
{
|
||||
var s = new LootEditSession();
|
||||
Check(s.Open(path), $"open {Path.GetFileName(path)}: {s.BuildState("", null).Message}");
|
||||
return s;
|
||||
}
|
||||
|
||||
private static LootEditSession.Outcome Cmd(LootEditSession s, string op, int index = -1, string value = "", int to = -1,
|
||||
string? expect = null, bool force = false, LootEditRule? rule = null)
|
||||
=> s.Apply(new LootEditCommand { Op = op, Index = index, Value = value, To = to, Expect = expect, Force = force, Rule = rule });
|
||||
|
||||
private static void TestEditUneditedSaveIsByteIdentical()
|
||||
{
|
||||
Console.WriteLine("\n-- LootEdit: unedited save is byte-identical --");
|
||||
byte[] original = Encoding.UTF8.GetBytes(EditFixture);
|
||||
string path = TempFile("fixture.utl", original);
|
||||
LootEditSession s = OpenOk(path);
|
||||
Eq(s.RuleCount, 5, "fixture rule count");
|
||||
Check(!s.IsReadOnly, "fixture is editable (round-trips)");
|
||||
Check(s.Encode().AsSpan().SequenceEqual(original), "Encode() == original bytes");
|
||||
Check(s.Save(), "save");
|
||||
Check(File.ReadAllBytes(path).AsSpan().SequenceEqual(original), "saved file == original bytes");
|
||||
Check(File.Exists(path + ".bak"), ".bak kept");
|
||||
Check(!File.Exists(path + ".tmp"), "no .tmp left");
|
||||
}
|
||||
|
||||
private static void TestEditEncodingsPreserved()
|
||||
{
|
||||
Console.WriteLine("\n-- LootEdit: encoding, CRLF, BOM preserved --");
|
||||
// CRLF + UTF-8 BOM + a non-ASCII name.
|
||||
string crlf = EditFixture.Replace("Keep Coins", "Keep Coins \u00e9\u2605").Replace("\n", "\r\n");
|
||||
byte[] bomBytes = new byte[] { 0xEF, 0xBB, 0xBF }.Concat(Encoding.UTF8.GetBytes(crlf)).ToArray();
|
||||
LootEditSession s = OpenOk(TempFile("bom_crlf.utl", bomBytes));
|
||||
Check(s.Encode().AsSpan().SequenceEqual(bomBytes), "UTF-8 BOM + CRLF round-trips byte for byte");
|
||||
Eq(s.Profile!.Rules[0].Name, "Keep Coins \u00e9\u2605", "non-ASCII name read as UTF-8");
|
||||
|
||||
// Windows-1252/Latin-1 bytes (not valid UTF-8): kept byte for byte.
|
||||
byte[] latin = Encoding.Latin1.GetBytes(EditFixture.Replace("Keep Coins", "Keep Caf\u00e9 \u00a3"));
|
||||
LootEditSession l = OpenOk(TempFile("latin1.utl", latin));
|
||||
Check(l.Encode().AsSpan().SequenceEqual(latin), "Latin-1 file round-trips byte for byte");
|
||||
Eq(l.Profile!.Rules[0].Name, "Keep Caf\u00e9 \u00a3", "Latin-1 name decoded");
|
||||
|
||||
// No final newline stays that way.
|
||||
byte[] noNl = Encoding.UTF8.GetBytes(EditFixture.TrimEnd('\n'));
|
||||
LootEditSession n = OpenOk(TempFile("nonl.utl", noNl));
|
||||
Check(n.Encode().AsSpan().SequenceEqual(noNl), "missing final newline preserved");
|
||||
}
|
||||
|
||||
private static void TestEditWireRoundTripEveryRule()
|
||||
{
|
||||
Console.WriteLine("\n-- LootEdit: every rule through the wire DTOs and back --");
|
||||
byte[] original = Encoding.UTF8.GetBytes(EditFixture);
|
||||
LootEditSession s = OpenOk(TempFile("wire.utl", original));
|
||||
WireRoundTripAll(s, "fixture");
|
||||
Check(s.Encode().AsSpan().SequenceEqual(original), "after update_rule of every rule, bytes unchanged");
|
||||
Check(s.IsDirty, "update_rule marks dirty");
|
||||
|
||||
// The state payload survives JSON too.
|
||||
LootEditState st = s.BuildState(Path.Combine(_tempDir, "wire.utl"), new List<LootEditFile> { new() { Path = "x", Display = "x" } });
|
||||
string json = JsonSerializer.Serialize(st, LootEditJsonContext.Default.LootEditState);
|
||||
LootEditState? back = JsonSerializer.Deserialize(json, LootEditJsonContext.Default.LootEditState);
|
||||
Check(back != null && back.Rules.Count == 5 && back.InUse && back.Dirty, "state JSON round-trip");
|
||||
Eq(back?.Rules[2].Enabled, false, "row shows the disabled rule");
|
||||
Eq(back?.Rules[2].Conditions, 2, "row condition count leaves out DisabledRule");
|
||||
Eq(back?.Rules[1].KeepCount, 25, "row keep count");
|
||||
}
|
||||
|
||||
/// <summary>BuildRule(i) -> JSON -> update_rule(i) for every rule.</summary>
|
||||
private static void WireRoundTripAll(LootEditSession s, string label)
|
||||
{
|
||||
int failed = 0;
|
||||
for (int i = 0; i < s.RuleCount; i++)
|
||||
{
|
||||
LootEditRule? dto = s.BuildRule(i);
|
||||
if (dto == null) { failed++; continue; }
|
||||
string json = JsonSerializer.Serialize(dto, LootEditJsonContext.Default.LootEditRule);
|
||||
LootEditRule? back = JsonSerializer.Deserialize(json, LootEditJsonContext.Default.LootEditRule);
|
||||
var cmd = new LootEditCommand { Op = "update_rule", Index = i, Expect = dto.Name, Rule = back };
|
||||
string cmdJson = JsonSerializer.Serialize(cmd, LootEditJsonContext.Default.LootEditCommand);
|
||||
LootEditCommand? cmdBack = JsonSerializer.Deserialize(cmdJson, LootEditJsonContext.Default.LootEditCommand);
|
||||
if (cmdBack == null || s.Apply(cmdBack) != LootEditSession.Outcome.Changed)
|
||||
{
|
||||
failed++;
|
||||
if (failed <= 5) Console.WriteLine($" [FAIL] {label} rule {i} '{dto.Name}': {s.BuildState("", null).Message}");
|
||||
}
|
||||
}
|
||||
_asserts++;
|
||||
if (failed > 0) { _fails++; Console.WriteLine($" [FAIL] {label}: {failed} rule(s) refused update_rule"); }
|
||||
}
|
||||
|
||||
private static void TestEditCommands()
|
||||
{
|
||||
Console.WriteLine("\n-- LootEdit: commands --");
|
||||
LootEditSession s = OpenOk(TempFile("cmds.utl", Encoding.UTF8.GetBytes(EditFixture)));
|
||||
List<VTankLootRule> r = s.Profile!.Rules;
|
||||
|
||||
// Disable a rule that has no DisabledRule node: one is appended; enable removes it.
|
||||
Eq(Cmd(s, "set_enabled", 0, "false", expect: "Keep Coins"), LootEditSession.Outcome.Changed, "disable");
|
||||
Check(!r[0].Enabled && r[0].Conditions.Last().NodeType == VTankNodeTypes.DisabledRule, "disable appends 9999 true");
|
||||
Eq(Cmd(s, "set_enabled", 0, "true"), LootEditSession.Outcome.Changed, "enable");
|
||||
Check(r[0].Enabled && r[0].Conditions.All(c => c.NodeType != VTankNodeTypes.DisabledRule), "enable removes 9999");
|
||||
|
||||
// Enable/disable a rule whose DisabledRule node sits mid-list: disable reuses it in place.
|
||||
Cmd(s, "set_enabled", 2, "true");
|
||||
Check(r[2].Enabled, "rule 2 enabled");
|
||||
Eq(r[2].Conditions.Count, 2, "rule 2 lost its 9999 node");
|
||||
LootEditSession s2 = OpenOk(TempFile("cmds2.utl", Encoding.UTF8.GetBytes(EditFixture)));
|
||||
var c9999 = s2.Profile!.Rules[2].Conditions[1];
|
||||
c9999.DataLines[0] = "false";
|
||||
Cmd(s2, "set_enabled", 2, "false");
|
||||
Eq(s2.Profile.Rules[2].Conditions[1].DataLines[0], "true", "disable flips the existing node in place");
|
||||
Eq(s2.Profile.Rules[2].Conditions.Count, 3, "no extra node");
|
||||
|
||||
// Move, add, duplicate, delete, rename.
|
||||
Eq(Cmd(s, "move", 0, to: 3), LootEditSession.Outcome.Changed, "move 0 -> 3");
|
||||
Eq(r[3].Name, "Keep Coins", "moved rule at 3");
|
||||
Eq(r[0].Name, "Keep 25 Peas", "rule 1 shifted up");
|
||||
Eq(s.BuildState("", null).Focus, 3, "focus follows the moved rule");
|
||||
|
||||
Eq(Cmd(s, "add", 0), LootEditSession.Outcome.Changed, "add after 0");
|
||||
Eq(r[1].Name, "New Rule", "new rule at 1");
|
||||
Eq(r[1].CustomExpression, "", "new rule has the v1 custom expression line");
|
||||
Eq(s.RuleCount, 6, "count after add");
|
||||
|
||||
Eq(Cmd(s, "duplicate", 0), LootEditSession.Outcome.Changed, "duplicate 0");
|
||||
Eq(r[1].Name, "Keep 25 Peas (copy)", "copy name");
|
||||
Eq(r[1].KeepCount, 25, "copy keep count");
|
||||
Check(!ReferenceEquals(r[1].Conditions[0].DataLines, r[0].Conditions[0].DataLines), "copy has its own lines");
|
||||
|
||||
Eq(Cmd(s, "delete", 1, expect: "Keep 25 Peas (copy)"), LootEditSession.Outcome.Changed, "delete copy");
|
||||
Eq(s.RuleCount, 6, "count after delete");
|
||||
|
||||
Eq(Cmd(s, "rename", 1, "Renamed\nrule", expect: "New Rule"), LootEditSession.Outcome.Changed, "rename");
|
||||
Eq(r[1].Name, "Renamed rule", "newline in name became a space");
|
||||
|
||||
// A stale index is refused, not applied to the wrong rule.
|
||||
Eq(Cmd(s, "delete", 0, expect: "Something Else"), LootEditSession.Outcome.Refused, "Expect mismatch refused");
|
||||
Eq(s.RuleCount, 6, "nothing deleted");
|
||||
Eq(Cmd(s, "delete", 99), LootEditSession.Outcome.Refused, "out of range refused");
|
||||
|
||||
// Saved and reopened, the edits are there.
|
||||
Check(s.Save(), "save edits");
|
||||
LootEditSession re = OpenOk(s.Path);
|
||||
Eq(re.RuleCount, 6, "reopened count");
|
||||
Eq(re.Profile!.Rules[1].Name, "Renamed rule", "reopened rename");
|
||||
Eq(re.Profile.Rules[4].Name, "Keep Coins", "reopened order");
|
||||
Check(re.Profile.Rules[2].Enabled, "reopened enable");
|
||||
}
|
||||
|
||||
private static void TestEditValidation()
|
||||
{
|
||||
Console.WriteLine("\n-- LootEdit: validation --");
|
||||
LootEditSession s = OpenOk(TempFile("valid.utl", Encoding.UTF8.GetBytes(EditFixture)));
|
||||
|
||||
LootEditRule dto = s.BuildRule(0)!;
|
||||
dto.Conditions[0].Lines[0] = "abc"; // ObjectClass must be a whole number
|
||||
Eq(Cmd(s, "update_rule", 0, rule: dto), LootEditSession.Outcome.Refused, "non-numeric class refused");
|
||||
Eq(s.Profile!.Rules[0].Conditions[0].DataLines[0], "7", "rule untouched after refusal");
|
||||
|
||||
dto = s.BuildRule(0)!;
|
||||
dto.Action = 99;
|
||||
Eq(Cmd(s, "update_rule", 0, rule: dto), LootEditSession.Outcome.Refused, "unknown action refused");
|
||||
|
||||
dto = s.BuildRule(0)!;
|
||||
dto.Conditions[0].Lines.Add("extra");
|
||||
Eq(Cmd(s, "update_rule", 0, rule: dto), LootEditSession.Outcome.Refused, "too many lines refused");
|
||||
|
||||
dto = s.BuildRule(0)!;
|
||||
dto.Conditions.Add(new LootEditCondition { NodeType = VTankNodeTypes.LongValKeyGE, Lines = new List<string> { "300" } });
|
||||
Eq(Cmd(s, "update_rule", 0, rule: dto), LootEditSession.Outcome.Changed, "short lines padded");
|
||||
Eq(s.Profile.Rules[0].Conditions[1].DataLines.Count, 2, "padded to arity");
|
||||
|
||||
dto = s.BuildRule(0)!;
|
||||
dto.Name = "Two\r\nLines";
|
||||
dto.Action = (int)VTankLootAction.KeepUpTo;
|
||||
dto.KeepCount = 7;
|
||||
dto.Conditions[0].Lines[0] = " 12 ";
|
||||
Eq(Cmd(s, "update_rule", 0, rule: dto), LootEditSession.Outcome.Changed, "update with Keep #");
|
||||
VTankLootRule r0 = s.Profile.Rules[0];
|
||||
Eq(r0.Name, "Two Lines", "CRLF in name flattened");
|
||||
Eq(r0.KeepCount, 7, "keep count set");
|
||||
Eq(r0.Conditions[0].DataLines[0], "12", "number trimmed");
|
||||
dto = s.BuildRule(0)!;
|
||||
dto.Action = (int)VTankLootAction.Sell;
|
||||
Cmd(s, "update_rule", 0, rule: dto);
|
||||
Eq(s.Profile.Rules[0].KeepCount, null, "keep count line dropped for Sell");
|
||||
|
||||
// The unknown node type (4242) passes through untouched but can't be invented.
|
||||
dto = s.BuildRule(3)!;
|
||||
Eq(dto.Conditions[0].NodeType, 4242, "rule 3 has the unknown node");
|
||||
Eq(Cmd(s, "update_rule", 3, rule: dto), LootEditSession.Outcome.Changed, "unknown node kept as-is");
|
||||
dto.Conditions[0].Lines[0] = "changed";
|
||||
Eq(Cmd(s, "update_rule", 3, rule: dto), LootEditSession.Outcome.Refused, "edited unknown node refused");
|
||||
|
||||
// Enabled on the DTO reconciles the DisabledRule node.
|
||||
dto = s.BuildRule(2)!;
|
||||
dto.Enabled = true;
|
||||
Cmd(s, "update_rule", 2, rule: dto);
|
||||
Check(s.Profile.Rules[2].Enabled, "DTO Enabled=true enables");
|
||||
}
|
||||
|
||||
private static void TestEditNoClobber()
|
||||
{
|
||||
Console.WriteLine("\n-- LootEdit: save never clobbers a file changed on disk --");
|
||||
string path = TempFile("clobber.utl", Encoding.UTF8.GetBytes(EditFixture));
|
||||
LootEditSession s = OpenOk(path);
|
||||
Cmd(s, "rename", 0, "Mine");
|
||||
|
||||
string theirs = EditFixture.Replace("Keep Coins", "Theirs");
|
||||
File.WriteAllText(path, theirs);
|
||||
File.SetLastWriteTimeUtc(path, DateTime.UtcNow.AddMinutes(1));
|
||||
Eq(Cmd(s, "save"), LootEditSession.Outcome.Refused, "save refused after an outside change");
|
||||
Check(s.BuildState("", null).ChangedOnDisk, "ChangedOnDisk flagged");
|
||||
Eq(File.ReadAllText(path), theirs, "their file untouched");
|
||||
|
||||
Eq(Cmd(s, "open", force: false) , LootEditSession.Outcome.Refused, "open with no path refused");
|
||||
Eq(Cmd(s, "reload"), LootEditSession.Outcome.Refused, "reload without force refused (unsaved edits)");
|
||||
Eq(Cmd(s, "save", force: true), LootEditSession.Outcome.Saved, "Save anyway");
|
||||
Check(File.ReadAllText(path).Contains("Mine\n"), "our edit written");
|
||||
Eq(File.ReadAllText(path + ".bak"), theirs, "their version kept as .bak");
|
||||
Check(!s.BuildState("", null).ChangedOnDisk && !s.IsDirty, "clean after save");
|
||||
|
||||
// Same-content rewrite (write time moves, bytes the same) doesn't block a save.
|
||||
Cmd(s, "rename", 0, "Mine again");
|
||||
File.SetLastWriteTimeUtc(path, DateTime.UtcNow.AddMinutes(2));
|
||||
Eq(Cmd(s, "save"), LootEditSession.Outcome.Saved, "touch-only change doesn't block save");
|
||||
}
|
||||
|
||||
private static void TestEditDiskWatch()
|
||||
{
|
||||
Console.WriteLine("\n-- LootEdit: disk watch --");
|
||||
string path = TempFile("watch.utl", Encoding.UTF8.GetBytes(EditFixture));
|
||||
LootEditSession s = OpenOk(path);
|
||||
Check(!s.CheckDisk(), "no change, nothing to do");
|
||||
|
||||
File.WriteAllText(path, EditFixture.Replace("Keep Coins", "Outside Edit"));
|
||||
File.SetLastWriteTimeUtc(path, DateTime.UtcNow.AddMinutes(1));
|
||||
Check(s.CheckDisk(), "change noticed");
|
||||
Eq(s.Profile!.Rules[0].Name, "Outside Edit", "clean session reloaded");
|
||||
|
||||
Cmd(s, "rename", 1, "Local");
|
||||
File.WriteAllText(path, EditFixture);
|
||||
File.SetLastWriteTimeUtc(path, DateTime.UtcNow.AddMinutes(2));
|
||||
Check(s.CheckDisk(), "change under edits noticed");
|
||||
Eq(s.Profile.Rules[1].Name, "Local", "dirty session keeps its edits");
|
||||
Check(s.BuildState("", null).ChangedOnDisk, "and flags the conflict");
|
||||
Eq(Cmd(s, "reload", force: true), LootEditSession.Outcome.Opened, "Reload (discard)");
|
||||
Eq(s.Profile!.Rules[1].Name, "Keep 25 Peas", "reloaded from disk");
|
||||
}
|
||||
|
||||
private static void TestEditReadOnlyCases()
|
||||
{
|
||||
Console.WriteLine("\n-- LootEdit: read-only cases --");
|
||||
string json = "{\"Name\":\"j\",\"Rules\":[{\"Name\":\"Keep gems\",\"Enabled\":true,\"Action\":\"Keep\",\"Conditions\":[{\"$t\":\"ObjectClass\",\"ObjectClass\":\"Gem\"}]}]}";
|
||||
string jpath = TempFile("legacy.json", Encoding.UTF8.GetBytes(json));
|
||||
LootEditSession j = OpenOk(jpath);
|
||||
Check(j.IsReadOnly, "JSON opens read-only");
|
||||
Eq(j.RuleCount, 1, "JSON rule count");
|
||||
Eq(Cmd(j, "rename", 0, "x"), LootEditSession.Outcome.Refused, "JSON edit refused");
|
||||
Eq(Cmd(j, "save"), LootEditSession.Outcome.Refused, "JSON save refused");
|
||||
Eq(File.ReadAllText(jpath), json, "JSON file untouched");
|
||||
Eq(j.BuildRule(0)?.Conditions[0].NodeType, -1, "JSON condition shown as text");
|
||||
Eq(j.BuildState("", null).Format, "json", "format json");
|
||||
|
||||
// An info line with a part the parser skips: writing back would drop it, so read-only.
|
||||
string odd = EditFixture.Replace("3;1;7", "3;1;7;abc");
|
||||
string opath = TempFile("odd.utl", Encoding.UTF8.GetBytes(odd));
|
||||
LootEditSession o = OpenOk(opath);
|
||||
Check(o.IsReadOnly, "non-round-tripping .utl opens read-only");
|
||||
Eq(o.RuleCount, 5, "still lists its rules");
|
||||
Eq(Cmd(o, "delete", 0), LootEditSession.Outcome.Refused, "edits refused");
|
||||
Eq(File.ReadAllText(opath), odd, "file untouched");
|
||||
|
||||
// An unknown node in the last rule swallows the file's tail (the parser can't
|
||||
// tell where it ends): writing back would change the file, so read-only.
|
||||
string tail = string.Join("\n", new[] { "UTL", "1", "1", "Last", "", "0;1;4242", "0", "a", "b" }) + "\n";
|
||||
LootEditSession t = OpenOk(TempFile("tail.utl", Encoding.UTF8.GetBytes(tail)));
|
||||
Check(t.IsReadOnly, "unknown node in the last rule: read-only");
|
||||
|
||||
var bad = new LootEditSession();
|
||||
Check(!bad.Open(TempFile("bad.utl", Encoding.UTF8.GetBytes("UTL\nx\n"))), "garbage refused cleanly");
|
||||
Check(!bad.Open(Path.Combine(_tempDir, "missing.json")), "missing JSON refused");
|
||||
Check(!bad.Open(Path.Combine(_tempDir, "notes.txt")), "other extension refused");
|
||||
}
|
||||
|
||||
private static void TestEditNewProfile()
|
||||
{
|
||||
Console.WriteLine("\n-- LootEdit: new profile (vendor's first) --");
|
||||
string path = Path.Combine(_tempDir, "AutoVendor", "Some Vendor.utl");
|
||||
var s = new LootEditSession();
|
||||
Check(s.Open(path), "open a path that doesn't exist");
|
||||
Check(!s.BuildState("", null).Exists, "state says new");
|
||||
Eq(Cmd(s, "add"), LootEditSession.Outcome.Changed, "add a rule");
|
||||
Check(s.Save(), "save creates it (and its folder)");
|
||||
LootEditSession re = OpenOk(path);
|
||||
Eq(re.RuleCount, 1, "created file has the rule");
|
||||
Eq(re.Profile!.FileVersion, 1, "v1 file");
|
||||
}
|
||||
|
||||
private static void TestEditVocab()
|
||||
{
|
||||
Console.WriteLine("\n-- LootEdit: vocabulary --");
|
||||
LootEditVocab v = LootRuleText.BuildVocab();
|
||||
Eq(v.Actions.Count, 5, "5 actions");
|
||||
Eq(v.NodeTypes.Count, VTankNodeTypes.All.Length, "every node type listed");
|
||||
Check(v.NodeTypes.All(n => n.Defaults.Count == n.Lines), "defaults match arity");
|
||||
Check(v.NodeTypes.All(n => n.Name.All(ch => ch < 128)), "node names are ASCII");
|
||||
Check(v.ObjectClasses.Any(c => c.Id == 11 && c.Name == "Gem"), "object classes use the Decal/VTank numbering");
|
||||
string json = JsonSerializer.Serialize(v, LootEditJsonContext.Default.LootEditVocab);
|
||||
Check(JsonSerializer.Deserialize(json, LootEditJsonContext.Default.LootEditVocab)?.NodeTypes.Count == v.NodeTypes.Count, "vocab JSON round-trip");
|
||||
foreach (LootEditNodeType n in v.NodeTypes)
|
||||
if (n.Editor != "raw")
|
||||
Check(n.Labels.Count == n.Lines, $"typed editor {n.Name} labels every line");
|
||||
}
|
||||
|
||||
/// <summary>Bonus: the real deployed profile through the session (read only, never written).</summary>
|
||||
private static void TestEditDeployedProfile(string[] args)
|
||||
{
|
||||
string path = args.FirstOrDefault(a => a.EndsWith("LootSnobV4.utl", StringComparison.OrdinalIgnoreCase))
|
||||
?? @"C:\Games\RynthSuite\RynthAi\LootProfiles\LootSnobV4.utl";
|
||||
if (!File.Exists(path)) return;
|
||||
Console.WriteLine("\n-- LootEdit: deployed profile (bonus, read only) --");
|
||||
byte[] original = File.ReadAllBytes(path);
|
||||
var s = new LootEditSession();
|
||||
Check(s.Open(path), "open deployed profile");
|
||||
Check(!s.IsReadOnly, "deployed profile is editable");
|
||||
Check(s.Encode().AsSpan().SequenceEqual(original), "deployed: Encode() == file bytes");
|
||||
WireRoundTripAll(s, "deployed");
|
||||
Check(s.Encode().AsSpan().SequenceEqual(original), "deployed: every rule through the wire, bytes unchanged");
|
||||
var sw = System.Diagnostics.Stopwatch.StartNew();
|
||||
LootEditState st = s.BuildState(path, null);
|
||||
string json = JsonSerializer.Serialize(st, LootEditJsonContext.Default.LootEditState);
|
||||
long buildMs = sw.ElapsedMilliseconds;
|
||||
Console.WriteLine($" ok {Path.GetFileName(path)}: {s.RuleCount} rules, state JSON {json.Length / 1024} KB built in {buildMs} ms");
|
||||
}
|
||||
}
|
||||
152
Tools/RynthCore.LootSdkTests/LootValueNameTests.cs
Normal file
152
Tools/RynthCore.LootSdkTests/LootValueNameTests.cs
Normal file
|
|
@ -0,0 +1,152 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using RynthCore.Loot.Editing;
|
||||
using RynthCore.Loot.VTank;
|
||||
|
||||
namespace RynthCore.LootSdkTests;
|
||||
|
||||
// Named values for long keys (LootRuleText.ValueTableFor): the tables, the
|
||||
// lookup, the vocabulary's LongValueTables, and that a value picked by name is
|
||||
// saved as the same number (the .utl format is unchanged).
|
||||
internal static partial class Program
|
||||
{
|
||||
private static void TestEditValueNames()
|
||||
{
|
||||
Console.WriteLine("\n-- LootEdit: named values for long keys --");
|
||||
|
||||
// Tables: the keys that have one, and the ones that don't.
|
||||
Check(ReferenceEquals(LootRuleText.ValueTableFor(159), LootRuleText.Skills), "159 WieldSkilltype -> skills");
|
||||
Check(ReferenceEquals(LootRuleText.ValueTableFor(48), LootRuleText.Skills), "48 WeaponSkill -> skills");
|
||||
Check(ReferenceEquals(LootRuleText.ValueTableFor(158), LootRuleText.WieldRequirements), "158 -> wield requirements");
|
||||
Check(ReferenceEquals(LootRuleText.ValueTableFor(131), LootRuleText.Materials), "131 -> materials");
|
||||
Check(ReferenceEquals(LootRuleText.ValueTableFor(45), LootRuleText.DamageTypes), "45 -> damage types");
|
||||
Check(LootRuleText.ValueTableFor(160) == null, "160 WieldDifficulty is a plain number");
|
||||
Check(LootRuleText.ValueTableFor(19) == null, "19 Value is a plain number");
|
||||
Check(LootRuleText.ValueTableIsFlags(45) && !LootRuleText.ValueTableIsFlags(159), "only DamageType is flags");
|
||||
foreach (int key in LootRuleText.KeysWithValueTables)
|
||||
{
|
||||
Check(LootRuleText.ValueTableFor(key) != null, $"key {key} listed with a table");
|
||||
Check(LootRuleText.LongKeys.Any(k => k.Id == key), $"key {key} is in LongKeys (its picker can choose it)");
|
||||
}
|
||||
|
||||
// Every table: unique ids, ASCII names (the in-game font), no empty names.
|
||||
foreach (var (label, table) in new[]
|
||||
{
|
||||
("skills", LootRuleText.Skills), ("wield requirements", LootRuleText.WieldRequirements),
|
||||
("materials", LootRuleText.Materials), ("damage types", LootRuleText.DamageTypes),
|
||||
})
|
||||
{
|
||||
Eq(table.Select(t => t.Id).Distinct().Count(), table.Length, $"{label}: ids unique");
|
||||
Check(table.All(t => t.Name.Length > 0 && t.Name.All(ch => ch >= 32 && ch < 128)), $"{label}: names ASCII");
|
||||
Eq(table.Select(t => t.Name).Distinct().Count(), table.Length, $"{label}: names unique");
|
||||
}
|
||||
|
||||
// Retail / ACE numbering (the old table had Light Weapons at 47, which is Missile Weapons).
|
||||
string Skill(int id) => LootRuleText.Skills.First(s => s.Id == id).Name;
|
||||
Eq(Skill(45), "Light Weapons", "skill 45");
|
||||
Eq(Skill(44), "Heavy Weapons", "skill 44");
|
||||
Eq(Skill(46), "Finesse Weapons", "skill 46");
|
||||
Eq(Skill(47), "Missile Weapons", "skill 47");
|
||||
Eq(Skill(41), "Two Handed", "skill 41");
|
||||
Eq(Skill(34), "War Magic", "skill 34");
|
||||
Eq(Skill(43), "Void Magic", "skill 43");
|
||||
Eq(Skill(33), "Life Magic", "skill 33");
|
||||
Eq(Skill(49), "Dual Wield", "skill 49");
|
||||
Eq(Skill(48), "Shield", "skill 48");
|
||||
Eq(Skill(54), "Summoning", "skill 54");
|
||||
Eq(LootRuleText.Skills.Length, 54, "skills 1..54");
|
||||
Check(LootRuleText.Skills.Select(s => s.Name).SequenceEqual(LootRuleText.Skills.Select(s => s.Name).OrderBy(n => n, StringComparer.Ordinal)),
|
||||
"skills listed alphabetically");
|
||||
Eq(LootRuleText.Materials.First(m => m.Id == 64).Name, "Steel", "material 64");
|
||||
Eq(LootRuleText.Materials.First(m => m.Id == 51).Name, "Ivory", "material 51");
|
||||
Eq(LootRuleText.Materials.First(m => m.Id == 15).Name, "Black Garnet", "material 15");
|
||||
Eq(LootRuleText.Materials.Length, 78, "materials 0..77");
|
||||
Eq(LootRuleText.WieldRequirements.First(w => w.Id == 2).Name, "Base Skill", "wield requirement 2 (RawSkill)");
|
||||
Eq(LootRuleText.WieldRequirements.Length, 13, "wield requirements 0..12");
|
||||
Eq(LootRuleText.DamageTypes.First(d => d.Id == 64).Name, "Electric", "damage type 64");
|
||||
|
||||
// Lookup and labels; unknown and odd values stay expressible.
|
||||
Eq(LootRuleText.ValueName(159, "45"), "Light Weapons", "ValueName 159/45");
|
||||
Eq(LootRuleText.ValueName(159, " 45 "), "Light Weapons", "ValueName trims");
|
||||
Eq(LootRuleText.ValueName(159, "45.0"), "Light Weapons", "ValueName reads a whole double");
|
||||
Eq(LootRuleText.ValueName(159, "999"), null, "ValueName unknown value");
|
||||
Eq(LootRuleText.ValueName(160, "45"), null, "ValueName key without a table");
|
||||
Eq(LootRuleText.ValueName(159, "abc"), null, "ValueName not a number");
|
||||
Eq(LootRuleText.ValueLabel(159, "45"), "Light Weapons (45)", "ValueLabel named");
|
||||
Eq(LootRuleText.ValueLabel(131, "999"), "value 999", "ValueLabel unknown");
|
||||
Eq(LootRuleText.ValueLabel(45, "7"), "value 7", "ValueLabel flag combination not named");
|
||||
Eq(LootRuleText.ValueLabel(160, "300"), "value 300", "ValueLabel plain key");
|
||||
Check(LootRuleText.TryParseValue("-3", out int neg) && neg == -3, "TryParseValue negative");
|
||||
Check(!LootRuleText.TryParseValue("4.5", out _), "TryParseValue refuses a fraction");
|
||||
|
||||
// Which conditions pick values by name.
|
||||
Check(LootRuleText.UsesValueNames(VTankNodeTypes.LongValKeyE), "= uses names");
|
||||
Check(LootRuleText.UsesValueNames(VTankNodeTypes.LongValKeyGE), ">= uses names");
|
||||
Check(LootRuleText.UsesValueNames(VTankNodeTypes.BuffedLongValKeyGE), "buffed >= uses names");
|
||||
Check(!LootRuleText.UsesValueNames(VTankNodeTypes.LongValKeyFlagExists), "has flag does not");
|
||||
Check(!LootRuleText.UsesValueNames(VTankNodeTypes.DoubleValKeyGE), "double keys do not");
|
||||
|
||||
// Vocabulary: the tables reach the face, and survive JSON.
|
||||
LootEditVocab v = LootRuleText.BuildVocab();
|
||||
Eq(v.LongValueTables.Count, LootRuleText.KeysWithValueTables.Length, "vocab has every value table");
|
||||
LootEditVocab? back = JsonSerializer.Deserialize(JsonSerializer.Serialize(v, LootEditJsonContext.Default.LootEditVocab),
|
||||
LootEditJsonContext.Default.LootEditVocab);
|
||||
LootEditValueTable? wst = back?.LongValueTables.FirstOrDefault(t => t.Key == 159);
|
||||
Check(wst != null && wst.Values.Any(n => n.Id == 45 && n.Name == "Light Weapons"), "vocab JSON keeps 159's names");
|
||||
Check(back?.LongValueTables.First(t => t.Key == 45).Flags == true, "vocab JSON keeps the flags mark");
|
||||
Check(back?.NodeTypes.First(n => n.Id == VTankNodeTypes.LongValKeyE).NamedValues == true, "vocab marks = as named");
|
||||
Check(back?.NodeTypes.First(n => n.Id == VTankNodeTypes.LongValKeyFlagExists).NamedValues == false, "vocab leaves has flag unnamed");
|
||||
// An older RynthAi's vocabulary (no LongValueTables) still reads: the face falls back to the text box.
|
||||
LootEditVocab? old = JsonSerializer.Deserialize("{\"LongKeys\":[{\"Id\":159,\"Name\":\"WieldSkilltype\"}]}",
|
||||
LootEditJsonContext.Default.LootEditVocab);
|
||||
Check(old != null && old.LongValueTables.Count == 0, "old vocab JSON: no value tables, no error");
|
||||
|
||||
// Summaries name the value.
|
||||
var cond = new VTankLootCondition(VTankNodeTypes.LongValKeyE, "0", new[] { "45", "159" });
|
||||
Eq(LootRuleText.Condition(cond), "WieldSkilltype = Light Weapons", "summary names the skill");
|
||||
Eq(LootRuleText.Condition(new VTankLootCondition(VTankNodeTypes.LongValKeyGE, "0", new[] { "300", "160" })),
|
||||
"WieldDifficulty >= 300", "summary of a plain key unchanged");
|
||||
Eq(LootRuleText.Condition(new VTankLootCondition(VTankNodeTypes.LongValKeyE, "0", new[] { "999", "131" })),
|
||||
"MaterialType = 999", "summary of an unknown value is the number");
|
||||
|
||||
// Round trip: values picked by name through the wire, saved, reopened: the same numbers.
|
||||
string Pick((int Id, string Name)[] table, string name) =>
|
||||
table.First(t => t.Name == name).Id.ToString(CultureInfo.InvariantCulture);
|
||||
LootEditSession s = OpenOk(TempFile("values.utl", Encoding.UTF8.GetBytes(EditFixture)));
|
||||
Eq(Cmd(s, "add", -1), LootEditSession.Outcome.Changed, "add a rule for the values");
|
||||
int idx = s.RuleCount - 1;
|
||||
LootEditRule dto = s.BuildRule(idx)!;
|
||||
dto.Name = "Light weapons, steel";
|
||||
dto.Conditions.Clear();
|
||||
dto.Conditions.Add(new LootEditCondition { NodeType = VTankNodeTypes.LongValKeyE, Lines = new List<string> { Pick(LootRuleText.Skills, "Light Weapons"), "159" } });
|
||||
dto.Conditions.Add(new LootEditCondition { NodeType = VTankNodeTypes.LongValKeyE, Lines = new List<string> { Pick(LootRuleText.WieldRequirements, "Base Skill"), "158" } });
|
||||
dto.Conditions.Add(new LootEditCondition { NodeType = VTankNodeTypes.LongValKeyNE, Lines = new List<string> { Pick(LootRuleText.Materials, "Steel"), "131" } });
|
||||
dto.Conditions.Add(new LootEditCondition { NodeType = VTankNodeTypes.LongValKeyE, Lines = new List<string> { Pick(LootRuleText.DamageTypes, "Electric"), "45" } });
|
||||
dto.Conditions.Add(new LootEditCondition { NodeType = VTankNodeTypes.LongValKeyE, Lines = new List<string> { "12345", "131" } }); // typed raw
|
||||
string json = JsonSerializer.Serialize(new LootEditCommand { Op = "update_rule", Index = idx, Expect = s.BuildRule(idx)!.Name, Rule = dto },
|
||||
LootEditJsonContext.Default.LootEditCommand);
|
||||
Eq(s.Apply(JsonSerializer.Deserialize(json, LootEditJsonContext.Default.LootEditCommand)!), LootEditSession.Outcome.Changed, "update_rule with named values");
|
||||
Check(s.Save(), "save named values");
|
||||
|
||||
LootEditSession re = OpenOk(s.Path);
|
||||
VTankLootRule saved = re.Profile!.Rules[idx];
|
||||
Eq(saved.Name, "Light weapons, steel", "reopened rule");
|
||||
string Lines(int c) => string.Join(",", saved.Conditions[c].DataLines);
|
||||
Eq(Lines(0), "45,159", "Light Weapons saved as 45");
|
||||
Eq(Lines(1), "2,158", "Base Skill saved as 2");
|
||||
Eq(Lines(2), "64,131", "Steel saved as 64");
|
||||
Eq(Lines(3), "64,45", "Electric saved as 64");
|
||||
Eq(Lines(4), "12345,131", "a raw number saved as typed");
|
||||
string text = File.ReadAllText(s.Path).Replace("\r\n", "\n");
|
||||
Check(text.Contains("\n45\n159\n") && text.Contains("\n64\n131\n"), "file holds plain numbers (format unchanged)");
|
||||
Check(re.Encode().AsSpan().SequenceEqual(File.ReadAllBytes(s.Path)), "reopened file re-encodes byte for byte");
|
||||
string sum = LootRuleText.Summary(saved, 400);
|
||||
Check(sum.Contains("WieldSkilltype = Light Weapons") && sum.Contains("MaterialType != Steel") && sum.Contains("WieldRequirements = Base Skill"),
|
||||
"summary: " + sum);
|
||||
}
|
||||
}
|
||||
|
|
@ -15,7 +15,7 @@ namespace RynthCore.LootSdkTests;
|
|||
// are kept as an optional bonus pass at the end.
|
||||
//
|
||||
// Run: dotnet run -c Release (exit 0 = pass, 1 = fail)
|
||||
internal static class Program
|
||||
internal static partial class Program
|
||||
{
|
||||
private static int _asserts;
|
||||
private static int _fails;
|
||||
|
|
@ -42,6 +42,7 @@ internal static class Program
|
|||
TestVTankRoundTripProgrammatic();
|
||||
TestVTankFixtureParse();
|
||||
TestMalformedInputs();
|
||||
RunLootEditTests(args);
|
||||
|
||||
Console.WriteLine($"\nGolden tests: {_asserts} assertions, {_fails} failed.");
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue