Fix insufficient ImDrawList implementation

This commit is contained in:
Soreepeong 2024-02-14 21:59:20 +09:00
parent cf3091b409
commit 2de9c8ed5b
4 changed files with 128 additions and 80 deletions

View file

@ -11,6 +11,7 @@ using System.Text.Unicode;
using Dalamud.Game.Text;
using Dalamud.Hooking.WndProcHook;
using Dalamud.Interface.GameFonts;
using Dalamud.Interface.Internal.ManagedAsserts;
using Dalamud.Interface.ManagedFontAtlas.Internals;
using Dalamud.Interface.Utility;
@ -28,7 +29,6 @@ namespace Dalamud.Interface.Internal;
[ServiceManager.BlockingEarlyLoadedService]
internal sealed unsafe class DalamudIme : IDisposable, IServiceType
{
private const int ImGuiContextTextStateOffset = 0x4588;
private const int CImGuiStbTextCreateUndoOffset = 0xB57A0;
private const int CImGuiStbTextUndoOffset = 0xB59C0;
@ -178,7 +178,7 @@ internal sealed unsafe class DalamudIme : IDisposable, IServiceType
internal char InputModeIcon { get; private set; }
private static ImGuiInputTextState* TextState =>
(ImGuiInputTextState*)(ImGui.GetCurrentContext() + ImGuiContextTextStateOffset);
(ImGuiInputTextState*)(ImGui.GetCurrentContext() + ImGuiContextOffsets.TextStateOffset);
/// <inheritdoc/>
public void Dispose()

View file

@ -0,0 +1,124 @@
using System.Diagnostics;
using System.Linq;
using System.Numerics;
using Dalamud.Hooking;
using ImGuiNET;
namespace Dalamud.Interface.Internal;
/// <summary>
/// Fixes ImDrawList not correctly dealing with the current texture for that draw list not in tune with the global
/// state. Currently, ImDrawList::AddPolyLine and ImDrawList::AddRectFilled are affected.
///
/// * The implementation for AddRectFilled is entirely replaced with the hook below.
/// * The implementation for AddPolyLine is wrapped with Push/PopTextureID.
///
/// TODO:
/// * imgui_draw.cpp:1433 ImDrawList::AddRectFilled
/// The if block needs a PushTextureID(_Data->TexIdCommon)/PopTextureID() block,
/// if _Data->TexIdCommon != _CmdHeader.TextureId.
/// * imgui_draw.cpp:729 ImDrawList::AddPolyLine
/// The if block always needs to call PushTextureID if the abovementioned condition is not met.
/// Change push_texture_id to only have one condition.
/// </summary>
[ServiceManager.EarlyLoadedService]
internal sealed unsafe class ImGuiDrawListFixProvider : IServiceType, IDisposable
{
private const int CImGuiImDrawListAddPolyLineOffset = 0x589B0;
private const int CImGuiImDrawListAddRectFilled = 0x59FD0;
private const int CImGuiImDrawListSharedDataTexIdCommonOffset = 0;
private readonly Hook<ImDrawListAddPolyLine> hookImDrawListAddPolyline;
private readonly Hook<ImDrawListAddRectFilled> hookImDrawListAddRectFilled;
[ServiceManager.ServiceConstructor]
private ImGuiDrawListFixProvider(InterfaceManager.InterfaceManagerWithScene imws)
{
// Force cimgui.dll to be loaded.
_ = ImGui.GetCurrentContext();
var cimgui = Process.GetCurrentProcess().Modules.Cast<ProcessModule>()
.First(x => x.ModuleName == "cimgui.dll")
.BaseAddress;
this.hookImDrawListAddPolyline = Hook<ImDrawListAddPolyLine>.FromAddress(
cimgui + CImGuiImDrawListAddPolyLineOffset,
this.ImDrawListAddPolylineDetour);
this.hookImDrawListAddRectFilled = Hook<ImDrawListAddRectFilled>.FromAddress(
cimgui + CImGuiImDrawListAddRectFilled,
this.ImDrawListAddRectFilledDetour);
this.hookImDrawListAddPolyline.Enable();
this.hookImDrawListAddRectFilled.Enable();
}
private delegate void ImDrawListAddPolyLine(
ImDrawListPtr drawListPtr,
ref Vector2 points,
int pointsCount,
uint color,
ImDrawFlags flags,
float thickness);
private delegate void ImDrawListAddRectFilled(
ImDrawListPtr drawListPtr,
ref Vector2 min,
ref Vector2 max,
uint col,
float rounding,
ImDrawFlags flags);
/// <inheritdoc/>
public void Dispose()
{
this.hookImDrawListAddPolyline.Dispose();
this.hookImDrawListAddRectFilled.Dispose();
}
private void ImDrawListAddRectFilledDetour(
ImDrawListPtr drawListPtr,
ref Vector2 min,
ref Vector2 max,
uint col,
float rounding,
ImDrawFlags flags)
{
if (rounding < 0 || (flags & ImDrawFlags.RoundCornersMask) == ImDrawFlags.RoundCornersMask)
{
var texIdCommon = *(nint*)(drawListPtr._Data + CImGuiImDrawListSharedDataTexIdCommonOffset);
var pushTextureId = texIdCommon != drawListPtr._CmdHeader.TextureId;
if (pushTextureId)
drawListPtr.PushTextureID(texIdCommon);
drawListPtr.PrimReserve(6, 4);
drawListPtr.PrimRect(min, max, col);
if (pushTextureId)
drawListPtr.PopTextureID();
}
else
{
drawListPtr.PathRect(min, max, rounding, flags);
drawListPtr.PathFillConvex(col);
}
}
private void ImDrawListAddPolylineDetour(
ImDrawListPtr drawListPtr,
ref Vector2 points,
int pointsCount,
uint color,
ImDrawFlags flags,
float thickness)
{
var texIdCommon = *(nint*)(drawListPtr._Data + CImGuiImDrawListSharedDataTexIdCommonOffset);
var pushTextureId = texIdCommon != drawListPtr._CmdHeader.TextureId;
if (pushTextureId)
drawListPtr.PushTextureID(texIdCommon);
this.hookImDrawListAddPolyline.Original(drawListPtr, ref points, pointsCount, color, flags, thickness);
if (pushTextureId)
drawListPtr.PopTextureID();
}
}

View file

@ -18,4 +18,6 @@ internal static class ImGuiContextOffsets
public const int FontStackOffset = 0x7A4;
public const int BeginPopupStackOffset = 0x7B8;
public const int TextStateOffset = 0x4588;
}

