Internals rewritten. PHoton config can now be fetched from the server, and if it fails, fallback to the local configuration.

This commit is contained in:
2025-08-16 21:04:26 -04:00
parent 8ca37170e4
commit 087d9a900e
20 changed files with 393 additions and 234 deletions

View File

@@ -4,7 +4,7 @@ using System.Reflection;
using HarmonyLib;
using undead_universal_patch_il2cpp.Core;
using undead_universal_patch_il2cpp.Core.Config;
using undead_universal_patch_il2cpp.Core.UndeadGameManager;
using undead_universal_patch_il2cpp.Core.Content.UndeadGameManager;
namespace undead_universal_patch_il2cpp.Patches.UndeadGameManager;

View File

@@ -6,7 +6,7 @@ using Mapster;
using RecRoom.Core.GameManagement;
using undead_universal_patch_il2cpp.Core.Config;
using undead_universal_patch_il2cpp.Core;
using undead_universal_patch_il2cpp.Core.UndeadGameManager;
using undead_universal_patch_il2cpp.Core.Content.UndeadGameManager;
namespace undead_universal_patch_il2cpp.Patches.UndeadGameManager;

View File

@@ -1,11 +1,9 @@
using System.Reflection;
using HarmonyLib;
using Il2CppInterop.Runtime;
using RecRoom.Async;
using undead_universal_patch_il2cpp.Core;
using undead_universal_patch_il2cpp.Core.CustomRecNet;
using undead_universal_patch_il2cpp.Core.Content.CustomRecNet;
namespace undead_universal_patch_il2cpp.Patches;
namespace undead_universal_patch_il2cpp.Patches.Internals;
[HarmonyPatch]
public class AuthenticationEventPatch
@@ -24,7 +22,6 @@ public class AuthenticationEventPatch
static MethodBase TargetMethod() => patchResult.Method;
private static bool RanPostActions { get; set; } = false;
static void Postfix(ref string accessToken)
{

View File

@@ -0,0 +1,32 @@
using System.Reflection;
using BestHTTP;
using HarmonyLib;
using Il2CppInterop.Runtime;
using RecRoom.Async;
using undead_universal_patch_il2cpp.Core;
using undead_universal_patch_il2cpp.Core.Content.CustomRecNet;
namespace undead_universal_patch_il2cpp.Patches.Internals;
[HarmonyPatch]
public class GetMyAccountEventPatch
{
static PatchTypesResult patchResult = Util.PreparePatchTypes(
"RecNet GetLocalAccount event patch",
"RecNet.Accounts",
"GetLocalAccount"
);
static bool Prepare() => patchResult.Success;
static MethodBase TargetMethod() => patchResult.Method;
static void Postfix(ref IPromise<RecNet.SelfAccount> __result)
{
__result.Then(DelegateSupport.ConvertDelegate<Il2CppSystem.Action>(() =>
{
UniversalPatchPlugin.Log.LogInfo("Running post-GetLocalAccount actions");
foreach (var action in RecNetInteractions.postLocalAccountActions) action();
}));
}
}

View File

@@ -1,11 +1,13 @@
using System.Reflection;
using HarmonyLib;
using Il2CppInterop.Runtime;
using RecRoom.Async;
using undead_universal_patch_il2cpp.Core;
using undead_universal_patch_il2cpp.Core.CustomRecNet;
using undead_universal_patch_il2cpp.Core.Content.CustomRecNet;
namespace undead_universal_patch_il2cpp.Patches;
namespace undead_universal_patch_il2cpp.Patches.Internals;
[HarmonyPatch]
public class NameserverConnectEventPatch
{
static PatchTypesResult patchResult = Util.PreparePatchTypes(
@@ -25,7 +27,7 @@ public class NameserverConnectEventPatch
if (RecNetInteractions.HasNameserverConnected())
{
UniversalPatchPlugin.Log.LogInfo("Running post-nameserver actions");
foreach (var action in RecNetInteractions.postAuthenticationActions) action();
foreach (var action in RecNetInteractions.postNameServerActions) action();
}
else Util.ConditionalDebug("The nameserver request did not resolve successfully, skipping post-nameserver actions.");
}));

