Decompiled source of EnemyLocation WZK EnemySpawnFix v1.0.0

BepInEx/patchers/EnemyLocationWZKFix/EnemyLocationWZKFix.dll

Decompiled 2 weeks ago
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx.Logging;
using Mono.Cecil;
using Mono.Collections.Generic;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: AssemblyVersion("0.1.1.0")]
namespace EnemyLocationWZKFix;

public static class Patcher
{
	private static readonly ManualLogSource Log = Logger.CreateLogSource("EnemyLocationWZKFix");

	public static IEnumerable<string> TargetDLLs
	{
		get
		{
			yield return "Assembly-CSharp.dll";
		}
	}

	public static void Patch(AssemblyDefinition assembly)
	{
		Expose(assembly, "MapCustom", "mapCustomEntity", "MapCustomEntity");
		Expose(assembly, "PlayerAvatar", "playerDeathHead", "PlayerDeathHead");
	}

	private static void Expose(AssemblyDefinition assembly, string owner, string name, string fieldType)
	{
		//IL_0022: Unknown result type (might be due to invalid IL or missing references)
		//IL_0027: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f6: Unknown result type (might be due to invalid IL or missing references)
		//IL_00fb: Unknown result type (might be due to invalid IL or missing references)
		//IL_017e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0188: Unknown result type (might be due to invalid IL or missing references)
		//IL_019d: Unknown result type (might be due to invalid IL or missing references)
		//IL_01a7: Unknown result type (might be due to invalid IL or missing references)
		TypeDefinition type = assembly.MainModule.GetType(owner);
		if (type != null)
		{
			Enumerator<FieldDefinition> enumerator = type.Fields.GetEnumerator();
			try
			{
				while (enumerator.MoveNext())
				{
					FieldDefinition current = enumerator.Current;
					if (((MemberReference)current).Name != name)
					{
						continue;
					}
					if (((MemberReference)((FieldReference)current).FieldType).FullName != fieldType || current.IsStatic)
					{
						Log.LogWarning((object)("Unexpected field layout: " + owner + "." + name + "; skipped."));
						return;
					}
					if (current.IsPublic)
					{
						Log.LogInfo((object)(owner + "." + name + " already public; skipped."));
						return;
					}
					bool flag = false;
					Enumerator<CustomAttribute> enumerator2 = current.CustomAttributes.GetEnumerator();
					try
					{
						while (enumerator2.MoveNext())
						{
							CustomAttribute current2 = enumerator2.Current;
							if (((MemberReference)current2.AttributeType).FullName == "UnityEngine.SerializeField" || ((MemberReference)current2.AttributeType).FullName == "UnityEngine.SerializeReference")
							{
								flag = true;
							}
						}
					}
					finally
					{
						((IDisposable)enumerator2/*cast due to .constrained prefix*/).Dispose();
					}
					bool flag2 = !current.IsNotSerialized && (current.IsPublic || flag);
					current.Attributes = (FieldAttributes)(ushort)((ushort)(current.Attributes & 0xFFF8) | 6);
					if (!flag2)
					{
						current.Attributes = (FieldAttributes)(ushort)(current.Attributes | 0x80);
					}
					Log.LogInfo((object)("Restored Enemy Location access to " + owner + "." + name + " in memory."));
					return;
				}
			}
			finally
			{
				((IDisposable)enumerator/*cast due to .constrained prefix*/).Dispose();
			}
		}
		Log.LogWarning((object)("Field not found: " + owner + "." + name + "; skipped."));
	}
}