From 2e6f30160009fda9c945607334fb8677cf425ce7 Mon Sep 17 00:00:00 2001 From: Tyler Young Date: Sat, 25 Apr 2020 15:44:58 -0400 Subject: [PATCH] make menu cleaner more aggressive --- src/CommunityPatch/CommunityPatch.csproj | 3 ++ src/CommunityPatch/CommunityPatchSubModule.cs | 3 -- .../Patches/GroupEscapeMenuOptionsPatch.cs | 13 +++---- .../GroupMainMenuOptionsPatch.cs} | 36 +++++++++++-------- 4 files changed, 32 insertions(+), 23 deletions(-) rename src/CommunityPatch/{MenuCleaner.cs => Patches/GroupMainMenuOptionsPatch.cs} (81%) diff --git a/src/CommunityPatch/CommunityPatch.csproj b/src/CommunityPatch/CommunityPatch.csproj index e0b1a66..55f58a5 100644 --- a/src/CommunityPatch/CommunityPatch.csproj +++ b/src/CommunityPatch/CommunityPatch.csproj @@ -76,6 +76,9 @@ $([System.Text.RegularExpressions.Regex]::Replace('%(Filename)', '^(.*?)\..*$', '$1.cs')) %(WouldDependOn) + + MenuCleanerPatch + diff --git a/src/CommunityPatch/CommunityPatchSubModule.cs b/src/CommunityPatch/CommunityPatchSubModule.cs index b806f2e..d029865 100644 --- a/src/CommunityPatch/CommunityPatchSubModule.cs +++ b/src/CommunityPatch/CommunityPatchSubModule.cs @@ -56,9 +56,6 @@ protected override void OnBeforeInitialModuleScreenSetAsRoot() { } } - if (!DontGroupThirdPartyMenuOptions) - MenuCleaner.CleanUpMainMenu(); - if (DisableIntroVideo) try { typeof(Module) diff --git a/src/CommunityPatch/Patches/GroupEscapeMenuOptionsPatch.cs b/src/CommunityPatch/Patches/GroupEscapeMenuOptionsPatch.cs index 12b8af9..a9e5ff7 100644 --- a/src/CommunityPatch/Patches/GroupEscapeMenuOptionsPatch.cs +++ b/src/CommunityPatch/Patches/GroupEscapeMenuOptionsPatch.cs @@ -17,18 +17,19 @@ namespace CommunityPatch.Patches { [HarmonyPatch(typeof(EscapeMenuVM), MethodType.Constructor, typeof(IEnumerable), typeof(TextObject))] public static class GroupEscapeMenuOptionsPatch { - public static FieldInfo EscapeMenuItemVmOnExecute = typeof(EscapeMenuItemVM) + public static readonly FieldInfo EscapeMenuItemVmOnExecute = typeof(EscapeMenuItemVM) .GetField("_onExecute", NonPublic | Instance | DeclaredOnly); public static FieldInfo EscapeMenuItemVmIdentifier = typeof(EscapeMenuItemVM) .GetField("_identifier", NonPublic | Instance | DeclaredOnly); - private static readonly object _groupEscMenuOptsKey = new object(); + private static readonly object GroupEscMenuOptsKey = new object(); + [UsedImplicitly] public static void Postfix(EscapeMenuVM __instance, ref MBBindingList ____menuItems, IEnumerable items, TextObject title = null) { if (CommunityPatchSubModule.DontGroupThirdPartyMenuOptions) { ____menuItems.Add(new EscapeMenuItemVM(new TextObject("{=CommunityPatchOptions}Community Patch Options"), - _ => CommunityPatchSubModule.Current.ShowOptions(), _groupEscMenuOptsKey, false)); + _ => CommunityPatchSubModule.Current.ShowOptions(), GroupEscMenuOptsKey, false)); return; } @@ -68,7 +69,7 @@ public static void Postfix(EscapeMenuVM __instance, ref MBBindingList CommunityPatchSubModule.Current.ShowOptions(), _groupEscMenuOptsKey, false)); + _ => CommunityPatchSubModule.Current.ShowOptions(), GroupEscMenuOptsKey, false)); ____menuItems = newList; return; @@ -80,7 +81,7 @@ public static void Postfix(EscapeMenuVM __instance, ref MBBindingList { var picked = selection.FirstOrDefault()?.Identifier; - if (picked == _groupEscMenuOptsKey) { + if (picked == GroupEscMenuOptsKey) { CommunityPatchSubModule.Current.ShowOptions(); return; } diff --git a/src/CommunityPatch/MenuCleaner.cs b/src/CommunityPatch/Patches/GroupMainMenuOptionsPatch.cs similarity index 81% rename from src/CommunityPatch/MenuCleaner.cs rename to src/CommunityPatch/Patches/GroupMainMenuOptionsPatch.cs index 638b671..dd5cb7b 100644 --- a/src/CommunityPatch/MenuCleaner.cs +++ b/src/CommunityPatch/Patches/GroupMainMenuOptionsPatch.cs @@ -3,15 +3,26 @@ using System.Linq; using System.Reflection; using System.Threading; +using HarmonyLib; +using JetBrains.Annotations; using TaleWorlds.Core; using TaleWorlds.Localization; using TaleWorlds.MountAndBlade; -using static System.Reflection.BindingFlags; +using TaleWorlds.MountAndBlade.ViewModelCollection; using Module = TaleWorlds.MountAndBlade.Module; +using static System.Reflection.BindingFlags; namespace CommunityPatch { - public static class MenuCleaner { + [UsedImplicitly] + [HarmonyPatch(typeof(InitialMenuVM), MethodType.Constructor)] + public class GroupMainMenuOptionsPatch { + + [UsedImplicitly] + public static void Prefix() { + if (!CommunityPatchSubModule.DontGroupThirdPartyMenuOptions) + CleanUpMainMenu(); + } private const int MaxMenuLength = 8; @@ -20,17 +31,14 @@ public static class MenuCleaner { internal static List GetThirdPartyOptionsMenus() => _modOptionsMenus ??= Module.CurrentModule.GetInitialStateOptions() .Where(IsThirdPartyOption) - .Concat(_groupedOptionsMenus ?? (IEnumerable) Array.Empty()) + .Concat(GroupedOptionsMenus ?? (IEnumerable) Array.Empty()) .ToList(); private static bool _alreadyCleanedUpMainMenu; - internal static List _groupedOptionsMenus; - - public static void CleanUpMainMenu() - => SynchronizationContext.Current.Post(_ => CleanUpMainMenuInternal(), null); + internal static List GroupedOptionsMenus; - private static void CleanUpMainMenuInternal() { + internal static void CleanUpMainMenu() { if (_alreadyCleanedUpMainMenu) return; @@ -48,12 +56,12 @@ private static void CleanUpMainMenuInternal() { if (menu.Length <= MaxMenuLength) return; - if (_groupedOptionsMenus != null) + if (GroupedOptionsMenus != null) return; var menuLength = menu.Length; - _groupedOptionsMenus = new List(); + GroupedOptionsMenus = new List(); for (var i = menuLength - 1; i > 0; --i) { var item = menu[i]; @@ -63,12 +71,12 @@ private static void CleanUpMainMenuInternal() { if (menuLength <= MaxMenuLength) break; - _groupedOptionsMenus.Add(item); + GroupedOptionsMenus.Add(item); menu[i] = null; --menuLength; } - _groupedOptionsMenus.Sort(Comparer.Create((a, b) => { + GroupedOptionsMenus.Sort(Comparer.Create((a, b) => { var order = a.OrderIndex.CompareTo(b.OrderIndex); if (order == 0) order = string.Compare((a.Id ?? ""), b.Id ?? "", StringComparison.OrdinalIgnoreCase); @@ -101,7 +109,7 @@ private static bool IsThirdPartyOption(InitialStateOption item) { || optAsmName.StartsWith("SandBox.") || optAsmName.StartsWith("SandBoxCore.") || optAsmName.StartsWith("StoryMode.")) - if (PathHelpers.IsOfficialAssembly(optAsm)) + if (optAsm.IsOfficialAssembly()) return false; } catch (Exception) { @@ -112,7 +120,7 @@ private static bool IsThirdPartyOption(InitialStateOption item) { } internal static void ShowMoreMainMenuOptions() - => ShowOptions(_groupedOptionsMenus); + => ShowOptions(GroupedOptionsMenus); internal static void ShowOptions(List moreOptions) => InformationManager.ShowMultiSelectionInquiry(new MultiSelectionInquiryData(