using System; using System.Collections.Generic; using System.Globalization; using System.IO; using System.Linq; using LiteDB; using RynthCore.CreatureSeed; // LiteDB ships its own JsonSerializer; the seed is written with System.Text.Json (source-generated). using StjSerializer = System.Text.Json.JsonSerializer; namespace RynthCore.MonsterEditor; /// /// Converts UtilityBelt's mob_damage_insights.ldb (LiteDB 5) into the RynthAi /// damage-type seed (). Runs here rather than in the plugin /// because LiteDB is reflection-heavy and the RynthAi plugin is NativeAOT. /// /// UB rows record damage players dealt TO each monster, per damage type. The /// per-character databases are pushed into the shared server database every 12 hours, /// so they are mostly a subset of it: for each monster the single row with the most hits /// wins (summing would double-count). /// /// internal static class UbMobSeedImport { /// Hits a damage type needs before it is ranked. public const int DefaultMinHitsPerType = 20; private const string DbFileName = "mob_damage_insights.ldb"; private const string WcidCollection = "mob_damage_agg"; private const string MagCollection = "mob_mag_name_agg"; /// UB damage-type flags (ACE DamageType) mapped to RynthAi element names. /// Normal (0) and combined flags are not castable choices and are skipped. private static readonly Dictionary FlagToElement = new() { ["1"] = "Slash", ["2"] = "Pierce", ["4"] = "Bludgeon", ["8"] = "Cold", ["16"] = "Fire", ["32"] = "Acid", ["64"] = "Lightning", ["1024"] = "Nether", }; /// Outcome of an import run. public sealed record Result(bool Ok, string Message, UbMobSeedFile? Seed); /// UB's per-world folder for InfiniteLeaftide (holds the shared server database). public static string DefaultServerFolder => Path.Combine( Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Decal Plugins", "UtilityBelt", "InfiniteLeaftide"); /// Where the plugin reads the seed: <SuiteDir>\RynthAi\CreatureData\ub-mob-seed.json. public static string DefaultOutputPath => Path.Combine( RynthCore.Install.RynthInstallPaths.RynthAiDir, "CreatureData", UbMobSeedFile.FileName); /// /// Lists the shared server database (first) and every per-character database /// one level below it. /// public static List FindDatabases(string serverFolder) { var list = new List(); if (!Directory.Exists(serverFolder)) return list; string shared = Path.Combine(serverFolder, DbFileName); if (File.Exists(shared)) list.Add(shared); foreach (string dir in Directory.GetDirectories(serverFolder).OrderBy(d => d, StringComparer.OrdinalIgnoreCase)) { string perChar = Path.Combine(dir, DbFileName); if (File.Exists(perChar)) list.Add(perChar); } return list; } /// /// Reads every database under , ranks damage types per /// monster and writes the seed to atomically. /// public static Result Run(string serverFolder, string outputPath, int minHitsPerType = DefaultMinHitsPerType) { List dbs = FindDatabases(serverFolder); if (dbs.Count == 0) return new Result(false, $"No {DbFileName} found in {serverFolder}.", null); // Best row per wcid / per normalized Mag-Tools name, across all sources. var bestWcid = new Dictionary(); var bestMag = new Dictionary(StringComparer.OrdinalIgnoreCase); var used = new List(); var errors = new List(); foreach (string db in dbs) { try { ReadDatabase(db, bestWcid, bestMag); used.Add(db); } catch (Exception ex) { errors.Add($"{Path.GetFileName(Path.GetDirectoryName(db))}: {ex.Message}"); } } if (used.Count == 0) return new Result(false, "Could not read any UB database: " + string.Join("; ", errors), null); var seed = new UbMobSeedFile { GeneratedUtc = DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ssZ", CultureInfo.InvariantCulture), MinHitsPerType = minHitsPerType, Sources = used, World = MostCommonWorld(bestWcid.Values), }; // Live UB rows keyed by wcid. var wcidNames = new HashSet(StringComparer.OrdinalIgnoreCase); foreach (var (wcid, doc) in bestWcid.OrderBy(kv => kv.Key)) { string name = Str(doc, "LastSeenName"); if (name.Length > 0) wcidNames.Add(name); var elements = RankElements(doc, minHitsPerType, hasPeaks: true); if (elements.Count < 2) continue; // one type alone says nothing about weakness seed.Entries.Add(new UbMobSeedEntry { Wcid = wcid, Name = name, Source = "ub", TotalHits = Num(doc, "TotalHits"), Elements = elements, }); } // Mag-Tools name-only rows, skipped when a wcid row already covers the name. foreach (var doc in bestMag.Values) { string name = Str(doc, "LastSeenDisplayName"); if (name.Length == 0) name = Str(doc, "NormalizedMobName"); if (name.Length == 0 || wcidNames.Contains(name)) continue; var elements = RankElements(doc, minHitsPerType, hasPeaks: false); if (elements.Count < 2) continue; seed.Entries.Add(new UbMobSeedEntry { Wcid = 0, Name = name, Source = "magtools", TotalHits = elements.Sum(e => (long)e.Hits), Elements = elements, }); } WriteAtomic(outputPath, StjSerializer.Serialize(seed, EditorJsonContext.Default.UbMobSeedFile)); int ub = seed.Entries.Count(e => e.Source == "ub"); int mag = seed.Entries.Count - ub; string msg = $"UB seed written: {ub} monster(s) by wcid + {mag} by name (Mag-Tools) from {used.Count} database(s) → {outputPath}"; if (errors.Count > 0) msg += $" Skipped: {string.Join("; ", errors)}"; return new Result(true, msg, seed); } /// /// Opens a snapshot copy of (UB may have the live file open) /// and folds its rows into the best-row maps. /// private static void ReadDatabase(string dbPath, Dictionary bestWcid, Dictionary bestMag) { string tempDir = Path.Combine(Path.GetTempPath(), "RynthUbSeed-" + Guid.NewGuid().ToString("N")); Directory.CreateDirectory(tempDir); try { string copy = Path.Combine(tempDir, DbFileName); CopyShared(dbPath, copy); // LiteDB 5 keeps uncommitted pages in "-log.ldb"; copy it so recent hits are included. string log = Path.Combine(Path.GetDirectoryName(dbPath)!, Path.GetFileNameWithoutExtension(dbPath) + "-log.ldb"); if (File.Exists(log)) CopyShared(log, Path.Combine(tempDir, Path.GetFileNameWithoutExtension(DbFileName) + "-log.ldb")); // Writable connection on the private copy so LiteDB can replay the log; the original is untouched. using var db = new LiteDatabase(new ConnectionString { Filename = copy, Connection = ConnectionType.Direct }); if (db.CollectionExists(WcidCollection)) { foreach (var doc in db.GetCollection(WcidCollection).FindAll()) { long wcid = Num(doc, "Wcid"); // wcid 1 is the player weenie (self/PvP lines), not a monster. if (wcid <= 1 || wcid > uint.MaxValue) continue; KeepBest(bestWcid, (uint)wcid, doc, Num(doc, "TotalHits")); } } if (db.CollectionExists(MagCollection)) { foreach (var doc in db.GetCollection(MagCollection).FindAll()) { string key = Str(doc, "NormalizedMobName"); if (key.Length == 0) continue; KeepBest(bestMag, key, doc, SumValues(doc, "HitsByType")); } } } finally { try { Directory.Delete(tempDir, recursive: true); } catch { /* temp cleanup is best-effort */ } } } /// Keeps the row with the most hits per key (sources overlap; see class remarks). private static void KeepBest(Dictionary map, TKey key, BsonDocument doc, long hits) where TKey : notnull { if (!map.TryGetValue(key, out var existing) || hits > HitsOf(existing)) map[key] = doc; } /// Total hits of a row in either collection's shape. private static long HitsOf(BsonDocument doc) { long total = Num(doc, "TotalHits"); return total > 0 ? total : SumValues(doc, "HitsByType"); } /// /// Ranks the castable damage types with at least hits. /// Score blends average damage (vs the best average) with UB's own effectiveness /// measure, peak hit vs the best peak, when peaks are recorded. /// private static List RankElements(BsonDocument doc, int minHits, bool hasPeaks) { var hits = Doc(doc, "HitsByType"); var sums = Doc(doc, "SumDamageByType"); var peaks = hasPeaks ? Doc(doc, "MaxDamageByType") : null; if (hits == null || sums == null) return new List(); var list = new List(); foreach (string flag in hits.Keys) { if (!FlagToElement.TryGetValue(flag, out string? element)) continue; int n = (int)Math.Min(int.MaxValue, Num(hits, flag)); if (n < minHits || !sums.ContainsKey(flag)) continue; list.Add(new UbMobSeedElement { Element = element, Hits = n, AvgDamage = Math.Round(Num(sums, flag) / (double)n, 2), MaxHit = peaks != null && peaks.ContainsKey(flag) ? Num(peaks, flag) : 0, }); } if (list.Count == 0) return list; double bestAvg = list.Max(e => e.AvgDamage); long bestPeak = list.Max(e => e.MaxHit); foreach (var e in list) { double avgRatio = bestAvg > 0 ? e.AvgDamage / bestAvg : 0; double score = bestPeak > 0 ? 0.5 * avgRatio + 0.5 * (e.MaxHit / (double)bestPeak) : avgRatio; e.Score = Math.Round(score, 4); } return list.OrderByDescending(e => e.Score).ThenByDescending(e => e.Hits).ToList(); } /// Most frequent WorldName among the rows (empty when none recorded). private static string MostCommonWorld(IEnumerable rows) => rows.Select(r => Str(r, "WorldName")) .Where(w => w.Length > 0) .GroupBy(w => w, StringComparer.OrdinalIgnoreCase) .OrderByDescending(g => g.Count()) .Select(g => g.Key) .FirstOrDefault() ?? string.Empty; /// Copies a file another process may hold open for writing. private static void CopyShared(string source, string dest) { using var src = new FileStream(source, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete); using var dst = new FileStream(dest, FileMode.Create, FileAccess.Write, FileShare.None); src.CopyTo(dst); } /// Writes via a temp file so the plugin never reads a half-written seed. private static void WriteAtomic(string path, string contents) { Directory.CreateDirectory(Path.GetDirectoryName(path)!); string tmp = path + ".tmp"; File.WriteAllText(tmp, contents); File.Move(tmp, path, overwrite: true); } // ── BSON helpers (schema-tolerant: UB may store numbers as Int32, Int64 or Double) ── private static BsonDocument? Doc(BsonDocument doc, string field) => doc.TryGetValue(field, out var v) && v.IsDocument ? v.AsDocument : null; private static string Str(BsonDocument doc, string field) => doc.TryGetValue(field, out var v) && v.IsString ? v.AsString.Trim() : string.Empty; private static long Num(BsonDocument doc, string field) => doc.TryGetValue(field, out var v) && v.IsNumber ? Convert.ToInt64(v.RawValue, CultureInfo.InvariantCulture) : 0; private static long SumValues(BsonDocument doc, string field) { var d = Doc(doc, field); if (d == null) return 0; long total = 0; foreach (string k in d.Keys) total += Num(d, k); return total; } }