using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using TMPro;
using UnityEngine;
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyVersion("1.0.0.0")]
public class AedenthornUtils
{
public static bool IgnoreKeyPresses(bool extra = false)
{
if (!extra)
{
int result;
if (!((Object)(object)ZNetScene.instance == (Object)null) && !((Object)(object)Player.m_localPlayer == (Object)null) && !Minimap.IsOpen() && !Console.IsVisible() && !TextInput.IsVisible() && !ZNet.instance.InPasswordDialog())
{
Chat instance = Chat.instance;
result = ((instance != null && instance.HasFocus()) ? 1 : 0);
}
else
{
result = 1;
}
return (byte)result != 0;
}
int result2;
if (!((Object)(object)ZNetScene.instance == (Object)null) && !((Object)(object)Player.m_localPlayer == (Object)null) && !Minimap.IsOpen() && !Console.IsVisible() && !TextInput.IsVisible() && !ZNet.instance.InPasswordDialog())
{
Chat instance2 = Chat.instance;
if ((instance2 == null || !instance2.HasFocus()) && !StoreGui.IsVisible() && !InventoryGui.IsVisible() && !Menu.IsVisible())
{
TextViewer instance3 = TextViewer.instance;
result2 = ((instance3 != null && instance3.IsVisible()) ? 1 : 0);
goto IL_00d2;
}
}
result2 = 1;
goto IL_00d2;
IL_00d2:
return (byte)result2 != 0;
}
public static bool CheckKeyDown(string value)
{
try
{
return Input.GetKeyDown(value.ToLower());
}
catch
{
return false;
}
}
public static bool CheckKeyHeld(string value, bool req = true)
{
try
{
return Input.GetKey(value.ToLower());
}
catch
{
return !req;
}
}
}
namespace SleepWithoutSpawn;
[BepInPlugin("aedenthorn.SleepWithoutSpawn", "Sleep Without Spawn", "0.3.0")]
public class BepInExPlugin : BaseUnityPlugin
{
[HarmonyPatch(typeof(Bed), "GetHoverText")]
private static class Bed_GetHoverText_Patch
{
private static void Postfix(Bed __instance, ref string __result, ZNetView ___m_nview)
{
if ((!unclaimedOnly.Value || ___m_nview.GetZDO().GetLong("owner", 0L) == 0L) && !Traverse.Create((object)__instance).Method("IsCurrent", Array.Empty<object>()).GetValue<bool>())
{
__result += Localization.instance.Localize("\n[" + modKey.Value + "+<color=yellow><b>$KEY_Use</b></color>] $piece_bed_sleep");
}
}
}
[HarmonyPatch(typeof(Bed), "Interact")]
private static class Bed_Interact_Patch
{
private static bool Prefix(Bed __instance, Humanoid human, bool repeat, ref bool __result, ZNetView ___m_nview)
{
//IL_01d0: Unknown result type (might be due to invalid IL or missing references)
if (((!allowDaySleep.Value || EnvMan.instance.IsAfternoon() || EnvMan.instance.IsNight()) && Traverse.Create((object)__instance).Method("IsCurrent", Array.Empty<object>()).GetValue<bool>()) || repeat || !AedenthornUtils.CheckKeyHeld(modKey.Value) || (unclaimedOnly.Value && ___m_nview.GetZDO().GetLong("owner", 0L) != 0))
{
return true;
}
Dbgl("trying to sleep on bed");
Player val = (Player)(object)((human is Player) ? human : null);
if (!allowDaySleep.Value && !EnvMan.instance.IsAfternoon() && !EnvMan.instance.IsNight())
{
((Character)human).Message((MessageType)2, "$msg_cantsleep", 0, (Sprite)null);
__result = false;
return false;
}
if (!Traverse.Create((object)__instance).Method("CheckEnemies", new object[1] { val }).GetValue<bool>())
{
__result = false;
return false;
}
if (!Traverse.Create((object)__instance).Method("CheckExposure", new object[1] { val }).GetValue<bool>())
{
__result = false;
return false;
}
if (!Traverse.Create((object)__instance).Method("CheckFire", new object[1] { val }).GetValue<bool>())
{
__result = false;
return false;
}
if (!Traverse.Create((object)__instance).Method("CheckWet", new object[1] { val }).GetValue<bool>())
{
__result = false;
return false;
}
((Character)human).AttachStart(__instance.m_spawnPoint, ((Component)__instance).gameObject, true, true, false, "attach_bed", new Vector3(0f, 0.5f, 0f), (Transform)null);
__result = false;
return false;
}
}
[HarmonyPatch(typeof(Terminal), "InputText")]
private static class InputText_Patch
{
private static bool Prefix(Terminal __instance)
{
if (!modEnabled.Value)
{
return true;
}
string text = ((TMP_InputField)__instance.m_input).text;
if (text.ToLower().Equals(typeof(BepInExPlugin).Namespace.ToLower() + " reset"))
{
((BaseUnityPlugin)context).Config.Reload();
((BaseUnityPlugin)context).Config.Save();
Traverse.Create((object)__instance).Method("AddString", new object[1] { text }).GetValue();
Traverse.Create((object)__instance).Method("AddString", new object[1] { ((BaseUnityPlugin)context).Info.Metadata.Name + " config reloaded" }).GetValue();
return false;
}
return true;
}
}
private static readonly bool isDebug = true;
public static ConfigEntry<int> nexusID;
public static ConfigEntry<bool> modEnabled;
public static ConfigEntry<bool> unclaimedOnly;
public static ConfigEntry<bool> allowDaySleep;
public static ConfigEntry<string> modKey;
private static BepInExPlugin context;
public static void Dbgl(string str = "", bool pref = true)
{
if (isDebug)
{
Debug.Log((object)((pref ? (typeof(BepInExPlugin).Namespace + " ") : "") + str));
}
}
private void Awake()
{
context = this;
modEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "Enable this mod");
unclaimedOnly = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "UnclaimedOnly", false, "Only sleep on unclaimed beds");
allowDaySleep = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "AllowDaySleep", false, "Allow sleeping during the day");
modKey = ((BaseUnityPlugin)this).Config.Bind<string>("General", "ModKey", "left alt", "Modifier key to sleep without setting spawn point. Use https://docs.unity3d.com/Manual/ConventionalGameInput.html");
nexusID = ((BaseUnityPlugin)this).Config.Bind<int>("General", "NexusID", 261, "Nexus mod ID for updates");
if (modEnabled.Value)
{
Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly(), (string)null);
}
}
}