343 lines
19 KiB
C#
343 lines
19 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using RynthCore.Plugin.RynthAi;
|
|
using RynthCore.Plugin.RynthAi.LegacyUi;
|
|
using RynthCore.Plugin.RynthAi.Meta;
|
|
using RynthCore.Plugin.Shared;
|
|
using RynthCore.RynthAiHostTests.Fakes;
|
|
using RynthCore.RynthAiTests.Harness;
|
|
|
|
namespace RynthCore.RynthAiHostTests.Tests;
|
|
|
|
// 2026-11-30 hot fix: embedded nav routes in metas only loaded a nav file; they should embed the
|
|
// loaded route, so the meta still works with the .nav file deleted. The Meta panel's route picker stored a NavProfiles file
|
|
// name and the meta was saved without the route, so the meta read the file at run time; and in a meta
|
|
// with any NAVDATA block, a rule naming a route without one was blanked on load (no route at all).
|
|
// Scratch folders only: the meta's NavProfiles is redirected through MetaSaver.NavFolder.
|
|
internal static class MetaEmbedNavTests
|
|
{
|
|
private const BindingFlags Any = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public;
|
|
private static readonly DateTime T0 = new(2026, 11, 30, 12, 0, 0, DateTimeKind.Utc);
|
|
|
|
public static void Register(Runner r)
|
|
{
|
|
r.Add("meta embed nav: a route picked in the Meta panel is written inside the .af and runs with its .nav deleted", PickEmbeds);
|
|
r.Add("meta embed nav: re-picking takes the file's newer route; other edits keep the meta's copy", RepickRefreshes);
|
|
r.Add("meta embed nav: an older .af naming a NavProfiles route gets it written in on load (other blocks kept)", LoadEmbedsOldMeta);
|
|
r.Add("meta embed nav: a rule naming a route with no NAVDATA block keeps its name on load (was blanked)", ParserKeepsFileRef);
|
|
r.Add("meta embed nav: Patrol, Camp and [None] with no file, and an empty .nav, are not embedded", PseudoRoutesNotEmbedded);
|
|
r.Add("meta embed loaded: a recorded route never saved goes in as 'Route N' and runs with no file; the nav is the meta's now", EmbedLoadedUnsaved);
|
|
r.Add("meta embed loaded: a loaded .nav goes in under its own name, the loaded copy (not the file), in a DoAll too", EmbedLoadedFileRoute);
|
|
r.Add("meta embed loaded: a route a feature built (patrol, hunt) isn't embedded; the snapshot and chat say why", EmbedLoadedRefusesGenerated);
|
|
r.Add("meta embed loaded: the generated mark survives a hot-reload resume", MadeBySurvivesResume);
|
|
}
|
|
|
|
// ── helpers ──────────────────────────────────────────────────────────────
|
|
|
|
private static string Dir(string name)
|
|
{
|
|
string dir = Path.Combine(Program.TempRoot, "meta-embednav", name);
|
|
if (Directory.Exists(dir))
|
|
{
|
|
foreach (string f in Directory.GetFiles(dir, "*", SearchOption.AllDirectories)) File.SetAttributes(f, FileAttributes.Normal);
|
|
Directory.Delete(dir, recursive: true);
|
|
}
|
|
Directory.CreateDirectory(Path.Combine(dir, "NavProfiles"));
|
|
return dir;
|
|
}
|
|
|
|
private static string NavDir(string dir) => Path.Combine(dir, "NavProfiles");
|
|
|
|
private static void WriteNav(string dir, string name, int points)
|
|
{
|
|
var lines = new List<string> { "uTank2 NAV 1.2", "1", points.ToString() };
|
|
for (int i = 0; i < points; i++)
|
|
lines.AddRange(new[] { "0", (30 + i).ToString(), (40 + i).ToString(), "0", "0" });
|
|
File.WriteAllLines(Path.Combine(NavDir(dir), name + ".nav"), lines);
|
|
}
|
|
|
|
private static LegacyMetaUi MetaUi(LegacyDashboardRenderer dash) =>
|
|
(LegacyMetaUi)typeof(LegacyDashboardRenderer).GetField("_metaUi", Any)!.GetValue(dash)!;
|
|
|
|
private static MetaSaver Saver(LegacyDashboardRenderer dash) =>
|
|
(MetaSaver)typeof(LegacyDashboardRenderer).GetField("_metaSaver", Any)!.GetValue(dash)!;
|
|
|
|
/// <summary>A dashboard whose meta NavProfiles is <paramref name="dir"/>\NavProfiles, with <paramref name="path"/> loaded.</summary>
|
|
private static LegacyDashboardRenderer Dash(string dir, string path)
|
|
{
|
|
FakeHost.Reset();
|
|
DataWriteGuard.Reset();
|
|
var dash = new LegacyDashboardRenderer(FakeHost.Create());
|
|
Saver(dash).NavFolder = () => NavDir(dir);
|
|
var ui = MetaUi(dash);
|
|
Check.True(ui.LoadMacroFile(path), "loads: " + ui.LastLoadStatus);
|
|
return dash;
|
|
}
|
|
|
|
private static string RuleJson(int action, string data) =>
|
|
"{\"State\":\"Default\",\"Condition\":" + (int)MetaConditionType.Always + ",\"ConditionData\":\"\",\"Action\":" + action +
|
|
",\"ActionData\":\"" + data + "\",\"Children\":[],\"ActionChildren\":[],\"Enabled\":true,\"Name\":\"\",\"LastFiredMs\":0}";
|
|
|
|
private static string EmbedNavJson(string route) => RuleJson((int)MetaActionType.EmbeddedNavRoute, route);
|
|
|
|
private static void Send(LegacyDashboardRenderer dash, string op, int index, string rule)
|
|
{
|
|
dash.HandleMetaCommand("{\"Op\":\"" + op + "\",\"Index\":" + index + ",\"Value\":\"\",\"Path\":\"\",\"Text\":\"\",\"Rule\":" + rule + "}");
|
|
dash.DrainMetaCommands();
|
|
}
|
|
|
|
private static int Points(IDictionary<string, List<string>> navs, string name) =>
|
|
navs.TryGetValue(name, out var lines) ? NavRouteParser.LoadFromLines(lines).Points.Count : -1;
|
|
|
|
/// <summary>Runs <paramref name="settings"/>' meta for one tick the way the bot does, NavProfiles in <paramref name="navFolder"/>.</summary>
|
|
private static void RunMeta(LegacyUiSettings settings, string navFolder)
|
|
{
|
|
FakeHost.PlayerPose = (0xA9B4001Cu, 96f, 96f, 0f);
|
|
settings.IsMacroRunning = true;
|
|
settings.EnableMeta = true;
|
|
settings.CurrentState = "Default";
|
|
settings.ForceStateReset = true;
|
|
var m = new MetaManager(settings, FakeHost.Create(), new PlayerVitalsCache()) { Clock = () => T0, NavFolder = navFolder };
|
|
m.Think();
|
|
}
|
|
|
|
// ── tests ────────────────────────────────────────────────────────────────
|
|
|
|
private static void PickEmbeds()
|
|
{
|
|
string dir = Dir("pick");
|
|
string path = Path.Combine(dir, "farm.af");
|
|
File.WriteAllText(path, "STATE: {Default} ~~ {\n\tIF:\tAlways\n\t\tDO:\tChat {/say one}\n~~ }\n");
|
|
WriteNav(dir, "Hunt", 3);
|
|
var dash = Dash(dir, path);
|
|
|
|
// The Meta panel picks "Hunt" (a NavProfiles file) for rule 1's Embedded Nav Route.
|
|
Send(dash, "update_rule", 0, EmbedNavJson("Hunt"));
|
|
Check.Eq(Points(dash.Settings.EmbeddedNavs, "Hunt"), 3, "the route is in the meta in memory");
|
|
string text = File.ReadAllText(path);
|
|
Check.True(text.Contains("NAVDATA: Hunt "), "the .af carries the route: " + text);
|
|
|
|
// The file goes; the meta still has it.
|
|
File.Delete(Path.Combine(NavDir(dir), "Hunt.nav"));
|
|
var back = AfFileParser.Load(path);
|
|
Check.Eq(back.Rules[0].ActionData, "Hunt", "the rule still names it");
|
|
Check.Eq(Points(back.EmbeddedNavs, "Hunt"), 3, "the route reads back from the .af with its points");
|
|
|
|
var s = new LegacyUiSettings();
|
|
s.MetaRules = back.Rules;
|
|
foreach (var kv in back.EmbeddedNavs) s.EmbeddedNavs[kv.Key] = kv.Value;
|
|
FakeHost.Reset();
|
|
RunMeta(s, NavDir(dir));
|
|
Check.Eq(s.CurrentRoute.Points.Count, 3, "the meta runs the route with no .nav on disk");
|
|
Check.Eq(s.CurrentNavPath, "<embedded:Hunt>", "...from inside the meta");
|
|
Check.False(FakeHost.Chat.Exists(c => c.Contains("Route missing")), "no 'Route missing'");
|
|
}
|
|
|
|
private static void RepickRefreshes()
|
|
{
|
|
string dir = Dir("repick");
|
|
string path = Path.Combine(dir, "farm.af");
|
|
File.WriteAllText(path, "STATE: {Default} ~~ {\n\tIF:\tAlways\n\t\tDO:\tChat {/say one}\n~~ }\n");
|
|
WriteNav(dir, "Hunt", 3);
|
|
var dash = Dash(dir, path);
|
|
Send(dash, "update_rule", 0, EmbedNavJson("Hunt"));
|
|
Check.Eq(Points(dash.Settings.EmbeddedNavs, "Hunt"), 3, "first pick: 3 points");
|
|
|
|
// The route is re-recorded on disk. An edit that keeps the route (the rule's name here) keeps the meta's copy.
|
|
WriteNav(dir, "Hunt", 5);
|
|
Send(dash, "update_rule", 0, EmbedNavJson("Hunt").Replace("\"Name\":\"\"", "\"Name\":\"go hunt\""));
|
|
Check.Eq(Points(dash.Settings.EmbeddedNavs, "Hunt"), 3, "an edit that doesn't change the route keeps the embedded copy");
|
|
|
|
// Picking it again (another action, then the route) takes the file as it is now.
|
|
Send(dash, "update_rule", 0, RuleJson((int)MetaActionType.ChatCommand, "/say two"));
|
|
Send(dash, "update_rule", 0, EmbedNavJson("Hunt"));
|
|
Check.Eq(Points(dash.Settings.EmbeddedNavs, "Hunt"), 5, "a fresh pick copies the newer file");
|
|
Check.Eq(Points(AfFileParser.Load(path).EmbeddedNavs, "Hunt"), 5, "...and the .af has it");
|
|
|
|
// A new rule picking a route copies it too.
|
|
WriteNav(dir, "Bank Run", 2);
|
|
Send(dash, "add_rule", -1, EmbedNavJson("Bank Run"));
|
|
Check.Eq(Points(AfFileParser.Load(path).EmbeddedNavs, "Bank Run"), 2, "add_rule: a route name with a space is embedded and reads back");
|
|
}
|
|
|
|
private static void LoadEmbedsOldMeta()
|
|
{
|
|
string dir = Dir("load");
|
|
string path = Path.Combine(dir, "old.af");
|
|
// Written before the fix: rule 1 names a NavProfiles route, rule 2 a route the meta carries.
|
|
File.WriteAllText(path,
|
|
"STATE: {Default} ~~ {\n" +
|
|
"\tIF:\tAlways\n\t\tDO:\tEmbedNav Hunt {[none]}\n" +
|
|
"\tIF:\tNever\n\t\tDO:\tEmbedNav Other {[none]}\n" +
|
|
"~~ }\n" +
|
|
"NAVDATA: Other 8 ~~ {\nuTank2 NAV 1.2\n1\n1\n0\n10\n20\n0\n0\n~~ }\n");
|
|
WriteNav(dir, "Hunt", 4);
|
|
var dash = Dash(dir, path);
|
|
Check.Eq(dash.Settings.MetaRules[0].ActionData, "Hunt", "the rule keeps its route name");
|
|
Check.Eq(Points(dash.Settings.EmbeddedNavs, "Hunt"), 4, "the file route is in the meta after the load");
|
|
|
|
File.Delete(Path.Combine(NavDir(dir), "Hunt.nav"));
|
|
var back = AfFileParser.Load(path);
|
|
Check.Eq(Points(back.EmbeddedNavs, "Hunt"), 4, "the load wrote it into the .af");
|
|
Check.Eq(Points(back.EmbeddedNavs, "Other"), 1, "the route it already carried is kept");
|
|
Check.Eq(back.Rules.Count, 2, "both rules kept");
|
|
|
|
// A read-only .af isn't written by a load.
|
|
string ro = Path.Combine(dir, "readonly.af");
|
|
File.WriteAllText(ro, "STATE: {Default} ~~ {\n\tIF:\tAlways\n\t\tDO:\tEmbedNav Hunt2 {[none]}\n~~ }\n");
|
|
WriteNav(dir, "Hunt2", 2);
|
|
File.SetAttributes(ro, FileAttributes.ReadOnly);
|
|
var dash2 = Dash(dir, ro);
|
|
Check.Eq(Points(dash2.Settings.EmbeddedNavs, "Hunt2"), 2, "read-only: the route is still embedded in memory");
|
|
Check.False(File.ReadAllText(ro).Contains("NAVDATA"), "read-only: the file isn't written on load");
|
|
File.SetAttributes(ro, FileAttributes.Normal);
|
|
}
|
|
|
|
private static void ParserKeepsFileRef()
|
|
{
|
|
var m = AfFileParser.LoadFromText(
|
|
"STATE: {Default} ~~ {\n" +
|
|
"\tIF:\tAlways\n\t\tDO:\tDoAll\n\t\t\t\tEmbedNav Hunt {[none]}\n\t\t\t\tEmbedNav Empty {[none]}\n" +
|
|
"\tIF:\tNever\n\t\tDO:\tEmbedNav Other {[none]}\n" +
|
|
"~~ }\n" +
|
|
"NAVDATA: Other 8 ~~ {\nuTank2 NAV 1.2\n1\n1\n0\n10\n20\n0\n0\n~~ }\n" +
|
|
"NAVDATA: Empty 3 ~~ {\nuTank2 NAV 1.2\n1\n0\n~~ }\n");
|
|
var doAll = m.Rules[0].ActionChildren;
|
|
Check.Eq(doAll[0].ActionData, "Hunt", "a route with no block keeps its name (a NavProfiles route)");
|
|
Check.Eq(doAll[1].ActionData, "", "a route whose block is empty is still cleared");
|
|
Check.Eq(m.Rules[1].ActionData, "Other", "an embedded route is untouched");
|
|
}
|
|
|
|
// ── "Embed loaded" (the Meta panel's button) ─────────────────────────────
|
|
|
|
private static NavRouteParser Route(int points, string madeBy = "")
|
|
{
|
|
var route = new NavRouteParser { RouteType = NavRouteType.Circular, MadeBy = madeBy };
|
|
for (int i = 0; i < points; i++)
|
|
route.Points.Add(new NavPoint { Type = NavPointType.Point, NS = 40 + i, EW = 30 + i, Z = 0 });
|
|
return route;
|
|
}
|
|
|
|
/// <summary>The snapshot's loadedRoute, as the engine panel reads it.</summary>
|
|
private static (string Name, int Points, string Note) Loaded(LegacyDashboardRenderer dash)
|
|
{
|
|
using var doc = System.Text.Json.JsonDocument.Parse(dash.BuildMetaJson());
|
|
var e = doc.RootElement.GetProperty("loadedRoute");
|
|
return (e.GetProperty("name").GetString() ?? "", e.GetProperty("points").GetInt32(), e.GetProperty("note").GetString() ?? "");
|
|
}
|
|
|
|
private static string Marked(string name) => name + MetaNavEmbedding.LoadedMarker;
|
|
|
|
private static void EmbedLoadedUnsaved()
|
|
{
|
|
string dir = Dir("loaded-unsaved");
|
|
string path = Path.Combine(dir, "farm.af");
|
|
File.WriteAllText(path, "STATE: {Default} ~~ {\n\tIF:\tAlways\n\t\tDO:\tChat {/say one}\n~~ }\n");
|
|
WriteNav(dir, "Route 1", 2); // NavProfiles has a "Route 1": the new name skips it
|
|
var dash = Dash(dir, path);
|
|
dash.Settings.CurrentRoute = Route(4); // recorded in the nav editor, never saved
|
|
dash.Settings.CurrentNavPath = string.Empty;
|
|
|
|
var loaded = Loaded(dash);
|
|
Check.Eq(loaded.Note, "", "embeddable");
|
|
Check.Eq(loaded.Name, "Route 2", "a new name no file or meta route has");
|
|
Check.Eq(loaded.Points, 4, "its points");
|
|
|
|
Send(dash, "update_rule", 0, EmbedNavJson(Marked(loaded.Name)));
|
|
Check.Eq(dash.Settings.MetaRules[0].ActionData, "Route 2", "the rule keeps the plain name (the marker is gone)");
|
|
Check.Eq(Points(dash.Settings.EmbeddedNavs, "Route 2"), 4, "the route in memory went in");
|
|
Check.Eq(dash.Settings.CurrentNavPath, "<embedded:Route 2>", "the loaded route is the meta's route now");
|
|
Check.Eq(Loaded(dash).Name, "Route 2", "the button now names that route (no second copy)");
|
|
|
|
var back = AfFileParser.Load(path);
|
|
Check.True(File.ReadAllText(path).Contains("NAVDATA: Route 2 "), "written into the .af");
|
|
Check.False(File.ReadAllText(path).Contains(MetaNavEmbedding.LoadedMarker), "no marker in the file");
|
|
var s = new LegacyUiSettings();
|
|
s.MetaRules = back.Rules;
|
|
foreach (var kv in back.EmbeddedNavs) s.EmbeddedNavs[kv.Key] = kv.Value;
|
|
FakeHost.Reset();
|
|
RunMeta(s, NavDir(dir));
|
|
Check.Eq(s.CurrentRoute.Points.Count, 4, "the meta runs it; it never had a file");
|
|
}
|
|
|
|
private static void EmbedLoadedFileRoute()
|
|
{
|
|
string dir = Dir("loaded-file");
|
|
string path = Path.Combine(dir, "farm.af");
|
|
File.WriteAllText(path, "STATE: {Default} ~~ {\n\tIF:\tAlways\n\t\tDO:\tChat {/say one}\n~~ }\n");
|
|
WriteNav(dir, "Hunt", 3);
|
|
var dash = Dash(dir, path);
|
|
string navPath = Path.Combine(NavDir(dir), "Hunt.nav");
|
|
dash.Settings.CurrentNavPath = navPath;
|
|
dash.Settings.CurrentRoute = Route(6); // the loaded copy, edited since the file was written
|
|
|
|
var loaded = Loaded(dash);
|
|
Check.Eq(loaded.Name, "Hunt", "a .nav's own name");
|
|
// Inside a DoAll, as a second step.
|
|
string doAll = "{\"State\":\"Default\",\"Condition\":" + (int)MetaConditionType.Always + ",\"ConditionData\":\"\",\"Action\":" + (int)MetaActionType.All +
|
|
",\"ActionData\":\"\",\"Children\":[],\"ActionChildren\":[" + RuleJson((int)MetaActionType.ChatCommand, "/say go") + "," + EmbedNavJson(Marked("Hunt")) +
|
|
"],\"Enabled\":true,\"Name\":\"\",\"LastFiredMs\":0}";
|
|
Send(dash, "update_rule", 0, doAll);
|
|
Check.Eq(dash.Settings.MetaRules[0].ActionChildren[1].ActionData, "Hunt", "the DoAll step keeps the plain name");
|
|
Check.Eq(Points(dash.Settings.EmbeddedNavs, "Hunt"), 6, "the loaded copy went in, not the file's 3 points");
|
|
Check.Eq(dash.Settings.CurrentNavPath, navPath, "a file route stays the file's");
|
|
Check.Eq(Points(AfFileParser.Load(path).EmbeddedNavs, "Hunt"), 6, "...and the .af has it");
|
|
}
|
|
|
|
private static void EmbedLoadedRefusesGenerated()
|
|
{
|
|
string dir = Dir("loaded-generated");
|
|
string path = Path.Combine(dir, "farm.af");
|
|
File.WriteAllText(path, "STATE: {Default} ~~ {\n\tIF:\tAlways\n\t\tDO:\tChat {/say one}\n~~ }\n");
|
|
var dash = Dash(dir, path);
|
|
|
|
dash.Settings.CurrentRoute = new NavRouteParser();
|
|
dash.Settings.CurrentNavPath = string.Empty;
|
|
Check.True(Loaded(dash).Note.Contains("No nav route"), "nothing loaded: " + Loaded(dash).Note);
|
|
|
|
// /ra dunnav leaves the old file path while its route runs: the mark is what counts.
|
|
dash.Settings.CurrentNavPath = Path.Combine(NavDir(dir), "Hunt.nav");
|
|
dash.Settings.CurrentRoute = Route(9, "the dungeon patrol");
|
|
var loaded = Loaded(dash);
|
|
Check.True(loaded.Name == "" && loaded.Note.Contains("the dungeon patrol"), "the snapshot says why: " + loaded.Note);
|
|
|
|
Send(dash, "update_rule", 0, EmbedNavJson(Marked("Hunt")));
|
|
Check.Eq(dash.Settings.EmbeddedNavs.Count, 0, "nothing embedded");
|
|
Check.Eq(dash.Settings.MetaRules[0].ActionData, "Hunt", "the rule has the plain name, no marker");
|
|
Check.True(FakeHost.Chat.Exists(c => c.Contains("wasn't embedded") && c.Contains("the dungeon patrol")), "chat says why");
|
|
|
|
dash.Settings.CurrentNavPath = "<navheading>";
|
|
dash.Settings.CurrentRoute = Route(2);
|
|
Check.True(Loaded(dash).Note.Length > 0, "a <navheading> route isn't a nav route");
|
|
}
|
|
|
|
private static void MadeBySurvivesResume()
|
|
{
|
|
var s = new MetaResumeState { RouteLines = Route(3).ToLines(), RouteMadeBy = "the hunt", NavPath = "", State = "Default" };
|
|
string text = MetaResumeStore.Serialize(s);
|
|
Check.True(MetaResumeStore.TryParse(text, out MetaResumeState back), "reads back");
|
|
Check.Eq(back.RouteMadeBy, "the hunt", "the mark is kept");
|
|
}
|
|
|
|
private static void PseudoRoutesNotEmbedded()
|
|
{
|
|
string dir = Dir("pseudo");
|
|
WriteNav(dir, "Blank", 0);
|
|
var rules = new List<MetaRule>();
|
|
foreach (string r in new[] { "Patrol", "Camp", "[None]", "Blank", "NotThere", "..\\farm" })
|
|
rules.Add(new MetaRule { State = "Default", Condition = MetaConditionType.Always, Action = MetaActionType.EmbeddedNavRoute, ActionData = r });
|
|
File.WriteAllLines(Path.Combine(dir, "farm.nav"), new[] { "uTank2 NAV 1.2", "1", "1", "0", "1", "2", "0", "0" });
|
|
var navs = new Dictionary<string, List<string>>(StringComparer.OrdinalIgnoreCase);
|
|
var added = MetaNavEmbedding.EmbedMissing(rules, navs, NavDir(dir));
|
|
Check.Eq(added.Count, 0, "nothing embedded: " + string.Join(", ", added));
|
|
Check.Eq(navs.Count, 0, "no routes");
|
|
|
|
// A Patrol.nav that exists is a real route (MetaManager runs it): that one is embedded.
|
|
WriteNav(dir, "Patrol", 2);
|
|
added = MetaNavEmbedding.EmbedMissing(rules, navs, NavDir(dir));
|
|
Check.Eq(string.Join(",", added), "Patrol", "a real Patrol.nav is embedded");
|
|
}
|
|
}
|