Decompiled source of Sleepover v1.1.2

Sleepover.dll

Decompiled a month ago
using System;
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;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using JetBrains.Annotations;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("Sleepover")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Azumatt")]
[assembly: AssemblyProduct("Sleepover")]
[assembly: AssemblyCopyright("Copyright ©  2021")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("4358610B-F3F4-4843-B7AF-98B7BC60DCDE")]
[assembly: AssemblyFileVersion("1.1.2")]
[assembly: TargetFramework(".NETFramework,Version=v4.6.2", FrameworkDisplayName = ".NET Framework 4.6.2")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.1.2.0")]
[module: UnverifiableCode]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableAttribute : Attribute
	{
		public readonly byte[] NullableFlags;

		public NullableAttribute(byte P_0)
		{
			NullableFlags = new byte[1] { P_0 };
		}

		public NullableAttribute(byte[] P_0)
		{
			NullableFlags = P_0;
		}
	}
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableContextAttribute : Attribute
	{
		public readonly byte Flag;

		public NullableContextAttribute(byte P_0)
		{
			Flag = P_0;
		}
	}
}
namespace Sleepover
{
	[HarmonyPatch(typeof(Game), "UpdateSleeping")]
	internal static class GameUpdateSleepingPatch
	{
		private static bool Prefix(ref Game __instance)
		{
			if (SleepoverPlugin.ModEnabled.Value == SleepoverPlugin.Toggle.Off)
			{
				return true;
			}
			if (SleepoverPlugin.SleepAnyTime.Value == SleepoverPlugin.Toggle.Off)
			{
				return true;
			}
			if (!ZNet.instance.IsServer())
			{
				return false;
			}
			if (__instance.m_sleeping)
			{
				if (!EnvMan.instance.IsTimeSkipping())
				{
					__instance.m_sleeping = false;
					ZRoutedRpc.instance.InvokeRoutedRPC(ZRoutedRpc.Everybody, "SleepStop", Array.Empty<object>());
					return false;
				}
			}
			else if (!EnvMan.instance.IsTimeSkipping() && (EnvMan.instance.IsAfternoon() || EnvMan.instance.IsNight() || SleepoverPlugin.SleepAnyTime.Value == SleepoverPlugin.Toggle.On) && __instance.EverybodyIsTryingToSleep())
			{
				EnvMan.instance.SkipToMorning();
				__instance.m_sleeping = true;
				ZRoutedRpc.instance.InvokeRoutedRPC(ZRoutedRpc.Everybody, "SleepStart", Array.Empty<object>());
			}
			return false;
		}
	}
	[HarmonyPatch(typeof(Bed), "GetHoverText")]
	internal static class BedGetHoverTextPatch
	{
		private static bool Prefix(Bed __instance, ref string __result)
		{
			if (SleepoverPlugin.ModEnabled.Value == SleepoverPlugin.Toggle.Off)
			{
				return true;
			}
			string text = "[<color=yellow><b>$KEY_Use</b></color>] $piece_bed_sleep\n";
			string text2 = "[<color=yellow><b>shift + $KEY_Use</b></color>] $piece_bed_claim\n";
			string text3 = "[<color=yellow><b>alt + $KEY_Use</b></color>] $piece_bed_setspawn\n";
			string ownerName = __instance.GetOwnerName();
			string text4 = ownerName + "'s $piece_bed\n";
			bool num = (__instance.IsMine() && __instance.IsCurrent()) || (!__instance.IsMine() && ownerName != "" && SleepoverPlugin.EnableMultipleBedfellows.Value == SleepoverPlugin.Toggle.On) || (ownerName == "" && SleepoverPlugin.SleepWithoutClaiming.Value == SleepoverPlugin.Toggle.On) || (!__instance.IsCurrent() && SleepoverPlugin.SleepWithoutSpawnpoint.Value == SleepoverPlugin.Toggle.On);
			bool flag = ownerName == "";
			bool flag2 = (!__instance.IsCurrent() && __instance.IsMine()) || (!__instance.IsCurrent() && !__instance.IsMine() && SleepoverPlugin.MultipleSpawnpointsPerBed.Value == SleepoverPlugin.Toggle.On);
			__result = ((ownerName != "") ? text4 : "$piece_bed_unclaimed\n");
			if (num)
			{
				__result += text;
			}
			if (flag)
			{
				__result += text2;
			}
			if (flag2)
			{
				__result += text3;
			}
			__result = Localization.instance.Localize(__result);
			return false;
		}
	}
	[HarmonyPatch(typeof(Bed), "Interact")]
	internal static class BedInteractPatch
	{
		[HarmonyPriority(700)]
		private static bool Prefix(Bed __instance, ref bool __result, ref Humanoid human, ref bool repeat)
		{
			//IL_016b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0225: Unknown result type (might be due to invalid IL or missing references)
			if (SleepoverPlugin.ModEnabled.Value == SleepoverPlugin.Toggle.Off)
			{
				return true;
			}
			if (repeat)
			{
				return false;
			}
			Humanoid obj = human;
			Player val = (Player)(object)((obj is Player) ? obj : null);
			long playerID = Game.instance.GetPlayerProfile().GetPlayerID();
			bool num = Input.GetKey((KeyCode)304) || Input.GetKey((KeyCode)303);
			bool flag = Input.GetKey((KeyCode)308) || Input.GetKey((KeyCode)307);
			bool flag2 = !num && !flag;
			string ownerName = __instance.GetOwnerName();
			bool flag3 = (__instance.IsMine() && __instance.IsCurrent()) || (!__instance.IsMine() && ownerName != "" && SleepoverPlugin.EnableMultipleBedfellows.Value == SleepoverPlugin.Toggle.On) || (ownerName == "" && SleepoverPlugin.SleepWithoutClaiming.Value == SleepoverPlugin.Toggle.On) || (!__instance.IsCurrent() && SleepoverPlugin.SleepWithoutSpawnpoint.Value == SleepoverPlugin.Toggle.On);
			bool flag4 = ownerName == "";
			bool flag5 = (!__instance.IsCurrent() && __instance.IsMine()) || (!__instance.IsCurrent() && !__instance.IsMine() && SleepoverPlugin.MultipleSpawnpointsPerBed.Value == SleepoverPlugin.Toggle.On);
			if (num && flag4)
			{
				if (!__instance.CheckExposure(val))
				{
					__result = false;
					return false;
				}
				__instance.SetOwner(playerID, Game.instance.GetPlayerProfile().GetName());
			}
			if (flag && flag5)
			{
				if (!__instance.CheckExposure(val))
				{
					__result = false;
					return false;
				}
				Game.instance.GetPlayerProfile().SetCustomSpawnPoint(__instance.GetSpawnPoint());
				((Character)human).Message((MessageType)2, "$msg_spawnpointset", 0, (Sprite)null);
				return false;
			}
			if (flag2 && flag3)
			{
				if (SleepoverPlugin.SleepAnyTime.Value == SleepoverPlugin.Toggle.Off && !EnvMan.instance.IsAfternoon() && !EnvMan.instance.IsNight())
				{
					((Character)human).Message((MessageType)2, "$msg_cantsleep", 0, (Sprite)null);
					__result = false;
					return false;
				}
				if (!__instance.CheckEnemies(val))
				{
					__result = false;
					return false;
				}
				if (!__instance.CheckExposure(val))
				{
					__result = false;
					return false;
				}
				if (!__instance.CheckFire(val))
				{
					__result = false;
					return false;
				}
				if (!__instance.CheckWet(val))
				{
					__result = false;
					return false;
				}
				((Character)human).AttachStart(__instance.m_spawnPoint, ((Component)human).gameObject, true, true, false, "attach_bed", new Vector3(0f, 0.5f, 0f), (Transform)null);
			}
			__result = false;
			return false;
		}
	}
	[HarmonyPatch(typeof(Bed), "IsCurrent")]
	internal static class BedIsCurrentPatch
	{
		private static bool Prefix(Bed __instance, ref bool __result)
		{
			//IL_001b: Unknown result type (might be due to invalid IL or missing references)
			//IL_002a: Unknown result type (might be due to invalid IL or missing references)
			if (!__instance.IsMine() && SleepoverPlugin.MultipleSpawnpointsPerBed.Value == SleepoverPlugin.Toggle.Off)
			{
				__result = false;
				return false;
			}
			__result = Vector3.Distance(__instance.GetSpawnPoint(), Game.instance.GetPlayerProfile().GetCustomSpawnPoint()) < 1f;
			return false;
		}
	}
	[HarmonyPatch(typeof(Bed), "CheckExposure")]
	internal static class BedCheckExposurePatch
	{
		private static bool Prefix(ref bool __result)
		{
			if (SleepoverPlugin.IgnoreExposure.Value == SleepoverPlugin.Toggle.On)
			{
				__result = true;
				return false;
			}
			return true;
		}
	}
	[HarmonyPatch(typeof(Bed), "CheckEnemies")]
	internal static class BedCheckEnemiesPatch
	{
		private static bool Prefix(ref bool __result)
		{
			if (SleepoverPlugin.IgnoreEnemies.Value == SleepoverPlugin.Toggle.On)
			{
				__result = true;
				return false;
			}
			return true;
		}
	}
	[HarmonyPatch(typeof(Bed), "CheckFire")]
	internal static class BedCheckFirePatch
	{
		private static bool Prefix(ref bool __result)
		{
			if (SleepoverPlugin.IgnoreFire.Value == SleepoverPlugin.Toggle.On)
			{
				__result = true;
				return false;
			}
			return true;
		}
	}
	[HarmonyPatch(typeof(Bed), "CheckWet")]
	internal static class BedCheckWetPatch
	{
		private static bool Prefix(ref bool __result)
		{
			if (SleepoverPlugin.IgnoreWet.Value == SleepoverPlugin.Toggle.On)
			{
				__result = true;
				return false;
			}
			return true;
		}
	}
	[BepInPlugin("Azumatt.Sleepover", "Sleepover", "1.1.2")]
	public class SleepoverPlugin : BaseUnityPlugin
	{
		public enum Toggle
		{
			On = 1,
			Off = 0
		}

