Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Soreepeong committed Jul 15, 2024
1 parent 6ddc51e commit 5e803c8
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 39 deletions.
38 changes: 19 additions & 19 deletions Dalamud/Interface/Internal/InterfaceManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ internal class InterfaceManager : IInternalDisposableService
private readonly ConcurrentQueue<Action> runAfterImGuiRender = new();

private readonly SwapChainVtableResolver address = new();
private IWin32Backend? scene;
private IWin32Backend? backend;

private Hook<SetCursorDelegate>? setCursorHook;
private Hook<PresentDelegate>? presentHook;
Expand Down Expand Up @@ -181,26 +181,26 @@ private unsafe delegate HRESULT ResizeBuffersDelegate(
/// <summary>
/// Gets the DX11 scene.
/// </summary>
public IImGuiBackend? Scene => this.scene;
public IImGuiBackend? Backend => this.backend;

/// <summary>
/// Gets or sets a value indicating whether or not the game's cursor should be overridden with the ImGui cursor.
/// </summary>
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;
}
}

/// <summary>
/// Gets a value indicating whether the Dalamud interface ready to use.
/// </summary>
public bool IsReady => this.scene != null;
public bool IsReady => this.backend != null;

/// <summary>
/// Gets or sets a value indicating whether or not Draw events should be dispatched.
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -418,14 +418,14 @@ public Task<T> RunAfterImGuiRender<T>(Func<T> func)
/// <returns>The currently used video memory, or null if not available.</returns>
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<IDXGIDevice>);
using var adapter = default(ComPtr<IDXGIAdapter>);
using var adapter4 = default(ComPtr<IDXGIAdapter4>);

if (new ComPtr<IUnknown>((IUnknown*)this.scene.DeviceHandle).As(&device).FAILED)
if (new ComPtr<IUnknown>((IUnknown*)this.backend.DeviceHandle).As(&device).FAILED)
return null;

if (device.Get()->GetAdapter(adapter.GetAddressOf()).FAILED)
Expand Down Expand Up @@ -619,15 +619,15 @@ private unsafe void InitScene(IDXGISwapChain* swapChain)
Log.Information("[IM] Scene & ImGui setup OK!");
}

this.scene = newBackend;
this.backend = newBackend;
Service<InterfaceManagerWithScene>.Provide(new(this));

this.wndProcHookManager.PreWndProc += this.WndProcHookManagerOnPreWndProc;
}

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);
}
Expand All @@ -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.
Expand All @@ -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;

Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ public DalamudFontAtlas(
throw;
}

this.factory.SceneTask.ContinueWith(
this.factory.BackendTask.ContinueWith(
r =>
{
lock (this.syncRoot)
Expand Down Expand Up @@ -734,15 +734,15 @@ private async Task<FontAtlasBuiltData> 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)",
this.Name,
nameof(this.RebuildFontsPrivateReal),
atlasPtr,
sw.ElapsedMilliseconds);
await sceneTask.ConfigureAwait(!isAsync);
await backendTask.ConfigureAwait(!isAsync);
}

#if VeryVerboseLog
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ private FontAtlasFactory(
this.Framework = framework;
this.InterfaceManager = interfaceManager;
this.dalamudAssetManager = dalamudAssetManager;
this.SceneTask = Service<InterfaceManager.InterfaceManagerWithScene>
this.BackendTask = Service<InterfaceManager.InterfaceManagerWithScene>
.GetAsync()
.ContinueWith(r => r.Result.Manager.Scene);
.ContinueWith(r => r.Result.Manager.Backend);

var gffasInfo = Enum.GetValues<GameFontFamilyAndSize>()
.Select(
Expand Down Expand Up @@ -140,7 +140,7 @@ private FontAtlasFactory(

/// <summary>
/// Gets the service instance of <see cref="InterfaceManager"/>.<br />
/// <see cref="Internal.InterfaceManager.Scene"/> may not yet be available.
/// <see cref="Internal.InterfaceManager.Backend"/> may not yet be available.
/// </summary>
public InterfaceManager InterfaceManager { get; }

Expand All @@ -152,7 +152,7 @@ private FontAtlasFactory(
/// <summary>
/// Gets the async task for <see cref="IImGuiBackend"/> inside <see cref="InterfaceManager"/>.
/// </summary>
public Task<IImGuiBackend> SceneTask { get; }
public Task<IImGuiBackend> BackendTask { get; }

/// <summary>
/// Gets the default glyph ranges (glyph ranges of <see cref="GameFontFamilyAndSize.Axis12"/>).
Expand Down
2 changes: 1 addition & 1 deletion Dalamud/Interface/Textures/Internal/TextureManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
35 changes: 23 additions & 12 deletions Dalamud/Interface/UiBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,22 @@ public interface IUiBuilder
/// <summary>
/// Gets the game's active Direct3D device.
/// </summary>
// 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; }

/// <summary>
/// Gets the game's main window handle.
/// </summary>
IntPtr WindowHandlePtr { get; }
/// <summary>Gets the game's active Direct3D device.</summary>
/// <value>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.</value>
/// <remarks>Use
/// <a href="https://learn.microsoft.com/en-us/windows/win32/api/unknwn/nf-unknwn-iunknown-queryinterface(q)">
/// QueryInterface</a> with IID of <c>IID_ID3D11Device</c> if you want to ensure that the interface type contained
/// within is indeed an instance of ID3D11Device.</remarks>
nint DeviceHandle { get; }

/// <summary>Gets the game's main window handle.</summary>
/// <value>HWND of the main game window, or 0 if it is not available yet.</value>
nint WindowHandlePtr { get; }

/// <summary>
/// Gets or sets a value indicating whether this plugin should hide its UI automatically when the game's UI is hidden.
Expand Down Expand Up @@ -415,16 +425,17 @@ internal UiBuilder(LocalPlugin plugin, string namespaceName)
this.InterfaceManagerWithScene?.MonoFontHandle
?? throw new InvalidOperationException("Scene is not yet ready.")));

/// <summary>
/// Gets the game's active Direct3D device.
/// </summary>
/// <inheritdoc/>
// 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);

/// <summary>
/// Gets the game's main window handle.
/// </summary>
public nint WindowHandlePtr => this.InterfaceManagerWithScene!.GameWindowHandle;
/// <inheritdoc/>
public nint DeviceHandle => this.InterfaceManagerWithScene?.Backend?.DeviceHandle ?? 0;

/// <inheritdoc/>
public nint WindowHandlePtr => this.InterfaceManagerWithScene is { } imws ? imws.GameWindowHandle : 0;

/// <summary>
/// Gets or sets a value indicating whether this plugin should hide its UI automatically when the game's UI is hidden.
Expand Down

0 comments on commit 5e803c8

Please sign in to comment.