using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
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 BepInEx.Logging;
using HarmonyLib;
using UnityEngine;
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("LetMeSleep")]
[assembly: AssemblyDescription("Skip the night with only some players in bed.")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Blockheim")]
[assembly: AssemblyProduct("LetMeSleep")]
[assembly: AssemblyCopyright("Copyright © 2023 Blockheim")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("e3243d22-4307-4008-ba36-9f326008cde5")]
[assembly: AssemblyFileVersion("1.0.4")]
[assembly: TargetFramework(".NETFramework,Version=v4.6.2", FrameworkDisplayName = ".NET Framework 4.6.2")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.4.0")]
namespace LetMeSleep;
[BepInPlugin("blockchaaain.LetMeSleep", "LetMeSleep", "1.0.4")]
[BepInIncompatibility("com.rinsev.SkipSleep")]
[BepInIncompatibility("Azumatt.SleepSkip")]
[BepInIncompatibility("com.comoyi.valheim.SleepPlease")]
[HarmonyPatch]
public class LetMeSleepPlugin : BaseUnityPlugin
{
public const string PluginGUID = "blockchaaain.LetMeSleep";
public const string PluginName = "LetMeSleep";
public const string PluginVersion = "1.0.4";
private static readonly Harmony harmony = new Harmony("blockchaaain.LetMeSleep");
private static ConfigFile configFile = new ConfigFile(Path.Combine(Paths.ConfigPath, "blockchaaain.LetMeSleep.cfg"), true);
private static ConfigEntry<double> ratio = configFile.Bind<double>("General", "ratio", 0.5, new ConfigDescription("Fraction of players needed in bed to skip the night.", (AcceptableValueBase)(object)new AcceptableValueRange<double>(0.01, 1.0), Array.Empty<object>()));
private static ConfigEntry<bool> showMessage = configFile.Bind<bool>("General", "showMessage", true, "Show a chat message with the number of players currently in bed.");
private static ManualLogSource Logger;
private static int prevInBed = 0;
private void Awake()
{
harmony.PatchAll();
Logger = ((BaseUnityPlugin)this).Logger;
}
private void OnDestroy()
{
harmony.UnpatchSelf();
}
[HarmonyPrefix]
[HarmonyPatch(typeof(Game), "EverybodyIsTryingToSleep")]
private static bool LetMeSleep(ref Game __instance, ref bool __result)
{
//IL_0075: Unknown result type (might be due to invalid IL or missing references)
//IL_007a: Unknown result type (might be due to invalid IL or missing references)
//IL_00ce: Unknown result type (might be due to invalid IL or missing references)
List<ZDO> allCharacterZDOS = ZNet.instance.GetAllCharacterZDOS();
int count = allCharacterZDOS.Count;
if (count == 0)
{
__result = false;
return false;
}
int num = allCharacterZDOS.Where((ZDO zdo) => zdo.GetBool("inBed", false)).Count();
double num2 = Convert.ToDouble(num) / (double)allCharacterZDOS.Count;
if (showMessage.Value && num > 0 && num != prevInBed)
{
try
{
Vector3 zero = Vector3.zero;
int num3 = 2;
UserInfo localUser = UserInfo.GetLocalUser();
localUser.Name = "Server";
string text = $"{num:d}/{count:d} ({num2:p0}) sleeping";
string networkUserId = PrivilegeManager.GetNetworkUserId();
ZRoutedRpc.instance.InvokeRoutedRPC(ZRoutedRpc.Everybody, "ChatMessage", new object[5] { zero, num3, localUser, text, networkUserId });
}
catch (Exception ex)
{
Logger.LogWarning((object)("Exception while sending server message from LetMeSleep:" + Environment.NewLine + ex.Message));
Logger.LogMessage((object)"Consider disabling server messages in blockchaaain.LetMeSleep.cfg");
Logger.LogMessage((object)"Bug reports can be submitted at: https://github.com/blockchaaain/LetMeSleep");
showMessage.Value = false;
}
}
prevInBed = num;
if (num2 >= ratio.Value)
{
__result = true;
return false;
}
__result = false;
return false;
}
}