RynthSuite/Tools/RynthCore.RynthAiHostTests/Tests/UbInventoryTests.cs

291 lines
15 KiB
C#

using System;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using RynthCore.Loot.VTank;
using RynthCore.Plugin.RynthAi;
using RynthCore.Plugin.RynthAi.LegacyUi;
using RynthCore.PluginCore;
using RynthCore.RynthAiHostTests.Fakes;
using RynthCore.RynthAiTests.Harness;
namespace RynthCore.RynthAiHostTests.Tests;
// /ub equip list|load|test|create and /ub autosalvage [force] (2026-12-01 command audit): equip went on
// to /mt equip ("Item not found: load ...") so a meta's armor or weapon swap did nothing; autosalvage
// was accepted and ignored. Both judge the carried items by a loot profile's rules.
internal static class UbInventoryTests
{
private const uint Player = 0x50000F71;
private const uint WornRobe = 0x80287101, HelmA = 0x80287102, HelmB = 0x80287103;
private const uint Ring1 = 0x80287104, Ring2 = 0x80287105, Ring3 = 0x80287106;
private const uint Sword = 0x80287107, Bow = 0x80287108, Junk = 0x80287109;
private const uint Scrap1 = 0x8028710A, Tinked = 0x8028710B, Imbued = 0x8028710C, Unknown = 0x8028710D, WornScrap = 0x8028710E;
private const BindingFlags Any = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public;
public static void Register(Runner r)
{
r.Add("ub equip: the plan puts one item in each slot (two rings, one weapon) and keeps what is worn", PlanSlots);
r.Add("ub equip: load wields the profile's carried items one at a time, never a worn one; test only says", LoadAndTest);
r.Add("ub equip: create writes the worn items as a profile, never over a file; list names it", CreateAndList);
r.Add("ub equip: '/ub equip <item name>' still wields that item (the /mt equip it was)", ByNameStillWorks);
r.Add("ub autosalvage: lists without force; force queues the Salvage items, never worn, tinkered or imbued", AutoSalvage);
r.Add("ub count profile: counts against the named profile without making it the one looting uses", CountProfileKeepsLootProfile);
r.Add("ub autosalvage: an item its rule needs identified is identified first", AutoSalvageWaitsForId);
}
private static string Folder => Path.Combine(Program.TempRoot, "UbEquip");
private sealed class World
{
public readonly RynthAiPlugin P = new();
public readonly LegacyUiSettings S = new();
public long Now = 1_000_000;
public readonly WorldObjectCache Cache;
public World(params (uint Id, string Name, int Slots, uint WornAt)[] items)
{
FakeHost.Reset();
FakeHost.SalvageCalls = true; // UseObject, and GetObjectWielderInfo answering from Wielded
FakeHost.WeaponCalls = true; // RequestId and HasAppraisalData
FakeHost.Names[Player] = "Equipper";
FakeHost.PlayerIdValue = Player;
foreach (var (id, name, slots, wornAt) in items)
{
FakeHost.Names[id] = name;
FakeHost.Containers[id] = Player;
FakeHost.ItemTypes[id] = slots != 0 ? 0x2u : 0x1u;
if (slots != 0) FakeHost.Ints[(id, 9)] = slots; // ValidLocations
if (wornAt != 0) FakeHost.Wielded[id] = wornAt;
FakeHost.Appraised.Add(id);
}
var host = FakeHost.Create(playerId: Player);
typeof(RynthPluginBase).GetMethod("Attach", Any)!.Invoke(P, new object[] { FakeHost.LastApi });
var dash = (LegacyDashboardRenderer)RuntimeHelpers.GetUninitializedObject(typeof(LegacyDashboardRenderer));
typeof(LegacyDashboardRenderer).GetField("_profileLoaded", Any)!.SetValue(dash, true);
typeof(LegacyDashboardRenderer).GetField("_settings", Any)!.SetValue(dash, S);
Set("_dashboard", dash);
Cache = new WorldObjectCache(host);
Cache.SetPlayerId(Player);
Set("_objectCache", Cache);
Set("_playerId", Player);
Set("_initialized", true);
Set("_loginComplete", true);
Set("_salvageManager", new RynthCore.Plugin.RynthAi.Loot.SalvageManager(host, S, Cache));
foreach (var it in items) Cache.OnCreateObject(it.Id);
Cache.Tick();
P.UbInvClock = () => Now;
RynthAiPlugin.UbEquipFolder = Folder;
}
public void Set(string f, object? v) => typeof(RynthAiPlugin).GetField(f, Any)!.SetValue(P, v);
public object? Get(string f) => typeof(RynthAiPlugin).GetField(f, Any)!.GetValue(P);
public bool Ub(string line) => (bool)typeof(RynthAiPlugin).GetMethod("HandleUbCommand", Any)!.Invoke(P, new object[] { line })!;
public void Tick(long advanceMs)
{
Now += advanceMs;
typeof(RynthAiPlugin).GetMethod("TickUbInventoryJobs", Any)!.Invoke(P, null);
}
public bool ChatHas(string text) => FakeHost.Chat.Any(c => c.Contains(text, StringComparison.OrdinalIgnoreCase));
public int SalvageQueued => ((RynthCore.Plugin.RynthAi.Loot.SalvageManager)Get("_salvageManager")!).GetStateSnapshot().QueueCount;
}
private static VTankLootRule Rule(string name, VTankLootAction action, params VTankLootCondition[] conds)
{
var r = new VTankLootRule { Name = name, Action = action, CustomExpression = string.Empty };
r.Conditions.AddRange(conds);
return r;
}
private static VTankLootCondition NameIs(string pattern) => new(VTankNodeTypes.StringValueMatch, "0", new[] { pattern, "1" });
private static string WriteProfile(string fileName, params VTankLootRule[] rules)
{
Directory.CreateDirectory(Folder);
var p = new VTankLootProfile();
p.Rules.AddRange(rules);
string path = Path.Combine(Folder, fileName);
VTankLootWriter.Save(p, path);
return path;
}
private static void PlanSlots()
{
const int Head = 0x1, Fingers = 0x000C0000;
var plan = UbEquipPlan.Plan(new[]
{
new UbEquipPlan.Item(1, "Helm A", Head, false, 0, 0),
new UbEquipPlan.Item(2, "Helm B", Head, false, 0, 1),
new UbEquipPlan.Item(3, "Ring 1", Fingers, false, 0, 2),
new UbEquipPlan.Item(4, "Ring 2", Fingers, false, 0, 3),
new UbEquipPlan.Item(5, "Ring 3", Fingers, false, 0, 4),
new UbEquipPlan.Item(6, "Sword", UbEquipPlan.MeleeWeapon, false, 0, 5),
new UbEquipPlan.Item(7, "Bow", UbEquipPlan.MissileWeapon, false, 0, 6),
new UbEquipPlan.Item(8, "Robe", 0x2, true, 0x2, 7),
new UbEquipPlan.Item(9, "Coat", 0x2, false, 0, 8),
new UbEquipPlan.Item(10, "Pea", 0, false, 0, 9),
});
Check.Eq(string.Join(",", plan.Equip.Select(i => i.Name)), "Helm A,Ring 1,Ring 2,Sword", "first helm, two rings, one weapon");
Check.Eq(string.Join(",", plan.AlreadyWorn.Select(i => i.Name)), "Robe", "the worn robe stays");
Check.Eq(string.Join(",", plan.Skipped.Select(s => s.Item.Name)), "Helm B,Ring 3,Bow,Coat,Pea", "the rest is left out");
// A worn ring keeps its finger: one more ring fits, not two.
var rings = UbEquipPlan.Plan(new[]
{
new UbEquipPlan.Item(1, "Worn Ring", Fingers, true, 0x00040000, 0),
new UbEquipPlan.Item(2, "Ring 2", Fingers, false, 0, 1),
new UbEquipPlan.Item(3, "Ring 3", Fingers, false, 0, 2),
});
Check.Eq(rings.Equip.Count, 1, "one free finger");
// Worn with its slot unknown: kept, and it takes no slot by guess.
var unknown = UbEquipPlan.Plan(new[]
{
new UbEquipPlan.Item(1, "Worn Thing", Head, true, 0, 0),
new UbEquipPlan.Item(2, "Helm", Head, false, 0, 1),
});
Check.True(unknown.AlreadyWorn.Count == 1 && unknown.Equip.Count == 1, "worn with no known slot: kept, nothing blocked");
}
private static World EquipWorld() => new(
(WornRobe, "Faran Robe", 0x2, 0x2),
(HelmA, "Helm of A", 0x1, 0),
(HelmB, "Helm of B", 0x1, 0),
(Ring1, "Ring", 0x000C0000, 0),
(Ring2, "Ring", 0x000C0000, 0),
(Ring3, "Ring", 0x000C0000, 0),
(Sword, "Sword", UbEquipPlan.MeleeWeapon, 0),
(Bow, "Bow", UbEquipPlan.MissileWeapon, 0),
(Junk, "Junk Hat", 0x1, 0));
private static void LoadAndTest()
{
WriteProfile("melee.utl",
Rule("Robe", VTankLootAction.Keep, NameIs("^Faran Robe$")),
Rule("Helm A", VTankLootAction.Keep, NameIs("^Helm of A$")),
Rule("Helms", VTankLootAction.Keep, NameIs("^Helm of")),
Rule("Rings", VTankLootAction.Keep, NameIs("^Ring$")),
Rule("Sword", VTankLootAction.Keep, NameIs("^Sword$")),
Rule("Bow", VTankLootAction.Keep, NameIs("^Bow$")),
Rule("Junk", VTankLootAction.Sell, NameIs("^Junk")));
var w = EquipWorld();
Check.True(w.Ub("/ub equip test melee"), "/ub equip test is handled");
Check.Eq(FakeHost.Uses.Count, 0, "test wields nothing");
Check.True(w.ChatHas("would equip") && w.ChatHas("Helm of A") && w.ChatHas("already worn: Faran Robe"), "test says what would go on");
Check.True(w.Ub("/ub equip load melee.utl"), "/ub equip load is handled (a name with .utl)");
Check.Eq(FakeHost.Uses.Count, 1, "the first wield goes at once");
for (int i = 0; i < 10; i++) w.Tick(RynthAiPlugin.UbEquipSpacingMs);
var used = FakeHost.Uses.ToList();
Check.Eq(used.Count, 4, "helm, two rings, the sword: one item a slot");
Check.True(used.Contains(HelmA) && !used.Contains(HelmB), "the helm of the first rule that takes one");
Check.Eq(used.Count(u => u is Ring1 or Ring2 or Ring3), 2, "two rings of three");
Check.True(used.Contains(Sword) && !used.Contains(Bow), "one weapon: the earlier rule's");
Check.False(used.Contains(WornRobe), "the worn robe is never used (that would take it off)");
Check.False(used.Contains(Junk), "a Sell rule's item isn't equipped");
// Nothing went on in the fake: the check after the last wield says so.
w.Tick(RynthAiPlugin.UbEquipSettleMs);
Check.True(w.ChatHas("equipped 0 of 4"), "after the wields: how many went on");
Check.True(w.Ub("/ub equip load nosuchprofile"), "a missing profile is handled");
Check.True(w.ChatHas("no profile 'nosuchprofile'"), "and said");
Check.True(w.Ub("/ub equip load"), "no name: handled");
Check.True(w.ChatHas("Usage: /ub equip"), "with the usage");
}
private static void CreateAndList()
{
var w = EquipWorld();
FakeHost.Wielded[HelmA] = 0x1;
Check.True(w.Ub("/ub equip create mysuit"), "/ub equip create is handled");
string path = Path.Combine(Folder, "mysuit.utl");
Check.True(File.Exists(path), "mysuit.utl written in the equipment folder");
VTankLootProfile p = VTankLootParser.Load(path);
Check.Eq(p.Rules.Count, 2, "a rule for each worn item");
Check.True(p.Rules.All(r => r.Action == VTankLootAction.Keep), "Keep rules");
// The profile made from the worn items takes them (and nothing else).
FakeHost.Uses.Clear();
FakeHost.Chat.Clear();
Check.True(w.Ub("/ub equip test mysuit"), "test the new profile");
Check.True(w.ChatHas("nothing to equip") && w.ChatHas("Faran Robe") && w.ChatHas("Helm of A"), "both already worn, nothing else");
DateTime before = File.GetLastWriteTimeUtc(path);
Check.True(w.Ub("/ub equip create mysuit"), "again");
Check.True(w.ChatHas("already exists"), "never over an existing file");
Check.Eq(File.GetLastWriteTimeUtc(path), before, "the file is untouched");
FakeHost.Chat.Clear();
Check.True(w.Ub("/ub equip list"), "/ub equip list");
Check.True(w.ChatHas("mysuit.utl"), "lists it");
}
private static void ByNameStillWorks()
{
var w = EquipWorld();
Check.True(w.Ub("/ub equip Helm of B"), "/ub equip <item name> is handled");
Check.True(FakeHost.Uses.Count == 1 && FakeHost.Uses[0] == HelmB, "it wields that item, as /mt equip did");
}
private static World SalvageWorld() => new(
(Scrap1, "Iron Scrap Sword", 0, 0),
(Tinked, "Iron Tinked Sword", 0, 0),
(Imbued, "Iron Imbued Sword", 0, 0),
(WornScrap, "Iron Worn Sword", UbEquipPlan.MeleeWeapon, UbEquipPlan.MeleeWeapon),
(Junk, "Junk Hat", 0x1, 0));
private static void AutoSalvage()
{
string path = WriteProfile("salv.utl",
Rule("Iron", VTankLootAction.Salvage, NameIs("^Iron")),
Rule("Junk", VTankLootAction.Sell, NameIs("^Junk")));
var w = SalvageWorld();
FakeHost.Ints[(Tinked, 171)] = 2; // NumTimesTinkered
FakeHost.Ints[(Imbued, 179)] = 0x1; // ImbuedEffect
Check.True(w.Ub("/ub autosalvage"), "no loot profile in use: handled");
Check.True(w.ChatHas("no loot profile in use"), "and said");
w.S.CurrentLootPath = path;
FakeHost.Chat.Clear();
Check.True(w.Ub("/ub autosalvage"), "/ub autosalvage");
Check.True(w.ChatHas("1 item(s) would be salvaged: Iron Scrap Sword"), "lists the one item it would salvage");
Check.True(w.ChatHas("Iron Tinked Sword (tinkered)") && w.ChatHas("Iron Imbued Sword (imbued)"), "says why the others are left");
Check.Eq(w.SalvageQueued, 0, "without force nothing is salvaged");
Check.True(w.Ub("/ub autosalvage force"), "/ub autosalvage force");
Check.Eq(w.SalvageQueued, 1, "force queues the one item");
Check.True(w.ChatHas("salvaging 1 item(s): Iron Scrap Sword"), "and says so");
Check.True(w.Ub("/ub autosalvage banana"), "a wrong word is handled");
Check.True(w.ChatHas("Usage: /ub autosalvage"), "with the usage");
}
private static void AutoSalvageWaitsForId()
{
string path = WriteProfile("salvid.utl", Rule("Iron", VTankLootAction.Salvage, NameIs("^Iron")));
var w = new World((Unknown, "Iron Unknown Sword", 0, 0));
FakeHost.Appraised.Remove(Unknown);
w.S.CurrentLootPath = path;
Check.True(w.Ub("/ub autosalvage force"), "handled");
Check.True(w.ChatHas("identifying 1 item(s) first"), "it identifies the item first");
Check.Eq(w.SalvageQueued, 0, "nothing queued yet");
w.Tick(RynthAiPlugin.UbIdSpacingMs);
Check.True(FakeHost.IdRequests.Contains(Unknown), "the identify is sent");
FakeHost.Appraised.Add(Unknown);
w.Tick(100);
Check.Eq(w.SalvageQueued, 1, "once identified, it is salvaged");
}
// 2026-12-01 command audit: /ub count profile <name> loaded the profile through TryLoadLootProfile, which
// also made it the profile looting uses, so a meta counting against another profile switched its looting.
private static void CountProfileKeepsLootProfile()
{
string inUse = WriteProfile("inuse.utl", Rule("Keep all", VTankLootAction.Keep, NameIs(".")));
string other = WriteProfile("count.utl", Rule("Iron", VTankLootAction.Keep, NameIs("^Iron")));
var w = SalvageWorld();
w.S.CurrentLootPath = inUse;
Check.True(w.Ub("/ub count profile " + other), "/ub count profile");
Check.True(w.ChatHas("matching count.utl"), "it counts against that profile");
Check.Eq(w.S.CurrentLootPath, inUse, "the loot profile in use is unchanged");
}
}