Decompiled source of Autodoors v1.0.0

winkio.autodoors.dll

Decompiled a year 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 System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETFramework,Version=v4.6", FrameworkDisplayName = "")]
[assembly: AssemblyCompany("winkio.autodoors")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyDescription("Plugin that automatically opens doors when the player approaches and closes them when the player leaves")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("winkio.autodoors")]
[assembly: AssemblyTitle("winkio.autodoors")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.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 AutoDoors
{
	[BepInPlugin("holdentorana74.autodoors", "Auto Doors", "1.1.0")]
	public class AutoDoorPlugin : BaseUnityPlugin
	{
		public static AutoDoorPlugin Instance { get; private set; }

		public static bool HasInstance => (Object)(object)Instance != (Object)null;

		public static bool IsRunning => (Object)(object)Instance != (Object)null && ((Behaviour)Instance).enabled;

		public static ManualLogSource InstanceLogger => ((BaseUnityPlugin)Instance).Logger;

		public PluginConfig Cfg { get; private set; } = new PluginConfig();


		public bool IsCrypt { get; set; }

		public bool IsActive => !Cfg.DisableInCrypt || !IsCrypt;

		public DateTime LastUpdate { get; set; }

		public List<TrackedDoor> TrackedDoors { get; private set; } = new List<TrackedDoor>();


		public List<int> AutoDoorIds { get; private set; } = new List<int>();


		public List<int> ManualDoorIds { get; private set; } = new List<int>();


		private void Awake()
		{
			((BaseUnityPlugin)this).Logger.LogInfo((object)("Plugin " + ((BaseUnityPlugin)this).Info.Metadata.GUID + " loading..."));
			Instance = this;
			Cfg.Reload();
			TrackedDoors.Clear();
			AutoDoorIds.Clear();
			ManualDoorIds.Clear();
			Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly(), (string)null);
			((BaseUnityPlugin)this).Logger.LogInfo((object)("Plugin " + ((BaseUnityPlugin)this).Info.Metadata.GUID + " loading complete!"));
		}
	}
	public class PluginConfig
	{
		private ConfigEntry<float> updateInterval;

		private ConfigEntry<bool> disableInCrypt;

		private ConfigEntry<float> interactDistance;

		public float UpdateInterval => updateInterval.Value;

		public bool DisableInCrypt => disableInCrypt.Value;

		public float InteractDistance => interactDistance.Value;

		public void Reload()
		{
			if (AutoDoorPlugin.HasInstance)
			{
				ConfigFile config = ((BaseUnityPlugin)AutoDoorPlugin.Instance).Config;
				updateInterval = config.Bind<float>("General", "updateInterval", 0.0625f, "Minimum interval between updates (s)");
				disableInCrypt = config.Bind<bool>("General", "disableInCrypt", true, "Disables auto doors inside crypts");
				interactDistance = config.Bind<float>("General", "interactDistance", 0.01f, "Interact Distance");
			}
		}
	}
	public class TrackedDoor
	{
		public int Id { get; private set; }

		public ZNetView ZNetView { get; private set; }

		public int State { get; private set; }

		public bool IsValid { get; private set; } = true;


		public bool InAutoRange { get; set; }

		public bool IsManual { get; set; }

		public bool IsAutoOpened { get; set; }

		public TrackedDoor(int id, ZNetView zNetView)
		{
			Id = id;
			ZNetView = zNetView;
			Update();
			IsManual = State != 0;
		}

		public bool Update()
		{
			Object val = Object.FindObjectFromInstanceID(Id);
			if (val != (Object)null)
			{
				Door val2 = (Door)(object)((val is Door) ? val : null);
				if (val2 != null)
				{
					ZNetView zNetView = ZNetView;
					ZDO val3 = ((zNetView != null) ? zNetView.GetZDO() : null);
					if (val3 != null)
					{
						IsValid = true;
						State = ZNetView.GetZDO().GetInt("state", 0);
					}
					else
					{
						IsValid = false;
					}
					goto IL_0084;
				}
			}
			IsValid = false;
			goto IL_0084;
			IL_0084:
			return IsValid;
		}

		public void SetState(int newState)
		{
			ZNetView zNetView = ZNetView;
			ZDO val = ((zNetView != null) ? zNetView.GetZDO() : null);
			if (val != null)
			{
				val.Set("state", newState);
			}
		}
	}
	public static class Utility
	{
	}
	public static class PluginInfo
	{
		public const string PLUGIN_GUID = "winkio.autodoors";

		public const string PLUGIN_NAME = "winkio.autodoors";

		public const string PLUGIN_VERSION = "1.0.0";
	}
}
namespace AutoDoors.GameClasses
{
	[HarmonyPatch(typeof(Door), "UpdateState")]
	public static class Door_UpdateState_Patch
	{
		private static void Postfix(ref Door __instance, ZNetView ___m_nview)
		{
			if (AutoDoorPlugin.IsRunning && AutoDoorPlugin.Instance.IsActive && (Object)(object)__instance.m_keyItem == (Object)null)
			{
				int id = ((Object)__instance).GetInstanceID();
				if (!AutoDoorPlugin.Instance.TrackedDoors.Any((TrackedDoor td) => td.Id == id))
				{
					AutoDoorPlugin.Instance.TrackedDoors.Add(new TrackedDoor(id, ___m_nview));
				}
			}
		}
	}
	[HarmonyPatch(typeof(EnvMan), "SetForceEnvironment")]
	public static class EnvMan_SetForceEnvironment_Patch
	{
		private static void Postfix(string ___m_forceEnv)
		{
			if (AutoDoorPlugin.IsRunning)
			{
				AutoDoorPlugin.Instance.IsCrypt = ___m_forceEnv.Contains("Crypt");
			}
		}
	}
	[HarmonyPatch(typeof(Player), "Update")]
	public static class Player_Update_Patch
	{
		private static void Postfix(Player __instance)
		{
			//IL_014e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0159: Unknown result type (might be due to invalid IL or missing references)
			//IL_015e: Unknown result type (might be due to invalid IL or missing references)
			if (!AutoDoorPlugin.IsRunning)
			{
				return;
			}
			Player localPlayer = Player.m_localPlayer;
			if ((Object)(object)localPlayer != (Object)(object)__instance)
			{
				return;
			}
			bool flag = (Object)(object)localPlayer != (Object)null && !((Character)localPlayer).IsDead();
			DateTime utcNow = DateTime.UtcNow;
			bool flag2 = (utcNow - AutoDoorPlugin.Instance.LastUpdate).TotalSeconds >= (double)AutoDoorPlugin.Instance.Cfg.UpdateInterval;
			if (!(AutoDoorPlugin.Instance.IsActive && flag && flag2))
			{
				return;
			}
			AutoDoorPlugin.Instance.LastUpdate = utcNow;
			float interactDistance = AutoDoorPlugin.Instance.Cfg.InteractDistance;
			float num = interactDistance * interactDistance;
			AutoDoorPlugin.Instance.TrackedDoors.RemoveAll((TrackedDoor td) => !td.IsValid);
			foreach (TrackedDoor trackedDoor in AutoDoorPlugin.Instance.TrackedDoors)
			{
				if (!trackedDoor.Update())
				{
					continue;
				}
				Object val = Object.FindObjectFromInstanceID(trackedDoor.Id);
				Door val2 = (Door)(object)((val is Door) ? val : null);
				if (val2 == null)
				{
					continue;
				}
				bool inAutoRange = trackedDoor.InAutoRange;
				float num2 = Vector3.SqrMagnitude(((Component)val2).transform.position - ((Component)localPlayer).transform.position);
				trackedDoor.InAutoRange = num2 <= num;
				if (trackedDoor.InAutoRange)
				{
					if (trackedDoor.IsManual)
					{
						continue;
					}
					if (trackedDoor.State == 0)
					{
						if (!trackedDoor.IsAutoOpened)
						{
							val2.Interact((Humanoid)(object)localPlayer, false, false);
						}
						else
						{
							trackedDoor.IsManual = true;
						}
					}
					else if (!inAutoRange)
					{
						trackedDoor.IsManual = true;
					}
					else
					{
						trackedDoor.IsAutoOpened = true;
					}
				}
				else if (inAutoRange)
				{
					if (!trackedDoor.IsManual)
					{
						trackedDoor.SetState(0);
					}
					else
					{
						trackedDoor.IsManual = false;
					}
					trackedDoor.IsAutoOpened = false;
				}
			}
		}
	}
}