mirror of
https://github.com/All-Of-Us-Mods/MiraAPI.git
synced 2026-10-24 16:46:45 +00:00
464 lines
16 KiB
C#
464 lines
16 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Linq;
|
|
using BepInEx.Unity.IL2CPP.Utils.Collections;
|
|
using HarmonyLib;
|
|
using MiraAPI.GameOptions;
|
|
using MiraAPI.Modifiers;
|
|
using MiraAPI.Presets;
|
|
using MiraAPI.Utilities;
|
|
using MiraAPI.Utilities.Assets;
|
|
using Reactor.Localization.Utilities;
|
|
using Reactor.Utilities.Extensions;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.Events;
|
|
using UnityEngine.UI;
|
|
using Object = UnityEngine.Object;
|
|
|
|
namespace MiraAPI.Patches.Options;
|
|
|
|
[HarmonyPatch(typeof(GameOptionsMenu))]
|
|
internal static class GameOptionsMenuPatch
|
|
{
|
|
[HarmonyPostfix]
|
|
[HarmonyPatch(nameof(GameOptionsMenu.OnEnable))]
|
|
public static void OpenPatch(GameOptionsMenu __instance)
|
|
{
|
|
HudManager.Instance.PlayerCam.OverrideScreenShakeEnabled = false;
|
|
}
|
|
|
|
[HarmonyPostfix]
|
|
[HarmonyPatch(nameof(GameOptionsMenu.OnDisable))]
|
|
public static void ClosePatch(GameOptionsMenu __instance)
|
|
{
|
|
HudManager.Instance.PlayerCam.OverrideScreenShakeEnabled = true;
|
|
}
|
|
|
|
[HarmonyPostfix]
|
|
[HarmonyPatch(nameof(GameOptionsMenu.CloseMenu))]
|
|
public static void FullMenuClosePatch(GameOptionsMenu __instance)
|
|
{
|
|
Utilities.Extensions.ClearGarbageCollector();
|
|
}
|
|
|
|
[HarmonyPostfix]
|
|
[HarmonyPatch(nameof(GameOptionsMenu.Update))]
|
|
// ReSharper disable once InconsistentNaming
|
|
public static void UpdatePatch(GameOptionsMenu __instance)
|
|
{
|
|
if (GameSettingMenuPatches.SelectedModIdx == 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
var num = 2.1f;
|
|
if (__instance.name == "MODIFIERS TAB")
|
|
{
|
|
ModifiersUpdate(ref num);
|
|
}
|
|
else if (__instance.name == "CUSTOM TAB 1")
|
|
{
|
|
CustomMenuOneUpdate(ref num);
|
|
}
|
|
else if (__instance.name == "CUSTOM TAB 2")
|
|
{
|
|
CustomMenuTwoUpdate(ref num);
|
|
}
|
|
else
|
|
{
|
|
var filteredGroups =
|
|
GameSettingMenuPatches.SelectedMod?.InternalOptionGroups
|
|
.Where(x => x.OptionableType == null &&
|
|
x.ParentMenu == MenuCategory.Game) ?? [];
|
|
|
|
foreach (var group in filteredGroups)
|
|
{
|
|
UpdateGroup(group, ref num);
|
|
}
|
|
}
|
|
|
|
__instance.scrollBar.SetYBoundsMax(-num - 1.65f);
|
|
}
|
|
|
|
private static void UpdateGroup(AbstractOptionGroup? group, ref float num)
|
|
{
|
|
if (group is null || group.Options.Count == 0 || group.Header is null || group.CreationCoroutine is not null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (!group.GroupVisible.Invoke())
|
|
{
|
|
group.Header.gameObject.SetActive(false);
|
|
foreach (var option in group.Options)
|
|
{
|
|
option.OptionBehaviour?.gameObject.SetActive(false);
|
|
}
|
|
|
|
return;
|
|
}
|
|
|
|
group.Header.gameObject.SetActive(true);
|
|
group.Header.transform.localScale = Vector3.one * 0.63f;
|
|
group.Header.transform.localPosition = new Vector3(-0.903f, num, -2f);
|
|
|
|
num -= 0.58f;
|
|
|
|
foreach (var opt in group.Options)
|
|
{
|
|
var newOpt = opt.OptionBehaviour;
|
|
|
|
if (!newOpt || newOpt == null)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
if (!opt.Visible.Invoke())
|
|
{
|
|
newOpt.gameObject.SetActive(false);
|
|
continue;
|
|
}
|
|
|
|
if (!group.AllOptionsHidden)
|
|
{
|
|
newOpt.gameObject.SetActive(true);
|
|
newOpt.transform.localPosition = new Vector3(0.952f, num, -2f);
|
|
num -= 0.45f;
|
|
}
|
|
else
|
|
{
|
|
newOpt.gameObject.SetActive(false);
|
|
}
|
|
}
|
|
}
|
|
|
|
private static void CustomMenuOneUpdate(ref float num)
|
|
{
|
|
var groups = GameSettingMenuPatches.SelectedMod?.InternalOptionGroups
|
|
.Where(x => x.ParentMenu == MenuCategory.CustomOne) ?? [];
|
|
|
|
foreach (var modGroup in groups)
|
|
{
|
|
UpdateGroup(modGroup, ref num);
|
|
}
|
|
}
|
|
|
|
private static void CustomMenuOneCreate(GameOptionsMenu menu)
|
|
{
|
|
var groups = GameSettingMenuPatches.SelectedMod?.InternalOptionGroups
|
|
.Where(x => x.ParentMenu == MenuCategory.CustomOne) ?? [];
|
|
foreach (var group in groups)
|
|
{
|
|
CreateGroup(menu, group);
|
|
}
|
|
}
|
|
|
|
private static void CustomMenuTwoUpdate(ref float num)
|
|
{
|
|
var groups = GameSettingMenuPatches.SelectedMod?.InternalOptionGroups
|
|
.Where(x => x.ParentMenu == MenuCategory.CustomTwo) ?? [];
|
|
|
|
foreach (var modGroup in groups)
|
|
{
|
|
UpdateGroup(modGroup, ref num);
|
|
}
|
|
}
|
|
|
|
private static void CustomMenuTwoCreate(GameOptionsMenu menu)
|
|
{
|
|
var groups = GameSettingMenuPatches.SelectedMod?.InternalOptionGroups
|
|
.Where(x => x.ParentMenu == MenuCategory.CustomTwo) ?? [];
|
|
foreach (var group in groups)
|
|
{
|
|
CreateGroup(menu, group);
|
|
}
|
|
}
|
|
|
|
private static void ModifiersUpdate(ref float num)
|
|
{
|
|
var groups = GameSettingMenuPatches.SelectedMod?.InternalOptionGroups
|
|
.Where(x => x.ParentMenu is MenuCategory.Modifiers ||
|
|
x.OptionableType?.IsAssignableTo(typeof(BaseModifier)) == true) ?? [];
|
|
|
|
foreach (var modGroup in groups)
|
|
{
|
|
UpdateGroup(modGroup, ref num);
|
|
}
|
|
}
|
|
|
|
private static void ModifiersCreate(GameOptionsMenu menu)
|
|
{
|
|
var groups = GameSettingMenuPatches.SelectedMod?.InternalOptionGroups
|
|
.Where(x => x.ParentMenu is MenuCategory.Modifiers ||
|
|
x.OptionableType?.IsAssignableTo(typeof(BaseModifier)) == true) ?? [];
|
|
foreach (var group in groups)
|
|
{
|
|
CreateGroup(menu, group);
|
|
}
|
|
}
|
|
|
|
[HarmonyPrefix]
|
|
[HarmonyPatch(nameof(GameOptionsMenu.CreateSettings))]
|
|
// ReSharper disable once InconsistentNaming
|
|
public static bool SettingsPatch(GameOptionsMenu __instance)
|
|
{
|
|
if (GameSettingMenuPatches.SelectedModIdx == 0)
|
|
{
|
|
return true;
|
|
}
|
|
|
|
__instance.MapPicker.gameObject.SetActive(false);
|
|
|
|
switch (__instance.name)
|
|
{
|
|
case "MODIFIERS TAB":
|
|
ModifiersCreate(__instance);
|
|
return false;
|
|
case "CUSTOM TAB 1":
|
|
CustomMenuOneCreate(__instance);
|
|
return false;
|
|
case "CUSTOM TAB 2":
|
|
CustomMenuTwoCreate(__instance);
|
|
return false;
|
|
}
|
|
|
|
var filteredGroups = GameSettingMenuPatches.SelectedMod?.InternalOptionGroups
|
|
.Where(x => x.OptionableType == null && x.ParentMenu == MenuCategory.Game) ?? [];
|
|
|
|
foreach (var group in filteredGroups)
|
|
{
|
|
CreateGroup(__instance, group);
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
[HarmonyPrefix]
|
|
[HarmonyPatch(nameof(GameOptionsMenu.Initialize))]
|
|
// ReSharper disable once InconsistentNaming
|
|
public static bool InitPatch(GameOptionsMenu __instance)
|
|
{
|
|
if (__instance.Children != null && __instance.Children.Count != 0)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
__instance.MapPicker.gameObject.SetActive(true);
|
|
__instance.MapPicker.Initialize(20);
|
|
var mapNameSetting = GameManager.Instance.GameSettingsList.MapNameSetting;
|
|
__instance.MapPicker.SetUpFromData(mapNameSetting, 20);
|
|
__instance.Children = new Il2CppSystem.Collections.Generic.List<OptionBehaviour>();
|
|
__instance.Children.Add(__instance.MapPicker);
|
|
__instance.CreateSettings();
|
|
__instance.cachedData = GameOptionsManager.Instance.CurrentGameOptions;
|
|
foreach (var optionBehaviour in __instance.Children)
|
|
{
|
|
if (AmongUsClient.Instance && !AmongUsClient.Instance.AmHost)
|
|
{
|
|
optionBehaviour.SetAsPlayer();
|
|
}
|
|
|
|
if (optionBehaviour.IsCustom())
|
|
{
|
|
continue;
|
|
}
|
|
|
|
optionBehaviour.OnValueChanged = new Action<OptionBehaviour>(__instance.ValueChanged);
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
private static IEnumerator CoCreateGroup(GameOptionsMenu menu, AbstractOptionGroup group)
|
|
{
|
|
var categoryHeaderMasked = Object.Instantiate(
|
|
menu.categoryHeaderOrigin,
|
|
Vector3.zero,
|
|
Quaternion.identity,
|
|
menu.settingsContainer);
|
|
|
|
categoryHeaderMasked.SetHeader(CustomStringName.CreateAndRegister(group.GroupName), 20);
|
|
categoryHeaderMasked.Background.color = group.GroupColor;
|
|
categoryHeaderMasked.Divider.color = group.GroupColor;
|
|
categoryHeaderMasked.Title.color = group.GroupColor.Equals(MiraApiPlugin.DefaultHeaderColor)
|
|
? Color.white
|
|
: group.GroupColor.FindAlternateColor();
|
|
|
|
categoryHeaderMasked.Background.sprite = MiraAssets.CategoryHeader.LoadAsset();
|
|
categoryHeaderMasked.Background.sprite.texture.filterMode = FilterMode.Bilinear;
|
|
categoryHeaderMasked.Background.sprite.texture.wrapMode = TextureWrapMode.Clamp;
|
|
|
|
categoryHeaderMasked.Background.transform.localPosition = new Vector3(0.5f, -0.1833f, 0);
|
|
categoryHeaderMasked.Background.size = new Vector2(
|
|
categoryHeaderMasked.Background.size.x + 1.5f,
|
|
categoryHeaderMasked.Background.size.y);
|
|
|
|
categoryHeaderMasked.gameObject.SetActive(false);
|
|
group.Header = categoryHeaderMasked;
|
|
|
|
var newText = Object.Instantiate(categoryHeaderMasked.Title, categoryHeaderMasked.transform);
|
|
newText.text = "<size=70%>(Click to close)</size>";
|
|
newText.transform.localPosition = new Vector3(2.6249f, -0.165f, 0f);
|
|
newText.gameObject.GetComponent<TextTranslatorTMP>().Destroy();
|
|
|
|
var options = group.Options.Select(opt => opt.CreateOption(
|
|
menu.checkboxOrigin,
|
|
menu.numberOptionOrigin,
|
|
menu.stringOptionOrigin,
|
|
menu.playerOptionOrigin,
|
|
menu.settingsContainer));
|
|
|
|
OptionPreset? defaultPreset = null;
|
|
if (GameSettingMenuPatches.SelectedMod != null && PresetManager.DefaultPresets.TryGetValue(
|
|
GameSettingMenuPatches.SelectedMod,
|
|
out var preset))
|
|
{
|
|
defaultPreset = preset;
|
|
}
|
|
|
|
yield return options.CoLoopWithBudget(newOpt =>
|
|
{
|
|
newOpt.SetClickMask(menu.ButtonClickMask);
|
|
|
|
SpriteRenderer[] componentsInChildren = newOpt.GetComponentsInChildren<SpriteRenderer>(true);
|
|
foreach (var renderer in componentsInChildren)
|
|
{
|
|
if (group.GroupColor != MiraApiPlugin.DefaultHeaderColor)
|
|
{
|
|
renderer.color = group.GroupColor.FindAlternateColor();
|
|
if (renderer.transform.parent.TryGetComponent<GameOptionButton>(out var btn))
|
|
{
|
|
btn.interactableColor = group.GroupColor.FindAlternateColor();
|
|
btn.interactableHoveredColor = Color.white;
|
|
}
|
|
}
|
|
|
|
renderer.material.SetInt(PlayerMaterial.MaskLayer, 20);
|
|
}
|
|
|
|
foreach (var textMeshPro in newOpt.GetComponentsInChildren<TextMeshPro>(true))
|
|
{
|
|
if (group.GroupColor != MiraApiPlugin.DefaultHeaderColor)
|
|
{
|
|
textMeshPro.color = group.GroupColor;
|
|
}
|
|
|
|
textMeshPro.fontMaterial.SetFloat(ShaderID.StencilComp, 3f);
|
|
textMeshPro.fontMaterial.SetFloat(ShaderID.Stencil, 20);
|
|
}
|
|
|
|
if (newOpt is ToggleOption toggle)
|
|
{
|
|
toggle.CheckMark.sprite = MiraAssets.Checkmark.LoadAsset();
|
|
toggle.CheckMark.color = group.GroupColor != MiraApiPlugin.DefaultHeaderColor
|
|
? group.GroupColor
|
|
: MiraAssets.AcceptedTeal;
|
|
var rend = toggle.CheckMark.transform.parent.FindChild("ActiveSprite")
|
|
.GetComponent<SpriteRenderer>();
|
|
rend.sprite = MiraAssets.CheckmarkBox.LoadAsset();
|
|
rend.color = group.GroupColor != MiraApiPlugin.DefaultHeaderColor
|
|
? group.GroupColor
|
|
: MiraAssets.AcceptedTeal;
|
|
}
|
|
|
|
menu.Children.Add(newOpt);
|
|
var resetBtn = new GameObject("ResetOption");
|
|
resetBtn.transform.parent = newOpt.transform;
|
|
resetBtn.transform.localScale = new(.5f, .5f, 1);
|
|
resetBtn.layer = LayerMask.NameToLayer("UI");
|
|
resetBtn.transform.localPosition = new Vector3(-3.1f, 0f, -2f);
|
|
var resetRend = resetBtn.AddComponent<SpriteRenderer>();
|
|
resetRend.sprite = MiraAssets.ResetButton.LoadAsset();
|
|
resetRend.maskInteraction = SpriteMaskInteraction.VisibleInsideMask;
|
|
resetRend.color = group.GroupColor.Equals(MiraApiPlugin.DefaultHeaderColor)
|
|
? Color.white
|
|
: group.GroupColor.FindAlternateColor();
|
|
var resetBoxCol = resetBtn.gameObject.AddComponent<BoxCollider2D>();
|
|
resetBoxCol.size = new Vector2(1f, 1f);
|
|
resetBoxCol.offset = new Vector2(0, 0);
|
|
var passiveButton = resetBtn.AddComponent<PassiveButton>();
|
|
passiveButton.OnClick = new Button.ButtonClickedEvent();
|
|
passiveButton.ClickSound = menu.BackButton.GetComponent<PassiveButton>().ClickSound;
|
|
passiveButton.OnMouseOver = new UnityEvent();
|
|
passiveButton.OnMouseOver.AddListener(
|
|
(UnityAction)(() =>
|
|
{
|
|
resetRend.color = group.GroupColor != MiraApiPlugin.DefaultHeaderColor
|
|
? group.GroupColor
|
|
: MiraAssets.AcceptedTeal;
|
|
}));
|
|
passiveButton.OnMouseOut = new UnityEvent();
|
|
passiveButton.OnMouseOut.AddListener(
|
|
(UnityAction)(() =>
|
|
{
|
|
resetRend.color = group.GroupColor.Equals(MiraApiPlugin.DefaultHeaderColor)
|
|
? Color.white
|
|
: group.GroupColor.FindAlternateColor();
|
|
}));
|
|
if (newOpt is ToggleOption toggleOpt)
|
|
{
|
|
passiveButton.OnClick.AddListener(
|
|
(UnityAction)(() =>
|
|
{
|
|
defaultPreset!.ResetOption(toggleOpt);
|
|
}));
|
|
}
|
|
else if (newOpt is NumberOption numOpt)
|
|
{
|
|
passiveButton.OnClick.AddListener(
|
|
(UnityAction)(() =>
|
|
{
|
|
defaultPreset!.ResetOption(numOpt);
|
|
}));
|
|
}
|
|
else if (newOpt is StringOption strOpt)
|
|
{
|
|
passiveButton.OnClick.AddListener(
|
|
(UnityAction)(() =>
|
|
{
|
|
defaultPreset!.ResetOption(strOpt);
|
|
}));
|
|
}
|
|
|
|
if (!defaultPreset!.IsOptionInPreset(newOpt))
|
|
{
|
|
resetBtn.Destroy();
|
|
}
|
|
|
|
newOpt.Initialize();
|
|
newOpt.gameObject.SetActive(false);
|
|
}).WrapToIl2Cpp();
|
|
|
|
var boxCol = categoryHeaderMasked.gameObject.AddComponent<BoxCollider2D>();
|
|
boxCol.size = new Vector2(7, 0.7f);
|
|
boxCol.offset = new Vector2(1.5f, -0.3f);
|
|
|
|
var headerBtn = categoryHeaderMasked.gameObject.AddComponent<PassiveButton>();
|
|
headerBtn.ClickSound = menu.BackButton.GetComponent<PassiveButton>().ClickSound;
|
|
headerBtn.OnMouseOver = new UnityEvent();
|
|
headerBtn.OnMouseOut = new UnityEvent();
|
|
headerBtn.OnClick.AddListener(
|
|
(UnityAction)(() =>
|
|
{
|
|
group.AllOptionsHidden = !group.AllOptionsHidden;
|
|
newText.text = group.AllOptionsHidden
|
|
? "<size=70%>(Click to open)</size>"
|
|
: "<size=70%>(Click to close)</size>";
|
|
}));
|
|
headerBtn.SetButtonEnableState(true);
|
|
|
|
group.CreationCoroutine = null;
|
|
}
|
|
|
|
private static void CreateGroup(GameOptionsMenu menu, AbstractOptionGroup group)
|
|
{
|
|
if (group.CreationCoroutine != null)
|
|
{
|
|
GameSettingMenu.Instance.StopCoroutine(group.CreationCoroutine);
|
|
}
|
|
|
|
group.CreationCoroutine = GameSettingMenu.Instance.StartCoroutine(CoCreateGroup(menu, group).WrapToIl2Cpp());
|
|
}
|
|
}
|