Decompiled source of DragonRiders UpdateFix v1.0.0

patchers/DragonRiders_UpdateFix.Patcher.dll

Decompiled a week ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx.Logging;
using Mono.Cecil;
using Mono.Cecil.Cil;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyCompany("DragonRiders_UpdateFix.Patcher")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("DragonRiders_UpdateFix.Patcher")]
[assembly: AssemblyTitle("DragonRiders_UpdateFix.Patcher")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace DragonRidersUpdateFix;

public static class ShimPatcher
{
	public static IEnumerable<string> TargetDLLs
	{
		get
		{
			yield return "assembly_valheim.dll";
		}
	}

	public static void Patch(AssemblyDefinition assembly)
	{
		ManualLogSource log = Logger.CreateLogSource("DragonRiders_UpdateFix");
		try
		{
			PatchAssembly(assembly, delegate(string s)
			{
				log.LogInfo((object)s);
			}, delegate(string s)
			{
				log.LogWarning((object)s);
			});
		}
		catch (Exception ex)
		{
			log.LogError((object)("Unexpected error, no further shims applied: " + ex));
		}
		finally
		{
			Logger.Sources.Remove((ILogSource)(object)log);
		}
	}

	internal static int PatchAssembly(AssemblyDefinition assembly, Action<string> info, Action<string> warn)
	{
		int num = 0;
		foreach (ShimSpec item in ShimTable.All)
		{
			try
			{
				if (ApplyShim(assembly.MainModule, item, info, warn))
				{
					num++;
				}
			}
			catch (Exception ex)
			{
				warn(Describe(item) + ": skipped, error: " + ex.Message);
			}
		}
		info("Done. Shims added: " + num + " of " + ShimTable.All.Count + ".");
		return num;
	}

	private static bool ApplyShim(ModuleDefinition module, ShimSpec spec, Action<string> info, Action<string> warn)
	{
		//IL_0159: Unknown result type (might be due to invalid IL or missing references)
		//IL_0176: Unknown result type (might be due to invalid IL or missing references)
		//IL_017f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0186: Expected O, but got Unknown
		//IL_0164: Unknown result type (might be due to invalid IL or missing references)
		//IL_0168: Unknown result type (might be due to invalid IL or missing references)
		//IL_0169: Unknown result type (might be due to invalid IL or missing references)
		//IL_01b1: Unknown result type (might be due to invalid IL or missing references)
		//IL_01bb: Expected O, but got Unknown
		//IL_01f8: Unknown result type (might be due to invalid IL or missing references)
		//IL_0274: Unknown result type (might be due to invalid IL or missing references)
		//IL_026d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0282: Unknown result type (might be due to invalid IL or missing references)
		string text = Describe(spec);
		TypeDefinition type = module.GetType(spec.DeclaringType);
		if (type == null)
		{
			warn(text + ": skipped, type not found.");
			return false;
		}
		List<MethodDefinition> source = ((IEnumerable<MethodDefinition>)type.Methods).Where((MethodDefinition m) => ((MemberReference)m).Name == spec.Name).ToList();
		if (source.Any((MethodDefinition m) => Matches(m, spec.ShimParams, spec.ReturnType)))
		{
			info(text + ": already present, skipped.");
			return false;
		}
		MethodDefinition val = ((IEnumerable<MethodDefinition>)source).FirstOrDefault((Func<MethodDefinition, bool>)((MethodDefinition m) => Matches(m, spec.TargetParams, spec.ReturnType)));
		if (val == null)
		{
			warn(text + ": skipped, target overload (" + string.Join(", ", spec.TargetParams) + ") not found; game changed again?");
			return false;
		}
		if (((MethodReference)val).HasGenericParameters || val.IsAbstract || val.IsPInvokeImpl || !val.HasBody)
		{
			warn(text + ": skipped, target is not a plain method with a body.");
			return false;
		}
		int num = spec.TargetParams.Length - spec.ShimParams.Length;
		if (num < 0 || spec.ExtraArgs == null || spec.ExtraArgs.Length != num)
		{
			warn(text + ": skipped, bad table row (ExtraArgs count must equal TargetParams - ShimParams).");
			return false;
		}
		MethodAttributes val2 = (MethodAttributes)134;
		if (val.IsStatic)
		{
			val2 = (MethodAttributes)(val2 | 0x10);
		}
		MethodDefinition val3 = new MethodDefinition(spec.Name, val2, ((MethodReference)val).ReturnType);
		for (int num2 = 0; num2 < spec.ShimParams.Length; num2++)
		{
			ParameterDefinition val4 = ((MethodReference)val).Parameters[num2];
			((MethodReference)val3).Parameters.Add(new ParameterDefinition(((ParameterReference)val4).Name, (ParameterAttributes)0, ((ParameterReference)val4).ParameterType));
		}
		ILProcessor iLProcessor = val3.Body.GetILProcessor();
		val3.Body.InitLocals = false;
		if (!val.IsStatic)
		{
			iLProcessor.Emit(OpCodes.Ldarg_0);
		}
		for (int num3 = 0; num3 < spec.ShimParams.Length; num3++)
		{
			EmitLdarg(iLProcessor, num3 + ((!val.IsStatic) ? 1 : 0));
		}
		object[] extraArgs = spec.ExtraArgs;
		foreach (object value in extraArgs)
		{
			EmitConst(iLProcessor, value);
		}
		iLProcessor.Emit(val.IsVirtual ? OpCodes.Callvirt : OpCodes.Call, (MethodReference)(object)val);
		iLProcessor.Emit(OpCodes.Ret);
		type.Methods.Add(val3);
		info(text + ": ADDED forwarding overload -> " + ((MemberReference)val).FullName);
		return true;
	}

	private static bool Matches(MethodDefinition m, string[] paramTypes, string returnType)
	{
		if (((MemberReference)((MethodReference)m).ReturnType).FullName != returnType)
		{
			return false;
		}
		if (((MethodReference)m).Parameters.Count != paramTypes.Length)
		{
			return false;
		}
		for (int i = 0; i < paramTypes.Length; i++)
		{
			if (((MemberReference)((ParameterReference)((MethodReference)m).Parameters[i]).ParameterType).FullName != paramTypes[i])
			{
				return false;
			}
		}
		return true;
	}

	private static void EmitLdarg(ILProcessor il, int index)
	{
		//IL_0019: Unknown result type (might be due to invalid IL or missing references)
		//IL_0025: Unknown result type (might be due to invalid IL or missing references)
		//IL_0031: Unknown result type (might be due to invalid IL or missing references)
		//IL_003d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0049: Unknown result type (might be due to invalid IL or missing references)
		switch (index)
		{
		case 0:
			il.Emit(OpCodes.Ldarg_0);
			break;
		case 1:
			il.Emit(OpCodes.Ldarg_1);
			break;
		case 2:
			il.Emit(OpCodes.Ldarg_2);
			break;
		case 3:
			il.Emit(OpCodes.Ldarg_3);
			break;
		default:
			il.Emit(OpCodes.Ldarg_S, (byte)index);
			break;
		}
	}

	private static void EmitConst(ILProcessor il, object value)
	{
		//IL_0004: Unknown result type (might be due to invalid IL or missing references)
		//IL_003b: 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_0020: Unknown result type (might be due to invalid IL or missing references)
		//IL_0055: Unknown result type (might be due to invalid IL or missing references)
		if (value == null)
		{
			il.Emit(OpCodes.Ldnull);
			return;
		}
		if (value is bool)
		{
			il.Emit(((bool)value) ? OpCodes.Ldc_I4_1 : OpCodes.Ldc_I4_0);
			return;
		}
		if (value is int)
		{
			il.Emit(OpCodes.Ldc_I4, (int)value);
			return;
		}
		if (value is string)
		{
			il.Emit(OpCodes.Ldstr, (string)value);
			return;
		}
		throw new NotSupportedException("Unsupported constant type " + value.GetType());
	}

	private static string Describe(ShimSpec s)
	{
		return s.DeclaringType + "." + s.Name + "(" + string.Join(", ", s.ShimParams.Select((string p) => p.Substring(p.LastIndexOf('.') + 1)).ToArray()) + ")";
	}
}
internal sealed class ShimSpec
{
	public string DeclaringType;

	public string Name;

	public string[] ShimParams;

	public string[] TargetParams;

	public object[] ExtraArgs;

	public string ReturnType;

	public string Reason;
}
internal static class ShimTable
{
	public static readonly IList<ShimSpec> All = new List<ShimSpec>
	{
		new ShimSpec
		{
			DeclaringType = "MessageHud",
			Name = "ShowMessage",
			ShimParams = new string[5] { "MessageHud/MessageType", "System.String", "System.Int32", "UnityEngine.Sprite", "System.Boolean" },
			TargetParams = new string[6] { "MessageHud/MessageType", "System.String", "System.Int32", "UnityEngine.Sprite", "System.Boolean", "System.Boolean" },
			ExtraArgs = new object[1] { true },
			ReturnType = "System.Void",
			Reason = "ShowMessage gained a trailing 'bool log' parameter; old callers use the 5-parameter overload"
		},
		new ShimSpec
		{
			DeclaringType = "Character",
			Name = "Message",
			ShimParams = new string[4] { "MessageHud/MessageType", "System.String", "System.Int32", "UnityEngine.Sprite" },
			TargetParams = new string[5] { "MessageHud/MessageType", "System.String", "System.Int32", "UnityEngine.Sprite", "System.Boolean" },
			ExtraArgs = new object[1] { false },
			ReturnType = "System.Void",
			Reason = "Message gained a trailing 'bool log' parameter; old callers use the 4-parameter overload"
		}
	};
}