mirror of
https://github.com/All-Of-Us-Mods/MiraAPI.git
synced 2026-11-19 13:17:10 +00:00
some more code cleanup + undoing a todo
it is a miracle the game mod option worked the way it did. i would like to have a word with the one who was using a dictionary like a list. prepare to meet your maker
This commit is contained in:
10 files changed
+71
-131
No files matched your search
@@ -77,6 +77,8 @@ public abstract class AbstractGameMode : IOptionable
|
||||
/// </summary>
|
||||
public virtual float DefaultImpostorKillCooldown => GameOptionsManager.Instance.CurrentGameOptions.GetFloat(FloatOptionNames.KillCooldown);
|
||||
|
||||
internal StringNames StringName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Called on GameManager.GetBodyType().
|
||||
/// </summary>
|
||||
|
||||
@@ -44,9 +44,10 @@ public static class CustomGameModeManager
|
||||
if (mode.HideMode)
|
||||
return true;
|
||||
|
||||
IdToModeMap.Add(GetNextId(), mode);
|
||||
pluginInfo.GameModes.Add(lastId, mode);
|
||||
mode.ID = lastId;
|
||||
var id = GetNextId();
|
||||
mode.ID = id;
|
||||
IdToModeMap.Add(id, mode);
|
||||
pluginInfo.GameModes.Add(id, mode);
|
||||
GameModeOption.AddOption(mode);
|
||||
return true;
|
||||
}
|
||||
@@ -107,34 +108,31 @@ public static class CustomGameModeManager
|
||||
|
||||
internal static void RegisterDefaultMode()
|
||||
{
|
||||
var defaultMode = new ClassicMode();
|
||||
var defaultMode = new ClassicMode { ID = 0 };
|
||||
IdToModeMap.Add(0, defaultMode);
|
||||
GameModeOption.AddOption(defaultMode);
|
||||
|
||||
// no need to add to game mode option as it already contains it
|
||||
// because we cannot have the option be created with no values
|
||||
defaultMode.ID = 0;
|
||||
var hnsMode = new HideAndSeekMode();
|
||||
var hnsMode = new HideAndSeekMode() { ID = 1 };
|
||||
|
||||
if (hnsMode.HideMode)
|
||||
return;
|
||||
|
||||
IdToModeMap.Add(1, hnsMode);
|
||||
hnsMode.ID = 1;
|
||||
GameModeOption.AddOption(hnsMode);
|
||||
lastId++;
|
||||
}
|
||||
|
||||
internal static void GetAndSetGameMode()
|
||||
{
|
||||
var id = (uint)GameModeOption.Value;
|
||||
var index = GameModeOption.Value;
|
||||
|
||||
if (IdToModeMap.TryGetValue(id, out var mode))
|
||||
if ((uint)index < (uint)GameModeOption.Values.Count)
|
||||
{
|
||||
ActiveMode = mode;
|
||||
ActiveMode = GameModeOption.Values[index];
|
||||
return;
|
||||
}
|
||||
|
||||
ActiveMode = IdToModeMap[0];
|
||||
Warning($"Unable to find game mode of id {id}!");
|
||||
Warning($"Unable to find game mode at index {index}!");
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,5 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
using HarmonyLib;
|
||||
using MiraAPI.GameOptions;
|
||||
using MiraAPI.Networking;
|
||||
@@ -27,113 +26,63 @@ public static class GameModeOption
|
||||
/// </summary>
|
||||
public static int Value
|
||||
{
|
||||
get =>
|
||||
OptionBehaviour != null
|
||||
? OptionBehaviour.GetInt()
|
||||
: LastValue;
|
||||
get => OptionBehaviour != null
|
||||
? OptionBehaviour.GetInt()
|
||||
: LastValue;
|
||||
private set
|
||||
{
|
||||
LastValue = value;
|
||||
|
||||
if (OptionBehaviour == null)
|
||||
return;
|
||||
|
||||
OptionBehaviour.Value = value;
|
||||
OptionBehaviour.UpdateValue();
|
||||
LastValue = value;
|
||||
}
|
||||
}
|
||||
|
||||
internal static readonly StringNames GamemodeName = MiraLocaleManager.GetOrCreateLocaleString("MiraApi.Gamemode");
|
||||
internal static readonly StringNames CustomName = MiraLocaleManager.GetOrCreateLocaleString("Custom");
|
||||
internal static readonly Dictionary<uint, StringNames> Values = new()
|
||||
{
|
||||
[0] = MiraLocaleManager.GetOrCreateLocaleString("MiraApi.Gamemode.Classic"),
|
||||
};
|
||||
internal static readonly List<AbstractGameMode> Values = [];
|
||||
|
||||
internal static StringOption OptionBehaviour = null!;
|
||||
internal static int LastValue;
|
||||
|
||||
internal static void AddOption(AbstractGameMode mode)
|
||||
{
|
||||
if (!Values.ContainsKey(mode.ID))
|
||||
Values.Add(mode.ID, MiraLocaleManager.GetOrCreateLocaleString(mode.Name));
|
||||
mode.StringName = MiraLocaleManager.GetOrCreateLocaleString(mode.Name);
|
||||
Values.Add(mode);
|
||||
}
|
||||
|
||||
/*[HarmonyPatch(typeof(GameOptionsMenu), nameof(GameOptionsMenu.CreateSettings))]
|
||||
[HarmonyPostfix]
|
||||
private static void CreateSettingsPatch(GameOptionsMenu __instance)
|
||||
{
|
||||
if (MenuState.Instance.CurrentModIdx != 0 || GameManager.Instance.IsHideAndSeek() || CustomGameModeManager.ActiveMode == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var num = 0.713f;
|
||||
|
||||
foreach (var category in __instance.settingsContainer.GetComponentsInChildren<CategoryHeaderMasked>())
|
||||
{
|
||||
if (category)
|
||||
category.gameObject.transform.localPosition -= new Vector3(0, 1.3f, 0);
|
||||
}
|
||||
CategoryHeaderMasked categoryHeaderMasked = Object.Instantiate(__instance.categoryHeaderOrigin, Vector3.zero, Quaternion.identity, __instance.settingsContainer);
|
||||
categoryHeaderMasked.SetHeader(CustomName, 20);
|
||||
categoryHeaderMasked.transform.localScale = Vector3.one * 0.63f;
|
||||
categoryHeaderMasked.transform.localPosition = new Vector3(-0.903f, num, -2f);
|
||||
OptionBehaviour = Object.Instantiate(
|
||||
__instance.stringOptionOrigin,
|
||||
Vector3.zero,
|
||||
Quaternion.identity,
|
||||
__instance.settingsContainer);
|
||||
num -= 0.63f;
|
||||
OptionBehaviour.transform.localPosition = new Vector3(0.952f, num, -2f);
|
||||
OptionBehaviour.SetClickMask(__instance.ButtonClickMask);
|
||||
StringGameSetting setting = ScriptableObject.CreateInstance<StringGameSetting>();
|
||||
setting.Type = OptionTypes.MultipleChoice;
|
||||
setting.Title = GamemodeName;
|
||||
setting.Index = _lastValue;
|
||||
setting.Values = new Il2CppStructArray<StringNames>([Values[0]]);
|
||||
OptionBehaviour.SetUpFromData(setting, 20);
|
||||
Set(_lastValue);
|
||||
OptionBehaviour.TitleText.fontSize = 3;
|
||||
foreach (var optionBehaviour in __instance.Children.ToArray().Skip(1))
|
||||
{
|
||||
optionBehaviour.gameObject.transform.localPosition -= new Vector3(0, 1.3f, 0);
|
||||
}
|
||||
__instance.Children.Add(OptionBehaviour);
|
||||
for (var i = 1; i < Values.Count; i++)
|
||||
OptionBehaviour.Values = (Il2CppStructArray<StringNames>)OptionBehaviour.Values.Add(Values.ElementAt(i).Value);
|
||||
__instance.scrollBar.SetYBoundsMax(__instance.scrollBar.GetYBounds().max + 1);
|
||||
}*/
|
||||
|
||||
internal static void Set(int val)
|
||||
{
|
||||
Value = val;
|
||||
var previousMode = CustomGameModeManager.ActiveMode;
|
||||
CustomGameModeManager.GetAndSetGameMode();
|
||||
HudPatches.SetGameModeText(CustomGameModeManager.GetMode(Values.ElementAt(LastValue).Key).ColoredName);
|
||||
HudPatches.SetGameModeText(Values[LastValue].ColoredName);
|
||||
var gm = CustomGameModeManager.ActiveMode!;
|
||||
|
||||
if (gm != previousMode)
|
||||
{
|
||||
ModdedOptionsManager.AddSettingsChangeMessage(
|
||||
HudManager.Instance.Notifier,
|
||||
GamemodeName,
|
||||
gm.ColoredName,
|
||||
new Color(0.7333f, 0.7333f, 0.7333f, 1),
|
||||
gm.TmpIcon);
|
||||
if (MenuState.Instance)
|
||||
{
|
||||
// should force reset roles for gamemodes properly
|
||||
foreach (var roleMenu in MenuState.Instance.QueuedRoleMenuRefresh)
|
||||
{
|
||||
MenuState.Instance.QueuedRoleMenuRefresh[roleMenu.Key] = true;
|
||||
}
|
||||
}
|
||||
if (gm == previousMode)
|
||||
return;
|
||||
|
||||
GameOptionsMenuPatch.RefreshForGameMode();
|
||||
RoleGuidePatches.RefreshSettingsForGameMode();
|
||||
ModdedOptionsManager.AddSettingsChangeMessage(
|
||||
HudManager.Instance.Notifier,
|
||||
GamemodeName,
|
||||
gm.ColoredName,
|
||||
new Color(0.7333f, 0.7333f, 0.7333f, 1),
|
||||
gm.TmpIcon);
|
||||
|
||||
if (MenuState.Instance)
|
||||
{
|
||||
// should force reset roles for gamemodes properly
|
||||
foreach (var roleMenu in MenuState.Instance.QueuedRoleMenuRefresh)
|
||||
{
|
||||
MenuState.Instance.QueuedRoleMenuRefresh[roleMenu.Key] = true;
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: could make Values a dict of AbstractGameMode too
|
||||
GameOptionsMenuPatch.RefreshForGameMode();
|
||||
RoleGuidePatches.RefreshSettingsForGameMode();
|
||||
}
|
||||
|
||||
[HarmonyPatch(typeof(GameOptionsMenu), nameof(GameOptionsMenu.ValueChanged))]
|
||||
@@ -150,8 +99,7 @@ public static class GameModeOption
|
||||
}
|
||||
|
||||
[MethodRpc((uint)MiraRpc.SyncGamemodeOption)]
|
||||
[SuppressMessage("Style", "IDE0060:Remove unused parameter", Justification = "Intentionally unused because the main intent is for it to be used by dependent mods to add to a blacklist.")]
|
||||
internal static void RpcSyncGamemode(PlayerControl host, int data)
|
||||
internal static void RpcSyncGamemode(PlayerControl _, int data)
|
||||
{
|
||||
Set(data);
|
||||
}
|
||||
|
||||
@@ -29,12 +29,7 @@ public abstract class CustomMultiSelectMenu<TEntry> : CustomPhoneMenu<CustomMult
|
||||
private Color? activeColor;
|
||||
private Color? hoverSelectColor;
|
||||
private Color? hoverDeselectColor;
|
||||
|
||||
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
|
||||
[SuppressMessage("Minor Code Smell", "S1104:Fields should not have public accessibility", Justification = "Unity convention.")]
|
||||
public UiElement confirmButton;
|
||||
#pragma warning restore CS1591 // Missing XML comment for publicly visible type or member
|
||||
|
||||
private UiElement confirmButton;
|
||||
private Action<List<TEntry>>? onSelection;
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -175,13 +175,13 @@ public static class OptionsMenuPatches
|
||||
LocalSettingsManager.AvailableTabs.ForEach(CustomClose); // Close all mod tabs
|
||||
|
||||
// Tabs with index 10 and above are the mod settings tabs
|
||||
if (index >= 10)
|
||||
if (index < 10)
|
||||
{
|
||||
CustomOpen(LocalSettingsManager.AvailableTabs[index - 10]);
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
return true;
|
||||
CustomOpen(LocalSettingsManager.AvailableTabs[index - 10]);
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -4,7 +4,6 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using BepInEx.Unity.IL2CPP.Utils.Collections;
|
||||
using HarmonyLib;
|
||||
using Il2CppInterop.Runtime.InteropTypes.Arrays;
|
||||
using MiraAPI.GameModes;
|
||||
using MiraAPI.GameOptions;
|
||||
using MiraAPI.Modifiers;
|
||||
@@ -16,7 +15,6 @@ using Reactor.Utilities.Extensions;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Events;
|
||||
using UnityEngine.ProBuilder;
|
||||
using UnityEngine.UI;
|
||||
using Object = UnityEngine.Object;
|
||||
|
||||
@@ -218,7 +216,7 @@ internal static class GameOptionsMenuPatch
|
||||
{
|
||||
if (!option || !option.gameObject.activeInHierarchy ||
|
||||
option == menu.MapPicker ||
|
||||
(GameModeOption.OptionBehaviour && option == GameModeOption.OptionBehaviour) ||
|
||||
option == GameModeOption.OptionBehaviour ||
|
||||
!option.Data)
|
||||
{
|
||||
continue;
|
||||
@@ -250,7 +248,6 @@ internal static class GameOptionsMenuPatch
|
||||
|
||||
private static void CreateSettings(GameOptionsMenu instance, Transform container)
|
||||
{
|
||||
var num = 0.713f;
|
||||
VanillaHeaders.Clear();
|
||||
VanillaOptions.Clear();
|
||||
MainOptions.Clear();
|
||||
@@ -258,35 +255,36 @@ internal static class GameOptionsMenuPatch
|
||||
GameModeOptions.Clear();
|
||||
GameModeGroups.Clear();
|
||||
|
||||
var gmCategory = Object.Instantiate(instance.categoryHeaderOrigin, Vector3.zero, Quaternion.identity, container);
|
||||
var num = 0.713f;
|
||||
|
||||
var gmCategory = Object.Instantiate(instance.categoryHeaderOrigin, container);
|
||||
gmCategory.SetHeader(GameModeOption.CustomName, 20);
|
||||
gmCategory.transform.localScale = Vector3.one * 0.63f;
|
||||
gmCategory.transform.localPosition = new Vector3(-0.903f, num, -2f);
|
||||
GameModeOption.OptionBehaviour = Object.Instantiate(instance.stringOptionOrigin, container);
|
||||
|
||||
num -= 0.63f;
|
||||
|
||||
GameModeOption.OptionBehaviour = Object.Instantiate(instance.stringOptionOrigin, container);
|
||||
GameModeOption.OptionBehaviour.transform.localPosition = new Vector3(0.952f, num, -2f);
|
||||
GameModeOption.OptionBehaviour.SetClickMask(instance.ButtonClickMask);
|
||||
|
||||
var setting = ScriptableObject.CreateInstance<StringGameSetting>();
|
||||
setting.Type = OptionTypes.MultipleChoice;
|
||||
setting.Title = GameModeOption.GamemodeName;
|
||||
setting.Index = GameModeOption.LastValue;
|
||||
setting.Values = new Il2CppStructArray<StringNames>([GameModeOption.Values[0]]);
|
||||
setting.Values = GameModeOption.Values.Select(x => x.StringName).ToArray();
|
||||
|
||||
GameModeOption.OptionBehaviour.SetUpFromData(setting, 20);
|
||||
GameModeOption.Set(GameModeOption.LastValue);
|
||||
GameModeOption.OptionBehaviour.TitleText.fontSize = 3;
|
||||
|
||||
instance.Children.Add(GameModeOption.OptionBehaviour);
|
||||
|
||||
foreach (var optionBehaviour in instance.Children)
|
||||
{
|
||||
MainOptions.Add(optionBehaviour);
|
||||
}
|
||||
|
||||
for (var i = 1; i < GameModeOption.Values.Count; i++)
|
||||
{
|
||||
GameModeOption.OptionBehaviour.Values =
|
||||
(Il2CppStructArray<StringNames>)GameModeOption.OptionBehaviour.Values.Add(
|
||||
GameModeOption.Values.ElementAt(i).Value);
|
||||
}
|
||||
|
||||
var modeInfoHolder = new GameObject("GamemodeInfo")
|
||||
{
|
||||
transform =
|
||||
@@ -298,7 +296,7 @@ internal static class GameOptionsMenuPatch
|
||||
layer = container.gameObject.layer,
|
||||
}.transform;
|
||||
|
||||
var gamemodeBg = Object.Instantiate(GameModeOption.OptionBehaviour.transform.GetChild(0).gameObject, Vector3.zero, Quaternion.identity, modeInfoHolder);
|
||||
var gamemodeBg = Object.Instantiate(GameModeOption.OptionBehaviour.transform.GetChild(0).gameObject, modeInfoHolder);
|
||||
gamemodeBg.transform.localPosition = Vector3.zero;
|
||||
gamemodeBg.transform.localScale = new Vector3(1.35f, 0.85f, 1);
|
||||
gamemodeBg.name = "Background";
|
||||
@@ -317,7 +315,7 @@ internal static class GameOptionsMenuPatch
|
||||
_modIcon.sprite = MiraAssets.BlankSprite.LoadAsset();
|
||||
_modIcon.material = gamemodeBg.gameObject.GetComponent<SpriteRenderer>().material;
|
||||
|
||||
var gamemodeTextObj = Object.Instantiate(GameModeOption.OptionBehaviour.transform.GetChild(1).gameObject, Vector3.zero, Quaternion.identity, modeInfoHolder);
|
||||
var gamemodeTextObj = Object.Instantiate(GameModeOption.OptionBehaviour.transform.GetChild(1).gameObject, modeInfoHolder);
|
||||
gamemodeTextObj.transform.localPosition = new Vector3(0.3f, 0, -0.01f);
|
||||
gamemodeTextObj.transform.localScale = new Vector3(2.12f, 2.12f, 1);
|
||||
_gamemodeDescription = gamemodeTextObj.GetComponentInChildren<TextMeshPro>();
|
||||
@@ -469,11 +467,7 @@ internal static class GameOptionsMenuPatch
|
||||
|
||||
private static void CreateGroup(GameOptionsMenu menu, AbstractOptionGroup group, Transform container, ref float num, ref List<OptionBehaviour> optionBehaviours, ref List<CategoryHeaderMasked> categoryHeaders)
|
||||
{
|
||||
var categoryHeaderMasked = Object.Instantiate(
|
||||
menu.categoryHeaderOrigin,
|
||||
Vector3.zero,
|
||||
Quaternion.identity,
|
||||
container);
|
||||
var categoryHeaderMasked = Object.Instantiate(menu.categoryHeaderOrigin, container);
|
||||
categoryHeaderMasked.transform.localPosition = new Vector3(-0.903f, num + 1, -2f);
|
||||
categoryHeaderMasked.transform.localScale = Vector3.one * 0.63f;
|
||||
categoryHeaders.Add(categoryHeaderMasked);
|
||||
@@ -510,12 +504,14 @@ internal static class GameOptionsMenuPatch
|
||||
foreach (var newOpt in options)
|
||||
{
|
||||
newOpt.SetClickMask(menu.ButtonClickMask);
|
||||
SpriteRenderer[] componentsInChildren = newOpt.GetComponentsInChildren<SpriteRenderer>(true);
|
||||
var 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();
|
||||
|
||||
@@ -32,7 +32,7 @@ public static class RoleSettingMenuPatches
|
||||
private static readonly Dictionary<RoleOptionsGroup, bool> RoleGroupHidden = [];
|
||||
|
||||
internal static RoleBehaviour? CurrentRole;
|
||||
internal static List<IModdedOption>? CurrentRoleOptions;
|
||||
internal static IModdedOption[]? CurrentRoleOptions;
|
||||
|
||||
private static float scrollerNum = 0.522f;
|
||||
|
||||
@@ -503,10 +503,11 @@ public static class RoleSettingMenuPatches
|
||||
__instance.advancedSettingChildren.Clear();
|
||||
|
||||
// TODO: create sub groups under the role settings.
|
||||
var type = role.GetType();
|
||||
var filteredOptions = MenuState.Instance.CurrentMod.InternalOptionGroups
|
||||
.Where(x => x.GroupVisible() && x.OptionableType == role.GetType())
|
||||
.Where(x => x.OptionableType == type && x.GroupVisible())
|
||||
.SelectMany(x => x.Options)
|
||||
.ToList();
|
||||
.ToArray();
|
||||
|
||||
foreach (var option in filteredOptions)
|
||||
{
|
||||
|
||||
@@ -269,7 +269,7 @@ internal static class MeetingHudPatches
|
||||
|
||||
[HarmonyPrefix]
|
||||
[HarmonyPatch(nameof(MeetingHud.PopulateResults))]
|
||||
public static bool PopulateResultsPatch(ref Il2CppStructArray<MeetingHud.VoterState> states)
|
||||
public static bool PopulateResultsPatch(Il2CppStructArray<MeetingHud.VoterState> states)
|
||||
{
|
||||
var votes = states.Select(x => new CustomVote(x.VoterId, x.VotedForId)).ToList();
|
||||
|
||||
|
||||
@@ -682,7 +682,7 @@ public static class Extensions
|
||||
/// <returns><see langword="true"/> if the <see cref="OptionBehaviour"/> is for a custom options, <see langword="false"/> otherwise.</returns>
|
||||
public static bool IsCustom(this OptionBehaviour optionBehaviour)
|
||||
{
|
||||
return ModdedOptionsManager.ModdedOptions.Values.Any(opt => opt.OptionBehaviour && opt.OptionBehaviour == optionBehaviour) || optionBehaviour.Equals(GameModeOption.OptionBehaviour);
|
||||
return ModdedOptionsManager.ModdedOptions.Values.Any(opt => opt.OptionBehaviour == optionBehaviour) || optionBehaviour.Equals(GameModeOption.OptionBehaviour);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -370,7 +370,7 @@ public static class Helpers
|
||||
public static LobbyNotificationMessage CreateAndShowNotification(string text, Color color, Vector3 localPos, AudioClip? clip = null, Sprite? spr = null)
|
||||
{
|
||||
var popper = HudManager.Instance.Notifier;
|
||||
var newMessage = Object.Instantiate(popper.notificationMessageOrigin, Vector3.zero, Quaternion.identity, popper.transform);
|
||||
var newMessage = Object.Instantiate(popper.notificationMessageOrigin, popper.transform);
|
||||
newMessage.transform.localPosition = localPos;
|
||||
newMessage.SetUp(text, spr ?? null, color, new Action(() => popper.OnMessageDestroy(newMessage)));
|
||||
popper.lastMessageKey = -1;
|
||||
|
||||
Reference in new issue
Block a user