Decompiled source of LegacyDunGenFix v0.1.0

BepInEx/patchers/LegacyDunGenFix.dll

Decompiled 2 hours ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Logging;
using Microsoft.CodeAnalysis;
using Mono.Cecil;
using Mono.Collections.Generic;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("LegacyDunGenFix")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyDescription("Lets mods and asset bundles built before the game moved DunGen into DunGen.dll load again (fixes the SCP Foundation Dungeon infinite load).")]
[assembly: AssemblyFileVersion("0.1.0.0")]
[assembly: AssemblyInformationalVersion("0.1.0+16c075f6f4cfce48c86c16a0d303d7979c7c76f1")]
[assembly: AssemblyProduct("LegacyDunGenFix")]
[assembly: AssemblyTitle("LegacyDunGenFix")]
[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 LegacyDunGenFix
{
	public static class Patcher
	{
		private const string Target = "Assembly-CSharp.dll";

		private static readonly string[] SourceAssemblies = new string[3] { "DunGen", "DunGen.Integration.UnityNav", "DunGen.Integration.ASPP" };

		private static readonly ManualLogSource Log = Logger.CreateLogSource("LegacyDunGenFix");

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

		public static void Patch(AssemblyDefinition assembly)
		{
			try
			{
				int num = AddForwarders(assembly.MainModule, Paths.ManagedPath);
				Log.LogInfo((object)string.Format("{0} {1}: forwarded {2} DunGen types from {3}.", "LegacyDunGenFix", "0.1.0", num, "Assembly-CSharp.dll"));
			}
			catch (Exception arg)
			{
				Log.LogError((object)string.Format("{0}: could not add DunGen type forwarders. {1}", "LegacyDunGenFix", arg));
			}
		}

		internal static int AddForwarders(ModuleDefinition module, string managedDir)
		{
			//IL_000c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			//IL_0048: Unknown result type (might be due to invalid IL or missing references)
			//IL_004d: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fa: Unknown result type (might be due to invalid IL or missing references)
			//IL_0139: Unknown result type (might be due to invalid IL or missing references)
			//IL_013e: Unknown result type (might be due to invalid IL or missing references)
			//IL_014b: Expected O, but got Unknown
			HashSet<string> hashSet = new HashSet<string>();
			Enumerator<TypeDefinition> enumerator = module.Types.GetEnumerator();
			try
			{
				while (enumerator.MoveNext())
				{
					TypeDefinition current = enumerator.Current;
					hashSet.Add(((MemberReference)current).FullName);
				}
			}
			finally
			{
				((IDisposable)enumerator/*cast due to .constrained prefix*/).Dispose();
			}
			Enumerator<ExportedType> enumerator2 = module.ExportedTypes.GetEnumerator();
			try
			{
				while (enumerator2.MoveNext())
				{
					ExportedType current2 = enumerator2.Current;
					hashSet.Add(current2.FullName);
				}
			}
			finally
			{
				((IDisposable)enumerator2/*cast due to .constrained prefix*/).Dispose();
			}
			int num = 0;
			string[] sourceAssemblies = SourceAssemblies;
			foreach (string text in sourceAssemblies)
			{
				string text2 = Path.Combine(managedDir, text + ".dll");
				if (!File.Exists(text2))
				{
					Log.LogDebug((object)(text + ".dll not found in the game's Managed folder; skipped."));
					continue;
				}
				AssemblyDefinition val = AssemblyDefinition.ReadAssembly(text2);
				try
				{
					AssemblyNameReference orAddReference = GetOrAddReference(module, val.Name);
					enumerator = val.MainModule.Types.GetEnumerator();
					try
					{
						while (enumerator.MoveNext())
						{
							TypeDefinition current3 = enumerator.Current;
							if (!((MemberReference)current3).Name.StartsWith("<") && !hashSet.Contains(((MemberReference)current3).FullName))
							{
								ExportedType val2 = new ExportedType(((TypeReference)current3).Namespace, ((MemberReference)current3).Name, module, (IMetadataScope)(object)orAddReference)
								{
									Attributes = (TypeAttributes)2097152
								};
								module.ExportedTypes.Add(val2);
								hashSet.Add(((MemberReference)current3).FullName);
								num++;
								num += AddNested(module, orAddReference, current3, val2);
							}
						}
					}
					finally
					{
						((IDisposable)enumerator/*cast due to .constrained prefix*/).Dispose();
					}
				}
				finally
				{
					((IDisposable)val)?.Dispose();
				}
			}
			return num;
		}

		private static int AddNested(ModuleDefinition module, AssemblyNameReference reference, TypeDefinition parent, ExportedType parentForwarder)
		{
			//IL_0008: Unknown result type (might be due to invalid IL or missing references)
			//IL_000d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0038: 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_0045: Expected O, but got Unknown
			int num = 0;
			Enumerator<TypeDefinition> enumerator = parent.NestedTypes.GetEnumerator();
			try
			{
				while (enumerator.MoveNext())
				{
					TypeDefinition current = enumerator.Current;
					if (!((MemberReference)current).Name.StartsWith("<"))
					{
						ExportedType val = new ExportedType(((TypeReference)current).Namespace, ((MemberReference)current).Name, module, (IMetadataScope)(object)reference)
						{
							DeclaringType = parentForwarder
						};
						module.ExportedTypes.Add(val);
						num++;
						num += AddNested(module, reference, current, val);
					}
				}
				return num;
			}
			finally
			{
				((IDisposable)enumerator/*cast due to .constrained prefix*/).Dispose();
			}
		}

		private static AssemblyNameReference GetOrAddReference(ModuleDefinition module, AssemblyNameDefinition name)
		{
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_000b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0052: Unknown result type (might be due to invalid IL or missing references)
			//IL_0058: Expected O, but got Unknown
			Enumerator<AssemblyNameReference> enumerator = module.AssemblyReferences.GetEnumerator();
			try
			{
				while (enumerator.MoveNext())
				{
					AssemblyNameReference current = enumerator.Current;
					if (current.Name == ((AssemblyNameReference)name).Name)
					{
						return current;
					}
				}
			}
			finally
			{
				((IDisposable)enumerator/*cast due to .constrained prefix*/).Dispose();
			}
			AssemblyNameReference val = new AssemblyNameReference(((AssemblyNameReference)name).Name, ((AssemblyNameReference)name).Version);
			module.AssemblyReferences.Add(val);
			return val;
		}
	}
	public static class PluginInfo
	{
		public const string GUID = "Haaylo.LegacyDunGenFix";

		public const string NAME = "LegacyDunGenFix";

		public const string VERSION = "0.1.0";
	}
}