Decompiled source of ServersideQoL JustSleep BETA v1.99.8

ServersideQoL.JustSleep.dll

Decompiled 11 hours ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using Microsoft.CodeAnalysis;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("ArgusMagnus")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyCopyright("ArgusMagnus")]
[assembly: AssemblyDescription("Allow players to vote to sleep and skip the night when at least one player is in bed")]
[assembly: AssemblyFileVersion("1.99.8.0")]
[assembly: AssemblyInformationalVersion("1.99.8-beta+328851184cc278c858ffbc70e717af9f9d7b6973")]
[assembly: AssemblyProduct("ServersideQoL.JustSleep")]
[assembly: AssemblyTitle("ServersideQoL.JustSleep")]
[assembly: AssemblyMetadata("RepositoryUrl", "https://github.com/ArgusMagnus/ValheimServersideQoL")]
[assembly: AssemblyVersion("1.99.8.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[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]
	[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;
		}
	}
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace ServersideQoL.JustSleep
{
	public sealed class Config : ConfigBase<Config>
	{
		public sealed class LocalizationConfig
		{
			private string Prompt { get; init; } = "{0} of {1} players want to sleep.<br>Sit down if you want to sleep as well";

			public string FormatPrompt(int sleepingPlayers, int totalPlayers)
			{
				return string.Format(Prompt, sleepingPlayers, totalPlayers);
			}
		}

		private const string Section = "JustSleep";

		public override ConfigEntry<bool> Enabled { get; } = ConfigBase<Config>.BindEx<bool>(cfg, "JustSleep", true, "Enables/disables the entire mod", (AcceptableValueBase)null, (Deprecated<Config>)null, "Enabled");

		public ConfigEntry<int> MinPlayersInBed { get; } = ConfigBase<Config>.BindEx<int>(cfg, "JustSleep", 1, "Minimum number of players in bed to show the sleep prompt to the other players", (AcceptableValueBase)(object)new AcceptableValueRange<int>(1, 10), (Deprecated<Config>)null, "MinPlayersInBed");

		public ConfigEntry<int> RequiredPlayerPercentage { get; } = ConfigBase<Config>.BindEx<int>(cfg, "JustSleep", 100, "Percentage of players that must be in bed or sitting to skip the night", (AcceptableValueBase)(object)new AcceptableValueRange<int>(0, 100), (Deprecated<Config>)null, "RequiredPlayerPercentage");

		public ConfigEntry<MessageType> SleepPromptMessageType { get; } = ConfigBase<Config>.BindEx<MessageType>(cfg, "JustSleep", (MessageType)2, "Type of message to show for the sleep prompt", (AcceptableValueBase)(object)AcceptableEnum<MessageType>.Default, (Deprecated<Config>)null, "SleepPromptMessageType");

		public YamlConfigEntry<LocalizationConfig> Localization { get; } = ConfigBase<Config>.BindYaml<LocalizationConfig>(cfg, "Localization");

		public Config(ConfigFile cfg, Logger logger)
			: base(cfg, logger)
		{
		}
	}
	[BepInPlugin("ArgusMagnus.ServersideQoL.JustSleep", "ServersideQoL.JustSleep", "1.99.8")]
	public sealed class JustSleepPlugin : ServersideQoLPluginBase<JustSleepPlugin, Config>
	{
		public const string Author = "ArgusMagnus";

		public const string PluginName = "ServersideQoL.JustSleep";

		public const string PluginGuid = "ArgusMagnus.ServersideQoL.JustSleep";

		public const string PluginVersion = "1.99.8";

		public const string PluginInformationalVersion = "1.99.8-beta";

		internal static Harmony HarmonyInstance { get; } = new Harmony("ArgusMagnus.ServersideQoL.JustSleep");

		private DateTimeOffset BuildTimestamp { get; } = new DateTimeOffset(639215507745838134L, default(TimeSpan));

		protected override Config CreateConfigSingleton(ConfigFile configFile, Logger logger)
		{
			return new Config(configFile, logger);
		}

		protected override void RegisterProcessors(IProcessorCollection processors)
		{
			processors.Add<PlayerProcessor>();
		}

		private void Awake()
		{
			ServersideQoLPluginBase<JustSleepPlugin, Config>.Logger.LogWarning((object)string.Format("You are running a pre-release version: {0} ({1})", "1.99.8-beta", BuildTimestamp.LocalDateTime));
		}
	}
	[Processor("500d524e-2dbc-493d-8231-c8e9f03158bf")]
	[DependsOn<PlayerRegistryProcessor>]
	public sealed class PlayerProcessor : Processor<ProcessorPrefabInfo<Player>>
	{
		[HarmonyPatch(typeof(Game), "EverybodyIsTryingToSleep")]
		private static class EverybodyIsTryingToSleepPatch
		{
			[HarmonyPrefix]
			public static bool Prefix(ref bool __result)
			{
				__result = EverybodyIsTryingToSleep();
				return false;
			}
		}

		private bool _patched;

		protected override void Initialize()
		{
			if (!_patched)
			{
				_patched = true;
				JustSleepPlugin.HarmonyInstance.PatchAll(typeof(EverybodyIsTryingToSleepPatch));
			}
		}

		protected override ProcessResult Process(ServersideQoLZDO zdo, IReadOnlyList<Peer> peers, ProcessorPrefabInfo<Player> prefabInfo)
		{
			return (ProcessResult)2;
		}

		private static bool EverybodyIsTryingToSleep()
		{
			//IL_0037: Unknown result type (might be due to invalid IL or missing references)
			//IL_003c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0055: Unknown result type (might be due to invalid IL or missing references)
			//IL_005a: Unknown result type (might be due to invalid IL or missing references)
			//IL_005f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0065: Invalid comparison between Unknown and I4
			//IL_00d2: Unknown result type (might be due to invalid IL or missing references)
			IReadOnlyCollection<PlayerState> playerStates = Processor.Instance<PlayerRegistryProcessor>().PlayerStates;
			if (playerStates == null || playerStates.Count <= 0)
			{
				return false;
			}
			int num = 0;
			int num2 = 0;
			foreach (PlayerState item in playerStates)
			{
				ZDOVars vars = item.ZDO.Vars;
				if (((ZDOVars)(ref vars)).GetInBed(false))
				{
					num++;
					continue;
				}
				vars = item.ZDO.Vars;
				if ((int)((ZDOVars)(ref vars)).GetEmote((Emotes)(-1)) == 1)
				{
					num2++;
				}
			}
			if (num == playerStates.Count)
			{
				return true;
			}
			if (num < ConfigBase<Config>.Instance.MinPlayersInBed.Value)
			{
				return false;
			}
			int num3 = num + num2;
			if (num3 * 100 / playerStates.Count >= ConfigBase<Config>.Instance.RequiredPlayerPercentage.Value)
			{
				return true;
			}
			RPC.ShowMessage(ZRoutedRpc.Everybody, ConfigBase<Config>.Instance.SleepPromptMessageType.Value, ConfigBase<Config>.Instance.Localization.Value.FormatPrompt(num3, playerStates.Count));
			return false;
		}
	}
}