Decompiled source of TruckRespawn v1.0.0

RespawnInTruck.dll

Decompiled 3 days ago
using System;
using System.Collections;
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.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Photon.Pun;
using UnityEngine;

[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: IgnoresAccessChecksTo("")]
[assembly: AssemblyCompany("Omniscye")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("RespawnInTruck")]
[assembly: AssemblyTitle("RespawnInTruck")]
[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.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;
		}
	}
	[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 RespawnInTruck
{
	[BepInPlugin("Omniscye.RespawnInTruck", "RespawnInTruck", "1.0")]
	public class RespawnInTruck : BaseUnityPlugin
	{
		internal static RespawnInTruck Instance { get; private set; }

		internal static ManualLogSource Logger => Instance._logger;

		private ManualLogSource _logger => ((BaseUnityPlugin)this).Logger;

		internal Harmony? Harmony { get; set; }

		private void Awake()
		{
			Instance = this;
			((Component)this).gameObject.transform.parent = null;
			((Object)((Component)this).gameObject).hideFlags = (HideFlags)61;
			Patch();
			Logger.LogInfo((object)$"{((BaseUnityPlugin)this).Info.Metadata.GUID} v{((BaseUnityPlugin)this).Info.Metadata.Version} has loaded!");
		}

		internal void Patch()
		{
			//IL_001a: Unknown result type (might be due to invalid IL or missing references)
			//IL_001f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0021: Expected O, but got Unknown
			//IL_0026: Expected O, but got Unknown
			if (Harmony == null)
			{
				Harmony val = new Harmony(((BaseUnityPlugin)this).Info.Metadata.GUID);
				Harmony val2 = val;
				Harmony = val;
			}
			Harmony.PatchAll();
		}

		internal void Unpatch()
		{
			Harmony? harmony = Harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
		}

		private void Update()
		{
		}
	}
}
namespace Empress.Repo.RespawnInTruck
{
	[BepInPlugin("com.empress.repo.truckrespawn", "RespawnInTruck", "2.0.0")]
	public class RespawnInTruckPlugin : BaseUnityPlugin
	{
		[CompilerGenerated]
		private sealed class <ReviveAfterDelay>d__12 : IEnumerator<object>, IEnumerator, IDisposable
		{
			private int <>1__state;

			private object <>2__current;

			public PlayerAvatar avatar;

			public int actorNr;

			public RespawnInTruckPlugin <>4__this;

			private float <wait>5__1;

			private float <t>5__2;

			private Exception <ex>5__3;

			object IEnumerator<object>.Current
			{
				[DebuggerHidden]
				get
				{
					return <>2__current;
				}
			}

			object IEnumerator.Current
			{
				[DebuggerHidden]
				get
				{
					return <>2__current;
				}
			}

			[DebuggerHidden]
			public <ReviveAfterDelay>d__12(int <>1__state)
			{
				this.<>1__state = <>1__state;
			}

			[DebuggerHidden]
			void IDisposable.Dispose()
			{
				<ex>5__3 = null;
				<>1__state = -2;
			}

			private bool MoveNext()
			{
				//IL_006f: Unknown result type (might be due to invalid IL or missing references)
				//IL_0079: Expected O, but got Unknown
				//IL_015d: Unknown result type (might be due to invalid IL or missing references)
				//IL_0167: Expected O, but got Unknown
				switch (<>1__state)
				{
				default:
					return false;
				case 0:
					<>1__state = -1;
					<wait>5__1 = Mathf.Max(0f, <>4__this._respawnDelay.Value);
					if (<wait>5__1 > 0f)
					{
						<>2__current = (object)new WaitForSecondsRealtime(<wait>5__1);
						<>1__state = 1;
						return true;
					}
					goto IL_0089;
				case 1:
					<>1__state = -1;
					goto IL_0089;
				case 2:
					<>1__state = -1;
					goto IL_00c1;
				case 3:
					{
						<>1__state = -1;
						break;
					}
					IL_0089:
					<t>5__2 = 0f;
					goto IL_00c1;
					IL_00c1:
					if ((Object)(object)avatar != (Object)null && (Object)(object)avatar.playerDeathHead == (Object)null && <t>5__2 < 2f)
					{
						<t>5__2 += Time.unscaledDeltaTime;
						<>2__current = null;
						<>1__state = 2;
						return true;
					}
					if ((Object)(object)avatar == (Object)null)
					{
						Scheduled.Remove(actorNr);
						return false;
					}
					if ((Object)(object)avatar.playerDeathHead == (Object)null)
					{
						Log.LogWarning((object)string.Format("[{0}] Death head missing for actor {1}; postponing revive by 0.25s", "RespawnInTruck", actorNr));
						<>2__current = (object)new WaitForSecondsRealtime(0.25f);
						<>1__state = 3;
						return true;
					}
					break;
				}
				try
				{
					avatar.Revive(true);
					Log.LogDebug((object)string.Format("[{0}] Revive(true) sent for actor {1} after {2:F2}s", "RespawnInTruck", actorNr, <wait>5__1));
				}
				catch (Exception ex)
				{
					<ex>5__3 = ex;
					Log.LogError((object)string.Format("[{0}] Revive call failed for actor {1}: {2}", "RespawnInTruck", actorNr, <ex>5__3));
				}
				finally
				{
					Scheduled.Remove(actorNr);
				}
				return false;
			}

			bool IEnumerator.MoveNext()
			{
				//ILSpy generated this explicit interface implementation from .override directive in MoveNext
				return this.MoveNext();
			}

			[DebuggerHidden]
			void IEnumerator.Reset()
			{
				throw new NotSupportedException();
			}
		}

		public const string GUID = "com.empress.repo.truckrespawn";

		public const string Name = "RespawnInTruck";

		public const string Version = "2.0.0";

		internal static RespawnInTruckPlugin Instance;

		internal static Harmony Harmony;

		internal static ManualLogSource Log;

		private ConfigEntry<bool> _enabled;

		private ConfigEntry<float> _respawnDelay;

		internal static readonly HashSet<int> Scheduled = new HashSet<int>();

		private void Awake()
		{
			//IL_005d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0067: Expected O, but got Unknown
			Instance = this;
			Log = ((BaseUnityPlugin)this).Logger;
			_enabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "If true, dead players are revived in the truck (master-controlled).");
			_respawnDelay = ((BaseUnityPlugin)this).Config.Bind<float>("General", "RespawnDelay", 10f, "Real-time delay after death before reviving (seconds).");
			Harmony = new Harmony("com.empress.repo.truckrespawn");
			Harmony.PatchAll();
			Log.LogInfo((object)string.Format("[{0}] v{1} loaded. Enabled={2}, Delay={3:F2}s", "RespawnInTruck", "2.0.0", _enabled.Value, _respawnDelay.Value));
		}

		private void OnDestroy()
		{
			Harmony harmony = Harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
			Scheduled.Clear();
		}

		internal bool FeatureEnabledForThisClient()
		{
			return _enabled.Value;
		}

		[IteratorStateMachine(typeof(<ReviveAfterDelay>d__12))]
		internal IEnumerator ReviveAfterDelay(PlayerAvatar avatar, int actorNr)
		{
			//yield-return decompiler failed: Unexpected instruction in Iterator.Dispose()
			return new <ReviveAfterDelay>d__12(0)
			{
				<>4__this = this,
				avatar = avatar,
				actorNr = actorNr
			};
		}

		internal static bool TryGetTruckSpawn(out Vector3 pos, out Quaternion rot)
		{
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			//IL_000d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0012: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e5: 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_008b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0053: 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_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_0110: Unknown result type (might be due to invalid IL or missing references)
			//IL_0115: Unknown result type (might be due to invalid IL or missing references)
			//IL_011d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0122: Unknown result type (might be due to invalid IL or missing references)
			pos = Vector3.zero;
			rot = Quaternion.identity;
			if ((Object)(object)TruckSafetySpawnPoint.instance != (Object)null)
			{
				Transform transform = ((Component)TruckSafetySpawnPoint.instance).transform;
				SpawnPoint[] componentsInChildren = ((Component)transform).GetComponentsInChildren<SpawnPoint>(false);
				if (componentsInChildren != null && componentsInChildren.Length != 0)
				{
					pos = ((Component)componentsInChildren[0]).transform.position;
					rot = ((Component)componentsInChildren[0]).transform.rotation;
					return true;
				}
				pos = transform.position;
				rot = transform.rotation;
				return true;
			}
			if ((Object)(object)LevelGenerator.Instance != (Object)null && (Object)(object)LevelGenerator.Instance.LevelPathTruck != (Object)null)
			{
				Transform transform2 = ((Component)LevelGenerator.Instance.LevelPathTruck).transform;
				pos = transform2.position;
				rot = transform2.rotation;
				return true;
			}
			if ((Object)(object)TruckHealer.instance != (Object)null)
			{
				Transform transform3 = ((Component)TruckHealer.instance).transform;
				pos = transform3.position;
				rot = transform3.rotation;
				return true;
			}
			return false;
		}

		internal static bool GetTruckSpawnFor(PlayerAvatar avatar, out Vector3 pos, out Quaternion rot)
		{
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			//IL_000d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0012: Unknown result type (might be due to invalid IL or missing references)
			//IL_012d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0132: Unknown result type (might be due to invalid IL or missing references)
			//IL_013a: 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)
			pos = Vector3.zero;
			rot = Quaternion.identity;
			if ((Object)(object)TruckSafetySpawnPoint.instance != (Object)null)
			{
				Transform transform = ((Component)TruckSafetySpawnPoint.instance).transform;
				List<Transform> list = new List<Transform>();
				try
				{
					SpawnPoint[] componentsInChildren = ((Component)transform).GetComponentsInChildren<SpawnPoint>(false);
					if (componentsInChildren != null && componentsInChildren.Length != 0)
					{
						SpawnPoint[] array = componentsInChildren;
						foreach (SpawnPoint val in array)
						{
							if (Object.op_Implicit((Object)(object)val))
							{
								list.Add(((Component)val).transform);
							}
						}
					}
				}
				catch
				{
				}
				if (list.Count == 0)
				{
					list.Add(transform);
				}
				int num = 0;
				try
				{
					num = (((Object)(object)avatar?.photonView != (Object)null) ? avatar.photonView.OwnerActorNr : ((Object)avatar).GetInstanceID());
				}
				catch
				{
					num = (((Object)(object)avatar != (Object)null) ? ((Object)avatar).GetInstanceID() : 0);
				}
				int index = ((list.Count > 0) ? (Mathf.Abs(num) % list.Count) : 0);
				Transform val2 = list[index];
				if (Object.op_Implicit((Object)(object)val2))
				{
					pos = val2.position;
					rot = val2.rotation;
					return true;
				}
			}
			return TryGetTruckSpawn(out pos, out rot);
		}

		internal static Vector3 SpreadOffsetFor(PlayerAvatar avatar)
		{
			//IL_006d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0072: 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)
			int num = 0;
			try
			{
				num = (((Object)(object)avatar?.photonView != (Object)null) ? avatar.photonView.OwnerActorNr : ((Object)avatar).GetInstanceID());
			}
			catch
			{
				num = (((Object)(object)avatar != (Object)null) ? ((Object)avatar).GetInstanceID() : 0);
			}
			float num2 = 0.6f;
			float num3 = (float)(num % 8) * (MathF.PI / 4f);
			return new Vector3(Mathf.Cos(num3) * num2, 0f, Mathf.Sin(num3) * num2);
		}
	}
	[HarmonyPatch(typeof(PlayerAvatar))]
	internal static class PlayerAvatar_TruckRespawn_Patches
	{
		[CompilerGenerated]
		private sealed class <OwnerMoveAfterRevive>d__2 : IEnumerator<object>, IEnumerator, IDisposable
		{
			private int <>1__state;

			private object <>2__current;

			public PlayerAvatar avatar;

			private Vector3 <basePos>5__1;

			private Quaternion <baseRot>5__2;

			private Vector3 <finalPos>5__3;

			private Quaternion <finalRot>5__4;

			private Vector3 <probe>5__5;

			private RaycastHit <hit>5__6;

			private Exception <ex>5__7;

			object IEnumerator<object>.Current
			{
				[DebuggerHidden]
				get
				{
					return <>2__current;
				}
			}

			object IEnumerator.Current
			{
				[DebuggerHidden]
				get
				{
					return <>2__current;
				}
			}

			[DebuggerHidden]
			public <OwnerMoveAfterRevive>d__2(int <>1__state)
			{
				this.<>1__state = <>1__state;
			}

			[DebuggerHidden]
			void IDisposable.Dispose()
			{
				<ex>5__7 = null;
				<>1__state = -2;
			}

			private bool MoveNext()
			{
				//IL_0048: Unknown result type (might be due to invalid IL or missing references)
				//IL_0052: Expected O, but got Unknown
				//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_00d5: Unknown result type (might be due to invalid IL or missing references)
				//IL_00da: Unknown result type (might be due to invalid IL or missing references)
				//IL_00df: Unknown result type (might be due to invalid IL or missing references)
				//IL_00e5: Unknown result type (might be due to invalid IL or missing references)
				//IL_00ea: Unknown result type (might be due to invalid IL or missing references)
				//IL_009d: Unknown result type (might be due to invalid IL or missing references)
				//IL_00a8: Unknown result type (might be due to invalid IL or missing references)
				//IL_00ad: Unknown result type (might be due to invalid IL or missing references)
				//IL_00b2: Unknown result type (might be due to invalid IL or missing references)
				//IL_00b9: 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_012c: Unknown result type (might be due to invalid IL or missing references)
				//IL_0131: Unknown result type (might be due to invalid IL or missing references)
				//IL_013b: Unknown result type (might be due to invalid IL or missing references)
				//IL_0140: Unknown result type (might be due to invalid IL or missing references)
				//IL_0145: Unknown result type (might be due to invalid IL or missing references)
				//IL_010a: Unknown result type (might be due to invalid IL or missing references)
				//IL_010f: Unknown result type (might be due to invalid IL or missing references)
				//IL_0119: Unknown result type (might be due to invalid IL or missing references)
				//IL_011e: Unknown result type (might be due to invalid IL or missing references)
				//IL_0123: 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_015e: Unknown result type (might be due to invalid IL or missing references)
				//IL_018c: Unknown result type (might be due to invalid IL or missing references)
				//IL_01bf: Unknown result type (might be due to invalid IL or missing references)
				switch (<>1__state)
				{
				default:
					return false;
				case 0:
					<>1__state = -1;
					<>2__current = null;
					<>1__state = 1;
					return true;
				case 1:
					<>1__state = -1;
					<>2__current = (object)new WaitForSeconds(0.05f);
					<>1__state = 2;
					return true;
				case 2:
					<>1__state = -1;
					if (!RespawnInTruckPlugin.TryGetTruckSpawn(out <basePos>5__1, out <baseRot>5__2))
					{
						return false;
					}
					if (!RespawnInTruckPlugin.GetTruckSpawnFor(avatar, out <finalPos>5__3, out <finalRot>5__4))
					{
						<finalPos>5__3 = <basePos>5__1 + RespawnInTruckPlugin.SpreadOffsetFor(avatar);
						<finalRot>5__4 = <baseRot>5__2;
					}
					<probe>5__5 = <finalPos>5__3 + Vector3.up * 2f;
					if (Physics.Raycast(<probe>5__5, Vector3.down, ref <hit>5__6, 5f))
					{
						<finalPos>5__3 = ((RaycastHit)(ref <hit>5__6)).point + Vector3.up * 0.05f;
					}
					else
					{
						<finalPos>5__3 += Vector3.up * 0.1f;
					}
					try
					{
						((Component)avatar).transform.SetPositionAndRotation(<finalPos>5__3, <finalRot>5__4);
						if ((Object)(object)avatar.playerTransform != (Object)null)
						{
							avatar.playerTransform.position = <finalPos>5__3;
						}
						if ((Object)(object)avatar.playerAvatarVisuals != (Object)null)
						{
							((Component)avatar.playerAvatarVisuals).transform.position = <finalPos>5__3;
						}
						try
						{
							avatar.FallDamageResetSet(2f);
						}
						catch
						{
						}
					}
					catch (Exception ex)
					{
						<ex>5__7 = ex;
						RespawnInTruckPlugin.Log.LogError((object)string.Format("[{0}] Owner teleport failed: {1}", "RespawnInTruck", <ex>5__7));
					}
					return false;
				}
			}

			bool IEnumerator.MoveNext()
			{
				//ILSpy generated this explicit interface implementation from .override directive in MoveNext
				return this.MoveNext();
			}

			[DebuggerHidden]
			void IEnumerator.Reset()
			{
				throw new NotSupportedException();
			}
		}

		[HarmonyPostfix]
		[HarmonyPatch("PlayerDeathRPC")]
		private static void Post_PlayerDeathRPC(PlayerAvatar __instance, int enemyIndex, PhotonMessageInfo _info)
		{
			RespawnInTruckPlugin instance = RespawnInTruckPlugin.Instance;
			if (!((Object)(object)instance == (Object)null) && instance.FeatureEnabledForThisClient() && PhotonNetwork.IsMasterClient && !((Object)(object)__instance == (Object)null) && !((Object)(object)__instance.photonView == (Object)null))
			{
				int ownerActorNr = __instance.photonView.OwnerActorNr;
				if (ownerActorNr > 0 && RespawnInTruckPlugin.Scheduled.Add(ownerActorNr))
				{
					((MonoBehaviour)instance).StartCoroutine(instance.ReviveAfterDelay(__instance, ownerActorNr));
				}
			}
		}

		[HarmonyPostfix]
		[HarmonyPatch("ReviveRPC")]
		private static void Post_ReviveRPC(PlayerAvatar __instance, bool _revivedByTruck, PhotonMessageInfo _info)
		{
			RespawnInTruckPlugin instance = RespawnInTruckPlugin.Instance;
			if ((Object)(object)instance == (Object)null || !instance.FeatureEnabledForThisClient() || !_revivedByTruck)
			{
				return;
			}
			try
			{
				if ((Object)(object)__instance.photonView != (Object)null && PhotonNetwork.InRoom && !__instance.photonView.IsMine)
				{
					return;
				}
			}
			catch
			{
			}
			((MonoBehaviour)instance).StartCoroutine(OwnerMoveAfterRevive(__instance));
		}

		[IteratorStateMachine(typeof(<OwnerMoveAfterRevive>d__2))]
		private static IEnumerator OwnerMoveAfterRevive(PlayerAvatar avatar)
		{
			//yield-return decompiler failed: Unexpected instruction in Iterator.Dispose()
			return new <OwnerMoveAfterRevive>d__2(0)
			{
				avatar = avatar
			};
		}
	}
}