fix comments and warnings

This commit is contained in:
XtraCube authored and miniduikboot committed 2026-06-10 00:24:01 +02:00
1 parent c3775ff33f
commit 16c8c3e553
6 files changed
+45 -29

No files matched your search

+6 -6
View File
@@ -38,12 +38,12 @@ internal static class ClientPatches
{ {
public static MethodBase TargetMethod() public static MethodBase TargetMethod()
{ {
return StateMachineWrapper<InnerNetClient>.GetStateMachineMoveNext(nameof(InnerNetClient.HandleGameDataInner))!; return Il2CppStateMachineWrapper<InnerNetClient>.GetStateMachineMoveNext(nameof(InnerNetClient.HandleGameDataInner))!;
} }
public static bool Prefix(Il2CppObjectBase __instance, ref bool __result) public static bool Prefix(Il2CppObjectBase __instance, ref bool __result)
{ {
var wrapper = new StateMachineWrapper<InnerNetClient>(__instance); var wrapper = new Il2CppStateMachineWrapper<InnerNetClient>(__instance);
var innerNetClient = wrapper.Instance; var innerNetClient = wrapper.Instance;
var reader = wrapper.GetParameter<MessageReader>("reader"); var reader = wrapper.GetParameter<MessageReader>("reader");
@@ -177,12 +177,12 @@ internal static class ClientPatches
{ {
public static MethodBase TargetMethod() public static MethodBase TargetMethod()
{ {
return StateMachineWrapper<InnerNetClient>.GetStateMachineMoveNext(nameof(InnerNetClient.CoSendSceneChange))!; return Il2CppStateMachineWrapper<InnerNetClient>.GetStateMachineMoveNext(nameof(InnerNetClient.CoSendSceneChange))!;
} }
public static bool Prefix(Il2CppObjectBase __instance, ref bool __result) public static bool Prefix(Il2CppObjectBase __instance, ref bool __result)
{ {
var wrapper = new StateMachineWrapper<InnerNetClient>(__instance); var wrapper = new Il2CppStateMachineWrapper<InnerNetClient>(__instance);
if (ReactorConnection.Instance!.Syncer != Syncer.Host) return true; if (ReactorConnection.Instance!.Syncer != Syncer.Host) return true;
@@ -233,14 +233,14 @@ internal static class ClientPatches
{ {
public static MethodBase TargetMethod() public static MethodBase TargetMethod()
{ {
return StateMachineWrapper<InnerNetClient>.GetStateMachineMoveNext(nameof(InnerNetClient.CoHandleSpawn))!; return Il2CppStateMachineWrapper<InnerNetClient>.GetStateMachineMoveNext(nameof(InnerNetClient.CoHandleSpawn))!;
} }
public static void Postfix(Il2CppObjectBase __instance, bool __result) public static void Postfix(Il2CppObjectBase __instance, bool __result)
{ {
if (ReactorConnection.Instance!.Syncer != Syncer.Host) return; if (ReactorConnection.Instance!.Syncer != Syncer.Host) return;
var wrapper = new StateMachineWrapper<InnerNetClient>(__instance); var wrapper = new Il2CppStateMachineWrapper<InnerNetClient>(__instance);
var ownerId = wrapper.GetParameter<int>("_ownerId_5__2"); var ownerId = wrapper.GetParameter<int>("_ownerId_5__2");
if (!__result && !AmongUsClient.Instance.AmHost && ownerId == AmongUsClient.Instance.ClientId) if (!__result && !AmongUsClient.Instance.AmHost && ownerId == AmongUsClient.Instance.ClientId)
@@ -28,7 +28,7 @@ public class ReactorConnection
{ {
public static MethodBase TargetMethod() public static MethodBase TargetMethod()
{ {
return StateMachineWrapper<InnerNetClient>.GetStateMachineMoveNext(nameof(InnerNetClient.CoConnect))!; return Il2CppStateMachineWrapper<InnerNetClient>.GetStateMachineMoveNext(nameof(InnerNetClient.CoConnect))!;
} }
public static void Prefix() public static void Prefix()
+1 -1
View File
@@ -12,7 +12,7 @@ internal static class CoFindGamePatch
{ {
public static MethodBase TargetMethod() public static MethodBase TargetMethod()
{ {
return StateMachineWrapper<AmongUsClient>.GetStateMachineMoveNext(nameof(AmongUsClient.CoFindGame))!; return Il2CppStateMachineWrapper<AmongUsClient>.GetStateMachineMoveNext(nameof(AmongUsClient.CoFindGame))!;
} }
public static void Prefix() public static void Prefix()
@@ -24,8 +24,8 @@ internal static class CustomServersPatch
{ {
public static IEnumerable<MethodBase> TargetMethods() => public static IEnumerable<MethodBase> TargetMethods() =>
[ [
StateMachineWrapper<AuthManager>.GetStateMachineMoveNext(nameof(AuthManager.CoConnect))!, Il2CppStateMachineWrapper<AuthManager>.GetStateMachineMoveNext(nameof(AuthManager.CoConnect))!,
StateMachineWrapper<AuthManager>.GetStateMachineMoveNext(nameof(AuthManager.CoWaitForNonce))! Il2CppStateMachineWrapper<AuthManager>.GetStateMachineMoveNext(nameof(AuthManager.CoWaitForNonce))!
]; ];
public static bool Prefix(ref bool __result) public static bool Prefix(ref bool __result)
@@ -45,28 +45,28 @@ internal static class CustomServersPatch
{ {
public static MethodBase TargetMethod() public static MethodBase TargetMethod()
{ {
return StateMachineWrapper<AmongUsClient>.GetStateMachineMoveNext(nameof(AmongUsClient.CoJoinOnlinePublicGame))!; return Il2CppStateMachineWrapper<AmongUsClient>.GetStateMachineMoveNext(nameof(AmongUsClient.CoJoinOnlinePublicGame))!;
} }
public static void Prefix(Il2CppObjectBase __instance) public static void Prefix(Il2CppObjectBase __instance)
{ {
var stateMachine = new StateMachineWrapper<AmongUsClient>(__instance); var stateMachine = new Il2CppStateMachineWrapper<AmongUsClient>(__instance);
// Skip to state 1 which just calls CoJoinOnlineGameDirect // Skip to state 1 which just calls CoJoinOnlineGameDirect
if (stateMachine.State == 0 && !ServerManager.Instance.IsHttp) if (stateMachine.State == 0 && !ServerManager.Instance.IsHttp)
{ {
stateMachine.State = 1; stateMachine.State = 1;
var lambdaType = stateMachine.GetParameter<Il2CppObjectBase>("__8__1").GetType(); var lambdaType = stateMachine.GetParameter<Il2CppObjectBase>("__8__1").GetType();
var newDisplayClass = Activator.CreateInstance(lambdaType); var newDisplayClassObject = Activator.CreateInstance(lambdaType);
if (newDisplayClass == null) if (newDisplayClassObject == null)
{ {
throw new InvalidOperationException($"Could not create display class of type '{lambdaType}'."); throw new InvalidOperationException($"Could not create display class of type '{lambdaType}'.");
} }
var displayClass = new CompilerGeneratedObjectWrapper(newDisplayClass); var wrappedDisplayClassObject = new Il2CppCompilerGeneratedObjectWrapper(newDisplayClassObject);
displayClass.SetField("matchmakerToken", string.Empty); wrappedDisplayClassObject.SetField("matchmakerToken", string.Empty);
stateMachine.SetParameter("__8__1", newDisplayClass); stateMachine.SetParameter("__8__1", newDisplayClassObject);
} }
} }
} }
@@ -6,9 +6,14 @@ using HarmonyLib;
namespace Reactor.Utilities; namespace Reactor.Utilities;
/// <summary> /// <summary>
/// A safe wrapper around compiler generated classes like DisplayClass or IEnumerator state machines. /// A safe wrapper around IL2CPP compiler generated classes like DisplayClass or IEnumerator state machines.
/// This is useful for gracefully handling changes across different game versions and updates, which may affect
/// compiler generated names.
/// To use this class, pass the instance object of the compiler generated class into the constructor.
/// Then you can use the GetField and SetField methods to access the fields of the compiler generated class
/// by their original names.
/// </summary> /// </summary>
public class CompilerGeneratedObjectWrapper public class Il2CppCompilerGeneratedObjectWrapper
{ {
/// <summary> /// <summary>
/// Gets a reference to the compiler generated object. /// Gets a reference to the compiler generated object.
@@ -36,10 +41,10 @@ public class CompilerGeneratedObjectWrapper
protected Dictionary<string, Delegate> SetterCache { get; } protected Dictionary<string, Delegate> SetterCache { get; }
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="CompilerGeneratedObjectWrapper"/> class. /// Initializes a new instance of the <see cref="Il2CppCompilerGeneratedObjectWrapper"/> class.
/// </summary> /// </summary>
/// <param name="generatedObject">An instance of the compiler generated object.</param> /// <param name="generatedObject">An instance of the compiler generated object.</param>
public CompilerGeneratedObjectWrapper(object generatedObject) public Il2CppCompilerGeneratedObjectWrapper(object generatedObject)
{ {
GeneratedObject = generatedObject; GeneratedObject = generatedObject;
GeneratedType = generatedObject.GetType(); GeneratedType = generatedObject.GetType();
@@ -49,6 +54,14 @@ public class CompilerGeneratedObjectWrapper
SetterCache = []; SetterCache = [];
} }
/// <summary>
/// Caches the property info, getter, and setter for a given field name and type.
/// </summary>
/// <param name="fieldName">The name of the field to cache.</param>
/// <typeparam name="T">The expected type of the field.</typeparam>
/// <returns>The cached <see cref="PropertyInfo"/> for the specified field.</returns>
/// <exception cref="MissingMemberException">Thrown if the field does not exist in the compiler generated type.</exception>
/// <exception cref="InvalidCastException">Thrown if the field exists but is not of the expected type.</exception>
public PropertyInfo CacheProperty<T>(string fieldName) public PropertyInfo CacheProperty<T>(string fieldName)
{ {
var propertyInfo = AccessTools.Property(GeneratedType, fieldName) var propertyInfo = AccessTools.Property(GeneratedType, fieldName)
@@ -117,4 +130,3 @@ public class CompilerGeneratedObjectWrapper
propertyInfo.SetValue(GeneratedObject, value); propertyInfo.SetValue(GeneratedObject, value);
} }
} }
@@ -6,10 +6,13 @@ using HarmonyLib;
namespace Reactor.Utilities; namespace Reactor.Utilities;
/// <summary> /// <summary>
/// A wrapper for state machine objects to access their parent instance and state. /// A wrapper for IL2CPP state machine objects to access their parent instance and state.
/// This class is useful for working with IL2CPP state machines across different game versions.
/// To use it, pass the instance object of the state machine into the constructor. To access the parent class,
/// use the Instance property. To access the current state of the state machine, use the State property.
/// </summary> /// </summary>
/// <typeparam name="T">The type of the parent class that owns the state machine.</typeparam> /// <typeparam name="T">The type of the parent class that owns the state machine.</typeparam>
public class StateMachineWrapper<T> : CompilerGeneratedObjectWrapper public class Il2CppStateMachineWrapper<T> : Il2CppCompilerGeneratedObjectWrapper
{ {
// normally it is fields, but IL2CPP turns them into properties // normally it is fields, but IL2CPP turns them into properties
private readonly PropertyInfo _thisProperty; private readonly PropertyInfo _thisProperty;
@@ -33,11 +36,13 @@ public class StateMachineWrapper<T> : CompilerGeneratedObjectWrapper
} }
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="StateMachineWrapper{T}"/> class. /// Initializes a new instance of the <see cref="Il2CppStateMachineWrapper{T}"/> class.
/// </summary> /// </summary>
/// <param name="stateMachine">The state machine instance to wrap.</param> /// <param name="stateMachine">The instance object of the state machine being wrapped.</param>
public StateMachineWrapper(object stateMachine) : base(stateMachine) public Il2CppStateMachineWrapper(object stateMachine) : base(stateMachine)
{ {
// The names of these properties are implementation details of the IL compiler used by Unity.
// They should be stable as long as Unity sticks to mono.
_thisProperty = AccessTools.Property(GeneratedType, "__4__this"); _thisProperty = AccessTools.Property(GeneratedType, "__4__this");
_stateProperty = AccessTools.Property(GeneratedType, "__1__state"); _stateProperty = AccessTools.Property(GeneratedType, "__1__state");
@@ -72,10 +77,9 @@ public class StateMachineWrapper<T> : CompilerGeneratedObjectWrapper
} }
/// <summary> /// <summary>
/// Attempts to retrieve the MoveNext method of a state machine for the specified method name in type T. /// Attempts to retrieve the MoveNext method of a state machine for the specified method name.
/// </summary> /// </summary>
/// <param name="methodName">The name of the method whose state machine MoveNext method is to be retrieved.</param> /// <param name="methodName">The name of the method whose state machine MoveNext method is to be retrieved.</param>
/// <typeparam name="T">The type containing the state machine.</typeparam>
/// <returns>The MoveNext <see cref="MethodBase"/> if found; otherwise, null.</returns> /// <returns>The MoveNext <see cref="MethodBase"/> if found; otherwise, null.</returns>
public static MethodBase? GetStateMachineMoveNext(string methodName) public static MethodBase? GetStateMachineMoveNext(string methodName)
{ {