using System; using System.Collections.Generic; using System.Globalization; using System.IO; using System.Text; using RynthCore.Loot.VTank; namespace RynthCore.Loot.Editing; /// /// One open loot profile being edited (RynthCore docs/IMGUI_LOOT_EDITOR.md): /// load, edit commands, validation and a save that never clobbers a file that /// changed on disk. Pure data and file I/O, no plugin or AC types, so it runs /// in RynthAi and in the offline tests alike. Not thread-safe: the caller /// serialises calls (RynthAi's bridge holds a lock). /// /// .utl files are edited in the VTank model, which keeps every line it parsed, /// and written back in the file's own encoding, line endings and BOM, so an /// unedited profile saves byte-identical. A file the model can't reproduce /// exactly opens read-only, as do .json profiles (their only writer is lossy). /// public sealed class LootEditSession { private VTankLootProfile? _utl; private LootProfile? _json; private string _path = string.Empty; private FileStamp _stamp; private Encoding _encoding = new UTF8Encoding(false); private bool _bom, _crlf, _finalNewline = true; private bool _dirty, _changedOnDisk; private string _readOnlyReason = string.Empty; private string _message = string.Empty; private bool _messageOk = true; private long _messageSeq; private int _focus = -1; /// Bumped on every change a face could show. public long Revision { get; private set; } = 1; public string Path => _path; public bool IsOpen => _path.Length > 0; public bool IsDirty => _dirty; public bool IsReadOnly => _utl == null; public int RuleCount => Shown?.Rules.Count ?? _json?.Rules.Count ?? 0; /// The edited model (null for JSON / nothing open). Tests read it. public VTankLootProfile? Profile => _utl; // ===================================================================== // Load // ===================================================================== /// /// Opens : a .utl or .json file, or a .utl path that /// doesn't exist yet (a new, empty profile that Save creates). Unsaved edits /// block it unless . False (with a message) on failure. /// public bool Open(string path, bool force = false) { path = (path ?? string.Empty).Trim().Trim('"'); if (path.Length == 0) return Fail("No loot profile selected."); if (_dirty && !force) return Fail($"Unsaved changes in {FileName(_path)}. Save them or discard them first."); try { path = System.IO.Path.GetFullPath(path); } catch (Exception ex) { return Fail($"Bad path {path}: {ex.Message}"); } bool json = path.EndsWith(".json", StringComparison.OrdinalIgnoreCase); if (!json && !path.EndsWith(".utl", StringComparison.OrdinalIgnoreCase)) return Fail($"{FileName(path)}: not a .utl or .json loot profile."); try { if (!File.Exists(path)) { if (json) return Fail($"{FileName(path)} not found."); SetLoaded(path, new VTankLootProfile { FileVersion = 1 }, null, default, readOnlyReason: string.Empty); return Ok($"New profile. Save creates {FileName(path)}."); } byte[] bytes = File.ReadAllBytes(path); FileStamp stamp = FileStamp.Of(path, bytes); if (json) { LootProfile? jp = System.Text.Json.JsonSerializer.Deserialize(Decode(bytes, out _, out _), LootJsonContext.Default.LootProfile); SetLoaded(path, null, jp ?? new LootProfile(), stamp, "JSON profiles are read-only here (saving JSON loses conditions). Use the external editor."); return Ok($"Opened {FileName(path)} ({RuleCount} rules), read-only."); } string text = Decode(bytes, out Encoding encoding, out bool bom); VTankLootProfile profile = VTankLootParser.LoadFromText(text); bool finalNewline = text.Length == 0 || text.EndsWith('\n'); string reason = RoundTrips(text, profile, finalNewline) ? string.Empty : "This file has lines the editor would not write back the same way; it is read-only here so nothing is lost. Use the external editor."; SetLoaded(path, profile, null, stamp, reason); _encoding = encoding; _bom = bom; _crlf = text.Contains("\r\n", StringComparison.Ordinal); _finalNewline = finalNewline; return Ok(reason.Length == 0 ? $"Opened {FileName(path)} ({RuleCount} rules)." : $"Opened {FileName(path)} ({RuleCount} rules), read-only."); } catch (Exception ex) { return Fail($"Could not open {FileName(path)}: {ex.Message}"); } } private void SetLoaded(string path, VTankLootProfile? utl, LootProfile? json, FileStamp stamp, string readOnlyReason) { _path = path; _utl = readOnlyReason.Length == 0 ? utl : null; _json = json; _readOnlyUtl = readOnlyReason.Length == 0 ? null : utl; _stamp = stamp; _encoding = new UTF8Encoding(false); _bom = false; _crlf = false; _finalNewline = true; _dirty = false; _changedOnDisk = false; _readOnlyReason = readOnlyReason; _focus = -1; Revision++; } // A .utl opened read-only (it doesn't round-trip): shown, never saved. private VTankLootProfile? _readOnlyUtl; private VTankLootProfile? Shown => _utl ?? _readOnlyUtl; /// /// True when writing the parsed profile gives the file's text back exactly, /// CRLF and a missing final newline aside (Encode restores both). /// private static bool RoundTrips(string text, VTankLootProfile profile, bool finalNewline) { string a = Norm(text); if (!finalNewline) a += "\n"; return string.Equals(a, Norm(VTankLootWriter.Serialize(profile)), StringComparison.Ordinal); } private static string Norm(string s) => s.Replace("\r\n", "\n"); /// UTF-8 when the bytes are valid UTF-8 (BOM noted), else Latin-1, which maps every byte to one char and back. internal static string Decode(byte[] bytes, out Encoding encoding, out bool bom) { bom = bytes.Length >= 3 && bytes[0] == 0xEF && bytes[1] == 0xBB && bytes[2] == 0xBF; int start = bom ? 3 : 0; try { string s = new UTF8Encoding(false, true).GetString(bytes, start, bytes.Length - start); encoding = new UTF8Encoding(false); return s; } catch (DecoderFallbackException) { bom = false; encoding = Encoding.Latin1; return Encoding.Latin1.GetString(bytes); } } // ===================================================================== // Disk watch // ===================================================================== /// /// Looks at the file on disk (a stat, no read). Changed with no unsaved edits: /// reload it. Changed under unsaved edits: flag it (Save then needs Force). /// Returns true when anything changed. Call now and then (every couple of seconds). /// public bool CheckDisk() { if (!IsOpen || _changedOnDisk) return false; FileStamp now; try { now = FileStamp.Stat(_path); } catch { return false; } if (now.SameStat(_stamp)) return false; if (!_dirty) { if (!now.Exists) return false; // deleted under a clean view: keep showing it; Save recreates string path = _path; Open(path, force: true); SetMessage($"{FileName(path)} changed on disk; reloaded ({RuleCount} rules).", ok: true); return true; } _changedOnDisk = true; SetMessage($"{FileName(_path)} changed on disk since you opened it. Reload (drops your edits) or Save anyway.", ok: false); return true; } // ===================================================================== // Commands // ===================================================================== /// What a command did, for the bridge (Saved: hot-reload RynthAi). public enum Outcome { Refused, Changed, Saved, Opened } /// /// Applies one command. open and save take the path/flags in the command; /// the caller resolves an empty open path first. Every call sets the message. /// public Outcome Apply(LootEditCommand cmd) { switch (cmd.Op) { case "open": return Open(cmd.Path, cmd.Force) ? Outcome.Opened : Outcome.Refused; case "reload": if (!IsOpen) { Fail("No profile open."); return Outcome.Refused; } return Open(_path, cmd.Force) ? Outcome.Opened : Outcome.Refused; case "save": return Save(cmd.Force) ? Outcome.Saved : Outcome.Refused; } if (_utl == null) { Fail(IsOpen ? _readOnlyReason : "No profile open."); return Outcome.Refused; } List rules = _utl.Rules; if (cmd.Op == "add") { int at = cmd.Index < 0 || cmd.Index >= rules.Count ? rules.Count : cmd.Index + 1; rules.Insert(at, NewRule("New Rule")); return Changed($"Added a rule at {at + 1}.", at); } if (cmd.Index < 0 || cmd.Index >= rules.Count) { Fail("That rule is no longer there. The list has been refreshed."); return Outcome.Refused; } VTankLootRule rule = rules[cmd.Index]; if (cmd.Expect != null && !string.Equals(cmd.Expect, rule.Name, StringComparison.Ordinal)) { Fail("The list changed under that edit; it was not applied. Try again."); return Outcome.Refused; } switch (cmd.Op) { case "set_enabled": { bool on = string.Equals(cmd.Value, "true", StringComparison.OrdinalIgnoreCase); if (rule.Enabled == on) return Changed(string.Empty, cmd.Index, quiet: true); SetEnabled(rule, on); return Changed($"{(on ? "Enabled" : "Disabled")} \"{rule.Name}\".", cmd.Index); } case "move": { int to = Math.Clamp(cmd.To, 0, rules.Count - 1); if (to == cmd.Index) return Changed(string.Empty, to, quiet: true); rules.RemoveAt(cmd.Index); rules.Insert(to, rule); return Changed($"Moved \"{rule.Name}\" to {to + 1}.", to); } case "duplicate": { VTankLootRule copy = CloneRule(rule); copy.Name = rule.Name + " (copy)"; rules.Insert(cmd.Index + 1, copy); return Changed($"Duplicated \"{rule.Name}\".", cmd.Index + 1); } case "delete": rules.RemoveAt(cmd.Index); return Changed($"Deleted \"{rule.Name}\".", Math.Min(cmd.Index, rules.Count - 1)); case "rename": { // Kept as typed (no trim): real profiles have unnamed rules and trailing spaces. string name = OneLine(cmd.Value); rule.Name = name; return Changed($"Renamed to \"{name}\".", cmd.Index); } case "update_rule": { if (cmd.Rule == null) { Fail("No rule in the update."); return Outcome.Refused; } string? error = ApplyRule(rule, cmd.Rule); if (error != null) { Fail(error); return Outcome.Refused; } return Changed($"Updated \"{rule.Name}\" (not saved yet).", cmd.Index); } } Fail($"Unknown loot edit command '{cmd.Op}'."); return Outcome.Refused; } private VTankLootRule NewRule(string name) => new() { Name = name, CustomExpression = _utl != null && _utl.FileVersion >= 1 ? string.Empty : null, Action = VTankLootAction.Keep, }; private static VTankLootRule CloneRule(VTankLootRule src) { var copy = new VTankLootRule { Name = src.Name, CustomExpression = src.CustomExpression, Priority = src.Priority, Action = src.Action, KeepCount = src.KeepCount, }; foreach (VTankLootCondition c in src.Conditions) copy.Conditions.Add(new VTankLootCondition(c.NodeType, c.LengthCode, c.DataLines)); return copy; } /// /// Disabling sets an existing DisabledRule node to "true" where it is (else /// appends one); enabling removes them, as the SDK and VTank do. /// public static void SetEnabled(VTankLootRule rule, bool enabled) { if (enabled) { rule.Enabled = true; return; } foreach (VTankLootCondition c in rule.Conditions) if (c.NodeType == VTankNodeTypes.DisabledRule) { if (c.DataLines.Count == 0) c.DataLines.Add("true"); else c.DataLines[0] = "true"; return; } rule.Conditions.Add(new VTankLootCondition(VTankNodeTypes.DisabledRule, "0", new[] { "true" })); } /// Replaces with after checking it. Null, or what is wrong. private string? ApplyRule(VTankLootRule rule, LootEditRule dto) { string name = OneLine(dto.Name); // unnamed rules are common (LootSnobV4 has 152) if (!LootRuleText.IsKnownAction(dto.Action)) return $"Unknown action {dto.Action}."; var action = (VTankLootAction)dto.Action; if (action == VTankLootAction.KeepUpTo && dto.KeepCount < 0) return "Keep # needs a count of 0 or more."; var conditions = new List(dto.Conditions.Count); for (int i = 0; i < dto.Conditions.Count; i++) { LootEditCondition c = dto.Conditions[i]; var lines = new List(c.Lines.Count); foreach (string line in c.Lines) lines.Add(OneLine(line)); int arity = VTankNodeTypes.GetDataLineCount(c.NodeType); bool unchanged = HasSame(rule, c.NodeType, lines); if (arity < 0 && !unchanged) return $"Condition {i + 1}: unknown node type {c.NodeType}."; if (arity >= 0 && lines.Count != arity) { if (lines.Count > arity) return $"Condition {i + 1} ({LootRuleText.NodeName(c.NodeType)}) has {lines.Count} lines; it takes {arity}."; List defaults = LootRuleText.DefaultLines(c.NodeType); while (lines.Count < arity) lines.Add(defaults[lines.Count]); } if (!unchanged && LootRuleText.EditorFor(c.NodeType).Editor != "raw") { string? bad = CheckNumbers(c.NodeType, lines); if (bad != null) return $"Condition {i + 1} ({LootRuleText.NodeName(c.NodeType)}): {bad}"; } conditions.Add(new VTankLootCondition(c.NodeType, OneLine(c.LengthCode ?? "0"), lines)); } rule.Name = name; rule.Action = action; rule.KeepCount = action == VTankLootAction.KeepUpTo ? dto.KeepCount : null; rule.Priority = dto.Priority; if (rule.CustomExpression != null || _utl!.FileVersion >= 1) rule.CustomExpression = OneLine(dto.CustomExpression ?? string.Empty); rule.Conditions = conditions; if (rule.Enabled != dto.Enabled) SetEnabled(rule, dto.Enabled); return null; } private static bool HasSame(VTankLootRule rule, int nodeType, List lines) { foreach (VTankLootCondition c in rule.Conditions) { if (c.NodeType != nodeType || c.DataLines.Count != lines.Count) continue; bool same = true; for (int i = 0; i < lines.Count && same; i++) same = string.Equals(c.DataLines[i], lines[i], StringComparison.Ordinal); if (same) return true; } return false; } private static string? CheckNumbers(int type, List lines) { string[] labels = LootRuleText.LineLabels(type); for (int i = 0; i < lines.Count; i++) { if (LootRuleText.IsTextLine(type, i)) continue; string v = lines[i].Trim(); string label = i < labels.Length ? labels[i] : $"line {i + 1}"; if (LootRuleText.IsIntLine(type, i)) { if (!int.TryParse(v, NumberStyles.Integer, CultureInfo.InvariantCulture, out _)) return $"{label} must be a whole number (got \"{v}\")."; } else if (!double.TryParse(v, NumberStyles.Float, CultureInfo.InvariantCulture, out _)) return $"{label} must be a number (got \"{v}\")."; lines[i] = v; } return null; } /// Newlines would break the line-based file: they become spaces. private static string OneLine(string? s) => string.IsNullOrEmpty(s) ? string.Empty : s.Replace("\r\n", " ").Replace('\r', ' ').Replace('\n', ' '); // ===================================================================== // Save // ===================================================================== /// /// Writes the profile: refuses when the file changed on disk since it was /// loaded (unless ), writes a temp file and swaps it /// in (the old file kept as .bak). /// public bool Save(bool force = false) { if (!IsOpen) return Fail("No profile open."); if (_utl == null) return Fail(_readOnlyReason); try { FileStamp now = FileStamp.Read(_path); if (!force && !now.SameContent(_stamp)) { _changedOnDisk = true; return Fail($"{FileName(_path)} changed on disk since you opened it. Reload (drops your edits) or Save anyway."); } byte[] bytes = Encode(); string? dir = System.IO.Path.GetDirectoryName(_path); if (!string.IsNullOrEmpty(dir)) Directory.CreateDirectory(dir); string tmp = _path + ".tmp"; File.WriteAllBytes(tmp, bytes); if (File.Exists(_path)) { try { File.Replace(tmp, _path, _path + ".bak", ignoreMetadataErrors: true); } catch (PlatformNotSupportedException) { File.Copy(tmp, _path, overwrite: true); File.Delete(tmp); } catch (IOException) { File.Copy(_path, _path + ".bak", overwrite: true); File.Copy(tmp, _path, overwrite: true); File.Delete(tmp); } } else File.Move(tmp, _path); _stamp = FileStamp.Read(_path); _dirty = false; _changedOnDisk = false; return Ok($"Saved {FileName(_path)} ({RuleCount} rules)."); } catch (Exception ex) { return Fail($"Save failed: {ex.Message}"); } } /// The file's bytes as Save writes them (its own encoding, line endings, BOM). public byte[] Encode() { if (_utl == null) return Array.Empty(); string text = VTankLootWriter.Serialize(_utl); if (!_finalNewline && text.EndsWith('\n')) text = text.Substring(0, text.Length - 1); if (_crlf) text = text.Replace("\n", "\r\n"); byte[] body = _encoding.GetBytes(text); if (!_bom) return body; byte[] withBom = new byte[body.Length + 3]; withBom[0] = 0xEF; withBom[1] = 0xBB; withBom[2] = 0xBF; Array.Copy(body, 0, withBom, 3, body.Length); return withBom; } // ===================================================================== // What the face sees // ===================================================================== /// The header and one row per rule. : RynthAi's loot profile. public LootEditState BuildState(string inUsePath, List? files) { var s = new LootEditState { Revision = Revision, Path = _path, FileName = FileName(_path), Format = !IsOpen ? string.Empty : _json != null ? "json" : "utl", Exists = _stamp.Exists, ReadOnly = IsOpen && _utl == null, ReadOnlyReason = _readOnlyReason, Dirty = _dirty, ChangedOnDisk = _changedOnDisk, InUsePath = inUsePath ?? string.Empty, InUse = IsOpen && SamePath(_path, inUsePath), Message = _message, MessageOk = _messageOk, MessageSeq = _messageSeq, Focus = _focus, }; if (files != null) s.Files = files; if (Shown is VTankLootProfile p) { s.Rules.Capacity = p.Rules.Count; foreach (VTankLootRule r in p.Rules) s.Rules.Add(new LootEditRow { Name = r.Name, Enabled = r.Enabled, Action = (int)r.Action, KeepCount = r.KeepCount ?? 0, Summary = LootRuleText.Summary(r), Conditions = CountConditions(r), }); } else if (_json != null) { foreach (LootRule r in _json.Rules) s.Rules.Add(new LootEditRow { Name = r.Name, Enabled = r.Enabled, Action = (int)JsonAction(r.Action), KeepCount = r.KeepCount, Summary = r.Conditions.Count == 0 ? "Matches every item" : string.Join(" AND ", r.Conditions), Conditions = r.Conditions.Count, }); } return s; } private static int CountConditions(VTankLootRule r) { int n = 0; foreach (VTankLootCondition c in r.Conditions) if (c.NodeType != VTankNodeTypes.DisabledRule) n++; return n; } /// The whole rule at , or null. public LootEditRule? BuildRule(int index) { if (Shown is VTankLootProfile p) { if (index < 0 || index >= p.Rules.Count) return null; VTankLootRule r = p.Rules[index]; var dto = new LootEditRule { Index = index, Name = r.Name, Enabled = r.Enabled, Action = (int)r.Action, KeepCount = r.KeepCount ?? 0, Priority = r.Priority, CustomExpression = r.CustomExpression, }; foreach (VTankLootCondition c in r.Conditions) dto.Conditions.Add(new LootEditCondition { NodeType = c.NodeType, LengthCode = c.LengthCode, Lines = new List(c.DataLines) }); return dto; } if (_json != null && index >= 0 && index < _json.Rules.Count) { LootRule r = _json.Rules[index]; var dto = new LootEditRule { Index = index, Name = r.Name, Enabled = r.Enabled, Action = (int)JsonAction(r.Action), KeepCount = r.KeepCount, }; foreach (LootCondition c in r.Conditions) dto.Conditions.Add(new LootEditCondition { NodeType = -1, Lines = new List { c.ToString() ?? string.Empty } }); return dto; } return null; } private static VTankLootAction JsonAction(LootAction a) => a switch { LootAction.Sell => VTankLootAction.Sell, LootAction.Salvage => VTankLootAction.Salvage, LootAction.Read => VTankLootAction.Read, LootAction.KeepUpTo => VTankLootAction.KeepUpTo, _ => VTankLootAction.Keep, }; // ===================================================================== // Helpers // ===================================================================== public static bool SamePath(string? a, string? b) { if (string.IsNullOrWhiteSpace(a) || string.IsNullOrWhiteSpace(b)) return false; try { return string.Equals(System.IO.Path.GetFullPath(a.Trim().Trim('"')), System.IO.Path.GetFullPath(b.Trim().Trim('"')), StringComparison.OrdinalIgnoreCase); } catch { return false; } } private static string FileName(string path) => path.Length == 0 ? string.Empty : System.IO.Path.GetFileName(path); private Outcome Changed(string message, int focus, bool quiet = false) { if (!quiet) { _dirty = true; _focus = focus; SetMessage(message, ok: true); } return quiet ? Outcome.Refused : Outcome.Changed; } private bool Ok(string message) { SetMessage(message, ok: true); return true; } private bool Fail(string message) { SetMessage(message, ok: false); return false; } private void SetMessage(string message, bool ok) { _message = message; _messageOk = ok; _messageSeq++; Revision++; } /// What we know of the file: size and write time (a stat), plus a content hash when read. private readonly record struct FileStamp(bool Exists, long Length, DateTime WriteUtc, ulong Hash) { public static FileStamp Stat(string path) { var fi = new FileInfo(path); return fi.Exists ? new FileStamp(true, fi.Length, fi.LastWriteTimeUtc, 0) : default; } public static FileStamp Read(string path) { if (!File.Exists(path)) return default; return Of(path, File.ReadAllBytes(path)); } public static FileStamp Of(string path, byte[] bytes) { var fi = new FileInfo(path); return new FileStamp(true, bytes.Length, fi.LastWriteTimeUtc, Fnv(bytes)); } public bool SameStat(FileStamp other) => Exists == other.Exists && (!Exists || (Length == other.Length && WriteUtc == other.WriteUtc)); public bool SameContent(FileStamp other) => Exists == other.Exists && (!Exists || (Length == other.Length && Hash == other.Hash)); private static ulong Fnv(byte[] bytes) { ulong h = 14695981039346656037UL; foreach (byte b in bytes) { h ^= b; h *= 1099511628211UL; } return h; } } }