Decompiled source of SaferTeleporter v1.0.1

BepInEx/plugins/SaferTeleporter.dll

Decompiled 2 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Bootstrap;
using BepInEx.Configuration;
using BepInEx.Logging;
using GameNetcodeStuff;
using HarmonyLib;
using LethalConfig;
using LethalConfig.ConfigItems;
using Microsoft.CodeAnalysis;
using Unity.Netcode;
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("SaferTeleporter")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyDescription("Makes teleporter safer")]
[assembly: AssemblyFileVersion("0.0.1.0")]
[assembly: AssemblyInformationalVersion("0.0.1+e5cbd81f0600a57dc495868fb6d22c52fdca8366")]
[assembly: AssemblyProduct("SaferTeleporter")]
[assembly: AssemblyTitle("SaferTeleporter")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.0.1.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;
		}
	}
}
internal static class LethalConfigCompat
{
	[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
	internal static void Register(ConfigEntry<bool> boolConfigEntry)
	{
		//IL_0008: Unknown result type (might be due to invalid IL or missing references)
		//IL_0012: Expected O, but got Unknown
		LethalConfigManager.SkipAutoGenFor((ConfigEntryBase)(object)boolConfigEntry);
		LethalConfigManager.AddConfigItem((BaseConfigItem)new BoolCheckBoxConfigItem(boolConfigEntry, false));
	}

	[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
	internal static void Register(ConfigEntry<int> intConfigEntry)
	{
		//IL_0008: Unknown result type (might be due to invalid IL or missing references)
		//IL_0012: Expected O, but got Unknown
		LethalConfigManager.SkipAutoGenFor((ConfigEntryBase)(object)intConfigEntry);
		LethalConfigManager.AddConfigItem((BaseConfigItem)new IntInputFieldConfigItem(intConfigEntry, false));
	}

	[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
	internal static void Register(ConfigEntry<float> floatConfigEntry)
	{
		//IL_0008: Unknown result type (might be due to invalid IL or missing references)
		//IL_0012: Expected O, but got Unknown
		LethalConfigManager.SkipAutoGenFor((ConfigEntryBase)(object)floatConfigEntry);
		LethalConfigManager.AddConfigItem((BaseConfigItem)new FloatInputFieldConfigItem(floatConfigEntry, false));
	}

	[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
	internal static void Register(ConfigEntry<string> stringConfigEntry)
	{
		//IL_0008: Unknown result type (might be due to invalid IL or missing references)
		//IL_0012: Expected O, but got Unknown
		LethalConfigManager.SkipAutoGenFor((ConfigEntryBase)(object)stringConfigEntry);
		LethalConfigManager.AddConfigItem((BaseConfigItem)new TextInputFieldConfigItem(stringConfigEntry, false));
	}
}
namespace SaferTeleporter
{
	internal static class ConfigSettings
	{
		private static ConfigEntry<float> _teleporterSafetySeconds;

		internal static float TeleporterSafetySeconds = 2.5f;

		internal static void Init(BaseUnityPlugin plugin)
		{
			//IL_0020: Unknown result type (might be due to invalid IL or missing references)
			//IL_002a: Expected O, but got Unknown
			_teleporterSafetySeconds = plugin.Config.Bind<float>("General", "TeleporterSafetySeconds", 2.5f, new ConfigDescription("Players will be immune to damage from other players for this many seconds after teleporting.", (AcceptableValueBase)null, Array.Empty<object>()));
			_teleporterSafetySeconds.SettingChanged += OnTeleporterSafetySecondsSettingChanged;
			UpdateTeleporterSafetySeconds();
		}

		private static void OnTeleporterSafetySecondsSettingChanged(object sender, EventArgs e)
		{
			UpdateTeleporterSafetySeconds();
		}

		private static void UpdateTeleporterSafetySeconds()
		{
			TeleporterSafetySeconds = _teleporterSafetySeconds?.Value ?? 2.5f;
		}

		internal static void RegisterWithLethalConfigIfPresent()
		{
			if (Chainloader.PluginInfos.ContainsKey("ainavt.lc.lethalconfig"))
			{
				LethalConfigCompat.Register(_teleporterSafetySeconds);
			}
		}
	}
	internal class Debug
	{
		private static readonly bool debugEnabled;

		internal static void Log(string message)
		{
			if (debugEnabled)
			{
				Plugin.Log.LogInfo((object)("Debug: " + message));
			}
		}
	}
	[BepInPlugin("Azx.SaferTeleporter", "Safer Teleporter", "1.0.1")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class Plugin : BaseUnityPlugin
	{
		public const string PluginGuid = "Azx.SaferTeleporter";

		public const string PluginName = "Safer Teleporter";

		public const string PluginVersion = "1.0.1";

		internal static ManualLogSource Log;

		internal static Harmony Harmony;

		internal static Plugin Instance;

		private void Awake()
		{
			//IL_0016: Unknown result type (might be due to invalid IL or missing references)
			//IL_0020: Expected O, but got Unknown
			Instance = this;
			Log = ((BaseUnityPlugin)this).Logger;
			Harmony = new Harmony("Azx.SaferTeleporter");
			ConfigSettings.Init((BaseUnityPlugin)(object)this);
			ConfigSettings.RegisterWithLethalConfigIfPresent();
			Harmony.PatchAll();
			Log.LogInfo((object)"Safer Teleporter 1.0.1 loaded.");
		}

		internal static bool IsHost()
		{
			if (Object.op_Implicit((Object)(object)NetworkManager.Singleton))
			{
				if (!NetworkManager.Singleton.IsServer)
				{
					return NetworkManager.Singleton.IsHost;
				}
				return true;
			}
			return false;
		}
	}
	public static class PluginInfo
	{
		public const string PLUGIN_GUID = "SaferTeleporter";

		public const string PLUGIN_NAME = "SaferTeleporter";

		public const string PLUGIN_VERSION = "0.0.1";
	}
}
namespace SaferTeleporter.Patches
{
	internal class PlayerDamagePatch
	{
		[HarmonyPatch]
		internal class PostTeleportDamagePatch
		{
			[HarmonyPatch(typeof(PlayerControllerB), "DamagePlayerFromOtherClientServerRpc")]
			[HarmonyPrefix]
			public static void DamagePlayerFromOtherClientServerRpc(PlayerControllerB __instance, ref int damageAmount, Vector3 hitDirection, int playerWhoHit)
			{
				if (RecentlyTeleported(__instance))
				{
					damageAmount = 0;
				}
			}
		}

		[HarmonyPatch(typeof(PlayerControllerB))]
		internal static class PlayerControllerB_TeleportPlayer_Patch
		{
			[HarmonyPostfix]
			[HarmonyPatch("TeleportPlayer", new Type[]
			{
				typeof(Vector3),
				typeof(bool),
				typeof(float),
				typeof(bool),
				typeof(bool)
			})]
			private static void Postfix(PlayerControllerB __instance, Vector3 pos, bool withRotation, float rot, bool allowInteractTrigger, bool enableController)
			{
				//IL_000b: Unknown result type (might be due to invalid IL or missing references)
				Debug.Log("teleportin 1");
				try
				{
					TryMarkShipTeleport(__instance, pos);
				}
				catch (Exception arg)
				{
					Debug.Log($"TryMarkShipTeleport() failed: {arg}");
				}
				Debug.Log("teleportin 2");
			}
		}

		private static readonly Dictionary<ulong, float> _lastShipTeleportAtByClient = new Dictionary<ulong, float>();

		private static void TryMarkShipTeleport(PlayerControllerB pc, Vector3 position)
		{
			//IL_0042: Unknown result type (might be due to invalid IL or missing references)
			Debug.Log("TRY MARK: 1");
			if ((Object)(object)pc == (Object)null)
			{
				return;
			}
			Debug.Log("TRY MARK: 3");
			if (!pc.isPlayerDead && pc.isPlayerControlled && pc.shipTeleporterId == 1)
			{
				Debug.Log("TRY MARK: 4");
				if (IsShipTeleporterPosition(position))
				{
					Debug.Log("TRY MARK: 5");
					_lastShipTeleportAtByClient[pc.playerClientId] = Time.time;
				}
			}
		}

		private static bool IsShipTeleporterPosition(Vector3 pos)
		{
			//IL_0033: 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)
			//IL_0039: Unknown result type (might be due to invalid IL or missing references)
			//IL_003e: Unknown result type (might be due to invalid IL or missing references)
			ShipTeleporter[] array = Object.FindObjectsOfType<ShipTeleporter>();
			foreach (ShipTeleporter val in array)
			{
				if (!((Object)(object)val == (Object)null) && !val.isInverseTeleporter && !((Object)(object)val.teleporterPosition == (Object)null))
				{
					Vector3 val2 = val.teleporterPosition.position - pos;
					if (((Vector3)(ref val2)).sqrMagnitude <= 0.01f)
					{
						return true;
					}
				}
			}
			return false;
		}

		private static bool RecentlyTeleported(PlayerControllerB pc)
		{
			float teleporterSafetySeconds = ConfigSettings.TeleporterSafetySeconds;
			if (teleporterSafetySeconds <= 0f || (Object)(object)pc == (Object)null)
			{
				return false;
			}
			ulong playerClientId = pc.playerClientId;
			float value;
			float num = (_lastShipTeleportAtByClient.TryGetValue(playerClientId, out value) ? value : (-99999f));
			return Time.time - num <= teleporterSafetySeconds;
		}
	}
}