Decompiled source of CruiserTerminal v1.0.0

CruiserTerminal.dll

Decompiled 4 hours ago
using System;
using System.Collections;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Logging;
using CruiserTerminal.NetcodePatcher;
using GameNetcodeStuff;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Unity.Netcode;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.Controls;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("CruiserTerminal")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+2571154d93ffd7067864b0bca403752439388e04")]
[assembly: AssemblyProduct("CruiserTerminal")]
[assembly: AssemblyTitle("CruiserTerminal")]
[assembly: AssemblyVersion("1.0.0.0")]
[module: NetcodePatchedAssembly]
internal class <Module>
{
	static <Module>()
	{
	}
}
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;
		}
	}
}
namespace CruiserTerminal
{
	public class CruiserTerminal : NetworkBehaviour
	{
		public bool cruiserTerminalInUse;

		public GameObject canvasMainContainer;

		public Terminal terminalScript;

		public GameObject cruiserTerminal;

		public InteractTrigger interactTrigger;

		public InteractTrigger terminalInteractTrigger;

		public AudioSource cruiserTerminalAudio;

		public AudioClip[] cruiserKeyboardClips;

		public AudioClip[] cruiserSyncedAudios;

		public PlayerActions playerActions;

		public GameObject terminalPos;

		public AudioClip enterTerminalSFX;

		public AudioClip leaveTerminalSFX;

		public Light terminalLight;

		public VehicleController cruiserController;

		private float timeSinceLastKeyboardPress;

		internal static CruiserTerminal Instance { get; private set; }

		public override void OnNetworkSpawn()
		{
			if (NetworkManager.Singleton.IsHost || NetworkManager.Singleton.IsServer)
			{
				CruiserTerminal instance = Instance;
				if (instance != null)
				{
					((Component)instance).gameObject.GetComponent<NetworkObject>().Despawn(true);
				}
			}
			Instance = this;
			((NetworkBehaviour)this).OnNetworkSpawn();
		}

		private void Awake()
		{
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_000c: Expected O, but got Unknown
			//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)
			playerActions = new PlayerActions();
			MovementActions movement = playerActions.Movement;
			((MovementActions)(ref movement)).Enable();
		}

		private void Start()
		{
			cruiserTerminalInUse = false;
			canvasMainContainer = CloneCanvas();
			canvasMainContainer.SetActive(false);
			terminalScript = GameObject.Find("Environment/HangarShip/Terminal/TerminalTrigger/TerminalScript").GetComponent<Terminal>();
			cruiserTerminal = ((Component)this).gameObject;
			interactTrigger = GameObject.Find("Cruiser Terminal/TerminalTrigger/Trigger").GetComponent<InteractTrigger>();
			((UnityEvent<PlayerControllerB>)(object)interactTrigger.onInteractEarly).AddListener((UnityAction<PlayerControllerB>)BeginUsingCruiserTerminal);
			terminalInteractTrigger = ((Component)terminalScript).gameObject.GetComponent<InteractTrigger>();
			cruiserTerminalAudio = terminalScript.terminalAudio;
			cruiserKeyboardClips = terminalScript.keyboardClips;
			cruiserSyncedAudios = terminalScript.syncedAudios;
			terminalPos = GameObject.Find("terminalPosition");
			enterTerminalSFX = terminalScript.enterTerminalSFX;
			leaveTerminalSFX = terminalScript.leaveTerminalSFX;
			terminalLight = GameObject.Find("Cruiser Terminal/terminalLight").GetComponent<Light>();
			cruiserController = GameObject.Find("CompanyCruiser(Clone)").GetComponent<VehicleController>();
		}

		private void Update()
		{
			//IL_0017: 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)
			cruiserTerminal.transform.position = terminalPos.transform.position;
			cruiserTerminal.transform.rotation = terminalPos.transform.rotation;
			if (cruiserTerminalInUse)
			{
				Object.Destroy((Object)(object)canvasMainContainer);
				canvasMainContainer = CloneCanvas();
				if (((ButtonControl)Keyboard.current.anyKey).wasPressedThisFrame && timeSinceLastKeyboardPress > 0.07f)
				{
					timeSinceLastKeyboardPress = 0f;
					RoundManager.PlayRandomClip(cruiserTerminalAudio, cruiserKeyboardClips, true, 1f, 0, 1000);
				}
				timeSinceLastKeyboardPress += Time.deltaTime;
			}
		}

		internal GameObject CloneCanvas()
		{
			return Object.Instantiate<GameObject>(GameObject.Find("Environment/HangarShip/Terminal/Canvas/MainContainer"), GameObject.Find("Cruiser Terminal/Canvas").transform);
		}

