Decompiled source of Remote Management Board v1.0.2

RemoteBoard.dll

Decompiled 4 hours ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
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("RemoteBoard")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("RemoteBoard")]
[assembly: AssemblyTitle("RemoteBoard")]
[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;
		}
	}
}
[BepInPlugin("aces.remote.managementboard", "Remote Management Board", "1.0.0")]
public class Plugin : BaseUnityPlugin
{
	private ConfigEntry<KeyboardShortcut> OpenKey;

	private ConfigEntry<KeyboardShortcut> DumpKey;

	private ConfigEntry<bool> EnableDumpKey;

	private ConfigEntry<bool> PreventOpenIfAlreadyOpen;

	private ConfigEntry<bool> ForceCursorVisible;

	private ConfigEntry<bool> FixOverlappingTabs;

	private void Awake()
	{
		//IL_001b: Unknown result type (might be due to invalid IL or missing references)
		//IL_006b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0158: Unknown result type (might be due to invalid IL or missing references)
		//IL_015d: Unknown result type (might be due to invalid IL or missing references)
		OpenKey = ((BaseUnityPlugin)this).Config.Bind<KeyboardShortcut>("Hotkeys", "OpenBoard", new KeyboardShortcut((KeyCode)290, Array.Empty<KeyCode>()), "Hotkey used to remotely open the management board. Editable with BepInEx Configuration Manager.");
		EnableDumpKey = ((BaseUnityPlugin)this).Config.Bind<bool>("Debug", "EnableDumpKey", false, "Enable the debug dump hotkey. Leave this off for normal use.");
		DumpKey = ((BaseUnityPlugin)this).Config.Bind<KeyboardShortcut>("Debug", "DumpInfoKey", new KeyboardShortcut((KeyCode)291, Array.Empty<KeyCode>()), "Debug only: dumps management board object information to BepInEx/LogOutput.log.");
		PreventOpenIfAlreadyOpen = ((BaseUnityPlugin)this).Config.Bind<bool>("Behaviour", "PreventOpenIfAlreadyOpen", true, "If true, pressing the hotkey while the board tabs are already active will do nothing.");
		ForceCursorVisible = ((BaseUnityPlugin)this).Config.Bind<bool>("Behaviour", "ForceCursorVisible", true, "If true, the mod will force the mouse cursor visible after opening the board.");
		FixOverlappingTabs = ((BaseUnityPlugin)this).Config.Bind<bool>("Behaviour", "FixOverlappingTabs", true, "If true, the mod will hide Products Order when the game restores another tab over it.");
		((BaseUnityPlugin)this).Logger.LogWarning((object)"=============================================");
		((BaseUnityPlugin)this).Logger.LogMessage((object)$"Remote Management Board v{((BaseUnityPlugin)this).Info.Metadata.Version} Initializing...");
		((BaseUnityPlugin)this).Logger.LogWarning((object)"=============================================");
		((BaseUnityPlugin)this).Logger.LogMessage((object)"Author: AcesGamingUK");
		((BaseUnityPlugin)this).Logger.LogMessage((object)"GitHub: https://github.com/AcesGamingUK");
		((BaseUnityPlugin)this).Logger.LogMessage((object)("Open hotkey: " + ((object)OpenKey.Value/*cast due to .constrained prefix*/).ToString()));
		((BaseUnityPlugin)this).Logger.LogWarning((object)"=============================================");
		((BaseUnityPlugin)this).Logger.LogMessage((object)"Remote Management Board Initialized successfully!");
		((BaseUnityPlugin)this).Logger.LogWarning((object)"=============================================");
	}

	private void Update()
	{
		//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_002e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0033: Unknown result type (might be due to invalid IL or missing references)
		KeyboardShortcut value = OpenKey.Value;
		if (((KeyboardShortcut)(ref value)).IsDown())
		{
			OpenBoardRemote();
		}
		if (EnableDumpKey.Value)
		{
			value = DumpKey.Value;
			if (((KeyboardShortcut)(ref value)).IsDown())
			{
				DumpBoardInfo();
			}
		}
	}

