Decompiled source of ValheimApiCompat v0.1.0

patchers/SansaraValheimApiCompat.dll

Decompiled 5 days 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 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.6.2", FrameworkDisplayName = ".NET Framework 4.6.2")]
[assembly: AssemblyCompany("SansaraValheimApiCompat")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("0.1.0.0")]
[assembly: AssemblyInformationalVersion("0.1.0+b497f2598de479c169daea3e792bd8aa9ba5955e")]
[assembly: AssemblyProduct("SansaraValheimApiCompat")]
[assembly: AssemblyTitle("SansaraValheimApiCompat")]
[assembly: AssemblyVersion("0.1.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;
		}
	}
}
namespace Sansara.ValheimApiCompat
{
	public static class Patcher
	{
		public const string Version = "0.1.0";

		public static IEnumerable<string> TargetDLLs => new string[2] { "SoftReferenceableAssets.dll", "assembly_valheim.dll" };

		public static void Patch(AssemblyDefinition assembly)
		{
			string name = ((AssemblyNameReference)assembly.Name).Name;
			bool flag;
			if (!(name == "SoftReferenceableAssets"))
			{
				if (!(name == "assembly_valheim"))
				{
					throw new NotSupportedException("Unexpected assembly: " + ((AssemblyNameReference)assembly.Name).Name);
				}
				flag = RestoreSetSector(assembly.MainModule);
			}
			else
			{
				flag = RestoreName(assembly.MainModule);
			}
			Console.WriteLine("[SansaraValheimApiCompat 0.1.0] " + ((AssemblyNameReference)assembly.Name).Name + (flag ? ": legacy API restored" : ": legacy API already present"));
		}

		private static bool RestoreName(ModuleDefinition module)
		{
			//IL_0075: Unknown result type (might be due to invalid IL or missing references)
			//IL_007b: Expected O, but got Unknown
			//IL_009e: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ab: Expected O, but got Unknown
			//IL_00c0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c7: Expected O, but got Unknown
			//IL_00d4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00df: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ea: Unknown result type (might be due to invalid IL or missing references)
			//IL_0118: Unknown result type (might be due to invalid IL or missing references)
			//IL_011d: Unknown result type (might be due to invalid IL or missing references)
			//IL_012a: Expected O, but got Unknown
			TypeDefinition val = module.GetType("SoftReferenceableAssets.SoftReference`1") ?? throw new InvalidOperationException("SoftReference<T> is missing; unsupported game API.");
			if (((IEnumerable<MethodDefinition>)val.Methods).Any((MethodDefinition m) => ((MemberReference)m).Name == "get_Name" && !((MethodReference)m).HasParameters && ((MemberReference)((MethodReference)m).ReturnType).FullName == "System.String"))
			{
				return false;
			}
			MethodDefinition val2 = ((IEnumerable<MethodDefinition>)val.Methods).Single((MethodDefinition m) => ((MemberReference)m).Name == "get_name" && !((MethodReference)m).HasParameters && ((MethodReference)m).HasThis && ((MemberReference)((MethodReference)m).ReturnType).FullName == "System.String");
			GenericInstanceType val3 = new GenericInstanceType((TypeReference)(object)val);
			val3.GenericArguments.Add((TypeReference)(object)((IEnumerable<GenericParameter>)((TypeReference)val).GenericParameters).Single());
			MethodReference val4 = new MethodReference(((MemberReference)val2).Name, ((MethodReference)val2).ReturnType, (TypeReference)(object)val3)
			{
				HasThis = true
			};
			MethodDefinition val5 = new MethodDefinition("get_Name", (MethodAttributes)2182, module.TypeSystem.String);
			ILProcessor iLProcessor = val5.Body.GetILProcessor();
			iLProcessor.Emit(OpCodes.Ldarg_0);
			iLProcessor.Emit(OpCodes.Call, val4);
			iLProcessor.Emit(OpCodes.Ret);
			val.Methods.Add(val5);
			val.Properties.Add(new PropertyDefinition("Name", (PropertyAttributes)0, module.TypeSystem.String)
			{
				GetMethod = val5
			});
			return true;
		}

		private static bool RestoreSetSector(ModuleDefinition module)
		{
			//IL_00b4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ba: Expected O, but got Unknown
			//IL_00d7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e1: Expected O, but got Unknown
			//IL_00ed: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f8: Unknown result type (might be due to invalid IL or missing references)
			//IL_0103: Unknown result type (might be due to invalid IL or missing references)
			//IL_010e: Unknown result type (might be due to invalid IL or missing references)
			//IL_011a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0125: Unknown result type (might be due to invalid IL or missing references)
			TypeDefinition val = module.GetType("ZDO") ?? throw new InvalidOperationException("ZDO is missing; unsupported game API.");
			if (((IEnumerable<MethodDefinition>)val.Methods).Any((MethodDefinition m) => ((MemberReference)m).Name == "SetSector" && ((MethodReference)m).Parameters.Count == 1 && ((MemberReference)((ParameterReference)((MethodReference)m).Parameters[0]).ParameterType).FullName == "ZoneSystem/SectorIndex"))
			{
				return false;
			}
			MethodDefinition val2 = ((IEnumerable<MethodDefinition>)val.Methods).Single((MethodDefinition m) => ((MemberReference)m).Name == "SetSector" && ((MethodReference)m).HasThis && ((MemberReference)((MethodReference)m).ReturnType).FullName == "System.Void" && ((MethodReference)m).Parameters.Count == 2 && ((IEnumerable<ParameterDefinition>)((MethodReference)m).Parameters).All((ParameterDefinition p) => ((MemberReference)((ParameterReference)p).ParameterType).FullName == "ZoneSystem/SectorIndex"));
			MethodDefinition val3 = ((IEnumerable<MethodDefinition>)val.Methods).Single((MethodDefinition m) => ((MemberReference)m).Name == "GetSectorIndex" && ((MethodReference)m).HasThis && !((MethodReference)m).HasParameters && ((MemberReference)((MethodReference)m).ReturnType).FullName == "ZoneSystem/SectorIndex");
			MethodDefinition val4 = new MethodDefinition("SetSector", (MethodAttributes)134, module.TypeSystem.Void);
			((MethodReference)val4).Parameters.Add(new ParameterDefinition("sectorIndex", (ParameterAttributes)0, ((ParameterReference)((MethodReference)val2).Parameters[0]).ParameterType));
			ILProcessor iLProcessor = val4.Body.GetILProcessor();
			iLProcessor.Emit(OpCodes.Ldarg_0);
			iLProcessor.Emit(OpCodes.Ldarg_1);
			iLProcessor.Emit(OpCodes.Ldarg_0);
			iLProcessor.Emit(OpCodes.Call, (MethodReference)(object)val3);
			iLProcessor.Emit(OpCodes.Call, (MethodReference)(object)val2);
			iLProcessor.Emit(OpCodes.Ret);
			val.Methods.Add(val4);
			return true;
		}
	}
}