using System;
using System.Reflection;
using HarmonyLib;
namespace undead_universal_patch.Patches
{
[HarmonyPatch]
class UWRPatchNoArg
{
const string TargetTypeName = "UnityEngine.Networking.UnityWebRequest";
const string Description = "Legacy UnityWebRequest URL rewrite patch";
const string UWRParams = "(NoArgs)";
static readonly Type targetType = AccessTools.TypeByName(TargetTypeName);
public static bool Prepare()
{
if (GenericConfig.IsNameserver.Value) return false;
if (targetType == null)
{
Plugin.Log.LogWarning($"'{Description}' disabled. The type for this patch was not found.");
return false;
}
if (GenericConfig.IsNameserver.Value)
{
Plugin.Log.LogInfo($"'{Description}' disabled. GenericConfig.IsNameserver was set.");
return false;
}
Plugin.Log.LogInfo($"'{Description}' succeeded validation.");
return true;
}
static MethodBase TargetMethod()
{
return AccessTools.Constructor(targetType, []);
}
[HarmonyPostfix]
static void Postfix(object __instance) => InPostfix(__instance, UWRParams);
public static void InPostfix(object __instance, string UWRParams)
{
var urlProperty = AccessTools.Property(__instance.GetType(), "url");
if (urlProperty == null)
{
Plugin.Log.LogWarning($"UWR {UWRParams} Prefix could not find the url field!");
return;
}
var targetUrl = (string)urlProperty.GetValue(__instance, null);
if (GenericConfig.LogAllRequests.Value) Plugin.Log.LogDebug($"UWR {UWRParams} Request URL: {targetUrl}");
urlProperty.SetValue(__instance, PatchUtils.RewriteUrlForGameserver(targetUrl).ToString(), null);
targetUrl = (string)urlProperty.GetValue(__instance, null);
if (GenericConfig.LogAllRequests.Value) Plugin.Log.LogDebug($"UWR {UWRParams} Request URL (Rewritten): {targetUrl}");
}
}
}