Decompiled source of Rowes Double Gates v3.0.0

DoubleGates/DoubleGates.dll

Decompiled a month ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
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.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyCompany("DoubleGates")]
[assembly: AssemblyConfiguration("release40")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("DoubleGates")]
[assembly: AssemblyTitle("DoubleGates")]
[assembly: AssemblyVersion("1.0.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 DoubleGates
{
	[BepInPlugin("rowe.valheim.doublegates", "Rowe's Double Gates", "2.0")]
	public class DoubleGatesPlugin : BaseUnityPlugin
	{
		private void Awake()
		{
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_000c: Expected O, but got Unknown
			Harmony val = new Harmony("ru.rowes.valheim.doublegates");
			val.PatchAll();
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Double Gates loaded");
		}
	}
	[HarmonyPatch(typeof(Door), "Interact")]
	public static class DoorPatch
	{
		private const float MaxPairDistance = 7f;

		private const float MaxHeightDifference = 0.4f;

		private static bool _busy;

		private static readonly Dictionary<Door, Door> PairCache = new Dictionary<Door, Door>();

		private static readonly HashSet<Door> NoPairCache = new HashSet<Door>();

		private static readonly FieldInfo NViewField = AccessTools.Field(typeof(Door), "m_nview");

		private static Door _pendingPair;

		[HarmonyPrefix]
		private static void Prefix(Door __instance, bool hold)
		{
			//IL_0032: 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)
			_pendingPair = null;
			if (_busy || hold)
			{
				return;
			}
			Door val = FindPair(__instance);
			if ((Object)(object)val == (Object)null)
			{
				return;
			}
			float num = Vector3.Dot(((Component)__instance).transform.forward, ((Component)val).transform.forward);
			if (num < -0.8f)
			{
				string reason;
				int state = GetState(__instance, out reason);
				string reason2;
				int state2 = GetState(val, out reason2);
				bool flag = state != 0;
				bool flag2 = state2 != 0;
				if (flag == flag2)
				{
					_pendingPair = val;
				}
			}
		}

		private static void Postfix(Door __instance, Humanoid character, bool hold, bool alt)
		{
			if (_busy || hold)
			{
				return;
			}
			Door pendingPair = _pendingPair;
			_pendingPair = null;
			if ((Object)(object)pendingPair == (Object)null)
			{
				return;
			}
			try
			{
				_busy = true;
				pendingPair.Interact(character, false, alt);
			}
			finally
			{
				_busy = false;
			}
		}

		private static int GetState(Door door, out string reason)
		{
			if ((Object)(object)door == (Object)null)
			{
				reason = "door==null";
				return -1;
			}
			object? value = NViewField.GetValue(door);
			ZNetView val = (ZNetView)((value is ZNetView) ? value : null);
			if ((Object)(object)val == (Object)null)
			{
				reason = "nview==null";
				return -1;
			}
			if (!val.IsValid())
			{
				reason = "nview.IsValid()==false";
				return -1;
			}
			ZDO zDO = val.GetZDO();
			if (zDO == null)
			{
				reason = "zdo==null";
				return -1;
			}
			reason = "ok";
			return zDO.GetInt("state", 0);
		}

		private static Door FindPair(Door source)
		{
			//IL_0064: Unknown result type (might be due to invalid IL or missing references)
			//IL_0069: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e3: 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)
			if (PairCache.TryGetValue(source, out var value))
			{
				if (IsValid(value))
				{
					return value;
				}
				PairCache.Remove(source);
			}
			if (NoPairCache.Contains(source))
			{
				return null;
			}
			Door[] array = Object.FindObjectsByType<Door>((FindObjectsSortMode)0);
			Door val = null;
			float num = float.MaxValue;
			Vector3 position = ((Component)source).transform.position;
			foreach (Door val2 in array)
			{
				if (IsValid(val2) && !((Object)(object)val2 == (Object)(object)source) && !(((object)val2).GetType() != ((object)source).GetType()))
				{
					Vector3 position2 = ((Component)val2).transform.position;
					float num2 = Vector3.Distance(position, position2);
					if (!(num2 > 7f) && !(Mathf.Abs(position.y - position2.y) > 0.4f) && num2 < num)
					{
						num = num2;
						val = val2;
					}
				}
			}
			if ((Object)(object)val != (Object)null)
			{
				PairCache[source] = val;
				PairCache[val] = source;
			}
			else
			{
				NoPairCache.Add(source);
			}
			return val;
		}

		private static bool IsValid(Door d)
		{
			return (Object)(object)d != (Object)null && Object.op_Implicit((Object)(object)d);
		}
	}
}