From f0d7ee814a75982beab4aaf0a655fd12404f319e Mon Sep 17 00:00:00 2001 From: Arne Kiesewetter Date: Sun, 7 Jul 2024 21:43:04 +0200 Subject: [PATCH] Makes the Favorites and Recents monkey togglable --- .../ComponentSelectorAdditions.csproj | 4 +- .../FavoritesCategories.cs | 47 +++++++++++++++---- .../RecentsCategories.cs | 45 ++++++++++++++---- 3 files changed, 74 insertions(+), 22 deletions(-) diff --git a/ComponentSelectorAdditions/ComponentSelectorAdditions.csproj b/ComponentSelectorAdditions/ComponentSelectorAdditions.csproj index 208fe71..378edc3 100644 --- a/ComponentSelectorAdditions/ComponentSelectorAdditions.csproj +++ b/ComponentSelectorAdditions/ComponentSelectorAdditions.csproj @@ -30,8 +30,8 @@ - - + + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/ComponentSelectorAdditions/FavoritesCategories.cs b/ComponentSelectorAdditions/FavoritesCategories.cs index 111ea07..f7b02a1 100644 --- a/ComponentSelectorAdditions/FavoritesCategories.cs +++ b/ComponentSelectorAdditions/FavoritesCategories.cs @@ -27,12 +27,16 @@ internal sealed class FavoritesCategories : ConfiguredResoniteMonkey _protoFluxRootCategory = null!; private CategoryNode _rootCategory = null!; + public override bool CanBeDisabled => true; public int Priority => HarmonyLib.Priority.Normal; public bool SkipCanceled => true; public void Handle(EnumerateComponentsEvent eventData) { + if (!Enabled) + return; + if (eventData.RootCategory != _favoritesCategory && eventData.RootCategory != _protoFluxFavoritesCategory) { if (ConfigSection.SortFavoriteComponentsToTop) @@ -59,6 +63,9 @@ public void Handle(EnumerateComponentsEvent eventData) public void Handle(EnumerateCategoriesEvent eventData) { + if (!Enabled) + return; + if (eventData.RootCategory == _rootCategory || eventData.RootCategory == _protoFluxRootCategory) { if (eventData.RootCategory == _rootCategory) @@ -96,6 +103,9 @@ public void Handle(EnumerateCategoriesEvent eventData) public void Handle(PostProcessButtonsEvent eventData) { + if (!Enabled) + return; + if (eventData.Path.IsSelectorRoot) return; @@ -118,6 +128,9 @@ public void Handle(PostProcessButtonsEvent eventData) public void Handle(EnumerateConcreteGenericsEvent eventData) { + if (!Enabled) + return; + var concreteGenerics = ConfigSection.Components .Concat(ConfigSection.ProtoFluxNodes) .Where(type => type is not null && type.IsGenericType && !type.ContainsGenericParameters && type.GetGenericTypeDefinition() == eventData.Component); @@ -128,13 +141,13 @@ public void Handle(EnumerateConcreteGenericsEvent eventData) protected override IEnumerable GetFeaturePatches() => Enumerable.Empty(); + protected override void OnDisabled() => RemoveCategories(); + + protected override void OnEnabled() => AddCategories(); + protected override bool OnEngineReady() { - _favoritesCategory = WorkerInitializer.ComponentLibrary.GetSubcategory(FavoritesPath); - SearchConfig.Instance.AddExcludedCategory(FavoritesPath); - - _protoFluxFavoritesCategory = WorkerInitializer.ComponentLibrary.GetSubcategory(ProtoFluxFavoritesPath); - SearchConfig.Instance.AddExcludedCategory(ProtoFluxFavoritesPath); + AddCategories(); _rootCategory = WorkerInitializer.ComponentLibrary; _protoFluxRootCategory = WorkerInitializer.ComponentLibrary.GetSubcategory(ProtoFluxPath); @@ -156,11 +169,7 @@ protected override bool OnShutdown(bool applicationExiting) Mod.UnregisterEventHandler(this); Mod.UnregisterEventHandler(this); - _rootCategory._subcategories.Remove("Favorites"); - SearchConfig.Instance.RemoveExcludedCategory(FavoritesPath); - - _protoFluxRootCategory._subcategories.Remove("Favorites"); - SearchConfig.Instance.RemoveExcludedCategory(ProtoFluxFavoritesPath); + RemoveCategories(); } return base.OnShutdown(applicationExiting); @@ -213,6 +222,15 @@ private static bool ToggleHashSetContains(ISet set, T value) return false; } + private void AddCategories() + { + _favoritesCategory = WorkerInitializer.ComponentLibrary.GetSubcategory(FavoritesPath); + SearchConfig.Instance.AddExcludedCategory(FavoritesPath); + + _protoFluxFavoritesCategory = WorkerInitializer.ComponentLibrary.GetSubcategory(ProtoFluxFavoritesPath); + SearchConfig.Instance.AddExcludedCategory(ProtoFluxFavoritesPath); + } + private bool IsFavoriteCategory(string name) => ConfigSection.Categories.Contains(name); @@ -225,6 +243,15 @@ private bool IsFavoriteProtoFluxNode(string name) private bool IsProtoFluxFavoriteCategory(string name) => ConfigSection.ProtoFluxCategories.Contains(name); + private void RemoveCategories() + { + _rootCategory._subcategories.Remove("Favorites"); + SearchConfig.Instance.RemoveExcludedCategory(FavoritesPath); + + _protoFluxRootCategory._subcategories.Remove("Favorites"); + SearchConfig.Instance.RemoveExcludedCategory(ProtoFluxFavoritesPath); + } + private bool ToggleFavoriteCategory(string name) => ToggleHashSetContains(ConfigSection.Categories, name); diff --git a/ComponentSelectorAdditions/RecentsCategories.cs b/ComponentSelectorAdditions/RecentsCategories.cs index c6174a5..d5d3579 100644 --- a/ComponentSelectorAdditions/RecentsCategories.cs +++ b/ComponentSelectorAdditions/RecentsCategories.cs @@ -30,12 +30,17 @@ internal sealed class RecentsCategories : ConfiguredResoniteMonkey _recentsCategory = null!; private CategoryNode _rootCategory = null!; + public override bool CanBeDisabled => true; + public int Priority => HarmonyLib.Priority.Normal; public bool SkipCanceled => true; public void Handle(EnumerateComponentsEvent eventData) { + if (!Enabled) + return; + if (eventData.RootCategory != _recentsCategory && eventData.RootCategory != _protoFluxRecentsCategory) return; @@ -51,6 +56,9 @@ public void Handle(EnumerateComponentsEvent eventData) public void Handle(EnumerateCategoriesEvent eventData) { + if (!Enabled) + return; + if (eventData.RootCategory == _rootCategory) eventData.AddItem(_recentsCategory, -1000, true); else if (eventData.RootCategory == _protoFluxRootCategory) @@ -59,6 +67,9 @@ public void Handle(EnumerateCategoriesEvent eventData) public void Handle(EnumerateConcreteGenericsEvent eventData) { + if (!Enabled) + return; + var concreteGenerics = ConfigSection.Components .Concat(ConfigSection.ProtoFluxNodes) .Where(type => type is not null && type.IsGenericType && !type.ContainsGenericParameters && type.GetGenericTypeDefinition() == eventData.Component); @@ -69,13 +80,13 @@ public void Handle(EnumerateConcreteGenericsEvent eventData) protected override IEnumerable GetFeaturePatches() => Enumerable.Empty(); + protected override void OnDisabled() => RemoveCategories(); + + protected override void OnEnabled() => AddCategories(); + protected override bool OnEngineReady() { - _recentsCategory = WorkerInitializer.ComponentLibrary.GetSubcategory(RecentsPath); - SearchConfig.Instance.AddExcludedCategory(RecentsPath); - - _protoFluxRecentsCategory = WorkerInitializer.ComponentLibrary.GetSubcategory(ProtoFluxRecentsPath); - SearchConfig.Instance.AddExcludedCategory(ProtoFluxRecentsPath); + AddCategories(); _rootCategory = WorkerInitializer.ComponentLibrary; _protoFluxRootCategory = WorkerInitializer.ComponentLibrary.GetSubcategory(ProtoFluxPath); @@ -95,11 +106,7 @@ protected override bool OnShutdown(bool applicationExiting) Mod.UnregisterEventHandler(this); Mod.UnregisterEventHandler(this); - _rootCategory._subcategories.Remove("Recents"); - SearchConfig.Instance.RemoveExcludedCategory(RecentsPath); - - _protoFluxRootCategory._subcategories.Remove("Recents"); - SearchConfig.Instance.RemoveExcludedCategory(ProtoFluxRecentsPath); + RemoveCategories(); } return base.OnShutdown(applicationExiting); @@ -156,5 +163,23 @@ private static void UpdateRecents(List recents, Type type) if (recents.Count > ConfigSection.RecentCap) recents.RemoveRange(0, recents.Count - ConfigSection.RecentCap); } + + private void AddCategories() + { + _recentsCategory = WorkerInitializer.ComponentLibrary.GetSubcategory(RecentsPath); + SearchConfig.Instance.AddExcludedCategory(RecentsPath); + + _protoFluxRecentsCategory = WorkerInitializer.ComponentLibrary.GetSubcategory(ProtoFluxRecentsPath); + SearchConfig.Instance.AddExcludedCategory(ProtoFluxRecentsPath); + } + + private void RemoveCategories() + { + _rootCategory._subcategories.Remove("Recents"); + SearchConfig.Instance.RemoveExcludedCategory(RecentsPath); + + _protoFluxRootCategory._subcategories.Remove("Recents"); + SearchConfig.Instance.RemoveExcludedCategory(ProtoFluxRecentsPath); + } } } \ No newline at end of file