Add call-on-services-ready attribute for service methods, and fix scene nullability (#900)

This commit is contained in:
kizer 2022-06-27 01:49:34 +09:00 committed by GitHub
parent 5809accf5d
commit 3369f569fb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
52 changed files with 274 additions and 273 deletions

View file

@ -22,7 +22,7 @@ namespace Dalamud.Game.Gui
[PluginInterface]
[InterfaceVersion("1.0")]
[ServiceManager.BlockingEarlyLoadedService]
public sealed class ChatGui : IDisposable
public sealed class ChatGui : IDisposable, IServiceType
{
private readonly ChatGuiAddressResolver address;
@ -123,16 +123,6 @@ namespace Dalamud.Game.Gui
/// </summary>
public byte LastLinkedItemFlags { get; private set; }
/// <summary>
/// Enables this module.
/// </summary>
public void Enable()
{
this.printMessageHook.Enable();
this.populateItemLinkHook.Enable();
this.interactableLinkClickedHook.Enable();
}
/// <summary>
/// Dispose of managed and unmanaged resources.
/// </summary>
@ -280,6 +270,14 @@ namespace Dalamud.Game.Gui
}
}
[ServiceManager.CallWhenServicesReady]
private void ContinueConstruction(GameGui gameGui, LibcFunction libcFunction)
{
this.printMessageHook.Enable();
this.populateItemLinkHook.Enable();
this.interactableLinkClickedHook.Enable();
}
private void HandlePopulateItemLinkDetour(IntPtr linkObjectPtr, IntPtr itemInfoPtr)
{
try

View file

@ -2,7 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using Dalamud.Configuration.Internal;
using Dalamud.Game.Gui.ContextMenus.OldStructs;
using Dalamud.Hooking;
using Dalamud.IoC;
@ -25,7 +25,7 @@ namespace Dalamud.Game.Gui.ContextMenus
[PluginInterface]
[InterfaceVersion("1.0")]
[ServiceManager.BlockingEarlyLoadedService]
public sealed class ContextMenu : IDisposable
public sealed class ContextMenu : IDisposable, IServiceType
{
private const int MaxContextMenuItemsPerContextMenu = 32;
@ -97,18 +97,6 @@ namespace Dalamud.Game.Gui.ContextMenus
this.contextMenuOpeningHook.Disable();
}
/// <summary>
/// Enable this subsystem.
/// </summary>
internal void Enable()
{
this.contextMenuOpeningHook.Enable();
this.contextMenuOpenedHook.Enable();
this.contextMenuItemSelectedHook.Enable();
this.subContextMenuOpeningHook.Enable();
this.subContextMenuOpenedHook.Enable();
}
private static unsafe bool IsInventoryContext(OldAgentContextInterface* agentContextInterface)
{
return agentContextInterface == AgentInventoryContext.Instance();
@ -122,6 +110,19 @@ namespace Dalamud.Game.Gui.ContextMenus
}
}
[ServiceManager.CallWhenServicesReady]
private void ContinueConstruction(GameGui gameGui)
{
if (!EnvironmentConfiguration.DalamudDoContextMenu)
return;
this.contextMenuOpeningHook.Enable();
this.contextMenuOpenedHook.Enable();
this.contextMenuItemSelectedHook.Enable();
this.subContextMenuOpeningHook.Enable();
this.subContextMenuOpenedHook.Enable();
}
private unsafe IntPtr ContextMenuOpeningDetour(IntPtr a1, IntPtr a2, IntPtr a3, uint a4, IntPtr a5, OldAgentContextInterface* agentContextInterface, IntPtr a7, ushort a8)
{
this.currentAgentContextInterface = agentContextInterface;

View file

@ -18,7 +18,7 @@ namespace Dalamud.Game.Gui.Dtr
[PluginInterface]
[InterfaceVersion("1.0")]
[ServiceManager.BlockingEarlyLoadedService]
public sealed unsafe class DtrBar : IDisposable
public sealed unsafe class DtrBar : IDisposable, IServiceType
{
private const uint BaseNodeId = 1000;

View file

@ -17,7 +17,7 @@ namespace Dalamud.Game.Gui.FlyText
[PluginInterface]
[InterfaceVersion("1.0")]
[ServiceManager.BlockingEarlyLoadedService]
public sealed class FlyTextGui : IDisposable
public sealed class FlyTextGui : IDisposable, IServiceType
{
/// <summary>
/// The native function responsible for adding fly text to the UI. See <see cref="FlyTextGuiAddressResolver.AddFlyText"/>.
@ -129,7 +129,10 @@ namespace Dalamud.Game.Gui.FlyText
var strOffset = 28u;
// Get the UI module and flytext addon pointers
var gameGui = Service<GameGui>.Get();
var gameGui = Service<GameGui>.GetNullable();
if (gameGui == null)
return;
var ui = (FFXIVClientStructs.FFXIV.Client.UI.UIModule*)gameGui.GetUIModule();
var flytext = gameGui.GetAddonByName("_FlyText", 1);
@ -174,10 +177,8 @@ namespace Dalamud.Game.Gui.FlyText
}
}
/// <summary>
/// Enables this module.
/// </summary>
internal void Enable()
[ServiceManager.CallWhenServicesReady]
private void ContinueConstruction(GameGui gameGui)
{
this.createFlyTextHook.Enable();
}

View file

@ -28,7 +28,7 @@ namespace Dalamud.Game.Gui
[PluginInterface]
[InterfaceVersion("1.0")]
[ServiceManager.BlockingEarlyLoadedService]
public sealed unsafe class GameGui : IDisposable
public sealed unsafe class GameGui : IDisposable, IServiceType
{
private readonly GameGuiAddressResolver address;
@ -418,19 +418,9 @@ namespace Dalamud.Game.Gui
/// <param name="bgmKey">The background music key.</param>
public void SetBgm(ushort bgmKey) => this.setGlobalBgmHook.Original(bgmKey, 0, 0, 0, 0, 0);
/// <summary>
/// Enables the hooks and submodules of this module.
/// </summary>
public void Enable()
[ServiceManager.CallWhenServicesReady]
private void ContinueConstruction()
{
Service<ChatGui>.GetAsync().ContinueWith(x => x.Result.Enable());
Service<ToastGui>.GetAsync().ContinueWith(x => x.Result.Enable());
Service<FlyTextGui>.GetAsync().ContinueWith(x => x.Result.Enable());
Service<PartyFinderGui>.GetAsync().ContinueWith(x => x.Result.Enable());
if (EnvironmentConfiguration.DalamudDoContextMenu)
Service<ContextMenu>.GetAsync().ContinueWith(x => x.Result.Enable());
this.setGlobalBgmHook.Enable();
this.handleItemHoverHook.Enable();
this.handleItemOutHook.Enable();

View file

@ -19,7 +19,7 @@ namespace Dalamud.Game.Gui.Internal
/// This class handles IME for non-English users.
/// </summary>
[ServiceManager.EarlyLoadedService]
internal unsafe class DalamudIME : IDisposable
internal unsafe class DalamudIME : IDisposable, IServiceType
{
private static readonly ModuleLog Log = new("IME");
@ -83,15 +83,13 @@ namespace Dalamud.Game.Gui.Internal
return new Vector2(this.cursorPos->X, this.cursorPos->Y);
}
/// <summary>
/// Enables the IME module.
/// </summary>
internal void Enable()
[ServiceManager.CallWhenServicesReady]
private void ContinueConstruction(InterfaceManager.InterfaceManagerWithScene interfaceManagerWithScene)
{
try
{
this.wndProcDelegate = this.WndProcDetour;
this.interfaceHandle = Service<InterfaceManager>.Get().WindowHandlePtr;
this.interfaceHandle = interfaceManagerWithScene.Manager.WindowHandlePtr;
this.wndProcPtr = Marshal.GetFunctionPointerForDelegate(this.wndProcDelegate);
this.oldWndProcPtr = SetWindowLongPtrW(this.interfaceHandle, WindowLongType.WndProc, this.wndProcPtr);

View file

@ -16,7 +16,7 @@ namespace Dalamud.Game.Gui.PartyFinder
[PluginInterface]
[InterfaceVersion("1.0")]
[ServiceManager.BlockingEarlyLoadedService]
public sealed class PartyFinderGui : IDisposable
public sealed class PartyFinderGui : IDisposable, IServiceType
{
private readonly PartyFinderAddressResolver address;
private readonly IntPtr memory;
@ -55,14 +55,6 @@ namespace Dalamud.Game.Gui.PartyFinder
/// </summary>
public event PartyFinderListingEventDelegate ReceiveListing;
/// <summary>
/// Enables this module.
/// </summary>
public void Enable()
{
this.receiveListingHook.Enable();
}
/// <summary>
/// Dispose of managed and unmanaged resources.
/// </summary>
@ -80,6 +72,12 @@ namespace Dalamud.Game.Gui.PartyFinder
}
}
[ServiceManager.CallWhenServicesReady]
private void ContinueConstruction(GameGui gameGui)
{
this.receiveListingHook.Enable();
}
private void HandleReceiveListingDetour(IntPtr managerPtr, IntPtr data)
{
try

View file

@ -15,7 +15,7 @@ namespace Dalamud.Game.Gui.Toast
[PluginInterface]
[InterfaceVersion("1.0")]
[ServiceManager.BlockingEarlyLoadedService]
public sealed partial class ToastGui : IDisposable
public sealed partial class ToastGui : IDisposable, IServiceType
{
private const uint QuestToastCheckmarkMagic = 60081;
@ -100,16 +100,6 @@ namespace Dalamud.Game.Gui.Toast
#endregion
/// <summary>
/// Enables this module.
/// </summary>
public void Enable()
{
this.showNormalToastHook.Enable();
this.showQuestToastHook.Enable();
this.showErrorToastHook.Enable();
}
/// <summary>
/// Disposes of managed and unmanaged resources.
/// </summary>
@ -153,6 +143,14 @@ namespace Dalamud.Game.Gui.Toast
return terminated;
}
[ServiceManager.CallWhenServicesReady]
private void ContinueConstruction(GameGui gameGui)
{
this.showNormalToastHook.Enable();
this.showQuestToastHook.Enable();
this.showErrorToastHook.Enable();
}
private SeString ParseString(IntPtr text)
{
var bytes = new List<byte>();
@ -202,7 +200,9 @@ namespace Dalamud.Game.Gui.Toast
{
options ??= new ToastOptions();
var manager = Service<GameGui>.Get().GetUIModule();
var manager = Service<GameGui>.GetNullable()?.GetUIModule();
if (manager == null)
return;
// terminate the string
var terminated = Terminate(bytes);
@ -211,7 +211,7 @@ namespace Dalamud.Game.Gui.Toast
{
fixed (byte* ptr = terminated)
{
this.HandleNormalToastDetour(manager, (IntPtr)ptr, 5, (byte)options.Position, (byte)options.Speed, 0);
this.HandleNormalToastDetour(manager!.Value, (IntPtr)ptr, 5, (byte)options.Position, (byte)options.Speed, 0);
}
}
}
@ -283,7 +283,9 @@ namespace Dalamud.Game.Gui.Toast
{
options ??= new QuestToastOptions();
var manager = Service<GameGui>.Get().GetUIModule();
var manager = Service<GameGui>.GetNullable()?.GetUIModule();
if (manager == null)
return;
// terminate the string
var terminated = Terminate(bytes);
@ -295,7 +297,7 @@ namespace Dalamud.Game.Gui.Toast
fixed (byte* ptr = terminated)
{
this.HandleQuestToastDetour(
manager,
manager!.Value,
(int)options.Position,
(IntPtr)ptr,
ioc1,
@ -385,7 +387,9 @@ namespace Dalamud.Game.Gui.Toast
private void ShowError(byte[] bytes)
{
var manager = Service<GameGui>.Get().GetUIModule();
var manager = Service<GameGui>.GetNullable()?.GetUIModule();
if (manager == null)
return;
// terminate the string
var terminated = Terminate(bytes);
@ -394,7 +398,7 @@ namespace Dalamud.Game.Gui.Toast
{
fixed (byte* ptr = terminated)
{
this.HandleErrorToastDetour(manager, (IntPtr)ptr, 0);
this.HandleErrorToastDetour(manager!.Value, (IntPtr)ptr, 0);
}
}
}