Decompiled source of GoodMenu v1.0.0

GoodMenu.dll

Decompiled 6 hours ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using GameNetcodeStuff;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Unity.Netcode;
using UnityEngine;
using UnityEngine.AI;
using UnityEngine.EventSystems;
using UnityEngine.Events;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.Controls;
using UnityEngine.UI;

[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("GoodMenu")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("GoodMenu")]
[assembly: AssemblyTitle("GoodMenu")]
[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 GoodMenu
{
	[BepInPlugin("danylo.goodmenu", "GoodMenu", "1.0.0")]
	public sealed class Plugin : BaseUnityPlugin
	{
		private GameObject nativeRoot;

		private GameObject nativePanel;

		private RectTransform nativeContent;

		private Text nativeNotice;

		private GameObject watchPanel;

		private RawImage watchImage;

		private Text watchLabel;

		private Text itemsTabLabel;

		private Font nativeFont;

		internal static Plugin Instance;

		private Harmony harmony;

		private bool menu;

		private bool lockedInput;

		private bool savedLook;

		private bool savedMove;

		private bool savedCursorVisible;

		private CursorLockMode savedCursorLock;

		private PlayerControllerB lockedPlayer;

		internal bool godMode;

		internal bool infiniteStamina;

		private int lastFrame = -1;

		private int lastToggle = -1;

		private int tab;

		private float refreshTimer;

		private float playerRefreshTimer;

		private string search = "";

		private string quantity = "1";

		private string notice = "";

		private readonly List<Item> items = new List<Item>();

		private readonly List<EnemyType> enemies = new List<EnemyType>();

		private PlayerControllerB watchTarget;

		private Camera watchCamera;

		private RenderTexture watchTexture;

		private int Count
		{
			get
			{
				if (!int.TryParse(quantity, out var result))
				{
					return 1;
				}
				return Mathf.Clamp(result, 1, 10);
			}
		}

		internal static bool IsHost
		{
			get
			{
				if ((Object)(object)NetworkManager.Singleton != (Object)null && NetworkManager.Singleton.IsListening)
				{
					return NetworkManager.Singleton.IsHost;
				}
				return false;
			}
		}

		internal static PlayerControllerB Local
		{
			get
			{
				if (!((Object)(object)GameNetworkManager.Instance != (Object)null) || !((Object)(object)GameNetworkManager.Instance.localPlayerController != (Object)null))
				{
					if (!((Object)(object)StartOfRound.Instance == (Object)null))
					{
						return StartOfRound.Instance.localPlayerController;
					}
					return null;
				}
				return GameNetworkManager.Instance.localPlayerController;
			}
		}

		private void RefreshCatalog()
		{
			items.Clear();
			enemies.Clear();
			if ((Object)(object)StartOfRound.Instance == (Object)null)
			{
				return;
			}
			AllItemsList allItemsList = StartOfRound.Instance.allItemsList;
			if ((Object)(object)allItemsList != (Object)null && allItemsList.itemsList != null)
			{
				items.AddRange(allItemsList.itemsList.Where((Item i) => (Object)(object)i != (Object)null && (Object)(object)i.spawnPrefab != (Object)null));
			}
			Terminal val = Object.FindObjectOfType<Terminal>();
			if ((Object)(object)val != (Object)null && val.buyableItemsList != null)
			{
				items.AddRange(val.buyableItemsList.Where((Item i) => (Object)(object)i != (Object)null && (Object)(object)i.spawnPrefab != (Object)null));
			}
			SelectableLevel[] array = (SelectableLevel[])(((object)StartOfRound.Instance.levels) ?? ((object)new SelectableLevel[0]));
			foreach (SelectableLevel val2 in array)
			{
				if ((Object)(object)val2 == (Object)null)
				{
					continue;
				}
				if (val2.spawnableScrap != null)
				{
					items.AddRange(from x in val2.spawnableScrap
						where x != null && (Object)(object)x.spawnableItem != (Object)null && (Object)(object)x.spawnableItem.spawnPrefab != (Object)null
						select x.spawnableItem);
				}
				if (val2.Enemies != null)
				{
					enemies.AddRange(from x in val2.Enemies
						where x != null
						select x.enemyType);
				}
				if (val2.OutsideEnemies != null)
				{
					enemies.AddRange(from x in val2.OutsideEnemies
						where x != null
						select x.enemyType);
				}
				if (val2.DaytimeEnemies != null)
				{
					enemies.AddRange(from x in val2.DaytimeEnemies
						where x != null
						select x.enemyType);
				}
			}
			enemies.AddRange(from e in Resources.FindObjectsOfTypeAll<EnemyType>()
				where (Object)(object)e != (Object)null && (Object)(object)e.enemyPrefab != (Object)null
				select e);
			items.AddRange(from i in Resources.FindObjectsOfTypeAll<Item>()
				where (Object)(object)i != (Object)null && (Object)(object)i.spawnPrefab != (Object)null && (Object)(object)i.spawnPrefab.GetComponent<GrabbableObject>() != (Object)null && (Object)(object)i.spawnPrefab.GetComponent<NetworkObject>() != (Object)null
				select i);
			Item[] collection = (from x in items.Distinct()
				orderby x.itemName
				select x).ToArray();
			items.Clear();
			items.AddRange(collection);
			EnemyType[] collection2 = (from x in enemies.Where((EnemyType x) => (Object)(object)x != (Object)null && (Object)(object)x.enemyPrefab != (Object)null).Distinct()
				orderby x.enemyName
				select x).ToArray();
			enemies.Clear();
			enemies.AddRange(collection2);
		}

		private void SafeRefreshCatalog()
		{
			try
			{
				Item[] first = items.ToArray();
				EnemyType[] first2 = enemies.ToArray();
				RefreshCatalog();
				if (menu && (!first.SequenceEqual(items) || !first2.SequenceEqual(enemies)))
				{
					RebuildNative(resetScroll: false);
				}
			}
			catch (Exception ex)
			{
				notice = "Catalog error: " + ex.Message;
				((BaseUnityPlugin)this).Logger.LogError((object)("GoodMenu catalog: " + ex));
			}
		}

		private void SpawnItem(Item item)
		{
			//IL_0034: Unknown result type (might be due to invalid IL or missing references)
			//IL_0043: Unknown result type (might be due to invalid IL or missing references)
			//IL_004d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0052: Unknown result type (might be due to invalid IL or missing references)
			//IL_0057: Unknown result type (might be due to invalid IL or missing references)
			//IL_0061: Unknown result type (might be due to invalid IL or missing references)
			//IL_0066: 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_0075: Unknown result type (might be due to invalid IL or missing references)
			//IL_007a: 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_0086: Unknown result type (might be due to invalid IL or missing references)
			//IL_0088: Unknown result type (might be due to invalid IL or missing references)
			//IL_008d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0111: Unknown result type (might be due to invalid IL or missing references)
			//IL_0116: Unknown result type (might be due to invalid IL or missing references)
			if (!IsHost || (Object)(object)Local == (Object)null || Local.isPlayerDead)
			{
				return;
			}
			for (int i = 0; i < Count; i++)
			{
				GameObject val = null;
				try
				{
					Vector3 val2 = ((Component)Local).transform.position + ((Component)Local).transform.forward * 1.5f + Vector3.up * 0.5f + Random.insideUnitSphere * 0.15f;
					val = Object.Instantiate<GameObject>(item.spawnPrefab, val2, Quaternion.Euler(item.restingRotation));
					NetworkObject component = val.GetComponent<NetworkObject>();
					GrabbableObject component2 = val.GetComponent<GrabbableObject>();
					if ((Object)(object)component == (Object)null || (Object)(object)component2 == (Object)null)
					{
						throw new InvalidOperationException("No network/grabbable component");
					}
					if (item.isScrap)
					{
						component2.SetScrapValue(Random.Range(item.minValue, Mathf.Max(item.minValue + 1, item.maxValue + 1)));
					}
					component.Spawn(false);
					if (item.isScrap)
					{
						RoundManager.Instance.SyncScrapValuesClientRpc((NetworkObjectReference[])(object)new NetworkObjectReference[1]
						{
							new NetworkObjectReference(component)
						}, new int[1] { component2.scrapValue });
					}
					notice = "Spawned: " + item.itemName;
				}
				catch (Exception ex)
				{
					if ((Object)(object)val != (Object)null)
					{
						NetworkObject component3 = val.GetComponent<NetworkObject>();
						if ((Object)(object)component3 != (Object)null && component3.IsSpawned)
						{
							component3.Despawn(true);
						}
						else
						{
							Object.Destroy((Object)(object)val);
						}
					}
					notice = "Failed: " + item.itemName + " / " + ex.Message;
					((BaseUnityPlugin)this).Logger.LogError((object)ex);
					break;
				}
			}
		}

		private void SpawnEnemy(EnemyType enemy)
		{
			//IL_00ad: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bc: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00cb: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ff: 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)
			if (!IsHost || (Object)(object)Local == (Object)null || Local.isPlayerDead)
			{
				return;
			}
			StartOfRound instance = StartOfRound.Instance;
			if ((Object)(object)instance == (Object)null || instance.inShipPhase || !instance.shipHasLanded || (Object)(object)RoundManager.Instance == (Object)null)
			{
				notice = "Land on a moon first.";
				return;
			}
			if ((enemy.isOutsideEnemy || enemy.isDaytimeEnemy) == Local.isInsideFactory)
			{
				notice = "Go " + ((enemy.isOutsideEnemy || enemy.isDaytimeEnemy) ? "outside." : "inside the facility.");
				return;
			}
			if (!TryPoint(((Component)Local).transform.position + ((Component)Local).transform.forward * 4f, 4f, out var point))
			{
				notice = "No walkable spawn point nearby.";
				return;
			}
			try
			{
				for (int i = 0; i < Count; i++)
				{
					RoundManager.Instance.SpawnEnemyGameObject(point, ((Component)Local).transform.eulerAngles.y, -1, enemy);
				}
				notice = "Spawned: " + enemy.enemyName;
			}
			catch (Exception ex)
			{
				notice = ex.Message;
				((BaseUnityPlugin)this).Logger.LogError((object)ex);
			}
		}

		private static bool TryPoint(Vector3 source, float radius, out Vector3 point)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_001c: Unknown result type (might be due to invalid IL or missing references)
			//IL_001d: Unknown result type (might be due to invalid IL or missing references)
			//IL_000f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0014: Unknown result type (might be due to invalid IL or missing references)
			NavMeshHit val = default(NavMeshHit);
			if (NavMesh.SamplePosition(source, ref val, radius, -1))
			{
				point = ((NavMeshHit)(ref val)).position;
				return true;
			}
			point = source;
			return false;
		}

		private void EnsureNative()
		{
			//IL_00c8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d2: Expected O, but got Unknown
			//IL_0116: Unknown result type (might be due to invalid IL or missing references)
			//IL_0164: Unknown result type (might be due to invalid IL or missing references)
			//IL_0197: Unknown result type (might be due to invalid IL or missing references)
			//IL_019e: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a0: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a1: 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_01b9: Unknown result type (might be due to invalid IL or missing references)
			//IL_0299: Unknown result type (might be due to invalid IL or missing references)
			//IL_02cb: Unknown result type (might be due to invalid IL or missing references)
			//IL_02d2: Unknown result type (might be due to invalid IL or missing references)
			//IL_02e4: Unknown result type (might be due to invalid IL or missing references)
			//IL_02ee: Unknown result type (might be due to invalid IL or missing references)
			//IL_055e: Unknown result type (might be due to invalid IL or missing references)
			//IL_05c3: Unknown result type (might be due to invalid IL or missing references)
			//IL_064c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0681: Unknown result type (might be due to invalid IL or missing references)
			//IL_068f: Unknown result type (might be due to invalid IL or missing references)
			//IL_069b: Unknown result type (might be due to invalid IL or missing references)
			if (!((Object)(object)nativeRoot != (Object)null) || !((Object)(object)nativePanel != (Object)null) || !((Object)(object)nativeNotice != (Object)null))
			{
				if ((Object)(object)nativeRoot != (Object)null)
				{
					Object.Destroy((Object)(object)nativeRoot);
				}
				try
				{
					nativeFont = Resources.GetBuiltinResource<Font>("Arial.ttf");
				}
				catch
				{
					nativeFont = Resources.GetBuiltinResource<Font>("LegacyRuntime.ttf");
				}
				if ((Object)(object)nativeFont == (Object)null)
				{
					nativeFont = Resources.GetBuiltinResource<Font>("LegacyRuntime.ttf");
				}
				nativeRoot = new GameObject("GoodMenu_UI_1_0_0", new Type[4]
				{
					typeof(RectTransform),
					typeof(Canvas),
					typeof(CanvasScaler),
					typeof(GraphicRaycaster)
				});
				Object.DontDestroyOnLoad((Object)(object)nativeRoot);
				Canvas component = nativeRoot.GetComponent<Canvas>();
				component.renderMode = (RenderMode)0;
				component.sortingOrder = 31000;
				CanvasScaler component2 = nativeRoot.GetComponent<CanvasScaler>();
				component2.uiScaleMode = (ScaleMode)1;
				component2.referenceResolution = new Vector2(1280f, 720f);
				component2.matchWidthOrHeight = 0.5f;
				watchPanel = ((Component)NativeBox(nativeRoot.transform, "LiveView", 0f, 0f, 440f, 282f, new Color(0.035f, 0.045f, 0.05f, 0.96f))).gameObject;
				RectTransform component3 = watchPanel.GetComponent<RectTransform>();
				Vector2 val = default(Vector2);
				((Vector2)(ref val))..ctor(1f, 1f);
				component3.pivot = val;
				Vector2 val2 = (component3.anchorMin = (component3.anchorMax = val));
				component3.anchoredPosition = new Vector2(-12f, -12f);
				watchLabel = NativeText(watchPanel.transform, "Status", "", 10f, 4f, 420f, 38f, 14);
				RectTransform val5 = NativeRect(watchPanel.transform, "LiveImage", 10f, 42f, 420f, 236f);
				watchImage = ((Component)val5).gameObject.AddComponent<RawImage>();
				((Graphic)watchImage).raycastTarget = false;
				((Graphic)watchPanel.GetComponent<Image>()).raycastTarget = false;
				watchPanel.SetActive(false);
				nativePanel = ((Component)NativeBox(nativeRoot.transform, "Menu", 0f, 0f, 800f, 610f, new Color(0.06f, 0.075f, 0.09f, 0.98f))).gameObject;
				RectTransform component4 = nativePanel.GetComponent<RectTransform>();
				((Vector2)(ref val2))..ctor(0.5f, 0.5f);
				component4.anchorMax = val2;
				component4.anchorMin = val2;
				component4.pivot = new Vector2(0.5f, 0.5f);
				component4.anchoredPosition = Vector2.zero;
				NativeText(nativePanel.transform, "Title", "GOODMENU 1.0.0 / HOST ONLY", 18f, 12f, 735f, 30f, 20);
				NativeButton(nativePanel.transform, "X", 748f, 10f, 38f, 30f, CloseMenu);
				itemsTabLabel = ((Component)NativeButton(nativePanel.transform, "ITEMS", 18f, 52f, 180f, 34f, delegate
				{
					tab = 0;
					RebuildNative();
				})).GetComponentInChildren<Text>();
				NativeButton(nativePanel.transform, "MONSTERS", 210f, 52f, 180f, 34f, delegate
				{
					tab = 1;
					RebuildNative();
				});
				NativeButton(nativePanel.transform, "HOST", 402f, 52f, 180f, 34f, delegate
				{
					tab = 2;
					RebuildNative();
				});
				NativeButton(nativePanel.transform, "PLAYERS", 594f, 52f, 180f, 34f, delegate
				{
					tab = 3;
					RebuildNative();
				});
				NativeText(nativePanel.transform, "SearchLabel", "Search:", 18f, 100f, 70f, 30f);
				NativeInput(nativePanel.transform, "Search", search, 90f, 98f, 440f, 32f, delegate(string value)
				{
					search = value;
					RebuildNative();
				});
				NativeText(nativePanel.transform, "QtyLabel", "Count 1-10:", 550f, 100f, 105f, 30f);
				NativeInput(nativePanel.transform, "Count", quantity, 660f, 98f, 110f, 32f, delegate(string value)
				{
					quantity = value;
				}).characterLimit = 2;
				RectTransform val6 = NativeBox(nativePanel.transform, "Scroll", 18f, 144f, 760f, 375f, new Color(0.1f, 0.12f, 0.14f));
				ScrollRect obj2 = ((Component)val6).gameObject.AddComponent<ScrollRect>();
				obj2.horizontal = false;
				obj2.vertical = true;
				obj2.movementType = (MovementType)2;
				obj2.scrollSensitivity = 32f;
				RectTransform val7 = NativeBox(((Component)val6).transform, "Viewport", 0f, 0f, 734f, 375f, new Color(0.1f, 0.12f, 0.14f));
				((Component)val7).gameObject.AddComponent<Mask>().showMaskGraphic = false;
				nativeContent = NativeRect(((Component)val7).transform, "Content", 0f, 0f, 760f, 375f);
				obj2.viewport = val7;
				obj2.content = nativeContent;
				RectTransform val8 = NativeBox(((Component)val6).transform, "Scrollbar", 742f, 0f, 18f, 375f, new Color(0.06f, 0.075f, 0.09f));
				RectTransform val9 = NativeBox((Transform)(object)val8, "Handle", 0f, 0f, 18f, 80f, new Color(0.3f, 0.48f, 0.4f));
				val9.sizeDelta = Vector2.zero;
				val9.anchoredPosition = Vector2.zero;
				Scrollbar val10 = ((Component)val8).gameObject.AddComponent<Scrollbar>();
				val10.handleRect = val9;
				((Selectable)val10).targetGraphic = (Graphic)(object)((Component)val9).GetComponent<Image>();
				val10.direction = (Direction)2;
				obj2.verticalScrollbar = val10;
				obj2.verticalScrollbarVisibility = (ScrollbarVisibility)0;
				nativeNotice = NativeText(nativePanel.transform, "Notice", "", 18f, 530f, 760f, 54f, 15);
				NativeText(nativePanel.transform, "Hint", "F6 / Insert / Escape: close. Spawn monsters after landing.", 18f, 580f, 760f, 25f, 14);
				nativePanel.SetActive(false);
				((BaseUnityPlugin)this).Logger.LogInfo((object)"GoodMenu Canvas created.");
			}
		}

		private void NativeFrame()
		{
			if (!IsHost || (Object)(object)Local == (Object)null)
			{
				if ((Object)(object)nativeRoot != (Object)null)
				{
					nativeRoot.SetActive(false);
				}
			}
			else
			{
				EnsureNative();
				nativeRoot.SetActive(true);
				nativePanel.SetActive(IsHost && menu);
				nativeNotice.text = notice;
				itemsTabLabel.text = "ITEMS (" + items.Count + ")";
			}
		}

		private void ShowNative()
		{
			if (IsHost)
			{
				EnsureNative();
				nativeRoot.SetActive(true);
				nativePanel.SetActive(true);
				RebuildNative();
			}
		}

		private void HideNative()
		{
			if (!((Object)(object)nativePanel == (Object)null))
			{
				EventSystem current = EventSystem.current;
				if ((Object)(object)current != (Object)null && (Object)(object)current.currentSelectedGameObject != (Object)null && current.currentSelectedGameObject.transform.IsChildOf(nativePanel.transform))
				{
					current.SetSelectedGameObject((GameObject)null);
				}
				nativePanel.SetActive(false);
			}
		}

		private void DestroyNative()
		{
			if ((Object)(object)nativeRoot != (Object)null)
			{
				Object.Destroy((Object)(object)nativeRoot);
			}
		}

		private void RebuildNative(bool resetScroll = true)
		{
			//IL_001c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0021: Unknown result type (might be due to invalid IL or missing references)
			//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)
			//IL_035d: Unknown result type (might be due to invalid IL or missing references)
			//IL_039d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0375: Unknown result type (might be due to invalid IL or missing references)
			//IL_0396: Unknown result type (might be due to invalid IL or missing references)
			if (!IsHost || (Object)(object)nativeContent == (Object)null)
			{
				return;
			}
			Vector2 anchoredPosition = nativeContent.anchoredPosition;
			foreach (Transform item in (Transform)nativeContent)
			{
				((Component)item).gameObject.SetActive(false);
				Object.Destroy((Object)(object)((Component)item).gameObject);
			}
			float num = 4f;
			if (tab == 0)
			{
				foreach (Item item2 in items.Where((Item x) => (x.itemName ?? "").IndexOf(search, StringComparison.OrdinalIgnoreCase) >= 0))
				{
					NativeItemRow(item2, num);
					num += 58f;
				}
			}
			else if (tab == 1)
			{
				foreach (EnemyType item3 in enemies.Where((EnemyType x) => (x.enemyName ?? "").IndexOf(search, StringComparison.OrdinalIgnoreCase) >= 0))
				{
					EnemyType enemy = item3;
					NativeRow(enemy.enemyName + ((enemy.isOutsideEnemy || enemy.isDaytimeEnemy) ? " [OUTSIDE]" : " [INSIDE]"), num, "SPAWN", delegate
					{
						SpawnEnemy(enemy);
					});
					num += 40f;
				}
			}
			else if (tab == 2)
			{
				NativeRow("God mode (HOST): " + (godMode ? "ON" : "OFF"), num, "TOGGLE", delegate
				{
					if (IsHost)
					{
						godMode = !godMode;
						RebuildNative(resetScroll: false);
					}
				});
				num += 44f;
				NativeRow("Infinite stamina (HOST): " + (infiniteStamina ? "ON" : "OFF"), num, "TOGGLE", delegate
				{
					if (IsHost)
					{
						infiniteStamina = !infiniteStamina;
						RebuildNative(resetScroll: false);
					}
				});
				num += 44f;
				NativeText((Transform)(object)nativeContent, "Info", "Protection and stamina apply only to the host.\nToggles start OFF and reset when leaving the lobby.\nPLAYERS: live camera for alive players or their bodies.\nF7 stops the camera without opening this menu.", 10f, num, 700f, 130f, 16);
				num += 138f;
			}
			else if (tab == 3)
			{
				NativeRow("Live camera", num, "STOP", StopWatching);
				num += 44f;
				PlayerControllerB[] array = ConnectedPlayers();
				foreach (PlayerControllerB val2 in array)
				{
					PlayerControllerB target = val2;
					NativeRow(target.playerUsername + (((Object)(object)target == (Object)(object)Local) ? " [HOST]" : "") + (target.isPlayerDead ? " [DEAD]" : (" [ALIVE / HP " + target.health + "]")), num, "WATCH", delegate
					{
						StartWatching(target);
					});
					num += 44f;
				}
			}
			nativeContent.sizeDelta = new Vector2(760f, Mathf.Max(375f, num));
			nativeContent.anchoredPosition = (Vector2)(resetScroll ? Vector2.zero : new Vector2(0f, Mathf.Clamp(anchoredPosition.y, 0f, Mathf.Max(0f, num - 375f))));
		}

		private void NativeRow(string label, float y, string action, Action callback)
		{
			NativeText((Transform)(object)nativeContent, "Name", label, 10f, y, 565f, 34f);
			NativeButton((Transform)(object)nativeContent, action, 600f, y, 125f, 34f, delegate
			{
				if (IsHost)
				{
					callback();
				}
			});
		}

		private void NativeItemRow(Item item, float y)
		{
			//IL_0055: Unknown result type (might be due to invalid IL or missing references)
			RectTransform parent = NativeBox((Transform)(object)nativeContent, "Icon_" + item.itemName, 8f, y + 3f, 48f, 48f, new Color(0.16f, 0.18f, 0.2f));
			if ((Object)(object)item.itemIcon != (Object)null)
			{
				Image obj = ((Component)NativeRect((Transform)(object)parent, "GameIcon", 3f, 3f, 42f, 42f)).gameObject.AddComponent<Image>();
				obj.sprite = item.itemIcon;
				obj.preserveAspect = true;
				((Graphic)obj).raycastTarget = false;
			}
			else
			{
				NativeText((Transform)(object)parent, "Missing", "?", 0f, 0f, 48f, 48f, 24).alignment = (TextAnchor)4;
			}
			NativeText((Transform)(object)nativeContent, "Name", item.itemName + (item.isScrap ? " [SCRAP]" : " [GEAR]"), 68f, y, 510f, 54f);
			NativeButton((Transform)(object)nativeContent, "SPAWN", 600f, y + 10f, 125f, 34f, delegate
			{
				if (IsHost)
				{
					SpawnItem(item);
				}
			});
		}

		private RectTransform NativeRect(Transform parent, string name, float x, float y, float width, float height)
		{
			//IL_0014: Unknown result type (might be due to invalid IL or missing references)
			//IL_0019: Unknown result type (might be due to invalid IL or missing references)
			//IL_003f: 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_0046: 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_004d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0058: Unknown result type (might be due to invalid IL or missing references)
			//IL_0067: Unknown result type (might be due to invalid IL or missing references)
			GameObject val = new GameObject(name, new Type[1] { typeof(RectTransform) });
			val.transform.SetParent(parent, false);
			RectTransform component = val.GetComponent<RectTransform>();
			Vector2 val2 = default(Vector2);
			((Vector2)(ref val2))..ctor(0f, 1f);
			component.pivot = val2;
			Vector2 anchorMin = (component.anchorMax = val2);
			component.anchorMin = anchorMin;
			component.anchoredPosition = new Vector2(x, 0f - y);
			component.sizeDelta = new Vector2(width, height);
			return component;
		}

		private RectTransform NativeBox(Transform parent, string name, float x, float y, float width, float height, Color color)
		{
			//IL_001a: Unknown result type (might be due to invalid IL or missing references)
			RectTransform obj = NativeRect(parent, name, x, y, width, height);
			((Graphic)((Component)obj).gameObject.AddComponent<Image>()).color = color;
			return obj;
		}

		private Text NativeText(Transform parent, string name, string value, float x, float y, float width, float height, int size = 17)
		{
			//IL_002f: Unknown result type (might be due to invalid IL or missing references)
			Text obj = ((Component)NativeRect(parent, name, x, y, width, height)).gameObject.AddComponent<Text>();
			obj.font = nativeFont;
			obj.fontSize = size;
			((Graphic)obj).color = Color.white;
			obj.text = value;
			obj.alignment = (TextAnchor)3;
			((Graphic)obj).raycastTarget = false;
			return obj;
		}

		private Button NativeButton(Transform parent, string label, float x, float y, float width, float height, Action callback)
		{
			//IL_002b: Unknown result type (might be due to invalid IL or missing references)
			//IL_005a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0064: Expected O, but got Unknown
			RectTransform val = NativeBox(parent, "Button", x, y, width, height, new Color(0.16f, 0.3f, 0.25f));
			Button obj = ((Component)val).gameObject.AddComponent<Button>();
			((Selectable)obj).targetGraphic = (Graphic)(object)((Component)val).GetComponent<Image>();
			((UnityEvent)obj.onClick).AddListener((UnityAction)delegate
			{
				callback();
			});
			NativeText((Transform)(object)val, "Label", label, 6f, 0f, width - 12f, height, 16).alignment = (TextAnchor)4;
			return obj;
		}

		private InputField NativeInput(Transform parent, string name, string value, float x, float y, float width, float height, Action<string> changed)
		{
			//IL_0028: Unknown result type (might be due to invalid IL or missing references)
			RectTransform val = NativeBox(parent, name, x, y, width, height, new Color(0.18f, 0.2f, 0.22f));
			InputField obj = ((Component)val).gameObject.AddComponent<InputField>();
			((Selectable)obj).targetGraphic = (Graphic)(object)((Component)val).GetComponent<Image>();
			obj.textComponent = NativeText((Transform)(object)val, "Value", "", 6f, 0f, width - 12f, height, 16);
			obj.text = value;
			((UnityEvent<string>)(object)obj.onValueChanged).AddListener((UnityAction<string>)delegate(string s)
			{
				changed(s);
			});
			return obj;
		}

		private void Awake()
		{
			//IL_000c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0016: Expected O, but got Unknown
			Instance = this;
			harmony = new Harmony("danylo.goodmenu");
			harmony.PatchAll(typeof(Plugin).Assembly);
			((BaseUnityPlugin)this).Logger.LogInfo((object)"GoodMenu 1.0.0 loaded. Host only. Insert / F6: menu. F7: stop watching.");
		}

		internal void Frame()
		{
			if (lastFrame == Time.frameCount)
			{
				return;
			}
			lastFrame = Time.frameCount;
			if (!IsHost || (Object)(object)Local == (Object)null)
			{
				CloseMenu();
				StopWatching();
				godMode = false;
				infiniteStamina = false;
				if ((Object)(object)nativeRoot != (Object)null)
				{
					nativeRoot.SetActive(false);
				}
				return;
			}
			if ((Object)(object)lockedPlayer != (Object)null && (Object)(object)lockedPlayer != (Object)(object)Local)
			{
				CloseMenu();
			}
			Keyboard current = Keyboard.current;
			if (current != null)
			{
				if (((ButtonControl)current.f7Key).wasPressedThisFrame)
				{
					StopWatching();
				}
				if ((((ButtonControl)current.insertKey).wasPressedThisFrame || ((ButtonControl)current.f6Key).wasPressedThisFrame) && lastToggle != Time.frameCount)
				{
					lastToggle = Time.frameCount;
					if (menu)
					{
						CloseMenu();
					}
					else if (!Local.inTerminalMenu && !Local.isTypingChat)
					{
						OpenMenu();
					}
				}
				if (menu && ((ButtonControl)current.escapeKey).wasPressedThisFrame)
				{
					CloseMenu();
				}
			}
			if (menu && Local.inTerminalMenu)
			{
				CloseMenu();
			}
			ApplyHostFeatures(Local);
			refreshTimer -= Time.unscaledDeltaTime;
			if (menu && refreshTimer <= 0f)
			{
				refreshTimer = 3f;
				SafeRefreshCatalog();
			}
			playerRefreshTimer -= Time.unscaledDeltaTime;
			if (menu && tab == 3 && playerRefreshTimer <= 0f)
			{
				playerRefreshTimer = 1f;
				RebuildNative(resetScroll: false);
			}
			if (menu)
			{
				LockInput();
			}
			NativeFrame();
			UpdateWatching();
		}

		private void OpenMenu()
		{
			//IL_005c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0061: Unknown result type (might be due to invalid IL or missing references)
			if (IsHost && !((Object)(object)Local == (Object)null))
			{
				menu = true;
				lockedPlayer = Local;
				lockedInput = true;
				savedLook = lockedPlayer.disableLookInput;
				savedMove = lockedPlayer.disableMoveInput;
				savedCursorVisible = Cursor.visible;
				savedCursorLock = Cursor.lockState;
				SafeRefreshCatalog();
				ShowNative();
				LockInput();
			}
		}

		private void LockInput()
		{
			if (IsHost && menu && !((Object)(object)lockedPlayer == (Object)null))
			{
				lockedPlayer.disableLookInput = true;
				lockedPlayer.disableMoveInput = true;
				Cursor.lockState = (CursorLockMode)0;
				Cursor.visible = true;
			}
		}

		private void CloseMenu()
		{
			//IL_0052: Unknown result type (might be due to invalid IL or missing references)
			menu = false;
			HideNative();
			if (lockedInput)
			{
				if ((Object)(object)lockedPlayer != (Object)null)
				{
					lockedPlayer.disableLookInput = savedLook;
					lockedPlayer.disableMoveInput = savedMove;
				}
				Cursor.visible = savedCursorVisible;
				Cursor.lockState = savedCursorLock;
				lockedInput = false;
				lockedPlayer = null;
			}
		}

		internal void ApplyHostFeatures(PlayerControllerB p)
		{
			if (IsHost && !((Object)(object)p == (Object)null) && !((Object)(object)p != (Object)(object)Local) && !p.isPlayerDead && infiniteStamina)
			{
				p.sprintMeter = 1f;
				p.isExhausted = false;
			}
		}

		internal bool Protects(PlayerControllerB p)
		{
			if (IsHost && godMode && (Object)(object)p != (Object)null && (Object)(object)p == (Object)(object)Local)
			{
				return !p.isPlayerDead;
			}
			return false;
		}

		internal void LogFailure(string where, Exception ex)
		{
			((BaseUnityPlugin)this).Logger.LogError((object)("GoodMenu " + where + ": " + ex));
		}

		private void OnDestroy()
		{
			CloseMenu();
			StopWatching();
			godMode = false;
			infiniteStamina = false;
			DestroyNative();
			Harmony obj = harmony;
			if (obj != null)
			{
				obj.UnpatchSelf();
			}
			Instance = null;
		}

		internal PlayerControllerB[] ConnectedPlayers()
		{
			if (!IsHost || (Object)(object)StartOfRound.Instance == (Object)null || StartOfRound.Instance.allPlayerScripts == null)
			{
				return (PlayerControllerB[])(object)new PlayerControllerB[0];
			}
			IReadOnlyList<ulong> ids = NetworkManager.Singleton.ConnectedClientsIds;
			return (from p in StartOfRound.Instance.allPlayerScripts
				where (Object)(object)p != (Object)null && !p.disconnectedMidGame && (p.isPlayerControlled || p.isPlayerDead) && ids.Contains(p.actualClientId)
				group p by p.actualClientId into g
				select g.OrderByDescending((PlayerControllerB p) => p.isPlayerControlled).First() into p
				orderby p.playerClientId
				select p).ToArray();
		}

		private void StartWatching(PlayerControllerB target)
		{
			//IL_004b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0051: Expected O, but got Unknown
			//IL_00ca: Unknown result type (might be due to invalid IL or missing references)
			//IL_0152: Unknown result type (might be due to invalid IL or missing references)
			//IL_015c: Expected O, but got Unknown
			if (!IsHost || (Object)(object)target == (Object)null || !ConnectedPlayers().Contains(target) || (Object)(object)Local == (Object)null || (Object)(object)Local.gameplayCamera == (Object)null)
			{
				return;
			}
			StopWatching();
			GameObject val = null;
			try
			{
				val = new GameObject("GoodMenu_WatchCamera");
				Object.DontDestroyOnLoad((Object)(object)val);
				watchCamera = val.AddComponent<Camera>();
				((Behaviour)watchCamera).enabled = false;
				watchCamera.CopyFrom(Local.gameplayCamera);
				watchCamera.nearClipPlane = 0.08f;
				watchCamera.depth = -10f;
				watchCamera.stereoTargetEye = (StereoTargetEyeMask)0;
				watchCamera.rect = new Rect(0f, 0f, 1f, 1f);
				Type type = Type.GetType("UnityEngine.Rendering.HighDefinition.HDAdditionalCameraData, Unity.RenderPipelines.HighDefinition.Runtime");
				if (type != null)
				{
					Component val2 = val.AddComponent(type);
					Component component = ((Component)Local.gameplayCamera).GetComponent(type);
					if ((Object)(object)component != (Object)null)
					{
						JsonUtility.FromJsonOverwrite(JsonUtility.ToJson((object)component), (object)val2);
					}
					PropertyInfo property = type.GetProperty("volumeAnchorOverride");
					if (property != null && property.CanWrite)
					{
						property.SetValue(val2, val.transform);
					}
				}
				watchTexture = new RenderTexture(640, 360, 24, (RenderTextureFormat)0);
				((Object)watchTexture).name = "GoodMenu_LiveView";
				watchTexture.Create();
				watchCamera.targetTexture = watchTexture;
				watchTarget = target;
				EnsureNative();
				watchImage.texture = (Texture)(object)watchTexture;
				watchPanel.SetActive(true);
				UpdateWatching();
				((Behaviour)watchCamera).enabled = true;
				notice = "Watching " + target.playerUsername + ". F7: stop.";
			}
			catch (Exception ex)
			{
				StopWatching();
				if ((Object)(object)val != (Object)null)
				{
					Object.Destroy((Object)(object)val);
				}
				notice = "Camera error: " + ex.Message;
				((BaseUnityPlugin)this).Logger.LogError((object)ex);
			}
		}

		private void UpdateWatching()
		{
			//IL_0083: Unknown result type (might be due to invalid IL or missing references)
			//IL_006c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0129: Unknown result type (might be due to invalid IL or missing references)
			//IL_012e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0130: Unknown result type (might be due to invalid IL or missing references)
			//IL_0135: 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_0144: 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_0153: 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_0088: Unknown result type (might be due to invalid IL or missing references)
			//IL_0094: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00be: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bf: 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_00ce: Unknown result type (might be due to invalid IL or missing references)
			//IL_00de: 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_00e8: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b0: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b1: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b2: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c8: Unknown result type (might be due to invalid IL or missing references)
			//IL_0166: Unknown result type (might be due to invalid IL or missing references)
			//IL_0167: Unknown result type (might be due to invalid IL or missing references)
			//IL_0168: Unknown result type (might be due to invalid IL or missing references)
			//IL_0169: Unknown result type (might be due to invalid IL or missing references)
			//IL_0184: Unknown result type (might be due to invalid IL or missing references)
			//IL_0189: Unknown result type (might be due to invalid IL or missing references)
			//IL_018a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0190: Unknown result type (might be due to invalid IL or missing references)
			//IL_019a: Unknown result type (might be due to invalid IL or missing references)
			//IL_019f: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a4: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)watchTarget == (Object)null)
			{
				return;
			}
			if (!IsHost || (Object)(object)watchCamera == (Object)null || !ConnectedPlayers().Contains(watchTarget))
			{
				StopWatching();
				return;
			}
			if (watchTarget.isPlayerDead)
			{
				Vector3 val = (((Object)(object)watchTarget.deadBody != (Object)null) ? ((Component)watchTarget.deadBody).transform.position : ((Component)watchTarget).transform.position);
				((Component)watchCamera).transform.position = val + new Vector3(0f, 2.1f, -1.3f);
				((Component)watchCamera).transform.rotation = Quaternion.LookRotation(val + Vector3.up * 0.3f - ((Component)watchCamera).transform.position);
			}
			else
			{
				Transform val2 = (((Object)(object)watchTarget.gameplayCamera != (Object)null) ? ((Component)watchTarget.gameplayCamera).transform : ((Component)watchTarget).transform);
				Vector3 position = val2.position;
				Vector3 val3 = -val2.forward * 1.3f + Vector3.up * 0.25f;
				RaycastHit val4 = default(RaycastHit);
				if ((Object)(object)StartOfRound.Instance != (Object)null && Physics.Linecast(position, position + val3, ref val4, StartOfRound.Instance.collidersAndRoomMaskAndDefault, (QueryTriggerInteraction)1))
				{
					val3 = ((RaycastHit)(ref val4)).point - position + val2.forward * 0.12f;
				}
				((Component)watchCamera).transform.position = position + val3;
				((Component)watchCamera).transform.rotation = val2.rotation;
			}
			watchLabel.text = watchTarget.playerUsername + (watchTarget.isPlayerDead ? " / DEAD - BODY VIEW" : (" / ALIVE / HP " + watchTarget.health)) + "  | F7: stop";
			((Behaviour)watchCamera).enabled = true;
			watchPanel.SetActive(true);
		}

		private void StopWatching()
		{
			watchTarget = null;
			if ((Object)(object)watchPanel != (Object)null)
			{
				watchPanel.SetActive(false);
			}
			if ((Object)(object)watchImage != (Object)null)
			{
				watchImage.texture = null;
			}
			if ((Object)(object)watchCamera != (Object)null)
			{
				((Behaviour)watchCamera).enabled = false;
				watchCamera.targetTexture = null;
				Object.Destroy((Object)(object)((Component)watchCamera).gameObject);
				watchCamera = null;
			}
			if ((Object)(object)watchTexture != (Object)null)
			{
				watchTexture.Release();
				Object.Destroy((Object)(object)watchTexture);
				watchTexture = null;
			}
		}
	}
	[HarmonyPatch(typeof(HUDManager), "Update")]
	internal static class HudFrame
	{
		private static float nextError;

		private static void Postfix()
		{
			Plugin instance = Plugin.Instance;
			if ((Object)(object)instance == (Object)null)
			{
				return;
			}
			try
			{
				instance.Frame();
			}
			catch (Exception ex)
			{
				if (Time.realtimeSinceStartup >= nextError)
				{
					nextError = Time.realtimeSinceStartup + 5f;
					instance.LogFailure("HUD", ex);
				}
			}
		}
	}
	[HarmonyPatch(typeof(PlayerControllerB), "Update")]
	internal static class PlayerFrame
	{
		private static void Postfix(PlayerControllerB __instance)
		{
			Plugin instance = Plugin.Instance;
			if ((Object)(object)instance != (Object)null && Plugin.IsHost && (Object)(object)__instance == (Object)(object)Plugin.Local)
			{
				instance.ApplyHostFeatures(__instance);
			}
		}
	}
	[HarmonyPatch(typeof(PlayerControllerB), "DamagePlayer")]
	internal static class DamageProtection
	{
		private static bool Prefix(PlayerControllerB __instance)
		{
			if (!((Object)(object)Plugin.Instance == (Object)null))
			{
				return !Plugin.Instance.Protects(__instance);
			}
			return true;
		}
	}
	[HarmonyPatch(typeof(PlayerControllerB), "KillPlayer")]
	internal static class DeathProtection
	{
		private static bool Prefix(PlayerControllerB __instance)
		{
			if (!((Object)(object)Plugin.Instance == (Object)null))
			{
				return !Plugin.Instance.Protects(__instance);
			}
			return true;
		}
	}
}