-
Notifications
You must be signed in to change notification settings - Fork 85
Add Instant Mode checkbox to settings UI #21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,166 @@ | ||
| using System; | ||
| using System.Reflection; | ||
| using Godot; | ||
| using HarmonyLib; | ||
| using MegaCrit.Sts2.addons.mega_text; | ||
| using MegaCrit.Sts2.Core.HoverTips; | ||
| using MegaCrit.Sts2.Core.Nodes.HoverTips; | ||
| using MegaCrit.Sts2.Core.Nodes.Screens.Settings; | ||
| using MegaCrit.Sts2.Core.Saves; | ||
| using MegaCrit.Sts2.Core.Settings; | ||
|
|
||
| namespace STS2_MCP; | ||
|
|
||
| public static partial class McpMod | ||
| { | ||
| private static NFastModeTickbox? _instantModeTickbox; | ||
| private static NFastModeTickbox? _originalFastModeTickbox; | ||
|
|
||
| private static bool IsInInstantModeLine(Node node) | ||
| { | ||
| var current = node; | ||
| while (current != null) | ||
| { | ||
| if (current.Name == "InstantMode") return true; | ||
| current = current.GetParent(); | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| private static T? FindChildOfType<T>(Node parent) where T : class | ||
| { | ||
| foreach (var child in parent.GetChildren()) | ||
| { | ||
| if (child is T typed) return typed; | ||
| if (child is Node childNode) | ||
| { | ||
| var found = FindChildOfType<T>(childNode); | ||
| if (found != null) return found; | ||
| } | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| private static void InjectInstantModeCheckbox(NSettingsScreen settingsScreen) | ||
| { | ||
| _instantModeTickbox = null; | ||
| _originalFastModeTickbox = null; | ||
|
|
||
| var content = settingsScreen.GetNode<NSettingsPanel>("%GeneralSettings").Content; | ||
| var fastModeLine = content.GetNodeOrNull("FastMode"); | ||
| if (fastModeLine == null) | ||
| { | ||
| GD.PrintErr("[STS2 MCP] Could not find FastMode settings line"); | ||
| return; | ||
| } | ||
|
|
||
| _originalFastModeTickbox = FindChildOfType<NFastModeTickbox>(fastModeLine); | ||
|
|
||
| // Duplicate the entire FastMode line and rename | ||
| var instantLine = (Node)fastModeLine.Duplicate(); | ||
| instantLine.Name = "InstantMode"; | ||
|
|
||
| // Insert right after FastMode in the VBoxContainer | ||
| int idx = fastModeLine.GetIndex(); | ||
| content.AddChild(instantLine); | ||
| content.MoveChild(instantLine, idx + 1); | ||
|
|
||
| // Update label text | ||
| var label = instantLine.GetNodeOrNull<MegaRichTextLabel>("Label"); | ||
| if (label != null) label.Text = "Instant Mode"; | ||
|
|
||
| // Store reference and correct initial tick state | ||
| // (NFastModeTickbox._Ready() already ran via AddChild, but used original SetFromSettings logic) | ||
| _instantModeTickbox = FindChildOfType<NFastModeTickbox>(instantLine); | ||
| if (_instantModeTickbox != null) | ||
| _instantModeTickbox.IsTicked = SaveManager.Instance.PrefsSave.FastMode == FastModeType.Instant; | ||
|
|
||
| GD.Print("[STS2 MCP] Instant Mode checkbox injected into settings"); | ||
| } | ||
|
|
||
| // --- Harmony Patches --- | ||
|
|
||
| [HarmonyPatch(typeof(NSettingsScreen), "_Ready")] | ||
| static class SettingsScreenReadyPatch | ||
| { | ||
| static void Postfix(NSettingsScreen __instance) | ||
| { | ||
| try { InjectInstantModeCheckbox(__instance); } | ||
| catch (Exception ex) { GD.PrintErr($"[STS2 MCP] Settings UI injection failed: {ex}"); } | ||
| } | ||
| } | ||
|
|
||
| [HarmonyPatch(typeof(NFastModeTickbox), nameof(NFastModeTickbox.SetFromSettings))] | ||
| static class FastModeSetFromSettingsPatch | ||
| { | ||
| static bool Prefix(NFastModeTickbox __instance) | ||
| { | ||
| if (!IsInInstantModeLine(__instance)) return true; | ||
| __instance.IsTicked = SaveManager.Instance.PrefsSave.FastMode == FastModeType.Instant; | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| [HarmonyPatch(typeof(NFastModeTickbox), "OnTick")] | ||
| static class FastModeOnTickPatch | ||
| { | ||
| static bool Prefix(NFastModeTickbox __instance) | ||
| { | ||
| if (!IsInInstantModeLine(__instance)) return true; | ||
| // Instant Mode turned on | ||
| SaveManager.Instance.PrefsSave.FastMode = FastModeType.Instant; | ||
| // Ensure the original Fast Mode checkbox also shows ticked | ||
| if (_originalFastModeTickbox != null && !_originalFastModeTickbox.IsTicked) | ||
| _originalFastModeTickbox.IsTicked = true; | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| [HarmonyPatch(typeof(NFastModeTickbox), "OnUntick")] | ||
| static class FastModeOnUntickPatch | ||
| { | ||
| static bool Prefix(NFastModeTickbox __instance) | ||
| { | ||
| if (!IsInInstantModeLine(__instance)) return true; | ||
| // Instant Mode turned off — fall back to Fast | ||
| SaveManager.Instance.PrefsSave.FastMode = FastModeType.Fast; | ||
| return false; | ||
| } | ||
|
|
||
| static void Postfix(NFastModeTickbox __instance) | ||
| { | ||
| // Only for the ORIGINAL Fast Mode tickbox being unticked (→ Normal) | ||
| if (IsInInstantModeLine(__instance)) return; | ||
| if (_instantModeTickbox != null && _instantModeTickbox.IsTicked) | ||
| _instantModeTickbox.IsTicked = false; | ||
| } | ||
| } | ||
|
|
||
| [HarmonyPatch(typeof(NFastModeHoverTip), "_Ready")] | ||
| static class InstantModeHoverTipPatch | ||
| { | ||
| static void Postfix(NFastModeHoverTip __instance) | ||
| { | ||
| if (!IsInInstantModeLine(__instance)) return; | ||
|
|
||
| // Build a HoverTip with custom strings via reflection | ||
| // (HoverTip is a record struct; Title/Description have private setters) | ||
| object boxed = (object)default(HoverTip); | ||
| var ht = typeof(HoverTip); | ||
| ht.GetProperty("Id")!.SetValue(boxed, "sts2mcp_instant_mode"); | ||
| ht.GetProperty("Title")!.GetSetMethod(true)! | ||
| .Invoke(boxed, ["Instant Mode"]); | ||
| ht.GetProperty("Description")!.GetSetMethod(true)! | ||
| .Invoke(boxed, [ | ||
| "This option is added by the STS2MCP mod.\n" + | ||
| "When enabled, most in-game animations will become instant.\n" + | ||
| "Can reduce token usage as get-state calls will no longer receive an intermediate state." | ||
| ]); | ||
|
|
||
| // Replace the _hoverTip field set by the original _Ready | ||
| typeof(NFastModeHoverTip) | ||
| .GetField("_hoverTip", BindingFlags.NonPublic | BindingFlags.Instance) | ||
| ?.SetValue(__instance, (IHoverTip)boxed); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Harmony.PatchAll()is executed inside the sametryblock as the server startup. If patching fails (e.g., due to a game update changing method signatures), the catch will prevent the HTTP listener/thread from starting at all. Consider isolating patch application in its own try/catch (logging and continuing), so optional UI patches can’t take down the core server functionality.