Decompiled source of ReviveHeadInTruckOrExtractionPointToxinFork v1.4.0

ReviveHeadInTruckOrExtractionPoint.dll

Decompiled 3 weeks 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.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using RITE.Models;
using RITE.Utils;
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-CSharp-firstpass")]
[assembly: IgnoresAccessChecksTo("Assembly-CSharp")]
[assembly: IgnoresAccessChecksTo("Autodesk.Fbx")]
[assembly: IgnoresAccessChecksTo("Discord.Sdk")]
[assembly: IgnoresAccessChecksTo("Domain_Reload")]
[assembly: IgnoresAccessChecksTo("Facepunch.Steamworks.Win64")]
[assembly: IgnoresAccessChecksTo("FbxBuildTestAssets")]
[assembly: IgnoresAccessChecksTo("Klattersynth")]
[assembly: IgnoresAccessChecksTo("Photon3Unity3D")]
[assembly: IgnoresAccessChecksTo("PhotonChat")]
[assembly: IgnoresAccessChecksTo("PhotonRealtime")]
[assembly: IgnoresAccessChecksTo("PhotonUnityNetworking")]
[assembly: IgnoresAccessChecksTo("PhotonUnityNetworking.Utilities")]
[assembly: IgnoresAccessChecksTo("PhotonVoice.API")]
[assembly: IgnoresAccessChecksTo("PhotonVoice")]
[assembly: IgnoresAccessChecksTo("PhotonVoice.PUN")]
[assembly: IgnoresAccessChecksTo("SingularityGroup.HotReload.Runtime.Public")]
[assembly: IgnoresAccessChecksTo("Sirenix.OdinInspector.Attributes")]
[assembly: IgnoresAccessChecksTo("Sirenix.OdinInspector.Modules.Unity.Addressables")]
[assembly: IgnoresAccessChecksTo("Sirenix.OdinInspector.Modules.UnityLocalization")]
[assembly: IgnoresAccessChecksTo("Sirenix.Serialization.Config")]
[assembly: IgnoresAccessChecksTo("Sirenix.Serialization")]
[assembly: IgnoresAccessChecksTo("Sirenix.Utilities")]
[assembly: IgnoresAccessChecksTo("Unity.Addressables")]
[assembly: IgnoresAccessChecksTo("Unity.AI.Navigation")]
[assembly: IgnoresAccessChecksTo("Unity.Burst")]
[assembly: IgnoresAccessChecksTo("Unity.Burst.Unsafe")]
[assembly: IgnoresAccessChecksTo("Unity.Collections")]
[assembly: IgnoresAccessChecksTo("Unity.Collections.LowLevel.ILSupport")]
[assembly: IgnoresAccessChecksTo("Unity.Formats.Fbx.Runtime")]
[assembly: IgnoresAccessChecksTo("Unity.InputSystem")]
[assembly: IgnoresAccessChecksTo("Unity.InputSystem.ForUI")]
[assembly: IgnoresAccessChecksTo("Unity.InternalAPIEngineBridge.013")]
[assembly: IgnoresAccessChecksTo("Unity.Localization")]
[assembly: IgnoresAccessChecksTo("Unity.Mathematics")]
[assembly: IgnoresAccessChecksTo("Unity.MemoryProfiler")]
[assembly: IgnoresAccessChecksTo("Unity.Postprocessing.Runtime")]
[assembly: IgnoresAccessChecksTo("Unity.Profiling.Core")]
[assembly: IgnoresAccessChecksTo("Unity.RenderPipelines.Core.Runtime")]
[assembly: IgnoresAccessChecksTo("Unity.RenderPipelines.Core.ShaderLibrary")]
[assembly: IgnoresAccessChecksTo("Unity.RenderPipelines.ShaderGraph.ShaderGraphLibrary")]
[assembly: IgnoresAccessChecksTo("Unity.ResourceManager")]
[assembly: IgnoresAccessChecksTo("Unity.ScriptableBuildPipeline")]
[assembly: IgnoresAccessChecksTo("Unity.Splines")]
[assembly: IgnoresAccessChecksTo("Unity.TextMeshPro")]
[assembly: IgnoresAccessChecksTo("Unity.Timeline")]
[assembly: IgnoresAccessChecksTo("Unity.VisualScripting.Antlr3.Runtime")]
[assembly: IgnoresAccessChecksTo("Unity.VisualScripting.Core")]
[assembly: IgnoresAccessChecksTo("Unity.VisualScripting.Flow")]
[assembly: IgnoresAccessChecksTo("Unity.VisualScripting.State")]
[assembly: IgnoresAccessChecksTo("UnityEngine.ARModule")]
[assembly: IgnoresAccessChecksTo("UnityEngine.NVIDIAModule")]
[assembly: IgnoresAccessChecksTo("UnityEngine.UI")]
[assembly: IgnoresAccessChecksTo("websocket-sharp")]
[assembly: AssemblyCompany("Hypn,Toxin")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.4.0.0")]
[assembly: AssemblyInformationalVersion("1.4.0+80e7c8efbdee89e6184850f54c2c1f44f1131f18")]
[assembly: AssemblyProduct("ReviveHeadInTruckOrExtractionPoint")]
[assembly: AssemblyTitle("ReviveHeadInTruckOrExtractionPoint")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.4.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[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]
	[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]
	[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 RITE
{
	public static class Config
	{
		public static ConfigEntry<int> ReviveCooldownSeconds { get; private set; }

		public static ConfigEntry<bool> InstaRevive { get; private set; }

		public static ConfigEntry<float> ReviveCostMultiplier { get; private set; }

		public static ConfigEntry<float> HealMultiplier { get; private set; }

		public static void Prepare(ConfigFile bConfig)
		{
			//IL_0026: Unknown result type (might be due to invalid IL or missing references)
			//IL_0030: Expected O, but got Unknown
			//IL_005b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0065: Expected O, but got Unknown
			//IL_0094: Unknown result type (might be due to invalid IL or missing references)
			//IL_009e: Expected O, but got Unknown
			//IL_00cd: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d7: Expected O, but got Unknown
			ReviveCooldownSeconds = bConfig.Bind<int>("Revive", "ReviveCooldownSeconds", 300, new ConfigDescription("How long should one wait after the last revive", (AcceptableValueBase)(object)new AcceptableValueRange<int>(1, 600), Array.Empty<object>()));
			InstaRevive = bConfig.Bind<bool>("Revive", "InstaRevive", false, new ConfigDescription("If true, players will be revived instantly without speak", (AcceptableValueBase)(object)new AcceptableValueList<bool>(new bool[2] { true, false }), Array.Empty<object>()));
			ReviveCostMultiplier = bConfig.Bind<float>("Revive", "ReviveCostMultiplier", 1f, new ConfigDescription("Multiplier for the revive cost, default is 1.0", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 10f), Array.Empty<object>()));
			HealMultiplier = bConfig.Bind<float>("Revive", "HealMultiplier", 0.5f, new ConfigDescription("Multiplier for the heal amount on revive, default is 0.5", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0.1f, 1f), Array.Empty<object>()));
		}
	}
	[BepInPlugin("HT.ReviveHeadInTruckOrExtractionPoint", "ReviveHeadInTruckOrExtractionPoint", "1.4.0")]
	public class Plugin : BaseUnityPlugin
	{
		public const string ModGUID = "HT.ReviveHeadInTruckOrExtractionPoint";

		public const string ModName = "ReviveHeadInTruckOrExtractionPoint";

		public const string ModAuthors = "Hypn, Toxin";

		public const string ModVersion = "1.4.0";

		private readonly Harmony harmony = new Harmony("HT.ReviveHeadInTruckOrExtractionPoint");

		internal static Plugin Instance { get; private set; }

		public static ManualLogSource Logger { get; private set; }

		private void Awake()
		{
			Config.Prepare(((BaseUnityPlugin)this).Config);
			Instance = this;
			Logger = ((BaseUnityPlugin)this).Logger;
			Logger.LogInfo((object)"Plugin ReviveHeadInTruckOrExtractionPoint v1.4.0 is loaded! Authors: Hypn, Toxin");
			harmony.PatchAll();
		}

		internal void Unpatch()
		{
			harmony.UnpatchSelf();
		}
	}
}
namespace RITE.Utils
{
	internal static class ReviveHelper
	{
		public static int CalculateReviveCost(int lvl, int count, float customMultiplier)
		{
			if (count == 0)
			{
				return 0;
			}
			int num = Math.Min(lvl, 20);
			float num2 = (float)num * 2.5f;
			float num3 = 1f + (float)(count - 1) * 0.2f;
			float num4 = num2 * num3 * customMultiplier;
			return Math.Max(0, (int)num4);
		}

		public static void LogInfo(string message)
		{
			Plugin.Logger.LogInfo((object)$"[{DateTime.Now:HH:mm:ss.fff}] {message}");
		}
	}
}
namespace RITE.Models
{
	internal class ReviveInfo
	{
		public readonly Queue<string> Messages = new Queue<string>();

		private const float MessageCooldownSeconds = 30f;

		public PlayerAvatar Player { get; }

		public int Count { get; private set; } = 0;

		public float LastReviveTime { get; private set; } = Time.realtimeSinceStartup;

		public float OutsideLevelTimer { get; private set; } = 0f;

		public bool IsPreparingToRevive { get; private set; } = false;

		public float LastMessageTime { get; private set; } = Time.realtimeSinceStartup;

		public bool IsWaitingForSpeech { get; private set; } = false;

		public float UpdateThrottle { get; set; } = 0f;

		public ReviveInfo(PlayerAvatar player)
		{
			Player = player ?? throw new ArgumentNullException("player");
		}

		public ReviveInfo SetCount(int c)
		{
			Count = c;
			return this;
		}

		public ReviveInfo IncCount()
		{
			Count++;
			return this;
		}

		public ReviveInfo SetLastReviveTime(float t)
		{
			LastReviveTime = t;
			return this;
		}

		public ReviveInfo SetOutsideLevelTimer(float t)
		{
			OutsideLevelTimer = t;
			return this;
		}

		public ReviveInfo SetReviving(bool r)
		{
			IsPreparingToRevive = r;
			return this;
		}

		public ReviveInfo AddMessage(string message)
		{
			Messages.Enqueue(message);
			return this;
		}

		public string GetMessage()
		{
			return Messages.Dequeue();
		}

		public ReviveInfo ClearMessages()
		{
			Messages.Clear();
			return this;
		}

		public ReviveInfo DropReviving()
		{
			return ClearMessages().SetReviving(r: false);
		}

		public bool AnyMessages()
		{
			return Messages.Count > 0;
		}

		public ReviveInfo SetWaitingForSpeech(bool value)
		{
			IsWaitingForSpeech = value;
			return this;
		}

		public ReviveInfo TryAddMessageWithCooldown(string message)
		{
			float realtimeSinceStartup = Time.realtimeSinceStartup;
			if (realtimeSinceStartup - LastMessageTime >= 30f)
			{
				AddMessage(message);
				LastMessageTime = realtimeSinceStartup;
			}
			return this;
		}

		public bool CanRevive(int currentLevel, int currentCurrency, uint reviveCooldownSeconds, float customMultiplier)
		{
			PlayerDeathHead playerDeathHead = Player.playerDeathHead;
			if ((Object)(object)playerDeathHead.physGrabObject?.impactDetector?.currentCart != (Object)null)
			{
				ReviveHelper.LogInfo("Player \"" + Player.playerName + "\" inCart");
				DropReviving();
				return false;
			}
			bool flag = playerDeathHead.roomVolumeCheck?.inTruck ?? false;
			bool flag2 = playerDeathHead.roomVolumeCheck?.inExtractionPoint ?? false;
			if (!flag && !flag2)
			{
				ReviveHelper.LogInfo("Player \"" + Player.playerName + "\" is not in the truck or at the extraction point");
				DropReviving();
				return false;
			}
			if (!CooldownPassed(reviveCooldownSeconds))
			{
				return false;
			}
			if (!IsCostPayable(currentLevel, currentCurrency, customMultiplier))
			{
				return false;
			}
			return true;
		}

		private bool IsCostPayable(int currentLevel, int currentCurrency, float customMultiplier)
		{
			int num = ReviveHelper.CalculateReviveCost(currentLevel, Count, customMultiplier);
			if (currentCurrency >= num)
			{
				return true;
			}
			DropReviving();
			TryAddMessageWithCooldown($"I can't be revived yet — not enough money. Cost: ${num}k, available: ${currentCurrency}");
			ReviveHelper.LogInfo($"Player \"{Player.playerName}\" cannot be revived, not enough money. Revive cost: ${num}k. Current currency: ${currentCurrency}k");
			return false;
		}

		private bool CooldownPassed(uint reviveCooldownSeconds)
		{
			if (Count == 0)
			{
				return true;
			}
			float num = Time.realtimeSinceStartup - LastReviveTime;
			if (num >= (float)reviveCooldownSeconds)
			{
				return true;
			}
			float num2 = (float)reviveCooldownSeconds - num;
			DropReviving();
			TryAddMessageWithCooldown($"I can't be revived yet, {Mathf.CeilToInt(num2)} seconds left");
			ReviveHelper.LogInfo($"Player \"{Player.playerName}\" cannot be revived, cooldown: {Mathf.CeilToInt(num2)} seconds left");
			return false;
		}
	}
}
namespace RITE.Patches
{
	[HarmonyPatch(typeof(PlayerDeathHead), "Update")]
	internal class PlayerDeathHeadPath
	{
		private static readonly Dictionary<string, ReviveInfo> reviveInfos = new Dictionary<string, ReviveInfo>();

		private static readonly object _reviveLock = new object();

		private static int CurrentLevel = 0;

		private static readonly string[] Countdown = new string[5] { "Five", "Four", "Three", "Two", "One" };

		private static uint ReviveCooldownSeconds => (uint)Config.ReviveCooldownSeconds.Value;

		private static bool InstaRevive => Config.InstaRevive.Value;

		private static float ReviveCostMultiplier => Config.ReviveCostMultiplier.Value;

		private static float HealMultiplier => Config.HealMultiplier.Value;

		private static int CurrentCurrency => SemiFunc.StatGetRunCurrency();

		private static void TeleportPlayer(ReviveInfo info)
		{
			//IL_018f: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a3: Unknown result type (might be due to invalid IL or missing references)
			//IL_0143: Unknown result type (might be due to invalid IL or missing references)
			//IL_0152: Unknown result type (might be due to invalid IL or missing references)
			PlayerAvatar player = info.Player;
			PlayerDeathHead playerDeathHead = player.playerDeathHead;
			int num = playerDeathHead.roomVolumeCheck?.CurrentRooms?.Count ?? 1;
			if (num > 0)
			{
				info.SetOutsideLevelTimer(0f);
				return;
			}
			info.SetOutsideLevelTimer(info.OutsideLevelTimer + Time.deltaTime);
			if ((Object)(object)playerDeathHead.physGrabObject == (Object)null)
			{
				ReviveHelper.LogInfo("Player \"" + player.playerName + "\" could not be teleported — no physGrabObject.");
			}
			else if (!(info.OutsideLevelTimer < 5f))
			{
				if (player.playerDeathHead?.roomVolumeCheck?.inTruck == true)
				{
					ReviveHelper.LogInfo("Teleporting player \"" + player.playerName + "\" to truck");
					playerDeathHead.physGrabObject.Teleport(((Component)TruckSafetySpawnPoint.instance).transform.position, ((Component)TruckSafetySpawnPoint.instance).transform.rotation);
				}
				else
				{
					ReviveHelper.LogInfo("Teleporting player \"" + player.playerName + "\" to extraction point");
					playerDeathHead.physGrabObject.Teleport(RoundDirector.instance.extractionPointCurrent.safetySpawn.position, RoundDirector.instance.extractionPointCurrent.safetySpawn.rotation);
				}
			}
		}

		private static void Revive(ReviveInfo info)
		{
			if (!info.AnyMessages() && !PlayerIsTalking(info))
			{
				int num = ReviveHelper.CalculateReviveCost(CurrentLevel, info.Count, ReviveCostMultiplier);
				SemiFunc.StatSetRunCurrency(Math.Max(CurrentCurrency - num, 0));
				int num2 = (int)((float)info.Player.playerHealth.maxHealth * HealMultiplier);
				info.IncCount().SetLastReviveTime(Time.realtimeSinceStartup).SetReviving(r: false);
				ReviveHelper.LogInfo($"Reviving player \"{info.Player.playerName}\" with {num2}/{info.Player.playerHealth.maxHealth} health (revives: {info.Count}). Revive cost: {num}. Currency left: {CurrentCurrency}.");
				info.Player.Revive(info.Player.playerDeathHead?.roomVolumeCheck?.inTruck == true);
				info.Player.playerHealth.HealOther(num2 - 1, true);
				TeleportPlayer(info);
			}
		}

		private static void Speak(ReviveInfo info)
		{
			if (info.AnyMessages() && !PlayerIsTalking(info))
			{
				string message = info.GetMessage();
				ReviveHelper.LogInfo("Player \"" + info.Player.playerName + "\" talk " + message);
				info.Player.ChatMessageSend(message);
			}
		}

		private static bool PlayerIsTalking(ReviveInfo info)
		{
			bool valueOrDefault = info.Player?.voiceChat?.isTalking == true;
			bool valueOrDefault2 = info.Player?.voiceChat?.ttsVoice?.isSpeaking == true;
			if (valueOrDefault || valueOrDefault2)
			{
				ReviveHelper.LogInfo("Player \"" + info.Player.playerName + "\" is talking");
				return true;
			}
			return false;
		}

		private static void CheckCurrentLevel(int lvl)
		{
			if (CurrentLevel != lvl)
			{
				ReviveHelper.LogInfo("New level. Reset count revive");
				CurrentLevel = lvl;
				reviveInfos.Clear();
			}
		}

		private static void StartReviving(ReviveInfo info)
		{
			if (!InstaRevive)
			{
				info.AddMessage($"My revive will cost ${ReviveHelper.CalculateReviveCost(CurrentLevel, info.Count, ReviveCostMultiplier)}k");
				info.AddMessage("Reviving");
				string[] countdown = Countdown;
				foreach (string message in countdown)
				{
					info.AddMessage(message);
				}
			}
			info.SetReviving(r: true);
		}

		private static ReviveInfo GetReviveInfo(string steamId, PlayerAvatar player)
		{
			reviveInfos.TryGetValue(steamId, out ReviveInfo value);
			if (value != null)
			{
				return value;
			}
			value = new ReviveInfo(player);
			reviveInfos[steamId] = value;
			return value;
		}

		private static void Postfix(PlayerDeathHead __instance)
		{
			if (!SemiFunc.IsMasterClientOrSingleplayer())
			{
				return;
			}
			PlayerAvatar val = __instance?.playerAvatar;
			if ((Object)(object)val == (Object)null)
			{
				return;
			}
			string steamID = val.steamID;
			if (string.IsNullOrEmpty(steamID) || (Object)(object)__instance == (Object)null || !__instance.triggered)
			{
				return;
			}
			lock (_reviveLock)
			{
				CheckCurrentLevel(StatsManager.instance.GetRunStatLevel() + 1);
				ReviveInfo reviveInfo = GetReviveInfo(steamID, val);
				Speak(reviveInfo);
				reviveInfo.UpdateThrottle += Time.deltaTime;
				if (reviveInfo.UpdateThrottle < 1f)
				{
					return;
				}
				reviveInfo.UpdateThrottle = 0f;
				if (reviveInfo.CanRevive(CurrentLevel, CurrentCurrency, ReviveCooldownSeconds, ReviveCostMultiplier))
				{
					if (reviveInfo.IsPreparingToRevive)
					{
						Revive(reviveInfo);
					}
					else
					{
						StartReviving(reviveInfo);
					}
				}
			}
		}
	}
}