Decompiled source of RespawnAtDeath v1.1.6

plugins/RespawnAtDeath.dll

Decompiled a day ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Bootstrap;
using HarmonyLib;
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("RespawnAtDeath.cs")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("RespawnAtDeath.cs")]
[assembly: AssemblyCopyright("Copyright ©  2025")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("aa85ea7d-d04a-4cc3-bfd3-47a13618fd5c")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("1.0.0.0")]
[BepInPlugin("com.yourname.respawnatdeath", "Respawn At Death", "1.1.6")]
public class RespawnAtDeath : BaseUnityPlugin
{
	[HarmonyPatch(typeof(Player), "OnDeath")]
	public static class CaptureDeathLocationPatch
	{
		private static void Postfix(Player __instance)
		{
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			//IL_000c: 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_0013: 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_0025: Unknown result type (might be due to invalid IL or missing references)
			//IL_002a: Unknown result type (might be due to invalid IL or missing references)
			//IL_005a: Unknown result type (might be due to invalid IL or missing references)
			//IL_005b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0065: Unknown result type (might be due to invalid IL or missing references)
			//IL_0044: 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)
			Vector3 position = ((Component)__instance).transform.position;
			RaycastHit val = default(RaycastHit);
			if (Physics.Raycast(new Vector3(position.x, position.y + 10f, position.z), Vector3.down, ref val, 50f))
			{
				position.y = ((RaycastHit)(ref val)).point.y + 1f;
			}
			lastDeathPosition = position;
			Debug.Log((object)$"[RespawnAtDeath] Captured safe death position: {lastDeathPosition}");
			if (!isInjured)
			{
				isInjured = true;
				injuredDeaths = 1;
				injuredUntil = DateTime.UtcNow.AddMinutes(10.0);
				((Character)__instance).Message((MessageType)1, "You are now Injured! Be careful.", 0, (Sprite)null);
			}
			else
			{
				injuredDeaths++;
				if (injuredDeaths >= 3)
				{
					lastDeathPosition = Vector3.zero;
					((Character)__instance).Message((MessageType)1, "You died too many times while Injured! Respawn reset.", 0, (Sprite)null);
				}
			}
			((RespawnAtDeath)(object)Chainloader.ManagerObject.GetComponent(typeof(RespawnAtDeath))).SaveInjuredState();
		}
	}

	[HarmonyPatch(typeof(Game), "SpawnPlayer")]
	public static class OverrideSpawnLocationPatch
	{
		private static void Prefix(ref Vector3 spawnPoint)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_0016: Unknown result type (might be due to invalid IL or missing references)
			//IL_001b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0026: Unknown result type (might be due to invalid IL or missing references)
			if (lastDeathPosition != Vector3.zero)
			{
				spawnPoint = lastDeathPosition;
				Debug.Log((object)$"[RespawnAtDeath] Forcing spawn to death position: {spawnPoint}");
				return;
			}
			isInjured = false;
			injuredDeaths = 0;
			((RespawnAtDeath)(object)Chainloader.ManagerObject.GetComponent(typeof(RespawnAtDeath))).SaveInjuredState();
			Debug.Log((object)"[RespawnAtDeath] Respawned at default spawn. Injured state cleared.");
		}
	}

	private static Vector3 lastDeathPosition = Vector3.zero;

	private static bool isInjured = false;

	private static int injuredDeaths = 0;

	private static DateTime injuredUntil = DateTime.MinValue;

	private void Awake()
	{
		//IL_000d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0013: Expected O, but got Unknown
		LoadInjuredState();
		Harmony val = new Harmony("com.yourname.respawnatdeath");
		val.PatchAll();
		Debug.Log((object)"[RespawnAtDeath] Loaded. Player will respawn at their death location.");
	}

	private void Update()
	{
		if (isInjured && DateTime.UtcNow > injuredUntil)
		{
			isInjured = false;
			injuredDeaths = 0;
			SaveInjuredState();
			Debug.Log((object)"[RespawnAtDeath] Injured expired.");
		}
	}

	private void OnApplicationQuit()
	{
		//IL_0001: Unknown result type (might be due to invalid IL or missing references)
		//IL_0006: Unknown result type (might be due to invalid IL or missing references)
		lastDeathPosition = Vector3.zero;
		SaveInjuredState();
	}

	private void SaveInjuredState()
	{
		PlayerPrefs.SetInt("RespawnAtDeath_isInjured", isInjured ? 1 : 0);
		PlayerPrefs.SetInt("RespawnAtDeath_injuredDeaths", injuredDeaths);
		PlayerPrefs.SetString("RespawnAtDeath_injuredUntil", injuredUntil.ToBinary().ToString());
		PlayerPrefs.Save();
	}

	private void LoadInjuredState()
	{
		isInjured = PlayerPrefs.GetInt("RespawnAtDeath_isInjured", 0) == 1;
		injuredDeaths = PlayerPrefs.GetInt("RespawnAtDeath_injuredDeaths", 0);
		if (long.TryParse(PlayerPrefs.GetString("RespawnAtDeath_injuredUntil", "0"), out var result))
		{
			injuredUntil = DateTime.FromBinary(result);
		}
		else
		{
			injuredUntil = DateTime.MinValue;
		}
	}
}