From 9874713ec8e2783b5cb5e1a075e41a124a732109 Mon Sep 17 00:00:00 2001
From: Az <56699741+AlchlcDvl@users.noreply.github.com>
Date: Thu, 5 Nov 2026 22:41:55 -0500
Subject: [PATCH 1/3] experimental settings sync fix + improvements
---
MiraAPI/Patches/AmongUsClientSyncPatch.cs | 27 +++++++++++++----
MiraAPI/Patches/Options/SettingPatches.cs | 4 +--
MiraAPI/Patches/RoleGuidePatches.cs | 2 +-
MiraAPI/Utilities/Helpers.cs | 36 ++++++++++-------------
MiraAPI/Utilities/StateMachineWrapper.cs | 15 +++++-----
5 files changed, 48 insertions(+), 36 deletions(-)
diff --git a/MiraAPI/Patches/AmongUsClientSyncPatch.cs b/MiraAPI/Patches/AmongUsClientSyncPatch.cs
index 926004d6..9fb6cbb6 100644
--- a/MiraAPI/Patches/AmongUsClientSyncPatch.cs
+++ b/MiraAPI/Patches/AmongUsClientSyncPatch.cs
@@ -1,23 +1,35 @@
-using HarmonyLib;
+using System.Reflection;
+using HarmonyLib;
+using Il2CppInterop.Runtime.InteropTypes;
using InnerNet;
using MiraAPI.Events;
using MiraAPI.Events.Vanilla.Player;
using MiraAPI.GameModes;
using MiraAPI.GameOptions;
using MiraAPI.Roles;
+using MiraAPI.Utilities;
namespace MiraAPI.Patches;
///
/// Sync all settings to the player when they join the game.
///
-[HarmonyPatch(typeof(AmongUsClient))]
+[HarmonyPatch]
internal static class AmongUsClientSyncPatch
{
- [HarmonyPostfix]
- [HarmonyPatch(typeof(AmongUsClient), nameof(AmongUsClient.CreatePlayer))]
- public static void CreatePlayerPatch(ClientData clientData)
+ public static MethodBase TargetMethod()
{
+ return Helpers.GetStateMachineMoveNext(nameof(AmongUsClient.CreatePlayer))!;
+ }
+
+ [HarmonyPostfix]
+ public static void CreatePlayerPatch(Il2CppObjectBase __instance, bool __result)
+ {
+ if (__result)
+ return;
+
+ var wrapper = new StateMachineWrapper(__instance);
+ var clientData = wrapper.GetParameter("clientData");
var joinEvent = new PlayerJoinEvent(clientData);
MiraEventManager.InvokeEvent(joinEvent);
@@ -35,9 +47,12 @@ internal static class AmongUsClientSyncPatch
CustomRoleManager.SyncAllRoleSettings(clientData.Id);
GameModeOption.RpcSyncGamemode(PlayerControl.LocalPlayer, GameModeOption.Value);
}
+}
+[HarmonyPatch(typeof(AmongUsClient), nameof(AmongUsClient.OnPlayerLeft))]
+internal static class AmongUsClientSyncPatch2
+{
[HarmonyPrefix]
- [HarmonyPatch(typeof(AmongUsClient), nameof(AmongUsClient.OnPlayerLeft))]
public static void PlayerLeftPatch(ClientData data, DisconnectReasons reason)
{
var leftEvent = new PlayerLeaveEvent(data, reason);
diff --git a/MiraAPI/Patches/Options/SettingPatches.cs b/MiraAPI/Patches/Options/SettingPatches.cs
index 1ef854b5..df2a31c7 100644
--- a/MiraAPI/Patches/Options/SettingPatches.cs
+++ b/MiraAPI/Patches/Options/SettingPatches.cs
@@ -52,7 +52,7 @@ public static class SettingPatches
MiraNumberSuffixes.None => part,
MiraNumberSuffixes.Multiplier => part + "x",
MiraNumberSuffixes.Percent => part + "%",
- MiraNumberSuffixes.Distance => string.Concat(part, " ", MiraApiPlugin.DistanceSuffix.Value),
+ MiraNumberSuffixes.Distance => part + MiraApiPlugin.DistanceSuffix.Value,
_ => TranslationController.Instance.GetString(StringNames.GameSecondsAbbrev, part),
};
}
@@ -69,7 +69,7 @@ public static class SettingPatches
MiraNumberSuffixes.None => part,
MiraNumberSuffixes.Multiplier => part + "x",
MiraNumberSuffixes.Percent => part + "%",
- MiraNumberSuffixes.Distance => string.Concat(part, " ", MiraApiPlugin.DistanceSuffix.Value),
+ MiraNumberSuffixes.Distance => part + MiraApiPlugin.DistanceSuffix.Value,
_ => TranslationController.Instance.GetString(StringNames.GameSecondsAbbrev, part),
};
}
diff --git a/MiraAPI/Patches/RoleGuidePatches.cs b/MiraAPI/Patches/RoleGuidePatches.cs
index 8cc2a67d..198e2774 100644
--- a/MiraAPI/Patches/RoleGuidePatches.cs
+++ b/MiraAPI/Patches/RoleGuidePatches.cs
@@ -296,7 +296,7 @@ public static class RoleGuidePatches
if (_titleText)
{
_titleText.transform.localPosition += new Vector3(0.5f, 0);
- _titleText.transform.GetComponent().Destroy();
+ _titleText.GetComponent().Destroy();
}
}
diff --git a/MiraAPI/Utilities/Helpers.cs b/MiraAPI/Utilities/Helpers.cs
index 823cdce4..638c41a4 100644
--- a/MiraAPI/Utilities/Helpers.cs
+++ b/MiraAPI/Utilities/Helpers.cs
@@ -177,7 +177,7 @@ public static class Helpers
{
var keybindIcon = Object.Instantiate(HudManager.Instance.AbilityButton.usesRemainingSprite.gameObject, button.transform);
keybindIcon.GetComponent().sprite = MiraAssets.KeybindButton.LoadAsset();
- keybindIcon.transform.GetComponentInChildren().text = keyCode.ToString();
+ keybindIcon.GetComponentInChildren().text = keyCode.ToString();
keybindIcon.name = "KeybindIcon";
keybindIcon.transform.localPosition = localPos;
return keybindIcon;
@@ -191,26 +191,21 @@ public static class Helpers
/// THe corresponding method to patch.
public static MethodBase? GetStateMachineMoveNext(string methodName)
{
- var typeName = typeof(T).FullName;
- var showRoleStateMachine =
- typeof(T)
- .GetNestedTypes()
- .FirstOrDefault(x => x.Name.Contains(methodName));
+ var stateMachine = typeof(T).GetNestedTypes().FirstOrDefault(x => x.Name.Contains(methodName, StringComparison.Ordinal));
- if (showRoleStateMachine == null)
+ if (stateMachine == null)
{
- Error($"Failed to find {methodName} state machine for {typeName}");
+ Error($"Failed to find {methodName} state machine for {typeof(T).FullName}");
return null;
}
- var moveNext = AccessTools.Method(showRoleStateMachine, "MoveNext");
+ var moveNext = AccessTools.Method(stateMachine, "MoveNext");
+
if (moveNext == null)
- {
- Error($"Failed to find MoveNext method for {typeName}.{methodName}");
- return null;
- }
+ Error($"Failed to find MoveNext method for {typeof(T).FullName}.{methodName}");
+ else
+ Info($"Found {methodName}.MoveNext");
- Info($"Found {methodName}.MoveNext");
return moveNext;
}
@@ -619,6 +614,7 @@ public static class Helpers
MiraNumberSuffixes.Multiplier => "x",
MiraNumberSuffixes.Seconds => "s",
MiraNumberSuffixes.Percent => "%",
+ MiraNumberSuffixes.Distance => MiraApiPlugin.DistanceSuffix.Value,
_ => string.Empty,
};
}
@@ -653,14 +649,14 @@ public static class Helpers
/// The formatted value.
public static string FormatValue(float value, MiraNumberSuffixes suffix = MiraNumberSuffixes.None, string formatString = "0.0")
{
+ var part = value.ToString(formatString, NumberFormatInfo.InvariantInfo);
return suffix switch
{
- MiraNumberSuffixes.None => value.ToString(formatString, NumberFormatInfo.InvariantInfo),
- MiraNumberSuffixes.Multiplier => value.ToString(formatString, NumberFormatInfo.InvariantInfo) + "x",
- MiraNumberSuffixes.Percent => value.ToString(formatString, NumberFormatInfo.InvariantInfo) + "%",
- _ => TranslationController.Instance.GetString(
- StringNames.GameSecondsAbbrev,
- (Il2CppSystem.Object[])[value.ToString(formatString, CultureInfo.InvariantCulture)]),
+ MiraNumberSuffixes.None => part,
+ MiraNumberSuffixes.Percent => part + "%",
+ MiraNumberSuffixes.Multiplier => part + "x",
+ MiraNumberSuffixes.Distance => part + MiraApiPlugin.DistanceSuffix.Value,
+ _ => TranslationController.Instance.GetString(StringNames.GameSecondsAbbrev, part),
};
}
diff --git a/MiraAPI/Utilities/StateMachineWrapper.cs b/MiraAPI/Utilities/StateMachineWrapper.cs
index 6b9d683d..cfe5d8a1 100644
--- a/MiraAPI/Utilities/StateMachineWrapper.cs
+++ b/MiraAPI/Utilities/StateMachineWrapper.cs
@@ -21,6 +21,7 @@ public class StateMachineWrapper
private readonly PropertyInfo _stateProperty;
private readonly PropertyInfo _currentProperty;
private readonly Dictionary _propertyCache;
+ private readonly Type _type;
private T? _parentInstance;
@@ -37,14 +38,14 @@ public class StateMachineWrapper
{
_stateMachine = stateMachine;
- var type = _stateMachine.GetType();
- _thisProperty = AccessTools.Property(type, "__4__this");
- _stateProperty = AccessTools.Property(type, "__1__state");
- _currentProperty = AccessTools.Property(type, "__2__current");
+ _type = _stateMachine.GetType();
+ _thisProperty = AccessTools.Property(_type, "__4__this");
+ _stateProperty = AccessTools.Property(_type, "__1__state");
+ _currentProperty = AccessTools.Property(_type, "__2__current");
if (_thisProperty == null || _stateProperty == null || _currentProperty == null)
{
- throw new MissingMemberException($"Could not find required properties in type '{type}'.");
+ throw new MissingMemberException($"Could not find required properties in type '{_type}'.");
}
_propertyCache = [];
@@ -91,8 +92,8 @@ public class StateMachineWrapper
return (TField)property.GetValue(_stateMachine)!;
}
- property = AccessTools.Property(_stateMachine.GetType(), parameterName)
- ?? throw new MissingFieldException($"Could not find parameter '{parameterName}' in state machine of type '{_stateMachine.GetType()}'.");
+ property = AccessTools.Property(_type, parameterName)
+ ?? throw new MissingFieldException($"Could not find parameter '{parameterName}' in state machine of type '{_type.Name}'.");
_propertyCache[parameterName] = property;
return (TField)property.GetValue(_stateMachine)!;
From f06562a28106a92d57d53b6590b97ff6e11ac0fe Mon Sep 17 00:00:00 2001
From: Az <56699741+AlchlcDvl@users.noreply.github.com>
Date: Thu, 5 Nov 2026 23:05:03 -0500
Subject: [PATCH 2/3] remove useless `.gameObject` uses
---
MiraAPI/Hud/CustomPaginableMenu.cs | 8 ++++----
.../ModifierDisplay/ModifierUiComponent.cs | 2 +-
.../Patches/Options/GameOptionsMenuPatch.cs | 4 ++--
.../Patches/Options/GamePresetsTabPatches.cs | 4 ++--
.../Patches/Options/GameSettingMenuPatches.cs | 5 +++--
.../Patches/Options/LobbyViewPanePatches.cs | 6 +++---
MiraAPI/Patches/Options/MenuState.cs | 20 +++++++++----------
.../Patches/Options/RoleSettingMenuPatches.cs | 2 +-
MiraAPI/Patches/RoleGuidePatches.cs | 13 +++---------
MiraAPI/Roles/CustomRoleManager.cs | 4 ++--
MiraAPI/Utilities/Extensions.cs | 15 ++++++++++++++
MiraAPI/Utilities/Helpers.cs | 2 +-
12 files changed, 47 insertions(+), 38 deletions(-)
diff --git a/MiraAPI/Hud/CustomPaginableMenu.cs b/MiraAPI/Hud/CustomPaginableMenu.cs
index aca20b53..c8ef1cef 100644
--- a/MiraAPI/Hud/CustomPaginableMenu.cs
+++ b/MiraAPI/Hud/CustomPaginableMenu.cs
@@ -72,18 +72,18 @@ public abstract class CustomPaginableMenu : CustomPhoneMenu().sprite = MiraAssets.NextButton.LoadAsset();
- nextButton.gameObject.GetComponent().DestroyImmediate();
+ nextButton.GetComponent().DestroyImmediate();
- var passiveButton = nextButton.gameObject.GetComponent();
+ var passiveButton = nextButton.GetComponent();
passiveButton.OnClick = new Button.ButtonClickedEvent();
passiveButton.OnClick.AddListener((UnityAction)customMenu.NextPage);
var backButton = Object.Instantiate(nextButton, customMenu.transform).gameObject;
backButton.transform.localPosition = new Vector3(-1.85f, -2.185f, customMenu.MenuDepth);
backButton.name = "LeftArrowButton";
- backButton.gameObject.GetComponent().Destroy();
+ backButton.GetComponent().Destroy();
backButton.GetComponent().flipX = true;
- var prevPassive = backButton.gameObject.GetComponent();
+ var prevPassive = backButton.GetComponent();
prevPassive.OnClick.AddListener((UnityAction)customMenu.PreviousPage);
customMenu.PhoneUI.GetChild(0).GetComponent().material =
diff --git a/MiraAPI/Modifiers/ModifierDisplay/ModifierUiComponent.cs b/MiraAPI/Modifiers/ModifierDisplay/ModifierUiComponent.cs
index 3c66358b..8638f75e 100644
--- a/MiraAPI/Modifiers/ModifierDisplay/ModifierUiComponent.cs
+++ b/MiraAPI/Modifiers/ModifierDisplay/ModifierUiComponent.cs
@@ -37,7 +37,7 @@ public class ModifierUiComponent(nint cppPtr) : MonoBehaviour(cppPtr)
icon = modBg.transform.GetChild(1).GetComponent();
var nameBg = transform.GetChild(1);
nameText = nameBg.GetChild(0).GetComponent();
- descRect = desc.gameObject.GetComponent();
+ descRect = desc.GetComponent();
nameText.font = desc.font = HudManager.Instance.TaskPanel.taskText.font;
nameText.fontMaterial = desc.fontMaterial = HudManager.Instance.TaskPanel.taskText.fontMaterial;
diff --git a/MiraAPI/Patches/Options/GameOptionsMenuPatch.cs b/MiraAPI/Patches/Options/GameOptionsMenuPatch.cs
index 0799bf2f..3d19f0f6 100644
--- a/MiraAPI/Patches/Options/GameOptionsMenuPatch.cs
+++ b/MiraAPI/Patches/Options/GameOptionsMenuPatch.cs
@@ -313,7 +313,7 @@ internal static class GameOptionsMenuPatch
};
_modIcon = modIconObj.AddComponent();
_modIcon.sprite = MiraAssets.BlankSprite.LoadAsset();
- _modIcon.material = gamemodeBg.gameObject.GetComponent().material;
+ _modIcon.material = gamemodeBg.GetComponent().material;
var gamemodeTextObj = Object.Instantiate(GameModeOption.OptionBehaviour.transform.GetChild(1).gameObject, modeInfoHolder);
gamemodeTextObj.transform.localPosition = new Vector3(0.3f, 0, -0.01f);
@@ -739,7 +739,7 @@ internal static class GameOptionsMenuPatch
var newText = Object.Instantiate(categoryHeaderMasked.Title, categoryHeaderMasked.transform);
newText.text = $"({"MiraApi.GameSetting.Global.ClickToClose".Translate()})";
newText.transform.localPosition = new Vector3(2.6249f, -0.165f, 0f);
- newText.gameObject.GetComponent().Destroy();
+ newText.GetComponent().Destroy();
var boxCol = categoryHeaderMasked.gameObject.AddComponent();
boxCol.size = new Vector2(7, 0.7f);
diff --git a/MiraAPI/Patches/Options/GamePresetsTabPatches.cs b/MiraAPI/Patches/Options/GamePresetsTabPatches.cs
index 4bf0cf83..a759cc1e 100644
--- a/MiraAPI/Patches/Options/GamePresetsTabPatches.cs
+++ b/MiraAPI/Patches/Options/GamePresetsTabPatches.cs
@@ -164,7 +164,7 @@ internal static class GamePresetsTabPatches
return;
}
- var tmpTranslator = presetTab.PresetDescriptionText.gameObject.GetComponent();
+ var tmpTranslator = presetTab.PresetDescriptionText.GetComponent();
if (tmpTranslator)
{
tmpTranslator.Destroy();
@@ -282,7 +282,7 @@ internal static class GamePresetsTabPatches
_folderButton.name = "OpenFolderButton";
_folderButton.transform.localPosition = new Vector3(3.55f, 1.1f, -2);
- var folderRend = openFolderButton.transform.FindChild("Sprite").gameObject.GetComponent();
+ var folderRend = openFolderButton.transform.FindChild("Sprite").GetComponent();
folderRend.sprite = MiraAssets.FolderIcon.LoadAsset();
folderRend.transform.localScale = new Vector3(0.4f, 0.4f, 1);
diff --git a/MiraAPI/Patches/Options/GameSettingMenuPatches.cs b/MiraAPI/Patches/Options/GameSettingMenuPatches.cs
index 6c1ede7a..47c45a06 100644
--- a/MiraAPI/Patches/Options/GameSettingMenuPatches.cs
+++ b/MiraAPI/Patches/Options/GameSettingMenuPatches.cs
@@ -20,9 +20,10 @@ internal static class GameSettingMenuPatches
}
// Add Menu State object
- if (!__instance.gameObject.GetComponent())
+ __instance.gameObject.GetOrAddComponent(out var existed);
+
+ if (!existed)
{
- __instance.gameObject.AddComponent();
Info("MenuState component added to GameSettingMenu.");
}
else
diff --git a/MiraAPI/Patches/Options/LobbyViewPanePatches.cs b/MiraAPI/Patches/Options/LobbyViewPanePatches.cs
index c77f93d1..79315f3a 100644
--- a/MiraAPI/Patches/Options/LobbyViewPanePatches.cs
+++ b/MiraAPI/Patches/Options/LobbyViewPanePatches.cs
@@ -72,7 +72,7 @@ public static class LobbyViewPanePatches
// create modifiers button
modifiersTabButton = Object.Instantiate(__instance.rolesTabButton, __instance.rolesTabButton.transform.parent);
- modifiersTabButton.buttonText.gameObject.GetComponent().Destroy();
+ modifiersTabButton.buttonText.GetComponent().Destroy();
modifiersTabButton.name = "ModifiersTabButton";
var pos = modifiersTabButton.transform.localPosition;
pos.x = 2.1f;
@@ -101,7 +101,7 @@ public static class LobbyViewPanePatches
hover.transform.localPosition = new Vector3(0, 0f, 0.3f);
hover.sprite = MiraAssets.NextButtonActive.LoadAsset();
- var passiveButton = nextButton.gameObject.GetComponent();
+ var passiveButton = nextButton.GetComponent();
passiveButton.OnClick = new Button.ButtonClickedEvent();
passiveButton.OnClick.AddListener(
(UnityAction)(() =>
@@ -123,7 +123,7 @@ public static class LobbyViewPanePatches
= backButton.transform.FindChild("Hover").gameObject.GetComponentInChildren().flipX
= true;
- var passiveButton2 = backButton.gameObject.GetComponent();
+ var passiveButton2 = backButton.GetComponent();
passiveButton2.OnClick = new Button.ButtonClickedEvent();
passiveButton2.OnClick.AddListener(
(UnityAction)(() =>
diff --git a/MiraAPI/Patches/Options/MenuState.cs b/MiraAPI/Patches/Options/MenuState.cs
index 66876991..ee3c75f9 100644
--- a/MiraAPI/Patches/Options/MenuState.cs
+++ b/MiraAPI/Patches/Options/MenuState.cs
@@ -121,24 +121,24 @@ public class MenuState(IntPtr cppPtr) : MonoBehaviour(cppPtr)
var nextButton = Instantiate(Gsm.BackButton, Gsm.BackButton.transform.parent).gameObject;
nextButton.transform.localPosition = new Vector3(-2.2663f, 1.5272f, -25f);
nextButton.name = "RightArrowButton";
- nextButton.transform.FindChild("Inactive").gameObject.GetComponent().sprite =
+ nextButton.transform.FindChild("Inactive").GetComponent().sprite =
MiraAssets.NextButton.LoadAsset();
- nextButton.transform.FindChild("Active").gameObject.GetComponent().sprite =
+ nextButton.transform.FindChild("Active").GetComponent().sprite =
MiraAssets.NextButtonActive.LoadAsset();
- nextButton.gameObject.GetComponent().DestroyImmediate();
+ nextButton.GetComponent().DestroyImmediate();
- var nextPassiveButton = nextButton.gameObject.GetComponent();
+ var nextPassiveButton = nextButton.GetComponent();
nextPassiveButton.OnClick = new Button.ButtonClickedEvent();
nextPassiveButton.OnClick.AddListener((UnityAction)NextMod);
var previousButton = Instantiate(nextButton, Gsm.BackButton.transform.parent).gameObject;
previousButton.transform.localPosition = new Vector3(-4.4209f, 1.5272f, -25f);
previousButton.name = "LeftArrowButton";
- previousButton.gameObject.GetComponent().Destroy();
- previousButton.transform.FindChild("Active").gameObject.GetComponent().flipX =
- previousButton.transform.FindChild("Inactive").gameObject.GetComponent().flipX = true;
+ previousButton.GetComponent().Destroy();
+ previousButton.transform.FindChild("Active").GetComponent().flipX =
+ previousButton.transform.FindChild("Inactive").GetComponent().flipX = true;
- var prevPassiveButton = previousButton.gameObject.GetComponent();
+ var prevPassiveButton = previousButton.GetComponent();
prevPassiveButton.OnClick = new Button.ButtonClickedEvent();
prevPassiveButton.OnClick.AddListener((UnityAction)PreviousMod);
@@ -149,7 +149,7 @@ public class MenuState(IntPtr cppPtr) : MonoBehaviour(cppPtr)
var mBtn = Instantiate(Gsm.RoleSettingsButton, Gsm.RoleSettingsButton.transform.parent);
_largeButtons[MenuButton.Modifiers] = mBtn;
mBtn.name = "ModifiersButton";
- mBtn.buttonText.gameObject.GetComponent().Destroy();
+ mBtn.buttonText.GetComponent().Destroy();
mBtn.buttonText.text = "Modifiers".Translate();
mBtn.OnClick = new Button.ButtonClickedEvent();
mBtn.OnClick.AddListener(
@@ -168,7 +168,7 @@ public class MenuState(IntPtr cppPtr) : MonoBehaviour(cppPtr)
var c1Btn = Instantiate(mBtn, mBtn.transform.parent);
_largeButtons[MenuButton.CustomOne] = c1Btn;
c1Btn.name = "CustomOneButton";
- c1Btn.buttonText.gameObject.GetComponent().Destroy();
+ c1Btn.buttonText.GetComponent().Destroy();
c1Btn.OnClick = new Button.ButtonClickedEvent();
c1Btn.OnClick.AddListener(
(UnityAction)(() =>
diff --git a/MiraAPI/Patches/Options/RoleSettingMenuPatches.cs b/MiraAPI/Patches/Options/RoleSettingMenuPatches.cs
index aa6396df..41e376d3 100644
--- a/MiraAPI/Patches/Options/RoleSettingMenuPatches.cs
+++ b/MiraAPI/Patches/Options/RoleSettingMenuPatches.cs
@@ -309,7 +309,7 @@ public static class RoleSettingMenuPatches
var newText = Object.Instantiate(categoryHeaderMasked.Title, categoryHeaderMasked.transform);
newText.text = $"{label}";
newText.transform.localPosition = new Vector3(2.6249f, -0.165f, 0f);
- newText.gameObject.GetComponent().Destroy();
+ newText.GetComponent().Destroy();
scrollerNum -= 0.422f;
diff --git a/MiraAPI/Patches/RoleGuidePatches.cs b/MiraAPI/Patches/RoleGuidePatches.cs
index 198e2774..239e3115 100644
--- a/MiraAPI/Patches/RoleGuidePatches.cs
+++ b/MiraAPI/Patches/RoleGuidePatches.cs
@@ -1575,20 +1575,13 @@ public static class RoleGuidePatches
private static void SetupPanelButton(MatchInfoRolePanel panel, Action onClick)
{
- var collider = panel.roleIcon.gameObject.GetComponent();
- if (!collider)
- {
- collider = panel.roleIcon.gameObject.AddComponent();
- }
+ var collider = panel.roleIcon.gameObject.GetOrAddComponent();
collider.size = new Vector2(0.13f, 0.13f);
collider.offset = new Vector2(0, 0);
collider.enabled = true;
- var passiveButton = panel.roleIcon.gameObject.GetComponent();
- if (!passiveButton)
- {
- passiveButton = panel.roleIcon.gameObject.AddComponent();
- }
+
+ var passiveButton = panel.roleIcon.gameObject.GetOrAddComponent();
passiveButton.enabled = true;
passiveButton.ClickSound = HudManager.Instance.MapButton.ClickSound;
diff --git a/MiraAPI/Roles/CustomRoleManager.cs b/MiraAPI/Roles/CustomRoleManager.cs
index d89e40a9..3b48087c 100644
--- a/MiraAPI/Roles/CustomRoleManager.cs
+++ b/MiraAPI/Roles/CustomRoleManager.cs
@@ -239,7 +239,7 @@ public static class CustomRoleManager
internal static TaskPanelBehaviour CreateRoleTab(ICustomRole role)
{
- var ogPanel = HudManager.Instance.TaskStuff.transform.FindChild("TaskPanel").gameObject.GetComponent();
+ var ogPanel = HudManager.Instance.TaskStuff.transform.FindChild("TaskPanel").GetComponent();
var clonePanel = Object.Instantiate(ogPanel.gameObject, ogPanel.transform.parent);
clonePanel.name = "RolePanel";
@@ -258,7 +258,7 @@ public static class CustomRoleManager
internal static void UpdateRoleTab(TaskPanelBehaviour panel, ICustomRole role)
{
var tabText = panel.tab.gameObject.GetComponentInChildren();
- var ogPanel = HudManager.Instance.TaskStuff.transform.FindChild("TaskPanel").gameObject.GetComponent();
+ var ogPanel = HudManager.Instance.TaskStuff.transform.FindChild("TaskPanel").GetComponent();
if (tabText.text != role.RoleName)
{
tabText.text = role.RoleName;
diff --git a/MiraAPI/Utilities/Extensions.cs b/MiraAPI/Utilities/Extensions.cs
index 0446e84f..d3a0fbae 100644
--- a/MiraAPI/Utilities/Extensions.cs
+++ b/MiraAPI/Utilities/Extensions.cs
@@ -843,6 +843,21 @@ public static class Extensions
return comp ? comp : go.AddComponent();
}
+ ///
+ /// Gets a component that is attached to the provided , attaching and returning the component if it wasn't found, and returns a bool that indicates if the component already existed before.
+ ///
+ /// The component type.
+ /// The to get the component from.
+ /// When this method returns true if the component already existed; otherwise false.
+ /// The component.
+ public static T GetOrAddComponent(this GameObject go, out bool exists)
+ where T : Component
+ {
+ var comp = go.GetComponent();
+ exists = comp;
+ return exists ? comp : go.AddComponent();
+ }
+
///
/// The type of the attribute.
/// true if a custom attribute of type is applied to element; otherwise, false.
diff --git a/MiraAPI/Utilities/Helpers.cs b/MiraAPI/Utilities/Helpers.cs
index 638c41a4..f5d7d25d 100644
--- a/MiraAPI/Utilities/Helpers.cs
+++ b/MiraAPI/Utilities/Helpers.cs
@@ -304,7 +304,7 @@ public static class Helpers
{
var prefab = Object.FindObjectOfType(true);
var arrow = Object.Instantiate(prefab, parent);
- arrow.image = arrow.gameObject.GetComponent();
+ arrow.image = arrow.GetComponent();
arrow.image.color = color;
arrow.gameObject.layer = 5;
arrow.gameObject.SetActive(true);
From ca285d4af665eb3d75454fa8a3a9345784fe5735 Mon Sep 17 00:00:00 2001
From: Az <56699741+AlchlcDvl@users.noreply.github.com>
Date: Thu, 5 Nov 2026 23:48:28 -0500
Subject: [PATCH 3/3] removing even more useless calls
---
MiraAPI/Hud/CustomPhoneMenu.cs | 4 ++--
MiraAPI/MeetingAbilities/TargetedMeetingButton.cs | 2 +-
MiraAPI/MiraApiSettings.cs | 12 ++++++------
MiraAPI/Patches/Freeplay/TaskAdderPatches.cs | 3 +--
MiraAPI/Patches/GameModes/HudPatches.cs | 4 ++--
MiraAPI/Patches/HudManagerPatches.cs | 10 +++++-----
MiraAPI/Patches/Options/GamePresetsTabPatches.cs | 2 +-
MiraAPI/Patches/Options/MenuState.cs | 4 ++--
MiraAPI/Patches/Options/RoleSettingMenuPatches.cs | 4 ++--
MiraAPI/Patches/PlayerControlPatches.cs | 4 ++--
MiraAPI/Patches/Roles/RoleManagerPatches.cs | 2 +-
MiraAPI/Patches/Roles/TaskPanelPatch.cs | 2 +-
MiraAPI/Presets/SavePresetPopup.cs | 2 +-
MiraAPI/Roles/CustomRoleManager.cs | 2 +-
14 files changed, 28 insertions(+), 29 deletions(-)
diff --git a/MiraAPI/Hud/CustomPhoneMenu.cs b/MiraAPI/Hud/CustomPhoneMenu.cs
index 1f636f73..55af740f 100644
--- a/MiraAPI/Hud/CustomPhoneMenu.cs
+++ b/MiraAPI/Hud/CustomPhoneMenu.cs
@@ -260,7 +260,7 @@ public abstract class CustomPhoneMenu : ICustomMenu
MenuEntries.Add(menuEntry);
var button = shapeshifterPanel.Button;
- var nameplate = shapeshifterPanel.gameObject.transform.FindChild("Nameplate");
+ var nameplate = shapeshifterPanel.transform.FindChild("Nameplate");
var highlight = nameplate.FindChild("Highlight").GetComponent();
var icon = highlight.transform.GetChild(0).GetComponent();
@@ -317,7 +317,7 @@ public abstract class CustomPhoneMenu : ICustomMenu
{
ShapeshifterPanel panel = menuEntry.Panel;
- var nameplate = panel.gameObject.transform.FindChild("Nameplate");
+ var nameplate = panel.transform.FindChild("Nameplate");
var highlight = nameplate.FindChild("Highlight").GetComponent();
var icon = highlight.transform.GetChild(0).GetComponent();
if (sprite != null)
diff --git a/MiraAPI/MeetingAbilities/TargetedMeetingButton.cs b/MiraAPI/MeetingAbilities/TargetedMeetingButton.cs
index 2d4766e4..aeeeedd8 100644
--- a/MiraAPI/MeetingAbilities/TargetedMeetingButton.cs
+++ b/MiraAPI/MeetingAbilities/TargetedMeetingButton.cs
@@ -89,7 +89,7 @@ public abstract class TargetedMeetingButton
if (MeetingHud.Instance == null) return null!;
var btn = Object.Instantiate(MeetingHud.Instance.PlayerButtonPrefab.ConfirmButton).GetComponent();
- btn.gameObject.name = Name + "Button";
+ btn.name = Name + "Button";
var spriteRenderer = btn.GetComponent();
spriteRenderer.sprite = Sprite.LoadAsset();
diff --git a/MiraAPI/MiraApiSettings.cs b/MiraAPI/MiraApiSettings.cs
index 2240d0c1..ce722351 100644
--- a/MiraAPI/MiraApiSettings.cs
+++ b/MiraAPI/MiraApiSettings.cs
@@ -144,15 +144,15 @@ public class MiraApiSettings(ConfigFile config) : LocalSettingsTab(config)
if (button.transform.name.Contains("Friends List Button"))
{
- button.gameObject.transform.localScale = new Vector3(0.2675f * actualScaleVal, 0.2675f * actualScaleVal, 1);
+ button.transform.localScale = new Vector3(0.2675f * actualScaleVal, 0.2675f * actualScaleVal, 1);
continue;
}
- button.gameObject.transform.localScale = actualScale;
+ button.transform.localScale = actualScale;
}
baseGrid.CellSize = new Vector2(alteredScale, alteredScale);
- if (baseGrid.gameObject.transform.childCount != 0)
+ if (baseGrid.transform.childCount != 0)
{
baseGrid.ArrangeChilds();
}
@@ -175,15 +175,15 @@ public class MiraApiSettings(ConfigFile config) : LocalSettingsTab(config)
if (button.transform.name.Contains("Modifiers"))
{
- button.gameObject.transform.localScale = new Vector3(0.65f * scaleFactor, 0.65f * scaleFactor, 1);
+ button.transform.localScale = new Vector3(0.65f * scaleFactor, 0.65f * scaleFactor, 1);
continue;
}
- button.gameObject.transform.localScale = actualScale;
+ button.transform.localScale = actualScale;
}
extraGrid.CellSize = new Vector2(alteredScale, alteredScale);
- if (extraGrid.gameObject.transform.childCount != 0)
+ if (extraGrid.transform.childCount != 0)
{
extraGrid.ArrangeChilds();
}
diff --git a/MiraAPI/Patches/Freeplay/TaskAdderPatches.cs b/MiraAPI/Patches/Freeplay/TaskAdderPatches.cs
index 6a97687f..a4ba9cbd 100644
--- a/MiraAPI/Patches/Freeplay/TaskAdderPatches.cs
+++ b/MiraAPI/Patches/Freeplay/TaskAdderPatches.cs
@@ -111,8 +111,7 @@ public static class TaskAdderPatches
if (plugin.InternalModifiers.Exists(x => x.ShowInFreeplay))
{
- __instance.CreateFolder(plugin.PluginInfo.Metadata.Name, modifiersFolder)
- .gameObject.name = plugin.PluginId;
+ __instance.CreateFolder(plugin.PluginInfo.Metadata.Name, modifiersFolder).name = plugin.PluginId;
}
}
diff --git a/MiraAPI/Patches/GameModes/HudPatches.cs b/MiraAPI/Patches/GameModes/HudPatches.cs
index 3c2d815d..325d193d 100644
--- a/MiraAPI/Patches/GameModes/HudPatches.cs
+++ b/MiraAPI/Patches/GameModes/HudPatches.cs
@@ -34,8 +34,8 @@ internal static class HudPatches
yield return new WaitUntil((Il2CppSystem.Func)(() => GameManager.Instance != null));
if (GameManager.Instance.IsHideAndSeek())
yield return null;
- var infoPane = instance.gameObject.transform.FindChild("LobbyInfoPane");
- var aspect = infoPane.gameObject.transform.Find("AspectSize");
+ var infoPane = instance.transform.FindChild("LobbyInfoPane");
+ var aspect = infoPane.transform.Find("AspectSize");
var modeLabel = aspect!.Find("ModeLabel");
var modeValue = aspect.Find("ModeValue");
var modelText = modeLabel!.Find("Text_TMP").gameObject;
diff --git a/MiraAPI/Patches/HudManagerPatches.cs b/MiraAPI/Patches/HudManagerPatches.cs
index 75b0e5e7..a6959ead 100644
--- a/MiraAPI/Patches/HudManagerPatches.cs
+++ b/MiraAPI/Patches/HudManagerPatches.cs
@@ -47,9 +47,9 @@ public static class HudManagerPatches
{
if (!aspect.gameObject) continue;
- if (aspect.gameObject.transform.parent.name == "TopRight") continue;
+ if (aspect.transform.parent.name == "TopRight") continue;
- if (aspect.gameObject.name.Contains("TopRight")) continue;
+ if (aspect.name.Contains("TopRight")) continue;
aspect.gameObject.SetActive(!aspect.isActiveAndEnabled);
aspect.DistanceFromEdge *= new Vector2(scaleFactor, scaleFactor);
@@ -62,7 +62,7 @@ public static class HudManagerPatches
if (!button.gameObject) continue;
button.gameObject.SetActive(!button.isActiveAndEnabled);
- button.gameObject.transform.localScale *= scaleFactor;
+ button.transform.localScale *= scaleFactor;
button.gameObject.SetActive(!button.isActiveAndEnabled);
}
@@ -71,12 +71,12 @@ public static class HudManagerPatches
{
if (!arrange.gameObject || !arrange.transform) continue;
- if (arrange.gameObject.name.Contains("TopRight")) continue;
+ if (arrange.name.Contains("TopRight")) continue;
arrange.gameObject.SetActive(!arrange.isActiveAndEnabled);
arrange.CellSize = new Vector2(scaleFactor, scaleFactor);
arrange.gameObject.SetActive(!arrange.isActiveAndEnabled);
- if (!arrange.isActiveAndEnabled || arrange.gameObject.transform.childCount == 0) continue;
+ if (!arrange.isActiveAndEnabled || arrange.transform.childCount == 0) continue;
try
{
arrange.ArrangeChilds();
diff --git a/MiraAPI/Patches/Options/GamePresetsTabPatches.cs b/MiraAPI/Patches/Options/GamePresetsTabPatches.cs
index a759cc1e..fe6ac117 100644
--- a/MiraAPI/Patches/Options/GamePresetsTabPatches.cs
+++ b/MiraAPI/Patches/Options/GamePresetsTabPatches.cs
@@ -184,7 +184,7 @@ internal static class GamePresetsTabPatches
{
saveButton = Object.Instantiate(prefab, presetTab.transform);
_saveButton = saveButton.gameObject;
- _saveButton.gameObject.name = "SaveButton";
+ _saveButton.name = "SaveButton";
_saveButton.transform.localPosition = new Vector3(3.2f, 1.7f, -2);
// set the button text and alignment
diff --git a/MiraAPI/Patches/Options/MenuState.cs b/MiraAPI/Patches/Options/MenuState.cs
index ee3c75f9..cd5e6c87 100644
--- a/MiraAPI/Patches/Options/MenuState.cs
+++ b/MiraAPI/Patches/Options/MenuState.cs
@@ -322,7 +322,7 @@ public class MenuState(IntPtr cppPtr) : MonoBehaviour(cppPtr)
// Create game options container
var gameContainer = Containers[MenuCategory.Game][idx + 1] = Instantiate(
Gsm.GameSettingsTab.settingsContainer.gameObject,
- Gsm.GameSettingsTab.settingsContainer.gameObject.transform.parent);
+ Gsm.GameSettingsTab.settingsContainer.transform.parent);
gameContainer.name = $"GameSettings_{mod.PluginInfo.Metadata.Name}";
gameContainer.transform.DestroyChildren();
gameContainer.SetActive(false);
@@ -340,7 +340,7 @@ public class MenuState(IntPtr cppPtr) : MonoBehaviour(cppPtr)
{
var cont = Containers[i][idx + 1] = Instantiate(
Gsm.GameSettingsTab.settingsContainer.gameObject,
- Gsm.GameSettingsTab.settingsContainer.gameObject.transform.parent);
+ Gsm.GameSettingsTab.settingsContainer.transform.parent);
cont.name = $"{i}_{mod.PluginInfo.Metadata.Name}";
cont.transform.DestroyChildren();
cont.SetActive(false);
diff --git a/MiraAPI/Patches/Options/RoleSettingMenuPatches.cs b/MiraAPI/Patches/Options/RoleSettingMenuPatches.cs
index 41e376d3..b6372387 100644
--- a/MiraAPI/Patches/Options/RoleSettingMenuPatches.cs
+++ b/MiraAPI/Patches/Options/RoleSettingMenuPatches.cs
@@ -604,14 +604,14 @@ public static class RoleSettingMenuPatches
var categoryHeaderMasked = __instance.AdvancedRolesSettings.transform.Find("CategoryHeaderMasked").GetComponent();
- if (categoryHeaderMasked.Title.gameObject.TryGetComponent(out var comp))
+ if (categoryHeaderMasked.Title.TryGetComponent(out var comp))
{
comp.Destroy();
}
categoryHeaderMasked.Title.text = "MiraApi.ReturnToRoleSettings".Translate();
- if (!categoryHeaderMasked.gameObject.TryGetComponent(out _))
+ if (!categoryHeaderMasked.TryGetComponent(out _))
{
var boxCol = categoryHeaderMasked.gameObject.AddComponent();
boxCol.size = new Vector2(7, 0.7f);
diff --git a/MiraAPI/Patches/PlayerControlPatches.cs b/MiraAPI/Patches/PlayerControlPatches.cs
index 791334d8..89c78e36 100644
--- a/MiraAPI/Patches/PlayerControlPatches.cs
+++ b/MiraAPI/Patches/PlayerControlPatches.cs
@@ -22,12 +22,12 @@ internal static class PlayerControlPatches
[HarmonyPatch(nameof(PlayerControl.Start))]
public static void PlayerControlStartPostfix(PlayerControl __instance)
{
- if (__instance.gameObject.TryGetComponent(out var modifierComp))
+ if (__instance.TryGetComponent(out var modifierComp))
{
modifierComp.DestroyImmediate();
}
- if (__instance.gameObject.TryGetComponent(out var voteComp))
+ if (__instance.TryGetComponent(out var voteComp))
{
voteComp.DestroyImmediate();
}
diff --git a/MiraAPI/Patches/Roles/RoleManagerPatches.cs b/MiraAPI/Patches/Roles/RoleManagerPatches.cs
index 82e937fc..82cf9911 100644
--- a/MiraAPI/Patches/Roles/RoleManagerPatches.cs
+++ b/MiraAPI/Patches/Roles/RoleManagerPatches.cs
@@ -37,7 +37,7 @@ public static class RoleManagerPatches
Object.Destroy(data.Role.gameObject);
}
- var roleBehaviour = Object.Instantiate(__instance.AllRoles.ToArray().First(r => r.Role == roleType), data.gameObject.transform);
+ var roleBehaviour = Object.Instantiate(__instance.AllRoles.ToArray().First(r => r.Role == roleType), data.transform);
roleBehaviour.Initialize(targetPlayer);
targetPlayer.Data.Role = roleBehaviour;
targetPlayer.Data.RoleType = roleType;
diff --git a/MiraAPI/Patches/Roles/TaskPanelPatch.cs b/MiraAPI/Patches/Roles/TaskPanelPatch.cs
index c858d9f0..e97646da 100644
--- a/MiraAPI/Patches/Roles/TaskPanelPatch.cs
+++ b/MiraAPI/Patches/Roles/TaskPanelPatch.cs
@@ -15,7 +15,7 @@ internal static class TaskPanelPatch
[HarmonyPatch(nameof(TaskPanelBehaviour.Update))]
public static bool UpdatePrefix(TaskPanelBehaviour __instance)
{
- if (__instance.gameObject.name != "RolePanel")
+ if (__instance.name != "RolePanel")
{
return true;
}
diff --git a/MiraAPI/Presets/SavePresetPopup.cs b/MiraAPI/Presets/SavePresetPopup.cs
index 9405e83b..d2db31ba 100644
--- a/MiraAPI/Presets/SavePresetPopup.cs
+++ b/MiraAPI/Presets/SavePresetPopup.cs
@@ -70,7 +70,7 @@ public class SavePresetPopup(nint cppPtr) : Minigame(cppPtr)
foreach (var tmp in gameObject.GetComponentsInChildren())
{
- if (tmp.gameObject.transform.parent.name == "Textbox")
+ if (tmp.transform.parent.name == "Textbox")
{
continue;
}
diff --git a/MiraAPI/Roles/CustomRoleManager.cs b/MiraAPI/Roles/CustomRoleManager.cs
index 3b48087c..d9325e6a 100644
--- a/MiraAPI/Roles/CustomRoleManager.cs
+++ b/MiraAPI/Roles/CustomRoleManager.cs
@@ -257,7 +257,7 @@ public static class CustomRoleManager
internal static void UpdateRoleTab(TaskPanelBehaviour panel, ICustomRole role)
{
- var tabText = panel.tab.gameObject.GetComponentInChildren();
+ var tabText = panel.tab.GetComponentInChildren();
var ogPanel = HudManager.Instance.TaskStuff.transform.FindChild("TaskPanel").GetComponent();
if (tabText.text != role.RoleName)
{