From 2e5296ea3c4007d5d3a599e97ff44a05b4202e08 Mon Sep 17 00:00:00 2001 From: Raymond Date: Mon, 1 Nov 2021 12:57:48 -0400 Subject: [PATCH 01/10] Console window: always have scrollbars --- Dalamud/Interface/Internal/Windows/ConsoleWindow.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dalamud/Interface/Internal/Windows/ConsoleWindow.cs b/Dalamud/Interface/Internal/Windows/ConsoleWindow.cs index fadf974c1..a34d758f7 100644 --- a/Dalamud/Interface/Internal/Windows/ConsoleWindow.cs +++ b/Dalamud/Interface/Internal/Windows/ConsoleWindow.cs @@ -196,7 +196,7 @@ namespace Dalamud.Interface.Internal.Windows if (ImGui.IsItemHovered()) ImGui.SetTooltip("Kill game"); - ImGui.BeginChild("scrolling", new Vector2(0, ImGui.GetFrameHeightWithSpacing() - 55), false, ImGuiWindowFlags.HorizontalScrollbar); + ImGui.BeginChild("scrolling", new Vector2(0, ImGui.GetFrameHeightWithSpacing() - 55), false, ImGuiWindowFlags.AlwaysHorizontalScrollbar | ImGuiWindowFlags.AlwaysVerticalScrollbar); if (clear) { From e5ad15b305b9f62ff44ce78e6bbaf0fc644342a2 Mon Sep 17 00:00:00 2001 From: Raymond Date: Mon, 1 Nov 2021 10:33:27 -0400 Subject: [PATCH 02/10] Clean imports --- Dalamud/Hooking/Hook.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/Dalamud/Hooking/Hook.cs b/Dalamud/Hooking/Hook.cs index ba19ffb18..f9892e92f 100644 --- a/Dalamud/Hooking/Hook.cs +++ b/Dalamud/Hooking/Hook.cs @@ -1,12 +1,10 @@ using System; -using System.Diagnostics; using System.Reflection; using Dalamud.Configuration.Internal; using Dalamud.Hooking.Internal; using Dalamud.Memory; using Reloaded.Hooks; -using Serilog; namespace Dalamud.Hooking { From 067ffc9fd282924bdeedc17cf47acb32953e05ef Mon Sep 17 00:00:00 2001 From: Raymond Date: Mon, 1 Nov 2021 10:33:44 -0400 Subject: [PATCH 03/10] Fix enum VS messages --- Dalamud/NativeFunctions.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Dalamud/NativeFunctions.cs b/Dalamud/NativeFunctions.cs index e23a7d237..d11c39464 100644 --- a/Dalamud/NativeFunctions.cs +++ b/Dalamud/NativeFunctions.cs @@ -478,8 +478,8 @@ namespace Dalamud WM_INPUT_DEVICE_CHANGE = 0x00FE, WM_INPUT = 0x00FF, - WM_KEYFIRST = WM_KEYDOWN, - WM_KEYDOWN = 0x0100, + WM_KEYFIRST = 0x0100, + WM_KEYDOWN = WM_KEYFIRST, WM_KEYUP = 0x0101, WM_CHAR = 0x0102, WM_DEADCHAR = 0x0103, @@ -525,8 +525,8 @@ namespace Dalamud WM_CTLCOLORSTATIC = 0x0138, MN_GETHMENU = 0x01E1, - WM_MOUSEFIRST = WM_MOUSEMOVE, - WM_MOUSEMOVE = 0x0200, + WM_MOUSEFIRST = 0x0200, + WM_MOUSEMOVE = WM_MOUSEFIRST, WM_LBUTTONDOWN = 0x0201, WM_LBUTTONUP = 0x0202, WM_LBUTTONDBLCLK = 0x0203, From 9c27212d3745eeae719c6cacb82b18d51ad805db Mon Sep 17 00:00:00 2001 From: Raymond Date: Mon, 1 Nov 2021 12:40:57 -0400 Subject: [PATCH 04/10] Fix chained ctor confusing callingAssembly value --- Dalamud/Hooking/Hook.cs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/Dalamud/Hooking/Hook.cs b/Dalamud/Hooking/Hook.cs index f9892e92f..13b199dea 100644 --- a/Dalamud/Hooking/Hook.cs +++ b/Dalamud/Hooking/Hook.cs @@ -27,7 +27,7 @@ namespace Dalamud.Hooking /// A memory address to install a hook. /// Callback function. Delegate must have a same original function prototype. public Hook(IntPtr address, T detour) - : this(address, detour, false) + : this(address, detour, false, Assembly.GetCallingAssembly()) { } @@ -40,6 +40,11 @@ namespace Dalamud.Hooking /// Callback function. Delegate must have a same original function prototype. /// Use the MinHook hooking library instead of Reloaded. public Hook(IntPtr address, T detour, bool useMinHook) + : this(address, detour, useMinHook, Assembly.GetCallingAssembly()) + { + } + + private Hook(IntPtr address, T detour, bool useMinHook, Assembly callingAssembly) { address = HookManager.FollowJmp(address); this.isMinHook = EnvironmentConfiguration.DalamudForceMinHook || useMinHook; @@ -69,7 +74,7 @@ namespace Dalamud.Hooking this.hookImpl = ReloadedHooks.Instance.CreateHook(detour, address.ToInt64()); } - HookManager.TrackedHooks.Add(new HookInfo(this, detour, Assembly.GetCallingAssembly())); + HookManager.TrackedHooks.Add(new HookInfo(this, detour, callingAssembly)); } /// From 8a58782e86914d9bab110e2484411b40b1c96b66 Mon Sep 17 00:00:00 2001 From: Raymond Date: Wed, 3 Nov 2021 08:31:36 -0400 Subject: [PATCH 05/10] Force Reloaded hook option --- Dalamud/Configuration/Internal/EnvironmentConfiguration.cs | 7 ++++++- Dalamud/Hooking/Hook.cs | 2 +- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/Dalamud/Configuration/Internal/EnvironmentConfiguration.cs b/Dalamud/Configuration/Internal/EnvironmentConfiguration.cs index 31bee1204..816282ab0 100644 --- a/Dalamud/Configuration/Internal/EnvironmentConfiguration.cs +++ b/Dalamud/Configuration/Internal/EnvironmentConfiguration.cs @@ -22,10 +22,15 @@ namespace Dalamud.Configuration.Internal /// public static bool DalamudNoPlugins { get; } = GetEnvironmentVariable("DALAMUD_NOT_HAVE_PLUGINS"); + /// + /// Gets a value indicating whether the DalamudForceReloaded setting has been enabled. + /// + public static bool DalamudForceReloaded { get; } = GetEnvironmentVariable("DALAMUD_FORCE_RELOADED"); + /// /// Gets a value indicating whether the DalamudForceCoreHook setting has been enabled. /// - public static bool DalamudForceMinHook { get; } = GetEnvironmentVariable("DALAMUD_FORCE_COREHOOK"); + public static bool DalamudForceMinHook { get; } = GetEnvironmentVariable("DALAMUD_FORCE_MINHOOK"); private static bool GetEnvironmentVariable(string name) => bool.Parse(Environment.GetEnvironmentVariable(name) ?? "false"); diff --git a/Dalamud/Hooking/Hook.cs b/Dalamud/Hooking/Hook.cs index 13b199dea..87584fee7 100644 --- a/Dalamud/Hooking/Hook.cs +++ b/Dalamud/Hooking/Hook.cs @@ -47,7 +47,7 @@ namespace Dalamud.Hooking private Hook(IntPtr address, T detour, bool useMinHook, Assembly callingAssembly) { address = HookManager.FollowJmp(address); - this.isMinHook = EnvironmentConfiguration.DalamudForceMinHook || useMinHook; + this.isMinHook = !EnvironmentConfiguration.DalamudForceReloaded && (EnvironmentConfiguration.DalamudForceMinHook || useMinHook); var hasOtherHooks = HookManager.Originals.ContainsKey(address); if (!hasOtherHooks) From c53aced272b8dd845d7a26bf24594f6b73e004a0 Mon Sep 17 00:00:00 2001 From: Raymond Date: Wed, 3 Nov 2021 15:56:33 -0400 Subject: [PATCH 06/10] Fix for minhook error --- Dalamud/Hooking/Hook.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dalamud/Hooking/Hook.cs b/Dalamud/Hooking/Hook.cs index 87584fee7..e7c040e05 100644 --- a/Dalamud/Hooking/Hook.cs +++ b/Dalamud/Hooking/Hook.cs @@ -61,7 +61,7 @@ namespace Dalamud.Hooking { var indexList = hasOtherHooks ? HookManager.MultiHookTracker[address] - : HookManager.MultiHookTracker[address] = new(); + : (HookManager.MultiHookTracker[address] = new()); var index = (ulong)indexList.Count; this.minHookImpl = new MinSharp.Hook(address, detour, index); From edc38bae5e36e32cc48d561c4d597195dfe1739d Mon Sep 17 00:00:00 2001 From: Raymond Date: Wed, 3 Nov 2021 21:20:32 -0400 Subject: [PATCH 07/10] bail on tasktracker hook error --- Dalamud/Logging/Internal/TaskTracker.cs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/Dalamud/Logging/Internal/TaskTracker.cs b/Dalamud/Logging/Internal/TaskTracker.cs index 2ed3ccbbe..fb99498a2 100644 --- a/Dalamud/Logging/Internal/TaskTracker.cs +++ b/Dalamud/Logging/Internal/TaskTracker.cs @@ -123,10 +123,16 @@ namespace Dalamud.Logging.Internal var patchMethod = typeof(TaskTracker).GetMethod(nameof(AddToActiveTasksHook), BindingFlags.NonPublic | BindingFlags.Static); if (targetMethod == null) - Log.Error("TargetMethod null!"); + { + Log.Error("AddToActiveTasks TargetMethod null!"); + return; + } if (patchMethod == null) - Log.Error("PatchMethod null!"); + { + Log.Error("AddToActiveTasks PatchMethod null!"); + return; + } this.scheduleAndStartHook = new MonoMod.RuntimeDetour.Hook(targetMethod, patchMethod); From 5380141d766944f5d11c0bf4db032a62e2cd1c24 Mon Sep 17 00:00:00 2001 From: Raymond Date: Wed, 3 Nov 2021 21:39:35 -0400 Subject: [PATCH 08/10] Prune tasks from the infinite task list --- .../Interface/Internal/Windows/DataWindow.cs | 6 ++++ Dalamud/Logging/Internal/TaskTracker.cs | 29 ++++++++++++++++++- 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/Dalamud/Interface/Internal/Windows/DataWindow.cs b/Dalamud/Interface/Internal/Windows/DataWindow.cs index 7d24198e7..4128d41d3 100644 --- a/Dalamud/Interface/Internal/Windows/DataWindow.cs +++ b/Dalamud/Interface/Internal/Windows/DataWindow.cs @@ -1564,6 +1564,8 @@ namespace Dalamud.Interface.Internal.Windows if (ImGui.CollapsingHeader($"#{task.Id} - {task.Status} {(subTime - task.StartTime).TotalMilliseconds}ms###task{i}")) { + task.IsBeingViewed = true; + if (ImGui.Button("CANCEL (May not work)")) { try @@ -1589,6 +1591,10 @@ namespace Dalamud.Interface.Internal.Windows ImGui.TextUnformatted(task.Exception.ToString()); } } + else + { + task.IsBeingViewed = false; + } ImGui.PopStyleColor(1); } diff --git a/Dalamud/Logging/Internal/TaskTracker.cs b/Dalamud/Logging/Internal/TaskTracker.cs index fb99498a2..3888d55db 100644 --- a/Dalamud/Logging/Internal/TaskTracker.cs +++ b/Dalamud/Logging/Internal/TaskTracker.cs @@ -15,6 +15,7 @@ namespace Dalamud.Logging.Internal /// internal class TaskTracker : IDisposable { + private static readonly ModuleLog Log = new("TT"); private static readonly List TrackedTasksInternal = new(); private static readonly ConcurrentQueue NewlyCreatedTasks = new(); private static bool clearRequested = false; @@ -53,12 +54,33 @@ namespace Dalamud.Logging.Internal clearRequested = false; } + var i = 0; + var pruned = 0; + // Prune old tasks. Technically a memory "leak" that grows infinitely otherwise. + while (i < TrackedTasksInternal.Count) + { + var taskInfo = TrackedTasksInternal[i]; + if (taskInfo.IsCompleted && !taskInfo.IsBeingViewed && TrackedTasksInternal.Count > 1000) + { + TrackedTasksInternal.RemoveAt(i); + pruned++; + continue; + } + + i++; + } + + // if (pruned > 0) + // Log.Debug($"Pruned {pruned} tasks"); + + // Consume from a queue to prevent iteration errors while (NewlyCreatedTasks.TryDequeue(out var newTask)) { TrackedTasksInternal.Add(newTask); } - for (var i = 0; i < TrackedTasksInternal.Count; i++) + // Update each task + for (i = 0; i < TrackedTasksInternal.Count; i++) { var taskInfo = TrackedTasksInternal[i]; if (taskInfo.Task == null) @@ -179,6 +201,11 @@ namespace Dalamud.Logging.Internal /// public bool IsCompletedSuccessfully { get; set; } + /// + /// Gets or sets a value indicating whether this task is being viewed. + /// + public bool IsBeingViewed { get; set; } + /// /// Gets or sets the status of the task. /// From eca4ce05fd2dd398d66e0b09c1b6a44a92b96a69 Mon Sep 17 00:00:00 2001 From: Raymond Date: Wed, 3 Nov 2021 22:14:37 -0400 Subject: [PATCH 09/10] Solve minhook crash on plugin reload --- Dalamud/Hooking/Hook.cs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/Dalamud/Hooking/Hook.cs b/Dalamud/Hooking/Hook.cs index e7c040e05..430527ed0 100644 --- a/Dalamud/Hooking/Hook.cs +++ b/Dalamud/Hooking/Hook.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Reflection; using Dalamud.Configuration.Internal; @@ -59,9 +60,9 @@ namespace Dalamud.Hooking this.address = address; if (this.isMinHook) { - var indexList = hasOtherHooks - ? HookManager.MultiHookTracker[address] - : (HookManager.MultiHookTracker[address] = new()); + if (!HookManager.MultiHookTracker.TryGetValue(address, out var indexList)) + indexList = HookManager.MultiHookTracker[address] = new(); + var index = (ulong)indexList.Count; this.minHookImpl = new MinSharp.Hook(address, detour, index); @@ -191,7 +192,9 @@ namespace Dalamud.Hooking if (this.isMinHook) { this.minHookImpl.Dispose(); - HookManager.MultiHookTracker[this.address] = null; + + var index = HookManager.MultiHookTracker[this.address].IndexOf(this); + HookManager.MultiHookTracker[this.address][index] = null; } else { From 003646e48d28807297d895edafdac80c4527982c Mon Sep 17 00:00:00 2001 From: Raymond Date: Thu, 4 Nov 2021 09:24:36 -0400 Subject: [PATCH 10/10] Docstring fix --- Dalamud/Configuration/Internal/EnvironmentConfiguration.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dalamud/Configuration/Internal/EnvironmentConfiguration.cs b/Dalamud/Configuration/Internal/EnvironmentConfiguration.cs index 816282ab0..641cd5c2b 100644 --- a/Dalamud/Configuration/Internal/EnvironmentConfiguration.cs +++ b/Dalamud/Configuration/Internal/EnvironmentConfiguration.cs @@ -28,7 +28,7 @@ namespace Dalamud.Configuration.Internal public static bool DalamudForceReloaded { get; } = GetEnvironmentVariable("DALAMUD_FORCE_RELOADED"); /// - /// Gets a value indicating whether the DalamudForceCoreHook setting has been enabled. + /// Gets a value indicating whether the DalamudForceMinHook setting has been enabled. /// public static bool DalamudForceMinHook { get; } = GetEnvironmentVariable("DALAMUD_FORCE_MINHOOK");