		private class ConfigurationManagerAttributes
		{
			[UsedImplicitly]
			public int? Order;

			[UsedImplicitly]
			public bool? Browsable;

			[UsedImplicitly]
			public string? Category;

			[UsedImplicitly]
			public Action<ConfigEntryBase>? CustomDrawer;
		}

		private class AcceptableShortcuts : AcceptableValueBase
		{
			public AcceptableShortcuts()
				: base(typeof(KeyboardShortcut))
			{
			}

			public override object Clamp(object value)
			{
				return value;
			}

			public override bool IsValid(object value)
			{
				return true;
			}

			public override string ToDescriptionString()
			{
				return "# Acceptable values: " + string.Join(", ", UnityInput.Current.SupportedKeyCodes);
			}
		}

		internal const string ModName = "Sleepover";

		internal const string ModVersion = "1.1.2";

		internal const string Author = "Azumatt";

		private const string ModGUID = "Azumatt.Sleepover";

		private static string ConfigFileName = "Azumatt.Sleepover.cfg";

		private static string ConfigFileFullPath;

		internal static string ConnectionError;

		private readonly Harmony _harmony = new Harmony("Azumatt.Sleepover");

		public static readonly ManualLogSource SleepoverLogger;

		public static ConfigEntry<Toggle> ModEnabled;

