From d8e2a5ba28bf1432a9058390b22304a883b01ec8 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Fri, 10 Mar 2023 15:17:45 +0100 Subject: [PATCH] Move UI Building to thread. --- Penumbra.Api | 2 +- Penumbra/Api/PenumbraApi.cs | 16 ++++++++- Penumbra/Penumbra.cs | 71 +++++++++++++++++++++++++++---------- 3 files changed, 68 insertions(+), 21 deletions(-) diff --git a/Penumbra.Api b/Penumbra.Api index a2b680a5..f66e49bd 160000 --- a/Penumbra.Api +++ b/Penumbra.Api @@ -1 +1 @@ -Subproject commit a2b680a5991d9287c2dcda7cfa54183c37384fd0 +Subproject commit f66e49bde2878542de17edf428de61f6c8a42efc diff --git a/Penumbra/Api/PenumbraApi.cs b/Penumbra/Api/PenumbraApi.cs index fc5b2006..da560ca9 100644 --- a/Penumbra/Api/PenumbraApi.cs +++ b/Penumbra/Api/PenumbraApi.cs @@ -174,16 +174,24 @@ public class PenumbraApi : IDisposable, IPenumbraApi public PenumbraApiEc OpenMainWindow( TabType tab, string modDirectory, string modName ) { CheckInitialized(); + if( _penumbra!.ConfigWindow == null ) + { + return PenumbraApiEc.SystemDisposed; + } _penumbra!.ConfigWindow.IsOpen = true; if( !Enum.IsDefined( tab ) ) + { return PenumbraApiEc.InvalidArgument; + } if( tab != TabType.None ) + { _penumbra!.ConfigWindow.SelectTab = tab; + } - if( tab == TabType.Mods && (modDirectory.Length > 0 || modName.Length > 0) ) + if( tab == TabType.Mods && ( modDirectory.Length > 0 || modName.Length > 0 ) ) { if( Penumbra.ModManager.TryGetMod( modDirectory, modName, out var mod ) ) { @@ -194,12 +202,18 @@ public class PenumbraApi : IDisposable, IPenumbraApi return PenumbraApiEc.ModMissing; } } + return PenumbraApiEc.Success; } public void CloseMainWindow() { CheckInitialized(); + if( _penumbra!.ConfigWindow == null ) + { + return; + } + _penumbra!.ConfigWindow.IsOpen = false; } diff --git a/Penumbra/Penumbra.cs b/Penumbra/Penumbra.cs index 524bd68c..8d8c70d2 100644 --- a/Penumbra/Penumbra.cs +++ b/Penumbra/Penumbra.cs @@ -4,6 +4,7 @@ using System.IO; using System.Linq; using System.Reflection; using System.Text; +using System.Threading.Tasks; using Dalamud.Interface.Windowing; using Dalamud.Plugin; using ImGuiNET; @@ -56,7 +57,6 @@ public class Penumbra : IDalamudPlugin public static IObjectIdentifier Identifier { get; private set; } = null!; public static IGamePathParser GamePathParser { get; private set; } = null!; public static StainManager StainManager { get; private set; } = null!; - public static ItemData ItemData { get; private set; } = null!; public static ValidityChecker ValidityChecker { get; private set; } = null!; @@ -71,12 +71,15 @@ public class Penumbra : IDalamudPlugin public readonly PenumbraApi Api; public readonly HttpApi HttpApi; public readonly PenumbraIpcProviders IpcProviders; - internal readonly ConfigWindow ConfigWindow; - private readonly LaunchButton _launchButton; - private readonly WindowSystem _windowSystem; - private readonly Changelog _changelog; - private readonly CommandHandler _commandHandler; + internal ConfigWindow? ConfigWindow { get; private set; } + private LaunchButton? _launchButton; + private WindowSystem? _windowSystem; + private Changelog? _changelog; + private CommandHandler? _commandHandler; private readonly ResourceWatcher _resourceWatcher; + private bool _disposed; + + public static ItemData ItemData { get; private set; } = null!; public Penumbra( DalamudPluginInterface pluginInterface ) { @@ -94,7 +97,7 @@ public class Penumbra : IDalamudPlugin StartTimer.Measure( StartTimeType.Identifier, () => Identifier = GameData.GameData.GetIdentifier( Dalamud.PluginInterface, Dalamud.GameData ) ); StartTimer.Measure( StartTimeType.GamePathParser, () => GamePathParser = GameData.GameData.GetGamePathParser() ); StartTimer.Measure( StartTimeType.Stains, () => StainManager = new StainManager( Dalamud.PluginInterface, Dalamud.GameData ) ); - StartTimer.Measure( StartTimeType.Items, () => ItemData = new ItemData( Dalamud.PluginInterface, Dalamud.GameData, Dalamud.GameData.Language ) ); + ItemData = StartTimer.Measure( StartTimeType.Items, () => new ItemData( Dalamud.PluginInterface, Dalamud.GameData, Dalamud.GameData.Language ) ); StartTimer.Measure( StartTimeType.Actors, () => Actors = new ActorManager( Dalamud.PluginInterface, Dalamud.Objects, Dalamud.ClientState, Dalamud.Framework, Dalamud.GameData, Dalamud.GameGui, ResolveCutscene ) ); @@ -128,8 +131,7 @@ public class Penumbra : IDalamudPlugin ObjectReloader = new ObjectReloader(); PathResolver = new PathResolver( ResourceLoader ); - SetupInterface( out ConfigWindow, out _launchButton, out _windowSystem, out _changelog ); - _commandHandler = new CommandHandler( Dalamud.Commands, ObjectReloader, Config, this, ConfigWindow, ModManager, CollectionManager, Actors ); + SetupInterface(); if( Config.EnableMods ) { @@ -140,7 +142,6 @@ public class Penumbra : IDalamudPlugin if( Config.DebugMode ) { ResourceLoader.EnableDebug(); - ConfigWindow.IsOpen = true; } using( var tApi = StartTimer.Measure( StartTimeType.Api ) ) @@ -156,8 +157,6 @@ public class Penumbra : IDalamudPlugin SubscribeItemLinks(); } - Dalamud.PluginInterface.UiBuilder.Draw += _windowSystem.Draw; - ValidityChecker.LogExceptions(); Log.Information( $"Penumbra Version {Version}, Commit #{CommitHash} successfully Loaded from {pluginInterface.SourceRepository}." ); OtterTex.NativeDll.Initialize( Dalamud.PluginInterface.AssemblyLocation.DirectoryName ); @@ -175,17 +174,40 @@ public class Penumbra : IDalamudPlugin } } - private void SetupInterface( out ConfigWindow cfg, out LaunchButton btn, out WindowSystem system, out Changelog changelog ) + private void SetupInterface() + { + Task.Run( () => { using var tInterface = StartTimer.Measure( StartTimeType.Interface ); - cfg = new ConfigWindow( this, _resourceWatcher ); - btn = new LaunchButton( ConfigWindow ); - system = new WindowSystem( Name ); - changelog = ConfigWindow.CreateChangelog(); - system.AddWindow( ConfigWindow ); + var changelog = ConfigWindow.CreateChangelog(); + var cfg = new ConfigWindow( this, _resourceWatcher ) + { + IsOpen = Config.DebugMode, + }; + var btn = new LaunchButton( cfg ); + var system = new WindowSystem( Name ); + var cmd = new CommandHandler( Dalamud.Commands, ObjectReloader, Config, this, cfg, ModManager, CollectionManager, Actors ); + system.AddWindow( cfg ); system.AddWindow( cfg.ModEditPopup ); system.AddWindow( changelog ); + if( !_disposed ) + { + _changelog = changelog; + ConfigWindow = cfg; + _windowSystem = system; + _launchButton = btn; + _commandHandler = cmd; Dalamud.PluginInterface.UiBuilder.OpenConfigUi += cfg.Toggle; + Dalamud.PluginInterface.UiBuilder.Draw += _windowSystem.Draw; + } + else + { + cfg.Dispose(); + btn.Dispose(); + cmd.Dispose(); + } + } + ); } private void DisposeInterface() @@ -243,7 +265,12 @@ public class Penumbra : IDalamudPlugin } public void ForceChangelogOpen() - => _changelog.ForceOpen = true; + { + if( _changelog != null ) + { + _changelog.ForceOpen = true; + } + } private void SubscribeItemLinks() { @@ -268,6 +295,12 @@ public class Penumbra : IDalamudPlugin public void Dispose() { + if( _disposed ) + { + return; + } + + _disposed = true; HttpApi?.Dispose(); IpcProviders?.Dispose(); Api?.Dispose();