		[ServerRpc(RequireOwnership = false)]
		public void SetCruiserTerminalInUseServerRpc(bool inUse)
		{
			//IL_0024: Unknown result type (might be due to invalid IL or missing references)
			//IL_002e: Invalid comparison between Unknown and I4
			//IL_00a7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b1: Invalid comparison between Unknown and I4
			//IL_005f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0068: Unknown result type (might be due to invalid IL or missing references)
			//IL_006d: Unknown result type (might be due to invalid IL or missing references)
			//IL_007d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0083: Unknown result type (might be due to invalid IL or missing references)
			//IL_0097: Unknown result type (might be due to invalid IL or missing references)
			NetworkManager networkManager = ((NetworkBehaviour)this).NetworkManager;
			if (networkManager != null && networkManager.IsListening)
			{
				if ((int)base.__rpc_exec_stage != 1 && (networkManager.IsClient || networkManager.IsHost))
				{
					ServerRpcParams val = default(ServerRpcParams);
					FastBufferWriter val2 = ((NetworkBehaviour)this).__beginSendServerRpc(1979242128u, val, (RpcDelivery)0);
					((FastBufferWriter)(ref val2)).WriteValueSafe<bool>(ref inUse, default(ForPrimitives));
					((NetworkBehaviour)this).__endSendServerRpc(ref val2, 1979242128u, val, (RpcDelivery)0);
				}
				if ((int)base.__rpc_exec_stage == 1 && (networkManager.IsServer || networkManager.IsHost))
				{
					CTPlugin.mls.LogMessage((object)"sending stuff to clients");
					SetCruiserTerminalInUseClientRpc(inUse);
				}
			}
		}

		[ClientRpc]
		public void SetCruiserTerminalInUseClientRpc(bool inUse)
		{
			//IL_0024: Unknown result type (might be due to invalid IL or missing references)
			//IL_002e: Invalid comparison between Unknown and I4
			//IL_00a7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b1: Invalid comparison between Unknown and I4
			//IL_005f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0068: Unknown result type (might be due to invalid IL or missing references)
			//IL_006d: Unknown result type (might be due to invalid IL or missing references)
			//IL_007d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0083: Unknown result type (might be due to invalid IL or missing references)
			//IL_0097: Unknown result type (might be due to invalid IL or missing references)
			NetworkManager networkManager = ((NetworkBehaviour)this).NetworkManager;
			if (networkManager == null || !networkManager.IsListening)
			{
				return;
			}
			if ((int)base.__rpc_exec_stage != 2 && (networkManager.IsServer || networkManager.IsHost))
			{
				ClientRpcParams val = default(ClientRpcParams);
				FastBufferWriter val2 = ((NetworkBehaviour)this).__beginSendClientRpc(3680983168u, val, (RpcDelivery)0);
				((FastBufferWriter)(ref val2)).WriteValueSafe<bool>(ref inUse, default(ForPrimitives));
				((NetworkBehaviour)this).__endSendClientRpc(ref val2, 3680983168u, val, (RpcDelivery)0);
			}
			if ((int)base.__rpc_exec_stage == 2 && (networkManager.IsClient || networkManager.IsHost))
			{
				CTPlugin.mls.LogMessage((object)("cruiser terminal in use: " + inUse));
				((Behaviour)terminalLight).enabled = inUse;
				if (inUse)
				{
					cruiserTerminalAudio.PlayOneShot(enterTerminalSFX);
				}
				else
				{
					cruiserTerminalAudio.PlayOneShot(leaveTerminalSFX);
				}
				interactTrigger.interactable = !inUse;
				terminalInteractTrigger.interactable = !inUse;
			}
		}

		public void BeginUsingCruiserTerminal(PlayerControllerB player)
		{
			if (!cruiserTerminalInUse)
			{
				SetCruiserTerminalInUseServerRpc(inUse: true);
				cruiserTerminalInUse = true;
				((MonoBehaviour)this).StartCoroutine(waitUntilFrameEndToSetActive(active: true));
				cruiserController.SetVehicleCollisionForPlayer(false, GameNetworkManager.Instance.localPlayerController);
				terminalScript.BeginUsingTerminal();
			}
		}

		public void QuitCruiserTerminal()
		{
			SetCruiserTerminalInUseServerRpc(inUse: false);
			cruiserController.SetVehicleCollisionForPlayer(true, GameNetworkManager.Instance.localPlayerController);
			cruiserTerminalInUse = false;
			((MonoBehaviour)this).StartCoroutine(waitUntilFrameEndToSetActive(active: false));
			terminalScript.QuitTerminal(true);
			interactTrigger.StopSpecialAnimation();
		}

