Decompiled source of NowYouSleep v1.0.1

plugins/NowYouSleep.dll

Decompiled 4 days ago
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("NowYouSleep")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("NowYouSleep")]
[assembly: AssemblyCopyright("Copyright ©  2026")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("406b012c-b787-44eb-afa1-2e527bde40aa")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace NowYouSleep;

[BepInPlugin("hex.nowyousleep", "NowYouSleep", "1.0.1")]
public class Plugin : BaseUnityPlugin
{
	private Harmony _harmony;

	private ConfigEntry<bool> _modEnabled;

	private ConfigEntry<bool> _debugModeEnabled;

	internal const string PluginGuid = "hex.nowyousleep";

	internal const string PluginName = "NowYouSleep";

	internal const string PluginVersion = "1.0.1";

	internal static Plugin Instance { get; private set; }

	internal static ManualLogSource Log { get; private set; }

	internal bool IsNowYouSleepEnabled
	{
		get
		{
			if (_modEnabled != null)
			{
				return _modEnabled.Value;
			}
			return false;
		}
	}

	internal bool IsDebugModeEnabled
	{
		get
		{
			if (_debugModeEnabled != null)
			{
				return _debugModeEnabled.Value;
			}
			return false;
		}
	}

	private void Awake()
	{
		//IL_0059: Unknown result type (might be due to invalid IL or missing references)
		//IL_0063: Expected O, but got Unknown
		Instance = this;
		Log = ((BaseUnityPlugin)this).Logger;
		_modEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "Whether NowYouSleep is enabled.");
		_debugModeEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "DebugMode", false, "Whether to enable debug logging for NowYouSleep.");
		_harmony = new Harmony("hex.nowyousleep");
		_harmony.PatchAll();
		Log.LogInfo((object)"NowYouSleep v1.0.1 loaded.");
	}

	private void OnDestroy()
	{
		Log.LogInfo((object)"NowYouSleep v1.0.1 unloaded.");
		Harmony harmony = _harmony;
		if (harmony != null)
		{
			harmony.UnpatchSelf();
		}
		_harmony = null;
		Instance = null;
	}

	internal void LogDebug(string message)
	{
		if (IsDebugModeEnabled)
		{
			Log.LogInfo((object)("[Debug] " + message));
		}
	}
}
[HarmonyPatch(typeof(Game), "EverybodyIsTryingToSleep")]
internal static class GameEverybodyIsTryingToSleepPatch
{
	[HarmonyPrefix]
	private static bool Prefix(ref bool __result)
	{
		if ((Object)(object)Plugin.Instance == (Object)null || !Plugin.Instance.IsNowYouSleepEnabled)
		{
			return true;
		}
		if ((Object)(object)ZNet.instance == (Object)null)
		{
			Plugin.Instance.LogDebug("ZNet.instance is null. Falling back to vanilla sleep check.");
			return true;
		}
		if (!ZNet.instance.IsServer())
		{
			Plugin.Instance.LogDebug("Not running on server. Falling back to vanilla sleep check.");
			return true;
		}
		List<ZDO> allCharacterZDOS = ZNet.instance.GetAllCharacterZDOS();
		if (allCharacterZDOS == null || allCharacterZDOS.Count == 0)
		{
			return true;
		}
		foreach (ZDO item in allCharacterZDOS)
		{
			if (item.GetBool(ZDOVars.s_inBed, false))
			{
				Plugin.Instance.LogDebug("At least one player is trying to sleep. Allowing sleep skip.");
				__result = true;
				return false;
			}
		}
		return true;
	}
}