From 0eff4e2e6724ebe17c0eff65f21161c1ffa512e2 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Tue, 22 Mar 2022 22:58:00 +0100 Subject: [PATCH] tmp --- Penumbra/Mods/CollectionManager.cs | 35 +++++++++++++++++++++++++++++- Penumbra/Mods/ModManager.cs | 21 +++++------------- Penumbra/Penumbra.cs | 7 ++++-- 3 files changed, 44 insertions(+), 19 deletions(-) diff --git a/Penumbra/Mods/CollectionManager.cs b/Penumbra/Mods/CollectionManager.cs index 0bb7182a..9763bfa4 100644 --- a/Penumbra/Mods/CollectionManager.cs +++ b/Penumbra/Mods/CollectionManager.cs @@ -21,7 +21,7 @@ public delegate void CollectionChangeDelegate( ModCollection? oldCollection, Mod string? characterName = null ); // Contains all collections and respective functions, as well as the collection settings. -public class CollectionManager +public class CollectionManager : IDisposable { private readonly ModManager _manager; @@ -42,10 +42,43 @@ public class CollectionManager { _manager = manager; + _manager.ModsRediscovered += OnModsRediscovered; ReadCollections(); LoadConfigCollections( Penumbra.Config ); } + public void Dispose() + { + _manager.ModsRediscovered -= OnModsRediscovered; + } + + private void OnModsRediscovered() + { + RecreateCaches(); + DefaultCollection.SetFiles(); + } + + private void OnModChanged( ModChangeType type, int idx, ModData mod ) + { + switch( type ) + { + case ModChangeType.Added: + foreach( var collection in Collections.Values ) + { + collection.AddMod( mod ); + } + + break; + case ModChangeType.Removed: + RemoveModFromCaches( mod.BasePath ); + break; + case ModChangeType.Changed: + // TODO + break; + default: throw new ArgumentOutOfRangeException( nameof( type ), type, null ); + } + } + public void CreateNecessaryCaches() { AddCache( DefaultCollection ); diff --git a/Penumbra/Mods/ModManager.cs b/Penumbra/Mods/ModManager.cs index c414260c..d3eadd77 100644 --- a/Penumbra/Mods/ModManager.cs +++ b/Penumbra/Mods/ModManager.cs @@ -32,9 +32,8 @@ public class ModManager public ModFolder StructuredMods { get; } = ModFileSystem.Root; - public CollectionManager Collections { get; } - public event ModChangeDelegate? ModChange; + public event Action? ModsRediscovered; public bool Valid { get; private set; } @@ -82,18 +81,14 @@ public class ModManager Config.ModDirectory = BasePath.FullName; Config.Save(); } - - if( !firstTime ) - { - Collections.RecreateCaches(); - } } + + ModsRediscovered?.Invoke(); } public ModManager() { SetBaseDirectory( Config.ModDirectory, true ); - Collections = new CollectionManager( this ); } private bool SetSortOrderPath( ModData mod, string path ) @@ -161,8 +156,7 @@ public class ModManager SetModStructure(); } - Collections.RecreateCaches(); - Collections.DefaultCollection.SetFiles(); + ModsRediscovered?.Invoke(); } public void DeleteMod( DirectoryInfo modFolder ) @@ -185,7 +179,6 @@ public class ModManager var mod = ModsInternal[ idx ]; mod.SortOrder.ParentFolder.RemoveMod( mod ); ModsInternal.RemoveAt( idx ); - Collections.RemoveModFromCaches( modFolder ); ModChange?.Invoke( ModChangeType.Removed, idx, mod ); } } @@ -213,10 +206,6 @@ public class ModManager ModsInternal.Add( mod ); ModChange?.Invoke( ModChangeType.Added, ModsInternal.Count - 1, mod ); - foreach( var collection in Collections.Collections.Values ) - { - collection.AddMod( mod ); - } return ModsInternal.Count - 1; } @@ -261,7 +250,7 @@ public class ModManager mod.Resources.MetaManipulations.SaveToFile( MetaCollection.FileName( mod.BasePath ) ); } - Collections.UpdateCollections( mod, metaChanges, fileChanges, nameChange, reloadMeta ); + Penumbra.CollectionManager.UpdateCollections( mod, metaChanges, fileChanges, nameChange, reloadMeta ); // TODO ModChange?.Invoke( ModChangeType.Changed, idx, mod ); return true; } diff --git a/Penumbra/Penumbra.cs b/Penumbra/Penumbra.cs index d82f556c..40c12864 100644 --- a/Penumbra/Penumbra.cs +++ b/Penumbra/Penumbra.cs @@ -34,6 +34,7 @@ public class Penumbra : IDalamudPlugin public static CharacterUtility CharacterUtility { get; private set; } = null!; public static ModManager ModManager { get; private set; } = null!; + public static CollectionManager CollectionManager { get; private set; } = null!; public static ResourceLoader ResourceLoader { get; set; } = null!; public ResourceLogger ResourceLogger { get; } @@ -66,8 +67,9 @@ public class Penumbra : IDalamudPlugin ResourceLogger = new ResourceLogger( ResourceLoader ); ModManager = new ModManager(); ModManager.DiscoverMods(); - ObjectReloader = new ObjectReloader(); - PathResolver = new PathResolver( ResourceLoader ); + CollectionManager = new CollectionManager( ModManager ); + ObjectReloader = new ObjectReloader(); + PathResolver = new PathResolver( ResourceLoader ); Dalamud.Commands.AddHandler( CommandName, new CommandInfo( OnCommand ) { @@ -193,6 +195,7 @@ public class Penumbra : IDalamudPlugin Api.Dispose(); SettingsInterface.Dispose(); ObjectReloader.Dispose(); + CollectionManager.Dispose(); Dalamud.Commands.RemoveHandler( CommandName );