diff --git a/Dalamud/Game/Addon/Lifecycle/AddonLifecycle.cs b/Dalamud/Game/Addon/Lifecycle/AddonLifecycle.cs index 4da8e429c..d07821149 100644 --- a/Dalamud/Game/Addon/Lifecycle/AddonLifecycle.cs +++ b/Dalamud/Game/Addon/Lifecycle/AddonLifecycle.cs @@ -31,6 +31,7 @@ internal unsafe class AddonLifecycle : IInternalDisposableService private readonly Framework framework = Service.Get(); private Hook? onInitializeAddonHook; + private bool isInvokingListeners = false; [ServiceManager.ServiceConstructor] private AddonLifecycle() @@ -61,7 +62,7 @@ internal unsafe class AddonLifecycle : IInternalDisposableService /// The listener to register. internal void RegisterListener(AddonLifecycleEventListener listener) { - this.framework.RunOnFrameworkThread(() => + this.framework.RunOnTick(() => { if (!this.EventListeners.ContainsKey(listener.EventType)) { @@ -77,7 +78,7 @@ internal unsafe class AddonLifecycle : IInternalDisposableService } this.EventListeners[listener.EventType][listener.AddonName].Add(listener); - }); + }, delayTicks: this.isInvokingListeners ? 1 : 0); } /// @@ -86,7 +87,7 @@ internal unsafe class AddonLifecycle : IInternalDisposableService /// The listener to unregister. internal void UnregisterListener(AddonLifecycleEventListener listener) { - this.framework.RunOnFrameworkThread(() => + this.framework.RunOnTick(() => { if (this.EventListeners.TryGetValue(listener.EventType, out var addonListeners)) { @@ -95,7 +96,7 @@ internal unsafe class AddonLifecycle : IInternalDisposableService addonListener.Remove(listener); } } - }); + }, delayTicks: this.isInvokingListeners ? 1 : 0); } /// @@ -106,6 +107,8 @@ internal unsafe class AddonLifecycle : IInternalDisposableService /// What to blame on errors. internal void InvokeListenersSafely(AddonEvent eventType, AddonArgs args, [CallerMemberName] string blame = "") { + this.isInvokingListeners = true; + // Early return if we don't have any listeners of this type if (!this.EventListeners.TryGetValue(eventType, out var addonListeners)) return; @@ -140,6 +143,8 @@ internal unsafe class AddonLifecycle : IInternalDisposableService } } } + + this.isInvokingListeners = false; } ///