From 5e803c858ae6aceaca931c8d82003a8e95f1e1c3 Mon Sep 17 00:00:00 2001 From: Soreepeong Date: Mon, 15 Jul 2024 17:07:42 +0900 Subject: [PATCH] cleanup --- .../Interface/Internal/InterfaceManager.cs | 38 +++++++++---------- .../FontAtlasFactory.Implementation.cs | 6 +-- .../Internals/FontAtlasFactory.cs | 8 ++-- .../Textures/Internal/TextureManager.cs | 2 +- Dalamud/Interface/UiBuilder.cs | 35 +++++++++++------ 5 files changed, 50 insertions(+), 39 deletions(-) diff --git a/Dalamud/Interface/Internal/InterfaceManager.cs b/Dalamud/Interface/Internal/InterfaceManager.cs index 16b127989..2e4bcc848 100644 --- a/Dalamud/Interface/Internal/InterfaceManager.cs +++ b/Dalamud/Interface/Internal/InterfaceManager.cs @@ -81,7 +81,7 @@ internal class InterfaceManager : IInternalDisposableService private readonly ConcurrentQueue runAfterImGuiRender = new(); private readonly SwapChainVtableResolver address = new(); - private IWin32Backend? scene; + private IWin32Backend? backend; private Hook? setCursorHook; private Hook? presentHook; @@ -181,26 +181,26 @@ private unsafe delegate HRESULT ResizeBuffersDelegate( /// /// Gets the DX11 scene. /// - public IImGuiBackend? Scene => this.scene; + public IImGuiBackend? Backend => this.backend; /// /// Gets or sets a value indicating whether or not the game's cursor should be overridden with the ImGui cursor. /// public bool OverrideGameCursor { - get => this.scene?.UpdateCursor ?? this.isOverrideGameCursor; + get => this.backend?.UpdateCursor ?? this.isOverrideGameCursor; set { this.isOverrideGameCursor = value; - if (this.scene != null) - this.scene.UpdateCursor = value; + if (this.backend != null) + this.backend.UpdateCursor = value; } } /// /// Gets a value indicating whether the Dalamud interface ready to use. /// - public bool IsReady => this.scene != null; + public bool IsReady => this.backend != null; /// /// Gets or sets a value indicating whether or not Draw events should be dispatched. @@ -284,7 +284,7 @@ void IInternalDisposableService.DisposeService() this.IconFontHandle = null; Interlocked.Exchange(ref this.dalamudAtlas, null)?.Dispose(); - Interlocked.Exchange(ref this.scene, null)?.Dispose(); + Interlocked.Exchange(ref this.backend, null)?.Dispose(); return; @@ -418,14 +418,14 @@ public Task RunAfterImGuiRender(Func func) /// The currently used video memory, or null if not available. public unsafe (long Used, long Available)? GetD3dMemoryInfo() { - if (this.scene?.DeviceHandle is 0 or null) + if (this.backend?.DeviceHandle is 0 or null) return null; using var device = default(ComPtr); using var adapter = default(ComPtr); using var adapter4 = default(ComPtr); - if (new ComPtr((IUnknown*)this.scene.DeviceHandle).As(&device).FAILED) + if (new ComPtr((IUnknown*)this.backend.DeviceHandle).As(&device).FAILED) return null; if (device.Get()->GetAdapter(adapter.GetAddressOf()).FAILED) @@ -619,7 +619,7 @@ private unsafe void InitScene(IDXGISwapChain* swapChain) Log.Information("[IM] Scene & ImGui setup OK!"); } - this.scene = newBackend; + this.backend = newBackend; Service.Provide(new(this)); this.wndProcHookManager.PreWndProc += this.WndProcHookManagerOnPreWndProc; @@ -627,7 +627,7 @@ private unsafe void InitScene(IDXGISwapChain* swapChain) private void WndProcHookManagerOnPreWndProc(WndProcEventArgs args) { - var r = this.scene?.ProcessWndProcW(args.Hwnd, args.Message, args.WParam, args.LParam); + var r = this.backend?.ProcessWndProcW(args.Hwnd, args.Message, args.WParam, args.LParam); if (r is not null) args.SuppressWithValue(r.Value); } @@ -640,14 +640,14 @@ private unsafe HRESULT PresentDetour(IDXGISwapChain* swapChain, uint syncInterva { Debug.Assert(this.presentHook is not null, "How did PresentDetour get called when presentHook is null?"); - if (this.scene is null) + if (this.backend is null) { this.InitScene(swapChain); - if (this.scene is null) + if (this.backend is null) throw new InvalidOperationException("InitScene did not set this.scene?"); } - if (!this.scene.IsAttachedToPresentationTarget((nint)swapChain)) + if (!this.backend.IsAttachedToPresentationTarget((nint)swapChain)) return this.presentHook!.Original(swapChain, syncInterval, presentFlags); // Do not do anything yet if no font atlas has been built yet. @@ -666,7 +666,7 @@ private unsafe HRESULT PresentDetour(IDXGISwapChain* swapChain, uint syncInterva this.IsMainThreadInPresent = true; this.CumulativePresentCalls++; this.PreImGuiRender(); - RenderImGui(this.scene!); + RenderImGui(this.backend!); this.PostImGuiRender(); this.IsMainThreadInPresent = false; @@ -830,23 +830,23 @@ private unsafe HRESULT ResizeBuffersDetour( this.ResizeBuffers?.InvokeSafely(); // We have to ensure we're working with the main swapchain, as other viewports might be resizing as well. - if (this.scene?.IsAttachedToPresentationTarget((nint)swapChain) is not true) + if (this.backend?.IsAttachedToPresentationTarget((nint)swapChain) is not true) return this.resizeBuffersHook!.Original(swapChain, bufferCount, width, height, newFormat, swapChainFlags); - this.scene?.OnPreResize(); + this.backend?.OnPreResize(); var ret = this.resizeBuffersHook!.Original(swapChain, bufferCount, width, height, newFormat, swapChainFlags); if (ret == DXGI.DXGI_ERROR_INVALID_CALL) Log.Error("invalid call to resizeBuffers"); - this.scene?.OnPostResize((int)width, (int)height); + this.backend?.OnPostResize((int)width, (int)height); return ret; } private HCURSOR SetCursorDetour(HCURSOR hCursor) { - if (this.lastWantCapture && (!this.scene?.IsImGuiCursor(hCursor) ?? false) && this.OverrideGameCursor) + if (this.lastWantCapture && (!this.backend?.IsImGuiCursor(hCursor) ?? false) && this.OverrideGameCursor) return default; return this.setCursorHook?.IsDisposed is not false diff --git a/Dalamud/Interface/ManagedFontAtlas/Internals/FontAtlasFactory.Implementation.cs b/Dalamud/Interface/ManagedFontAtlas/Internals/FontAtlasFactory.Implementation.cs index 3633a8f95..e4f9d4adc 100644 --- a/Dalamud/Interface/ManagedFontAtlas/Internals/FontAtlasFactory.Implementation.cs +++ b/Dalamud/Interface/ManagedFontAtlas/Internals/FontAtlasFactory.Implementation.cs @@ -309,7 +309,7 @@ public DalamudFontAtlas( throw; } - this.factory.SceneTask.ContinueWith( + this.factory.BackendTask.ContinueWith( r => { lock (this.syncRoot) @@ -734,7 +734,7 @@ private async Task RebuildFontsPrivateReal(bool isAsync, flo foreach (var font in toolkit.Fonts) toolkit.BuildLookupTable(font); - if (this.factory.SceneTask is { IsCompleted: false } sceneTask) + if (this.factory.BackendTask is { IsCompleted: false } backendTask) { Log.Verbose( "[{name}:{functionname}] 0x{ptr:X}: await SceneTask (at {sw}ms)", @@ -742,7 +742,7 @@ private async Task RebuildFontsPrivateReal(bool isAsync, flo nameof(this.RebuildFontsPrivateReal), atlasPtr, sw.ElapsedMilliseconds); - await sceneTask.ConfigureAwait(!isAsync); + await backendTask.ConfigureAwait(!isAsync); } #if VeryVerboseLog diff --git a/Dalamud/Interface/ManagedFontAtlas/Internals/FontAtlasFactory.cs b/Dalamud/Interface/ManagedFontAtlas/Internals/FontAtlasFactory.cs index e518c2c77..84d59b234 100644 --- a/Dalamud/Interface/ManagedFontAtlas/Internals/FontAtlasFactory.cs +++ b/Dalamud/Interface/ManagedFontAtlas/Internals/FontAtlasFactory.cs @@ -51,9 +51,9 @@ private FontAtlasFactory( this.Framework = framework; this.InterfaceManager = interfaceManager; this.dalamudAssetManager = dalamudAssetManager; - this.SceneTask = Service + this.BackendTask = Service .GetAsync() - .ContinueWith(r => r.Result.Manager.Scene); + .ContinueWith(r => r.Result.Manager.Backend); var gffasInfo = Enum.GetValues() .Select( @@ -140,7 +140,7 @@ private FontAtlasFactory( /// /// Gets the service instance of .
- /// may not yet be available. + /// may not yet be available. ///
public InterfaceManager InterfaceManager { get; } @@ -152,7 +152,7 @@ private FontAtlasFactory( /// /// Gets the async task for inside . /// - public Task SceneTask { get; } + public Task BackendTask { get; } /// /// Gets the default glyph ranges (glyph ranges of ). diff --git a/Dalamud/Interface/Textures/Internal/TextureManager.cs b/Dalamud/Interface/Textures/Internal/TextureManager.cs index ef85de53e..e608956c2 100644 --- a/Dalamud/Interface/Textures/Internal/TextureManager.cs +++ b/Dalamud/Interface/Textures/Internal/TextureManager.cs @@ -58,7 +58,7 @@ internal sealed partial class TextureManager private unsafe TextureManager(InterfaceManager.InterfaceManagerWithScene withScene) { using var failsafe = new DisposeSafety.ScopedFinalizer(); - failsafe.Add(this.device = new((ID3D11Device*)withScene.Manager.Scene!.DeviceHandle)); + failsafe.Add(this.device = new((ID3D11Device*)withScene.Manager.Backend!.DeviceHandle)); failsafe.Add(this.dynamicPriorityTextureLoader = new(Math.Max(1, Environment.ProcessorCount - 1))); failsafe.Add(this.sharedTextureManager = new(this)); failsafe.Add(this.wicManager = new(this)); diff --git a/Dalamud/Interface/UiBuilder.cs b/Dalamud/Interface/UiBuilder.cs index e199cea2e..1fd615b7e 100644 --- a/Dalamud/Interface/UiBuilder.cs +++ b/Dalamud/Interface/UiBuilder.cs @@ -118,12 +118,22 @@ public interface IUiBuilder /// /// Gets the game's active Direct3D device. /// + // TODO: Remove it on API11/APIXI, and remove SharpDX/PInvoke/etc. dependency from Dalamud. + [Obsolete($"Use {nameof(DeviceHandle)} and wrap it using DirectX wrapper library of your choice.")] SharpDX.Direct3D11.Device Device { get; } - /// - /// Gets the game's main window handle. - /// - IntPtr WindowHandlePtr { get; } + /// Gets the game's active Direct3D device. + /// Pointer to the instance of IUnknown that the game is using and should be containing an ID3D11Device, + /// or 0 if it is not available yet. + /// Use + /// + /// QueryInterface with IID of IID_ID3D11Device if you want to ensure that the interface type contained + /// within is indeed an instance of ID3D11Device. + nint DeviceHandle { get; } + + /// Gets the game's main window handle. + /// HWND of the main game window, or 0 if it is not available yet. + nint WindowHandlePtr { get; } /// /// Gets or sets a value indicating whether this plugin should hide its UI automatically when the game's UI is hidden. @@ -415,16 +425,17 @@ internal UiBuilder(LocalPlugin plugin, string namespaceName) this.InterfaceManagerWithScene?.MonoFontHandle ?? throw new InvalidOperationException("Scene is not yet ready."))); - /// - /// Gets the game's active Direct3D device. - /// + /// + // TODO: Remove it on API11/APIXI, and remove SharpDX/PInvoke/etc. dependency from Dalamud. + [Obsolete($"Use {nameof(DeviceHandle)} and wrap it using DirectX wrapper library of your choice.")] public SharpDX.Direct3D11.Device Device => - this.sdxDevice ??= new(this.InterfaceManagerWithScene!.Scene!.DeviceHandle); + this.sdxDevice ??= new(this.InterfaceManagerWithScene!.Backend!.DeviceHandle); - /// - /// Gets the game's main window handle. - /// - public nint WindowHandlePtr => this.InterfaceManagerWithScene!.GameWindowHandle; + /// + public nint DeviceHandle => this.InterfaceManagerWithScene?.Backend?.DeviceHandle ?? 0; + + /// + public nint WindowHandlePtr => this.InterfaceManagerWithScene is { } imws ? imws.GameWindowHandle : 0; /// /// Gets or sets a value indicating whether this plugin should hide its UI automatically when the game's UI is hidden.