Initial commit

This commit is contained in:
2024-11-22 21:13:17 -05:00
parent ae4cf9b7a1
commit 400bd791db
10 changed files with 179 additions and 88 deletions

View File

@@ -1,4 +1,5 @@
using HarmonyLib;
using Il2CppInterop.Runtime;
using System;
using System.Collections.Generic;
using System.Reflection;
@@ -6,89 +7,44 @@ using UnityEngine;
namespace undead_universal_patch_il2cpp.Patches
{
[HarmonyPatch]
public class PhotonPatchEvent
{
static readonly string TargetTypeName = "RecNet.Core";
static readonly string TargetMethodName = "ConnectToRecNet";
static readonly string Description = "Photon Patch event method";
static readonly MethodInfo connectMethod = AccessTools.Method(AccessTools.TypeByName(TargetTypeName), TargetMethodName);
static bool Prepare()
{
if (connectMethod == null)
{
Plugin.Log.LogWarning($"'{Description}' disabled. The type for this patch was not found.");
return false;
}
Plugin.Log.LogInfo($"'{Description}' succeeded validation.");
return true;
}
static MethodBase TargetMethod() => connectMethod;
static void Prefix()
{
if (PhotonConfig.PatchPhotonIds.Value) Photon.Patch();
}
}
public class Photon
{
public static object Patch()
public static void Patch()
{
Plugin.Log.LogInfo("Attempting Photon patch.");
Type serverSettingsType = AccessTools.TypeByName("ServerSettings");
/*
Normally, I would be using reflection and AccessTools to create a new instance of ServerSettings and set the value of PhotonNetwork.PhotonServerSettings,
but since it was difficult and confusing with IL2CPP, I ended up just using explicit references.. every targeted game build *should* have these types anyway.
Now I'm wondering if the patch runs faster when using explicit references rather than reflection
*/
Type hostingOptionType = AccessTools.Inner(serverSettingsType, "HostingOption");
if (serverSettingsType == null)
{
Plugin.Log.LogFatal("Photon patch failed early, this Photon client is unsupported!");
return null;
}
ScriptableObject settingsInstance = ScriptableObject.CreateInstance(serverSettingsType);
object realPhotonServerSettings = Resources.Load("PhotonServerSettings", serverSettingsType);
try
{
var rpcListField = AccessTools.Field(serverSettingsType, "RpcList");
if (rpcListField == null)
{
Plugin.Log.LogFatal("Photon patch failed (serverSettingsType did not have an RpcList), this Photon client is unsupported!");
return null;
}
if (realPhotonServerSettings == null)
{
Plugin.Log.LogFatal("Photon patch failed (existing photon settings was null, is the patch event set to 'Awake'?), this Photon client is unsupported!");
return null;
}
var existingRpcList = (List<string>)rpcListField.GetValue(realPhotonServerSettings);
rpcListField.SetValue(settingsInstance, existingRpcList);
}
catch (Exception e)
{
Plugin.Log.LogFatal("Photon patch failed (RpcList), this Photon client is unsupported!");
Plugin.Log.LogDebug(e);
return null;
}
var appIdField = AccessTools.Field(serverSettingsType, "AppID");
appIdField.SetValue(settingsInstance, PhotonConfig.AppID.Value);
var voiceAppIdField = AccessTools.Field(serverSettingsType, "VoiceAppID");
voiceAppIdField.SetValue(settingsInstance, PhotonConfig.VoiceAppID.Value);
var hostTypeField = AccessTools.Field(serverSettingsType, "HostType");
if (hostTypeField != null && hostingOptionType != null && hostingOptionType.IsEnum)
{
try
{
object enumValue = Enum.Parse(hostingOptionType, "PhotonCloud");
hostTypeField.SetValue(settingsInstance, enumValue);
}
catch (ArgumentException ex)
{
Plugin.Log.LogFatal($"Photon patch failed, cannot set HostingOption: {ex.Message}");
return null;
}
}
// Save to property
Type photonNetworkType = AccessTools.TypeByName("PhotonNetwork");
if (photonNetworkType == null)
{
Plugin.Log.LogFatal("Photon patch will not work (class not found). Is this build supported?");
return null;
}
FieldInfo photonServerSettingsField = photonNetworkType.GetField("PhotonServerSettings");
if (photonServerSettingsField == null)
{
Plugin.Log.LogFatal("Photon patch will not work (property not found). Is this build supported?");
return null;
}
photonServerSettingsField.SetValue(serverSettingsType, settingsInstance);
Plugin.Log.LogInfo("Photon patch was successful.");
return settingsInstance;
PhotonNetwork.PhotonServerSettings.AppID = PhotonConfig.AppID.Value;
PhotonNetwork.PhotonServerSettings.VoiceAppID = PhotonConfig.VoiceAppID.Value;
}
}
}