diff --git a/Penumbra/API/ModsController.cs b/Penumbra/API/ModsController.cs index 2453f862..5eeeb2e5 100644 --- a/Penumbra/API/ModsController.cs +++ b/Penumbra/API/ModsController.cs @@ -19,7 +19,7 @@ namespace Penumbra.Api public object? GetMods() { var modManager = Service< ModManager >.Get(); - return modManager.Collections.CurrentCollection.Cache?.AvailableMods.Select( x => new + return modManager.Collections.CurrentCollection.Cache?.AvailableMods.Values.Select( x => new { x.Settings.Enabled, x.Settings.Priority, diff --git a/Penumbra/Mods/ModCollection.cs b/Penumbra/Mods/ModCollection.cs index fcb0dee9..903fc402 100644 --- a/Penumbra/Mods/ModCollection.cs +++ b/Penumbra/Mods/ModCollection.cs @@ -37,6 +37,11 @@ namespace Penumbra.Mods public Mod.Mod GetMod( ModData mod ) { + if( Cache != null && Cache.AvailableMods.TryGetValue( mod.BasePath.Name, out var ret )) + { + return ret; + } + if( Settings.TryGetValue( mod.BasePath.Name, out var settings ) ) { return new Mod.Mod( settings, mod ); @@ -111,7 +116,7 @@ namespace Penumbra.Mods } var changes = false; - foreach( var mod in Cache.AvailableMods ) + foreach( var mod in Cache.AvailableMods.Values ) { changes |= mod.FixSettings(); } diff --git a/Penumbra/Mods/ModCollectionCache.cs b/Penumbra/Mods/ModCollectionCache.cs index d178b78c..6bd97a1c 100644 --- a/Penumbra/Mods/ModCollectionCache.cs +++ b/Penumbra/Mods/ModCollectionCache.cs @@ -13,7 +13,7 @@ namespace Penumbra.Mods // It will only be setup if a collection gets activated in any way. public class ModCollectionCache { - public readonly List AvailableMods = new(); + public readonly Dictionary< string, Mod.Mod > AvailableMods = new(); public readonly Dictionary< GamePath, FileInfo > ResolvedFiles = new(); public readonly Dictionary< GamePath, GamePath > SwappedFiles = new(); @@ -77,7 +77,7 @@ namespace Penumbra.Mods { MetaManipulations.Reset( false ); - foreach( var mod in AvailableMods.Where( m => m.Settings.Enabled && m.Data.Resources.MetaManipulations.Count > 0 ) ) + foreach( var mod in AvailableMods.Values.Where( m => m.Settings.Enabled && m.Data.Resources.MetaManipulations.Count > 0 ) ) { mod.Cache.ClearMetaConflicts(); AddManipulations( mod ); @@ -92,7 +92,7 @@ namespace Penumbra.Mods SwappedFiles.Clear(); var registeredFiles = new Dictionary< GamePath, Mod.Mod >(); - foreach( var mod in AvailableMods.Where( m => m.Settings.Enabled ) ) + foreach( var mod in AvailableMods.Values.Where( m => m.Settings.Enabled ) ) { mod.Cache.ClearFileConflicts(); AddFiles( registeredFiles, mod ); @@ -102,25 +102,16 @@ namespace Penumbra.Mods public void RemoveMod( DirectoryInfo basePath ) { - var hadMeta = false; - var wasEnabled = false; - AvailableMods.RemoveAll( m => + if( AvailableMods.TryGetValue( basePath.Name, out var mod ) ) { - if( m.Settings.Enabled ) + AvailableMods.Remove( basePath.Name ); + if( mod.Settings.Enabled ) { - wasEnabled = true; - hadMeta |= m.Data.Resources.MetaManipulations.Count > 0; - } - - return m.Data.BasePath.Name == basePath.Name; - } ); - - if( wasEnabled ) - { - CalculateEffectiveFileList(); - if( hadMeta ) - { - UpdateMetaManipulations(); + CalculateEffectiveFileList(); + if( mod.Data.Resources.MetaManipulations.Count > 0 ) + { + UpdateMetaManipulations(); + } } } } @@ -135,16 +126,18 @@ namespace Penumbra.Mods public void AddMod( ModSettings settings, ModData data ) { - var newMod = new Mod.Mod( settings, data ); - var idx = AvailableMods.BinarySearch( newMod, Comparer ); - idx = idx < 0 ? ~idx : idx; - AvailableMods.Insert( idx, newMod ); - if( settings.Enabled ) + if( !AvailableMods.TryGetValue( data.BasePath.Name, out var existingMod ) ) { - CalculateEffectiveFileList(); - if( data.Resources.MetaManipulations.Count > 0 ) + var newMod = new Mod.Mod( settings, data ); + AvailableMods[ data.BasePath.Name ] = newMod; + + if( settings.Enabled ) { - UpdateMetaManipulations(); + CalculateEffectiveFileList(); + if( data.Resources.MetaManipulations.Count > 0 ) + { + UpdateMetaManipulations(); + } } } } diff --git a/Penumbra/UI/MenuTabs/TabInstalled/TabInstalledModPanel.cs b/Penumbra/UI/MenuTabs/TabInstalled/TabInstalledModPanel.cs index e3707612..e44138d1 100644 --- a/Penumbra/UI/MenuTabs/TabInstalled/TabInstalledModPanel.cs +++ b/Penumbra/UI/MenuTabs/TabInstalled/TabInstalledModPanel.cs @@ -204,8 +204,8 @@ namespace Penumbra.UI if( ImGui.InputInt( "Priority", ref priority, 0 ) && priority != Mod!.Settings.Priority ) { Mod.Settings.Priority = priority; - _selector.Cache.ResetFilters(); _base.SaveCurrentCollection( Mod.Data.Resources.MetaManipulations.Count > 0 ); + _selector.Cache.ResetFilters(); } if( ImGui.IsItemHovered() ) @@ -221,8 +221,8 @@ namespace Penumbra.UI if( ImGui.Checkbox( LabelModEnabled, ref enabled ) ) { Mod.Settings.Enabled = enabled; - _selector.Cache.ResetFilters(); _base.SaveCurrentCollection( Mod.Data.Resources.MetaManipulations.Count > 0 ); + _selector.Cache.ResetFilters(); } }