Decompiled source of ChairView v0.0.3

ChairView.dll

Decompiled 4 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Logging;
using CG;
using CG.Client;
using CG.Game;
using CG.Game.Player;
using CG.Input;
using CG.Ship.Modules;
using CG.Space;
using CG.Space.SpaceObjects;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Photon.Pun;
using UnityEngine;
using UnityEngine.InputSystem;
using VoidManager;
using VoidManager.MPModChecks;
using VoidManager.Utilities;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyCompany("ChairView")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyDescription("Template")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("ChairView")]
[assembly: AssemblyTitle("ChairView")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace ChairView
{
	internal static class MyPluginInfo
	{
		internal const string PLUGIN_GUID = "id107.chairview";

		internal const string PLUGIN_NAME = "ChairView";

		internal const string PLUGIN_VERSION = "0.0.3";
	}
	[BepInPlugin("id107.chairview", "ChairView", "0.0.3")]
	[BepInProcess("Void Crew.exe")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class BepinPlugin : BaseUnityPlugin
	{
		internal static ManualLogSource Log;

		private void Awake()
		{
			Log = ((BaseUnityPlugin)this).Logger;
			Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly(), "id107.chairview");
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Plugin id107.chairview is loaded!");
		}
	}
	[HarmonyPatch(typeof(ChairBase))]
	internal class ChairBasePatch
	{
		private static ChairBase playerChair;

		internal static bool isHelm = true;

		private static readonly MethodInfo AbilityStartedMethod = AccessTools.Method(typeof(ControllingHelm), "AbilityStarted", (Type[])null, (Type[])null);

		private static readonly MethodInfo AbilityStoppedMethod = AccessTools.Method(typeof(ControllingHelm), "AbilityStopped", (Type[])null, (Type[])null);

		[HarmonyPostfix]
		[HarmonyPatch("TakeChairLocal")]
		private static void TakeChairLocal(ChairBase __instance)
		{
			ClientGame current = ClientGame.Current;
			object obj;
			if (current == null)
			{
				obj = null;
			}
			else
			{
				AbstractPlayerControlledShip playerShip = current.PlayerShip;
				if (playerShip == null)
				{
					obj = null;
				}
				else
				{
					GameObject gameObject = ((Component)playerShip).gameObject;
					if (gameObject == null)
					{
						obj = null;
					}
					else
					{
						Helm componentInChildren = gameObject.GetComponentInChildren<Helm>();
						obj = ((componentInChildren != null) ? componentInChildren.Chair : null);
					}
				}
			}
			ChairBase val = (ChairBase)obj;
			if (!((Object)(object)val == (Object)null) && ((MonoBehaviourPun)__instance).photonView.Owner == PhotonNetwork.LocalPlayer && !(__instance is TurretChair) && __instance is TakeoverChair && !((Object)(object)__instance == (Object)(object)val))
			{
				playerChair = __instance;
				ControllingHelmPatch.controllingHelm.AbilityActivator = (IAbilityActivator)(object)((Component)ClientGame.Current.PlayerShip).gameObject.GetComponentInChildren<Helm>();
				isHelm = false;
				AbilityStartedMethod.Invoke(ControllingHelmPatch.controllingHelm, null);
			}
		}

		[HarmonyPrefix]
		[HarmonyPatch("FreeChairLocal")]
		private static void FreeChairLocal(ChairBase __instance)
		{
			if (!((Object)(object)__instance != (Object)(object)playerChair))
			{
				playerChair = null;
				AbilityStoppedMethod.Invoke(ControllingHelmPatch.controllingHelm, new object[1] { true });
				isHelm = true;
			}
		}

		internal static void Reset(object sender = null, EventArgs e = null)
		{
			if (ControllingHelmPatch.controllingHelm != null)
			{
				AbilityStoppedMethod.Invoke(ControllingHelmPatch.controllingHelm, new object[1] { true });
				isHelm = true;
				ControllingHelmPatch.controllingHelm = null;
			}
		}
	}
	[HarmonyPatch(typeof(ControllingHelm))]
	internal class ControllingHelmPatch
	{
		internal static ControllingHelm controllingHelm;

		private static List<ControllingHelm> potentialHelm = new List<ControllingHelm>();

		private static LocalPlayer nullPlayer;

		private static readonly FieldInfo helmParentSpacecraftField = AccessTools.Field(typeof(ControllingHelm), "_helmParentSpacecraft");

		private static readonly FieldInfo helmField = AccessTools.Field(typeof(ControllingHelm), "_helm");

		private static readonly FieldInfo localPlayerField = AccessTools.Field(typeof(ControllingHelm), "_localPlayer");

		private static readonly MethodInfo ToggleExternalThirdPersonShipViewMethod = AccessTools.Method(typeof(ControllingHelm), "ToggleExternalThirdPersonShipView", (Type[])null, (Type[])null);

		[HarmonyPostfix]
		[HarmonyPatch("Awake")]
		private static void Awake(ControllingHelm __instance, LocalPlayer ____localPlayer)
		{
			nullPlayer = ____localPlayer;
			if (potentialHelm.Count == 0)
			{
				Events.Instance.LateUpdate += CheckHelm;
			}
			potentialHelm.Add(__instance);
		}

		private static void CheckHelm(object sender, EventArgs e)
		{
			//IL_0028: Unknown result type (might be due to invalid IL or missing references)
			//IL_002e: Expected O, but got Unknown
			for (int num = potentialHelm.Count - 1; num >= 0; num--)
			{
				ControllingHelm obj = potentialHelm[num];
				LocalPlayer val = (LocalPlayer)localPlayerField.GetValue(obj);
				if ((Object)(object)LocalPlayer.Instance == (Object)(object)val)
				{
					controllingHelm = obj;
					potentialHelm.RemoveAt(num);
				}
				else
				{
					potentialHelm.RemoveAt(num);
				}
			}
			if (potentialHelm.Count == 0)
			{
				Events.Instance.LateUpdate -= CheckHelm;
			}
		}

		[HarmonyTranspiler]
		[HarmonyPatch("AbilityStarted")]
		private static IEnumerable<CodeInstruction> AbilityStarted(IEnumerable<CodeInstruction> instructions)
		{
			//IL_000d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0017: Expected O, but got Unknown
			//IL_001f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0029: Expected O, but got Unknown
			//IL_0031: Unknown result type (might be due to invalid IL or missing references)
			//IL_003b: Expected O, but got Unknown
			//IL_0043: Unknown result type (might be due to invalid IL or missing references)
			//IL_004d: Expected O, but got Unknown
			//IL_0055: Unknown result type (might be due to invalid IL or missing references)
			//IL_005f: Expected O, but got Unknown
			//IL_0067: Unknown result type (might be due to invalid IL or missing references)
			//IL_0071: Expected O, but got Unknown
			//IL_0080: Unknown result type (might be due to invalid IL or missing references)
			//IL_008a: Expected O, but got Unknown
			//IL_00a7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b1: Expected O, but got Unknown
			List<CodeInstruction> list = new List<CodeInstruction>
			{
				new CodeInstruction(OpCodes.Call, (object)null),
				new CodeInstruction(OpCodes.Ldfld, (object)null),
				new CodeInstruction(OpCodes.Ldarg_0, (object)null),
				new CodeInstruction(OpCodes.Ldfld, (object)null),
				new CodeInstruction(OpCodes.Ldc_I4_1, (object)null),
				new CodeInstruction(OpCodes.Callvirt, (object)null)
			};
			List<CodeInstruction> list2 = new List<CodeInstruction>();
			list2.Add(new CodeInstruction(OpCodes.Ldc_I4_1, (object)null));
			list2.Add(new CodeInstruction(OpCodes.Call, (object)AccessTools.Method(typeof(ControllingHelmPatch), "ShipControlEvent", (Type[])null, (Type[])null)));
			List<CodeInstruction> list3 = list2;
			return HarmonyHelpers.PatchBySequence(instructions, (IEnumerable<CodeInstruction>)list, (IEnumerable<CodeInstruction>)list3, (PatchMode)2, (CheckMode)2, false);
		}

		[HarmonyTranspiler]
		[HarmonyPatch("AbilityStopped")]
		private static IEnumerable<CodeInstruction> AbilityStopped(IEnumerable<CodeInstruction> instructions)
		{
			//IL_000d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0017: Expected O, but got Unknown
			//IL_001f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0029: Expected O, but got Unknown
			//IL_0031: Unknown result type (might be due to invalid IL or missing references)
			//IL_003b: Expected O, but got Unknown
			//IL_0043: Unknown result type (might be due to invalid IL or missing references)
			//IL_004d: Expected O, but got Unknown
			//IL_0055: Unknown result type (might be due to invalid IL or missing references)
			//IL_005f: Expected O, but got Unknown
			//IL_0067: Unknown result type (might be due to invalid IL or missing references)
			//IL_0071: Expected O, but got Unknown
			//IL_0080: Unknown result type (might be due to invalid IL or missing references)
			//IL_008a: Expected O, but got Unknown
			//IL_00a7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b1: Expected O, but got Unknown
			List<CodeInstruction> list = new List<CodeInstruction>
			{
				new CodeInstruction(OpCodes.Call, (object)null),
				new CodeInstruction(OpCodes.Ldfld, (object)null),
				new CodeInstruction(OpCodes.Ldarg_0, (object)null),
				new CodeInstruction(OpCodes.Ldfld, (object)null),
				new CodeInstruction(OpCodes.Ldc_I4_0, (object)null),
				new CodeInstruction(OpCodes.Callvirt, (object)null)
			};
			List<CodeInstruction> list2 = new List<CodeInstruction>();
			list2.Add(new CodeInstruction(OpCodes.Ldc_I4_0, (object)null));
			list2.Add(new CodeInstruction(OpCodes.Call, (object)AccessTools.Method(typeof(ControllingHelmPatch), "ShipControlEvent", (Type[])null, (Type[])null)));
			List<CodeInstruction> list3 = list2;
			return HarmonyHelpers.PatchBySequence(instructions, (IEnumerable<CodeInstruction>)list, (IEnumerable<CodeInstruction>)list3, (PatchMode)2, (CheckMode)2, false);
		}

		private static void ShipControlEvent(bool b)
		{
			//IL_0023: Unknown result type (might be due to invalid IL or missing references)
			//IL_002e: Expected O, but got Unknown
			if (ChairBasePatch.isHelm)
			{
				ViewEventBus.Instance.OnShipControlToggle.Publish((ISpaceCraft)helmParentSpacecraftField.GetValue(controllingHelm), b);
			}
		}

		[HarmonyPrefix]
		[HarmonyPatch("EnableInput")]
		private static bool EnableInput()
		{
			//IL_002b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0031: Expected O, but got Unknown
			if (ChairBasePatch.isHelm)
			{
				return true;
			}
			InputActionReferences inputActionReferences = ServiceBase<InputService>.Instance.InputActionReferences;
			Helm @object = (Helm)helmField.GetValue(controllingHelm);
			inputActionReferences.ChangeView.action.started += (Action<CallbackContext>)ToggleExternalThirdPersonShipViewMethod.CreateDelegate(typeof(Action<CallbackContext>), controllingHelm);
			inputActionReferences.CameraViewHorizontal.action.performed += @object.SetMouseDeltaX;
			inputActionReferences.CameraViewHorizontal.action.canceled += @object.SetMouseDeltaX;
			inputActionReferences.CameraViewVertical.action.performed += @object.SetMouseDeltaY;
			inputActionReferences.CameraViewVertical.action.canceled += @object.SetMouseDeltaY;
			return false;
		}

		[HarmonyPrefix]
		[HarmonyPatch("DisableInput")]
		private static bool DisableInput()
		{
			//IL_002b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0031: Expected O, but got Unknown
			if (ChairBasePatch.isHelm)
			{
				return true;
			}
			InputActionReferences inputActionReferences = ServiceBase<InputService>.Instance.InputActionReferences;
			Helm @object = (Helm)helmField.GetValue(controllingHelm);
			inputActionReferences.ChangeView.action.started -= (Action<CallbackContext>)ToggleExternalThirdPersonShipViewMethod.CreateDelegate(typeof(Action<CallbackContext>), controllingHelm);
			inputActionReferences.CameraViewHorizontal.action.performed -= @object.SetMouseDeltaX;
			inputActionReferences.CameraViewHorizontal.action.canceled -= @object.SetMouseDeltaX;
			inputActionReferences.CameraViewVertical.action.performed -= @object.SetMouseDeltaY;
			inputActionReferences.CameraViewVertical.action.canceled -= @object.SetMouseDeltaY;
			return false;
		}
	}
	public class VoidManagerPlugin : VoidPlugin
	{
		public override MultiplayerType MPType => (MultiplayerType)3;

		public override string Author => "18107";

		public override string Description => "Pressing switch view (TAB) while in a chair shows the ship exterior view";

		static VoidManagerPlugin()
		{
			Events.Instance.JoinedRoom += ChairBasePatch.Reset;
			Events.Instance.LeftRoom += ChairBasePatch.Reset;
		}
	}
}