Decompiled source of GnomesCompat v1.0.0

patchers/GnomesCompat.dll

Decompiled a day ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx.Logging;
using Microsoft.CodeAnalysis;
using Mono.Cecil;
using Mono.Cecil.Cil;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyCompany("GnomesCompat")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+c914b31991990e7847448fd5a27793ff41e8f5ed")]
[assembly: AssemblyProduct("GnomesCompat")]
[assembly: AssemblyTitle("GnomesCompat")]
[assembly: AssemblyVersion("1.0.0.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[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;
		}
	}
}
public static class GnomesCompatPatcher
{
	private static readonly ManualLogSource Log = Logger.CreateLogSource("GnomesCompat");

	public static IEnumerable<string> TargetDLLs { get; } = new string[1] { "Assembly-CSharp.dll" };

	public static void Patch(AssemblyDefinition assembly)
	{
		ModuleDefinition module = assembly.MainModule;
		List<TypeDefinition> types = module.GetTypes().ToList();
		Run("ChestDataSO drop pools", () => PatchChestData(module, types));
		Run("PlayerStats.ServerHealWithFX(float)", () => PatchHeal(module, types));
		Run("DiggingPattern.Id", () => PatchDiggingPatternId(module, types));
		Run("SDiggingPattern.id", () => PatchSDiggingPatternId(module, types));
	}

	private static void Run(string name, Func<string> patch)
	{
		try
		{
			Log.LogInfo((object)(name + ": " + patch()));
		}
		catch (Exception arg)
		{
			Log.LogError((object)$"{name}: failed — {arg}");
		}
	}

	private static TypeDefinition Find(List<TypeDefinition> types, string name, string declaringType = null)
	{
		return ((IEnumerable<TypeDefinition>)types).FirstOrDefault((Func<TypeDefinition, bool>)delegate(TypeDefinition t)
		{
			if (((MemberReference)t).Name == name)
			{
				if (declaringType != null)
				{
					TypeDefinition declaringType2 = t.DeclaringType;
					return ((declaringType2 != null) ? ((MemberReference)declaringType2).Name : null) == declaringType;
				}
				return true;
			}
			return false;
		}) ?? throw new Exception("type " + name + " not found");
	}

	private static FieldDefinition AddShimField(TypeDefinition type, string name, TypeReference fieldType)
	{
		//IL_0007: Unknown result type (might be due to invalid IL or missing references)
		//IL_000d: Expected O, but got Unknown
		FieldDefinition val = new FieldDefinition(name, (FieldAttributes)134, fieldType);
		type.Fields.Add(val);
		return val;
	}

	private static string PatchChestData(ModuleDefinition module, List<TypeDefinition> types)
	{
		//IL_0139: Unknown result type (might be due to invalid IL or missing references)
		//IL_014e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0163: Unknown result type (might be due to invalid IL or missing references)
		//IL_0179: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e7: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ed: Expected O, but got Unknown
		//IL_0104: Unknown result type (might be due to invalid IL or missing references)
		TypeDefinition val = Find(types, "ChestDataSO");
		if (((IEnumerable<FieldDefinition>)val.Fields).Any((FieldDefinition f) => ((MemberReference)f).Name == "level1DropPoolSO"))
		{
			return "already present, skipped";
		}
		FieldDefinition val2 = ((IEnumerable<FieldDefinition>)val.Fields).FirstOrDefault((Func<FieldDefinition, bool>)((FieldDefinition f) => ((MemberReference)f).Name == "dropPoolSO")) ?? throw new Exception("ChestDataSO.dropPoolSO not found");
		FieldDefinition val3 = AddShimField(val, "level1DropPoolSO", ((FieldReference)val2).FieldType);
		AddShimField(val, "level2DropPoolSO", ((FieldReference)val2).FieldType);
		MethodDefinition val4 = ((IEnumerable<MethodDefinition>)val.Methods).FirstOrDefault((Func<MethodDefinition, bool>)((MethodDefinition m) => ((MemberReference)m).Name == "OnEnable" && !((MethodReference)m).HasParameters));
		bool flag = val4 == null;
		if (flag)
		{
			val4 = new MethodDefinition("OnEnable", (MethodAttributes)129, module.TypeSystem.Void);
			val.Methods.Add(val4);
			val4.Body.GetILProcessor().Append(Instruction.Create(OpCodes.Ret));
		}
		ILProcessor iLProcessor = val4.Body.GetILProcessor();
		Instruction val5 = val4.Body.Instructions[0];
		iLProcessor.InsertBefore(val5, iLProcessor.Create(OpCodes.Ldarg_0));
		iLProcessor.InsertBefore(val5, iLProcessor.Create(OpCodes.Ldarg_0));
		iLProcessor.InsertBefore(val5, iLProcessor.Create(OpCodes.Ldfld, (FieldReference)(object)val2));
		iLProcessor.InsertBefore(val5, iLProcessor.Create(OpCodes.Stfld, (FieldReference)(object)val3));
		return "added level1/level2 fields (" + (flag ? "new" : "existing") + " OnEnable fills level1)";
	}

