diff --git a/MiraAPI/GameModes/GameModeOption.cs b/MiraAPI/GameModes/GameModeOption.cs index c58030a..2d1cc88 100644 --- a/MiraAPI/GameModes/GameModeOption.cs +++ b/MiraAPI/GameModes/GameModeOption.cs @@ -4,6 +4,7 @@ using System.Linq; using HarmonyLib; using MiraAPI.GameOptions; using MiraAPI.Networking; +using MiraAPI.Patches; using MiraAPI.Patches.GameModes; using MiraAPI.Patches.Options; using MiraAPI.Translation; @@ -128,6 +129,9 @@ public static class GameModeOption MenuState.Instance.QueuedRoleMenuRefresh[roleMenu.Key] = true; } } + + GameOptionsMenuPatch.RefreshForGameMode(); + RoleGuidePatches.RefreshSettingsForGameMode(); } // TODO: could make Values a dict of AbstractGameMode too @@ -143,12 +147,6 @@ public static class GameModeOption Info($"Game mode changed to {option.GetInt()}"); RpcSyncGamemode(PlayerControl.LocalPlayer, option.GetInt()); - if (GameSettingMenu.Instance && CustomGameModeManager.ActiveMode != null) - { - GameOptionsMenuPatch.ToggleGamemodeOptions(CustomGameModeManager.ActiveMode, __instance); - GameSettingMenu.Instance.RoleSettingsButton.gameObject.SetActive(CustomGameModeManager.ActiveMode.ShowNormalRoleSettings); - } - return false; } diff --git a/MiraAPI/Patches/Options/GameOptionsMenuPatch.cs b/MiraAPI/Patches/Options/GameOptionsMenuPatch.cs index 69966d4..4313101 100644 --- a/MiraAPI/Patches/Options/GameOptionsMenuPatch.cs +++ b/MiraAPI/Patches/Options/GameOptionsMenuPatch.cs @@ -188,6 +188,66 @@ internal static class GameOptionsMenuPatch instance.scrollBar.SetYBoundsMax(-num - 1.65f); } + internal static void RefreshForGameMode() + { + if (CustomGameModeManager.ActiveMode == null || + !GameSettingMenu.Instance || + !GameSettingMenu.Instance.GameSettingsTab || + !MenuState.Instance || + MenuState.Instance.CurrentModIdx != 0 || + GameModeGroups.Count == 0) + { + return; + } + + ToggleGamemodeOptions(CustomGameModeManager.ActiveMode, GameSettingMenu.Instance.GameSettingsTab); + GameSettingMenu.Instance.RoleSettingsButton.gameObject.SetActive( + CustomGameModeManager.ActiveMode.ShowNormalRoleSettings); + } + + internal static List> GetActiveGameSettingsRows(Func formatBool) + { + var menu = GameSettingMenu.Instance ? GameSettingMenu.Instance.GameSettingsTab : null; + if (!menu || menu.Children == null) + { + return []; + } + + var rows = new List>(); + foreach (var option in menu.Children) + { + if (!option || !option!.gameObject.activeInHierarchy || + option == menu.MapPicker || + (GameModeOption.OptionBehaviour && option == GameModeOption.OptionBehaviour) || + !option.Data) + { + continue; + } + + var title = option switch + { + ToggleOption toggle => toggle.TitleText.text, + NumberOption number => number.TitleText.text, + StringOption str => str.TitleText.text, + PlayerOption player => player.TitleText.text, + _ => TranslationController.Instance.GetString(option.Data.Title), + }; + + var value = option switch + { + ToggleOption toggle => formatBool(toggle.GetBool()), + NumberOption number => number.ValueText.text, + StringOption str => str.ValueText.text, + PlayerOption player => player.ValueText.text, + _ => option.Data.GetValueString(option.GetFloat()), + }; + + rows.Add(new KeyValuePair(title, value)); + } + + return rows; + } + private static void CreateSettings(GameOptionsMenu instance, Transform container) { var num = 0.713f; diff --git a/MiraAPI/Patches/RoleGuidePatches.cs b/MiraAPI/Patches/RoleGuidePatches.cs index 62eedc9..2e6a926 100644 --- a/MiraAPI/Patches/RoleGuidePatches.cs +++ b/MiraAPI/Patches/RoleGuidePatches.cs @@ -10,6 +10,7 @@ using Innersloth.Assets; using MiraAPI.LocalSettings; using MiraAPI.Modifiers; using MiraAPI.Modifiers.Types; +using MiraAPI.Patches.Options; using MiraAPI.Roles; using MiraAPI.Translation; using MiraAPI.Utilities; @@ -1807,26 +1808,19 @@ public static class RoleGuidePatches return; } + var vanillaTab = guide.settingsTabs[SettingsTabIndex]; + vanillaTab.SetActive(false); + var vanillaEntries = GameOptionsMenuPatch.GetActiveGameSettingsRows(guide.GetBoolString); + if (vanillaEntries.Count == 0) + { + vanillaEntries = BuildVanillaSettingsEntries(guide); + } _settingsPages = GuideOptionPages.BuildPages(guide.GetBoolString); if (_settingsPage > _settingsPages.Count) { _settingsPage = 0; } - var vanillaTab = guide.settingsTabs[SettingsTabIndex]; - - // Keep the vanilla tab when there are no MiraAPI settings pages. - if (_settingsPages.Count == 0) - { - _settingsSearching = false; - _settingsScroller?.gameObject.SetActive(false); - _settingsNoResults?.SetActive(false); - vanillaTab.SetActive(true); - RestoreSortButtons(); - UpdateSettingsTitle(); - return; - } - var searchText = GetSearchText().Trim(); _settingsSearching = searchText.Length > 0; @@ -1838,9 +1832,7 @@ public static class RoleGuidePatches } else if (_settingsPage == 0) { - _settingsScroller?.gameObject.SetActive(false); - _settingsNoResults?.SetActive(false); - vanillaTab.SetActive(true); + ShowSettingsRows(vanillaEntries, scrollToTop); RepurposeSortButtons(true); } else @@ -1853,7 +1845,62 @@ public static class RoleGuidePatches UpdateSettingsTitle(); } + internal static void RefreshSettingsForGameMode() + { + if (IsSettingsTabActive) + { + RefreshSettingsTab(false); + } + } + + private static List> BuildVanillaSettingsEntries(MatchInfoGuide guide) + { + foreach (var setting in guide.NormalModeSettings.ToArray()) + { + if (setting) + { + setting.Destroy(); + } + } + + guide.NormalModeSettings.Clear(); + guide.CreateNormalModeSettings(); + + var entries = new List>(guide.NormalModeSettings.Count); + foreach (var setting in guide.NormalModeSettings) + { + if (!setting) + { + continue; + } + + var texts = setting.GetComponentsInChildren(true); + if (texts.Length < 2) + { + Warning($"Could not read vanilla settings row '{setting.name}'."); + continue; + } + + entries.Add(new KeyValuePair(texts[0].text, texts[1].text)); + } + + return entries; + } + private static void ShowSettingsEntries(IReadOnlyList entries, bool showGroup, bool scrollToTop) + { + var rows = new List>(entries.Count); + foreach (var entry in entries) + { + var label = showGroup ? $"{entry.GroupName}\n{entry.Title}" : entry.Title; + rows.Add(new KeyValuePair(label, entry.Value)); + } + + ShowSettingsRows(rows, scrollToTop); + _settingsNoResults?.SetActive(entries.Count == 0); + } + + private static void ShowSettingsRows(IReadOnlyList> rows, bool scrollToTop) { var guide = MatchInfoGuide.Instance; if (_settingsScroller == null || !guide || !guide.MatchInfoRolePanelPrefab) @@ -1870,16 +1917,9 @@ public static class RoleGuidePatches _settingsPageObject = null; } - var rows = new List>(entries.Count); - foreach (var entry in entries) - { - var label = showGroup ? $"{entry.GroupName}\n{entry.Title}" : entry.Title; - rows.Add(new KeyValuePair(label, entry.Value)); - } - _settingsScroller.gameObject.SetActive(true); _settingsPageObject = Helpers.CreateAdvancedSettingsPage(guide, _settingsScroller, rows); - _settingsNoResults?.SetActive(entries.Count == 0); + _settingsNoResults?.SetActive(rows.Count == 0); if (scrollToTop) { _settingsScroller.ScrollToTop(); @@ -1889,20 +1929,26 @@ public static class RoleGuidePatches private static string GetSettingsTitle() { var baseTitle = MiraLocaleManager.Get("MiraApi.Wiki.SettingsTab"); - if (!_uiInitialized || _settingsPages.Count == 0) + if (!_uiInitialized) { return baseTitle; } + if (_settingsPage == 0 || _settingsPages.Count == 0) + { + var vanillaTitle = $"{MiraLocaleManager.Get("MiraApi.Wiki.VanillaSettings", "Vanilla")} Settings"; + return _settingsPages.Count == 0 + ? vanillaTitle + : $"{vanillaTitle} ({_settingsPage + 1}/{_settingsPages.Count + 1})"; + } + if (_settingsSearching) { return $"{baseTitle}: {MiraLocaleManager.Get("MiraApi.Wiki.SearchResults", "Search Results")}"; } var counter = $"({_settingsPage + 1}/{_settingsPages.Count + 1})"; - return _settingsPage == 0 - ? $"{baseTitle} {counter}" - : $"{baseTitle}: {_settingsPages[_settingsPage - 1].Title} {counter}"; + return $"{_settingsPages[_settingsPage - 1].Title} Settings {counter}"; } private static void UpdateSettingsTitle()