Decompiled source of PEAKSleepTalk v0.3.1

plugins/com.github.lokno.PEAKSleepTalk.dll

Decompiled a week ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: IgnoresAccessChecksTo("Assembly-CSharp")]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("com.github.lokno.PEAKSleepTalk")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("0.3.1.0")]
[assembly: AssemblyInformationalVersion("0.3.1+923eafdb4bc96f3e779d371abd7cfbe79094aecb")]
[assembly: AssemblyProduct("com.github.lokno.PEAKSleepTalk")]
[assembly: AssemblyTitle("PEAKSleepTalk")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.3.1.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
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;
		}
	}
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.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 BepInEx
{
	[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
	[Conditional("CodeGeneration")]
	internal sealed class BepInAutoPluginAttribute : Attribute
	{
		public BepInAutoPluginAttribute(string? id = null, string? name = null, string? version = null)
		{
		}
	}
}
namespace BepInEx.Preloader.Core.Patching
{
	[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
	[Conditional("CodeGeneration")]
	internal sealed class PatcherAutoPluginAttribute : Attribute
	{
		public PatcherAutoPluginAttribute(string? id = null, string? name = null, string? version = null)
		{
		}
	}
}
namespace PEAKSleepTalk
{
	public class ConfigurationManager
	{
		private static ConfigEntry<bool> _configEnableReduceVolume;

		private static ConfigEntry<float> _configReductionFactor;

		private static ConfigEntry<bool> _configEnableQuietTime;

		private static ConfigEntry<float> _configQuietTime;

		private static ConfigEntry<bool> _configAllowSpectate;

		public static bool ReduceVolume => _configEnableReduceVolume.Value;

		public static float VolumeReductionFactor => Math.Clamp(_configReductionFactor.Value, 0f, 1f);

		public static bool EnableQuietTime => _configEnableQuietTime.Value;

		public static float QuietTimeDuration => _configQuietTime.Value;

		public static bool AllowSpectate => _configAllowSpectate.Value;

		public static void LoadConfig(BaseUnityPlugin plugin)
		{
			_configEnableReduceVolume = plugin.Config.Bind<bool>("General", "ReduceVolume", true, "Enable volume reduction of unconscious players");
			_configReductionFactor = plugin.Config.Bind<float>("General", "VolumeReductionFactor", 0.3f, "The factor by which the volume is reduced for unconscious players (0.0 to 1.0)");
			_configEnableQuietTime = plugin.Config.Bind<bool>("General", "QuietTime", true, "Enable a quiet time before unconscious players can speak again");
			_configQuietTime = plugin.Config.Bind<float>("General", "QuietTimeDuration", 8f, "The time in seconds before unconscious players can speak again");
			_configAllowSpectate = plugin.Config.Bind<bool>("General", "AllowSpectate", true, "Allow unconscious players to spectate others");
		}
	}
	internal class EnableSleepTalkPatch
	{
		public static class PlayerConsciousnessManager
		{
			public class ConsciousState
			{
				public bool passedOut;

				public bool fullyPassedOut;

				public float startTime;
			}

			private static Dictionary<Character, ConsciousState> states = new Dictionary<Character, ConsciousState>();

			public static ConsciousState Create(Character character)
			{
				return new ConsciousState
				{
					passedOut = character.data.passedOut,
					fullyPassedOut = character.data.fullyPassedOut,
					startTime = Time.time
				};
			}

			public static ConsciousState UpdateAndGet(Character character)
			{
				if (states.TryGetValue(character, out ConsciousState value))
				{
					if (value.passedOut != character.data.passedOut || value.fullyPassedOut != character.data.fullyPassedOut)
					{
						value.passedOut = character.data.passedOut;
						value.fullyPassedOut = character.data.fullyPassedOut;
						value.startTime = Time.time;
					}
					return value;
				}
				ConsciousState consciousState = Create(character);
				states.Add(character, consciousState);
				return consciousState;
			}
		}

		[HarmonyPatch(typeof(AnimatedMouth))]
		public class AnimatedMouthPatch
		{
			[HarmonyPatch(typeof(AnimatedMouth), "ProcessMicData")]
			private static void Prefix(AnimatedMouth __instance, out bool __state)
			{
				PlayerConsciousnessManager.ConsciousState consciousState = PlayerConsciousnessManager.UpdateAndGet(__instance.character);
				bool flag = !ConfigurationManager.EnableQuietTime || Time.time - consciousState.startTime >= ConfigurationManager.QuietTimeDuration;
				__state = __instance.character.data.passedOut;
				if (flag && !__instance.character.data.dead && (__instance.character.data.passedOut || __instance.character.data.fullyPassedOut))
				{
					__instance.character.data.passedOut = false;
				}
			}

			[HarmonyPatch(typeof(AnimatedMouth), "ProcessMicData")]
			private static void Postfix(AnimatedMouth __instance, bool __state)
			{
				__instance.character.data.passedOut = __state;
			}
		}

		[HarmonyPatch(typeof(CharacterVoiceHandler))]
		public class CharacterVoicePatch
		{
			public class StoreData
			{
				public PlayerConsciousnessManager.ConsciousState state;

				public float audioLevel = 0.5f;
			}

			[HarmonyPatch(typeof(CharacterVoiceHandler), "Update")]
			private static void Prefix(CharacterVoiceHandler __instance, out StoreData __state)
			{
				Character character = __instance.m_character;
				float audioLevel = __instance.audioLevel;
				__state = new StoreData
				{
					state = PlayerConsciousnessManager.UpdateAndGet(character),
					audioLevel = audioLevel
				};
				if ((!ConfigurationManager.EnableQuietTime || Time.time - __state.state.startTime >= ConfigurationManager.QuietTimeDuration) && !character.data.dead && (character.data.passedOut || character.data.fullyPassedOut))
				{
					character.data.passedOut = false;
					character.data.fullyPassedOut = false;
					if (ConfigurationManager.ReduceVolume)
					{
						__instance.audioLevel = audioLevel * ConfigurationManager.VolumeReductionFactor;
					}
				}
			}

			[HarmonyPatch(typeof(CharacterVoiceHandler), "Update")]
			private static void Postfix(CharacterVoiceHandler __instance, StoreData __state)
			{
				__instance.m_character.data.passedOut = __state.state.passedOut;
				__instance.m_character.data.fullyPassedOut = __state.state.fullyPassedOut;
				__instance.audioLevel = __state.audioLevel;
			}
		}

		[HarmonyPatch(typeof(MainCameraMovement))]
		public class PassedOutSpectatePatch
		{
			[HarmonyPatch("HandleSpecSelection")]
			private static void Prefix(MainCameraMovement __instance, out float __state)
			{
				__state = __instance.sinceSwitch;
				if (Object.op_Implicit((Object)(object)Character.localCharacter) && !Character.localCharacter.data.dead && !ConfigurationManager.AllowSpectate)
				{
					__instance.sinceSwitch = 0f;
					MainCameraMovement.specCharacter = Character.localCharacter;
				}
			}

			[HarmonyPatch("HandleSpecSelection")]
			private static void Postfix(MainCameraMovement __instance, float __state)
			{
				__instance.sinceSwitch = __state;
			}
		}
	}
	[BepInPlugin("com.github.lokno.PEAKSleepTalk", "PEAKSleepTalk", "0.3.1")]
	public class Plugin : BaseUnityPlugin
	{
		public const string Id = "com.github.lokno.PEAKSleepTalk";

		internal static ManualLogSource Log { get; private set; }

		public static string Name => "PEAKSleepTalk";

		public static string Version => "0.3.1";

		private void Awake()
		{
			//IL_0034: Unknown result type (might be due to invalid IL or missing references)
			//IL_003a: Expected O, but got Unknown
			Log = ((BaseUnityPlugin)this).Logger;
			Log.LogInfo((object)("Plugin " + Name + " is loaded: Allow passed out scouts to speak"));
			ConfigurationManager.LoadConfig((BaseUnityPlugin)(object)this);
			Harmony val = new Harmony("com.github.lokno.PEAKSleepTalk");
			val.PatchAll();
		}
	}
}
namespace System.Runtime.CompilerServices
{
	[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
	internal sealed class IgnoresAccessChecksToAttribute : Attribute
	{
		public IgnoresAccessChecksToAttribute(string assemblyName)
		{
		}
	}
}