View file

@ -1,78 +0,0 @@
using System.Diagnostics;
using System.Linq;
using Dalamud.Hooking;
using Dalamud.Interface.Internal;
using Dalamud.Interface.Utility;
namespace Dalamud.Interface.ManagedFontAtlas.Internals;
/// <summary>
/// Forces ImGui::RenderWindowDecorations to use the default font.
/// Fixes dock node draw using shared data across different draw lists.
/// TODO: figure out how to synchronize ImDrawList::_Data and ImDrawList::Push/PopTextureID across different instances.
/// It might be better to just special-case that particular function,
/// as no other code touches ImDrawList that is irrelevant to the global shared state,
/// with the exception of Dock... functions which are called from ImGui::NewFrame,
/// which are guaranteed to use the global default font.
/// </summary>
[ServiceManager.EarlyLoadedService]
internal class ImGuiRenderWindowDecorationsForceFont : IServiceType, IDisposable
{
private const int CImGuiRenderWindowDecorationsOffset = 0x461B0;
private const int CImGuiWindowDockIsActiveOffset = 0x401;
private readonly Hook<ImGuiRenderWindowDecorationsDelegate> hook;
[ServiceManager.ServiceConstructor]
private ImGuiRenderWindowDecorationsForceFont(InterfaceManager.InterfaceManagerWithScene imws)
{
// Effectively waiting for ImGui to become available.
Debug.Assert(ImGuiHelpers.IsImGuiInitialized, "IMWS initialized but IsImGuiInitialized is false?");
var cimgui = Process.GetCurrentProcess().Modules.Cast<ProcessModule>()
.First(x => x.ModuleName == "cimgui.dll")
.BaseAddress;
this.hook = Hook<ImGuiRenderWindowDecorationsDelegate>.FromAddress(
cimgui + CImGuiRenderWindowDecorationsOffset,
this.ImGuiRenderWindowDecorationsDetour);
this.hook.Enable();
}
private delegate void ImGuiRenderWindowDecorationsDelegate(
nint window,
nint titleBarRectPtr,
byte titleBarIsHighlight,
byte handleBordersAndResizeGrips,
int resizeGripCount,
nint resizeGripColPtr,
float resizeGripDrawSize);
/// <inheritdoc/>
public void Dispose() => this.hook.Dispose();
private unsafe void ImGuiRenderWindowDecorationsDetour(
nint window,
nint titleBarRectPtr,
byte titleBarIsHighlight,
byte handleBordersAndResizeGrips,
int resizeGripCount,
nint resizeGripColPtr,
float resizeGripDrawSize)
{
using (
((byte*)window)![CImGuiWindowDockIsActiveOffset] != 0
? Service<InterfaceManager>.Get().DefaultFontHandle?.Push()
: null)
{
this.hook.Original(
window,
titleBarRectPtr,
titleBarIsHighlight,
handleBordersAndResizeGrips,
resizeGripCount,
resizeGripColPtr,
resizeGripDrawSize);
}
}
}