From 692113958b3481aa75ce7d727d2ae158e143d619 Mon Sep 17 00:00:00 2001 From: MidoriKami <9083275+MidoriKami@users.noreply.github.com> Date: Tue, 5 Sep 2023 14:35:08 -0700 Subject: [PATCH] Scope DTRBar --- Dalamud/Game/Gui/Dtr/DtrBar.cs | 59 ++++++++++++++++++++++++++++-- Dalamud/Plugin/Services/IDtrBar.cs | 6 +++ 2 files changed, 62 insertions(+), 3 deletions(-) diff --git a/Dalamud/Game/Gui/Dtr/DtrBar.cs b/Dalamud/Game/Gui/Dtr/DtrBar.cs index 8b021bc7a..2ff99a450 100644 --- a/Dalamud/Game/Gui/Dtr/DtrBar.cs +++ b/Dalamud/Game/Gui/Dtr/DtrBar.cs @@ -26,9 +26,6 @@ namespace Dalamud.Game.Gui.Dtr; [PluginInterface] [InterfaceVersion("1.0")] [ServiceManager.BlockingEarlyLoadedService] -#pragma warning disable SA1015 -[ResolveVia] -#pragma warning restore SA1015 public sealed unsafe class DtrBar : IDisposable, IServiceType, IDtrBar { private const uint BaseNodeId = 1000; @@ -94,6 +91,15 @@ public sealed unsafe class DtrBar : IDisposable, IServiceType, IDtrBar return entry; } + + /// + public void Remove(string title) + { + if (this.entries.FirstOrDefault(entry => entry.Title == title) is { } dtrBarEntry) + { + dtrBarEntry.Remove(); + } + } /// void IDisposable.Dispose() @@ -497,3 +503,50 @@ public sealed unsafe class DtrBar : IDisposable, IServiceType, IDtrBar } } } + +/// +/// Plugin-scoped version of a AddonEventManager service. +/// +[PluginInterface] +[InterfaceVersion("1.0")] +[ServiceManager.ScopedService] +#pragma warning disable SA1015 +[ResolveVia] +#pragma warning restore SA1015 +internal class DtrBarPluginScoped : IDisposable, IServiceType, IDtrBar +{ + [ServiceManager.ServiceDependency] + private readonly DtrBar dtrBarService = Service.Get(); + + private readonly Dictionary pluginEntries = new(); + + /// + public void Dispose() + { + foreach (var entry in this.pluginEntries) + { + entry.Value.Remove(); + } + + this.pluginEntries.Clear(); + } + + /// + public DtrBarEntry Get(string title, SeString? text = null) + { + // If we already have a known entry for this plugin, return it. + if (this.pluginEntries.TryGetValue(title, out var existingEntry)) return existingEntry; + + return this.pluginEntries[title] = this.dtrBarService.Get(title, text); + } + + /// + public void Remove(string title) + { + if (this.pluginEntries.TryGetValue(title, out var existingEntry)) + { + existingEntry.Remove(); + this.pluginEntries.Remove(title); + } + } +} diff --git a/Dalamud/Plugin/Services/IDtrBar.cs b/Dalamud/Plugin/Services/IDtrBar.cs index 6c2b8ad1e..a5a750cf6 100644 --- a/Dalamud/Plugin/Services/IDtrBar.cs +++ b/Dalamud/Plugin/Services/IDtrBar.cs @@ -19,4 +19,10 @@ public interface IDtrBar /// The entry object used to update, hide and remove the entry. /// Thrown when an entry with the specified title exists. public DtrBarEntry Get(string title, SeString? text = null); + + /// + /// Removes a DTR bar entry from the system. + /// + /// Title of the entry to remove. + public void Remove(string title); }