	private void OpenBoardRemote()
	{
		if (PreventOpenIfAlreadyOpen.Value && IsBoardAlreadyOpen())
		{
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Open requested, but management board already appears to be open.");
			return;
		}
		((BaseUnityPlugin)this).Logger.LogInfo((object)"Remote open requested.");
		GameObject val = GameObject.Find("Shared_Interactables/ManagerContainer/Interactable_Camera_Manager");
		if ((Object)(object)val == (Object)null)
		{
			((BaseUnityPlugin)this).Logger.LogWarning((object)"Interactable_Camera_Manager not found. Falling back to direct board UI open.");
			ForceShowBoardUI();
			return;
		}
		((BaseUnityPlugin)this).Logger.LogInfo((object)("Found interactable: " + GetPath(val.transform)));
		int num = TrySendPlayMakerEvent(val, "Behaviour", "Send_Data");
		((BaseUnityPlugin)this).Logger.LogInfo((object)("PlayMaker open events attempted on interactable: " + num));
		ForceShowBoardUI();
		if (ForceCursorVisible.Value)
		{
			Cursor.visible = true;
			Cursor.lockState = (CursorLockMode)0;
		}
		((MonoBehaviour)this).StartCoroutine(DelayedRefresh());
	}

	private bool IsBoardAlreadyOpen()
	{
		GameObject val = GameObject.Find("Shared_Interactables/ManagerContainer/Canvas_Manager/Tabs");
		if ((Object)(object)val != (Object)null)
		{
			return val.activeInHierarchy;
		}
		return false;
	}

	private IEnumerator DelayedRefresh()
	{
		yield return null;
		yield return null;
		ForceShowBoardUI();
		yield return null;
		if (FixOverlappingTabs.Value)
		{
			FixProductOrderOverlap();
		}
	}

	private void ForceShowBoardUI()
	{
		ManagerBlackboard val = Object.FindObjectOfType<ManagerBlackboard>(true);
		if ((Object)(object)val == (Object)null)
		{
			((BaseUnityPlugin)this).Logger.LogWarning((object)"ManagerBlackboard not found.");
			return;
		}
		((BaseUnityPlugin)this).Logger.LogInfo((object)("ManagerBlackboard found: " + GetPath(((Component)val).transform)));
		try
		{
			((Component)val).gameObject.SetActive(true);
		}
		catch (Exception ex)
		{
			((BaseUnityPlugin)this).Logger.LogWarning((object)("Could not activate board GameObject: " + ex.Message));
		}
		GameObject val2 = GameObject.Find("Shared_Interactables/ManagerContainer/Canvas_Manager");
		GameObject val3 = (((Object)(object)val.tabsOBJ != (Object)null) ? val.tabsOBJ : GameObject.Find("Shared_Interactables/ManagerContainer/Canvas_Manager/Tabs"));
		if ((Object)(object)val2 != (Object)null)
		{
			val2.SetActive(true);
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Canvas_Manager active.");
		}
		if ((Object)(object)val3 != (Object)null)
		{
			val3.SetActive(true);
			((BaseUnityPlugin)this).Logger.LogInfo((object)("Tabs active: " + GetPath(val3.transform)));
		}
		else
		{
			((BaseUnityPlugin)this).Logger.LogWarning((object)"Tabs object not found.");
		}
		if (FixOverlappingTabs.Value)
		{
			FixProductOrderOverlap();
		}
		try
		{
			val.UpdateUnlockedFranchises();
			val.ReupdateVisibleProducts();
			((BaseUnityPlugin)this).Logger.LogInfo((object)"ManagerBlackboard refreshed.");
		}
		catch (Exception ex2)
		{
			((BaseUnityPlugin)this).Logger.LogError((object)("ManagerBlackboard refresh failed: " + ex2));
		}
	}