		private void OnEnable()
		{
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			//IL_000c: Unknown result type (might be due to invalid IL or missing references)
			MovementActions movement = playerActions.Movement;
			((MovementActions)(ref movement)).OpenMenu.performed += PressESC;
		}

		private void OnDisable()
		{
			//IL_0017: 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)
			CTPlugin.mls.LogInfo((object)"Terminal disabled, disabling ESC key listener");
			MovementActions movement = playerActions.Movement;
			((MovementActions)(ref movement)).OpenMenu.performed -= PressESC;
		}

		private void PressESC(CallbackContext context)
		{
			if (((CallbackContext)(ref context)).performed && cruiserTerminalInUse)
			{
				QuitCruiserTerminal();
			}
		}

		private IEnumerator waitUntilFrameEndToSetActive(bool active)
		{
			yield return (object)new WaitForEndOfFrame();
			canvasMainContainer.SetActive(active);
		}

		protected override void __initializeVariables()
		{
			((NetworkBehaviour)this).__initializeVariables();
		}

		[RuntimeInitializeOnLoadMethod]
		internal static void InitializeRPCS_CruiserTerminal()
		{
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			//IL_001b: Expected O, but got Unknown
			//IL_002c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0036: Expected O, but got Unknown
			NetworkManager.__rpc_func_table.Add(1979242128u, new RpcReceiveHandler(__rpc_handler_1979242128));
			NetworkManager.__rpc_func_table.Add(3680983168u, new RpcReceiveHandler(__rpc_handler_3680983168));
		}

		private static void __rpc_handler_1979242128(NetworkBehaviour target, FastBufferReader reader, __RpcParams rpcParams)
		{
			//IL_002f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0035: Unknown result type (might be due to invalid IL or missing references)
			//IL_0044: Unknown result type (might be due to invalid IL or missing references)
			//IL_005e: Unknown result type (might be due to invalid IL or missing references)
			NetworkManager networkManager = target.NetworkManager;
			if (networkManager != null && networkManager.IsListening)
			{
				bool cruiserTerminalInUseServerRpc = default(bool);
				((FastBufferReader)(ref reader)).ReadValueSafe<bool>(ref cruiserTerminalInUseServerRpc, default(ForPrimitives));
				target.__rpc_exec_stage = (__RpcExecStage)1;
				((CruiserTerminal)(object)target).SetCruiserTerminalInUseServerRpc(cruiserTerminalInUseServerRpc);
				target.__rpc_exec_stage = (__RpcExecStage)0;
			}
		}

		private static void __rpc_handler_3680983168(NetworkBehaviour target, FastBufferReader reader, __RpcParams rpcParams)
		{
			//IL_002f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0035: Unknown result type (might be due to invalid IL or missing references)
			//IL_0044: Unknown result type (might be due to invalid IL or missing references)
			//IL_005e: Unknown result type (might be due to invalid IL or missing references)
			NetworkManager networkManager = target.NetworkManager;
			if (networkManager != null && networkManager.IsListening)
			{
				bool cruiserTerminalInUseClientRpc = default(bool);
				((FastBufferReader)(ref reader)).ReadValueSafe<bool>(ref cruiserTerminalInUseClientRpc, default(ForPrimitives));
				target.__rpc_exec_stage = (__RpcExecStage)2;
				((CruiserTerminal)(object)target).SetCruiserTerminalInUseClientRpc(cruiserTerminalInUseClientRpc);
				target.__rpc_exec_stage = (__RpcExecStage)0;
			}
		}