		public static ConfigEntry<Toggle> EnableMultipleBedfellows;

		public static ConfigEntry<Toggle> SleepAnyTime;

		public static ConfigEntry<Toggle> IgnoreExposure;

		public static ConfigEntry<Toggle> IgnoreEnemies;

		public static ConfigEntry<Toggle> IgnoreFire;

		public static ConfigEntry<Toggle> IgnoreWet;

		public static ConfigEntry<Toggle> SleepWithoutSpawnpoint;

		public static ConfigEntry<Toggle> MultipleSpawnpointsPerBed;

		public static ConfigEntry<Toggle> SleepWithoutClaiming;

		public void Awake()
		{
			ModEnabled = config("1 - General", "Enabled", Toggle.On, "Enable this mod");
			EnableMultipleBedfellows = config("1 - General", "Multiple sleepers", Toggle.On, "Allow multiple people to use this bed simultaneously. Not tested on public servers.");
			SleepAnyTime = config("1 - General", "Ignore time restrictions", Toggle.On, "Sleep at any time of day, not just at night.");
			IgnoreExposure = config("1 - General", "Ignore exposure restrictions", Toggle.On, "Ignore restrictions for walls and a roof. Sleep under a starry sky.");
			IgnoreEnemies = config("1 - General", "Ignore nearby enemies", Toggle.On, "Enemies no longer prevent you from sleeping.");
			IgnoreFire = config("1 - General", "Ignore fire requirement", Toggle.On, "Sleep without a nearby fire.");
			IgnoreWet = config("1 - General", "Ignore wet restrictions", Toggle.On, "Sleep while wet.");
			SleepWithoutClaiming = config("1 - General", "Do not automatically claim beds", Toggle.On, "Sleep without claiming a bed first.");
			SleepWithoutSpawnpoint = config("1 - General", "Do not set spawnpoint", Toggle.On, "Sleep without setting a spawnpoint first.");
			MultipleSpawnpointsPerBed = config("1 - General", "Multiple spawnpoints per bed", Toggle.On, "Any number of players can use the same bed as a spawnpoint.");
			if (ModEnabled.Value != 0)
			{
				Assembly executingAssembly = Assembly.GetExecutingAssembly();
				_harmony.PatchAll(executingAssembly);
				SetupWatcher();
			}
		}

