diff --git a/OemLoader/MainWindow.cs b/OemLoader/MainWindow.cs new file mode 100644 index 0000000..9f1796d --- /dev/null +++ b/OemLoader/MainWindow.cs @@ -0,0 +1,19 @@ +using Brutal.ImGuiApi; + +namespace OemLoader; + +public class MainWindow(string title) +{ + private readonly ImInputString _filename = new(short.MaxValue); + + public void Render(double dt) + { + if (!ProgramPatch.WindowOpen) return; + + if (ImGui.Begin(title, ref ProgramPatch.WindowOpen)) + { + ImGui.InputTextWithHint("OEM file path", "/home/user/ephemeris.asc", _filename); + } + ImGui.End(); + } +} \ No newline at end of file diff --git a/OemLoader/OemLoader.cs b/OemLoader/OemLoader.cs index 4b4f551..a4641b2 100644 --- a/OemLoader/OemLoader.cs +++ b/OemLoader/OemLoader.cs @@ -1,8 +1,8 @@ using System.Diagnostics; using System.Reflection; +using HarmonyLib; using JetBrains.Annotations; using KSA; -using Brutal.ImGuiApi; using StarMap.API; namespace OemLoader; @@ -12,14 +12,17 @@ namespace OemLoader; public class OemLoader { private readonly string _version; + private readonly MainWindow _window; + private static Harmony? _harmony = new("OemLoader"); public OemLoader() { - var version = Assembly.GetExecutingAssembly() + var version = typeof(OemLoader).Assembly .GetCustomAttribute()? .InformationalVersion; Debug.Assert(version is not null); _version = version; + _window = new MainWindow($"OemLoader {_version}"); } [UsedImplicitly] @@ -33,10 +36,21 @@ public class OemLoader [StarMapAfterGui] public void AfterGui(double dt) { - if (ImGui.Begin($"OemLoader {_version}")) - { - ImGui.Text("Hello World!"); - } - ImGui.End(); + _window.Render(dt); + } + + [UsedImplicitly] + [StarMapAllModsLoaded] + public void FullyLoaded() + { + _harmony?.PatchAll(typeof(OemLoader).Assembly); + } + + [UsedImplicitly] + [StarMapUnload] + public void Unload() + { + _harmony?.UnpatchAll("OemLoader"); + _harmony = null; } } \ No newline at end of file diff --git a/OemLoader/ProgramPatch.cs b/OemLoader/ProgramPatch.cs new file mode 100644 index 0000000..5514b93 --- /dev/null +++ b/OemLoader/ProgramPatch.cs @@ -0,0 +1,41 @@ +using System.Reflection.Emit; +using HarmonyLib; +using JetBrains.Annotations; +using KSA; + +namespace OemLoader; + +[UsedImplicitly] +[HarmonyPatch(typeof(Program))] +internal static class ProgramPatch +{ + public static bool WindowOpen; + + private static void DrawMenuItem() + { + ImGuiHelper.DrawMenuItem("Ephemeris loader", ref WindowOpen); + } + + [UsedImplicitly] + [HarmonyPatch(nameof(Program.DrawMenuBar))] + [HarmonyTranspiler] + private static IEnumerable DrawMenuBarTranspiler(IEnumerable instructions, ILGenerator generator) + { + // Find the menu item that we want to add ours after + var matcher = new CodeMatcher(instructions, generator); + matcher.MatchEndForward( + CodeMatch.Calls(() => Staging.Toggle()), + new CodeMatch(OpCodes.Nop) + ); + matcher.ThrowIfInvalid("Could not find staging menu entry"); + matcher.Advance(); // the current instruction at this point is just a nop + // We want to inject our code before the current instruction, but it should be the target of + // the label that was previously on the current instruction, so we need to swap around the labels + var labels = new List