	private static string PatchHeal(ModuleDefinition module, List<TypeDefinition> types)
	{
		//IL_00b4: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ba: Expected O, but got Unknown
		//IL_00d1: Unknown result type (might be due to invalid IL or missing references)
		//IL_00db: Expected O, but got Unknown
		//IL_00e7: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f2: Unknown result type (might be due to invalid IL or missing references)
		//IL_00fd: Unknown result type (might be due to invalid IL or missing references)
		//IL_0108: Unknown result type (might be due to invalid IL or missing references)
		//IL_0113: Unknown result type (might be due to invalid IL or missing references)
		TypeDefinition val = Find(types, "PlayerStats");
		List<MethodDefinition> source = ((IEnumerable<MethodDefinition>)val.Methods).Where((MethodDefinition m) => ((MemberReference)m).Name == "ServerHealWithFX").ToList();
		if (source.Any((MethodDefinition m) => ((MethodReference)m).Parameters.Count == 1))
		{
			return "already present, skipped";
		}
		MethodDefinition val2 = ((IEnumerable<MethodDefinition>)source).FirstOrDefault((Func<MethodDefinition, bool>)((MethodDefinition m) => ((MethodReference)m).Parameters.Count == 2 && (int)((ParameterReference)((MethodReference)m).Parameters[0]).ParameterType.MetadataType == 12)) ?? throw new Exception("ServerHealWithFX(float, NetworkIdentity) not found");
		MethodDefinition val3 = new MethodDefinition("ServerHealWithFX", (MethodAttributes)134, module.TypeSystem.Void);
		((MethodReference)val3).Parameters.Add(new ParameterDefinition("amount", (ParameterAttributes)0, module.TypeSystem.Single));
		ILProcessor iLProcessor = val3.Body.GetILProcessor();
		iLProcessor.Emit(OpCodes.Ldarg_0);
		iLProcessor.Emit(OpCodes.Ldarg_1);
		iLProcessor.Emit(OpCodes.Ldnull);
		iLProcessor.Emit(OpCodes.Call, (MethodReference)(object)val2);
		iLProcessor.Emit(OpCodes.Ret);
		val.Methods.Add(val3);
		return "added forwarding overload";
	}

	private static string PatchDiggingPatternId(ModuleDefinition module, List<TypeDefinition> types)
	{
		//IL_0054: Unknown result type (might be due to invalid IL or missing references)
		//IL_005a: Expected O, but got Unknown
		//IL_0066: Unknown result type (might be due to invalid IL or missing references)
		//IL_0070: Unknown result type (might be due to invalid IL or missing references)
		//IL_009d: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a2: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ae: Expected O, but got Unknown
		TypeDefinition val = Find(types, "DiggingPattern");
		if (((IEnumerable<MethodDefinition>)val.Methods).Any((MethodDefinition m) => ((MemberReference)m).Name == "get_Id"))
		{
			return "already present, skipped";
		}
		MethodDefinition val2 = new MethodDefinition("get_Id", (MethodAttributes)2182, module.TypeSystem.String);
		ILProcessor iLProcessor = val2.Body.GetILProcessor();
		iLProcessor.Emit(OpCodes.Ldnull);
		iLProcessor.Emit(OpCodes.Ret);
		val.Methods.Add(val2);
		val.Properties.Add(new PropertyDefinition("Id", (PropertyAttributes)0, module.TypeSystem.String)
		{
			GetMethod = val2
		});
		return "added Id getter (returns null; matching falls back to position)";
	}

	private static string PatchSDiggingPatternId(ModuleDefinition module, List<TypeDefinition> types)
	{
		TypeDefinition val = Find(types, "SDiggingPattern", "DiggingManager");
		if (((IEnumerable<FieldDefinition>)val.Fields).Any((FieldDefinition f) => ((MemberReference)f).Name == "id"))
		{
			return "already present, skipped";
		}
		AddShimField(val, "id", module.TypeSystem.String);
		return "added id field";
	}
}