Decompiled source of HardcoreDeath v1.3.2

HardcoreDeath.dll

Decompiled 5 months ago
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using Jotunn;
using Jotunn.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: AssemblyTitle("HardcoreDeath")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("HardcoreDeath")]
[assembly: AssemblyCopyright("Copyright ©  2021")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("e3243d22-4307-4008-ba36-9f326008cde5")]
[assembly: AssemblyFileVersion("0.0.1.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.6.2", FrameworkDisplayName = ".NET Framework 4.6.2")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.0.1.0")]
namespace HardcoreDeath;

public static class Configs
{
	public static ConfigEntry<bool> EnableMod;

	public static ConfigEntry<bool> RemoveBackups;

	public static ConfigEntry<bool> RemoveWorld;

	public static ConfigEntry<bool> EnableMercifulDeath;

	public static ConfigEntry<int> MercifulDeathCooldown;

	public static ConfigurationManagerAttributes IsAdminOnly = new ConfigurationManagerAttributes
	{
		IsAdminOnly = true
	};

	public static void SetupConfigs(ConfigFile config)
	{
		//IL_0029: Unknown result type (might be due to invalid IL or missing references)
		//IL_0033: Expected O, but got Unknown
		//IL_0060: Unknown result type (might be due to invalid IL or missing references)
		//IL_006a: Expected O, but got Unknown
		//IL_0097: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a1: Expected O, but got Unknown
		//IL_00ce: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d8: Expected O, but got Unknown
		//IL_0113: Unknown result type (might be due to invalid IL or missing references)
		//IL_011d: Expected O, but got Unknown
		EnableMod = config.Bind<bool>("General", GenerateConfigKey("EnableMod"), true, new ConfigDescription("Determines if the mod should run at all. This is important for when switching characters!", (AcceptableValueBase)null, new object[1] { SetOrder(4) }));
		RemoveBackups = config.Bind<bool>("General", GenerateConfigKey("RemoveBackups"), true, new ConfigDescription("Enable this to also delete the backups.", (AcceptableValueBase)null, new object[1] { SetOrder(3) }));
		RemoveWorld = config.Bind<bool>("General", GenerateConfigKey("RemoveWorld"), false, new ConfigDescription("Enable this to also delete the world on death.", (AcceptableValueBase)null, new object[1] { SetOrder(2) }));
		EnableMercifulDeath = config.Bind<bool>("General", GenerateConfigKey("EnableMercifulDeath"), true, new ConfigDescription("If enabled, when receiving a final blow, your health will go down to 1 point instead. Can only be triggered once in a while.", (AcceptableValueBase)null, new object[1] { SetOrder(1) }));
		MercifulDeathCooldown = config.Bind<int>("General", GenerateConfigKey("MercifulDeathCooldown", "min"), 10, new ConfigDescription("How often, in minutes, the merciful death effect can be triggered (only if enabled).", (AcceptableValueBase)(object)new AcceptableValueRange<int>(10, 60), new object[1] { SetOrder(0) }));
	}

	public static string GenerateConfigKey(string key)
	{
		return GenerateConfigKey(key, null);
	}

	public static string GenerateConfigKey(string key, string unit)
	{
		key = string.Concat(key.Select((char x) => char.IsUpper(x) ? (" " + x) : x.ToString())).TrimStart(new char[1] { ' ' });
		if (!string.IsNullOrEmpty(unit))
		{
			key += $" ({unit})";
		}
		return key;
	}

	public static ConfigurationManagerAttributes SetOrder(int order, bool isAdvanced = false, bool isAdminOnly = true)
	{
		//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_0012: Unknown result type (might be due to invalid IL or missing references)
		//IL_001e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0027: Expected O, but got Unknown
		return new ConfigurationManagerAttributes
		{
			Order = order,
			IsAdvanced = isAdvanced,
			IsAdminOnly = isAdminOnly
		};
	}
}
[BepInPlugin("HardcoreDeath", "HardcoreDeath", "1.3.2")]
[BepInDependency(/*Could not decode attribute arguments.*/)]
[NetworkCompatibility(/*Could not decode attribute arguments.*/)]
internal class HardcoreDeath : BaseUnityPlugin
{
	[HarmonyPatch(typeof(Player), "Update")]
	public static class Player_Update_Patch
	{
		public static void Postfix()
		{
			if (mercifulDeathCooldown > 0f)
			{
				mercifulDeathCooldown -= Time.deltaTime;
			}
		}
	}

	[HarmonyPatch(typeof(Player), "OnDeath")]
	public static class Player_OnDeath_Patch
	{
		public static bool Prefix(Player __instance)
		{
			if (Configs.EnableMod.Value && Configs.EnableMercifulDeath.Value && mercifulDeathCooldown <= 0f)
			{
				((Character)__instance).Message((MessageType)2, "$msg_softdeath", 0, (Sprite)null);
				((Character)__instance).GetSEMan().RemoveStatusEffect(Character.s_statusEffectLightning, false);
				((Character)__instance).GetSEMan().RemoveStatusEffect(Character.s_statusEffectBurning, false);
				((Character)__instance).GetSEMan().RemoveStatusEffect(Character.s_statusEffectPoison, false);
				((Character)__instance).GetSEMan().RemoveStatusEffect(Character.s_statusEffectFrost, false);
				((Character)__instance).SetHealth(1f);
				mercifulDeathCooldown = Configs.MercifulDeathCooldown.Value * 60;
				justTriggeredMercifulDeath = true;
				return false;
			}
			return true;
		}

		public static void Postfix(Player __instance)
		{
			if (!Configs.EnableMod.Value || !((Object)(object)Player.m_localPlayer != (Object)null) || !((Object)(object)Player.m_localPlayer == (Object)(object)__instance))
			{
				return;
			}
			if (justTriggeredMercifulDeath)
			{
				justTriggeredMercifulDeath = false;
				return;
			}
			characterToRemove = __instance.GetPlayerName();
			Logger.LogInfo((object)$"Player '{characterToRemove}' has died. All save files will be deleted...");
			if (Configs.RemoveWorld.Value)
			{
				worldToRemove = ZNet.World.m_name;
				Logger.LogInfo((object)$"World '{worldToRemove}' will also be deleted.");
			}
			if (Configs.RemoveBackups.Value)
			{
				Logger.LogInfo((object)"Backup files will also be removed.");
			}
			Game.instance.Logout(false, true);
		}
	}

	[HarmonyPatch(typeof(FejdStartup), "Start")]
	public static class FejdStartup_Start_Patch
	{
		public static void Prefix()
		{
			if (!Configs.EnableMod.Value || characterToRemove == null)
			{
				return;
			}
			SaveWithBackups val = default(SaveWithBackups);
			if (SaveSystem.TryGetSaveByName(characterToRemove, (SaveDataType)1, ref val) && !val.IsDeleted)
			{
				SaveFile[] backupFiles = val.BackupFiles;
				SaveFile[] allFiles = val.AllFiles;
				foreach (SaveFile val2 in allFiles)
				{
					if (!backupFiles.Contains(val2) || Configs.RemoveBackups.Value)
					{
						if (SaveSystem.Delete(val2))
						{
							Logger.LogInfo((object)$"File '{val2.FileName}' deleted.");
						}
						else
						{
							Logger.LogError((object)$"Error when attempting to delete file '{val2.FileName}'.");
						}
					}
				}
				if (Configs.RemoveWorld.Value && worldToRemove != null)
				{
					SaveWithBackups val3 = default(SaveWithBackups);
					if (SaveSystem.TryGetSaveByName(worldToRemove, (SaveDataType)0, ref val3) && !val3.IsDeleted)
					{
						SaveFile[] backupFiles2 = val3.BackupFiles;
						SaveFile[] allFiles2 = val3.AllFiles;
						foreach (SaveFile val4 in allFiles2)
						{
							if (!backupFiles2.Contains(val4) || Configs.RemoveBackups.Value)
							{
								if (SaveSystem.Delete(val4))
								{
									Logger.LogInfo((object)$"File '{val4.FileName}' deleted.");
								}
								else
								{
									Logger.LogError((object)$"Error when attempting to delete file '{val4.FileName}'.");
								}
							}
						}
					}
					else
					{
						Logger.LogError((object)$"Error when attempting to delete world's save data '{worldToRemove}'.");
					}
				}
			}
			else
			{
				Logger.LogError((object)$"Error when attempting to delete player's save data '{characterToRemove}'.");
			}
			characterToRemove = null;
			worldToRemove = null;
		}
	}

	public const string PluginGUID = "HardcoreDeath";

	public const string PluginName = "HardcoreDeath";

	public const string PluginVersion = "1.3.2";

	private static Harmony harmony;

	private static string characterToRemove = null;

	private static string worldToRemove = null;

	private static float mercifulDeathCooldown = -1f;

	private static bool justTriggeredMercifulDeath = false;

	private void Awake()
	{
		Configs.SetupConfigs(((BaseUnityPlugin)this).Config);
		harmony = Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly(), "HardcoreDeath");
	}

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