// IltParse.cs — Shared parsing / formatting helpers for ILT Hub server output. // Originally SilentKelpie's ILT Hub (RynthAi IltHub/); moved to RynthILT 2026-10-13. // // Centralised so every feature uses the same number parsing (commas, k/m/b/t suffixes), // the same compact K/M/B display, and the same regexes. Regexes are static and built once; // RegexOptions.Compiled is harmless under NativeAOT (it falls back to the interpreter). using System; using System.Globalization; using System.Text.RegularExpressions; namespace RynthCore.Plugin.RynthILT.Hub; internal static class IltParse { private const RegexOptions Opt = RegexOptions.Compiled | RegexOptions.CultureInvariant; /// /// "/aug" level line: "Creature:1,234", "Duration: 1,234", or "Specialize: 2". /// Trailing notes ("Triune Weave: 5 (adds ...)") are allowed; the planner only keeps known keys. /// public static readonly Regex AugLine = new(@"^(\w+)\s*:\s*(\d[\d,.\s']*)", Opt); /// "/pets" / "/shinies" roster entry: "12. Shiny Drudge Ravener". public static readonly Regex RosterLine = new(@"^\s*(\d+)\.\s+(.+)$", Opt); /// Bank-command amount entry: "1.5m", "250,000", "2b". public static readonly Regex AmountInput = new(@"^([\d,.]+)\s*([kmbt]?)$", Opt | RegexOptions.IgnoreCase); /// AC character name: starts with a letter, then letters, spaces, apostrophes or hyphens (max 32). public static readonly Regex CharacterName = new(@"^[A-Za-z][A-Za-z '\-]{0,31}$", Opt); /// A comma-grouped or plain integer as printed by ACE's N0 format. public const string AmountPattern = @"(?:\d{1,3}(?:,\d{3})+|\d+)"; /// "Transferred 5,000 Pyreals to Bob" (optional " (offline)" / trailing "--- note"). public static readonly Regex BankSent = new( @"^Transferred (" + AmountPattern + @") ([^']+?) to ([^-]+?)(?:\s*---.*)?$", Opt); /// "Received 5,000 Pyreals from Bob". public static readonly Regex BankReceived = new( @"^Received (" + AmountPattern + @") ([^']+?) from (.+?)(?:\s*---.*)?$", Opt); /// "/xp all" cost line: "[XP] Your XP cost for next 1 Strength level is: 1,234". public static readonly Regex XpCost = new(@"\[XP\] Your XP cost for next 1 (\w+) level is: ([\d,]+)", Opt); /// "/attr" success: "[ATTR] Strength attribute raised by 10, costing 1,234!". public static readonly Regex AttrRaised = new(@"^\[ATTR\] (\w+) attribute raised by (\d+), costing ([\d,]+)", Opt); private static readonly Regex GroupedInt = new(@"^\d{1,3}(?:[,.\s']\d{3})+|^\d+", RegexOptions.CultureInvariant); /// /// The level in an AugLine value: digits grouped by a comma, period, space or apostrophe /// ("4,300", "4.300", "4 300", "4'300") read as one number; "2 (adds 5)" as 2; "1.5" as 1. 0 when absent. /// public static long ParseAugValue(string text) { if (string.IsNullOrEmpty(text)) return 0; var m = GroupedInt.Match(text.Trim()); if (!m.Success) return 0; var digits = new System.Text.StringBuilder(m.Value.Length); foreach (char c in m.Value) if (char.IsDigit(c)) digits.Append(c); return long.TryParse(digits.ToString(), NumberStyles.None, CultureInfo.InvariantCulture, out long v) ? v : 0; } /// Strips commas and parses the leading integer; 0 when absent. public static long ParseLeadingLong(string text) { if (string.IsNullOrEmpty(text)) return 0; string s = text.Replace(",", string.Empty).Trim(); int end = 0; while (end < s.Length && char.IsDigit(s[end])) end++; return end > 0 && long.TryParse(s.AsSpan(0, end), NumberStyles.None, CultureInfo.InvariantCulture, out long v) ? v : 0; } /// /// Parses a user-entered amount with optional k/m/b/t suffix ("1.5m" → 1,500,000). /// Returns false for empty, negative, or overflowing input. /// public static bool TryParseAmount(string input, out long amount) { amount = 0; var m = AmountInput.Match((input ?? string.Empty).Trim()); if (!m.Success) return false; if (!decimal.TryParse(m.Groups[1].Value.Replace(",", string.Empty), NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out decimal baseVal)) return false; decimal mult = char.ToLowerInvariant(m.Groups[2].Value.Length > 0 ? m.Groups[2].Value[0] : ' ') switch { 'k' => 1_000m, 'm' => 1_000_000m, 'b' => 1_000_000_000m, 't' => 1_000_000_000_000m, _ => 1m, }; decimal total = Math.Floor(baseVal * mult); if (total <= 0 || total > long.MaxValue) return false; amount = (long)total; return true; } /// Compact display: 950 → "950", 12_300 → "12.3K", 4_500_000 → "4.50M", 2e9 → "2.00B". public static string Compact(double value) { double a = Math.Abs(value); string s = a >= 1e12 ? (value / 1e12).ToString("0.00", CultureInfo.InvariantCulture) + "T" : a >= 1e9 ? (value / 1e9).ToString("0.00", CultureInfo.InvariantCulture) + "B" : a >= 1e6 ? (value / 1e6).ToString("0.00", CultureInfo.InvariantCulture) + "M" : a >= 1e3 ? (value / 1e3).ToString("0.0", CultureInfo.InvariantCulture) + "K" : value.ToString("0", CultureInfo.InvariantCulture); return s; } /// Full grouped display: 1234567 → "1,234,567". public static string N0(long value) => value.ToString("N0", CultureInfo.InvariantCulture); /// "3d 4h 12m 5s" style duration; "ready" when zero or negative. public static string Duration(TimeSpan span) { if (span <= TimeSpan.Zero) return "ready"; if (span.TotalDays >= 1) return $"{(int)span.TotalDays}d {span.Hours}h {span.Minutes}m"; if (span.TotalHours >= 1) return $"{span.Hours}h {span.Minutes}m {span.Seconds}s"; if (span.TotalMinutes >= 1) return $"{span.Minutes}m {span.Seconds}s"; return $"{span.Seconds}s"; } /// Strips "<Tell:…>" style markup the chat window sometimes leaves on names. public static string StripMarkup(string text) => Regex.Replace(text ?? string.Empty, @"<[^>]*>", string.Empty).Trim(); }