Decompiled source of AutoMapRoom v1.1.4

AutoMapRoom.dll

Decompiled 5 days 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 System.Text;
using AutoMapRoom.HarmonyPatches;
using AutoMapRoom.Wrappers;
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.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("s0apy.amr")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.1.4.0")]
[assembly: AssemblyInformationalVersion("1.1.4+4886c50c91c9fae2fed8d220c88b6521e863b597")]
[assembly: AssemblyProduct("AutoMapRoom")]
[assembly: AssemblyTitle("AutoMapRoom")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.1.4.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.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]
	[Microsoft.CodeAnalysis.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]
	[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 AutoMapRoom
{
	[BepInPlugin("s0apy.amr", "AutoMapRoom", "1.1.4")]
	public class Main : BaseUnityPlugin
	{
		public static string currentActiveTrigger = "";

		public static string currentMapRoomName = "";

		public static string currentChatRoom = "";

		public static Dictionary<string, string> roomNameCache = new Dictionary<string, string>();

		public static bool _modDisabledGlobalChat = false;

		public static bool IsReady = false;

		internal static ManualLogSource Log;

		internal static ConfigEntry<bool> ModEnabled;

		internal static ConfigEntry<bool> DebugLoggingEnabled;

		internal static ConfigEntry<bool> DisableGlobalOnRoomJoin;

		private ConfigEntry<KeyCode> _menuKey;

		private bool _showMenu;

		private Rect _windowRect = new Rect(20f, 20f, 280f, 180f);

		private bool _isBindingKey;

		private bool _ignoreToggleInputForFrame;

		private void Awake()
		{
			Log = ((BaseUnityPlugin)this).Logger;
			ModEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", false, "Globally enables or disables the auto room switching feature.");
			DebugLoggingEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Debug Logging", false, "Enables verbose logging for troubleshooting.");
			DisableGlobalOnRoomJoin = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Mute Global in Rooms", false, "Automatically mutes the global chat channel when you join a map/region room.");
			_menuKey = ((BaseUnityPlugin)this).Config.Bind<KeyCode>("General", "Menu Key", (KeyCode)289, "The key to press to show/hide the settings menu.");
			Harmony.CreateAndPatchAll(typeof(Hook), (string)null);
			Log.LogInfo((object)"Auto Map Room plugin loaded and patched!");
		}

		private void Update()
		{
			//IL_0016: Unknown result type (might be due to invalid IL or missing references)
			if (_ignoreToggleInputForFrame)
			{
				_ignoreToggleInputForFrame = false;
			}
			else if (Input.GetKeyDown(_menuKey.Value))
			{
				_showMenu = !_showMenu;
				if (!_showMenu)
				{
					_isBindingKey = false;
				}
			}
		}

		private void OnGUI()
		{
			//IL_001b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0027: Unknown result type (might be due to invalid IL or missing references)
			//IL_003b: Expected O, but got Unknown
			//IL_0036: Unknown result type (might be due to invalid IL or missing references)
			//IL_003b: Unknown result type (might be due to invalid IL or missing references)
			if (_showMenu)
			{
				Cursor.lockState = (CursorLockMode)0;
				Cursor.visible = true;
				_windowRect = GUILayout.Window(1862, _windowRect, new WindowFunction(DrawSettingsWindow), "AutoMapRoom [1.1.4]", Array.Empty<GUILayoutOption>());
			}
		}

		private void DrawSettingsWindow(int windowID)
		{
			//IL_0019: Unknown result type (might be due to invalid IL or missing references)
			//IL_0145: 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_0025: Unknown result type (might be due to invalid IL or missing references)
			//IL_002b: Invalid comparison between Unknown and I4
			//IL_0032: Unknown result type (might be due to invalid IL or missing references)
			//IL_0037: Unknown result type (might be due to invalid IL or missing references)
			//IL_0038: Unknown result type (might be due to invalid IL or missing references)
			//IL_003b: Invalid comparison between Unknown and I4
			//IL_004c: Unknown result type (might be due to invalid IL or missing references)
			if (_isBindingKey && Event.current.isKey && (int)Event.current.keyCode != 0)
			{
				if ((int)Event.current.type == 4)
				{
					KeyCode keyCode = Event.current.keyCode;
					if ((int)keyCode == 27)
					{
						_isBindingKey = false;
					}
					else
					{
						_menuKey.Value = keyCode;
						_isBindingKey = false;
						((BaseUnityPlugin)this).Config.Save();
						_ignoreToggleInputForFrame = true;
					}
				}
				Event.current.Use();
			}
			bool flag = GUILayout.Toggle(ModEnabled.Value, " Enable Auto Room Switching", Array.Empty<GUILayoutOption>());
			if (flag != ModEnabled.Value)
			{
				ModEnabled.Value = flag;
				if (!flag && !string.IsNullOrEmpty(currentChatRoom))
				{
					Hook.UpdateChatRoom("");
				}
			}
			DebugLoggingEnabled.Value = GUILayout.Toggle(DebugLoggingEnabled.Value, " Enable Debug Logging", Array.Empty<GUILayoutOption>());
			DisableGlobalOnRoomJoin.Value = GUILayout.Toggle(DisableGlobalOnRoomJoin.Value, " Mute Global in Rooms", Array.Empty<GUILayoutOption>());
			GUILayout.Space(10f);
			GUILayout.BeginHorizontal(Array.Empty<GUILayoutOption>());
			GUILayout.Label("Open Menu Key:", (GUILayoutOption[])(object)new GUILayoutOption[1] { GUILayout.Width(120f) });
			object obj;
			if (!_isBindingKey)
			{
				KeyCode value = _menuKey.Value;
				obj = ((object)(KeyCode)(ref value)).ToString();
			}
			else
			{
				obj = "Press any key...";
			}
			string text = (string)obj;
			if (GUILayout.Button(text, (GUILayoutOption[])(object)new GUILayoutOption[1] { GUILayout.ExpandWidth(true) }))
			{
				_isBindingKey = !_isBindingKey;
			}
			GUILayout.EndHorizontal();
			GUILayout.FlexibleSpace();
			GUI.DragWindow();
		}
	}
	internal static class ModInfo
	{
		public const string GUID = "s0apy.amr";

		public const string NAME = "AutoMapRoom";

		public const string VERSION = "1.1.4";
	}
}
namespace AutoMapRoom.Wrappers
{
	public class Player
	{
		internal static Player GetPlayer()
		{
			return Player._mainPlayer;
		}
	}
}
namespace AutoMapRoom.HarmonyPatches
{
	[HarmonyPatch]
	internal static class Hook
	{
		private static readonly FieldInfo _inGlobalChatField;

		static Hook()
		{
			_inGlobalChatField = AccessTools.Field(typeof(ChatBehaviour), "_inGlobalChat");
		}

		[HarmonyReversePatch(/*Could not decode attribute arguments.*/)]
		[HarmonyPatch(typeof(ChatBehaviour), "Cmd_JoinChatRoom")]
		public static void Call_Cmd_JoinChatRoom(ChatBehaviour instance, string chatroom)
		{
			throw new NotImplementedException("This is a stub and should have been patched by Harmony.");
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(MapVisualOverrideTrigger), "OnTriggerStay")]
		private static void OnTriggerStay_Postfix(MapVisualOverrideTrigger __instance, Collider other)
		{
			if (Main.IsReady && Main.ModEnabled.Value && !string.IsNullOrWhiteSpace(__instance._reigonName) && IsMainPlayer(other))
			{
				Main.currentActiveTrigger = FormatRoomName(__instance._reigonName);
			}
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(MapVisualOverrideTrigger), "OnTriggerExit")]
		private static void OnTriggerExit_Postfix(MapVisualOverrideTrigger __instance, Collider other)
		{
			if (Main.IsReady && Main.ModEnabled.Value && !string.IsNullOrWhiteSpace(__instance._reigonName) && IsMainPlayer(other))
			{
				string text = FormatRoomName(__instance._reigonName);
				if (Main.currentActiveTrigger == text)
				{
					Main.currentActiveTrigger = "";
				}
			}
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(Player), "OnPlayerMapInstanceChange")]
		private static void OnMapInstanceChange_Postfix(Player __instance, MapInstance _new)
		{
			if (!((Object)(object)Player._mainPlayer == (Object)null) && !((Object)(object)__instance != (Object)(object)Player._mainPlayer))
			{
				if (AtlyssNetworkManager._current._soloMode)
				{
					LogDebug("Singleplayer disabled.");
					Main.currentChatRoom = "";
					Main._modDisabledGlobalChat = false;
				}
				else if (!((Object)(object)_new == (Object)null))
				{
					Main.currentMapRoomName = FormatRoomName(_new._mapName);
					Main.currentActiveTrigger = "";
				}
			}
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(Player), "Update")]
		private static void PlayerUpdate_Postfix(Player __instance)
		{
			if ((Object)(object)Player._mainPlayer == (Object)null)
			{
				if (Main.IsReady)
				{
					LogDebug("YOUR PLAYER IS THERE, ITS RIGHT THERE BRO, LOOK AT IUT BRO ITS THIRGH THEREG AGESES.");
					Main.IsReady = false;
					Main.currentChatRoom = "";
				}
				return;
			}
			if (!Main.IsReady)
			{
				Main.IsReady = true;
			}
			if (!Main.ModEnabled.Value || (Object)(object)__instance != (Object)(object)Player._mainPlayer || AtlyssNetworkManager._current._soloMode)
			{
				return;
			}
			if (Main.currentMapRoomName.Equals("Sanctum", StringComparison.OrdinalIgnoreCase))
			{
				if (Main.currentChatRoom != "")
				{
					UpdateChatRoom("");
				}
				return;
			}
			string text = Main.currentActiveTrigger;
			if (string.IsNullOrEmpty(text))
			{
				text = Main.currentMapRoomName ?? "";
			}
			if (text != Main.currentChatRoom)
			{
				UpdateChatRoom(text);
			}
		}

		internal static void UpdateChatRoom(string desiredRoom)
		{
			LogDebug("Updating chatroom name...");
			if (string.IsNullOrEmpty(desiredRoom))
			{
				LeaveRoom();
			}
			else
			{
				JoinRoom(desiredRoom);
			}
			Main.currentChatRoom = desiredRoom;
		}

		private static string FormatRoomName(string regionName)
		{
			if (string.IsNullOrWhiteSpace(regionName))
			{
				return "";
			}
			if (Main.roomNameCache.TryGetValue(regionName, out string value))
			{
				return value;
			}
			string text = regionName.Replace(" ", "");
			string text3;
			if (text.Length > 12)
			{
				StringBuilder stringBuilder = new StringBuilder();
				string[] array = regionName.Split(new char[1] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
				string[] array2 = array;
				foreach (string text2 in array2)
				{
					if (char.IsLetterOrDigit(text2[0]))
					{
						stringBuilder.Append(char.ToUpper(text2[0]));
					}
				}
				text3 = stringBuilder.ToString();
			}
			else
			{
				text3 = text;
			}
			Main.roomNameCache[regionName] = text3;
			return text3;
		}

		private static bool IsMainPlayer(Collider other)
		{
			if ((Object)(object)Player._mainPlayer != (Object)null)
			{
				return (Object)(object)((Component)((Component)other).gameObject.transform.root).gameObject == (Object)(object)((Component)Player._mainPlayer).gameObject;
			}
			return false;
		}

		private static void JoinRoom(string roomName)
		{
			//IL_006b: Unknown result type (might be due to invalid IL or missing references)
			Player player = Player.GetPlayer();
			if ((Object)(object)player?._chatBehaviour != (Object)null)
			{
				bool flag = (bool)_inGlobalChatField.GetValue(player._chatBehaviour);
				if (Main.DisableGlobalOnRoomJoin.Value && flag)
				{
					_inGlobalChatField.SetValue(player._chatBehaviour, false);
					Main._modDisabledGlobalChat = true;
					LogDebug("Silently disabled Global Chat.");
				}
				player._chatBehaviour._setChatChannel = (ChatChannel)2;
				Call_Cmd_JoinChatRoom(player._chatBehaviour, roomName);
				LogDebug("Switched to room: #" + roomName);
			}
		}

		private static void LeaveRoom()
		{
			//IL_0073: Unknown result type (might be due to invalid IL or missing references)
			Player player = Player.GetPlayer();
			if ((Object)(object)player?._chatBehaviour != (Object)null)
			{
				bool flag = (bool)_inGlobalChatField.GetValue(player._chatBehaviour);
				if (Main.DisableGlobalOnRoomJoin.Value && !flag && Main._modDisabledGlobalChat)
				{
					_inGlobalChatField.SetValue(player._chatBehaviour, true);
					Main._modDisabledGlobalChat = false;
					LogDebug("Silently re-enabled Global Chat.");
				}
				player._chatBehaviour._setChatChannel = (ChatChannel)0;
				Call_Cmd_JoinChatRoom(player._chatBehaviour, "");
				LogDebug("Returned to Global chat.");
			}
		}

		private static void LogDebug(string message)
		{
			if (Main.DebugLoggingEnabled.Value)
			{
				Main.Log.LogInfo((object)message);
			}
		}
	}
}
namespace System.Runtime.CompilerServices
{
	[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
	internal sealed class IgnoresAccessChecksToAttribute : Attribute
	{
		public IgnoresAccessChecksToAttribute(string assemblyName)
		{
		}
	}
}