	private void FixProductOrderOverlap()
	{
		GameObject val = GameObject.Find("Shared_Interactables/ManagerContainer/Canvas_Manager/Tabs");
		if ((Object)(object)val == (Object)null)
		{
			return;
		}
		Transform val2 = val.transform.Find("ProductsOrder_Tab");
		if ((Object)(object)val2 == (Object)null || !((Component)val2).gameObject.activeSelf)
		{
			return;
		}
		for (int i = 0; i < val.transform.childCount; i++)
		{
			Transform child = val.transform.GetChild(i);
			if (!((Object)(object)child == (Object)null) && !((Object)(object)child == (Object)(object)val2) && ((Component)child).gameObject.activeSelf)
			{
				((Component)val2).gameObject.SetActive(false);
				((BaseUnityPlugin)this).Logger.LogInfo((object)("Fixed overlapping tabs: hid ProductsOrder_Tab because " + ((Object)child).name + " is active."));
				break;
			}
		}
	}

	private int TrySendPlayMakerEvent(GameObject obj, string fsmName, string eventName)
	{
		if ((Object)(object)obj == (Object)null)
		{
			return 0;
		}
		int num = 0;
		Component[] components = obj.GetComponents<Component>();
		foreach (Component val in components)
		{
			if ((Object)(object)val == (Object)null)
			{
				continue;
			}
			Type type = ((object)val).GetType();
			if (type.Name != "PlayMakerFSM")
			{
				continue;
			}
			string propertyString = GetPropertyString(val, "FsmName");
			if (!string.IsNullOrEmpty(fsmName) && propertyString != fsmName)
			{
				continue;
			}
			MethodInfo method = type.GetMethod("SendEvent", new Type[1] { typeof(string) });
			if (method != null)
			{
				try
				{
					method.Invoke(val, new object[1] { eventName });
					((BaseUnityPlugin)this).Logger.LogInfo((object)("Sent FSM event '" + eventName + "' to " + GetPath(obj.transform) + " / " + propertyString));
					num++;
				}
				catch (Exception ex)
				{
					((BaseUnityPlugin)this).Logger.LogWarning((object)("Failed sending FSM event '" + eventName + "' to " + propertyString + ": " + ex.Message));
				}
			}
			else
			{
				((BaseUnityPlugin)this).Logger.LogWarning((object)("PlayMakerFSM.SendEvent(string) not found on " + propertyString));
			}
		}
		return num;
	}

	private string GetPropertyString(object obj, string propertyName)
	{
		if (obj == null)
		{
			return string.Empty;
		}
		try
		{
			PropertyInfo property = obj.GetType().GetProperty(propertyName, BindingFlags.Instance | BindingFlags.Public);
			if (property == null)
			{
				return string.Empty;
			}
			object value = property.GetValue(obj, null);
			return (value != null) ? value.ToString() : string.Empty;
		}
		catch
		{
			return string.Empty;
		}
	}

	private void DumpBoardInfo()
	{
		((BaseUnityPlugin)this).Logger.LogInfo((object)"================ RemoteBoard F10 dump start ================");
		ManagerBlackboard val = Object.FindObjectOfType<ManagerBlackboard>(true);
		if ((Object)(object)val == (Object)null)
		{
			((BaseUnityPlugin)this).Logger.LogWarning((object)"ManagerBlackboard not found.");
		}
		else
		{
			((BaseUnityPlugin)this).Logger.LogInfo((object)("Board path: " + GetPath(((Component)val).transform)));
			((BaseUnityPlugin)this).Logger.LogInfo((object)("Board activeSelf/activeInHierarchy: " + ((Component)val).gameObject.activeSelf + "/" + ((Component)val).gameObject.activeInHierarchy));
			DumpObj("tabsOBJ", val.tabsOBJ);
			DumpObj("shortcutsParent", val.shortcutsParent);
			DumpObj("shopItemsParent", val.shopItemsParent);
			DumpObj("shoppingListParent", val.shoppingListParent);
		}
		DumpSpecific("Shared_Interactables/ManagerContainer/Interactable_Camera_Manager");
		DumpSpecific("Shared_Interactables/ManagerContainer/Interactable_Camera_Manager/CameraPoint");
		DumpSpecific("Shared_Interactables/ManagerContainer/Canvas_Manager");
		DumpSpecific("Shared_Interactables/ManagerContainer/Canvas_Manager/Tabs");
		DumpSpecific("Shared_Interactables/ManagerContainer/Canvas_Manager/Buttons_Bar/CloseButton");
		((BaseUnityPlugin)this).Logger.LogInfo((object)"================ RemoteBoard F10 dump end ==================");
	}