View File

@@ -0,0 +1,18 @@
using System.Reflection;
using HarmonyLib;
using undead_universal_patch_il2cpp.Core;
namespace undead_universal_patch_il2cpp.Patches.Photon;
[HarmonyPatch]
public class NameserverTest
{
static PatchTypesResult patchTypesResult = Util.PreparePatchTypes(
"Photon nameserver test",
"NetworkingPeer",
"GetNameServerAddress"
);
static bool Prepare() => patchTypesResult.Success;
static MethodBase TargetMethod() => patchTypesResult.Method;
}

View File

@@ -0,0 +1,108 @@
using System.Reflection;
using HarmonyLib;
using undead_universal_patch_il2cpp.Core;
using undead_universal_patch_il2cpp.Core.Config;
using undead_universal_patch_il2cpp.Core.Content.CustomRecNet.CustomPhoton;
namespace undead_universal_patch_il2cpp.Patches.Photon;
[HarmonyPatch]
public class ForceSelfHostedPhoton
{
class HarmonyState
{
public object code;
}
static PatchTypesResult patchResult = Util.ConfigsPreparePatchTypes(
[
PhotonConfig.PatchPhotonIds,
PhotonConfig.SelfHosted
],
"Force JoinOrCreateRoom when connected to master",
"PUNNetworkManager",
"OnConnectedToMaster"
);
static bool Prepare() => patchResult.Success;
static MethodBase TargetMethod() => patchResult.Method;
static void Prefix(ref PUNNetworkManager __instance, ref HarmonyState __state)
{
if (ServerPatchesConfig.CustomPhoton.Value && !CustomPhoton.ServerConfigFailed)
{
Util.ConditionalDebug("Skipping Selfhost Photon target server regionId roundtrip, CustomPhoton is enabled.");
return;
}
__state = new HarmonyState();
PropertyInfo targetGameSessionProperty = __instance.GetType().GetRuntimeProperty("targetGameSession");
if (targetGameSessionProperty == null)
{
UniversalPatchPlugin.Log.LogFatal("Cannot force masterserver: targetGameSessionProperty was null.");
return;
}
var targetGameSession = targetGameSessionProperty.GetValue(__instance);
if (targetGameSession == null)
{
UniversalPatchPlugin.Log.LogFatal("Cannot force masterserver: targetGameSession was null.");
return;
}
PropertyInfo photonRegionIdProperty = targetGameSession.GetType().GetRuntimeProperty("PhotonRegionId");
if (photonRegionIdProperty == null)
{
UniversalPatchPlugin.Log.LogFatal("Cannot force masterserver: photonRegionIdProperty was null.");
return;
}
__state.code = photonRegionIdProperty.GetValue(targetGameSession);
photonRegionIdProperty.SetValue(targetGameSession, 4);
Util.ConditionalDebug("Selfhost Photon target server regionId pre-roundtrip");
}
static void Postfix(ref PUNNetworkManager __instance, ref HarmonyState __state)
{
PropertyInfo targetGameSessionProperty = __instance.GetType().GetRuntimeProperty("targetGameSession");
if (targetGameSessionProperty == null)
{
UniversalPatchPlugin.Log.LogFatal("Cannot force masterserver postfix: targetGameSessionProperty was null.");
return;
}
var targetGameSession = targetGameSessionProperty.GetValue(__instance);
if (targetGameSession == null)
{
UniversalPatchPlugin.Log.LogFatal("Cannot force masterserver postfix: targetGameSession was null.");
return;
}
PropertyInfo photonRegionIdProperty = targetGameSession.GetType().GetRuntimeProperty("PhotonRegionId");
if (photonRegionIdProperty == null)
{
UniversalPatchPlugin.Log.LogFatal("Cannot force masterserver: photonRegionIdProperty was null.");
return;
}
photonRegionIdProperty.SetValue(targetGameSession, __state.code);
Util.ConditionalDebug("Selfhost Photon target server regionId post-roundtrip");
}
}
// Fixes some reflection used by Rec Room that, when TCP is used in the Photon patch config,
// incorrectly assumes that ENetPeer (UDP) is in use
// Allows TPeer (TCP) to be specified by PhotonConfig.ConnectionProtocol
[HarmonyPatch]
public class PhotonThrottlingPatch
{
static PatchTypesResult patchTypesResult = Util.ConfigPreparePatchTypes(
PhotonConfig.SelfHosted,
"ENet incorrect assumption patch",
"PUNNetworkManager",
"UpdatePhotonThrottling"
);
static bool Prepare() => patchTypesResult.Success;
static MethodBase TargetMethod() => patchTypesResult.Method;
static bool Prefix() => PhotonConfig.ConnectionProtocol.Value != 1;
}

