Decompiled source of DreamReset v1.0.2

DreamReset.dll

Decompiled 13 hours ago
using System;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: AssemblyVersion("0.0.0.0")]
namespace SleepResetPowers;

[BepInPlugin("DreamReset", "DreamReset", "1.0.2")]
public class SleepResetPowersPlugin : BaseUnityPlugin
{
	public const string PluginGuid = "DreamReset";

	public const string PluginName = "DreamReset";

	public const string PluginVersion = "1.0.2";

	private static readonly FieldInfo CooldownField = AccessTools.Field(typeof(Player), "m_guardianPowerCooldown");

	private static readonly MethodInfo InBedMethod = AccessTools.Method(typeof(Player), "InBed", (Type[])null, (Type[])null);

	private static ManualLogSource Log;

	private ConfigEntry<bool> _enabled;

	private ConfigEntry<bool> _showMessage;

	private ConfigEntry<bool> _verboseLogging;

	private bool _wasInBed;

	private bool _brokenByGameUpdate;

	private void Awake()
	{
		Log = ((BaseUnityPlugin)this).Logger;
		_enabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "Master switch. When false, cooldowns are never reset by this mod.");
		_showMessage = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "ShowMessage", true, "Show a short on-screen message when your Forsaken Power is refreshed after waking up.");
		_verboseLogging = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "VerboseLogging", false, "Log a line to the console every time a cooldown is reset (useful for testing).");
		if (CooldownField == null)
		{
			Log.LogError((object)"Could not find Player.m_guardianPowerCooldown — a game update may have renamed it. Cooldown reset is disabled.");
		}
		Log.LogInfo((object)"DreamReset v1.0.2 loaded.");
	}

	private void Update()
	{
		if (!_enabled.Value || CooldownField == null || _brokenByGameUpdate)
		{
			return;
		}
		Player localPlayer = Player.m_localPlayer;
		if ((Object)(object)localPlayer == (Object)null)
		{
			_wasInBed = false;
			return;
		}
		bool flag = IsInBed(localPlayer);
		if (_wasInBed && !flag)
		{
			ResetGuardianPowerCooldown(localPlayer);
		}
		_wasInBed = flag;
	}

	private bool IsInBed(Player player)
	{
		try
		{
			if (InBedMethod != null)
			{
				return (bool)InBedMethod.Invoke(player, null);
			}
			ZNetView component = ((Component)player).GetComponent<ZNetView>();
			if ((Object)(object)component != (Object)null && component.IsValid())
			{
				return component.GetZDO().GetBool("inBed", false);
			}
		}
		catch (Exception ex)
		{
			DisableAfterError("reading the player's bed state", ex);
		}
		return false;
	}

	private void ResetGuardianPowerCooldown(Player player)
	{
		try
		{
			float num = (float)CooldownField.GetValue(player);
			if (!(num <= 0f))
			{
				CooldownField.SetValue(player, 0f);
				if (_showMessage.Value)
				{
					((Character)player).Message((MessageType)2, "Forsaken Power ready", 0, (Sprite)null);
				}
				if (_verboseLogging.Value)
				{
					Log.LogInfo((object)$"Player woke up — reset Forsaken Power cooldown (was {num:0.#}s).");
				}
			}
		}
		catch (Exception ex)
		{
			DisableAfterError("resetting the Forsaken Power cooldown", ex);
		}
	}

	private void DisableAfterError(string whileDoing, Exception ex)
	{
		_brokenByGameUpdate = true;
		Log.LogError((object)("Error while " + whileDoing + " — disabling until next restart. " + ex.Message));
	}
}