Skip to content
This repository has been archived by the owner on Mar 29, 2022. It is now read-only.

Commit

Permalink
make menu cleaner more aggressive
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyler-IN committed Apr 25, 2020
1 parent 4347c28 commit 2e6f301
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 23 deletions.
3 changes: 3 additions & 0 deletions src/CommunityPatch/CommunityPatch.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@
<WouldDependOn>$([System.Text.RegularExpressions.Regex]::Replace('%(Filename)', '^(.*?)\..*$', '$1.cs'))</WouldDependOn>
<DependentUpon Condition="'%(DependentUpon)' == '' And '%(WouldDependOn)' != '%(Filename)'">%(WouldDependOn)</DependentUpon>
</Compile>
<Compile Update="Patches\GroupMainMenuOptionsPatch.cs">
<WouldDependOn>MenuCleanerPatch</WouldDependOn>
</Compile>
</ItemGroup>

<Target Name="ReportVersionInfo" BeforeTargets="AddSourceRevisionToInformationalVersion">
Expand Down
3 changes: 0 additions & 3 deletions src/CommunityPatch/CommunityPatchSubModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,6 @@ protected override void OnBeforeInitialModuleScreenSetAsRoot() {
}
}

if (!DontGroupThirdPartyMenuOptions)
MenuCleaner.CleanUpMainMenu();

if (DisableIntroVideo)
try {
typeof(Module)
Expand Down
13 changes: 7 additions & 6 deletions src/CommunityPatch/Patches/GroupEscapeMenuOptionsPatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,19 @@ namespace CommunityPatch.Patches {
[HarmonyPatch(typeof(EscapeMenuVM), MethodType.Constructor, typeof(IEnumerable<EscapeMenuItemVM>), 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<EscapeMenuItemVM> ____menuItems, IEnumerable<EscapeMenuItemVM> 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;
}

Expand Down Expand Up @@ -68,7 +69,7 @@ public static void Postfix(EscapeMenuVM __instance, ref MBBindingList<EscapeMenu

if (customOptions.Count <= 0) {
newList.Insert(newList.Count - 2, new EscapeMenuItemVM(new TextObject("{=CommunityPatchOptions}Community Patch Options"),
_ => CommunityPatchSubModule.Current.ShowOptions(), _groupEscMenuOptsKey, false));
_ => CommunityPatchSubModule.Current.ShowOptions(), GroupEscMenuOptsKey, false));

____menuItems = newList;
return;
Expand All @@ -80,7 +81,7 @@ public static void Postfix(EscapeMenuVM __instance, ref MBBindingList<EscapeMenu
foreach (var item in customOptions)
options.Add(new InquiryElement(item, item.ActionText, null, !item.IsDisabled, null));

options.Add(new InquiryElement(_groupEscMenuOptsKey, new TextObject("{=CommunityPatchOptions}Community Patch Options").ToString(), null));
options.Add(new InquiryElement(GroupEscMenuOptsKey, new TextObject("{=CommunityPatchOptions}Community Patch Options").ToString(), null));

InformationManager.ShowMultiSelectionInquiry(new MultiSelectionInquiryData(
new TextObject("{=MoreOptions}More Options").ToString(),
Expand All @@ -92,7 +93,7 @@ public static void Postfix(EscapeMenuVM __instance, ref MBBindingList<EscapeMenu
null,
selection => {
var picked = selection.FirstOrDefault()?.Identifier;
if (picked == _groupEscMenuOptsKey) {
if (picked == GroupEscMenuOptsKey) {
CommunityPatchSubModule.Current.ShowOptions();
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -20,17 +31,14 @@ public static class MenuCleaner {
internal static List<InitialStateOption> GetThirdPartyOptionsMenus()
=> _modOptionsMenus ??= Module.CurrentModule.GetInitialStateOptions()
.Where(IsThirdPartyOption)
.Concat(_groupedOptionsMenus ?? (IEnumerable<InitialStateOption>) Array.Empty<InitialStateOption>())
.Concat(GroupedOptionsMenus ?? (IEnumerable<InitialStateOption>) Array.Empty<InitialStateOption>())
.ToList();

private static bool _alreadyCleanedUpMainMenu;

internal static List<InitialStateOption> _groupedOptionsMenus;

public static void CleanUpMainMenu()
=> SynchronizationContext.Current.Post(_ => CleanUpMainMenuInternal(), null);
internal static List<InitialStateOption> GroupedOptionsMenus;

private static void CleanUpMainMenuInternal() {
internal static void CleanUpMainMenu() {
if (_alreadyCleanedUpMainMenu)
return;

Expand All @@ -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<InitialStateOption>();
GroupedOptionsMenus = new List<InitialStateOption>();
for (var i = menuLength - 1; i > 0; --i) {
var item = menu[i];

Expand All @@ -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<InitialStateOption>.Create((a, b) => {
GroupedOptionsMenus.Sort(Comparer<InitialStateOption>.Create((a, b) => {
var order = a.OrderIndex.CompareTo(b.OrderIndex);
if (order == 0)
order = string.Compare((a.Id ?? ""), b.Id ?? "", StringComparison.OrdinalIgnoreCase);
Expand Down Expand Up @@ -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) {
Expand All @@ -112,7 +120,7 @@ private static bool IsThirdPartyOption(InitialStateOption item) {
}

internal static void ShowMoreMainMenuOptions()
=> ShowOptions(_groupedOptionsMenus);
=> ShowOptions(GroupedOptionsMenus);

internal static void ShowOptions(List<InitialStateOption> moreOptions)
=> InformationManager.ShowMultiSelectionInquiry(new MultiSelectionInquiryData(
Expand Down

0 comments on commit 2e6f301

Please sign in to comment.