Let DI handle lifetime and make Plugin Service.

This commit is contained in:
Ottermandias 2023-06-03 00:01:01 +02:00
parent bd8da4bebf
commit 07a92ba025
3 changed files with 15 additions and 24 deletions

View file

@ -151,9 +151,6 @@ internal sealed class Dalamud : IServiceType
// will not receive any windows messages
Service<DalamudIME>.GetNullable()?.Dispose();
// this must be done before unloading interface manager, since it relies on the window handle members.
Service<DragDropManager>.GetNullable()?.Dispose();
// this must be done before unloading plugins, or it can cause a race condition
// due to rendering happening on another thread, where a plugin might receive
// a render call after it has been disposed, which can crash if it attempts to

View file

@ -1,8 +1,10 @@
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using Dalamud.Interface.Internal;
using Dalamud.IoC;
using Dalamud.IoC.Internal;
using ImGuiNET;
using Serilog;
@ -12,22 +14,19 @@ namespace Dalamud.Interface.DragDrop;
/// A manager that keeps state of external windows drag and drop events,
/// and can be used to create ImGui drag and drop sources and targets for those external events.
/// </summary>
[PluginInterface]
[ServiceManager.EarlyLoadedService]
[InherentDependency<InterfaceManager.InterfaceManagerWithScene>]
internal partial class DragDropManager : IDisposable, IDragDropManager, IServiceType
{
private InterfaceManager? interfaceManager;
private readonly InterfaceManager interfaceManager = Service<InterfaceManager>.Get();
private int lastDropFrame = -2;
private int lastTooltipFrame = -1;
[ServiceManager.ServiceConstructor]
private DragDropManager()
{
Service<InterfaceManager.InterfaceManagerWithScene>.GetAsync().ContinueWith(task =>
{
this.interfaceManager = task.Result.Manager;
this.Enable();
});
}
=> this.Enable();
/// <summary> Gets a value indicating whether external drag and drop is available at all. </summary>
public bool ServiceAvailable { get; private set; }
@ -51,16 +50,16 @@ internal partial class DragDropManager : IDisposable, IDragDropManager, IService
/// <summary> Enable external drag and drop. </summary>
public void Enable()
{
if (this.ServiceAvailable || this.interfaceManager == null)
if (this.ServiceAvailable)
{
return;
}
try
{
var ret2 = DragDropInterop.RegisterDragDrop(this.interfaceManager.WindowHandlePtr, this);
Log.Information($"[DragDrop] Registered window {this.interfaceManager.WindowHandlePtr} for external drag and drop operations. ({ret2})");
Marshal.ThrowExceptionForHR(ret2);
var ret = DragDropInterop.RegisterDragDrop(this.interfaceManager.WindowHandlePtr, this);
Log.Information($"[DragDrop] Registered window {this.interfaceManager.WindowHandlePtr} for external drag and drop operations. ({ret})");
Marshal.ThrowExceptionForHR(ret);
this.ServiceAvailable = true;
}
catch (Exception ex)
@ -79,8 +78,9 @@ internal partial class DragDropManager : IDisposable, IDragDropManager, IService
try
{
DragDropInterop.RevokeDragDrop(this.interfaceManager!.WindowHandlePtr);
Log.Information($"[DragDrop] Disabled external drag and drop operations for window {this.interfaceManager.WindowHandlePtr}.");
var ret = DragDropInterop.RevokeDragDrop(this.interfaceManager!.WindowHandlePtr);
Log.Information($"[DragDrop] Disabled external drag and drop operations for window {this.interfaceManager.WindowHandlePtr}. ({ret})");
Marshal.ThrowExceptionForHR(ret);
}
catch (Exception ex)
{

View file

@ -31,7 +31,6 @@ public sealed class UiBuilder : IDisposable
private readonly string namespaceName;
private readonly InterfaceManager interfaceManager = Service<InterfaceManager>.Get();
private readonly GameFontManager gameFontManager = Service<GameFontManager>.Get();
private readonly DragDropManager dragDropManager = Service<DragDropManager>.Get();
[ServiceManager.ServiceDependency]
private readonly DalamudConfiguration configuration = Service<DalamudConfiguration>.Get();
@ -102,11 +101,6 @@ public sealed class UiBuilder : IDisposable
/// </summary>
public event Action HideUi;
/// <summary>
/// Gets the manager for external, WinAPI-based drag and drop functionality.
/// </summary>
public IDragDropManager DragDropManager => this.dragDropManager;
/// <summary>
/// Gets the default Dalamud font based on Noto Sans CJK Medium in 17pt - supporting all game languages and icons.
/// </summary>