	private void DumpSpecific(string path)
	{
		GameObject val = GameObject.Find(path);
		if ((Object)(object)val == (Object)null)
		{
			((BaseUnityPlugin)this).Logger.LogWarning((object)("Missing: " + path));
		}
		else
		{
			DumpGameObject(val);
		}
	}

	private void DumpObj(string label, GameObject obj)
	{
		if ((Object)(object)obj == (Object)null)
		{
			((BaseUnityPlugin)this).Logger.LogWarning((object)(label + ": null"));
			return;
		}
		((BaseUnityPlugin)this).Logger.LogInfo((object)(label + ": " + GetPath(obj.transform) + " | active " + obj.activeSelf + "/" + obj.activeInHierarchy));
	}

	private void DumpGameObject(GameObject obj)
	{
		if ((Object)(object)obj == (Object)null)
		{
			return;
		}
		Component[] components = obj.GetComponents<Component>();
		((BaseUnityPlugin)this).Logger.LogInfo((object)("GameObject: " + GetPath(obj.transform) + " | active " + obj.activeSelf + "/" + obj.activeInHierarchy + " | components: " + components.Length));
		Component[] array = components;
		foreach (Component val in array)
		{
			if ((Object)(object)val == (Object)null)
			{
				((BaseUnityPlugin)this).Logger.LogInfo((object)"  - <null component>");
				continue;
			}
			((BaseUnityPlugin)this).Logger.LogInfo((object)("  - " + ((object)val).GetType().FullName));
			if (((object)val).GetType().Name == "PlayMakerFSM")
			{
				DumpPlayMakerFsm(val);
			}
		}
	}

	private void DumpPlayMakerFsm(Component fsm)
	{
		try
		{
			string propertyString = GetPropertyString(fsm, "FsmName");
			((BaseUnityPlugin)this).Logger.LogInfo((object)("    FSM label: FsmName=" + propertyString));
			PropertyInfo property = ((object)fsm).GetType().GetProperty("FsmEvents", BindingFlags.Instance | BindingFlags.Public);
			Array array = ((property != null) ? property.GetValue(fsm, null) : null) as Array;
			List<string> list = new List<string>();
			if (array != null)
			{
				foreach (object item in array)
				{
					if (item != null)
					{
						string propertyString2 = GetPropertyString(item, "Name");
						if (!string.IsNullOrEmpty(propertyString2))
						{
							list.Add(propertyString2);
						}
					}
				}
			}
			((BaseUnityPlugin)this).Logger.LogInfo((object)("    FSM events: " + string.Join(", ", list.ToArray())));
		}
		catch (Exception ex)
		{
			((BaseUnityPlugin)this).Logger.LogWarning((object)("    FSM dump failed: " + ex.Message));
		}
	}

	private string GetPath(Transform transform)
	{
		if ((Object)(object)transform == (Object)null)
		{
			return "<null>";
		}
		string text = ((Object)transform).name;
		while ((Object)(object)transform.parent != (Object)null)
		{
			transform = transform.parent;
			text = ((Object)transform).name + "/" + text;
		}
		return text;
	}
}