From feb8edb6c974d2bc959628b24470241ba19f6f8e Mon Sep 17 00:00:00 2001 From: goat Date: Sun, 12 Jan 2020 16:00:31 +0900 Subject: [PATCH] feat: add plugin API for interfaces --- Dalamud.Injector/Dalamud.Injector.csproj | 4 +-- Dalamud.Injector/Program.cs | 2 ++ Dalamud/Dalamud.cs | 44 +++++++++++++++++++++++- Dalamud/Interface/InterfaceManager.cs | 20 +++++++---- Dalamud/Plugin/DalamudPluginInterface.cs | 10 ++++++ 5 files changed, 71 insertions(+), 9 deletions(-) diff --git a/Dalamud.Injector/Dalamud.Injector.csproj b/Dalamud.Injector/Dalamud.Injector.csproj index 0ac4cccf8..b8f07fcab 100644 --- a/Dalamud.Injector/Dalamud.Injector.csproj +++ b/Dalamud.Injector/Dalamud.Injector.csproj @@ -1,4 +1,4 @@ - + AnyCPU net48 @@ -23,7 +23,7 @@ - + diff --git a/Dalamud.Injector/Program.cs b/Dalamud.Injector/Program.cs index db9ceb692..8610ac7c2 100644 --- a/Dalamud.Injector/Program.cs +++ b/Dalamud.Injector/Program.cs @@ -4,6 +4,7 @@ using System.Diagnostics; using System.IO; using System.Linq; using System.Text; +using System.Threading; using System.Windows.Forms; using Dalamud.DiscordBot; using Dalamud.Game.Chat; @@ -34,6 +35,7 @@ namespace Dalamud.Injector { process = Process.Start( "C:\\Program Files (x86)\\SquareEnix\\FINAL FANTASY XIV - A Realm Reborn\\game\\ffxiv_dx11.exe", "DEV.TestSID=0 DEV.UseSqPack=1 DEV.DataPathType=1 DEV.LobbyHost01=127.0.0.1 DEV.LobbyPort01=54994 DEV.LobbyHost02=127.0.0.1 DEV.LobbyPort02=54994 DEV.LobbyHost03=127.0.0.1 DEV.LobbyPort03=54994 DEV.LobbyHost04=127.0.0.1 DEV.LobbyPort04=54994 DEV.LobbyHost05=127.0.0.1 DEV.LobbyPort05=54994 DEV.LobbyHost06=127.0.0.1 DEV.LobbyPort06=54994 DEV.LobbyHost07=127.0.0.1 DEV.LobbyPort07=54994 DEV.LobbyHost08=127.0.0.1 DEV.LobbyPort08=54994 SYS.Region=0 language=1 version=1.0.0.0 DEV.MaxEntitledExpansionID=2 DEV.GMServerHost=127.0.0.1 DEV.GameQuitMessageBox=0"); + Thread.Sleep(1000); break; default: process = Process.GetProcessById(pid); diff --git a/Dalamud/Dalamud.cs b/Dalamud/Dalamud.cs index 02a3c6542..91ffada34 100644 --- a/Dalamud/Dalamud.cs +++ b/Dalamud/Dalamud.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; +using System.Reflection; using System.Text; using System.Threading; using System.Threading.Tasks; @@ -18,6 +19,7 @@ using Dalamud.Game.Internal.Gui; using Dalamud.Game.Network; using Dalamud.Interface; using Dalamud.Plugin; +using ImGuiNET; using Serilog; using XIVLauncher.Dalamud; @@ -52,6 +54,8 @@ namespace Dalamud { public readonly InterfaceManager InterfaceManager; + private readonly string assemblyVersion = Assembly.GetAssembly(typeof(ChatHandlers)).GetName().Version.ToString(); + public Dalamud(DalamudStartInfo info) { this.StartInfo = info; this.Configuration = DalamudConfiguration.Load(info.ConfigurationPath); @@ -85,7 +89,8 @@ namespace Dalamud { this.WinSock2 = new WinSockHandlers(); this.InterfaceManager = new InterfaceManager(this.sigScanner); - //this.InterfaceManager.Start(); + this.InterfaceManager.ReadyToDraw += (sender, args) => this.InterfaceManager.OnBuildUi += BuildDalamudUi; + this.InterfaceManager.Enable(); try { this.PluginManager.LoadPlugins(); @@ -96,6 +101,34 @@ namespace Dalamud { } } + private bool isImguiDrawDemoWindow = true; + private bool isImguiDrawWelcome = true; + + private bool neverDrawWelcome = false; + + private void BuildDalamudUi() { + if (this.isImguiDrawDemoWindow) + ImGui.ShowDemoWindow(); + + if (this.isImguiDrawWelcome) { + if (!ImGui.Begin("Welcome to XIVLauncher", ImGuiWindowFlags.Modal | ImGuiWindowFlags.NoCollapse | ImGuiWindowFlags.NoResize)) + { + // Early out if the window is collapsed, as an optimization. + ImGui.End(); + } else { + ImGui.Text($"dalamud says hello. ({this.assemblyVersion})"); + ImGui.Checkbox("Don't show this message again", ref this.neverDrawWelcome); + ImGui.Spacing(); + + if (ImGui.Button("Close")) { + this.isImguiDrawWelcome = false; + } + + ImGui.End(); + } + } + } + public void Start() { Framework.Enable(); @@ -198,6 +231,11 @@ namespace Dalamud { { HelpMessage = "Notify when a roulette has a bonus you specified. Run without parameters for more info. Usage: /xlbonus " }); + + CommandManager.AddHandler("/xlduidrawdemo", new CommandInfo(OnDebugDrawImGuiDemo) { + HelpMessage = "Open ImGUI demo window", + ShowInHelp = false + }); } private void OnUnloadCommand(string command, string arguments) { @@ -483,6 +521,10 @@ namespace Dalamud { "Possible values for role: tank, dps, healer, all, none/reset"); } + private void OnDebugDrawImGuiDemo(string command, string arguments) { + this.isImguiDrawDemoWindow = true; + } + private int RouletteSlugToKey(string slug) => slug.ToLower() switch { "leveling" => 1, "506070" => 2, diff --git a/Dalamud/Interface/InterfaceManager.cs b/Dalamud/Interface/InterfaceManager.cs index c33970dc1..96c49d856 100644 --- a/Dalamud/Interface/InterfaceManager.cs +++ b/Dalamud/Interface/InterfaceManager.cs @@ -48,9 +48,19 @@ namespace Dalamud.Interface private SwapChainAddressResolver Address { get; } - private RawDX11Scene scene; + /// + /// This event gets called when ImGUI is ready to draw your UI. + /// + public event RawDX11Scene.BuildUIDelegate OnBuildUi + { + add => this.scene.OnBuildUI += value; + remove => this.scene.OnBuildUI -= value; + } + + public EventHandler ReadyToDraw; + public InterfaceManager(SigScanner scanner) { Address = new SwapChainAddressResolver(); @@ -63,7 +73,6 @@ namespace Dalamud.Interface new Hook(Address.Present, new PresentDelegate(PresentDetour), this); - Enable(); } public void Enable() @@ -96,7 +105,8 @@ namespace Dalamud.Interface #else this.scene = new RawDX11Scene(swapChain); #endif - this.scene.OnBuildUI += DrawUI; + this.scene.OnBuildUI += HandleMouseUI; + this.ReadyToDraw?.Invoke(this, null); } this.scene.Render(); @@ -104,7 +114,7 @@ namespace Dalamud.Interface return this.presentHook.Original(swapChain, syncInterval, presentFlags); } - private void DrawUI() + private void HandleMouseUI() { // this is more or less part of what reshade/etc do to avoid having to manually // set the cursor inside the ui @@ -112,8 +122,6 @@ namespace Dalamud.Interface // the normal one from the game, and the one for ImGui // Doing this here because it's somewhat application-specific behavior ImGui.GetIO().MouseDrawCursor = ImGui.GetIO().WantCaptureMouse; - - ImGui.ShowDemoWindow(); } } } diff --git a/Dalamud/Plugin/DalamudPluginInterface.cs b/Dalamud/Plugin/DalamudPluginInterface.cs index 38be7d39c..c633d9485 100644 --- a/Dalamud/Plugin/DalamudPluginInterface.cs +++ b/Dalamud/Plugin/DalamudPluginInterface.cs @@ -10,6 +10,7 @@ using Dalamud.Game.ClientState; using Dalamud.Game.Command; using Dalamud.Game.Internal; using Dalamud.Game.Internal.Gui; +using Dalamud.Interface; namespace Dalamud.Plugin { @@ -32,6 +33,11 @@ namespace Dalamud.Plugin /// public readonly Framework Framework; + /// + /// The InterfaceManager object which allows you to draw UI into the game via ImGui draw calls. + /// + public readonly InterfaceManager Interface; + private readonly Dalamud dalamud; private readonly string pluginName; @@ -43,6 +49,7 @@ namespace Dalamud.Plugin this.CommandManager = dalamud.CommandManager; this.Framework = dalamud.Framework; this.ClientState = dalamud.ClientState; + this.Interface = dalamud.InterfaceManager; this.dalamud = dalamud; this.pluginName = pluginName; @@ -61,6 +68,9 @@ namespace Dalamud.Plugin return; } + if (currentConfig == null) + return; + this.dalamud.Configuration.PluginConfigurations.Add(this.pluginName, currentConfig); this.dalamud.Configuration.Save(this.dalamud.StartInfo.ConfigurationPath); }