using System; using System.Collections.Generic; using System.Globalization; using System.IO; using RynthCore.Plugin.RynthAi.LegacyUi; namespace RynthCore.Plugin.RynthAi.Meta; /// /// Parses metaf .af files into RynthAi MetaRule lists. /// Embedded NAV routes are returned in /// and stay in memory — they are never written to the NavProfiles folder. /// internal static class AfFileParser { // .af keyword ↔ MetaConditionType/MetaActionType now lives in MetaSchema // (§3.3 single source of truth; replaces the read/write map pair that // desynced and caused the §2.6 SecsOnSpell _LE→_GE loss). // Per-parse warning sink (ThreadStatic: parsing is synchronous, but two // threads could load different metas at once). Points at the active // LoadedMeta.Warnings for the duration of ParseLines. [ThreadStatic] private static List? _warn; // ── Public API ────────────────────────────────────────────────────────── public static LoadedMeta Load(string filePath) { var result = new LoadedMeta(); if (!File.Exists(filePath)) return result; string[] lines = File.ReadAllLines(filePath); ParseLines(lines, result); return result; } public static LoadedMeta LoadFromText(string text) { var result = new LoadedMeta(); // Normalize line endings then split string[] lines = text.Replace("\r\n", "\n").Replace("\r", "\n").Split('\n'); ParseLines(lines, result); return result; } private static void ParseLines(string[] lines, LoadedMeta result) { _warn = result.Warnings; try { int idx = 0; while (idx < lines.Length) { string line = lines[idx]; string trimmed = line.TrimStart(); // Skip comments and blank lines if (string.IsNullOrWhiteSpace(trimmed) || trimmed.StartsWith("~~")) { idx++; continue; } if (trimmed.StartsWith("STATE:")) { result.DeclaredStates.Add(ExtractBraceContent(lines[idx], out _)); ParseState(lines, ref idx, result.Rules); } else if (trimmed.StartsWith("NAVDATA:")) { ParseNavData(lines, ref idx, result.EmbeddedNavs); } else if (trimmed.StartsWith("NAV:")) { ParseNavSection(lines, ref idx, result.EmbeddedNavs); } else { idx++; } } // Drop empty routes (header only, 0 points) and clear dead EmbedNav refs PruneEmbeddedNavs(result.Rules, result.EmbeddedNavs); } catch (Exception ex) { // Malformed meta yields whatever parsed so far rather than throwing // into the caller; record why so the loader can show it. result.Warnings.Add($"parse aborted: {ex.GetType().Name}: {ex.Message}"); } finally { _warn = null; } } // ── State parsing ─────────────────────────────────────────────────────── private static void ParseState(string[] lines, ref int idx, List rules) { string stateName = ExtractBraceContent(lines[idx], out _); idx++; // consume STATE: line bool pendingDisabled = false; // set by a "~~ @disabled" marker line string pendingName = ""; // set by a "~~ @name ..." marker line while (idx < lines.Length) { string trimmed = lines[idx].TrimStart(); if (trimmed == "~~ }" || trimmed.StartsWith("STATE:") || trimmed.StartsWith("NAV:")) { if (trimmed == "~~ }") idx++; return; } if (string.IsNullOrWhiteSpace(trimmed) || trimmed.StartsWith("~~")) { if (trimmed.Equals("~~ @disabled", StringComparison.OrdinalIgnoreCase)) pendingDisabled = true; else if (TryRuleName(trimmed, out string name)) pendingName = name; idx++; continue; } // Look for IF: line if (trimmed.StartsWith("IF:")) { var rule = new MetaRule { State = stateName, Enabled = !pendingDisabled, Name = pendingName }; pendingDisabled = false; pendingName = ""; int ifIndent = CountLeadingTabs(lines[idx]); ParseIfBlock(lines, ref idx, rule, ifIndent); rules.Add(rule); } else { idx++; } } } /// "~~ @name Rebuff when low" (AfFileWriter's rule name line): the name, trimmed. /// "~~ @names..." or an empty "~~ @name" is just a comment. internal static bool TryRuleName(string trimmedLine, out string name) { name = ""; const string marker = "~~ @name"; if (!trimmedLine.StartsWith(marker, StringComparison.OrdinalIgnoreCase)) return false; string rest = trimmedLine.Substring(marker.Length); if (rest.Length == 0 || !char.IsWhiteSpace(rest[0])) return false; name = rest.Trim(); return name.Length > 0; } private static void ParseIfBlock(string[] lines, ref int idx, MetaRule rule, int ifIndent) { // Parse condition from IF: line string ifLine = lines[idx].TrimStart(); string afterIf = StripPrefix(ifLine, "IF:"); ParseConditionLine(afterIf, rule); idx++; // If compound condition, parse children until DO: or end of state MetaRule? container = ChildContainer(rule); if (container != null) { container.Children ??= new List(); while (idx < lines.Length) { string trimmed = lines[idx].TrimStart(); if (string.IsNullOrWhiteSpace(trimmed) || trimmed.StartsWith("~~")) { if (trimmed == "~~ }") break; idx++; continue; } int lineIndent = CountLeadingTabs(lines[idx]); // DO: at expected indent = end of condition children if (trimmed.StartsWith("DO:") && lineIndent <= ifIndent + 1) break; // IF: at same indent = new rule (shouldn't happen in children) if (trimmed.StartsWith("IF:") && lineIndent <= ifIndent) break; // STATE: or NAV: = end of state if (trimmed.StartsWith("STATE:") || trimmed.StartsWith("NAV:")) break; // This line is a child condition var child = new MetaRule { State = rule.State }; ParseConditionLine(trimmed, child); idx++; // If child is compound, recursively parse its children MetaRule? childBox = ChildContainer(child); if (childBox != null) { childBox.Children ??= new List(); int childIndent = lineIndent; while (idx < lines.Length) { string ct = lines[idx].TrimStart(); if (string.IsNullOrWhiteSpace(ct) || ct.StartsWith("~~")) { if (ct == "~~ }") break; idx++; continue; } int ci = CountLeadingTabs(lines[idx]); if (ci <= childIndent) break; if (ct.StartsWith("DO:") || ct.StartsWith("IF:") || ct.StartsWith("STATE:") || ct.StartsWith("NAV:")) break; var grandchild = new MetaRule { State = rule.State }; ParseConditionLine(ct, grandchild); idx++; // Support one more level of nesting MetaRule? grandBox = ChildContainer(grandchild); if (grandBox != null) { grandBox.Children ??= new List(); ParseNestedConditionChildren(lines, ref idx, grandBox, ci); } childBox.Children.Add(grandchild); } } container.Children.Add(child); } } // Now parse DO: line while (idx < lines.Length) { string trimmed = lines[idx].TrimStart(); if (string.IsNullOrWhiteSpace(trimmed) || trimmed.StartsWith("~~")) { if (trimmed == "~~ }") return; idx++; continue; } if (trimmed.StartsWith("DO:")) { string afterDo = StripPrefix(trimmed, "DO:"); ParseActionLine(afterDo, rule); int doIndent = CountLeadingTabs(lines[idx]); idx++; // If DoAll, parse children if (rule.Action == MetaActionType.All) { rule.ActionChildren = new List(); while (idx < lines.Length) { string ct = lines[idx].TrimStart(); if (string.IsNullOrWhiteSpace(ct) || ct.StartsWith("~~")) { if (ct == "~~ }") break; idx++; continue; } int ci = CountLeadingTabs(lines[idx]); if (ci <= doIndent) break; if (ct.StartsWith("IF:") || ct.StartsWith("STATE:") || ct.StartsWith("NAV:")) break; var child = new MetaRule { State = rule.State }; ParseActionLine(ct, child); idx++; // Nested DoAll if (child.Action == MetaActionType.All) { child.ActionChildren = new List(); int childDoIndent = ci; while (idx < lines.Length) { string ct2 = lines[idx].TrimStart(); if (string.IsNullOrWhiteSpace(ct2) || ct2.StartsWith("~~")) { if (ct2 == "~~ }") break; idx++; continue; } int ci2 = CountLeadingTabs(lines[idx]); if (ci2 <= childDoIndent) break; if (ct2.StartsWith("IF:") || ct2.StartsWith("STATE:") || ct2.StartsWith("NAV:")) break; var grandchild = new MetaRule { State = rule.State }; ParseActionLine(ct2, grandchild); idx++; child.ActionChildren.Add(grandchild); } } rule.ActionChildren.Add(child); } } return; } // Not a DO: line — shouldn't happen, skip if (trimmed.StartsWith("IF:") || trimmed.StartsWith("STATE:") || trimmed.StartsWith("NAV:")) return; idx++; } } private static void ParseNestedConditionChildren(string[] lines, ref int idx, MetaRule parent, int parentIndent) { while (idx < lines.Length) { string ct = lines[idx].TrimStart(); if (string.IsNullOrWhiteSpace(ct) || ct.StartsWith("~~")) { if (ct == "~~ }") break; idx++; continue; } int ci = CountLeadingTabs(lines[idx]); if (ci <= parentIndent) break; if (ct.StartsWith("DO:") || ct.StartsWith("IF:") || ct.StartsWith("STATE:") || ct.StartsWith("NAV:")) break; var child = new MetaRule { State = parent.State }; ParseConditionLine(ct, child); idx++; MetaRule? childBox = ChildContainer(child); if (childBox != null) { childBox.Children ??= new List(); ParseNestedConditionChildren(lines, ref idx, childBox, ci); } parent.Children.Add(child); } } /// /// The rule that the indented condition lines under belong to. /// All/Any take them, and so does a bare Not (RynthAi's old two-line form). A metaf /// Not carries its operand on its own line ("Not NoMobsInDist 5"), so lines follow /// only when that operand is itself All/Any ("Not All" + children) — they are its. /// Null when nothing may follow. /// private static MetaRule? ChildContainer(MetaRule rule) { while (rule.Condition == MetaConditionType.Not && rule.Children is { Count: 1 }) rule = rule.Children[0]; return rule.Condition is MetaConditionType.All or MetaConditionType.Any or MetaConditionType.Not ? rule : null; } // ── Line parsers ──────────────────────────────────────────────────────── private static void ParseConditionLine(string text, MetaRule rule) { text = text.Trim(); // Remove trailing ~~ comments int commentIdx = text.IndexOf("~~", StringComparison.Ordinal); if (commentIdx > 0) { // Make sure we're not inside a brace group int braceDepth = 0; for (int i = 0; i < commentIdx; i++) { if (text[i] == '{') braceDepth++; else if (text[i] == '}') braceDepth--; } if (braceDepth == 0) text = text.Substring(0, commentIdx).TrimEnd(); } // Extract keyword (first word before space or brace) int spaceIdx = text.IndexOf(' '); int braceIdx = text.IndexOf('{'); int keyEnd = text.Length; if (spaceIdx > 0 && (braceIdx < 0 || spaceIdx < braceIdx)) keyEnd = spaceIdx; else if (braceIdx > 0) keyEnd = braceIdx; string keyword = text.Substring(0, keyEnd).Trim(); string rest = keyEnd < text.Length ? text.Substring(keyEnd).Trim() : ""; if (!MetaSchema.TryCondition(keyword, out MetaConditionType condType)) { rule.Condition = MetaConditionType.Never; _warn?.Add($"unknown condition keyword '{keyword}' in state '{rule.State}' — rule will never fire"); return; } rule.Condition = condType; // metaf writes Not's operand on the same line: "Not NoMobsInDist 5". This used // to be dropped — an empty Not never fires. (A bare "Not" with its operand on // the next line is RynthAi's old form; ChildContainer still accepts it.) if (condType == MetaConditionType.Not) { if (rest.Length > 0) { var operand = new MetaRule { State = rule.State }; ParseConditionLine(rest, operand); rule.Children = new List { operand }; } return; } // Parse condition data based on type switch (condType) { case MetaConditionType.ChatMessage: case MetaConditionType.Expression: rule.ConditionData = ExtractBraceContent(rest, out _); break; case MetaConditionType.ChatMessageCapture: { // ChatCapture {pattern} {colour id list, e.g. 2;4} string pattern = ExtractBraceContent(rest, out int endPos); rule.ConditionData = pattern; string r1 = endPos > 0 && endPos < rest.Length ? rest.Substring(endPos).Trim() : ""; rule.ChatColors = r1.StartsWith('{') ? ExtractBraceContent(r1, out _).Trim() : ""; break; } case MetaConditionType.InventoryItemCount_LE: case MetaConditionType.InventoryItemCount_GE: { // Format: ItemCountLE {} SplitBareAndBrace(rest, out string countStr, out string nameStr); rule.ConditionData = $"{nameStr},{countStr}"; break; } case MetaConditionType.MonsterNameCountWithinDistance: { // Format: MobsInDist_Name {} SplitBareAndBrace(rest, out string bareArgs, out string name); string[] bp = bareArgs.Split(' ', StringSplitOptions.RemoveEmptyEntries); string count = bp.Length > 0 ? bp[0] : "0"; string dist = bp.Length > 1 ? bp[1] : "0"; rule.ConditionData = $"{name},{dist},{count}"; break; } case MetaConditionType.MonsterPriorityCountWithinDistance: { // Format: MobsInDist_Priority string[] bp = rest.Split(new[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries); string count = bp.Length > 0 ? bp[0] : "0"; string dist = bp.Length > 1 ? bp[1] : "0"; rule.ConditionData = $"{count},{dist}"; break; } case MetaConditionType.TimeLeftOnSpell_GE: case MetaConditionType.TimeLeftOnSpell_LE: { // Format: SecsOnSpell{GE,LE} string[] bp = rest.Split(new[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries); string spellId = bp.Length > 0 ? bp[0] : "0"; string secs = bp.Length > 1 ? bp[1] : "0"; rule.ConditionData = $"{spellId},{secs}"; break; } case MetaConditionType.PackSlots_LE: case MetaConditionType.SecondsInState_GE: case MetaConditionType.SecondsInStateP_GE: case MetaConditionType.BurdenPercentage_GE: case MetaConditionType.DistAnyRoutePT_GE: case MetaConditionType.NoMonstersWithinDistance: case MetaConditionType.Landblock_EQ: case MetaConditionType.Landcell_EQ: // §2.6: typed vitals now have real .af keywords (bare numeric data, // same as MainSlotsLE) so they round-trip instead of being written // as Expr{…} and reloaded as a generic Expression. case MetaConditionType.MainHealthLE: case MetaConditionType.MainHealthPHE: case MetaConditionType.MainManaLE: case MetaConditionType.MainManaPHE: case MetaConditionType.MainStamLE: case MetaConditionType.VitaePHE: // RynthAi only: NoKillsInMins 5 (minutes, decimals allowed). case MetaConditionType.NoKillsWithinMinutes: // These use bare numbers (no braces): e.g. MainSlotsLE 4, BlockE F6820033 rule.ConditionData = ExtractBareOrBrace(rest); break; // No-data conditions default: break; } } private static void ParseActionLine(string text, MetaRule rule) { text = text.Trim(); // Remove trailing ~~ comments (same brace-aware logic) int commentIdx = text.IndexOf("~~", StringComparison.Ordinal); if (commentIdx > 0) { int braceDepth = 0; for (int i = 0; i < commentIdx; i++) { if (text[i] == '{') braceDepth++; else if (text[i] == '}') braceDepth--; } if (braceDepth == 0) text = text.Substring(0, commentIdx).TrimEnd(); } // Extract keyword int spaceIdx = text.IndexOf(' '); int braceIdx = text.IndexOf('{'); int keyEnd = text.Length; if (spaceIdx > 0 && (braceIdx < 0 || spaceIdx < braceIdx)) keyEnd = spaceIdx; else if (braceIdx > 0) keyEnd = braceIdx; string keyword = text.Substring(0, keyEnd).Trim(); string rest = keyEnd < text.Length ? text.Substring(keyEnd).Trim() : ""; if (!MetaSchema.TryAction(keyword, out MetaActionType actionType)) { rule.Action = MetaActionType.None; _warn?.Add($"unknown action keyword '{keyword}' in state '{rule.State}'"); return; } rule.Action = actionType; switch (actionType) { case MetaActionType.CallMetaState: { // CallState {ToState} {ReturnState}: the second brace is what gets // pushed on the call stack (it was dropped, so Return went back to the caller). rule.ActionData = ExtractBraceContent(rest, out int e1); string r1 = e1 < rest.Length ? rest.Substring(e1).Trim() : ""; rule.CallReturnState = r1.StartsWith('{') ? ExtractBraceContent(r1, out _) : ""; break; } case MetaActionType.SetMetaState: case MetaActionType.ChatCommand: case MetaActionType.ExpressionAction: case MetaActionType.ChatExpression: case MetaActionType.DestroyView: rule.ActionData = ExtractBraceContent(rest, out _); break; case MetaActionType.EmbeddedNavRoute: { // EmbedNav navname {type/filename} // (metaf: EmbedNav tag {name} {transform}; the tag names the NAV block.) // Stray brackets go ("None]" from VTank's "[None]"), the same as on the NAV keys. int braceStart = rest.IndexOf('{'); string navName = braceStart >= 0 ? rest.Substring(0, braceStart) : rest; rule.ActionData = EmbedNavName.Clean(navName); break; } case MetaActionType.SetWatchdog: { // Format: SetWatchdog {} SplitBareAndBrace(rest, out string bareArgs, out string state); string[] bp = bareArgs.Split(' ', StringSplitOptions.RemoveEmptyEntries); string range = bp.Length > 0 ? bp[0] : "10"; string time = bp.Length > 1 ? bp[1] : "60"; rule.ActionData = $"{state};{range};{time}"; break; } case MetaActionType.SetRAOption: { string option = ExtractBraceContent(rest, out int e1); string r1 = e1 < rest.Length ? rest.Substring(e1).Trim() : ""; string value = ExtractBraceContent(r1, out _); rule.ActionData = $"{option};{value}"; break; } case MetaActionType.GetRAOption: { // metaf: GetOpt {Option} {Variable} (GetOpt {OpenDoors} {doors}). // ActionData stays "variable;option". string option = ExtractBraceContent(rest, out int e1); string r1 = e1 < rest.Length ? rest.Substring(e1).Trim() : ""; string variable = ExtractBraceContent(r1, out _); rule.ActionData = $"{variable};{option}"; break; } case MetaActionType.CreateView: { string name = ExtractBraceContent(rest, out int e1); string r1 = e1 < rest.Length ? rest.Substring(e1).Trim() : ""; string xml = ExtractBraceContent(r1, out _); rule.ActionData = $"{name};{xml}"; break; } // No data default: break; } } // ── NAV section parsing ───────────────────────────────────────────────── // Reads a `NAVDATA: ~~ {` block (written by AfFileWriter): // the canonical uTank2 nav lines stored verbatim. Count-prefixed so a waypoint // string containing "~~ }" cannot terminate the block early. This is the exact // representation MetFileParser produces and the nav engine consumes — so a route // round-trips losslessly regardless of waypoint type (Portal/Vendor/NPC included). private static void ParseNavData(string[] lines, ref int idx, Dictionary> navRoutes) { string body = StripPrefix(lines[idx].TrimStart(), "NAVDATA:"); int comment = body.IndexOf("~~", StringComparison.Ordinal); if (comment >= 0) body = body.Substring(0, comment).TrimEnd(); // Trailing token is the line count; everything before it is the route name // (names are space-sanitized on write; parse from the right to be safe). int sp = body.LastIndexOf(' '); string name = sp > 0 ? body.Substring(0, sp).Trim() : body.Trim(); int count = 0; if (sp > 0) int.TryParse(body.Substring(sp + 1).Trim(), NumberStyles.Integer, CultureInfo.InvariantCulture, out count); idx++; // consume header var navLines = new List(count); for (int k = 0; k < count && idx < lines.Length; k++) navLines.Add(lines[idx++]); if (idx < lines.Length && lines[idx].Trim() == "~~ }") idx++; // consume closer name = EmbedNavName.Clean(name); if (!string.IsNullOrEmpty(name) && navLines.Count >= 3) navRoutes[name] = navLines; } private static void ParseNavSection(string[] lines, ref int idx, Dictionary> navRoutes) { // NAV: navname type ~~ { string navLine = lines[idx].TrimStart(); string afterNav = StripPrefix(navLine, "NAV:"); // Remove trailing ~~ { comment int commentIdx = afterNav.IndexOf("~~", StringComparison.Ordinal); if (commentIdx > 0) afterNav = afterNav.Substring(0, commentIdx).TrimEnd(); // Parse "navname type" string[] parts = afterNav.Trim().Split(' ', StringSplitOptions.RemoveEmptyEntries); string navName = EmbedNavName.Clean(parts.Length > 0 ? parts[0] : ""); string navType = parts.Length > 1 ? parts[1] : "circular"; idx++; // Read nav points until ~~ } var navContent = new List(); navContent.Add("uTank2 NAV 1.2"); int routeTypeNum = navType.ToLowerInvariant() switch { "circular" => 1, "linear" => 2, "follow" => 3, "once" => 4, _ => 1 }; navContent.Add(routeTypeNum.ToString()); var pointLines = new List(); while (idx < lines.Length) { string trimmed = lines[idx].TrimStart(); if (trimmed == "~~ }" || trimmed.StartsWith("STATE:") || trimmed.StartsWith("NAV:")) { if (trimmed == "~~ }") idx++; break; } if (!string.IsNullOrWhiteSpace(trimmed) && !trimmed.StartsWith("~~")) pointLines.Add(trimmed); idx++; } // Convert .af nav points to .nav format var navFileLines = new List(); foreach (string pointLine in pointLines) { string[] tokens = SplitNavTokens(pointLine); if (tokens.Length == 0) continue; string nodeType = tokens[0]; switch (nodeType) { case "pnt": // pnt EW NS Z (VTank convention: X=EW, Y=NS) if (tokens.Length >= 4) { navFileLines.Add("0"); // type 0 = Point navFileLines.Add(tokens[1]); // EW navFileLines.Add(tokens[2]); // NS navFileLines.Add(tokens[3]); // Z navFileLines.Add("0"); } break; case "rcl": // rcl EW NS Z {RecallName} if (tokens.Length >= 4) { navFileLines.Add("2"); // type 2 = Recall navFileLines.Add(tokens[1]); // EW navFileLines.Add(tokens[2]); // NS navFileLines.Add(tokens[3]); // Z navFileLines.Add("0"); // Recall spell ID — we'll use 0 as placeholder string recallName = tokens.Length >= 5 ? tokens[4] : ""; navFileLines.Add(RecallNameToSpellId(recallName).ToString()); } break; case "pau": // metaf: pau X Y Z PauseMs (old RynthAi form: pau seconds) if (tokens.Length >= 5) { // The metaf form was read as "pau seconds": the X coordinate became // the pause (e.g. -62144 ms) and the point moved to 0,0,0. navFileLines.Add("3"); // type 3 = Pause navFileLines.Add(tokens[1]); // EW navFileLines.Add(tokens[2]); // NS navFileLines.Add(tokens[3]); // Z navFileLines.Add("0"); double.TryParse(tokens[4], NumberStyles.Any, CultureInfo.InvariantCulture, out double pauseMs); navFileLines.Add(((int)pauseMs).ToString(CultureInfo.InvariantCulture)); } else if (tokens.Length >= 2) { navFileLines.Add("3"); // type 3 = Pause navFileLines.Add("0"); navFileLines.Add("0"); navFileLines.Add("0"); navFileLines.Add("0"); double.TryParse(tokens[1], NumberStyles.Any, CultureInfo.InvariantCulture, out double pauseSec); navFileLines.Add(((int)(pauseSec * 1000)).ToString()); } break; case "cht": // cht EW NS Z {command} if (tokens.Length >= 5) { navFileLines.Add("4"); // type 4 = Chat navFileLines.Add(tokens[1]); // EW navFileLines.Add(tokens[2]); // NS navFileLines.Add(tokens[3]); // Z navFileLines.Add("0"); navFileLines.Add(tokens[4]); // chat command } break; // The trailer for type 6 is exactly the 6 lines NavRouteParser reads: name, // class, tie, targetEW, targetNS, targetZ. ptl wrote 5 extra "0" lines and // vnd/tlk 5 more too, which NavRouteParser (and CountNavPoints) then read as // an extra Point at 0,0,0 after every portal, vendor or NPC: after using a // portal the bot headed for the middle of Dereth. case "ptl": // metaf: ptl X Y Z TargetX TargetY TargetZ ObjectClass {Name} case "tlk": // metaf: tlk X Y Z TargetX TargetY TargetZ ObjectClass {Name} if (tokens.Length >= 8) { navFileLines.Add("6"); // type 6 = PortalNPC (used by name; NPC talk too) navFileLines.Add(tokens[1]); // EW navFileLines.Add(tokens[2]); // NS navFileLines.Add(tokens[3]); // Z navFileLines.Add("0"); string targetName = tokens.Length >= 9 ? tokens[8] : tokens[7]; if (!int.TryParse(tokens[7], out int objClass)) objClass = nodeType == "tlk" ? 37 : 14; // NPC / Portal navFileLines.Add(targetName); navFileLines.Add(objClass.ToString()); navFileLines.Add("False"); navFileLines.Add(tokens[4]); // target EW navFileLines.Add(tokens[5]); // target NS navFileLines.Add(tokens[6]); // target Z } else if (nodeType == "tlk" && tokens.Length >= 5) { // Old RynthAi form: tlk EW NS Z objectId {NPCName} navFileLines.Add("6"); navFileLines.Add(tokens[1]); navFileLines.Add(tokens[2]); navFileLines.Add(tokens[3]); navFileLines.Add("0"); navFileLines.Add(tokens.Length >= 6 ? tokens[5] : "NPC"); navFileLines.Add("37"); // NPC class navFileLines.Add("False"); navFileLines.Add(tokens[1]); navFileLines.Add(tokens[2]); navFileLines.Add(tokens[3]); } break; case "vnd": // metaf: vnd X Y Z TargetGUID {TargetName} if (tokens.Length >= 5) { navFileLines.Add("6"); // treat vendor as PortalNPC type (used by name) navFileLines.Add(tokens[1]); // EW navFileLines.Add(tokens[2]); // NS navFileLines.Add(tokens[3]); // Z navFileLines.Add("0"); string vndName = tokens.Length >= 6 ? tokens[5] : "Vendor"; navFileLines.Add(vndName); navFileLines.Add("12"); // NPC class navFileLines.Add("False"); navFileLines.Add(tokens[1]); navFileLines.Add(tokens[2]); navFileLines.Add(tokens[3]); } break; case "flw": // flw objectId {Name} if (tokens.Length >= 2) { // Follow routes don't have standard nav points — skip // The route type (3=Follow) already indicates follow behavior } break; case "chk": // checkpoint — treat as point at 0,0,0 case "jmp": // jump — treat as point at 0,0,0 break; } } // Insert point count after route type int pointCount = 0; for (int i = 0; i < navFileLines.Count; i++) { string l = navFileLines[i]; if (l == "0" || l == "2" || l == "3" || l == "4" || l == "6") { // Count nav point type entries if (i == 0 || navFileLines[i - 1] == "0" || !int.TryParse(navFileLines[i - 1], out _)) pointCount++; } } // Actually count points more simply: each point starts with a type line // followed by EW, NS, Z, flag pointCount = CountNavPoints(navFileLines); navContent.Add(pointCount.ToString()); navContent.AddRange(navFileLines); if (!string.IsNullOrEmpty(navName)) navRoutes[navName] = navContent; } private static int CountNavPoints(List navFileLines) { // Mirror NavRouteParser exactly via its shared trailer table so the two // counters cannot drift (the drift — type 6 counted as +12 here but read // as 6 trailer lines there — is what corrupted Portal routes pre-fix). int count = 0; int i = 0; while (i < navFileLines.Count) { if (!int.TryParse(navFileLines[i], NumberStyles.Integer, CultureInfo.InvariantCulture, out int pointType)) break; // desync / trailing data — stop counting if (!NavRouteParser.IsKnownType(pointType)) break; // unknown type: trailer length unknown, stop (matches NavRouteParser) count++; i += 5 + NavRouteParser.TrailerLineCount((NavPointType)pointType); // type+EW+NS+Z+flag + trailer } return count; } /// /// Drops empty nav routes (header only, 0 points) from the dict and /// blanks EmbedNav action data on rules whose referenced nav was empty. /// Non-empty embedded navs survive in the dict for runtime + save. /// private static void PruneEmbeddedNavs(List rules, Dictionary> navRoutes) { if (navRoutes.Count == 0) return; var toRemove = new List(); foreach (var kvp in navRoutes) { var content = kvp.Value; if (content.Count <= 3 || content[2] == "0") toRemove.Add(kvp.Key); } foreach (string key in toRemove) navRoutes.Remove(key); ClearMissingEmbedRefs(rules, navRoutes); } private static void ClearMissingEmbedRefs(List rules, Dictionary> navRoutes) { foreach (var rule in rules) { if (rule.Action == MetaActionType.EmbeddedNavRoute && !string.IsNullOrEmpty(rule.ActionData) && !navRoutes.ContainsKey(rule.ActionData)) { rule.ActionData = ""; } if (rule.ActionChildren != null) ClearMissingEmbedRefs(rule.ActionChildren, navRoutes); } } // ── Helpers ────────────────────────────────────────────────────────────── private static int CountLeadingTabs(string line) { int count = 0; foreach (char c in line) { if (c == '\t') count++; else break; } return count; } private static string StripPrefix(string text, string prefix) { text = text.Trim(); if (text.StartsWith(prefix, StringComparison.Ordinal)) text = text.Substring(prefix.Length); return text.TrimStart('\t', ' '); } /// /// Splits text into bare arguments (before first brace) and brace content. /// E.g. "0 {Portal Gem}" → bare="0", brace="Portal Gem" /// private static void SplitBareAndBrace(string text, out string bareArgs, out string braceContent) { int braceStart = text.IndexOf('{'); if (braceStart < 0) { bareArgs = text.Trim(); braceContent = ""; return; } bareArgs = text.Substring(0, braceStart).Trim(); braceContent = ExtractBraceContent(text.Substring(braceStart), out _); } /// /// Returns bare text (no braces) or brace content if braces exist. /// Handles both "4" and "{4}" formats. /// private static string ExtractBareOrBrace(string text) { text = text.Trim(); if (text.StartsWith("{")) return ExtractBraceContent(text, out _); // Return the first token (up to space or end) int space = text.IndexOf(' '); return space > 0 ? text.Substring(0, space).Trim() : text; } /// /// Extracts content from the first {brace group} in the text. /// Returns the content inside braces and sets endPos to the position after the closing brace. /// private static string ExtractBraceContent(string text, out int endPos) { endPos = 0; int start = text.IndexOf('{'); if (start < 0) return text.Trim(); int depth = 0; for (int i = start; i < text.Length; i++) { if (text[i] == '{') depth++; else if (text[i] == '}') { depth--; if (depth == 0) { endPos = i + 1; return text.Substring(start + 1, i - start - 1); } } } // Unmatched brace — return everything after the opening brace endPos = text.Length; return text.Substring(start + 1); } /// /// Splits a nav point line into tokens, treating {brace groups} as single tokens. /// private static string[] SplitNavTokens(string line) { var tokens = new List(); int i = 0; while (i < line.Length) { // Skip whitespace while (i < line.Length && (line[i] == ' ' || line[i] == '\t')) i++; if (i >= line.Length) break; if (line[i] == '{') { // Read brace-delimited token int depth = 0; int start = i + 1; for (; i < line.Length; i++) { if (line[i] == '{') depth++; else if (line[i] == '}') { depth--; if (depth == 0) { i++; break; } } } tokens.Add(line.Substring(start, Math.Max(0, i - start - 1))); } else { // Read space-delimited token int start = i; while (i < line.Length && line[i] != ' ' && line[i] != '\t' && line[i] != '{') i++; tokens.Add(line.Substring(start, i - start)); } } return tokens.ToArray(); } private static int RecallNameToSpellId(string name) { // Map common recall names to spell IDs return name.ToLowerInvariant() switch { "recall aphus lassel" or "aphus lassel recall" => 2931, "lifestone recall" or "lifestone" => 1635, "primary portal recall" or "primary portal" => 48, "secondary portal recall" or "secondary portal" => 2647, "portal recall" => 2645, "lifestone sending" or "lifestone tie" => 1635, "recall the sanctuary" or "sanctuary recall" => 2023, "call of the mhoire forge" or "mhoire forge" => 4213, "glenden wood recall" or "glenden wood" => 3865, "aerlinthe recall" or "aerlinthe" => 2041, "colosseum recall" or "colosseum" => 4084, "facility hub recall" or "facility hub" => 5541, "gear knight recall" or "gear knight" => 5542, "neftet recall" or "neftet" => 5543, "rynthid recall" or "rynthid" => 6321, "viridian rise recall" or "viridian rise" => 6322, _ => 0 }; } }