From 92e95400b0a8b60fd282c1e5d16149e544c0e73b Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sun, 11 Jul 2021 23:15:42 +0200 Subject: [PATCH] Changed initial mod directory to be empty and prevent crashes on invalid or no root directories (also add some information to debug). --- Penumbra/Configuration.cs | 2 +- Penumbra/Meta/MetaManager.cs | 6 ++- Penumbra/Mods/ModManager.cs | 48 +++++++++++++------ Penumbra/UI/MenuTabs/TabDebug.cs | 23 ++++++--- Penumbra/UI/MenuTabs/TabImport.cs | 32 +++++++++++-- .../UI/MenuTabs/TabInstalled/TabInstalled.cs | 5 -- Penumbra/UI/MenuTabs/TabSettings.cs | 8 +++- Penumbra/UI/SettingsInterface.cs | 4 +- Penumbra/UI/SettingsMenu.cs | 4 +- 9 files changed, 95 insertions(+), 37 deletions(-) diff --git a/Penumbra/Configuration.cs b/Penumbra/Configuration.cs index 968fef36..3f2c8d0f 100644 --- a/Penumbra/Configuration.cs +++ b/Penumbra/Configuration.cs @@ -22,7 +22,7 @@ namespace Penumbra public bool EnableHttpApi { get; set; } public bool EnableActorWatch { get; set; } = false; - public string ModDirectory { get; set; } = @"D:/ffxiv/fs_mods/"; + public string ModDirectory { get; set; } = string.Empty; public string CurrentCollection { get; set; } = "Default"; public string DefaultCollection { get; set; } = "Default"; diff --git a/Penumbra/Meta/MetaManager.cs b/Penumbra/Meta/MetaManager.cs index d4adf31f..13352ba7 100644 --- a/Penumbra/Meta/MetaManager.cs +++ b/Penumbra/Meta/MetaManager.cs @@ -126,7 +126,11 @@ namespace Penumbra.Meta public void WriteNewFiles() { - Directory.CreateDirectory( _dir.FullName ); + if( _currentFiles.Any() ) + { + Directory.CreateDirectory( _dir.FullName ); + } + foreach( var kvp in _currentFiles.Where( kvp => kvp.Value.Changed ) ) { kvp.Value.Write( _dir, kvp.Key ); diff --git a/Penumbra/Mods/ModManager.cs b/Penumbra/Mods/ModManager.cs index 0075968f..c04e0c11 100644 --- a/Penumbra/Mods/ModManager.cs +++ b/Penumbra/Mods/ModManager.cs @@ -14,26 +14,42 @@ namespace Penumbra.Mods public class ModManager { private readonly Plugin _plugin; - public DirectoryInfo BasePath { get; private set; } + public DirectoryInfo BasePath { get; private set; } = null!; public Dictionary< string, ModData > Mods { get; } = new(); public CollectionManager Collections { get; } + public bool Valid { get; private set; } + public Configuration Config => _plugin.Configuration; + private void SetBaseDirectory( string basePath ) + { + if( basePath.Any() ) + { + BasePath = new DirectoryInfo( basePath ); + Valid = Path.IsPathRooted( basePath ); + } + else + { + BasePath = new DirectoryInfo( "." ); + Valid = false; + } + } + public ModManager( Plugin plugin ) { - _plugin = plugin; - BasePath = new DirectoryInfo( plugin.Configuration.ModDirectory ); - MetaManager.ClearBaseDirectory( BasePath ); + _plugin = plugin; + SetBaseDirectory( plugin.Configuration.ModDirectory ); + MetaManager.ClearBaseDirectory( BasePath! ); Collections = new CollectionManager( plugin, this ); } - public void DiscoverMods( DirectoryInfo basePath ) + public void DiscoverMods( string basePath ) { - BasePath = basePath; + SetBaseDirectory( basePath ); DiscoverMods(); } @@ -62,7 +78,7 @@ namespace Penumbra.Mods public void DiscoverMods() { Mods.Clear(); - if( !BasePath.Exists ) + if( Valid && !BasePath.Exists ) { PluginLog.Debug( "The mod directory {Directory} does not exist.", BasePath.FullName ); try @@ -72,22 +88,26 @@ namespace Penumbra.Mods catch( Exception e ) { PluginLog.Error( $"The mod directory {BasePath.FullName} does not exist and could not be created:\n{e}" ); - return; + Valid = false; } } - foreach( var modFolder in BasePath.EnumerateDirectories() ) + if( Valid ) { - var mod = ModData.LoadMod( modFolder ); - if( mod == null ) + foreach( var modFolder in BasePath.EnumerateDirectories() ) { - continue; + var mod = ModData.LoadMod( modFolder ); + if( mod == null ) + { + continue; + } + + Mods.Add( modFolder.Name, mod ); } - Mods.Add( modFolder.Name, mod ); + SetModOrders( _plugin.Configuration ); } - SetModOrders( _plugin.Configuration ); Collections.RecreateCaches(); } diff --git a/Penumbra/UI/MenuTabs/TabDebug.cs b/Penumbra/UI/MenuTabs/TabDebug.cs index 50b0e69d..56345914 100644 --- a/Penumbra/UI/MenuTabs/TabDebug.cs +++ b/Penumbra/UI/MenuTabs/TabDebug.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.IO; using System.Linq; using System.Numerics; using System.Reflection; @@ -111,6 +112,15 @@ namespace Penumbra.UI } } + private static void PrintValue( string name, string value ) + { + ImGui.TableNextRow(); + ImGui.TableNextColumn(); + ImGui.Text( name ); + ImGui.TableNextColumn(); + ImGui.Text( value ); + } + private void DrawDebugTabGeneral() { if( !ImGui.CollapsingHeader( "General##Debug" ) ) @@ -124,16 +134,17 @@ namespace Penumbra.UI return; } - ImGui.TableNextRow(); - ImGui.TableNextColumn(); + var manager = Service< ModManager >.Get(); + PrintValue( "Active Collection", manager.Collections.ActiveCollection.Name ); + PrintValue( "Mod Manager BasePath", manager.BasePath.Name ); + PrintValue( "Mod Manager BasePath-Full", manager.BasePath.FullName ); + PrintValue( "Mod Manager BasePath IsRooted", Path.IsPathRooted( _plugin.Configuration.ModDirectory ).ToString() ); + PrintValue( "Mod Manager BasePath Exists", Directory.Exists( manager.BasePath.FullName ).ToString() ); + PrintValue( "Mod Manager Valid", manager.Valid.ToString() ); - ImGui.Text( "Active Collection" ); - ImGui.TableNextColumn(); - ImGui.Text( Service< ModManager >.Get().Collections.ActiveCollection.Name ); ImGui.EndTable(); } - private void DrawDebugTabRedraw() { if( !ImGui.CollapsingHeader( "Redrawing##Debug" ) ) diff --git a/Penumbra/UI/MenuTabs/TabImport.cs b/Penumbra/UI/MenuTabs/TabImport.cs index 20f5bb48..95b6200b 100644 --- a/Penumbra/UI/MenuTabs/TabImport.cs +++ b/Penumbra/UI/MenuTabs/TabImport.cs @@ -6,6 +6,7 @@ using System.Windows.Forms; using Dalamud.Plugin; using ImGuiNET; using Penumbra.Importer; +using Penumbra.Mods; using Penumbra.Util; namespace Penumbra.UI @@ -22,7 +23,8 @@ namespace Penumbra.UI private const string TooltipModpack1 = "Writing modpack to disk before extracting..."; private const string FailedImport = "One or more of your modpacks failed to import.\nPlease submit a bug report."; - private const uint ColorRed = 0xFF0000C8; + private const uint ColorRed = 0xFF0000C8; + private const uint ColorYellow = 0xFF00C8C8; private static readonly Vector2 ImportBarSize = new( -1, 0 ); @@ -30,9 +32,13 @@ namespace Penumbra.UI private bool _hasError; private TexToolsImport? _texToolsImport; private readonly SettingsInterface _base; + private readonly ModManager _manager; public TabImport( SettingsInterface ui ) - => _base = ui; + { + _base = ui; + _manager = Service< ModManager >.Get(); + } public bool IsImporting() => _isImportRunning; @@ -64,7 +70,7 @@ namespace Penumbra.UI try { - _texToolsImport = new TexToolsImport( new DirectoryInfo( _base._plugin!.Configuration!.ModDirectory ) ); + _texToolsImport = new TexToolsImport( _manager.BasePath ); _texToolsImport.ImportModPack( new FileInfo( fileName ) ); PluginLog.Log( $"-> {fileName} OK!" ); @@ -96,7 +102,25 @@ namespace Penumbra.UI private void DrawImportButton() { - if( ImGui.Button( LabelImportButton ) ) + if( !_manager.Valid ) + { + ImGui.PushStyleVar( ImGuiStyleVar.Alpha, 0.5f ); + ImGui.Button( LabelImportButton ); + ImGui.PopStyleVar(); + + ImGui.PushStyleColor( ImGuiCol.Text, ColorRed ); + ImGui.Text( "Can not import since the mod directory path is not valid." ); + ImGui.Dummy( Vector2.UnitY * ImGui.GetTextLineHeightWithSpacing() ); + ImGui.PopStyleColor(); + + ImGui.Text( "Please set the mod directory in the settings tab." ); + ImGui.Text( "This folder should preferably be close to the root directory of your (preferably SSD) drive, for example" ); + ImGui.PushStyleColor( ImGuiCol.Text, ColorYellow ); + ImGui.Text( " D:\\ffxivmods" ); + ImGui.PopStyleColor(); + ImGui.Text( "You can return to this tab once you've done that." ); + } + else if( ImGui.Button( LabelImportButton ) ) { RunImportTask(); } diff --git a/Penumbra/UI/MenuTabs/TabInstalled/TabInstalled.cs b/Penumbra/UI/MenuTabs/TabInstalled/TabInstalled.cs index dbc7e895..c44b9919 100644 --- a/Penumbra/UI/MenuTabs/TabInstalled/TabInstalled.cs +++ b/Penumbra/UI/MenuTabs/TabInstalled/TabInstalled.cs @@ -24,11 +24,6 @@ namespace Penumbra.UI private static void DrawNoModsAvailable() { ImGui.Text( "You don't have any mods :(" ); - Custom.ImGuiCustom.VerticalDistance( 20f ); - ImGui.Text( "You'll need to install them first by creating a folder close to the root of your drive (preferably an SSD)." ); - ImGui.Text( "For example: D:/ffxiv/mods/" ); - ImGui.Text( "And pasting that path into the settings tab and clicking the 'Rediscover Mods' button." ); - ImGui.Text( "You can return to this tab once you've done that." ); } public void Draw() diff --git a/Penumbra/UI/MenuTabs/TabSettings.cs b/Penumbra/UI/MenuTabs/TabSettings.cs index 6511d55f..24ff3811 100644 --- a/Penumbra/UI/MenuTabs/TabSettings.cs +++ b/Penumbra/UI/MenuTabs/TabSettings.cs @@ -5,6 +5,7 @@ using System.Text.RegularExpressions; using Dalamud.Plugin; using ImGuiNET; using Penumbra.Interop; +using Penumbra.Mods; using Penumbra.Util; namespace Penumbra.UI @@ -39,10 +40,13 @@ namespace Penumbra.UI private void DrawRootFolder() { var basePath = _config.ModDirectory; - if( ImGui.InputText( LabelRootFolder, ref basePath, 255 ) && _config.ModDirectory != basePath ) + if( ImGui.InputText( LabelRootFolder, ref basePath, 255, ImGuiInputTextFlags.EnterReturnsTrue ) + && _config.ModDirectory != basePath ) { _config.ModDirectory = basePath; _configChanged = true; + _base.ReloadMods(); + _base._menu.InstalledTab.Selector.ClearSelection(); } } @@ -59,7 +63,7 @@ namespace Penumbra.UI { if( ImGui.Button( LabelOpenFolder ) ) { - if( !Directory.Exists( _config.ModDirectory ) ) + if( !Directory.Exists( _config.ModDirectory ) || !Service< ModManager >.Get().Valid ) { return; } diff --git a/Penumbra/UI/SettingsInterface.cs b/Penumbra/UI/SettingsInterface.cs index 62756046..a82726af 100644 --- a/Penumbra/UI/SettingsInterface.cs +++ b/Penumbra/UI/SettingsInterface.cs @@ -43,11 +43,9 @@ namespace Penumbra.UI { _menu.InstalledTab.Selector.ResetModNamesLower(); _menu.InstalledTab.Selector.ClearSelection(); - // create the directory if it doesn't exist - Directory.CreateDirectory( _plugin!.Configuration!.ModDirectory ); var modManager = Service< ModManager >.Get(); - modManager.DiscoverMods( new DirectoryInfo( _plugin.Configuration.ModDirectory ) ); + modManager.DiscoverMods( _plugin.Configuration.ModDirectory ); _menu.InstalledTab.Selector.ResetModNamesLower(); } } diff --git a/Penumbra/UI/SettingsMenu.cs b/Penumbra/UI/SettingsMenu.cs index 7779e2d2..7eda5b1b 100644 --- a/Penumbra/UI/SettingsMenu.cs +++ b/Penumbra/UI/SettingsMenu.cs @@ -1,5 +1,7 @@ using System.Numerics; using ImGuiNET; +using Penumbra.Mods; +using Penumbra.Util; namespace Penumbra.UI { @@ -63,7 +65,7 @@ namespace Penumbra.UI _collectionsTab.Draw(); _importTab.Draw(); - if( !_importTab.IsImporting() ) + if( Service.Get().Valid && !_importTab.IsImporting() ) { _browserTab.Draw(); InstalledTab.Draw();