View File

@@ -0,0 +1,93 @@
using System;
using System.Reflection;
using HarmonyLib;
using undead_universal_patch_il2cpp.Core;
using undead_universal_patch_il2cpp.Core.Config;
using undead_universal_patch_il2cpp.Core.Content.CustomRecNet.CustomPhoton;
namespace undead_universal_patch_il2cpp.Patches.Photon;
public class PhotonPatch
{
public static void Patch(PhotonConfigDTO photonConfig)
{
// It's fine to reference enums after we're pretty sure the relevant types exist
// i think
Type photonNetworkType = AccessTools.TypeByName("PhotonNetwork");
Type serverSettingsType = AccessTools.TypeByName("ServerSettings");
if (photonNetworkType == null || serverSettingsType == null)
{
UniversalPatchPlugin.Log.LogError("Cannot patch Photon: PhotonNetwork or ServerSettings types were not found. Is this build supported?");
return;
}
PropertyInfo photonServerSettingsProperty = photonNetworkType.GetRuntimeProperty("PhotonServerSettings");
object serverSettings = photonServerSettingsProperty.GetValue(serverSettingsType);
if (PhotonConfig.PunLogging.Value)
{
PropertyInfo loggingProperty = serverSettingsType.GetRuntimeProperty("PunLogging");
loggingProperty.SetValue(serverSettings, PhotonLogLevel.Full);
PropertyInfo networkLoggingProperty = serverSettingsType.GetRuntimeProperty("NetworkLogging");
networkLoggingProperty.SetValue(serverSettings, ExitGames.Client.Photon.DebugLevel.ALL);
}
PropertyInfo appIdProperty = serverSettingsType.GetRuntimeProperty("AppID");
PropertyInfo voiceAppIdProperty = serverSettingsType.GetRuntimeProperty("VoiceAppID");
Util.ConditionalDebug($"New Photon AppID: '{photonConfig.AppID}'");
Util.ConditionalDebug($"New Photon AppID: '{photonConfig.VoiceAppID}'");
appIdProperty.SetValue(serverSettings, photonConfig.AppID);
voiceAppIdProperty.SetValue(serverSettings, photonConfig.VoiceAppID);
if (photonConfig.SelfHosted)
{
MethodInfo useMyServerMethod = serverSettingsType.GetMethod("UseMyServer");
useMyServerMethod.Invoke(serverSettings, [
photonConfig.ServerAddress,
photonConfig.ServerPort,
photonConfig.AppID,
]);
Type authValuesType = AccessTools.TypeByName("AuthenticationValues");
if (authValuesType != null)
{
PropertyInfo networkingPeerProperty = photonNetworkType.GetRuntimeProperty("networkingPeer");
if (networkingPeerProperty == null)
{
UniversalPatchPlugin.Log.LogError("Cannot continue to patch Photon: networkingPeerProperty was null. Is this build supported?");
return;
}
if (networkingPeerProperty.GetValue(null) == null)
{
UniversalPatchPlugin.Log.LogError("Cannot continue to patch Photon: networkingPeerInstance was null. Is this build supported?");
return;
}
int id = RecNetUtil.GetAccountId();
PhotonNetwork.AuthValues = new AuthenticationValues
{
UserId = id.ToString()
};
Util.ConditionalDebug($"Set the authValues userId to {id}");
}
Type connectionProtocolType = AccessTools.TypeByName("ExitGames.Client.Photon.ConnectionProtocol");
PropertyInfo protocolProp = serverSettingsType.GetRuntimeProperty("Protocol");
if (connectionProtocolType != null && protocolProp != null)
{
protocolProp.SetValue(serverSettings, photonConfig.ConnectionProtocol);
}
else
{
UniversalPatchPlugin.Log.LogError("Cannot continue to patch Photon: connectionProtocolType or protocolProp was null");
return;
}
}
UniversalPatchPlugin.Log.LogInfo($"Photon patch ({(photonConfig.SelfHosted ? "self-hosted" : "cloud")}) succeeded.");
}
}

