From 23c1ee9dc6fd64914b691582ac479d77373c5ae4 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Fri, 10 Mar 2023 15:16:38 +0100 Subject: [PATCH] Move Validating outside of main class. --- Penumbra/Meta/Manager/MetaManager.Imc.cs | 2 +- Penumbra/Penumbra.cs | 113 ++++------------------- Penumbra/UI/ConfigWindow.cs | 14 +-- Penumbra/ValidityChecker.cs | 87 +++++++++++++++++ 4 files changed, 115 insertions(+), 101 deletions(-) create mode 100644 Penumbra/ValidityChecker.cs diff --git a/Penumbra/Meta/Manager/MetaManager.Imc.cs b/Penumbra/Meta/Manager/MetaManager.Imc.cs index 406b5671..661a692c 100644 --- a/Penumbra/Meta/Manager/MetaManager.Imc.cs +++ b/Penumbra/Meta/Manager/MetaManager.Imc.cs @@ -83,7 +83,7 @@ public partial class MetaManager } catch( ImcException e ) { - Penumbra.ImcExceptions.Add( e ); + Penumbra.ValidityChecker.ImcExceptions.Add( e ); Penumbra.Log.Error( e.ToString() ); } catch( Exception e ) diff --git a/Penumbra/Penumbra.cs b/Penumbra/Penumbra.cs index e440d9e4..524bd68c 100644 --- a/Penumbra/Penumbra.cs +++ b/Penumbra/Penumbra.cs @@ -24,7 +24,6 @@ using Penumbra.GameData.Data; using Penumbra.Interop.Loader; using Penumbra.Interop.Resolver; using Penumbra.Mods; -using Action = System.Action; using CharacterUtility = Penumbra.Interop.CharacterUtility; using DalamudUtil = Dalamud.Utility.Util; using ResidentResourceManager = Penumbra.Interop.ResidentResourceManager; @@ -33,10 +32,6 @@ namespace Penumbra; public class Penumbra : IDalamudPlugin { - public const string Repository = "https://raw.githubusercontent.com/xivdev/Penumbra/master/repo.json"; - public const string RepositoryLower = "https://raw.githubusercontent.com/xivdev/penumbra/master/repo.json"; - public const string TestRepositoryLower = "https://raw.githubusercontent.com/xivdev/penumbra/test/repo.json"; - public string Name => "Penumbra"; @@ -45,10 +40,6 @@ public class Penumbra : IDalamudPlugin public static readonly string CommitHash = Assembly.GetExecutingAssembly().GetCustomAttribute< AssemblyInformationalVersionAttribute >()?.InformationalVersion ?? "Unknown"; - public static bool DevPenumbraExists; - public static bool IsNotInstalledPenumbra; - public static bool IsValidSourceRepo; - public static Logger Log { get; private set; } = null!; public static Configuration Config { get; private set; } = null!; @@ -67,26 +58,25 @@ public class Penumbra : IDalamudPlugin public static StainManager StainManager { get; private set; } = null!; public static ItemData ItemData { get; private set; } = null!; + public static ValidityChecker ValidityChecker { get; private set; } = null!; public static PerformanceTracker< PerformanceType > Performance { get; private set; } = null!; public static readonly StartTimeTracker< StartTimeType > StartTimer = new(); - public static readonly List< Exception > ImcExceptions = new(); - - public readonly ResourceLogger ResourceLogger; - public readonly PathResolver PathResolver; - public readonly ObjectReloader ObjectReloader; - public readonly ModFileSystem ModFileSystem; - public readonly PenumbraApi Api; - public readonly HttpApi HttpApi; - public readonly PenumbraIpcProviders IpcProviders; + public readonly ResourceLogger ResourceLogger; + public readonly PathResolver PathResolver; + public readonly ObjectReloader ObjectReloader; + public readonly ModFileSystem ModFileSystem; + 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; - private readonly ResourceWatcher _resourceWatcher; + private readonly ResourceWatcher _resourceWatcher; public Penumbra( DalamudPluginInterface pluginInterface ) { @@ -96,11 +86,9 @@ public class Penumbra : IDalamudPlugin { Dalamud.Initialize( pluginInterface ); - Performance = new PerformanceTracker< PerformanceType >( Dalamud.Framework ); - Log = new Logger(); - DevPenumbraExists = CheckDevPluginPenumbra(); - IsNotInstalledPenumbra = CheckIsNotInstalled(); - IsValidSourceRepo = CheckSourceRepo(); + Performance = new PerformanceTracker< PerformanceType >( Dalamud.Framework ); + Log = new Logger(); + ValidityChecker = new ValidityChecker( Dalamud.PluginInterface ); GameEvents = new GameEventManager(); StartTimer.Measure( StartTimeType.Identifier, () => Identifier = GameData.GameData.GetIdentifier( Dalamud.PluginInterface, Dalamud.GameData ) ); @@ -168,17 +156,10 @@ public class Penumbra : IDalamudPlugin SubscribeItemLinks(); } - if( ImcExceptions.Count > 0 ) - { - Log.Error( $"{ImcExceptions} IMC Exceptions thrown. Please repair your game files." ); - } - else - { - Log.Information( $"Penumbra Version {Version}, Commit #{CommitHash} successfully Loaded from {pluginInterface.SourceRepository}." ); - } - 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 ); Log.Information( $"Loading native OtterTex assembly from {OtterTex.NativeDll.Directory}." ); @@ -195,16 +176,16 @@ public class Penumbra : IDalamudPlugin } private void SetupInterface( out ConfigWindow cfg, out LaunchButton btn, out WindowSystem system, out Changelog changelog ) - { - using var tInterface = StartTimer.Measure( StartTimeType.Interface ); + { + 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 ); - system.AddWindow( cfg.ModEditPopup ); - system.AddWindow( changelog ); - Dalamud.PluginInterface.UiBuilder.OpenConfigUi += cfg.Toggle; + system.AddWindow( cfg.ModEditPopup ); + system.AddWindow( changelog ); + Dalamud.PluginInterface.UiBuilder.OpenConfigUi += cfg.Toggle; } private void DisposeInterface() @@ -347,7 +328,7 @@ public class Penumbra : IDalamudPlugin sb.Append( $"> **`Mods with File Redirections: `** {ModManager.Count( m => m.TotalFileCount > 0 )}, Total: {ModManager.Sum( m => m.TotalFileCount )}\n" ); sb.Append( $"> **`Mods with FileSwaps: `** {ModManager.Count( m => m.TotalSwapCount > 0 )}, Total: {ModManager.Sum( m => m.TotalSwapCount )}\n" ); sb.Append( $"> **`Mods with Meta Manipulations:`** {ModManager.Count( m => m.TotalManipulations > 0 )}, Total {ModManager.Sum( m => m.TotalManipulations )}\n" ); - sb.Append( $"> **`IMC Exceptions Thrown: `** {ImcExceptions.Count}\n" ); + sb.Append( $"> **`IMC Exceptions Thrown: `** {ValidityChecker.ImcExceptions.Count}\n" ); sb.Append( $"> **`#Temp Mods: `** {TempMods.Mods.Sum( kvp => kvp.Value.Count ) + TempMods.ModsForAllCollections.Count}\n" ); string CharacterName( ActorIdentifier id, string name ) @@ -395,58 +376,4 @@ public class Penumbra : IDalamudPlugin return sb.ToString(); } - - // Because remnants of penumbra in devPlugins cause issues, we check for them to warn users to remove them. - private static bool CheckDevPluginPenumbra() - { -#if !DEBUG - var path = Path.Combine( Dalamud.PluginInterface.DalamudAssetDirectory.Parent?.FullName ?? "INVALIDPATH", "devPlugins", "Penumbra" ); - var dir = new DirectoryInfo( path ); - - try - { - return dir.Exists && dir.EnumerateFiles( "*.dll", SearchOption.AllDirectories ).Any(); - } - catch( Exception e ) - { - Log.Error( $"Could not check for dev plugin Penumbra:\n{e}" ); - return true; - } -#else - return false; -#endif - } - - // Check if the loaded version of Penumbra itself is in devPlugins. - private static bool CheckIsNotInstalled() - { -#if !DEBUG - var checkedDirectory = Dalamud.PluginInterface.AssemblyLocation.Directory?.Parent?.Parent?.Name; - var ret = checkedDirectory?.Equals( "installedPlugins", StringComparison.OrdinalIgnoreCase ) ?? false; - if( !ret ) - { - Log.Error( $"Penumbra is not correctly installed. Application loaded from \"{Dalamud.PluginInterface.AssemblyLocation.Directory!.FullName}\"." ); - } - - return !ret; -#else - return false; -#endif - } - - // Check if the loaded version of Penumbra is installed from a valid source repo. - private static bool CheckSourceRepo() - { -#if !DEBUG - return Dalamud.PluginInterface.SourceRepository.Trim().ToLowerInvariant() switch - { - null => false, - RepositoryLower => true, - TestRepositoryLower => true, - _ => false, - }; -#else - return true; -#endif - } } \ No newline at end of file diff --git a/Penumbra/UI/ConfigWindow.cs b/Penumbra/UI/ConfigWindow.cs index b16252c5..f836d7e5 100644 --- a/Penumbra/UI/ConfigWindow.cs +++ b/Penumbra/UI/ConfigWindow.cs @@ -86,21 +86,21 @@ public sealed partial class ConfigWindow : Window, IDisposable try { - if( Penumbra.ImcExceptions.Count > 0 ) + if( Penumbra.ValidityChecker.ImcExceptions.Count > 0 ) { - DrawProblemWindow( $"There were {Penumbra.ImcExceptions.Count} errors while trying to load IMC files from the game data.\n" + DrawProblemWindow( $"There were {Penumbra.ValidityChecker.ImcExceptions.Count} errors while trying to load IMC files from the game data.\n" + "This usually means that your game installation was corrupted by updating the game while having TexTools mods still active.\n" + "It is recommended to not use TexTools and Penumbra (or other Lumina-based tools) at the same time.\n\n" + "Please use the Launcher's Repair Game Files function to repair your client installation.", true ); } - else if( !Penumbra.IsValidSourceRepo ) + else if( !Penumbra.ValidityChecker.IsValidSourceRepo ) { DrawProblemWindow( $"You are loading a release version of Penumbra from the repository \"{Dalamud.PluginInterface.SourceRepository}\" instead of the official repository.\n" - + $"Please use the official repository at {Penumbra.Repository}.\n\n" + + $"Please use the official repository at {ValidityChecker.Repository}.\n\n" + "If you are developing for Penumbra and see this, you should compile your version in debug mode to avoid it.", false ); } - else if( Penumbra.IsNotInstalledPenumbra ) + else if( Penumbra.ValidityChecker.IsNotInstalledPenumbra ) { DrawProblemWindow( $"You are loading a release version of Penumbra from \"{Dalamud.PluginInterface.AssemblyLocation.Directory?.FullName ?? "Unknown"}\" instead of the installedPlugins directory.\n\n" @@ -108,7 +108,7 @@ public sealed partial class ConfigWindow : Window, IDisposable + "If you do not know how to do this, please take a look at the readme in Penumbras github repository or join us in discord.\n" + "If you are developing for Penumbra and see this, you should compile your version in debug mode to avoid it.", false ); } - else if( Penumbra.DevPenumbraExists ) + else if( Penumbra.ValidityChecker.DevPenumbraExists ) { DrawProblemWindow( $"You are loading a installed version of Penumbra from \"{Dalamud.PluginInterface.AssemblyLocation.Directory?.FullName ?? "Unknown"}\", " @@ -153,7 +153,7 @@ public sealed partial class ConfigWindow : Window, IDisposable ImGui.TextUnformatted( "Exceptions" ); ImGui.Separator(); using var box = ImRaii.ListBox( "##Exceptions", new Vector2( -1, -1 ) ); - foreach( var exception in Penumbra.ImcExceptions ) + foreach( var exception in Penumbra.ValidityChecker.ImcExceptions ) { ImGuiUtil.TextWrapped( exception.ToString() ); ImGui.Separator(); diff --git a/Penumbra/ValidityChecker.cs b/Penumbra/ValidityChecker.cs new file mode 100644 index 00000000..6b55fc16 --- /dev/null +++ b/Penumbra/ValidityChecker.cs @@ -0,0 +1,87 @@ +using System; +using System.Collections.Generic; +using Dalamud.Interface.Internal.Notifications; +using Dalamud.Plugin; +using Penumbra.Util; + +namespace Penumbra; + +public class ValidityChecker +{ + public const string Repository = "https://raw.githubusercontent.com/xivdev/Penumbra/master/repo.json"; + public const string RepositoryLower = "https://raw.githubusercontent.com/xivdev/penumbra/master/repo.json"; + public const string TestRepositoryLower = "https://raw.githubusercontent.com/xivdev/penumbra/test/repo.json"; + + public readonly bool DevPenumbraExists; + public readonly bool IsNotInstalledPenumbra; + public readonly bool IsValidSourceRepo; + + public readonly List ImcExceptions = new(); + + public ValidityChecker(DalamudPluginInterface pi) + { + DevPenumbraExists = CheckDevPluginPenumbra(pi); + IsNotInstalledPenumbra = CheckIsNotInstalled(pi); + IsValidSourceRepo = CheckSourceRepo(pi); + } + + public void LogExceptions() + { + if( ImcExceptions.Count > 0 ) + ChatUtil.NotificationMessage( $"{ImcExceptions} IMC Exceptions thrown during Penumbra load. Please repair your game files.", "Warning", NotificationType.Warning ); + } + + // Because remnants of penumbra in devPlugins cause issues, we check for them to warn users to remove them. + private static bool CheckDevPluginPenumbra( DalamudPluginInterface pi ) + { +#if !DEBUG + var path = Path.Combine( pi.DalamudAssetDirectory.Parent?.FullName ?? "INVALIDPATH", "devPlugins", "Penumbra" ); + var dir = new DirectoryInfo( path ); + + try + { + return dir.Exists && dir.EnumerateFiles( "*.dll", SearchOption.AllDirectories ).Any(); + } + catch( Exception e ) + { + Log.Error( $"Could not check for dev plugin Penumbra:\n{e}" ); + return true; + } +#else + return false; +#endif + } + + // Check if the loaded version of Penumbra itself is in devPlugins. + private static bool CheckIsNotInstalled( DalamudPluginInterface pi ) + { +#if !DEBUG + var checkedDirectory = pi.AssemblyLocation.Directory?.Parent?.Parent?.Name; + var ret = checkedDirectory?.Equals( "installedPlugins", StringComparison.OrdinalIgnoreCase ) ?? false; + if( !ret ) + { + Log.Error( $"Penumbra is not correctly installed. Application loaded from \"{pi.AssemblyLocation.Directory!.FullName}\"." ); + } + + return !ret; +#else + return false; +#endif + } + + // Check if the loaded version of Penumbra is installed from a valid source repo. + private static bool CheckSourceRepo( DalamudPluginInterface pi ) + { +#if !DEBUG + return pi.SourceRepository.Trim().ToLowerInvariant() switch + { + null => false, + RepositoryLower => true, + TestRepositoryLower => true, + _ => false, + }; +#else + return true; +#endif + } +} \ No newline at end of file