using System;
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 Fusion;
using HarmonyLib;
using UnityEngine;
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("NoBedtime")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("NoBedtime")]
[assembly: AssemblyCopyright("Copyright © 2024")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("0bd62a28-1041-4cbe-82ab-f108dcf8ed05")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace NoBedtime;
[BepInPlugin("randyknapp.mods.nobedtime", "No Bedtime", "0.1.0")]
public class NoBedtime : BaseUnityPlugin
{
public const string GUID = "randyknapp.mods.nobedtime";
public const string NAME = "No Bedtime";
public const string VERSION = "0.1.0";
public static ConfigEntry<int> SleepHoursOverride;
public static ConfigEntry<int> BedtimeHoursOverride;
public static ConfigEntry<int> AlarmHour;
public static ManualLogSource logger;
public void Awake()
{
logger = ((BaseUnityPlugin)this).Logger;
SleepHoursOverride = ((BaseUnityPlugin)this).Config.Bind<int>("Config", "Sleep Hours Override", -1, "The number of in-game hours to pass when sleeping. If set to -1, does not override the game's default value (10 hours).");
BedtimeHoursOverride = ((BaseUnityPlugin)this).Config.Bind<int>("Config", "Bedtime Hours Override", 0, "The number of in-game hours before you can sleep again. If set to -1, does not override the game's default value (24 hours).");
AlarmHour = ((BaseUnityPlugin)this).Config.Bind<int>("Config", "Alarm Hour", -1, "What time of day to wake, regardless of when you slept. If set to -1, does nothing. If in use, 'Sleep Hours Override' is ignored.");
((BaseUnityPlugin)this).Logger.LogWarning((object)"No Bedtime Loaded");
((BaseUnityPlugin)this).Logger.LogWarning((object)$"- Sleep Hours Override: {SleepHoursOverride.Value}");
((BaseUnityPlugin)this).Logger.LogWarning((object)$"- Bedtime Hours Override: {BedtimeHoursOverride.Value}");
((BaseUnityPlugin)this).Logger.LogWarning((object)$"- Alarm Hour: {AlarmHour.Value}");
Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly(), "randyknapp.mods.nobedtime");
}
public void Update()
{
}
}
[HarmonyPatch]
public static class GlobalDataPatch
{
private static int lastTimeSinceSleep;
private static int GetMinutesSinceSleep()
{
if (!GlobalDataHelper.IsGlobalDataValid())
{
return 0;
}
if (EnviroSkyMgr.instance.GetCurrentHour() >= 24)
{
return 0;
}
GlobalData m_GlobalData = Mainframe.code.M_GlobalData;
DateTime dateTime = new DateTime(2024, 1, 1, m_GlobalData.SleepHour, m_GlobalData.SleepMinute, 0);
DateTime dateTime2 = new DateTime(2024, 1, (EnviroSkyMgr.instance.GetCurrentHour() >= m_GlobalData.SleepHour) ? 1 : 2, EnviroSkyMgr.instance.GetCurrentHour(), EnviroSkyMgr.instance.GetCurrentMinute(), 0);
return (int)(dateTime2 - dateTime).TotalMinutes;
}
[HarmonyPatch(typeof(Bed), "GetSleepState")]
[HarmonyPrefix]
public static bool Bed_GetSleepState_Prefix(ref float __result)
{
if (!GlobalDataHelper.IsGlobalDataValid())
{
return true;
}
if (Mainframe.code.M_GlobalData.MaxSleepCoolingTime == 0)
{
__result = 0f;
return false;
}
int minutesSinceSleep = GetMinutesSinceSleep();
__result = (float)minutesSinceSleep / (float)Mainframe.code.M_GlobalData.MaxSleepCoolingTime;
return false;
}
[HarmonyPatch(typeof(GlobalData), "SetNextSleepTime")]
[HarmonyPostfix]
public static void SetNextSleepTime_Postfix(GlobalData __instance)
{
int num = __instance.SleepTime;
bool flag = NoBedtime.AlarmHour.Value >= 0;
if (flag)
{
__instance.SleepTime = 0;
int currentHour = EnviroSkyMgr.instance.GetCurrentHour();
num = (24 + NoBedtime.AlarmHour.Value - currentHour) % 24;
if (num == 0)
{
num = 24;
}
NoBedtime.logger.LogWarning((object)$"Alarm set for {NoBedtime.AlarmHour.Value}. Sleeping for {num} hours");
}
else if (NoBedtime.SleepHoursOverride.Value >= 0)
{
num = (__instance.SleepTime = NoBedtime.SleepHoursOverride.Value);
}
int num2 = ((NoBedtime.BedtimeHoursOverride.Value < 0) ? 24 : NoBedtime.BedtimeHoursOverride.Value);
__instance.MaxSleepCoolingTime = Mathf.Max(0, num2 * 60);
__instance.CanSleep = __instance.MaxSleepCoolingTime == 0;
if (flag && ((SimulationBehaviour)__instance).Object.HasStateAuthority)
{
__instance.SetCurTime((float)(EnviroSkyMgr.instance.GetCurrentHour() + num));
}
}
[HarmonyPatch(typeof(GlobalData), "Update")]
[HarmonyPostfix]
public static void Update_Postfix(GlobalData __instance)
{
if (!__instance.CanSleep)
{
int minutesSinceSleep = GetMinutesSinceSleep();
if (minutesSinceSleep != lastTimeSinceSleep)
{
lastTimeSinceSleep = minutesSinceSleep;
NoBedtime.logger.LogInfo((object)$"Time Since Sleep: {minutesSinceSleep} / ({__instance.MaxSleepCoolingTime})");
}
if (minutesSinceSleep >= __instance.MaxSleepCoolingTime)
{
NoBedtime.logger.LogWarning((object)"Can Sleep!");
__instance.CanSleep = true;
__instance.SleepCoolingTime = 0;
}
}
}
}