Decompiled source of FinalSunsetPermadeath v1.1.0

plugins\FinalSunsetPermadeath.dll

Decompiled 2 days ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyVersion("0.0.0.0")]
[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;
		}
	}
}
[BepInPlugin("fix.permadeath.finalsunset", "FinalSunsetPermadeath", "1.1.0")]
[BepInDependency(/*Could not decode attribute arguments.*/)]
[BepInDependency(/*Could not decode attribute arguments.*/)]
public class FinalSunsetPermadeath : BaseUnityPlugin
{
	[HarmonyPatch(typeof(Player), "CreateTombStone")]
	private static class Patch_CreateTombStone
	{
		[HarmonyPrefix]
		private static void Prefix(Player __instance, ref HashSet<int> __state)
		{
			__state = null;
			if (!((Object)(object)__instance != (Object)(object)Player.m_localPlayer))
			{
				__state = new HashSet<int>();
				TombStone[] array = Object.FindObjectsOfType<TombStone>();
				for (int i = 0; i < array.Length; i++)
				{
					__state.Add(((Object)array[i]).GetInstanceID());
				}
			}
		}

		[HarmonyPostfix]
		private static void Postfix(Player __instance, HashSet<int> __state)
		{
			if (__state == null)
			{
				return;
			}
			RealDeath = true;
			TombStone[] array = Object.FindObjectsOfType<TombStone>();
			TombStone val = null;
			for (int i = 0; i < array.Length; i++)
			{
				if (!__state.Contains(((Object)array[i]).GetInstanceID()))
				{
					val = array[i];
					break;
				}
			}
			if ((Object)(object)val == (Object)null)
			{
				Debug.Log((object)"[FinalSunsetPermadeath] No new tombstone detected — items may survive");
				return;
			}
			Container component = ((Component)val).GetComponent<Container>();
			if ((Object)(object)component == (Object)null)
			{
				return;
			}
			Inventory inventory = component.GetInventory();
			if (inventory != null)
			{
				List<ItemData> list = new List<ItemData>(inventory.GetAllItems());
				int count = list.Count;
				for (int j = 0; j < list.Count; j++)
				{
					inventory.RemoveItem(list[j]);
				}
				Debug.Log((object)("[FinalSunsetPermadeath] Destroyed " + count + " item(s) from tombstone [id=" + ((Object)val).GetInstanceID() + "]"));
			}
		}
	}

	[HarmonyPatch(typeof(Player), "OnDeath")]
	[HarmonyAfter(new string[] { "shudnal.ExtraSlots", "balrond.astafaraios.BalrondSecondChance" })]
	private static class Patch_OnDeath
	{
		[HarmonyPrefix]
		private static void Prefix(Player __instance)
		{
			if ((Object)(object)__instance == (Object)(object)Player.m_localPlayer)
			{
				RealDeath = false;
			}
		}

		[HarmonyPostfix]
		private static void Postfix(Player __instance)
		{
			if ((Object)(object)__instance != (Object)(object)Player.m_localPlayer || !RealDeath)
			{
				return;
			}
			RealDeath = false;
			if (FSkillData == null || FSkillLevel == null)
			{
				return;
			}
			Skills skills = ((Character)__instance).GetSkills();
			if ((Object)(object)skills == (Object)null || !(FSkillData.GetValue(skills) is IDictionary dictionary))
			{
				return;
			}
			int num = 0;
			foreach (object value in dictionary.Values)
			{
				if (FSkillLevel != null)
				{
					FSkillLevel.SetValue(value, 0f);
				}
				if (FSkillAcc != null)
				{
					FSkillAcc.SetValue(value, 0f);
				}
				num++;
			}
			Debug.Log((object)("[FinalSunsetPermadeath] Reset " + num + " skill(s) to level 0"));
		}
	}

	public const string PluginGUID = "fix.permadeath.finalsunset";

	public const string PluginName = "FinalSunsetPermadeath";

	public const string PluginVersion = "1.1.0";

	internal static bool RealDeath;

	internal static FieldInfo FSkillData;

	internal static FieldInfo FSkillLevel;

	internal static FieldInfo FSkillAcc;

	private void Awake()
	{
		//IL_007a: Unknown result type (might be due to invalid IL or missing references)
		BindingFlags bindingAttr = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
		FSkillData = typeof(Skills).GetField("m_skillData", bindingAttr);
		Type nestedType = typeof(Skills).GetNestedType("Skill", BindingFlags.Public | BindingFlags.NonPublic);
		if (nestedType != null)
		{
			FSkillLevel = nestedType.GetField("m_level", bindingAttr);
			FSkillAcc = nestedType.GetField("m_accumulator", bindingAttr);
		}
		MethodInfo method = typeof(Player).GetMethod("CreateTombStone", bindingAttr);
		new Harmony("fix.permadeath.finalsunset").PatchAll();
		((BaseUnityPlugin)this).Logger.LogInfo((object)("FinalSunsetPermadeath v1.1.0 loaded | skillData=" + R(FSkillData) + " level=" + R(FSkillLevel) + " acc=" + R(FSkillAcc) + " createTombStone=" + R(method)));
	}

	private static string R(object o)
	{
		if (o == null)
		{
			return "NULL";
		}
		return "ok";
	}
}