View File

@@ -1,180 +0,0 @@
using System;
using System.Reflection;
using HarmonyLib;
using undead_universal_patch_il2cpp.Core;
using undead_universal_patch_il2cpp.Core.Config;
namespace undead_universal_patch_il2cpp.Patches
{
[HarmonyPatch]
public class PhotonPatchEvent
{
static PatchTypesResult patchResult = Util.ConfigPreparePatchTypes(
PhotonConfig.PatchPhotonIds,
"Photon ConnectUsingSettings event path",
"PhotonNetwork",
"ConnectUsingSettings"
);
static bool Prepare() => patchResult.Success;
static MethodInfo TargetMethod() => patchResult.Method;
static void Prefix()
{
Util.ConditionalDebug("Attempting Photon patch");
PhotonPatch.Patch();
}
}
[HarmonyPatch]
public class ForceSelfHostedPhoton
{
class HarmonyState
{
public object code;
}
static PatchTypesResult patchResult = Util.ConfigsPreparePatchTypes(
[
PhotonConfig.PatchPhotonIds,
PhotonConfig.SelfHosted
],
"Force JoinOrCreateRoom when connected to master",
"PUNNetworkManager",
"OnConnectedToMaster"
);
static bool Prepare() => patchResult.Success;
static MethodBase TargetMethod() => patchResult.Method;
static void Prefix(ref PUNNetworkManager __instance, ref HarmonyState __state)
{
__state = new HarmonyState();
PropertyInfo targetGameSessionProperty = __instance.GetType().GetRuntimeProperty("targetGameSession");
if (targetGameSessionProperty == null)
{
UniversalPatchPlugin.Log.LogFatal("Cannot force masterserver: targetGameSessionProperty was null.");
return;
}
var targetGameSession = targetGameSessionProperty.GetValue(__instance);
if (targetGameSession == null)
{
UniversalPatchPlugin.Log.LogFatal("Cannot force masterserver: targetGameSession was null.");
return;
}
PropertyInfo photonRegionIdProperty = targetGameSession.GetType().GetRuntimeProperty("PhotonRegionId");
if (photonRegionIdProperty == null)
{
UniversalPatchPlugin.Log.LogFatal("Cannot force masterserver: photonRegionIdProperty was null.");
return;
}
__state.code = photonRegionIdProperty.GetValue(targetGameSession);
photonRegionIdProperty.SetValue(targetGameSession, 4);
Util.ConditionalDebug("Forcing masterserver");
}
static void Postfix(ref PUNNetworkManager __instance, ref HarmonyState __state)
{
PropertyInfo targetGameSessionProperty = __instance.GetType().GetRuntimeProperty("targetGameSession");
if (targetGameSessionProperty == null)
{
UniversalPatchPlugin.Log.LogFatal("Cannot force masterserver postfix: targetGameSessionProperty was null.");
return;
}
var targetGameSession = targetGameSessionProperty.GetValue(__instance);
if (targetGameSession == null)
{
UniversalPatchPlugin.Log.LogFatal("Cannot force masterserver postfix: targetGameSession was null.");
return;
}
PropertyInfo photonRegionIdProperty = targetGameSession.GetType().GetRuntimeProperty("PhotonRegionId");
if (photonRegionIdProperty == null)
{
UniversalPatchPlugin.Log.LogFatal("Cannot force masterserver: photonRegionIdProperty was null.");
return;
}
photonRegionIdProperty.SetValue(targetGameSession, __state.code);
Util.ConditionalDebug("Masterserver regionId roundtrip");
}
}
public class PhotonPatch
{
public static void Patch()
{
// It's fine to reference enums after we're pretty sure the relevant types exist
// i think
Type photonNetworkType = AccessTools.TypeByName("PhotonNetwork");
Type serverSettingsType = AccessTools.TypeByName("ServerSettings");
if (photonNetworkType == null || serverSettingsType == null)
{
UniversalPatchPlugin.Log.LogError("Cannot patch Photon: PhotonNetwork or ServerSettings types were not found. Is this build supported?");
return;
}
PropertyInfo photonServerSettingsProperty = photonNetworkType.GetRuntimeProperty("PhotonServerSettings");
object serverSettings = photonServerSettingsProperty.GetValue(serverSettingsType);
if (PhotonConfig.PunLogging.Value)
{
PropertyInfo loggingProperty = serverSettingsType.GetRuntimeProperty("PunLogging");
loggingProperty.SetValue(serverSettings, PhotonLogLevel.Full);
PropertyInfo networkLoggingProperty = serverSettingsType.GetRuntimeProperty("NetworkLogging");
networkLoggingProperty.SetValue(serverSettings, ExitGames.Client.Photon.DebugLevel.ALL);
}
PropertyInfo appIdProperty = serverSettingsType.GetRuntimeProperty("AppID");
PropertyInfo voiceAppIdProperty = serverSettingsType.GetRuntimeProperty("VoiceAppID");
Util.ConditionalDebug($"New Photon AppID: '{PhotonConfig.AppID.Value}'");
Util.ConditionalDebug($"New Photon AppID: '{PhotonConfig.VoiceAppID.Value}'");
appIdProperty.SetValue(serverSettings, PhotonConfig.AppID.Value);
voiceAppIdProperty.SetValue(serverSettings, PhotonConfig.VoiceAppID.Value);
if (PhotonConfig.SelfHosted.Value)
{
MethodInfo useMyServerMethod = serverSettingsType.GetMethod("UseMyServer");
useMyServerMethod.Invoke(serverSettings, [
PhotonConfig.ServerAddress.Value,
PhotonConfig.ServerPort.Value,
PhotonConfig.AppID.Value,
]);
Type authValuesType = AccessTools.TypeByName("AuthenticationValues");
if (authValuesType != null)
{
PropertyInfo networkingPeerProperty = photonNetworkType.GetRuntimeProperty("networkingPeer");
if (networkingPeerProperty == null)
{
UniversalPatchPlugin.Log.LogError("Cannot patch Photon: networkingPeerProperty was null. Is this build supported?");
return;
}
if (networkingPeerProperty.GetValue(null) == null)
{
UniversalPatchPlugin.Log.LogError("Cannot patch Photon: networkingPeerInstance was null. Is this build supported?");
return;
}
string id = Util.LocalInstanceGuid;
Util.ConditionalDebug($"Instance GUID is {id}");
PhotonNetwork.AuthValues = new AuthenticationValues
{
UserId = id
};
Util.ConditionalDebug($"Set the authValues userId to {id}");
}
}
UniversalPatchPlugin.Log.LogInfo($"Photon patch ({(PhotonConfig.SelfHosted.Value ? "self-hosted" : "cloud")}) succeeded.");
}
}
}