Decompiled source of PowerCooldownOnSleep v1.0.0

PowerCooldownOnSleep.dll

Decompiled 2 weeks ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyCompany("PowerCooldownOnSleep")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyDescription("Sleeping advances the guardian power cooldown along with world time instead of leaving it frozen.")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+45f1db0b0085dc39ab45145208d0b526eb021323")]
[assembly: AssemblyProduct("PowerCooldownOnSleep")]
[assembly: AssemblyTitle("PowerCooldownOnSleep")]
[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 PowerCooldownOnSleep
{
	[BepInPlugin("org.vibeodrone.powercooldownonsleep", "PowerCooldownOnSleep", "1.0.0")]
	public class PowerCooldownOnSleepPlugin : BaseUnityPlugin
	{
		public const string PluginGuid = "org.vibeodrone.powercooldownonsleep";

		public const string PluginName = "PowerCooldownOnSleep";

		public const string PluginVersion = "1.0.0";

		public const string BoostyUrl = "https://boosty.to/vibeodrone";

		internal static ManualLogSource Log;

		internal static ConfigEntry<float> CooldownFactor;

		private Harmony _harmony;

		private void Awake()
		{
			//IL_0055: Unknown result type (might be due to invalid IL or missing references)
			//IL_005f: Expected O, but got Unknown
			Log = ((BaseUnityPlugin)this).Logger;
			((BaseUnityPlugin)this).Config.Bind<string>("Support", "Boosty", "https://boosty.to/vibeodrone", "Follow Vibe O'Drone on Boosty.");
			CooldownFactor = ((BaseUnityPlugin)this).Config.Bind<float>("General", "CooldownFactor", 1f, "How much of the slept game time is taken off the cooldown. 1.0 means the cooldown flows exactly like world time, 0.5 is half as fast, 0 is vanilla.");
			_harmony = new Harmony("org.vibeodrone.powercooldownonsleep");
			_harmony.PatchAll(typeof(SetSleepingPatch));
			Log.LogInfo((object)"PowerCooldownOnSleep 1.0.0 loaded.");
		}

		private void OnDestroy()
		{
			Harmony harmony = _harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
		}
	}
	[HarmonyPatch(typeof(Player), "SetSleeping")]
	internal static class SetSleepingPatch
	{
		private static bool _asleep;

		private static double _netTimeAtSleepStart;

		private static float _cooldownAtSleepStart;

		private static void Prefix(Player __instance, bool sleep)
		{
			try
			{
				if (!((Object)(object)__instance != (Object)(object)Player.m_localPlayer) && __instance.IsSleeping() != sleep)
				{
					if (sleep)
					{
						OnFallAsleep(__instance);
					}
					else
					{
						OnWakeUp(__instance);
					}
				}
			}
			catch (Exception ex)
			{
				_asleep = false;
				PowerCooldownOnSleepPlugin.Log.LogError((object)("Patch threw, the cooldown was left as it was: " + ex));
			}
		}

		private static void OnFallAsleep(Player player)
		{
			if ((Object)(object)ZNet.instance == (Object)null)
			{
				_asleep = false;
				return;
			}
			_netTimeAtSleepStart = ZNet.instance.GetTimeSeconds();
			_cooldownAtSleepStart = player.m_guardianPowerCooldown;
			_asleep = true;
		}

		private static void OnWakeUp(Player player)
		{
			if (!_asleep)
			{
				return;
			}
			_asleep = false;
			if (!((Object)(object)ZNet.instance == (Object)null))
			{
				double num = ZNet.instance.GetTimeSeconds() - _netTimeAtSleepStart;
				if (!IsPlausible(num))
				{
					PowerCooldownOnSleepPlugin.Log.LogWarning((object)("Slept " + num.ToString("F0") + " game seconds, which does not look like sleep. Leaving the cooldown alone."));
					return;
				}
				float guardianPowerCooldown = player.m_guardianPowerCooldown;
				float num2 = (player.m_guardianPowerCooldown = Mathf.Max(0f, _cooldownAtSleepStart - (float)num * PowerCooldownOnSleepPlugin.CooldownFactor.Value));
				PowerCooldownOnSleepPlugin.Log.LogInfo((object)("Slept " + num.ToString("F0") + " game seconds, guardian power cooldown " + guardianPowerCooldown.ToString("F0") + " -> " + num2.ToString("F0") + "."));
			}
		}

		private static bool IsPlausible(double sleptSeconds)
		{
			if (sleptSeconds <= 0.0)
			{
				return false;
			}
			double num = (((Object)(object)EnvMan.instance != (Object)null) ? ((double)EnvMan.instance.m_dayLengthSec) : 1200.0);
			return sleptSeconds <= num * 2.0;
		}
	}
}