Decompiled source of InstantDoors v1.0.0

BepInEx/plugins/InstantDoors/InstantDoors.dll

Decompiled 2 hours ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;

[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("InstantDoors")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("InstantDoors")]
[assembly: AssemblyTitle("InstantDoors")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace InstantDoors;

[BepInPlugin("com.nova.ultrakill.instantdoors", "ULTRAKILL Instant Doors", "1.0.0")]
public sealed class InstantDoorsPlugin : BaseUnityPlugin
{
	internal static ManualLogSource Log;

	private void Awake()
	{
		//IL_0010: Unknown result type (might be due to invalid IL or missing references)
		//IL_0016: Expected O, but got Unknown
		Log = ((BaseUnityPlugin)this).Logger;
		Harmony harmony = new Harmony("com.nova.ultrakill.instantdoors");
		int num = 0;
		num += PatchOpen(harmony, "Door", 2, null, "DoorPostfix");
		num += PatchOpen(harmony, "BigDoor", 0, "BigDoorPrefix", "BigDoorPostfix");
		num += PatchOpen(harmony, "FinalDoor", 0, null, "FinalDoorPostfix");
		((BaseUnityPlugin)this).Logger.LogInfo((object)("Instant Doors loaded. Installed " + num + " door patch(es)."));
	}

	private int PatchOpen(Harmony harmony, string shortTypeName, int parameterCount, string prefixName, string postfixName)
	{
		//IL_00fb: Unknown result type (might be due to invalid IL or missing references)
		//IL_010d: Unknown result type (might be due to invalid IL or missing references)
		Type type = ReflectionHelpers.FindType(shortTypeName);
		if (type == null)
		{
			((BaseUnityPlugin)this).Logger.LogWarning((object)("Could not find game type: " + shortTypeName));
			return 0;
		}
		MethodInfo methodInfo = null;
		MethodInfo[] methods = type.GetMethods(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
		foreach (MethodInfo methodInfo2 in methods)
		{
			if (methodInfo2.Name == "Open" && methodInfo2.GetParameters().Length == parameterCount)
			{
				methodInfo = methodInfo2;
				break;
			}
		}
		if (methodInfo == null)
		{
			((BaseUnityPlugin)this).Logger.LogWarning((object)("Could not find " + shortTypeName + ".Open with " + parameterCount + " parameter(s)."));
			return 0;
		}
		MethodInfo methodInfo3 = ((prefixName == null) ? null : typeof(DoorPatches).GetMethod(prefixName, BindingFlags.Static | BindingFlags.Public));
		MethodInfo methodInfo4 = ((postfixName == null) ? null : typeof(DoorPatches).GetMethod(postfixName, BindingFlags.Static | BindingFlags.Public));
		harmony.Patch((MethodBase)methodInfo, (methodInfo3 == null) ? ((HarmonyMethod)null) : new HarmonyMethod(methodInfo3), (methodInfo4 == null) ? ((HarmonyMethod)null) : new HarmonyMethod(methodInfo4), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
		((BaseUnityPlugin)this).Logger.LogInfo((object)("Patched " + type.FullName + "." + methodInfo.Name));
		return 1;
	}
}
internal static class DoorPatches
{
	public static void DoorPostfix(object __instance)
	{
		if (!ReflectionHelpers.GetBool(__instance, "inPos", fallback: true) && ReflectionHelpers.GetBool(__instance, "open", fallback: false))
		{
			object member = ReflectionHelpers.GetMember(__instance, "targetPos");
			object member2 = ReflectionHelpers.GetMember(__instance, "transform");
			if (member != null && member2 != null && ReflectionHelpers.SetMember(member2, "localPosition", member))
			{
				InstantDoorsPlugin.Log.LogDebug((object)"Snapped a normal door to its open position.");
			}
		}
	}

	public static void BigDoorPrefix(object __instance, out bool __state)
	{
		__state = ReflectionHelpers.GetBool(__instance, "open", fallback: false);
	}

	public static void BigDoorPostfix(object __instance, bool __state)
	{
		if (__state || !ReflectionHelpers.GetBool(__instance, "open", fallback: false))
		{
			return;
		}
		object member = ReflectionHelpers.GetMember(__instance, "targetOpenRotation");
		object member2 = ReflectionHelpers.GetMember(__instance, "transform");
		object member3 = ReflectionHelpers.GetMember(__instance, "controller");
		if (member != null && member2 != null)
		{
			ReflectionHelpers.SetMember(member2, "localRotation", member);
			if (member3 != null)
			{
				ReflectionHelpers.InvokeNoArg(ReflectionHelpers.GetMember(member3, "onFullyOpened"));
				ReflectionHelpers.SetMember(member3, "isFullyOpened", true);
			}
			InstantDoorsPlugin.Log.LogDebug((object)"Snapped a big door to its open rotation.");
		}
	}

	public static void FinalDoorPostfix(object __instance)
	{
		ReflectionHelpers.InvokeNoArg(__instance, "OpenDoors");
	}
}
internal static class ReflectionHelpers
{
	private const BindingFlags Flags = BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;

	public static Type FindType(string shortName)
	{
		Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
		foreach (Assembly assembly in assemblies)
		{
			Type[] types;
			try
			{
				types = assembly.GetTypes();
			}
			catch (ReflectionTypeLoadException ex)
			{
				types = ex.Types;
			}
			catch
			{
				continue;
			}
			if (types == null)
			{
				continue;
			}
			Type[] array = types;
			foreach (Type type in array)
			{
				if (type != null && type.Name == shortName)
				{
					return type;
				}
			}
		}
		return null;
	}

	public static object GetMember(object instance, string name)
	{
		if (instance == null)
		{
			return null;
		}
		Type type = instance.GetType();
		while (type != null)
		{
			FieldInfo field = type.GetField(name, BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
			if (field != null)
			{
				return field.GetValue(instance);
			}
			PropertyInfo property = type.GetProperty(name, BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
			if (property != null && property.GetIndexParameters().Length == 0)
			{
				return property.GetValue(instance, null);
			}
			type = type.BaseType;
		}
		return null;
	}

	public static bool SetMember(object instance, string name, object value)
	{
		if (instance == null || value == null)
		{
			return false;
		}
		Type type = instance.GetType();
		while (type != null)
		{
			FieldInfo field = type.GetField(name, BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
			if (field != null)
			{
				field.SetValue(instance, value);
				return true;
			}
			PropertyInfo property = type.GetProperty(name, BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
			if (property != null && property.CanWrite)
			{
				property.SetValue(instance, value, null);
				return true;
			}
			type = type.BaseType;
		}
		return false;
	}

	public static bool GetBool(object instance, string name, bool fallback)
	{
		object member = GetMember(instance, name);
		if (!(member is bool))
		{
			return fallback;
		}
		return (bool)member;
	}

	public static void InvokeNoArg(object instance, string methodName)
	{
		if (instance != null)
		{
			MethodInfo method = instance.GetType().GetMethod(methodName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, Type.EmptyTypes, null);
			if (method != null)
			{
				method.Invoke(instance, null);
			}
		}
	}

	public static void InvokeNoArg(object eventObject)
	{
		if (eventObject != null)
		{
			MethodInfo method = eventObject.GetType().GetMethod("Invoke", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, Type.EmptyTypes, null);
			if (method != null)
			{
				method.Invoke(eventObject, null);
			}
		}
	}
}