diff --git a/src/RynthCore.Engine/ImGui/Panels/SettingsRows.cs b/src/RynthCore.Engine/ImGui/Panels/SettingsRows.cs index 3b24c45..9e883ab 100644 --- a/src/RynthCore.Engine/ImGui/Panels/SettingsRows.cs +++ b/src/RynthCore.Engine/ImGui/Panels/SettingsRows.cs @@ -310,19 +310,26 @@ internal sealed class SettingsRows } // Square dot + label; the whole row toggles. + /// at of its alpha (a greyed-out row). + private static uint Faded(uint abgr, float a) => a >= 1f ? abgr : (abgr & 0x00FFFFFFu) | ((uint)((abgr >> 24) * a) << 24); + private void BoolRow(SettingRow row, float w) { var dl = ImGuiNET.ImGui.GetWindowDrawList(); ImGuiNET.ImGui.Dummy(new Vector2(0, 2)); Vector2 p = ImGuiNET.ImGui.GetCursorScreenPos(); - bool on = row.Get!(_data) != 0; + // Greyed out (DisabledWhen): shown off, not clickable, and the tooltip says why. + bool disabled = row.IsDisabled(_data); + bool on = !disabled && row.Get!(_data) != 0; float labelW = ImGuiNET.ImGui.CalcTextSize(row.Label).X; - if (ImGuiNET.ImGui.InvisibleButton("##b", new Vector2(Math.Min(w, 18 + labelW), 16))) + if (ImGuiNET.ImGui.InvisibleButton("##b", new Vector2(Math.Min(w, 18 + labelW), 16)) && !disabled) Changed(row, on ? 0 : 1); - if (ImGuiNET.ImGui.IsItemHovered()) ImGuiNET.ImGui.SetMouseCursor(ImGuiMouseCursor.Hand); - Tooltip(row); - dl.AddRectFilled(p + new Vector2(0, 2), p + new Vector2(12, 14), on ? ToggleOn : ToggleOff, 2); - dl.AddText(p + new Vector2(18, (16 - ImGuiNET.ImGui.GetFontSize()) * 0.5f), TextDim, row.Label); + if (ImGuiNET.ImGui.IsItemHovered() && !disabled) ImGuiNET.ImGui.SetMouseCursor(ImGuiMouseCursor.Hand); + if (disabled && row.DisabledTip != null) { if (ImGuiNET.ImGui.IsItemHovered()) SafeImGui.SetTooltip(row.DisabledTip); } + else Tooltip(row); + float alpha = disabled ? 0.4f : 1f; + dl.AddRectFilled(p + new Vector2(0, 2), p + new Vector2(12, 14), Faded(on ? ToggleOn : ToggleOff, alpha), 2); + dl.AddText(p + new Vector2(18, (16 - ImGuiNET.ImGui.GetFontSize()) * 0.5f), Faded(TextDim, alpha), row.Label); ImGuiNET.ImGui.Dummy(new Vector2(0, 2)); } diff --git a/src/RynthCore.Engine/UI/Data/SettingsData.cs b/src/RynthCore.Engine/UI/Data/SettingsData.cs index e7f5ce7..e24df27 100644 --- a/src/RynthCore.Engine/UI/Data/SettingsData.cs +++ b/src/RynthCore.Engine/UI/Data/SettingsData.cs @@ -39,6 +39,8 @@ internal sealed class RynthAiSettings public int TargetFPSFocused { get; set; } = 60; public int TargetFPSBackground { get; set; } = 30; public bool EnableAutocram { get; set; } + /// RynthSort's auto-sort is on (RynthAi sends it; never saved): Auto Cram is greyed out and off. + public bool AutoSortOn { get; set; } public bool PeaceModeWhenIdle { get; set; } // RynthAi's Weapons Back After Heals. Nullable: an older RynthAi doesn't send it, and the // engine then sends none back (a false would turn it off). @@ -51,7 +53,7 @@ internal sealed class RynthAiSettings // Decal bridge: stop RynthAi's macro when VTank's starts (one bot per client). Default on; // an older RynthAi doesn't send it, and a missing field must not read as "off". public bool YieldToVTank { get; set; } = true; - public bool EnableRaycasting { get; set; } + public bool EnableRaycasting { get; set; } = true; // RynthAi's own default (2026-11-05) public bool UseArcs { get; set; } public float BowArcVelocity { get; set; } = 25f; public float MissileArcClearance { get; set; } = 0.5f; @@ -305,7 +307,20 @@ internal sealed class RynthAiSettings public bool? MChargesWhenOff { get; set; } public int? CustomPetRange { get; set; } - public RynthAiSettings Clone() => (RynthAiSettings)MemberwiseClone(); + /// + /// The snapshot this copy was cloned from (null on a snapshot itself). + /// sends only the fields that differ from it, so a face holding an old or default copy can change + /// nothing but what the player just edited (2026-11-05: after a hot reload the dashboard held + /// RynthAi's built-in defaults, and one click wrote them all over the character's profile). + /// + [JsonIgnore] public RynthAiSettings? Base { get; private set; } + + public RynthAiSettings Clone() + { + var copy = (RynthAiSettings)MemberwiseClone(); + copy.Base = Base ?? this; + return copy; + } } [JsonSerializable(typeof(RynthAiSettings))] @@ -327,7 +342,14 @@ internal sealed unsafe class SettingsSource : UiSource if (_getSettingsJson == null) return; IntPtr ptr = _getSettingsJson(); string? json = ptr == IntPtr.Zero ? null : Marshal.PtrToStringAnsi(ptr); - if (string.IsNullOrEmpty(json) || json == _lastJson) return; + if (string.IsNullOrEmpty(json) || json == "{}") + { + // RynthAi hasn't loaded the character's profile (it answers nothing until then): drop any + // copy held from before, so no face shows or saves values that aren't the character's. + if (_lastJson != null) { _lastJson = null; ClearSnapshot(); } + return; + } + if (json == _lastJson) return; _lastJson = json; RynthAiSettings? parsed = JsonSerializer.Deserialize(json, RynthAiSettingsJsonContext.Default.RynthAiSettings); if (parsed != null) Publish(parsed); @@ -351,6 +373,32 @@ internal static unsafe class SettingsCommands Plugins.PluginManager.PluginsUnloaded += () => _setSettingsJson = null; } + /// + /// The fields of that differ from the snapshot it was cloned from, as a + /// JSON object (RynthAi lays sent fields over its own and keeps the rest). Null when nothing differs, + /// or when the copy has no snapshot behind it (it was never filled from RynthAi: never sent whole). + /// + internal static string? ChangedFieldsJson(RynthAiSettings settings) + { + RynthAiSettings? baseline = settings.Base; + if (baseline == null) + { + RynthLog.Compat("SettingsCommands: a settings save with no RynthAi snapshot behind it was not sent."); + return null; + } + var now = JsonSerializer.SerializeToNode(settings, RynthAiSettingsJsonContext.Default.RynthAiSettings)?.AsObject(); + var before = JsonSerializer.SerializeToNode(baseline, RynthAiSettingsJsonContext.Default.RynthAiSettings)?.AsObject(); + if (now == null || before == null) return null; + var changed = new System.Text.Json.Nodes.JsonObject(); + foreach (var (name, value) in now) + { + string a = value?.ToJsonString() ?? "null"; + string b = before.TryGetPropertyValue(name, out var old) ? old?.ToJsonString() ?? "null" : "\u0000"; + if (a != b) changed[name] = value?.DeepClone(); + } + return changed.Count == 0 ? null : changed.ToJsonString(); + } + /// /// must be a copy the caller no longer changes /// (it is serialized later, on the pump thread). Any thread. @@ -362,7 +410,8 @@ internal static unsafe class SettingsCommands if (_setSettingsJson == null) _setSettingsJson = (delegate* unmanaged[Cdecl])PluginExportBinder.Resolve("RynthAi", "RynthPluginSetSettingsJson"); if (_setSettingsJson == null) return; - string json = JsonSerializer.Serialize(settings, RynthAiSettingsJsonContext.Default.RynthAiSettings); + string? json = ChangedFieldsJson(settings); + if (json == null) return; // nothing changed, or a copy with no snapshot behind it IntPtr ansi = Marshal.StringToHGlobalAnsi(json); try { _setSettingsJson(ansi); } finally { Marshal.FreeHGlobal(ansi); } @@ -501,8 +550,13 @@ internal sealed record SettingRow( Func? ItemsNow = null, // Button rows that can't be undone: the question a click asks first (asked when it opens); // Click/Edit run only when the player confirms. - Func? Confirm = null) + Func? Confirm = null, + // Bool rows: greyed out, shown off and not clickable while this holds; DisabledTip says why. + Func? DisabledWhen = null, + string? DisabledTip = null) { + public bool IsDisabled(RynthAiSettings s) => DisabledWhen != null && DisabledWhen(s); + public bool IsVisible(RynthAiSettings s) => VisibleWhen == null || VisibleWhen(s); /// A Combo row's list now: ItemsNow's when it has one, else Items. @@ -585,8 +639,13 @@ internal static class SettingsSchema } private static SettingRow Bool(string label, Func get, Action set, - string? tip = null, Func? when = null, bool gates = false) => - new(SettingKind.Bool, label, tip, s => get(s) ? 1 : 0, (s, v) => set(s, v != 0), VisibleWhen: when, Gates: gates); + string? tip = null, Func? when = null, bool gates = false, + Func? disabledWhen = null, string? disabledTip = null) => + new(SettingKind.Bool, label, tip, s => get(s) ? 1 : 0, (s, v) => set(s, v != 0), VisibleWhen: when, Gates: gates, + DisabledWhen: disabledWhen, DisabledTip: disabledTip); + + /// Auto Cram's rows: off and greyed out while RynthSort's auto-sort is on (the two fought, 2026-11-03). + private const string CramOffForSortTip = "Off while RynthSort's auto-sort is on: RynthSort keeps your packs sorted, and the two fight.\nTurn auto-sort off (RynthSort, or the dashboard's Sort key) to use Auto Cram."; private static SettingRow Int(string label, Func get, Action set, int min, int max, int step, string? tip = null, Func? when = null) => @@ -981,7 +1040,8 @@ internal static class SettingsSchema when: _ => Plugins.EngineSettings.HoldReloadInWorld), Spacer(), Bool("Auto Cram", s => s.EnableAutocram, (s, v) => s.EnableAutocram = v, - "Automatically moves items from your main pack into side packs.\nNote: recently used weapons stay in the main pack."), + "Automatically moves items from your main pack into side packs.\nNote: recently used weapons stay in the main pack.", + disabledWhen: s => s.AutoSortOn, disabledTip: CramOffForSortTip), Bool("Peace Mode When Idle", s => s.PeaceModeWhenIdle, (s, v) => s.PeaceModeWhenIdle = v), Bool("Weapons Back After Heals", s => s.RearmAfterVitals == true, (s, v) => s.RearmAfterVitals = v, "After a heal, restam or remana spell with nothing to fight, put the last fight's weapons\n(main hand and off hand) back on instead of waiting for the next monster."), @@ -1446,7 +1506,8 @@ internal static class SettingsSchema Section("Inventory Management"), Bool("Enable Autostack", s => s.EnableAutostack, (s, v) => s.EnableAutostack = v), Bool("Enable Autocram", s => s.EnableAutocram, (s, v) => s.EnableAutocram = v, - "Automatically moves items from your main pack into side packs.\nNote: recently used weapons stay in the main pack."), + "Automatically moves items from your main pack into side packs.\nNote: recently used weapons stay in the main pack.", + disabledWhen: s => s.AutoSortOn, disabledTip: CramOffForSortTip), Bool("Combine Salvage Bags", s => s.EnableCombineSalvage, (s, v) => s.EnableCombineSalvage = v, "After the salvage queue empties, move same-name bags together so the server merges them."), Bool("Combine Bags During Salvage", s => s.CombineBagsDuringSalvage, (s, v) => s.CombineBagsDuringSalvage = v, diff --git a/tools/SettingsSaveTests/Program.cs b/tools/SettingsSaveTests/Program.cs new file mode 100644 index 0000000..df35483 --- /dev/null +++ b/tools/SettingsSaveTests/Program.cs @@ -0,0 +1,83 @@ +using System.Reflection; +using System.Text.Json; +using System.Text.Json.Nodes; +using System.Text.Json.Serialization.Metadata; + +namespace SettingsSaveTests +{ + // A Settings save sends only what the player changed (see the csproj). + internal static class Program + { + private static int _fails, _asserts; + private static void Check(bool cond, string msg) + { + _asserts++; + if (!cond) { _fails++; Console.WriteLine($" [FAIL] {msg}"); } + } + + private const string Ns = "RynthCore.Engine.UI.Data."; + private const BindingFlags All = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance; + + private static int Main() + { + Assembly engine = Assembly.Load("RynthCore.Engine"); + Type settingsType = engine.GetType(Ns + "RynthAiSettings", throwOnError: true)!; + Type ctxType = engine.GetType(Ns + "RynthAiSettingsJsonContext", throwOnError: true)!; + Type commands = engine.GetType(Ns + "SettingsCommands", throwOnError: true)!; + object ctx = ctxType.GetProperty("Default", All)!.GetValue(null)!; + var info = (JsonTypeInfo)ctxType.GetProperty("RynthAiSettings", All)!.GetValue(ctx)!; + MethodInfo changed = commands.GetMethod("ChangedFieldsJson", All)!; + MethodInfo clone = settingsType.GetMethod("Clone", All)!; + + object Parse(string json) => JsonSerializer.Deserialize(json, info)!; + object Clone(object s) => clone.Invoke(s, null)!; + void Set(object s, string name, object value) => settingsType.GetProperty(name, All)!.SetValue(s, value); + JsonObject? Sent(object s) => changed.Invoke(null, new[] { s }) is string j ? JsonNode.Parse(j)!.AsObject() : null; + + // The character's settings as RynthAi sent them (the snapshot). + object snapshot = Parse("{\"StartMacroOnLogin\":true,\"TopOffHP\":95,\"EnableLooting\":true,\"EnableRaycasting\":false,\"RestamTo\":90}"); + + // A face clones it, the player ticks one box: only that box is sent. + object copy = Clone(snapshot); + Set(copy, "EnableLooting", false); + JsonObject? sent = Sent(copy); + Check(sent != null && sent.Count == 1 && sent.ContainsKey("EnableLooting") && sent["EnableLooting"]!.GetValue() == false, + "one edit: only that field is sent (got " + sent?.ToJsonString() + ")"); + + // Faces save a clone of their copy: the base is still the snapshot. + JsonObject? sent2 = Sent(Clone(copy)); + Check(sent2 != null && sent2.Count == 1 && sent2.ContainsKey("EnableLooting"), "a clone of an edited copy diffs against the same snapshot"); + + // Nothing edited: nothing is sent. + Check(Sent(Clone(snapshot)) == null, "an unedited copy sends nothing"); + + // The 2026-11-05 case: the face's copy came from a default snapshot (RynthAi not ready yet); one + // edit must not carry every other default with it. + object defaults = Parse("{}"); + object staleCopy = Clone(defaults); + Set(staleCopy, "TopOffHP", 80); + JsonObject? sent3 = Sent(staleCopy); + Check(sent3 != null && sent3.Count == 1 && sent3.ContainsKey("TopOffHP"), + "a copy of a default snapshot sends only the edited field, never the defaults (got " + sent3?.ToJsonString() + ")"); + + // A copy that never came from a snapshot is never sent at all. + object orphan = Activator.CreateInstance(settingsType, nonPublic: true)!; + Set(orphan, "TopOffHP", 80); + Check(Sent(orphan) == null, "a settings object with no snapshot behind it is not sent"); + + // Two edits: both, and nothing else. + object two = Clone(snapshot); + Set(two, "RestamTo", 70); + Set(two, "StartMacroOnLogin", false); + JsonObject? sent4 = Sent(two); + Check(sent4 != null && sent4.Count == 2 && sent4.ContainsKey("RestamTo") && sent4.ContainsKey("StartMacroOnLogin"), "two edits: those two"); + + // Raycasting is on unless RynthAi says otherwise. + object fresh = Activator.CreateInstance(settingsType, nonPublic: true)!; + Check((bool)settingsType.GetProperty("EnableRaycasting", All)!.GetValue(fresh)! == true, "the engine's raycasting default is on"); + + Console.WriteLine(_fails == 0 ? $"PASS ({_asserts} checks)" : $"FAIL ({_fails} of {_asserts})"); + return _fails == 0 ? 0 : 1; + } + } +} diff --git a/tools/SettingsSaveTests/SettingsSaveTests.csproj b/tools/SettingsSaveTests/SettingsSaveTests.csproj new file mode 100644 index 0000000..07b50ca --- /dev/null +++ b/tools/SettingsSaveTests/SettingsSaveTests.csproj @@ -0,0 +1,28 @@ + + + + + Exe + net10.0-windows + x86 + win-x86 + false + x86 + enable + enable + true + SettingsSaveTests + + + + + + +