Decompiled source of ServersideQoL AutoDoors v2.5.1

ServersideQoL.AutoDoors.dll

Decompiled 4 days ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using System.Text;
using BepInEx;
using BepInEx.Configuration;
using Microsoft.CodeAnalysis;
using ServersideQoL.Utilities;
using UnityEngine;

[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("ArgusMagnus")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyCopyright("ArgusMagnus")]
[assembly: AssemblyDescription("Automatically closes doors")]
[assembly: AssemblyFileVersion("2.5.1.0")]
[assembly: AssemblyInformationalVersion("2.5.1+04def6c26ce1cbdb190c3998d27901a2c26eb789")]
[assembly: AssemblyProduct("ServersideQoL.AutoDoors")]
[assembly: AssemblyTitle("ServersideQoL.AutoDoors")]
[assembly: AssemblyMetadata("RepositoryUrl", "https://github.com/ArgusMagnus/ValheimServersideQoL")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("2.5.1.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableAttribute : Attribute
	{
		public readonly byte[] NullableFlags;

		public NullableAttribute(byte P_0)
		{
			NullableFlags = new byte[1] { P_0 };
		}

		public NullableAttribute(byte[] P_0)
		{
			NullableFlags = P_0;
		}
	}
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableContextAttribute : Attribute
	{
		public readonly byte Flag;

		public NullableContextAttribute(byte P_0)
		{
			Flag = P_0;
		}
	}
	[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 ServersideQoL.AutoDoors
{
	[BepInPlugin("ArgusMagnus.ServersideQoL.AutoDoors", "ServersideQoL.AutoDoors", "2.5.1")]
	public sealed class AutoDoorsPlugin : ServersideQoLPluginBase<AutoDoorsPlugin, Config>
	{
		public const string Author = "ArgusMagnus";

		public const string PluginName = "ServersideQoL.AutoDoors";

		public const string PluginGuid = "ArgusMagnus.ServersideQoL.AutoDoors";

		public const string PluginVersion = "2.5.1";

		public const string PluginInformationalVersion = "2.5.1";

		private DateTimeOffset BuildTimestamp { get; } = new DateTimeOffset(639287675791581842L, default(TimeSpan));

		protected override Config CreateConfigSingleton(ConfigFile configFile, Logger logger)
		{
			return new Config(configFile, logger);
		}

		protected override void RegisterProcessors(IProcessorCollection processors)
		{
			processors.Add<DoorProcessor>();
		}

		private void Awake()
		{
		}
	}
	public sealed class Config : ConfigBase<Config>
	{
		public override ConfigEntry<bool> Enabled { get; } = ConfigBase<Config>.BindEx<bool>(cfg, true, "Enables/disables the entire mod", (IAcceptableValueBase<bool>)null, (Deprecated<Config>)null, "Enabled");

		public ConfigEntry<float> AutoCloseMinPlayerDistance { get; } = ConfigBase<Config>.BindEx<float>(cfg, 4f, FormattableString.Invariant($"Minimum distance all players must have to the door before it is closed."), (IAcceptableValueBase<float>)null, (Deprecated<Config>)null, "AutoCloseMinPlayerDistance");

		public ConfigEntry<float> AutoCloseMinOpenSeconds { get; } = ConfigBase<Config>.BindEx<float>(cfg, 2f, FormattableString.Invariant($"Minimum time the door must have been open before it is closed."), (IAcceptableValueBase<float>)null, (Deprecated<Config>)null, "AutoCloseMinOpenSeconds");

		public ConfigEntry<bool> SynchronizePairs { get; } = ConfigBase<Config>.BindEx<bool>(cfg, true, "True to synchronize the state of door/gate pairs", (IAcceptableValueBase<bool>)null, (Deprecated<Config>)null, "SynchronizePairs");

		public IReadOnlyDictionary<Door, ConfigEntry<bool>> AutoClose { get; } = GetAutoClosePerDoor(cfg);

		public Config(ConfigFile cfg, Logger logger)
			: base(cfg, logger)
		{
		}

		private static IReadOnlyDictionary<Door, ConfigEntry<bool>> GetAutoClosePerDoor(ConfigFile cfg)
		{
			Dictionary<Door, ConfigEntry<bool>> dictionary = new Dictionary<Door, ConfigEntry<bool>>();
			foreach (GameObject prefab in ZNetScene.instance.m_prefabs)
			{
				Door componentInChildren = prefab.GetComponentInChildren<Door>();
				if (componentInChildren != null && componentInChildren.m_keyItem == null && !componentInChildren.m_canNotBeClosed && prefab.GetComponentInChildren<Piece>() != null && ConfigBase.PieceTablesByPieceName.ContainsKey(((Object)prefab).name))
				{
					string text = SQoLUtils.ToPascalCase(((Object)componentInChildren).name);
					bool flag = !text.Contains("Window") && !text.Contains("Drawbridge");
					dictionary.Add(componentInChildren, ConfigBase<Config>.BindEx<bool>(cfg, "Doors", flag, "True to automatically close " + Localization.instance.Localize(componentInChildren.m_name), (IAcceptableValueBase<bool>)null, (Deprecated<Config>)null, "Close" + text));
				}
			}
			return dictionary;
		}
	}
	[Processor("f91beb92-c76b-43d5-91d4-82c1f7de2929")]
	public sealed class DoorProcessor : Processor<DoorProcessor.PrefabInfo>
	{
		public sealed record PrefabInfo(Door Door) : ProcessorPrefabInfo()
		{
			public ConfigEntry<bool> AutoClose { get; private set; }

			[MemberNotNullWhen(true, "AutoClose")]
			public override bool IsValid
			{
				[MemberNotNullWhen(true, "AutoClose")]
				get
				{
					ConfigEntry<bool> value;
					ConfigEntry<bool> val = (AutoClose = (ConfigBase<Config>.Instance.AutoClose.TryGetValue(Door, out value) ? value : null));
					return val != null;
				}
			}

			[CompilerGenerated]
			public override string ToString()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.Append("PrefabInfo");
				stringBuilder.Append(" { ");
				if (((ProcessorPrefabInfo)this).PrintMembers(stringBuilder))
				{
					stringBuilder.Append(' ');
				}
				stringBuilder.Append('}');
				return stringBuilder.ToString();
			}

			[CompilerGenerated]
			protected override bool PrintMembers(StringBuilder builder)
			{
				if (((ProcessorPrefabInfo)this).PrintMembers(builder))
				{
					builder.Append(", ");
				}
				builder.Append("Door = ");
				builder.Append(Door);
				builder.Append(", AutoClose = ");
				builder.Append(AutoClose);
				return true;
			}

			[CompilerGenerated]
			public override int GetHashCode()
			{
				return (((ProcessorPrefabInfo)this).GetHashCode() * -1521134295 + EqualityComparer<Door>.Default.GetHashCode(Door)) * -1521134295 + EqualityComparer<ConfigEntry<bool>>.Default.GetHashCode(AutoClose);
			}

			[CompilerGenerated]
			public sealed override bool Equals(ProcessorPrefabInfo? other)
			{
				return ((object)this).Equals((object?)other);
			}

			[CompilerGenerated]
			public bool Equals(PrefabInfo? other)
			{
				if ((object)this != other)
				{
					if (((ProcessorPrefabInfo)this).Equals((ProcessorPrefabInfo)(object)other) && EqualityComparer<Door>.Default.Equals(Door, other.Door))
					{
						return EqualityComparer<ConfigEntry<bool>>.Default.Equals(AutoClose, other.AutoClose);
					}
					return false;
				}
				return true;
			}

			[CompilerGenerated]
			private PrefabInfo(PrefabInfo original)
				: base((ProcessorPrefabInfo)(object)original)
			{
				Door = original.Door;
				AutoClose = original.AutoClose;
			}
		}

		public const string Id = "f91beb92-c76b-43d5-91d4-82c1f7de2929";

		private const int StateClosed = 0;

		private readonly Dictionary<ServersideQoLZDO, Timestamp> _closeAfter = new Dictionary<ServersideQoLZDO, Timestamp>();

		private readonly SectorDictionary<PrefabInfo, HashSet<ServersideQoLZDO>> _doors = new SectorDictionary<PrefabInfo, HashSet<ServersideQoLZDO>>(4f);

		private readonly Dictionary<ServersideQoLZDO, ServersideQoLZDO> _pairedDoors = new Dictionary<ServersideQoLZDO, ServersideQoLZDO>();

		protected override void Initialize()
		{
			_closeAfter.Clear();
			_doors.Clear();
			_pairedDoors.Clear();
		}

		protected override ProcessResult Process(ServersideQoLZDO zdo, IReadOnlyList<Peer> peers, PrefabInfo prefabInfo)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//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_0011: Unknown result type (might be due to invalid IL or missing references)
			//IL_0012: Unknown result type (might be due to invalid IL or missing references)
			//IL_0017: Unknown result type (might be due to invalid IL or missing references)
			//IL_043e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0455: Unknown result type (might be due to invalid IL or missing references)
			//IL_0024: 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_0040: Unknown result type (might be due to invalid IL or missing references)
			//IL_0041: Unknown result type (might be due to invalid IL or missing references)
			//IL_0045: Unknown result type (might be due to invalid IL or missing references)
			//IL_004a: Unknown result type (might be due to invalid IL or missing references)
			//IL_04f2: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a5: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a8: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00aa: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b7: Unknown result type (might be due to invalid IL or missing references)
			//IL_01dc: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e7: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ec: Unknown result type (might be due to invalid IL or missing references)
			//IL_01f0: Unknown result type (might be due to invalid IL or missing references)
			//IL_01f5: Unknown result type (might be due to invalid IL or missing references)
			//IL_012c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0131: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ec: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f2: Unknown result type (might be due to invalid IL or missing references)
			//IL_010b: Unknown result type (might be due to invalid IL or missing references)
			//IL_011c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0121: Unknown result type (might be due to invalid IL or missing references)
			//IL_0122: Unknown result type (might be due to invalid IL or missing references)
			//IL_04d3: Unknown result type (might be due to invalid IL or missing references)
			//IL_04d8: Unknown result type (might be due to invalid IL or missing references)
			//IL_04b8: Unknown result type (might be due to invalid IL or missing references)
			//IL_04bd: Unknown result type (might be due to invalid IL or missing references)
			//IL_015c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0161: Unknown result type (might be due to invalid IL or missing references)
			//IL_0146: Unknown result type (might be due to invalid IL or missing references)
			//IL_014a: Unknown result type (might be due to invalid IL or missing references)
			//IL_014f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0150: Unknown result type (might be due to invalid IL or missing references)
			//IL_0246: Unknown result type (might be due to invalid IL or missing references)
			//IL_024b: Unknown result type (might be due to invalid IL or missing references)
			//IL_025d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0262: Unknown result type (might be due to invalid IL or missing references)
			//IL_029d: Unknown result type (might be due to invalid IL or missing references)
			//IL_02bd: Unknown result type (might be due to invalid IL or missing references)
			//IL_034c: Unknown result type (might be due to invalid IL or missing references)
			//IL_036d: Unknown result type (might be due to invalid IL or missing references)
			//IL_02fe: Unknown result type (might be due to invalid IL or missing references)
			//IL_03a8: Unknown result type (might be due to invalid IL or missing references)
			//IL_03b6: Unknown result type (might be due to invalid IL or missing references)
			ZDOVars vars = zdo.Vars;
			PlayerID val = default(PlayerID);
			val = ((ZDOVars)(ref vars)).GetCreator(val);
			if (((PlayerID)(ref val)).Value == 0L)
			{
				return (ProcessResult)2;
			}
			ProcessResult val2 = (ProcessResult)2;
			int? num = null;
			if (prefabInfo.AutoClose.Value)
			{
				val2 = (ProcessResult)(val2 & -3);
				vars = zdo.Vars;
				num = ((ZDOVars)(ref vars)).GetState(0);
				Timestamp value;
				Timestamp now;
				if (num.HasValue && num.GetValueOrDefault() == 0)
				{
					if (_closeAfter.Remove(zdo))
					{
						zdo.Destroyed -= OnDoorDestroyed;
					}
				}
				else if (!Processor.CheckMinDistance(peers, zdo, ConfigBase<Config>.Instance.AutoCloseMinPlayerDistance.Value))
				{
					val2 |= ((Processor)this).ScheduleReprocessing(0f);
				}
				else if (!_closeAfter.TryGetValue(zdo, out value))
				{
					Dictionary<ServersideQoLZDO, Timestamp> closeAfter = _closeAfter;
					now = Timestamp.Now;
					closeAfter.Add(zdo, value = ((Timestamp)(ref now)).AddSeconds(ConfigBase<Config>.Instance.AutoCloseMinOpenSeconds.Value));
					zdo.Destroyed += OnDoorDestroyed;
					val2 |= ((Processor)this).ScheduleReprocessing(ConfigBase<Config>.Instance.AutoCloseMinOpenSeconds.Value);
				}
				else
				{
					float seconds = ((Timestamp)(ref value)).Seconds;
					now = Timestamp.Now;
					float num2 = seconds - ((Timestamp)(ref now)).Seconds;
					if (num2 > 0f)
					{
						val2 |= ((Processor)this).ScheduleReprocessing(num2);
					}
					else
					{
						num = 0;
						vars = zdo.Vars;
						((ZDOVars)(ref vars)).SetState(0, "C:\\repos\\Valheim.ServersideQoL\\ServersideQoL.AutoDoors\\DoorProcessor.cs", 68);
						if (_closeAfter.Remove(zdo))
						{
							zdo.Destroyed -= OnDoorDestroyed;
						}
					}
				}
			}
			if (ConfigBase<Config>.Instance.SynchronizePairs.Value)
			{
				val2 = (ProcessResult)(val2 & -3);
				if (((ProcessorPrefabInfo)prefabInfo).PrefabInfo.Prefab != null && SectorDictionary.TryAdd<PrefabInfo>(_doors, prefabInfo, zdo, true))
				{
					Dictionary<string, Vector3> dictionary = null;
					AdjacentEnumerator<PrefabInfo, HashSet<ServersideQoLZDO>> enumerator = _doors.EnumerateAdjacent((zdo.ZDO.GetPosition(), prefabInfo)).GetEnumerator();
					try
					{
						while (enumerator.MoveNext())
						{
							HashSet<ServersideQoLZDO> current = enumerator.Current;
							foreach (ServersideQoLZDO item in current)
							{
								if (item == zdo || _pairedDoors.ContainsKey(item))
								{
									continue;
								}
								Vector3 localPosition = ((ProcessorPrefabInfo)prefabInfo).PrefabInfo.Prefab.transform.localPosition;
								Quaternion localRotation = ((ProcessorPrefabInfo)prefabInfo).PrefabInfo.Prefab.transform.localRotation;
								try
								{
									if (dictionary == null)
									{
										dictionary = new Dictionary<string, Vector3>(((ProcessorPrefabInfo)prefabInfo).PrefabInfo.Prefab.transform.childCount);
										((ProcessorPrefabInfo)prefabInfo).PrefabInfo.Prefab.transform.localPosition = zdo.ZDO.GetPosition();
										((ProcessorPrefabInfo)prefabInfo).PrefabInfo.Prefab.transform.localRotation = zdo.ZDO.GetRotation();
										for (int i = 0; i < ((ProcessorPrefabInfo)prefabInfo).PrefabInfo.Prefab.transform.childCount; i++)
										{
											Transform child = ((ProcessorPrefabInfo)prefabInfo).PrefabInfo.Prefab.transform.GetChild(i);
											if (((Component)child).CompareTag("snappoint"))
											{
												dictionary.Add(((Object)child).name, child.position);
											}
										}
									}
									if (dictionary.Count == 0)
									{
										continue;
									}
									((ProcessorPrefabInfo)prefabInfo).PrefabInfo.Prefab.transform.localPosition = item.ZDO.GetPosition();
									((ProcessorPrefabInfo)prefabInfo).PrefabInfo.Prefab.transform.localRotation = item.ZDO.GetRotation();
									for (int j = 0; j < ((ProcessorPrefabInfo)prefabInfo).PrefabInfo.Prefab.transform.childCount; j++)
									{
										Transform child2 = ((ProcessorPrefabInfo)prefabInfo).PrefabInfo.Prefab.transform.GetChild(j);
										if (((Component)child2).CompareTag("snappoint") && !(Utils.DistanceSqr(child2.position, dictionary[((Object)child2).name]) > 0.01f))
										{
											_pairedDoors.Add(zdo, item);
											_pairedDoors.Add(item, zdo);
											zdo.Destroyed += OnPairedDoorDestroyed;
											item.Destroyed += OnPairedDoorDestroyed;
											break;
										}
									}
								}
								finally
								{
									((ProcessorPrefabInfo)prefabInfo).PrefabInfo.Prefab.transform.localPosition = localPosition;
									((ProcessorPrefabInfo)prefabInfo).PrefabInfo.Prefab.transform.localRotation = localRotation;
								}
							}
						}
					}
					finally
					{
						((IDisposable)enumerator/*cast due to .constrained prefix*/).Dispose();
					}
				}
				if (_pairedDoors.TryGetValue(zdo, out ServersideQoLZDO value2))
				{
					int valueOrDefault = num.GetValueOrDefault();
					if (!num.HasValue)
					{
						vars = zdo.Vars;
						valueOrDefault = ((ZDOVars)(ref vars)).GetState(0);
						num = valueOrDefault;
					}
					vars = value2.Vars;
					((ZDOVars)(ref vars)).SetState(-num.Value, "C:\\repos\\Valheim.ServersideQoL\\ServersideQoL.AutoDoors\\DoorProcessor.cs", 142);
				}
			}
			return val2;
		}

		private void OnDoorDestroyed(ServersideQoLZDO zdo)
		{
			_closeAfter.Remove(zdo);
		}

		private void OnPairedDoorDestroyed(ServersideQoLZDO zdo)
		{
			if (_pairedDoors.Remove(zdo, out zdo) && _pairedDoors.Remove(zdo))
			{
				zdo.Destroyed -= OnPairedDoorDestroyed;
			}
		}
	}
}