Decompiled source of DunGenReferenceFixer v0.0.1

BepInEx/patchers/DunGenReferenceFixer.dll

Decompiled 2 weeks ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using System.Text;
using BepInEx;
using BepInEx.Bootstrap;
using BepInEx.Logging;
using DunGenReferenceFixer.Patches.Native;
using DunGenReferenceFixer.Utilities.Native;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Mono.Cecil;
using MonoMod.RuntimeDetour;
using Unity.Collections.LowLevel.Unsafe;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("DunGenReferenceFixer")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("DunGenReferenceFixer")]
[assembly: AssemblyCopyright("Copyright ©  2024")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("ee30c7a9-db89-425a-acff-2202e901ce36")]
[assembly: AssemblyFileVersion("0.0.1")]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.0.1.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace DunGenReferenceFixer
{
	[BepInPlugin("Zaggy1024.DunGenReferenceFixer", "DunGenReferenceFixer", "0.0.1")]
	public static class DunGenReferenceFixerPreloader
	{
		public const string PluginName = "DunGenReferenceFixer";

		public const string PluginGUID = "Zaggy1024.DunGenReferenceFixer";

		public const string PluginVersion = "0.0.1";

		public static ManualLogSource Log = Logger.CreateLogSource("DunGenReferenceFixer");

		private static Harmony harmony = new Harmony("DunGenReferenceFixer");

		public static IEnumerable<string> TargetDLLs { get; } = Array.Empty<string>();


		public static void Patch(AssemblyDefinition assembly)
		{
		}

		public static void Initialize()
		{
		}

		public static void Finish()
		{
			harmony.PatchAll(typeof(DunGenReferenceFixerPreloader));
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(Chainloader), "Initialize")]
		private static void ApplyAllNativePatches()
		{
			PatchGetMonoClassWithAssemblyName.Apply();
		}
	}
}
namespace DunGenReferenceFixer.Utilities.Native
{
	internal static class NativeHelpers
	{
		internal static readonly IntPtr BaseAddress = GetUnityPlayerModule().BaseAddress;

		internal static readonly bool IsDebugBuild = Debug.isDebugBuild;

		private static ProcessModule GetUnityPlayerModule()
		{
			ProcessModuleCollection modules = Process.GetCurrentProcess().Modules;
			for (int i = 0; i < modules.Count; i++)
			{
				ProcessModule processModule = modules[i];
				if (processModule.ModuleName.Contains("UnityPlayer"))
				{
					return processModule;
				}
			}
			return null;
		}
	}
}
namespace DunGenReferenceFixer.Patches.Native
{
	internal static class PatchGetMonoClassWithAssemblyName
	{
		[UnmanagedFunctionPointer(CallingConvention.ThisCall)]
		private delegate IntPtr GetMonoClassWithAssemblyNameDelegate(IntPtr thisMonoManager, IntPtr result, IntPtr param_2, IntPtr param_3, ref BasicStringRef param_4);

		private struct BasicStringRef
		{
			public unsafe char* Characters;

			public ulong Size;

			public unsafe BasicStringRef(ReadOnlySpan<byte> data)
			{
				Characters = (char*)UnsafeUtility.AddressOf<byte>(ref MemoryMarshal.GetReference(data));
				Size = (ulong)data.Length;
			}

			public unsafe static implicit operator ReadOnlySpan<byte>(BasicStringRef str)
			{
				if (str.Size > int.MaxValue)
				{
					throw new InvalidCastException("String is too large");
				}
				return new ReadOnlySpan<byte>(str.Characters, (int)str.Size);
			}

			public unsafe override readonly string ToString()
			{
				return Encoding.UTF8.GetString((byte*)Characters, (int)Math.Min(Size, 2147483647uL));
			}
		}

		private static NativeDetour getMonoClassWithAssemblyNameDetour;

		private static GetMonoClassWithAssemblyNameDelegate getMonoClassWithAssemblyNameOriginal;

		[MethodImpl(MethodImplOptions.NoInlining)]
		internal static void Apply()
		{
			//IL_0040: Unknown result type (might be due to invalid IL or missing references)
			//IL_004a: Expected O, but got Unknown
			int num = 7881440;
			if (NativeHelpers.IsDebugBuild)
			{
				num = 16836128;
			}
			IntPtr intPtr = NativeHelpers.BaseAddress + num;
			IntPtr functionPointerForDelegate = Marshal.GetFunctionPointerForDelegate<GetMonoClassWithAssemblyNameDelegate>(GetMonoClassWithAssemblyNameDetour);
			getMonoClassWithAssemblyNameDetour = new NativeDetour(intPtr, functionPointerForDelegate);
			getMonoClassWithAssemblyNameOriginal = getMonoClassWithAssemblyNameDetour.GenerateTrampoline<GetMonoClassWithAssemblyNameDelegate>();
			DunGenReferenceFixerPreloader.Log.LogInfo((object)"Patched MonoManager::GetMonoClassWithAssemblyName() to fix DunGen references.");
		}

		private unsafe static bool CheckNamespace(IntPtr namespaceName, ReadOnlySpan<byte> bytes)
		{
			byte* ptr = (byte*)(void*)namespaceName;
			for (int i = 0; i < bytes.Length; i++)
			{
				if (ptr[i] != bytes[i])
				{
					return false;
				}
			}
			byte b = ptr[bytes.Length];
			if (b != 0)
			{
				return b == 46;
			}
			return true;
		}

		private static IntPtr GetMonoClassWithAssemblyNameDetour(IntPtr thisMonoManager, IntPtr result, IntPtr className, IntPtr namespaceName, ref BasicStringRef assemblyName)
		{
			ReadOnlySpan<byte> bytes = "DunGen"u8;
			ReadOnlySpan<byte> span = "Assembly-CSharp"u8;
			if (CheckNamespace(namespaceName, bytes) && span.SequenceEqual(assemblyName))
			{
				ReadOnlySpan<byte> data = "DunGen"u8;
				BasicStringRef param_ = new BasicStringRef(data);
				getMonoClassWithAssemblyNameOriginal(thisMonoManager, result, className, namespaceName, ref param_);
			}
			else
			{
				getMonoClassWithAssemblyNameOriginal(thisMonoManager, result, className, namespaceName, ref assemblyName);
			}
			return result;
		}
	}
}
namespace System.Runtime.CompilerServices
{
	[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
	internal sealed class IgnoresAccessChecksToAttribute : Attribute
	{
		public IgnoresAccessChecksToAttribute(string assemblyName)
		{
		}
	}
}