		private void OnDestroy()
		{
			((BaseUnityPlugin)this).Config.Save();
		}

		private void SetupWatcher()
		{
			FileSystemWatcher fileSystemWatcher = new FileSystemWatcher(Paths.ConfigPath, ConfigFileName);
			fileSystemWatcher.Changed += ReadConfigValues;
			fileSystemWatcher.Created += ReadConfigValues;
			fileSystemWatcher.Renamed += ReadConfigValues;
			fileSystemWatcher.IncludeSubdirectories = true;
			fileSystemWatcher.SynchronizingObject = ThreadingHelper.SynchronizingObject;
			fileSystemWatcher.EnableRaisingEvents = true;
		}

		private void ReadConfigValues(object sender, FileSystemEventArgs e)
		{
			if (!File.Exists(ConfigFileFullPath))
			{
				return;
			}
			try
			{
				SleepoverLogger.LogDebug((object)"ReadConfigValues called");
				((BaseUnityPlugin)this).Config.Reload();
			}
			catch
			{
				SleepoverLogger.LogError((object)("There was an issue loading your " + ConfigFileName));
				SleepoverLogger.LogError((object)"Please check your config entries for spelling and format!");
			}
		}

		private ConfigEntry<T> config<T>(string group, string name, T value, ConfigDescription description)
		{
			return ((BaseUnityPlugin)this).Config.Bind<T>(group, name, value, description);
		}

		private ConfigEntry<T> config<T>(string group, string name, T value, string description)
		{
			//IL_000c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0016: Expected O, but got Unknown
			return config(group, name, value, new ConfigDescription(description, (AcceptableValueBase)null, Array.Empty<object>()));
		}

		static SleepoverPlugin()
		{
			string configPath = Paths.ConfigPath;
			char directorySeparatorChar = Path.DirectorySeparatorChar;
			ConfigFileFullPath = configPath + directorySeparatorChar + ConfigFileName;
			ConnectionError = "";
			SleepoverLogger = Logger.CreateLogSource("Sleepover");
			ModEnabled = null;
			EnableMultipleBedfellows = null;
			SleepAnyTime = null;
			IgnoreExposure = null;
			IgnoreEnemies = null;
			IgnoreFire = null;
			IgnoreWet = null;
			SleepWithoutSpawnpoint = null;
			MultipleSpawnpointsPerBed = null;
			SleepWithoutClaiming = null;
		}
	}
	public static class KeyboardExtensions
	{
		public static bool IsKeyDown(this KeyboardShortcut shortcut)
		{
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_000b: Unknown result type (might be due to invalid IL or missing references)
			if ((int)((KeyboardShortcut)(ref shortcut)).MainKey != 0 && Input.GetKeyDown(((KeyboardShortcut)(ref shortcut)).MainKey))
			{
				return ((KeyboardShortcut)(ref shortcut)).Modifiers.All((Func<KeyCode, bool>)Input.GetKey);
			}
			return false;
		}

		public static bool IsKeyHeld(this KeyboardShortcut shortcut)
		{
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_000b: Unknown result type (might be due to invalid IL or missing references)
			if ((int)((KeyboardShortcut)(ref shortcut)).MainKey != 0 && Input.GetKey(((KeyboardShortcut)(ref shortcut)).MainKey))
			{
				return ((KeyboardShortcut)(ref shortcut)).Modifiers.All((Func<KeyCode, bool>)Input.GetKey);
			}
			return false;
		}
	}
}