experimental settings sync fix + improvements

This commit is contained in:
Az committed 2026-11-05 22:41:55 -05:00
1 parent 01925d94e4
commit 9874713ec8
5 files changed
+48 -36

No files matched your search

+21 -6
View File
@@ -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;
/// <summary>
/// Sync all settings to the player when they join the game.
/// </summary>
[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<AmongUsClient>(nameof(AmongUsClient.CreatePlayer))!;
}
[HarmonyPostfix]
public static void CreatePlayerPatch(Il2CppObjectBase __instance, bool __result)
{
if (__result)
return;
var wrapper = new StateMachineWrapper<AmongUsClient>(__instance);
var clientData = wrapper.GetParameter<ClientData>("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);
+2 -2
View File
@@ -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),
};
}
+1 -1
View File
@@ -296,7 +296,7 @@ public static class RoleGuidePatches
if (_titleText)
{
_titleText.transform.localPosition += new Vector3(0.5f, 0);
_titleText.transform.GetComponent<TextTranslatorTMP>().Destroy();
_titleText.GetComponent<TextTranslatorTMP>().Destroy();
}
}
+16 -20
View File
@@ -177,7 +177,7 @@ public static class Helpers
{
var keybindIcon = Object.Instantiate(HudManager.Instance.AbilityButton.usesRemainingSprite.gameObject, button.transform);
keybindIcon.GetComponent<SpriteRenderer>().sprite = MiraAssets.KeybindButton.LoadAsset();
keybindIcon.transform.GetComponentInChildren<TextMeshPro>().text = keyCode.ToString();
keybindIcon.GetComponentInChildren<TextMeshPro>().text = keyCode.ToString();
keybindIcon.name = "KeybindIcon";
keybindIcon.transform.localPosition = localPos;
return keybindIcon;
@@ -191,26 +191,21 @@ public static class Helpers
/// <returns>THe corresponding method to patch.</returns>
public static MethodBase? GetStateMachineMoveNext<T>(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
/// <returns>The formatted value.</returns>
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),
};
}
+8 -7
View File
@@ -21,6 +21,7 @@ public class StateMachineWrapper<T>
private readonly PropertyInfo _stateProperty;
private readonly PropertyInfo _currentProperty;
private readonly Dictionary<string, PropertyInfo> _propertyCache;
private readonly Type _type;
private T? _parentInstance;
@@ -37,14 +38,14 @@ public class StateMachineWrapper<T>
{
_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<T>
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)!;