Decompiled source of KindTeleporters v1.0.3

KindTeleporters.dll

Decompiled 9 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.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Logging;
using GameNetcodeStuff;
using HarmonyLib;
using Unity.Netcode;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("KindTeleporters")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("KindTeleporters")]
[assembly: AssemblyCopyright("Copyright ©  2023")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("9ed8bd95-d965-4a14-8dd4-9049815d47c6")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace KindTeleporters
{
	[BepInPlugin("stormytuna.KindTeleporters", "KindTeleporters", "1.0.2")]
	public class KindTeleportersBase : BaseUnityPlugin
	{
		public const string ModGUID = "stormytuna.KindTeleporters";

		public const string ModName = "KindTeleporters";

		public const string ModVersion = "1.0.2";

		public static ManualLogSource Log = Logger.CreateLogSource("stormytuna.KindTeleporters");

		public static KindTeleportersBase Instance;

		private readonly Harmony harmony = new Harmony("stormytuna.KindTeleporters");

		private void Awake()
		{
			if (Instance == null)
			{
				Instance = this;
			}
			Log.LogInfo((object)"Kind Teleporters has awoken!");
			harmony.PatchAll();
		}
	}
}
namespace KindTeleporters.Patches
{
	[HarmonyPatch(typeof(ShipTeleporter))]
	public class ShipTeleporterPatch
	{
		private static readonly CodeMatch[] inverseTeleporterPatchIlMatch = (CodeMatch[])(object)new CodeMatch[4]
		{
			new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => CodeInstructionExtensions.IsLdloc(i, (LocalBuilder)null)), (string)null),
			new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => CodeInstructionExtensions.LoadsConstant(i, 1L)), (string)null),
			new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => CodeInstructionExtensions.LoadsConstant(i, 0L)), (string)null),
			new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => CodeInstructionExtensions.Calls(i, dropAllHeldItemsMethodInfo)), (string)null)
		};

		private static readonly CodeMatch[] teleporterPatchIlMatch = (CodeMatch[])(object)new CodeMatch[5]
		{
			new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => CodeInstructionExtensions.IsLdarg(i, (int?)0)), (string)null),
			new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => i.opcode == OpCodes.Ldfld), (string)null),
			new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => CodeInstructionExtensions.LoadsConstant(i, 1L)), (string)null),
			new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => CodeInstructionExtensions.LoadsConstant(i, 0L)), (string)null),
			new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => CodeInstructionExtensions.Calls(i, dropAllHeldItemsMethodInfo)), (string)null)
		};

		private static readonly MethodInfo dropAllHeldItemsMethodInfo = typeof(PlayerControllerB).GetMethod("DropAllHeldItems", BindingFlags.Instance | BindingFlags.Public);

		private static readonly MethodInfo dropAllButHeldItemMethodInfo = typeof(ShipTeleporterPatch).GetMethod("DropAllButHeldItem", BindingFlags.Static | BindingFlags.NonPublic);

		[HarmonyTranspiler]
		[HarmonyPatch("TeleportPlayerOutWithInverseTeleporter")]
		public static IEnumerable<CodeInstruction> InverseTeleporterDropAllButHeldItem(IEnumerable<CodeInstruction> instructions)
		{
			//IL_0003: Unknown result type (might be due to invalid IL or missing references)
			//IL_0009: Expected O, but got Unknown
			//IL_0040: Unknown result type (might be due to invalid IL or missing references)
			//IL_0046: Expected O, but got Unknown
			CodeMatcher val = new CodeMatcher(instructions, (ILGenerator)null);
			val.Start();
			val.MatchStartForward(inverseTeleporterPatchIlMatch);
			val.Advance(1);
			val.RemoveInstructionsWithOffsets(0, 2);
			val.Insert((CodeInstruction[])(object)new CodeInstruction[1]
			{
				new CodeInstruction(OpCodes.Callvirt, (object)dropAllButHeldItemMethodInfo)
			});
			KindTeleportersBase.Log.LogInfo((object)"Patched 'ShipTeleporterPatch.TeleportPlayerOutWithInverseTeleporter' :D");
			return val.Instructions();
		}

		[HarmonyTranspiler]
		[HarmonyPatch(/*Could not decode attribute arguments.*/)]
		public static IEnumerable<CodeInstruction> TeleporterDropAllButHeldItem(IEnumerable<CodeInstruction> instructions)
		{
			//IL_0003: Unknown result type (might be due to invalid IL or missing references)
			//IL_0009: Expected O, but got Unknown
			//IL_0040: Unknown result type (might be due to invalid IL or missing references)
			//IL_0046: Expected O, but got Unknown
			CodeMatcher val = new CodeMatcher(instructions, (ILGenerator)null);
			val.End();
			val.MatchStartBackwards(teleporterPatchIlMatch);
			val.Advance(2);
			val.RemoveInstructionsWithOffsets(0, 2);
			val.Insert((CodeInstruction[])(object)new CodeInstruction[1]
			{
				new CodeInstruction(OpCodes.Callvirt, (object)dropAllButHeldItemMethodInfo)
			});
			KindTeleportersBase.Log.LogInfo((object)"Patched 'ShipTeleporterPatch.beamUpPlayer' :D");
			return val.Instructions();
		}

		private static void DropAllButHeldItem(PlayerControllerB player)
		{
			//IL_00b6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00eb: Unknown result type (might be due to invalid IL or missing references)
			KindTeleportersBase.Log.LogInfo((object)"Dropping all but held items!");
			for (int i = 0; i < player.ItemSlots.Length; i++)
			{
				GrabbableObject val = player.ItemSlots[i];
				if (val != null && i != player.currentItemSlot)
				{
					val.parentObject = null;
					val.heldByPlayerOnServer = false;
					if (player.isInElevator)
					{
						((Component)val).transform.SetParent(player.playersManager.elevatorTransform, true);
					}
					else
					{
						((Component)val).transform.SetParent(player.playersManager.propsContainer, true);
					}
					player.SetItemInElevator(player.isInHangarShipRoom, player.isInElevator, val);
					val.EnablePhysics(true);
					val.EnableItemMeshes(true);
					((Component)val).transform.localScale = val.originalScale;
					val.isHeld = false;
					val.isPocketed = false;
					val.startFallingPosition = ((Component)val).transform.parent.InverseTransformPoint(((Component)val).transform.position);
					val.FallToGround(true);
					val.fallTime = Random.Range(-0.3f, 0.05f);
					if (((NetworkBehaviour)player).IsOwner)
					{
						val.DiscardItemOnClient();
					}
					else if (!val.itemProperties.syncDiscardFunction)
					{
						val.playerHeldBy = null;
					}
					if (((NetworkBehaviour)player).IsOwner)
					{
						((Behaviour)HUDManager.Instance.holdingTwoHandedItem).enabled = false;
						((Behaviour)HUDManager.Instance.itemSlotIcons[i]).enabled = false;
						HUDManager.Instance.ClearControlTips();
						player.activatingItem = false;
					}
					player.ItemSlots[i] = null;
				}
			}
			GrabbableObject val2 = player.ItemSlots[player.currentItemSlot];
			if (val2 == null)
			{
				player.twoHanded = false;
				player.carryWeight = 1f;
				player.currentlyHeldObjectServer = null;
			}
			else
			{
				KindTeleportersBase.Log.LogInfo((object)$"Carry weight before: {player.carryWeight}");
				player.twoHanded = val2.itemProperties.twoHanded;
				player.carryWeight = 1f + Mathf.Clamp(val2.itemProperties.weight - 1f, 0f, 10f);
				player.currentlyHeldObjectServer = val2;
				KindTeleportersBase.Log.LogInfo((object)$"Carry weight after: {player.carryWeight}");
			}
		}
	}
}