Decompiled source of OpenSaysMe v1.2.0

OpenSaysMe.dll

Decompiled 2 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Bootstrap;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using OpenSaysMe.Utils;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyCompany("OpenSaysMe")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.2.0.0")]
[assembly: AssemblyInformationalVersion("1.2.0+da5ecbcff14e8eff65b50dd35b01eb150dd2b61d")]
[assembly: AssemblyProduct("OpenSaysMe")]
[assembly: AssemblyTitle("OpenSaysMe")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.2.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 OpenSaysMe
{
	public enum AutoUseType
	{
		Disabled,
		OpenClose,
		Close
	}
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[BepInPlugin("OpenSaysMe", "OpenSaysMe", "1.2.0")]
	public class OpenSaysMePlugin : BaseUnityPlugin
	{
		public static readonly string IgnoreAutoUseZDO_DEPRECATED = "OpenSaysMe_ignoreAutoUse";

		public static readonly string AutoUseTypeZDO = "OpenSaysMe_AutoUseType";

		private static ConfigEntry<bool> _enableAutoUse;

		private static ConfigEntry<float> _autoUseRadius;

		private static ConfigEntry<AutoUseType> _defaultAutoUseType;

		internal static ManualLogSource Logger;

		private readonly Harmony _harmony = new Harmony("OpenSaysMe");

		public static bool EnableAutoUse => _enableAutoUse.Value;

		public static float AutoUseRadius => _autoUseRadius.Value;

		public static AutoUseType DefaultAutoUseType => _defaultAutoUseType.Value;

		private void Awake()
		{
			Logger = ((BaseUnityPlugin)this).Logger;
			_setupConfig();
			Assembly executingAssembly = Assembly.GetExecutingAssembly();
			_harmony.PatchAll(executingAssembly);
			Logger.LogInfo((object)"Plugin OpenSaysMe is loaded!");
		}

		private void _setupConfig()
		{
			//IL_005a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0064: Expected O, but got Unknown
			((BaseUnityPlugin)this).Config.SaveOnConfigSet = true;
			_enableAutoUse = ((BaseUnityPlugin)this).Config.Bind<bool>("Settings", "Enable Auto Use", true, "The distance a player needs to be at to activate auto use.");
			_autoUseRadius = ((BaseUnityPlugin)this).Config.Bind<float>("Settings", "Auto Use Radius", 5f, new ConfigDescription("The distance a player needs to be at to activate auto use.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(1f, 10f), Array.Empty<object>()));
			_defaultAutoUseType = ((BaseUnityPlugin)this).Config.Bind<AutoUseType>("Settings", "Default Auto Use Type", AutoUseType.OpenClose, "The default auto use type to set the door to when it's first created.");
		}
	}
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "OpenSaysMe";

		public const string PLUGIN_NAME = "OpenSaysMe";

		public const string PLUGIN_VERSION = "1.2.0";
	}
}
namespace OpenSaysMe.Utils
{
	public static class DoorUtil
	{
		private static readonly MethodInfo CanInteractMethod = AccessTools.Method(typeof(Door), "CanInteract", (Type[])null, (Type[])null);

		public static bool CanInteract(Door door)
		{
			if (!Object.op_Implicit((Object)(object)door))
			{
				return false;
			}
			return (bool)CanInteractMethod.Invoke(door, null);
		}

		public static bool IsOpen(Door door)
		{
			int @int = ((Component)door).GetComponent<ZNetView>().GetZDO().GetInt(ZDOVars.s_state, 0);
			if (!door.m_invertedOpenClosedText)
			{
				return @int == 0;
			}
			return @int != 0;
		}
	}
	public static class PlayerUtil
	{
		private static readonly MethodInfo TakeInputMethod = AccessTools.Method(typeof(Player), "TakeInput", (Type[])null, (Type[])null);

		public static bool TakeInput(Player player)
		{
			return (bool)TakeInputMethod.Invoke(player, null);
		}
	}
	public static class PrivateAreaUtil
	{
		private static readonly MethodInfo IsEnabledMethod = AccessTools.Method(typeof(PrivateArea), "IsEnabled", (Type[])null, (Type[])null);

		private static readonly MethodInfo IsInsideMethod = AccessTools.Method(typeof(PrivateArea), "IsInside", (Type[])null, (Type[])null);

		private static readonly MethodInfo IsPermittedMethod = AccessTools.Method(typeof(PrivateArea), "IsPermitted", (Type[])null, (Type[])null);

		private static readonly FieldInfo m_allAreasField = AccessTools.Field(typeof(PrivateArea), "m_allAreas");

		private static readonly FieldInfo m_piece = AccessTools.Field(typeof(PrivateArea), "m_piece");

		private static List<PrivateArea> AllAreas => m_allAreasField.GetValue(null) as List<PrivateArea>;

		public static bool CanAccess(Player player, Vector3 point)
		{
			//IL_001e: Unknown result type (might be due to invalid IL or missing references)
			foreach (PrivateArea allArea in AllAreas)
			{
				if (IsEnabled(allArea) && IsInside(allArea, point) && !HaveAccess(allArea, player))
				{
					return false;
				}
			}
			return true;
		}

		public static bool IsEnabled(PrivateArea area)
		{
			return (bool)IsEnabledMethod.Invoke(area, null);
		}

		public static bool IsInside(PrivateArea area, Vector3 point)
		{
			//IL_000e: Unknown result type (might be due to invalid IL or missing references)
			return (bool)IsInsideMethod.Invoke(area, new object[2] { point, 0 });
		}

		public static bool IsPermitted(PrivateArea area, Player player)
		{
			return (bool)IsPermittedMethod.Invoke(area, new object[1] { player.GetPlayerID() });
		}

		public static bool HaveAccess(PrivateArea area, Player player)
		{
			if (player.GetPlayerID() != GetPiece(area).GetCreator())
			{
				return IsPermitted(area, player);
			}
			return true;
		}

		public static Piece GetPiece(PrivateArea area)
		{
			//IL_000b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0011: Expected O, but got Unknown
			return (Piece)m_piece.GetValue(area);
		}
	}
}
namespace OpenSaysMe.Patches
{
	[HarmonyPatch(typeof(Door))]
	public static class DoorPatch
	{
		private static readonly HashSet<Door> Doors = new HashSet<Door>();

		[HarmonyPostfix]
		[HarmonyPatch("GetHoverText")]
		public static void GetHoverTextPostfix(Door __instance, ref string __result)
		{
			//IL_002c: Unknown result type (might be due to invalid IL or missing references)
			ZNetView val = default(ZNetView);
			if (OpenSaysMePlugin.EnableAutoUse && ((Component)__instance).TryGetComponent<ZNetView>(ref val) && DoorUtil.CanInteract(__instance) && (!__instance.m_checkGuardStone || PrivateArea.CheckAccess(((Component)__instance).transform.position, 0f, false, false)))
			{
				string text = (AutoUseType)val.GetZDO().GetInt(OpenSaysMePlugin.AutoUseTypeZDO, 0) switch
				{
					AutoUseType.Disabled => "Enable auto open and close", 
					AutoUseType.OpenClose => "Enable auto close only", 
					AutoUseType.Close => "Disable auto use", 
					_ => "", 
				};
				__result = __result + "\n" + Localization.instance.Localize("[<color=yellow><b>$KEY_Run</b></color> + <color=yellow><b>$KEY_Use</b></color>]") + " " + text;
			}
		}

		[HarmonyPostfix]
		[HarmonyPatch("Awake")]
		public static void AwakePostfix(Door __instance)
		{
			ZNetView val = default(ZNetView);
			if (OpenSaysMePlugin.EnableAutoUse && ((Component)__instance).TryGetComponent<ZNetView>(ref val) && val.IsValid())
			{
				bool @bool = val.GetZDO().GetBool(OpenSaysMePlugin.IgnoreAutoUseZDO_DEPRECATED, false);
				bool num = val.GetZDO().RemoveInt(OpenSaysMePlugin.IgnoreAutoUseZDO_DEPRECATED);
				AutoUseType autoUseType = (AutoUseType)val.GetZDO().GetInt(OpenSaysMePlugin.AutoUseTypeZDO, (int)OpenSaysMePlugin.DefaultAutoUseType);
				if (num && @bool)
				{
					autoUseType = AutoUseType.Disabled;
				}
				val.GetZDO().Set(OpenSaysMePlugin.AutoUseTypeZDO, (int)autoUseType);
			}
		}

		[HarmonyPostfix]
		[HarmonyPatch("UpdateState")]
		public static void UpdateStatePostfix(Door __instance)
		{
			//IL_0020: Unknown result type (might be due to invalid IL or missing references)
			//IL_003c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0047: Unknown result type (might be due to invalid IL or missing references)
			//IL_007f: Unknown result type (might be due to invalid IL or missing references)
			//IL_013f: Unknown result type (might be due to invalid IL or missing references)
			//IL_014e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0112: Unknown result type (might be due to invalid IL or missing references)
			if (!OpenSaysMePlugin.EnableAutoUse || !Object.op_Implicit((Object)(object)Player.m_localPlayer))
			{
				return;
			}
			float autoUseRadius = OpenSaysMePlugin.AutoUseRadius;
			Player closestPlayer = Player.GetClosestPlayer(((Component)__instance).transform.position, autoUseRadius + 1f);
			if (Vector3.Distance(((Component)Player.m_localPlayer).transform.position, ((Component)__instance).transform.position) > autoUseRadius && !Doors.Contains(__instance))
			{
				return;
			}
			ZNetView val = default(ZNetView);
			if ((Object)(object)closestPlayer != (Object)null && (Object)(object)closestPlayer != (Object)(object)Player.m_localPlayer && PrivateAreaUtil.CanAccess(closestPlayer, ((Component)__instance).transform.position))
			{
				if (Doors.Contains(__instance))
				{
					OpenSaysMePlugin.Logger.LogDebug((object)("Not the closest permitted player, removing " + _getDoorID(__instance)));
					Doors.Remove(__instance);
				}
			}
			else if (((Component)__instance).TryGetComponent<ZNetView>(ref val) && val.IsValid())
			{
				Doors.Add(__instance);
				bool flag = DoorUtil.CanInteract(__instance);
				AutoUseType @int = (AutoUseType)val.GetZDO().GetInt(OpenSaysMePlugin.AutoUseTypeZDO, 0);
				if (@int != 0 && flag && (!__instance.m_checkGuardStone || PrivateArea.CheckAccess(((Component)__instance).transform.position, 0f, false, false)))
				{
					int int2 = val.GetZDO().GetInt(ZDOVars.s_state, 0);
					float num = Vector3.Distance(((Component)__instance).transform.position, ((Component)Player.m_localPlayer).transform.position);
					bool flag2 = (__instance.m_invertedOpenClosedText ? (int2 != 0) : (int2 == 0));
					bool flag3 = (__instance.m_invertedOpenClosedText ? (int2 == 0) : (int2 != 0));
					bool flag4 = num <= autoUseRadius;
					bool flag5 = num >= autoUseRadius;
					if (flag4 && flag2 && @int == AutoUseType.OpenClose)
					{
						__instance.Interact((Humanoid)(object)Player.m_localPlayer, false, false);
					}
					else if (flag5 && flag3 && Doors.Contains(__instance))
					{
						OpenSaysMePlugin.Logger.LogDebug((object)("Closing door " + _getDoorID(__instance)));
						__instance.Interact((Humanoid)(object)Player.m_localPlayer, false, false);
						Doors.Remove(__instance);
					}
				}
			}
			else
			{
				OpenSaysMePlugin.Logger.LogDebug((object)("No ZNetView " + _getDoorID(__instance)));
				Doors.Remove(__instance);
			}
		}

		private static string _getDoorID(Door door)
		{
			return $"{((door != null) ? new int?(((Object)((Component)door).gameObject).GetInstanceID()) : null)}";
		}
	}
	[HarmonyPatch(typeof(Door), "Awake")]
	[HarmonyPriority(0)]
	public static class DoorPatch_OdinsKingdomFix
	{
		public const string OdinKingdomGuid = "odinplus.plugins.odinskingdom";

		public static readonly List<string> BrokenDoorNames = new List<string>(4) { "$GB_Stone_RoundWall_Door", "$GB_Wood_RoundWall_Door", "$GB_Hidden_Floor_Hatch", "$GB_Stone_Hatch" };

		[HarmonyPrepare]
		public static bool IsOdinsKingdomLoaded()
		{
			bool flag = Chainloader.PluginInfos.ContainsKey("odinplus.plugins.odinskingdom");
			OpenSaysMePlugin.Logger.LogInfo((object)("OdinsKingdom " + (flag ? "loaded, applying fix" : "not loaded, skipping fix")));
			return flag;
		}

		[HarmonyPostfix]
		public static void AwakePostfix(Door __instance)
		{
			if (BrokenDoorNames.Contains(__instance.m_name))
			{
				__instance.m_invertedOpenClosedText = true;
			}
		}
	}
	[HarmonyPatch(typeof(Player))]
	public static class PlayerPatch
	{
		[HarmonyPrefix]
		[HarmonyPatch("Interact")]
		public static bool InteractPrefix(Player __instance, GameObject go, bool hold, bool alt)
		{
			//IL_003d: Unknown result type (might be due to invalid IL or missing references)
			if (!OpenSaysMePlugin.EnableAutoUse)
			{
				return true;
			}
			Door componentInParent = go.GetComponentInParent<Door>();
			if (!Object.op_Implicit((Object)(object)componentInParent))
			{
				return true;
			}
			ZNetView val = default(ZNetView);
			if (PlayerUtil.TakeInput(__instance) && !Hud.InRadial() && (!componentInParent.m_checkGuardStone || PrivateArea.CheckAccess(((Component)__instance).transform.position, 0f, false, false)) && ((Component)componentInParent).TryGetComponent<ZNetView>(ref val))
			{
				AutoUseType @int = (AutoUseType)val.GetZDO().GetInt(OpenSaysMePlugin.AutoUseTypeZDO, 0);
				bool num = ZInput.GetButton("Run") && ZInput.GetButtonDown("Use");
				bool flag = ZInput.GetButton("Use") && ZInput.GetButtonDown("Use");
				if (num)
				{
					val.GetZDO().Set(OpenSaysMePlugin.AutoUseTypeZDO, (int)((@int != AutoUseType.Close) ? (@int + 1) : AutoUseType.Disabled));
					return false;
				}
				if (flag)
				{
					bool flag2 = DoorUtil.IsOpen(componentInParent);
					if (@int != 0 && ((@int == AutoUseType.Close && !flag2) || @int != AutoUseType.Close))
					{
						val.GetZDO().Set(OpenSaysMePlugin.AutoUseTypeZDO, 0);
					}
				}
			}
			return true;
		}
	}
}