		protected internal override string __getTypeName()
		{
			return "CruiserTerminal";
		}
	}
	public static class CTFunctions
	{
		public static void Spawn()
		{
			//IL_010b: Unknown result type (might be due to invalid IL or missing references)
			if (Object.op_Implicit((Object)(object)GameObject.Find("Cruiser Terminal")))
			{
				return;
			}
			GameObject val = GameObject.Find("CompanyCruiser(Clone)");
			GameObject val2 = Object.Instantiate<GameObject>(CTPlugin.terminalPrefab);
			((Object)val2).name = "Cruiser Terminal";
			NetworkObject component = val.GetComponent<NetworkObject>();
			CTPlugin.mls.LogInfo((object)("cruiserNO is null = " + ((Object)(object)component == (Object)null)));
			Object obj = CTPlugin.mainAssetBundle.LoadAsset("terminalPosition.prefab");
			GameObject val3 = Object.Instantiate<GameObject>((GameObject)(object)((obj is GameObject) ? obj : null));
			((Object)val3).name = "terminalPosition";
			if (NetworkManager.Singleton.IsHost || NetworkManager.Singleton.IsServer)
			{
				val2.GetComponent<NetworkObject>().Spawn(false);
			}
			if (NetworkManager.Singleton.IsClient)
			{
				try
				{
					Object.Destroy((Object)(object)GameObject.Find("Cruiser Terminal(Clone)"));
				}
				catch
				{
				}
			}
			val3.transform.SetParent(val.transform);
			val3.transform.localPosition = new Vector3(1.293f, 0.938f, -3.274f);
		}
	}
	public static class CTPatches
	{
		[HarmonyPostfix]
		[HarmonyPatch(typeof(VehicleController), "Awake")]
		private static void StartPatch()
		{
			CTFunctions.Spawn();
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(GameNetworkManager), "Start")]
		private static void AddPrefabsToNetwork()
		{
			CTPlugin.terminalPrefab = CTPlugin.mainAssetBundle.LoadAsset<GameObject>("CruiserTerminal.prefab");
			CTPlugin.terminalPrefab.AddComponent<CruiserTerminal>();
			NetworkManager.Singleton.AddNetworkPrefab(CTPlugin.terminalPrefab);
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(Terminal), "OnDisable")]
		[HarmonyPatch(typeof(VehicleController), "DestroyCar")]
		[HarmonyPatch(typeof(VehicleController), "OnDisable")]
		private static void TerminalOnDisablePatch()
		{
			CTPlugin.mls.LogInfo((object)"Destroying the terminal in a cruiser");
			Object.Destroy((Object)(object)GameObject.Find("Cruiser Terminal"));
			Object.Destroy((Object)(object)GameObject.Find("terminalPosition"));
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(Terminal), "SetTerminalInUseClientRpc")]
		private static void SetTerminalInUsePatch(ref bool ___terminalInUse)
		{
			GameObject val = GameObject.Find("Cruiser Terminal/TerminalTrigger/Trigger");
			if ((Object)(object)val != (Object)null)
			{
				val.GetComponent<InteractTrigger>().interactable = !___terminalInUse;
				CTPlugin.mls.LogInfo((object)("cruiser terminal interactable = " + !___terminalInUse));
			}
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(ManualCameraRenderer), "MeetsCameraEnabledConditions")]
		private static void MeetsCameraEnabledConditionsPatch(ref bool __result)
		{
			if (!((Object)(object)StartOfRound.Instance == (Object)null) && !StartOfRound.Instance.inShipPhase && !__result)
			{
				__result = true;
			}
		}
	}
	[BepInPlugin("mborsh.CruiserTerminal", "CruiserTerminal", "1.0.0")]
	public class CTPlugin : BaseUnityPlugin
	{
		private const string modGUID = "mborsh.CruiserTerminal";

		private const string modName = "CruiserTerminal";

		private const string modVersion = "1.0.0";

		private readonly Harmony harmony = new Harmony("mborsh.CruiserTerminal");

		public static ManualLogSource mls;

		public static AssetBundle mainAssetBundle;

		private static CTPlugin Instance;

		public static GameObject terminalPrefab;

		private static void NetcodePatcher()
		{
			Type[] types = Assembly.GetExecutingAssembly().GetTypes();
			Type[] array = types;
			foreach (Type type in array)
			{
				MethodInfo[] methods = type.GetMethods(BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic);
				MethodInfo[] array2 = methods;
				foreach (MethodInfo methodInfo in array2)
				{
					object[] customAttributes = methodInfo.GetCustomAttributes(typeof(RuntimeInitializeOnLoadMethodAttribute), inherit: false);
					if (customAttributes.Length != 0)
					{
						methodInfo.Invoke(null, null);
					}
				}
			}
		}

		private void Awake()
		{
			NetcodePatcher();
			Instance = this;
			mls = Logger.CreateLogSource("Cruiser Terminal");
			mls = ((BaseUnityPlugin)this).Logger;
			mls.LogInfo((object)"Cruiser Terminal loaded. Patching.");
			harmony.PatchAll(typeof(CTPatches));
			if (!LoadAssetBundle())
			{
				mls.LogError((object)"Failed to load asset bundle! Abort mission!");
			}
			static bool LoadAssetBundle()
			{
				mls.LogInfo((object)"Loading AssetBundle...");
				string directoryName = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
				mainAssetBundle = AssetBundle.LoadFromFile(Path.Combine(directoryName, "CruiserTerminal"));
				if ((Object)(object)mainAssetBundle == (Object)null)
				{
					return false;
				}
				mls.LogInfo((object)("AssetBundle " + ((Object)mainAssetBundle).name + " loaded from " + directoryName + "."));
				return true;
			}
		}
	}
}
namespace CruiserTerminal.NetcodePatcher
{
	[AttributeUsage(AttributeTargets.Module)]
	internal class NetcodePatchedAssemblyAttribute : Attribute
	{
	}
}