From be9216bbc53bd17bf69b5c7e4ba77ea15e2839d1 Mon Sep 17 00:00:00 2001 From: Soreepeong Date: Thu, 21 Dec 2023 15:59:38 +0900 Subject: [PATCH] Make ChatGui lazy dependency of PM --- Dalamud/Game/Gui/ChatGui.cs | 4 +- Dalamud/Plugin/Internal/PluginManager.cs | 92 ++++++++++++++---------- 2 files changed, 55 insertions(+), 41 deletions(-) diff --git a/Dalamud/Game/Gui/ChatGui.cs b/Dalamud/Game/Gui/ChatGui.cs index 9128a2a31..2361ec653 100644 --- a/Dalamud/Game/Gui/ChatGui.cs +++ b/Dalamud/Game/Gui/ChatGui.cs @@ -29,9 +29,7 @@ namespace Dalamud.Game.Gui; /// This class handles interacting with the native chat UI. /// [InterfaceVersion("1.0")] -[ServiceManager.BlockingEarlyLoadedService($"{nameof(PluginManager)} currently uses this.")] -// ^ TODO: This seems unnecessary, remove the hard dependency at a later time. -// Otherwise, if PM eventually marks this class as required, note that in the comment above. +[ServiceManager.EarlyLoadedService] internal sealed unsafe class ChatGui : IInternalDisposableService, IChatGui { private static readonly ModuleLog Log = new("ChatGui"); diff --git a/Dalamud/Plugin/Internal/PluginManager.cs b/Dalamud/Plugin/Internal/PluginManager.cs index fa768b495..5924a4e08 100644 --- a/Dalamud/Plugin/Internal/PluginManager.cs +++ b/Dalamud/Plugin/Internal/PluginManager.cs @@ -60,7 +60,7 @@ internal partial class PluginManager : IInternalDisposableService private readonly List availablePluginsList = new(); private readonly List updatablePluginsList = new(); - private readonly DalamudLinkPayload openInstallerWindowPluginChangelogsLink; + private readonly Task openInstallerWindowPluginChangelogsLink; [ServiceManager.ServiceDependency] private readonly DalamudConfiguration configuration = Service.Get(); @@ -74,9 +74,6 @@ internal partial class PluginManager : IInternalDisposableService [ServiceManager.ServiceDependency] private readonly HappyHttpClient happyHttpClient = Service.Get(); - [ServiceManager.ServiceDependency] - private readonly ChatGui chatGui = Service.Get(); - static PluginManager() { DalamudApiLevel = typeof(PluginManager).Assembly.GetName().Version!.Major; @@ -125,10 +122,16 @@ internal partial class PluginManager : IInternalDisposableService throw new InvalidDataException("Couldn't deserialize banned plugins manifest."); } - this.openInstallerWindowPluginChangelogsLink = this.chatGui.AddChatLinkHandler("Dalamud", 1003, (_, _) => - { - Service.GetNullable()?.OpenPluginInstallerTo(PluginInstallerWindow.PluginInstallerOpenKind.Changelogs); - }); + this.openInstallerWindowPluginChangelogsLink = + Service.GetAsync().ContinueWith( + chatGuiTask => chatGuiTask.Result.AddChatLinkHandler( + "Dalamud", + 1003, + (_, _) => + { + Service.GetNullable()?.OpenPluginInstallerTo( + PluginInstallerWindow.PluginInstallerOpenKind.Changelogs); + })); this.configuration.PluginTestingOptIns ??= new(); this.MainRepo = PluginRepository.CreateMainRepo(this.happyHttpClient); @@ -292,41 +295,54 @@ internal partial class PluginManager : IInternalDisposableService /// The list of updated plugin metadata. /// The header text to send to chat prior to any update info. public void PrintUpdatedPlugins(List? updateMetadata, string header) - { - if (updateMetadata is { Count: > 0 }) - { - this.chatGui.Print(new XivChatEntry + => Service.GetAsync().ContinueWith( + chatGuiTask => { - Message = new SeString(new List() - { - new TextPayload(header), - new TextPayload(" ["), - new UIForegroundPayload(500), - this.openInstallerWindowPluginChangelogsLink, - new TextPayload(Loc.Localize("DalamudInstallerPluginChangelogHelp", "Open plugin changelogs")), - RawPayload.LinkTerminator, - new UIForegroundPayload(0), - new TextPayload("]"), - }), - }); + if (!chatGuiTask.IsCompletedSuccessfully) + return; - foreach (var metadata in updateMetadata) - { - if (metadata.Status == PluginUpdateStatus.StatusKind.Success) + var chatGui = chatGuiTask.Result; + if (updateMetadata is { Count: > 0 }) { - this.chatGui.Print(Locs.DalamudPluginUpdateSuccessful(metadata.Name, metadata.Version)); - } - else - { - this.chatGui.Print(new XivChatEntry + chatGui.Print( + new XivChatEntry + { + Message = new SeString( + new List() + { + new TextPayload(header), + new TextPayload(" ["), + new UIForegroundPayload(500), + this.openInstallerWindowPluginChangelogsLink.Result, + new TextPayload( + Loc.Localize("DalamudInstallerPluginChangelogHelp", "Open plugin changelogs")), + RawPayload.LinkTerminator, + new UIForegroundPayload(0), + new TextPayload("]"), + }), + }); + + foreach (var metadata in updateMetadata) { - Message = Locs.DalamudPluginUpdateFailed(metadata.Name, metadata.Version, PluginUpdateStatus.LocalizeUpdateStatusKind(metadata.Status)), - Type = XivChatType.Urgent, - }); + if (metadata.Status == PluginUpdateStatus.StatusKind.Success) + { + chatGui.Print(Locs.DalamudPluginUpdateSuccessful(metadata.Name, metadata.Version)); + } + else + { + chatGui.Print( + new XivChatEntry + { + Message = Locs.DalamudPluginUpdateFailed( + metadata.Name, + metadata.Version, + PluginUpdateStatus.LocalizeUpdateStatusKind(metadata.Status)), + Type = XivChatType.Urgent, + }); + } + } } - } - } - } + }); /// /// For a given manifest, determine if the user opted into testing this plugin.