Decompiled source of World Fixes v1.0.0

patchers/WorldFixes.Patcher.dll

Decompiled an hour ago
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Security.Cryptography;
using BepInEx;
using BepInEx.Logging;
using Mono.Cecil;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: AssemblyVersion("0.0.0.0")]
namespace Frank.WorldFixes;

public static class Patcher
{
	public static IEnumerable<string> TargetDLLs => new string[0];

	public static void Patch(AssemblyDefinition assembly)
	{
	}

	public static void Initialize()
	{
		ManualLogSource val = Logger.CreateLogSource("World Fixes");
		try
		{
			string[] files = Directory.GetFiles(Paths.PluginPath, "WesleysInteriorShenanigans.dll", SearchOption.AllDirectories);
			if (files.Length == 0)
			{
				val.LogInfo((object)"Wesley helper not installed; nothing to repair.");
				return;
			}
			if (files.Length != 1)
			{
				val.LogWarning((object)"Multiple Wesley helper DLLs found. Remove duplicate installations; no files changed.");
				return;
			}
			string text = WesleyRepair.Apply(files[0], Paths.ManagedPath, Path.Combine(Paths.BepInExRootPath, "cache", "WorldFixes"));
			val.LogInfo((object)text);
		}
		catch (Exception ex)
		{
			val.LogError((object)("Wesley repair could not be applied: " + ex));
		}
	}
}
public static class WesleyRepair
{
	public const string OriginalHash = "67BD403ADAF18F19C12E362B7EB43E0012F6E3BA101681F44CD0926A8F543F53";

	public const string RepairedHash = "FB742025DEA576A998AF504F98AB5E7534B3BA43C1024950B50DBDF95B61FA30";

	public static string Hash(byte[] bytes)
	{
		using SHA256 sHA = SHA256.Create();
		return BitConverter.ToString(sHA.ComputeHash(bytes)).Replace("-", "");
	}

	public static string Apply(string file, string managed, string backups)
	{
		//IL_0034: Unknown result type (might be due to invalid IL or missing references)
		//IL_003a: Expected O, but got Unknown
		//IL_0041: Unknown result type (might be due to invalid IL or missing references)
		//IL_0048: Expected O, but got Unknown
		byte[] array = File.ReadAllBytes(file);
		string text = Hash(array);
		if (text == "FB742025DEA576A998AF504F98AB5E7534B3BA43C1024950B50DBDF95B61FA30")
		{
			return "Wesley DunGen repair already present; no duplicate plugin created.";
		}
		if (text != "67BD403ADAF18F19C12E362B7EB43E0012F6E3BA101681F44CD0926A8F543F53")
		{
			return "Unrecognized Wesley helper version; skipped without changes. This release supports the helper shipped with WesleysInteriors 5.0.19.";
		}
		DefaultAssemblyResolver val = new DefaultAssemblyResolver();
		byte[] bytes;
		try
		{
			((BaseAssemblyResolver)val).AddSearchDirectory(managed);
			ReaderParameters val2 = new ReaderParameters();
			val2.AssemblyResolver = (IAssemblyResolver)(object)val;
			ReaderParameters val3 = val2;
			AssemblyDefinition val4 = AssemblyDefinition.ReadAssembly(Path.Combine(managed, "DunGen.dll"), val3);
			try
			{
				AssemblyDefinition val5 = AssemblyDefinition.ReadAssembly((Stream)new MemoryStream(array), val3);
				try
				{
					AssemblyNameReference val6 = AssemblyNameReference.Parse(((AssemblyNameReference)val4.Name).FullName);
					val5.MainModule.AssemblyReferences.Add(val6);
					TypeReference[] array2 = (from t in val5.MainModule.GetTypeReferences()
						where ((MemberReference)t).FullName.StartsWith("DunGen.", StringComparison.Ordinal)
						select t).ToArray();
					if (array2.Length != 8)
					{
						throw new InvalidDataException("Unexpected DunGen reference count.");
					}
					TypeReference[] array3 = array2;
					foreach (TypeReference val7 in array3)
					{
						if (((MemberReference)val7).DeclaringType == null)
						{
							val7.Scope = (IMetadataScope)(object)val6;
						}
					}
					TypeReference[] array4 = array2;
					foreach (TypeReference val8 in array4)
					{
						TypeDefinition val9 = val8.Resolve();
						if (val9 == null || ((AssemblyNameReference)((MemberReference)val9).Module.Assembly.Name).Name != "DunGen")
						{
							throw new InvalidDataException("Unresolved type: " + ((MemberReference)val8).FullName);
						}
					}
					foreach (MemberReference item in from m in val5.MainModule.GetMemberReferences()
						where ((MemberReference)m.DeclaringType).FullName.StartsWith("DunGen.", StringComparison.Ordinal)
						select m)
					{
						MethodReference val10 = (MethodReference)(object)((item is MethodReference) ? item : null);
						FieldReference val11 = (FieldReference)(object)((item is FieldReference) ? item : null);
						if ((val10 != null && val10.Resolve() == null) || (val11 != null && val11.Resolve() == null))
						{
							throw new InvalidDataException("Unresolved member: " + item.FullName);
						}
					}
					using MemoryStream memoryStream = new MemoryStream();
					val5.Write((Stream)memoryStream);
					bytes = memoryStream.ToArray();
				}
				finally
				{
					((IDisposable)val5)?.Dispose();
				}
			}
			finally
			{
				((IDisposable)val4)?.Dispose();
			}
		}
		finally
		{
			((IDisposable)val)?.Dispose();
		}
		if (Hash(bytes) != "FB742025DEA576A998AF504F98AB5E7534B3BA43C1024950B50DBDF95B61FA30")
		{
			throw new InvalidDataException("Output differs from the validated repair; original left unchanged.");
		}
		Directory.CreateDirectory(backups);
		string path = Path.Combine(backups, "WesleysInteriorShenanigans.67BD403ADAF18F19C12E362B7EB43E0012F6E3BA101681F44CD0926A8F543F53.bak");
		if (!File.Exists(path))
		{
			File.WriteAllBytes(path, array);
		}
		if (Hash(File.ReadAllBytes(path)) != "67BD403ADAF18F19C12E362B7EB43E0012F6E3BA101681F44CD0926A8F543F53")
		{
			throw new InvalidDataException("Original backup verification failed.");
		}
		string text2 = file + ".worldfixes-" + Guid.NewGuid().ToString("N") + ".tmp";
		try
		{
			File.WriteAllBytes(text2, bytes);
			if (Hash(File.ReadAllBytes(file)) != "67BD403ADAF18F19C12E362B7EB43E0012F6E3BA101681F44CD0926A8F543F53")
			{
				throw new IOException("Wesley helper changed during repair; refusing replacement.");
			}
			File.Replace(text2, file, null);
		}
		finally
		{
			if (File.Exists(text2))
			{
				File.Delete(text2);
			}
		}
		return "Applied validated Wesley DunGen repair. Original Armory/Citadel connection rules preserved; original backed up in BepInEx/cache/WorldFixes.";
	}
}