Decompiled source of ForsakenPowerDurationSleepReset v1.3.0

ForsakenPowerDurationSleepReset.dll

Decompiled 2 hours ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using Jotunn.Entities;
using Jotunn.Managers;
using Jotunn.Utils;
using Microsoft.CodeAnalysis;
using Splatform;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.6.2", FrameworkDisplayName = ".NET Framework 4.6.2")]
[assembly: AssemblyCompany("ForsakenPowerDurationSleepReset")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("ForsakenPowerDurationSleepReset")]
[assembly: AssemblyTitle("ForsakenPowerDurationSleepReset")]
[assembly: AssemblyVersion("1.0.0.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[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 ForsakenPowerDurationSleepReset
{
	[BepInPlugin("com.forsakenpowerdurationsleepreset", "ForsakenPowerDurationSleepReset", "1.3.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[NetworkCompatibility(/*Could not decode attribute arguments.*/)]
	public class Plugin : BaseUnityPlugin
	{
		public const string PluginGUID = "com.forsakenpowerdurationsleepreset";

		public const string PluginName = "ForsakenPowerDurationSleepReset";

		public const string PluginVersion = "1.3.0";

		internal static ConfigEntry<float> PowerDurationMinutes;

		internal static ConfigEntry<float> CooldownMinutes;

		internal static ConfigEntry<bool> ResetOnSleep;

		internal static ConfigEntry<bool> EndPowerOnSleep;

		internal static readonly FieldRef<Player, float> GuardianCooldown = AccessTools.FieldRefAccess<Player, float>("m_guardianPowerCooldown");

		internal static readonly FieldRef<Player, StatusEffect> GuardianSE = AccessTools.FieldRefAccess<Player, StatusEffect>("m_guardianSE");

		private readonly Harmony _harmony = new Harmony("com.forsakenpowerdurationsleepreset");

		internal const string ResetRpcName = "ForsakenPowerDurationSleepReset_ResetCooldown";

		internal static bool SleepOnlyMode => CooldownMinutes.Value < 0f;

		internal static bool SleepResetsCooldown
		{
			get
			{
				if (!SleepOnlyMode)
				{
					return ResetOnSleep.Value;
				}
				return true;
			}
		}

		private void Awake()
		{
			//IL_0031: Unknown result type (might be due to invalid IL or missing references)
			//IL_0036: Unknown result type (might be due to invalid IL or missing references)
			//IL_003d: Unknown result type (might be due to invalid IL or missing references)
			//IL_004a: Expected O, but got Unknown
			//IL_004a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0054: Expected O, but got Unknown
			//IL_008a: Unknown result type (might be due to invalid IL or missing references)
			//IL_008f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0096: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a3: Expected O, but got Unknown
			//IL_00a3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ad: Expected O, but got Unknown
			//IL_00d1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00dd: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ea: Expected O, but got Unknown
			//IL_00ea: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f4: Expected O, but got Unknown
			//IL_0118: 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_0124: Unknown result type (might be due to invalid IL or missing references)
			//IL_0131: Expected O, but got Unknown
			//IL_0131: Unknown result type (might be due to invalid IL or missing references)
			//IL_013b: Expected O, but got Unknown
			PowerDurationMinutes = ((BaseUnityPlugin)this).Config.Bind<float>("General", "PowerDurationMinutes", 30f, new ConfigDescription("How long a Forsaken power lasts once activated, in minutes.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(1f, 240f), new object[1] { (object)new ConfigurationManagerAttributes
			{
				IsAdminOnly = true,
				Order = 3
			} }));
			CooldownMinutes = ((BaseUnityPlugin)this).Config.Bind<float>("General", "CooldownMinutes", -1f, new ConfigDescription("Cooldown after using a Forsaken power, in minutes.\n-1 = no timer: the power only recharges when the night is slept through.\n0 = no cooldown at all.\nAbove 0 = normal countdown timer of that many minutes.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(-1f, 1440f), new object[1] { (object)new ConfigurationManagerAttributes
			{
				IsAdminOnly = true,
				Order = 2
			} }));
			ResetOnSleep = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "ResetOnSleep", true, new ConfigDescription("When CooldownMinutes is 0 or more, also recharge the power when the night is slept through.\nIgnored when CooldownMinutes is -1 (sleeping is then the only way to recharge).", (AcceptableValueBase)null, new object[1] { (object)new ConfigurationManagerAttributes
			{
				IsAdminOnly = true,
				Order = 1
			} }));
			EndPowerOnSleep = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "EndPowerOnSleep", false, new ConfigDescription("Sleeping through the night also ends any Forsaken power that is still running, instead of letting its remaining time carry over.\nWorks with any CooldownMinutes setting.", (AcceptableValueBase)null, new object[1] { (object)new ConfigurationManagerAttributes
			{
				IsAdminOnly = true,
				Order = 0
			} }));
			CommandManager.Instance.AddConsoleCommand((ConsoleCommand)(object)new ResetMineCommand());
			CommandManager.Instance.AddConsoleCommand((ConsoleCommand)(object)new ResetAllCommand());
			_harmony.PatchAll();
			((BaseUnityPlugin)this).Logger.LogInfo((object)"ForsakenPowerDurationSleepReset 1.3.0 loaded");
		}

		internal static void OnNightSkipped(Player player)
		{
			if (SleepResetsCooldown)
			{
				ResetCooldown(player);
			}
			if (EndPowerOnSleep.Value)
			{
				EndActivePower(player);
			}
		}

		internal static bool EndActivePower(Player player)
		{
			if ((Object)(object)player == (Object)null)
			{
				return false;
			}
			SEMan sEMan = ((Character)player).GetSEMan();
			bool flag = false;
			foreach (StatusEffect item in new List<StatusEffect>(sEMan.GetStatusEffects()))
			{
				if ((Object)(object)item != (Object)null && ((Object)item).name != null && ((Object)item).name.StartsWith("GP_") && sEMan.RemoveStatusEffect(item, false))
				{
					flag = true;
				}
			}
			if (flag)
			{
				((Character)player).Message((MessageType)2, "Your Forsaken power has faded with the night", 0, (Sprite)null, false);
			}
			return flag;
		}

		internal static bool ResetCooldown(Player player)
		{
			if ((Object)(object)player == (Object)null || GuardianCooldown.Invoke(player) <= 0f)
			{
				return false;
			}
			GuardianCooldown.Invoke(player) = 0f;
			((Character)player).Message((MessageType)2, "Your Forsaken power has recharged", 0, (Sprite)null, false);
			return true;
		}

		internal static void RPC_ResetCooldown(long sender)
		{
			ResetCooldown(Player.m_localPlayer);
		}

		internal static bool LocalIsAdmin(out string why)
		{
			why = null;
			if ((Object)(object)ZNet.instance == (Object)null)
			{
				why = "not connected";
				return false;
			}
			if (ZNet.instance.LocalPlayerIsAdminOrHost())
			{
				return true;
			}
			if (Terminal.m_cheat)
			{
				return true;
			}
			string arg = "?";
			try
			{
				arg = ((object)Unsafe.As<PlatformUserID, PlatformUserID>(ref UserInfo.GetLocalUser().UserId)/*cast due to .constrained prefix*/).ToString();
			}
			catch
			{
			}
			why = $"server={ZNet.instance.IsServer()}, adminListEntries={ZNet.instance.GetAdminList()?.Count}, steamId={arg}";
			return false;
		}
	}
	internal class ResetMineCommand : ConsoleCommand
	{
		public override string Name => "forsaken_reset";

		public override string Help => "Admin: recharge your own Forsaken power.";

		public override void Run(string[] args)
		{
			if (!Plugin.LocalIsAdmin(out var why))
			{
				Console.instance.Print("Admins only. (" + why + ")");
			}
			else if ((Object)(object)Player.m_localPlayer == (Object)null)
			{
				Console.instance.Print("You are not in a world.");
			}
			else
			{
				Console.instance.Print(Plugin.ResetCooldown(Player.m_localPlayer) ? "Your Forsaken power was recharged." : "Your Forsaken power was already ready.");
			}
		}
	}
	internal class ResetAllCommand : ConsoleCommand
	{
		public override string Name => "forsaken_reset_all";

		public override string Help => "Admin: recharge every player's Forsaken power (players need this mod installed).";

		public override void Run(string[] args)
		{
			if (!Plugin.LocalIsAdmin(out var why))
			{
				Console.instance.Print("Admins only. (" + why + ")");
				return;
			}
			if (ZRoutedRpc.instance == null)
			{
				Console.instance.Print("You are not in a world.");
				return;
			}
			ZRoutedRpc.instance.InvokeRoutedRPC(0L, "ForsakenPowerDurationSleepReset_ResetCooldown", Array.Empty<object>());
			Console.instance.Print("Recharge sent to all players.");
		}
	}
	[HarmonyPatch(typeof(StatusEffect), "Setup")]
	internal static class StatusEffect_Setup_Patch
	{
		private static void Postfix(StatusEffect __instance)
		{
			if ((Object)(object)__instance != (Object)null && ((Object)__instance).name != null && ((Object)__instance).name.StartsWith("GP_"))
			{
				__instance.m_ttl = Plugin.PowerDurationMinutes.Value * 60f;
			}
		}
	}
	[HarmonyPatch(typeof(Player), "ActivateGuardianPower")]
	internal static class Player_ActivateGuardianPower_Patch
	{
		private static void Prefix(Player __instance)
		{
			StatusEffect val = Plugin.GuardianSE.Invoke(__instance);
			if (!((Object)(object)val == (Object)null))
			{
				val.m_cooldown = (Plugin.SleepOnlyMode ? 60f : (Plugin.CooldownMinutes.Value * 60f));
			}
		}
	}
	[HarmonyPatch(typeof(Player), "UpdateGuardianPower")]
	internal static class Player_UpdateGuardianPower_Patch
	{
		private static bool Prefix(Player __instance)
		{
			if (!Plugin.SleepOnlyMode)
			{
				return true;
			}
			return Plugin.GuardianCooldown.Invoke(__instance) <= 0f;
		}
	}
	[HarmonyPatch(typeof(Hud), "UpdateGuardianPower")]
	internal static class Hud_UpdateGuardianPower_Patch
	{
		private static void Postfix(Hud __instance)
		{
			if (Plugin.SleepOnlyMode)
			{
				Player localPlayer = Player.m_localPlayer;
				if (!((Object)(object)localPlayer == (Object)null) && !(Plugin.GuardianCooldown.Invoke(localPlayer) <= 0f))
				{
					Traverse.Create((object)__instance).Field("m_gpCooldown").Property("text", (object[])null)
						.SetValue((object)"Sleep");
				}
			}
		}
	}
	[HarmonyPatch(typeof(Game), "SleepStop")]
	internal static class Game_SleepStop_Patch
	{
		private static void Postfix()
		{
			Player localPlayer = Player.m_localPlayer;
			if ((Object)(object)localPlayer != (Object)null)
			{
				Plugin.OnNightSkipped(localPlayer);
			}
		}
	}
	[HarmonyPatch(typeof(Game), "Start")]
	internal static class Game_Start_Patch
	{
		private static void Postfix()
		{
			ZRoutedRpc.instance.Register("ForsakenPowerDurationSleepReset_ResetCooldown", (Action<long>)Plugin.RPC_ResetCooldown);
		}
	}
}