Decompiled source of Silence v0.3.7

BepInEx/plugins/Silence/Silence.dll

Decompiled 2 weeks ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using Dissonance;
using Dissonance.Audio.Capture;
using Dissonance.VAD;
using DunGen;
using GameNetcodeStuff;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using NAudio.Wave;
using Unity.Collections;
using Unity.Netcode;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.Controls;
using UnityEngine.Networking;
using UnityEngine.SceneManagement;

[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("Silence")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("0.3.7.0")]
[assembly: AssemblyInformationalVersion("0.3.7")]
[assembly: AssemblyProduct("Silence")]
[assembly: AssemblyTitle("Silence")]
[assembly: AssemblyVersion("0.3.7.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[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 Silence
{
	internal sealed class Blackout
	{
		private sealed class SavedLight
		{
			internal int Mask;

			internal float Intensity;
		}

		private readonly Dictionary<Light, SavedLight> masks = new Dictionary<Light, SavedLight>();

		private readonly HashSet<Light> candidates = new HashSet<Light>();

		private readonly List<Light> extra = new List<Light>();

		private Dungeon dungeon;

		private bool collected;

		private float nextCollection;

		internal int Count => masks.Count;

		internal int Collected => candidates.Count;

		internal int Excluded { get; private set; }

		internal int Changed { get; private set; }

		internal int Unsupported { get; private set; }

		internal int Restored { get; private set; }

		internal bool Ready { get; private set; }

		internal string Interior
		{
			get
			{
				if (!Object.op_Implicit((Object)(object)dungeon))
				{
					return "not ready";
				}
				return ((Object)dungeon).name;
			}
		}

		private static bool Exclude(Light light)
		{
			//IL_0009: Unknown result type (might be due to invalid IL or missing references)
			//IL_000f: Invalid comparison between Unknown and I4
			if (Object.op_Implicit((Object)(object)light) && (int)light.type != 1 && !Object.op_Implicit((Object)(object)((Component)light).GetComponentInParent<GrabbableObject>()) && !Object.op_Implicit((Object)(object)((Component)light).GetComponentInParent<PlayerControllerB>()) && !Object.op_Implicit((Object)(object)((Component)light).GetComponentInParent<EnemyAI>()) && !Object.op_Implicit((Object)(object)((Component)light).GetComponentInParent<Canvas>()))
			{
				return Object.op_Implicit((Object)(object)((Component)light).GetComponentInParent<ParticleSystem>());
			}
			return true;
		}

		private void Collect(Dungeon current, RoundManager round)
		{
			dungeon = current;
			foreach (Tile allTile in current.AllTiles)
			{
				if (!Object.op_Implicit((Object)(object)allTile))
				{
					continue;
				}
				extra.Clear();
				((Component)allTile).GetComponentsInChildren<Light>(true, extra);
				foreach (Light item in extra)
				{
					candidates.Add(item);
				}
			}
			List<Light> allPoweredLights = round.allPoweredLights;
			if (allPoweredLights != null)
			{
				foreach (Light item2 in allPoweredLights)
				{
					if (Object.op_Implicit((Object)(object)item2) && ((Component)item2).transform.IsChildOf(((Component)current).transform))
					{
						candidates.Add(item2);
					}
				}
			}
			List<Animator> allPoweredLightsAnimators = round.allPoweredLightsAnimators;
			if (allPoweredLightsAnimators != null)
			{
				foreach (Animator item3 in allPoweredLightsAnimators)
				{
					if (!Object.op_Implicit((Object)(object)item3) || !((Component)item3).transform.IsChildOf(((Component)current).transform))
					{
						continue;
					}
					extra.Clear();
					((Component)item3).GetComponentsInChildren<Light>(true, extra);
					foreach (Light item4 in extra)
					{
						candidates.Add(item4);
					}
				}
			}
			extra.Clear();
			collected = true;
		}

		internal void Apply()
		{
			//IL_0157: Unknown result type (might be due to invalid IL or missing references)
			//IL_015c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0162: Invalid comparison between Unknown and I4
			//IL_0064: Unknown result type (might be due to invalid IL or missing references)
			//IL_0069: Unknown result type (might be due to invalid IL or missing references)
			Ready = false;
			RoundManager instance = RoundManager.Instance;
			DungeonGenerator val = ((Object.op_Implicit((Object)(object)instance) && Object.op_Implicit((Object)(object)instance.dungeonGenerator)) ? instance.dungeonGenerator.Generator : null);
			Dungeon val2 = ((val != null) ? val.CurrentDungeon : null);
			if (Object.op_Implicit((Object)(object)instance) && !instance.dungeonIsGenerating && instance.dungeonCompletedGenerating && Object.op_Implicit((Object)(object)val2))
			{
				Scene scene = ((Component)val2).gameObject.scene;
				if (((Scene)(ref scene)).isLoaded)
				{
					if ((Object)(object)dungeon != (Object)(object)val2)
					{
						Restore();
					}
					if (!collected)
					{
						if (Time.unscaledTime < nextCollection)
						{
							return;
						}
						nextCollection = Time.unscaledTime + 1f;
						if (val2.AllTiles.Count == 0)
						{
							return;
						}
						try
						{
							Collect(val2, instance);
						}
						catch (Exception)
						{
							Restore();
							nextCollection = Time.unscaledTime + 1f;
							Unsupported = 1;
							return;
						}
					}
					int num = 0;
					Excluded = 0;
					Unsupported = 0;
					Changed = 0;
					foreach (Light candidate in candidates)
					{
						if (Exclude(candidate))
						{
							Excluded++;
							Release(candidate);
							continue;
						}
						if ((int)candidate.bakingOutput.lightmapBakeType == 2)
						{
							Unsupported++;
							continue;
						}
						try
						{
							if (((Behaviour)candidate).enabled && ((Component)candidate).gameObject.activeInHierarchy && candidate.intensity != 0f && candidate.cullingMask != 0)
							{
								Changed++;
							}
							Hide(candidate);
							if (candidate.intensity == 0f && candidate.cullingMask == 0)
							{
								num++;
							}
						}
						catch (Exception)
						{
							Unsupported++;
						}
					}
					Ready = num > 0 && Unsupported == 0;
					return;
				}
			}
			if (collected)
			{
				Restore();
			}
		}

		private void Hide(Light light)
		{
			if (!masks.TryGetValue(light, out var value))
			{
				Dictionary<Light, SavedLight> dictionary = masks;
				SavedLight obj = new SavedLight
				{
					Mask = light.cullingMask,
					Intensity = light.intensity
				};
				value = obj;
				dictionary.Add(light, obj);
			}
			else
			{
				if (light.cullingMask != 0)
				{
					value.Mask = light.cullingMask;
				}
				if (light.intensity != 0f)
				{
					value.Intensity = light.intensity;
				}
			}
			light.cullingMask = 0;
			light.intensity = 0f;
		}

		private void Release(Light light)
		{
			if (light == null || !masks.TryGetValue(light, out var value))
			{
				return;
			}
			if (Object.op_Implicit((Object)(object)light))
			{
				if (light.cullingMask == 0)
				{
					light.cullingMask = value.Mask;
				}
				if (light.intensity == 0f)
				{
					light.intensity = value.Intensity;
				}
			}
			masks.Remove(light);
		}

		internal void Restore()
		{
			Restored = 0;
			foreach (KeyValuePair<Light, SavedLight> mask in masks)
			{
				if (Object.op_Implicit((Object)(object)mask.Key))
				{
					if (mask.Key.cullingMask == 0)
					{
						mask.Key.cullingMask = mask.Value.Mask;
					}
					if (mask.Key.intensity == 0f)
					{
						mask.Key.intensity = mask.Value.Intensity;
					}
					Restored++;
				}
			}
			masks.Clear();
			candidates.Clear();
			extra.Clear();
			dungeon = null;
			collected = false;
			Ready = false;
			nextCollection = 0f;
		}
	}
	internal sealed class ShhAudio
	{
		private readonly Plugin plugin;

		private readonly AudioSource source;

		private AudioClip clip;

		internal string Status { get; private set; } = "Not loaded";

		internal ShhAudio(Plugin owner)
		{
			plugin = owner;
			source = ((Component)owner).gameObject.AddComponent<AudioSource>();
			source.playOnAwake = false;
			source.loop = false;
			source.spatialBlend = 0f;
			source.panStereo = 0f;
			source.dopplerLevel = 0f;
			source.bypassReverbZones = true;
		}

		internal IEnumerator Load()
		{
			string path = null;
			try
			{
				string directoryName = Path.GetDirectoryName(((BaseUnityPlugin)plugin).Info.Location);
				string value = plugin.Options.SoundFile.Value;
				if (string.IsNullOrWhiteSpace(value) || Path.IsPathRooted(value))
				{
					throw new IOException("Use a relative audio filename.");
				}
				path = Path.GetFullPath(Path.Combine(directoryName, value));
				if (!path.StartsWith(Path.GetFullPath(directoryName) + Path.DirectorySeparatorChar, StringComparison.OrdinalIgnoreCase))
				{
					throw new IOException("Audio must be inside the plugin directory.");
				}
				if (!File.Exists(path))
				{
					throw new FileNotFoundException("Sound file missing", path);
				}
				if (new FileInfo(path).Length > 33554432)
				{
					throw new IOException("Sound exceeds 32 MiB.");
				}
				string text = Path.GetExtension(path).ToLowerInvariant();
				if (text != ".wav" && text != ".ogg")
				{
					throw new IOException("Use WAV or OGG.");
				}
			}
			catch (Exception ex)
			{
				Status = ex.Message;
				plugin.Warn("Sound skipped: " + Status);
			}
			if (path == null || Status != "Not loaded")
			{
				yield break;
			}
			Status = "Pending: creating audio request";
			UnityWebRequest request;
			try
			{
				request = UnityWebRequestMultimedia.GetAudioClip(new Uri(path).AbsoluteUri, (AudioType)(Path.GetExtension(path).Equals(".ogg", StringComparison.OrdinalIgnoreCase) ? 14 : 20));
			}
			catch (Exception ex2)
			{
				Status = "Failed at request creation: " + ex2.GetType().Name;
				plugin.Warn(Status);
				yield break;
			}
			UnityWebRequest val = request;
			try
			{
				UnityWebRequestAsyncOperation val2;
				try
				{
					val2 = request.SendWebRequest();
				}
				catch (Exception ex3)
				{
					Status = "Failed at request send: " + ex3.GetType().Name;
					plugin.Warn(Status);
					yield break;
				}
				Status = "Pending: reading/decoding audio";
				plugin.Log("Audio initialization: " + Status);
				yield return val2;
				try
				{
					if ((int)request.result != 1)
					{
						throw new IOException(request.error);
					}
					AudioClip content = DownloadHandlerAudioClip.GetContent(request);
					if (!Object.op_Implicit((Object)(object)content) || content.samples == 0 || content.length > 30f)
					{
						throw new IOException("Invalid sound or longer than 30 seconds.");
					}
					if (content.channels > 1)
					{
						float[] array = new float[content.samples * content.channels];
						if (!content.GetData(array, 0))
						{
							throw new IOException("Could not decode sound samples.");
						}
						float[] array2 = new float[content.samples];
						for (int i = 0; i < array2.Length; i++)
						{
							for (int j = 0; j < content.channels; j++)
							{
								array2[i] += array[i * content.channels + j] / (float)content.channels;
							}
						}
						clip = AudioClip.Create("Silence shh", array2.Length, 1, content.frequency, false);
						clip.SetData(array2, 0);
						Object.Destroy((Object)(object)content);
					}
					else
					{
						clip = content;
					}
					Status = "Ready: " + Path.GetFileName(path);
					plugin.Log(Status);
				}
				catch (Exception ex4)
				{
					Status = ex4.Message;
					plugin.Warn("Sound skipped: " + Status);
				}
			}
			finally
			{
				((IDisposable)val)?.Dispose();
			}
		}

		internal void Play()
		{
			if (Object.op_Implicit((Object)(object)clip) && plugin.Options.Sound.Value)
			{
				source.PlayOneShot(clip, Settings.Safe(plugin.Options.Volume.Value, 0.8f, 0f, 1f));
			}
		}

		internal void Stop()
		{
			source.Stop();
		}

		internal void Dispose()
		{
			source.Stop();
			if (Object.op_Implicit((Object)(object)clip))
			{
				Object.Destroy((Object)(object)clip);
			}
			Object.Destroy((Object)(object)source);
		}
	}
	internal static class EventConfigValues
	{
		internal static Dictionary<string, string> ReadEventLines(IEnumerable<string> lines)
		{
			Dictionary<string, string> dictionary = new Dictionary<string, string>(StringComparer.Ordinal);
			string text = "";
			foreach (string line in lines)
			{
				string text2 = line.Trim();
				if (text2.StartsWith("[") && text2.EndsWith("]"))
				{
					text = text2.Substring(1, text2.Length - 2);
				}
				else if (!(text != "Event") && !text2.StartsWith("#") && !text2.StartsWith(";"))
				{
					int num = text2.IndexOf('=');
					if (num > 0)
					{
						dictionary[text2.Substring(0, num).Trim()] = text2.Substring(num + 1).Trim();
					}
				}
			}
			return dictionary;
		}

		internal static float Number(string raw, float fallback, float min, float max, string name, Action<string> log)
		{
			if (raw == null)
			{
				return fallback;
			}
			if (!float.TryParse(raw, NumberStyles.Float, CultureInfo.InvariantCulture, out var result) || float.IsNaN(result) || float.IsInfinity(result))
			{
				log(name + ": invalid value; using " + fallback.ToString(CultureInfo.InvariantCulture));
				return fallback;
			}
			float num = Math.Max(min, Math.Min(max, result));
			if (result != num)
			{
				log(name + ": out of range; clamped to " + num.ToString(CultureInfo.InvariantCulture));
			}
			return num;
		}

		internal static float Chance(string percent, string legacy, Action<string> log)
		{
			if (percent != null)
			{
				return Number(percent, 0.5f, 0.1f, 100f, "EventChancePercent", log);
			}
			float num = Number(legacy, 0.03f, 0f, 1f, "legacy EventChance", log);
			if (num == 0.03f)
			{
				if (legacy != null)
				{
					log("Legacy EventChance=0.03 treated as old default; migrated to 0.5%.");
				}
				return 0.5f;
			}
			float result = Math.Max(0.1f, num * 100f);
			log("Legacy EventChance converted to percent: " + result.ToString(CultureInfo.InvariantCulture));
			return result;
		}

		internal static float Default(string raw, float previous, float current, float min, float max, bool migrate, string name, Action<string> log)
		{
			float num = Number(raw, current, min, max, name, log);
			if (raw != null && migrate && num == previous)
			{
				log(name + ": old default migrated to " + current.ToString(CultureInfo.InvariantCulture));
				return current;
			}
			return num;
		}

		internal static double Probability(float percent)
		{
			return (double)percent / 100.0;
		}
	}
	internal enum EventPhase
	{
		Idle,
		Warning,
		Active,
		Cooldown
	}
	internal sealed class EventTimeline
	{
		internal int Revision { get; private set; } = -1;

		internal bool Started { get; private set; }

		internal bool Scheduled { get; private set; }

		internal double Start { get; private set; }

		internal double GraceEnd { get; private set; }

		internal double End { get; private set; }

		internal double CooldownEnd { get; private set; }

		internal bool Accept(int revision, bool scheduled, double start, double end, double cooldownEnd, double? graceEnd = null, bool started = true)
		{
			double num = graceEnd ?? start;
			if (!Finite(num) || num < start || num > end)
			{
				return false;
			}
			if (revision <= Revision || !Finite(start) || !Finite(end) || !Finite(cooldownEnd) || end < start || cooldownEnd < end || (scheduled && (end - start < 1.0 || end - start > 300.0)))
			{
				return false;
			}
			Revision = revision;
			Scheduled = scheduled;
			Started = scheduled && started;
			Start = start;
			End = end;
			GraceEnd = num;
			CooldownEnd = cooldownEnd;
			return true;
		}

		internal EventPhase Phase(double now)
		{
			if (!Scheduled || !Started || !(now >= Start) || !(now < End))
			{
				if ((Scheduled && !Started) || !(now >= End) || !(now < CooldownEnd))
				{
					return EventPhase.Idle;
				}
				return EventPhase.Cooldown;
			}
			if (!(now < GraceEnd))
			{
				return EventPhase.Active;
			}
			return EventPhase.Warning;
		}

		internal bool BlackedOut(double now)
		{
			if (Scheduled && Started && now >= Start)
			{
				return now < End;
			}
			return false;
		}

		internal bool Pending(double now)
		{
			if (Scheduled)
			{
				if (Started)
				{
					return now < Start;
				}
				return true;
			}
			return false;
		}

		internal bool ConfirmStart(double now)
		{
			if (!Scheduled || Started || now < Start || now >= End)
			{
				return false;
			}
			return Accept(Revision + 1, scheduled: true, Start, End, CooldownEnd, GraceEnd);
		}

		internal bool Finish(double now, double cooldownSeconds, bool reset = false)
		{
			if (!reset && !Scheduled)
			{
				return false;
			}
			double val = ((reset || !Started) ? now : ((now >= End) ? CooldownEnd : (now + cooldownSeconds)));
			return Accept(Revision + 1, scheduled: false, now, now, Math.Max(now, val));
		}

		internal static double ResumeNaturalChecks(double now, double cooldownEnd)
		{
			return Math.Max(now, cooldownEnd);
		}

		private static bool Finite(double x)
		{
			if (!double.IsNaN(x))
			{
				return !double.IsInfinity(x);
			}
			return false;
		}
	}
	internal sealed class NaturalCheckClock
	{
		private bool occupied;

		private double next;

		internal bool HasOccupancy => occupied;

		internal double NextCheckAt => next;

		internal void Reset()
		{
			occupied = false;
			next = 0.0;
		}

		internal bool Poll(double now, bool hasPlayers, bool eligible, double cooldownEnd, double interval)
		{
			if (!hasPlayers)
			{
				Reset();
				return false;
			}
			if (!occupied)
			{
				occupied = true;
				next = now + interval;
			}
			next = Math.Max(next, cooldownEnd);
			if (!eligible || now < next)
			{
				return false;
			}
			next = now + interval;
			return true;
		}
	}
	internal sealed class PendingDeath
	{
		internal const double Delay = 0.3;

		internal bool Pending { get; private set; }

		internal double DueAt { get; private set; }

		internal bool Schedule(double now)
		{
			if (Pending || double.IsNaN(now) || double.IsInfinity(now))
			{
				return false;
			}
			Pending = true;
			DueAt = now + 0.3;
			return true;
		}

		internal bool Consume(double now)
		{
			if (!Pending || !(now >= DueAt))
			{
				return false;
			}
			Pending = false;
			return true;
		}

		internal void Cancel()
		{
			Pending = false;
			DueAt = 0.0;
		}
	}
	[BepInPlugin("local.silence", "Silence", "0.3.7")]
	public sealed class Plugin : BaseUnityPlugin
	{
		private const string StateMessage = "Silence.v036.state";

		private const string RequestMessage = "Silence.v036.sync";

		internal static Plugin Instance;

		internal Settings Options;

		private Harmony harmony;

		private NetworkManager network;

		private CustomMessagingManager messaging;

		private EventTimeline timeline = new EventTimeline();

		private readonly Blackout blackout = new Blackout();

		private readonly VoiceMonitor voice = new VoiceMonitor();

		private readonly Random random = new Random();

		private ShhAudio audio;

		private PowerOffAudio powerAudio;

		private bool effectActive;

		private bool killed;

		private bool detectionMovement;

		private bool detectionVoice;

		private bool sampled;

		private bool wasRound;

		private float voiceThreshold;

		private float nextLog;

		private float nextSync;

		private readonly NaturalCheckClock checkClock = new NaturalCheckClock();

		private readonly ReactionWindow reaction = new ReactionWindow();

		private bool detectionArmed;

		private bool blackoutAcknowledged;

		private bool blackoutWarning;

		private readonly PendingDeath pendingDeath = new PendingDeath();

		private PlayerControllerB pendingPlayer;

		private string pendingReason;

		private static readonly FieldInfo JumpingField = AccessTools.Field(typeof(PlayerControllerB), "isJumping");

		private int soundedRevision = -1;

		private Vector3 previousPosition;

		private PlayerControllerB sampledPlayer;

		private PlayerControllerB deathRequestedFor;

		private long diagnosticChecks;

		private long diagnosticWins;

		private double diagnosticLastRoll = double.NaN;

		private double diagnosticLastChance;

		private string diagnosticLastEligibility;

		private bool diagnosticFirstUpdate;

		private bool diagnosticPendingReported;

		private bool diagnosticDisabledKeyReported;

		private float diagnosticAwakeTime;

		private string diagnosticInputState;

		private bool Lethal
		{
			get
			{
				if (Active && effectActive && blackout.Ready && blackoutAcknowledged)
				{
					return reaction.Lethal(Now, timeline.End);
				}
				return false;
			}
		}

		private double Now
		{
			get
			{
				//IL_001d: Unknown result type (might be due to invalid IL or missing references)
				//IL_0022: Unknown result type (might be due to invalid IL or missing references)
				if (!Object.op_Implicit((Object)(object)network))
				{
					return 0.0;
				}
				NetworkTime serverTime = network.ServerTime;
				return ((NetworkTime)(ref serverTime)).Time;
			}
		}

		private PlayerControllerB Local
		{
			get
			{
				if (!Object.op_Implicit((Object)(object)GameNetworkManager.Instance))
				{
					return null;
				}
				return GameNetworkManager.Instance.localPlayerController;
			}
		}

		private bool RoundRunning
		{
			get
			{
				if (Object.op_Implicit((Object)(object)StartOfRound.Instance) && StartOfRound.Instance.shipHasLanded)
				{
					return !StartOfRound.Instance.inShipPhase;
				}
				return false;
			}
		}

		private bool Active
		{
			get
			{
				if (Options.EnableMod.Value && Object.op_Implicit((Object)(object)network) && network.IsListening && RoundRunning)
				{
					return timeline.BlackedOut(Now);
				}
				return false;
			}
		}

		private float Interval => Settings.Safe(Options.Interval.Value, 1f, 1f, 3600f);

		internal static bool Inside(PlayerControllerB p)
		{
			if (Object.op_Implicit((Object)(object)p) && p.isPlayerControlled && !p.isPlayerDead && p.isInsideFactory)
			{
				return !p.isInHangarShipRoom;
			}
			return false;
		}

		private void Awake()
		{
			//IL_006b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0075: Expected O, but got Unknown
			Instance = this;
			Options = new Settings(((BaseUnityPlugin)this).Config, Warn);
			LogInitializationPaths();
			audio = new ShhAudio(this);
			((MonoBehaviour)this).StartCoroutine(audio.Load());
			powerAudio = new PowerOffAudio(this);
			((MonoBehaviour)this).StartCoroutine(powerAudio.LoadCustom());
			harmony = new Harmony("local.silence");
			harmony.PatchAll(typeof(Plugin).Assembly);
			Log("Silence 0.3.7 initialization diagnostic build loaded.");
		}

		internal void Log(string message)
		{
			((BaseUnityPlugin)this).Logger.LogInfo((object)message);
		}

		internal void Warn(string message)
		{
			((BaseUnityPlugin)this).Logger.LogWarning((object)message);
		}

		private unsafe void Update()
		{
			//IL_00e1: Unknown result type (might be due to invalid IL or missing references)
			ObserveInitialization();
			DiagnosticHotkey();
			NetworkManager singleton = NetworkManager.Singleton;
			if (!Object.op_Implicit((Object)(object)singleton) || !singleton.IsListening || (!singleton.IsServer && !singleton.IsConnectedClient))
			{
				if (messaging != null)
				{
					Detach();
				}
				return;
			}
			if ((Object)(object)singleton != (Object)(object)network || messaging != singleton.CustomMessagingManager)
			{
				Attach(singleton);
			}
			if (messaging == null)
			{
				return;
			}
			voice.Bind(Object.op_Implicit((Object)(object)StartOfRound.Instance) ? StartOfRound.Instance.voiceChatModule : null);
			if (!network.IsServer && timeline.Revision < 0 && Time.unscaledTime >= nextSync)
			{
				nextSync = Time.unscaledTime + 2f;
				FastBufferWriter val = default(FastBufferWriter);
				((FastBufferWriter)(ref val))..ctor(1, (Allocator)2, -1);
				try
				{
					messaging.SendNamedMessage("Silence.v036.sync", 0uL, val, (NetworkDelivery)3);
				}
				finally
				{
					((IDisposable)(*(FastBufferWriter*)(&val))/*cast due to .constrained prefix*/).Dispose();
				}
			}
			bool roundRunning = RoundRunning;
			if (Object.op_Implicit((Object)(object)deathRequestedFor) && deathRequestedFor.isPlayerDead)
			{
				deathRequestedFor = null;
			}
			if (roundRunning && !wasRound)
			{
				deathRequestedFor = null;
				CancelDeath("new round");
			}
			if (!roundRunning || !Options.EnableMod.Value)
			{
				CancelDeath("round ended or disabled");
			}
			ProcessDeath();
			if (roundRunning)
			{
				powerAudio.Prepare();
			}
			else if (wasRound)
			{
				powerAudio.ResetCache();
			}
			if (network.IsServer)
			{
				if (!roundRunning || !Options.EnableMod.Value)
				{
					if (timeline.Scheduled || timeline.Phase(Now) == EventPhase.Cooldown)
					{
						EndEvent(reset: true);
					}
					checkClock.Reset();
				}
				else
				{
					if (!wasRound)
					{
						checkClock.Reset();
					}
					int num = IndoorPlayerCount();
					ObserveEligibility(num);
					if (timeline.Scheduled)
					{
						if (!timeline.Started && (num == 0 || Now >= timeline.End))
						{
							Log("Start cancelled before confirmation; no new cooldown.");
							EndEvent(reset: false);
						}
						else if (!timeline.Started && Now >= timeline.Start)
						{
							if (IndoorPlayerCount() == 0)
							{
								EndEvent(reset: false);
							}
							else if (timeline.ConfirmStart(Now))
							{
								Broadcast();
							}
						}
						if (timeline.Scheduled && timeline.Started && (num == 0 || Now >= timeline.End))
						{
							Log((num == 0) ? "Empty interior: end event with normal cooldown." : "Event ended normally.");
							EndEvent(reset: false);
						}
					}
					bool eligible = Options.Natural.Value && !timeline.Scheduled && timeline.Phase(Now) == EventPhase.Idle;
					if (checkClock.Poll(Now, num > 0, eligible, timeline.Scheduled ? 0.0 : timeline.CooldownEnd, Interval))
					{
						diagnosticChecks++;
						diagnosticLastRoll = random.NextDouble();
						diagnosticLastChance = Options.ChanceProbability;
						if (diagnosticLastRoll < diagnosticLastChance)
						{
							diagnosticWins++;
							StartEvent();
						}
					}
				}
			}
			wasRound = roundRunning;
			if (Active && !effectActive)
			{
				effectActive = true;
				killed = false;
				sampled = false;
				blackoutAcknowledged = false;
				blackoutWarning = false;
				reaction.Begin(Now, timeline.Start, timeline.GraceEnd, timeline.End);
				detectionArmed = false;
				voice.Disarm();
				blackout.Apply();
				if (soundedRevision != timeline.Revision)
				{
					soundedRevision = timeline.Revision;
					if (Inside(Local))
					{
						powerAudio.Play();
						audio.Play();
					}
				}
				Log($"Event active; revision={timeline.Revision}, lights={blackout.Count}");
			}
			if (!Active && effectActive)
			{
				RestoreEffects();
			}
			DebugKeys();
		}

		private int IndoorPlayerCount()
		{
			PlayerControllerB[] array = (Object.op_Implicit((Object)(object)StartOfRound.Instance) ? StartOfRound.Instance.allPlayerScripts : null);
			if (array == null || !Object.op_Implicit((Object)(object)network) || !network.IsListening)
			{
				return 0;
			}
			int num = 0;
			PlayerControllerB[] array2 = array;
			foreach (PlayerControllerB val in array2)
			{
				if (!Inside(val) || !((NetworkBehaviour)val).IsSpawned)
				{
					continue;
				}
				bool flag = false;
				foreach (ulong connectedClientsId in network.ConnectedClientsIds)
				{
					if (connectedClientsId == val.actualClientId)
					{
						flag = true;
						break;
					}
				}
				if (flag)
				{
					num++;
				}
			}
			return num;
		}

		private string StartBlockReason()
		{
			if (!Object.op_Implicit((Object)(object)network) || !network.IsServer)
			{
				return "host only";
			}
			if (!Options.EnableMod.Value || !RoundRunning)
			{
				return "disabled or round not landed";
			}
			if (IndoorPlayerCount() == 0)
			{
				return "no connected living interior players";
			}
			if (timeline.Scheduled)
			{
				return "event already scheduled/active";
			}
			if (timeline.Phase(Now) == EventPhase.Cooldown)
			{
				return "cooldown running";
			}
			return "none";
		}

		private void LateUpdate()
		{
			//IL_0200: Unknown result type (might be due to invalid IL or missing references)
			//IL_0205: Unknown result type (might be due to invalid IL or missing references)
			//IL_021c: Unknown result type (might be due to invalid IL or missing references)
			//IL_022f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0223: 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)
			//IL_022a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0238: Unknown result type (might be due to invalid IL or missing references)
			//IL_023e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0244: Unknown result type (might be due to invalid IL or missing references)
			//IL_0249: Unknown result type (might be due to invalid IL or missing references)
			//IL_02f3: Unknown result type (might be due to invalid IL or missing references)
			//IL_02f4: Unknown result type (might be due to invalid IL or missing references)
			//IL_0349: Unknown result type (might be due to invalid IL or missing references)
			if (!Active || !effectActive)
			{
				return;
			}
			blackout.Apply();
			if (!blackout.Ready)
			{
				blackoutAcknowledged = false;
				reaction.Reset();
				sampled = false;
				detectionArmed = false;
				voice.Disarm();
				if (!blackoutWarning)
				{
					blackoutWarning = true;
					Warn($"Blackout not ready; lethal detection blocked locally. interior={blackout.Interior}, collected={blackout.Collected}, excluded={blackout.Excluded}, unsupported={blackout.Unsupported}");
				}
				return;
			}
			if (!blackoutAcknowledged)
			{
				blackoutAcknowledged = true;
				reaction.Begin(Now, timeline.Start, timeline.GraceEnd, timeline.End);
				if (Options.Logs.Value)
				{
					Log($"Blackout applied: interior={blackout.Interior}, collected={blackout.Collected}, controlled={blackout.Count}, changed={blackout.Changed}, excluded={blackout.Excluded}");
				}
			}
			PlayerControllerB local = Local;
			if (!Inside(local))
			{
				sampled = false;
				return;
			}
			if (!Lethal)
			{
				sampled = false;
				voice.Disarm();
				return;
			}
			if (!detectionArmed)
			{
				detectionArmed = true;
				sampled = false;
				voice.Arm();
			}
			bool flag = detectionVoice && voice.Violates(voiceThreshold);
			Vector3 position = ((Component)local).transform.position;
			Vector3 val = ((sampled && (Object)(object)sampledPlayer == (Object)(object)local) ? (position - previousPosition) : Vector3.zero);
			float magnitude;
			if (!local.isClimbingLadder)
			{
				Vector2 val2 = new Vector2(val.x, val.z);
				magnitude = ((Vector2)(ref val2)).magnitude;
			}
			else
			{
				magnitude = ((Vector3)(ref val)).magnitude;
			}
			float num = magnitude;
			int num3;
			if (!local.isFallingFromJump)
			{
				object obj = JumpingField?.GetValue(local);
				bool flag2 = default(bool);
				int num2;
				if (obj is bool)
				{
					flag2 = (bool)obj;
					num2 = 1;
				}
				else
				{
					num2 = 0;
				}
				num3 = num2 & (flag2 ? 1 : 0);
			}
			else
			{
				num3 = 1;
			}
			bool flag3 = (byte)num3 != 0;
			bool flag4 = detectionMovement && ReactionWindow.Movement(!local.disableMoveInput && !local.isTypingChat && ((Vector2)(ref local.moveInputVector)).sqrMagnitude > 0.01f, num, flag3 && Object.op_Implicit((Object)(object)local.thisController) && !local.thisController.isGrounded);
			previousPosition = position;
			sampledPlayer = local;
			sampled = true;
			if (Options.Logs.Value && Time.unscaledTime >= nextLog)
			{
				nextLog = Time.unscaledTime + 0.5f;
				Log($"Detection: input={local.moveInputVector}, displacement={num:F5}, VAD={voice.IsVoiceActive}, mic={voice.Amplitude:F4}, threshold={voiceThreshold:F4}");
			}
			if (flag4 || flag)
			{
				Kill(local, flag4 ? "movement" : "microphone");
			}
		}

		internal void Jump(PlayerControllerB player)
		{
			if (Lethal && detectionMovement && (Object)(object)player == (Object)(object)Local)
			{
				Kill(player, "jump");
			}
		}

		private void Kill(PlayerControllerB player, string reason)
		{
			if (Lethal && !killed && !((Object)(object)player != (Object)(object)Local) && ((NetworkBehaviour)player).IsOwner && Inside(player) && ScheduleDeath(player, reason))
			{
				killed = true;
			}
		}

		private bool ScheduleDeath(PlayerControllerB player, string reason)
		{
			if (!Object.op_Implicit((Object)(object)player) || (Object)(object)player != (Object)(object)Local || !((NetworkBehaviour)player).IsOwner || !player.isPlayerControlled || player.isPlayerDead || (Object)(object)deathRequestedFor == (Object)(object)player)
			{
				return false;
			}
			if (!pendingDeath.Schedule(Time.timeAsDouble))
			{
				return false;
			}
			pendingPlayer = player;
			pendingReason = reason;
			Log($"Silence violation confirmed: {reason}; localOwner=true; death scheduled: 0.3s; due={pendingDeath.DueAt:F3}");
			return true;
		}

		private void ProcessDeath()
		{
			if (pendingDeath.Pending)
			{
				if (!Object.op_Implicit((Object)(object)pendingPlayer) || (Object)(object)pendingPlayer != (Object)(object)Local || !((NetworkBehaviour)pendingPlayer).IsOwner || !pendingPlayer.isPlayerControlled || pendingPlayer.isPlayerDead)
				{
					CancelDeath("player unavailable or already dead");
				}
				else if (pendingDeath.Consume(Time.timeAsDouble))
				{
					PlayerControllerB player = pendingPlayer;
					string reason = pendingReason;
					pendingPlayer = null;
					pendingReason = null;
					Log("Silence death delay elapsed; executing existing special death.");
					SpecialDeath(player, reason);
				}
			}
		}

		private void CancelDeath(string reason)
		{
			if (pendingDeath.Pending)
			{
				Log("Silence pending death cancelled: " + reason);
			}
			pendingDeath.Cancel();
			pendingPlayer = null;
			pendingReason = null;
		}

		private void SpecialDeath(PlayerControllerB player, string reason)
		{
			//IL_00c1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c9: Unknown result type (might be due to invalid IL or missing references)
			if (Object.op_Implicit((Object)(object)player) && !((Object)(object)player != (Object)(object)Local) && ((NetworkBehaviour)player).IsOwner && player.isPlayerControlled && !player.isPlayerDead && !((Object)(object)deathRequestedFor == (Object)(object)player))
			{
				List<GameObject> list = (Object.op_Implicit((Object)(object)StartOfRound.Instance) ? StartOfRound.Instance.playerRagdolls : null);
				if (list == null || list.Count <= 1 || !Object.op_Implicit((Object)(object)list[1]))
				{
					Warn("Head-burst ragdoll (index 1) unavailable; death skipped to avoid a partial vanilla death.");
					return;
				}
				deathRequestedFor = player;
				Log("Special death: " + reason + "; localOwner=true; spawnBody=true; deathAnimation=1; prefab=" + ((Object)list[1]).name + "; single KillPlayer call");
				player.KillPlayer(Vector3.zero, true, (CauseOfDeath)0, 1, Vector3.zero, false);
			}
		}

		private void StartEvent()
		{
			string text = StartBlockReason();
			if (text != "none")
			{
				Log("Event start refused: " + text);
				return;
			}
			float num = Settings.Safe(Options.MinDuration.Value, 5f, 1f, 300f);
			float num2 = Math.Max(num, Settings.Safe(Options.MaxDuration.Value, 10f, 1f, 300f));
			double num3 = Now + 0.75;
			double num4 = num3 + (double)num + random.NextDouble() * (double)(num2 - num);
			double num5 = ReactionWindow.ChooseGrace(Settings.Safe(Options.GraceMin.Value, 1f, 0f, 300f), Settings.Safe(Options.GraceMax.Value, 1.5f, 0f, 300f), num4 - num3, random.NextDouble());
			timeline.Accept(timeline.Revision + 1, scheduled: true, num3, num4, num4 + (double)Settings.Safe(Options.Cooldown.Value, 60f, 0f, 7200f), num3 + num5, started: false);
			detectionMovement = Options.Movement.Value;
			detectionVoice = Options.Voice.Value;
			voiceThreshold = Settings.Safe(Options.Threshold.Value, 0.02f, 0.001f, 1f);
			Broadcast();
			Log($"Event scheduled: {num3:F3} -> {num4:F3}; grace ends={timeline.GraceEnd:F3}");
		}

		private void EndEvent(bool reset)
		{
			if (!Object.op_Implicit((Object)(object)network) || !network.IsServer)
			{
				return;
			}
			double now = Now;
			if (timeline.Finish(now, Settings.Safe(Options.Cooldown.Value, 60f, 0f, 7200f), reset))
			{
				if (reset)
				{
					checkClock.Reset();
				}
				Broadcast();
				RestoreEffects();
			}
		}

		private void Broadcast()
		{
			foreach (ulong connectedClientsId in network.ConnectedClientsIds)
			{
				if (connectedClientsId != network.LocalClientId)
				{
					SendState(connectedClientsId);
				}
			}
		}

		private unsafe void SendState(ulong id, bool snapshot = false)
		{
			//IL_0013: Unknown result type (might be due to invalid IL or missing references)
			//IL_0019: Unknown result type (might be due to invalid IL or missing references)
			//IL_0031: Unknown result type (might be due to invalid IL or missing references)
			//IL_0037: Unknown result type (might be due to invalid IL or missing references)
			//IL_0050: Unknown result type (might be due to invalid IL or missing references)
			//IL_0056: Unknown result type (might be due to invalid IL or missing references)
			//IL_006f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0075: Unknown result type (might be due to invalid IL or missing references)
			//IL_008e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0094: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ad: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bf: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00de: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fd: Unknown result type (might be due to invalid IL or missing references)
			//IL_0103: Unknown result type (might be due to invalid IL or missing references)
			//IL_0113: Unknown result type (might be due to invalid IL or missing references)
			//IL_0119: Unknown result type (might be due to invalid IL or missing references)
			//IL_0129: Unknown result type (might be due to invalid IL or missing references)
			//IL_012f: Unknown result type (might be due to invalid IL or missing references)
			//IL_013f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0145: Unknown result type (might be due to invalid IL or missing references)
			//IL_0157: Unknown result type (might be due to invalid IL or missing references)
			FastBufferWriter val = default(FastBufferWriter);
			((FastBufferWriter)(ref val))..ctor(64, (Allocator)2, -1);
			try
			{
				int num = 4;
				((FastBufferWriter)(ref val)).WriteValueSafe<int>(ref num, default(ForPrimitives));
				int revision = timeline.Revision;
				((FastBufferWriter)(ref val)).WriteValueSafe<int>(ref revision, default(ForPrimitives));
				bool started = timeline.Started;
				((FastBufferWriter)(ref val)).WriteValueSafe<bool>(ref started, default(ForPrimitives));
				started = timeline.Scheduled;
				((FastBufferWriter)(ref val)).WriteValueSafe<bool>(ref started, default(ForPrimitives));
				double start = timeline.Start;
				((FastBufferWriter)(ref val)).WriteValueSafe<double>(ref start, default(ForPrimitives));
				start = timeline.GraceEnd;
				((FastBufferWriter)(ref val)).WriteValueSafe<double>(ref start, default(ForPrimitives));
				((FastBufferWriter)(ref val)).WriteValueSafe<bool>(ref snapshot, default(ForPrimitives));
				start = timeline.End;
				((FastBufferWriter)(ref val)).WriteValueSafe<double>(ref start, default(ForPrimitives));
				start = timeline.CooldownEnd;
				((FastBufferWriter)(ref val)).WriteValueSafe<double>(ref start, default(ForPrimitives));
				((FastBufferWriter)(ref val)).WriteValueSafe<bool>(ref detectionMovement, default(ForPrimitives));
				((FastBufferWriter)(ref val)).WriteValueSafe<bool>(ref detectionVoice, default(ForPrimitives));
				((FastBufferWriter)(ref val)).WriteValueSafe<float>(ref voiceThreshold, default(ForPrimitives));
				messaging.SendNamedMessage("Silence.v036.state", id, val, (NetworkDelivery)3);
			}
			finally
			{
				((IDisposable)(*(FastBufferWriter*)(&val))/*cast due to .constrained prefix*/).Dispose();
			}
		}

		private void ReceiveState(ulong sender, FastBufferReader reader)
		{
			//IL_0018: Unknown result type (might be due to invalid IL or missing references)
			//IL_001e: Unknown result type (might be due to invalid IL or missing references)
			//IL_002b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0031: Unknown result type (might be due to invalid IL or missing references)
			//IL_003e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0044: Unknown result type (might be due to invalid IL or missing references)
			//IL_0051: Unknown result type (might be due to invalid IL or missing references)
			//IL_0057: Unknown result type (might be due to invalid IL or missing references)
			//IL_0064: Unknown result type (might be due to invalid IL or missing references)
			//IL_006a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0077: Unknown result type (might be due to invalid IL or missing references)
			//IL_007d: Unknown result type (might be due to invalid IL or missing references)
			//IL_008a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0090: Unknown result type (might be due to invalid IL or missing references)
			//IL_009d: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00dc: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ef: Unknown result type (might be due to invalid IL or missing references)
			if (network.IsServer || sender != 0L)
			{
				return;
			}
			try
			{
				int num = default(int);
				((FastBufferReader)(ref reader)).ReadValueSafe<int>(ref num, default(ForPrimitives));
				int revision = default(int);
				((FastBufferReader)(ref reader)).ReadValueSafe<int>(ref revision, default(ForPrimitives));
				bool started = default(bool);
				((FastBufferReader)(ref reader)).ReadValueSafe<bool>(ref started, default(ForPrimitives));
				bool scheduled = default(bool);
				((FastBufferReader)(ref reader)).ReadValueSafe<bool>(ref scheduled, default(ForPrimitives));
				double num2 = default(double);
				((FastBufferReader)(ref reader)).ReadValueSafe<double>(ref num2, default(ForPrimitives));
				double value = default(double);
				((FastBufferReader)(ref reader)).ReadValueSafe<double>(ref value, default(ForPrimitives));
				bool flag = default(bool);
				((FastBufferReader)(ref reader)).ReadValueSafe<bool>(ref flag, default(ForPrimitives));
				double end = default(double);
				((FastBufferReader)(ref reader)).ReadValueSafe<double>(ref end, default(ForPrimitives));
				double cooldownEnd = default(double);
				((FastBufferReader)(ref reader)).ReadValueSafe<double>(ref cooldownEnd, default(ForPrimitives));
				bool flag2 = default(bool);
				((FastBufferReader)(ref reader)).ReadValueSafe<bool>(ref flag2, default(ForPrimitives));
				bool flag3 = default(bool);
				((FastBufferReader)(ref reader)).ReadValueSafe<bool>(ref flag3, default(ForPrimitives));
				float value2 = default(float);
				((FastBufferReader)(ref reader)).ReadValueSafe<float>(ref value2, default(ForPrimitives));
				if (num == 4 && timeline.Accept(revision, scheduled, num2, end, cooldownEnd, value, started))
				{
					detectionMovement = flag2;
					detectionVoice = flag3;
					voiceThreshold = Settings.Safe(value2, 0.02f, 0.001f, 1f);
					RestoreEffects();
					if (flag && Now >= num2)
					{
						soundedRevision = revision;
					}
				}
			}
			catch (Exception ex)
			{
				Warn("Invalid state message ignored: " + ex.Message);
			}
		}

		private void ReceiveRequest(ulong sender, FastBufferReader reader)
		{
			if (network.IsServer && network.ConnectedClients.ContainsKey(sender))
			{
				SendState(sender, snapshot: true);
			}
		}

		private void Attach(NetworkManager manager)
		{
			//IL_0034: Unknown result type (might be due to invalid IL or missing references)
			//IL_003e: Expected O, but got Unknown
			//IL_0050: Unknown result type (might be due to invalid IL or missing references)
			//IL_005a: Expected O, but got Unknown
			Detach();
			network = manager;
			messaging = manager.CustomMessagingManager;
			if (messaging != null)
			{
				messaging.RegisterNamedMessageHandler("Silence.v036.state", new HandleNamedMessageDelegate(ReceiveState));
				messaging.RegisterNamedMessageHandler("Silence.v036.sync", new HandleNamedMessageDelegate(ReceiveRequest));
				if (network.IsServer)
				{
					timeline.Accept(0, scheduled: false, Now, Now, Now);
				}
				checkClock.Reset();
				nextSync = 0f;
				Log("Network handlers ready.");
				if (network.IsServer)
				{
					Log("Effective host settings: " + EffectiveSettings());
				}
			}
		}

		private void RestoreEffects()
		{
			blackout.Restore();
			if (effectActive && Options.Logs.Value)
			{
				Log($"Blackout restored: lights={blackout.Restored}");
			}
			audio?.Stop();
			powerAudio?.Stop();
			effectActive = false;
			sampled = false;
			killed = false;
			detectionArmed = false;
			blackoutAcknowledged = false;
			reaction.Reset();
			voice.Disarm();
		}

		private void Detach()
		{
			CancelDeath("network detach or plugin cleanup");
			RestoreEffects();
			voice.Bind(null);
			powerAudio?.ResetCache();
			deathRequestedFor = null;
			if (messaging != null)
			{
				messaging.UnregisterNamedMessageHandler("Silence.v036.state");
				messaging.UnregisterNamedMessageHandler("Silence.v036.sync");
			}
			messaging = null;
			network = null;
			timeline = new EventTimeline();
			soundedRevision = -1;
			wasRound = false;
		}

		private void DebugKeys()
		{
			Keyboard current = Keyboard.current;
			if (!Options.Hotkeys.Value || current == null)
			{
				return;
			}
			if (((ButtonControl)current.f6Key).wasPressedThisFrame)
			{
				if (network.IsServer)
				{
					StartEvent();
				}
				else
				{
					Log("F6 received but rejected: host only.");
				}
			}
			if (((ButtonControl)current.f7Key).wasPressedThisFrame && network.IsServer)
			{
				Log("F7 stops the event; confirmed deaths remain scheduled (same policy as natural end).");
				EndEvent(reset: false);
			}
			if (((ButtonControl)current.f9Key).wasPressedThisFrame && !Active && Inside(Local))
			{
				audio.Play();
			}
			if (((ButtonControl)current.f10Key).wasPressedThisFrame)
			{
				powerAudio.CyclePreview();
			}
			if (((ButtonControl)current.f11Key).wasPressedThisFrame && !Active && Inside(Local))
			{
				powerAudio.Preview();
			}
			if (((ButtonControl)current.f12Key).wasPressedThisFrame && Options.EnableMod.Value && RoundRunning)
			{
				ScheduleDeath(Local, "F12 local debug test");
			}
		}

		private void OnDisable()
		{
			if (Options != null)
			{
				Warn("Silence component disabled; Update and coroutine progress may stop.");
				Detach();
			}
		}

		private void OnDestroy()
		{
			if (Options != null)
			{
				Warn("Silence component destroyed; initialization/runtime stopped.");
			}
			Detach();
			audio?.Dispose();
			powerAudio?.Dispose();
			Harmony obj = harmony;
			if (obj != null)
			{
				obj.UnpatchSelf();
			}
			if ((Object)(object)Instance == (Object)(object)this)
			{
				Instance = null;
			}
		}

		private string EffectiveSettings()
		{
			float num = Settings.Safe(Options.MinDuration.Value, 5f, 1f, 300f);
			float num2 = Math.Max(num, Settings.Safe(Options.MaxDuration.Value, 10f, 1f, 300f));
			return $"enabled={Options.EnableMod.Value}, natural={Options.Natural.Value}, CheckInterval={Interval}, EventChancePercent={Options.Chance.Value}, probability={Options.ChanceProbability:R}, CooldownSeconds={Settings.Safe(Options.Cooldown.Value, 60f, 0f, 7200f)}, duration={num}-{num2}, grace={Options.GraceMin.Value}-{Options.GraceMax.Value}, deathDelay={0.3}";
		}

		private string DiagnosticBlockReason(int inside)
		{
			if (!Object.op_Implicit((Object)(object)network) || !network.IsListening)
			{
				return "Network not ready";
			}
			if (!network.IsServer)
			{
				return "Host authoritative; client view is not a spawning decision";
			}
			if (!Options.EnableMod.Value)
			{
				return "Mod disabled";
			}
			if (!RoundRunning)
			{
				return "Round not ready";
			}
			if (!Options.Natural.Value)
			{
				return "Natural spawning disabled";
			}
			if (timeline.Scheduled)
			{
				if (!timeline.Started)
				{
					return "Event awaiting start confirmation";
				}
				return "Event already active";
			}
			if (timeline.Phase(Now) == EventPhase.Cooldown)
			{
				return "Cooldown active";
			}
			if (inside == 0)
			{
				return "No eligible indoor players";
			}
			return "Eligible";
		}

		private void ObserveEligibility(int inside)
		{
			if (Options.Logs.Value)
			{
				string text = $"inside={inside}; {DiagnosticBlockReason(inside)}";
				if (!(text == diagnosticLastEligibility))
				{
					diagnosticLastEligibility = text;
					Log("Eligibility changed: " + text);
				}
			}
		}

		private void DiagnosticHotkey()
		{
			Keyboard current = Keyboard.current;
			if (current == null)
			{
				return;
			}
			bool wasPressedThisFrame = ((ButtonControl)current.f6Key).wasPressedThisFrame;
			bool wasPressedThisFrame2 = ((ButtonControl)current.f8Key).wasPressedThisFrame;
			if (!wasPressedThisFrame && !wasPressedThisFrame2)
			{
				return;
			}
			if (!Options.Hotkeys.Value)
			{
				if (!diagnosticDisabledKeyReported)
				{
					diagnosticDisabledKeyReported = true;
					Log("F6/F8 input received but debug hotkeys are disabled in the active config.");
				}
				return;
			}
			if (wasPressedThisFrame && (!Object.op_Implicit((Object)(object)network) || messaging == null))
			{
				Log("F6 received but network initialization is not ready.");
			}
			if (!wasPressedThisFrame2)
			{
				return;
			}
			try
			{
				DumpPlayerDiagnostics();
			}
			catch (Exception ex)
			{
				Warn("F8 diagnostic snapshot unavailable: " + ex.GetType().Name);
			}
		}

		private void DumpPlayerDiagnostics()
		{
			NetworkManager val = (Object.op_Implicit((Object)(object)network) ? network : NetworkManager.Singleton);
			bool flag = Object.op_Implicit((Object)(object)val) && val.IsServer && val.IsListening;
			HashSet<ulong> hashSet = new HashSet<ulong>();
			if (flag)
			{
				foreach (ulong connectedClientsId in val.ConnectedClientsIds)
				{
					hashSet.Add(connectedClientsId);
				}
			}
			Log(string.Format("F8: host={0}, server={1}, listening={2}, connectedPlayers={3}", Object.op_Implicit((Object)(object)val) && val.IsHost, Object.op_Implicit((Object)(object)val) && val.IsServer, Object.op_Implicit((Object)(object)val) && val.IsListening, flag ? hashSet.Count.ToString() : "unknown; run F8 on host"));
			PlayerControllerB[] array = (Object.op_Implicit((Object)(object)StartOfRound.Instance) ? StartOfRound.Instance.allPlayerScripts : null);
			int num = 0;
			HashSet<ulong> hashSet2 = new HashSet<ulong>();
			if (array == null)
			{
				Log("Player slots unavailable (StartOfRound/allPlayerScripts).");
			}
			else
			{
				for (int i = 0; i < array.Length; i++)
				{
					PlayerControllerB val2 = array[i];
					if (!Object.op_Implicit((Object)(object)val2))
					{
						Log($"Player slot={i}: excluded=missing/destroyed PlayerControllerB");
						continue;
					}
					bool flag2 = flag && hashSet.Contains(val2.actualClientId);
					List<string> list = new List<string>();
					if (!val2.isPlayerControlled)
					{
						list.Add("not controlled (unused/disconnected/spectating slot)");
					}
					if (val2.isPlayerDead)
					{
						list.Add("dead/spectating");
					}
					if (!val2.isInsideFactory)
					{
						list.Add("outside facility");
					}
					if (val2.isInHangarShipRoom)
					{
						list.Add("in ship");
					}
					if (!((NetworkBehaviour)val2).IsSpawned)
					{
						list.Add("NetworkBehaviour not spawned");
					}
					if (!flag)
					{
						list.Add("membership unknown on client");
					}
					else if (!flag2)
					{
						list.Add("actualClientId not connected");
					}
					bool flag3 = list.Count == 0;
					if (flag3)
					{
						num++;
					}
					NetworkObject networkObject = ((NetworkBehaviour)val2).NetworkObject;
					string text = ((flag2 && !hashSet2.Add(val2.actualClientId)) ? "; duplicate client ID in slots (investigate)" : "");
					Log(string.Format("Player slot={0}, ClientId={1}, controlled={2}, dead={3}, inside={4}, ship={5}, behaviourSpawned={6}, networkObjectPresent={7}, objectSpawned={8}, ownerClientId={9}, connected={10}, excluded={11}{12}", i, val2.actualClientId, val2.isPlayerControlled, val2.isPlayerDead, val2.isInsideFactory, val2.isInHangarShipRoom, ((NetworkBehaviour)val2).IsSpawned, Object.op_Implicit((Object)(object)networkObject), Object.op_Implicit((Object)(object)networkObject) && networkObject.IsSpawned, Object.op_Implicit((Object)(object)networkObject) ? networkObject.OwnerClientId.ToString() : "none", flag ? flag2.ToString() : "unknown", flag3 ? "none" : string.Join(", ", list), text));
				}
			}
			Log(string.Format("Eligible indoor players: {0}; slots={1}", flag ? num.ToString() : "unknown; host F8 required", (array != null) ? array.Length : 0));
			Log("Effective local settings" + (flag ? " (host authoritative): " : " (host may differ): ") + EffectiveSettings());
			double num2 = (timeline.Scheduled ? 0.0 : Math.Max(0.0, timeline.CooldownEnd - Now));
			bool flag4 = flag && DiagnosticBlockReason(num) == "Eligible";
			bool flag5 = flag4 && checkClock.HasOccupancy && Now >= checkClock.NextCheckAt;
			Log($"Event state={timeline.Phase(Now)}, revision={timeline.Revision}, scheduled={timeline.Scheduled}, confirmed={timeline.Started}, cooldownRemaining={num2:F3}, nextCheckAt={checkClock.NextCheckAt:F3}, clockOccupied={checkClock.HasOccupancy}, canCheck={flag4}, dueNow={flag5}, reason={DiagnosticBlockReason(num)}");
			Log($"Probability checks since load={diagnosticChecks}, wins={diagnosticWins}, lastRoll={diagnosticLastRoll:R}, lastProbability={diagnosticLastChance:R}; winning draw and refused start are distinct outcomes.");
			Log(string.Format("Blackout active={0}, ready={1}, collected={2}, controlled={3}, excluded={4}, unsupported={5}, status={6}, pendingDeath={7}, shhStatus={8}, powerStatus={9}", effectActive, blackout.Ready, blackout.Collected, blackout.Count, blackout.Excluded, blackout.Unsupported, (effectActive && !blackout.Ready) ? "Blackout initialization failed/not ready; see blackout warning" : (effectActive ? "Light properties applied; visual confirmation still required" : "Not initializing while event inactive"), pendingDeath.Pending, audio?.Status, powerAudio?.Status));
		}

		internal string PrivatePath(string path)
		{
			if (string.IsNullOrEmpty(path))
			{
				return "<empty>";
			}
			try
			{
				string fullPath = Path.GetFullPath(path);
				string text = Path.GetFullPath(Paths.PluginPath).TrimEnd(Path.DirectorySeparatorChar) + Path.DirectorySeparatorChar;
				return fullPath.StartsWith(text, StringComparison.OrdinalIgnoreCase) ? ("<plugins>/" + fullPath.Substring(text.Length)) : ("<external>/" + Path.GetFileName(fullPath));
			}
			catch
			{
				return "<invalid path>";
			}
		}

		private void LogInitializationPaths()
		{
			diagnosticAwakeTime = Time.realtimeSinceStartup;
			Log("Plugin assembly path: " + PrivatePath(typeof(Plugin).Assembly.Location));
			Log("Plugin metadata location: " + PrivatePath(((BaseUnityPlugin)this).Info.Location));
			Log("Config file: <config>/" + Path.GetFileName(((BaseUnityPlugin)this).Config.ConfigFilePath));
			try
			{
				string directoryName = Path.GetDirectoryName(((BaseUnityPlugin)this).Info.Location);
				string value = Options.SoundFile.Value;
				Log("Plugin directory: " + PrivatePath(directoryName + Path.DirectorySeparatorChar));
				Log("Audio setting: " + (Path.IsPathRooted(value) ? PrivatePath(value) : value));
				if (!string.IsNullOrWhiteSpace(value) && !Path.IsPathRooted(value))
				{
					string fullPath = Path.GetFullPath(Path.Combine(directoryName, value));
					Log("Resolved audio path: " + PrivatePath(fullPath));
					Log("Audio file exists: " + File.Exists(fullPath));
				}
			}
			catch (Exception ex)
			{
				Warn("Audio path diagnostic failed: " + ex.GetType().Name);
			}
			Log($"Debug hotkeys enabled: {Options.Hotkeys.Value}; debug logs enabled: {Options.Logs.Value}; natural spawning enabled: {Options.Natural.Value}");
			Options.Hotkeys.SettingChanged += delegate
			{
				Log("Debug hotkeys setting changed: " + Options.Hotkeys.Value);
			};
			Options.Logs.SettingChanged += delegate
			{
				Log("Debug logs setting changed: " + Options.Logs.Value);
			};
		}

		private void ObserveInitialization()
		{
			string text = $"focused={Application.isFocused}, keyboardAvailable={Keyboard.current != null}, hotkeys={Options.Hotkeys.Value}";
			if (!diagnosticFirstUpdate)
			{
				diagnosticFirstUpdate = true;
				Log($"First Update reached: enabled={((Behaviour)this).enabled}, active={((Component)this).gameObject.activeInHierarchy}; {text}; audio={audio?.Status}");
				diagnosticInputState = text;
			}
			else if (Options.Logs.Value && text != diagnosticInputState)
			{
				diagnosticInputState = text;
				Log("Input state changed: " + text);
			}
			if (!diagnosticPendingReported && audio != null && audio.Status.StartsWith("Pending") && Time.realtimeSinceStartup - diagnosticAwakeTime >= 15f)
			{
				diagnosticPendingReported = true;
				Warn("Audio initialization still Pending after 15 seconds; request is not aborted and event policy is unchanged.");
			}
		}
	}
	[HarmonyPatch(typeof(PlayerControllerB), "Jump_performed")]
	internal static class JumpPatch
	{
		private static void Prefix(bool ___isJumping, out bool __state)
		{
			__state = ___isJumping;
		}

		private static void Postfix(PlayerControllerB __instance, bool ___isJumping, bool __state)
		{
			if (!__state && ___isJumping)
			{
				Plugin.Instance?.Jump(__instance);
			}
		}
	}
	internal enum PowerOffSoundMode
	{
		VanillaBreaker,
		VanillaLightOff,
		VanillaApparatusDisconnect,
		VanillaApparatusRemoval,
		Custom,
		Disabled
	}
	internal sealed class PowerOffAudio
	{
		private readonly Plugin plugin;

		private readonly AudioSource source;

		private readonly Dictionary<PowerOffSoundMode, AudioClip> clips = new Dictionary<PowerOffSoundMode, AudioClip>();

		private readonly Dictionary<PowerOffSoundMode, Object> owners = new Dictionary<PowerOffSoundMode, Object>();

		private readonly Dictionary<PowerOffSoundMode, float> nextSearch = new Dictionary<PowerOffSoundMode, float>();

		private readonly HashSet<PowerOffSoundMode> warned = new HashSet<PowerOffSoundMode>();

		private AudioClip custom;

		private RoundManager round;

		private PowerOffSoundMode preview;

		private string customStatus = "Not loaded (Custom mode loads at startup)";

		internal string Status { get; private set; } = "Waiting for facility";

		internal PowerOffAudio(Plugin plugin)
		{
			this.plugin = plugin;
			source = ((Component)plugin).gameObject.AddComponent<AudioSource>();
			source.playOnAwake = false;
			source.loop = false;
			source.spatialBlend = 0f;
			source.panStereo = 0f;
			source.dopplerLevel = 0f;
			source.bypassReverbZones = true;
			SceneManager.sceneLoaded += OnSceneLoaded;
			SceneManager.sceneUnloaded += OnSceneUnloaded;
		}

		private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
		{
			ResetCache();
		}

		private void OnSceneUnloaded(Scene scene)
		{
			ResetCache();
		}

		internal void ResetCache()
		{
			clips.Clear();
			owners.Clear();
			nextSearch.Clear();
			warned.Clear();
			round = null;
			Status = "Waiting for facility";
		}

		internal void Prepare()
		{
			try
			{
				if ((Object)(object)round != (Object)(object)RoundManager.Instance)
				{
					ResetCache();
					round = RoundManager.Instance;
				}
				Resolve(plugin.Options.PowerMode.Value);
			}
			catch (Exception ex)
			{
				WarnOnce(plugin.Options.PowerMode.Value, ex.Message);
			}
		}

		private static AudioClip ReadClip(object owner, string field)
		{
			object? obj = owner.GetType().GetField(field, BindingFlags.Instance | BindingFlags.Public)?.GetValue(owner);
			return (AudioClip)((obj is AudioClip) ? obj : null);
		}

		private AudioClip Resolve(PowerOffSoundMode mode)
		{
			switch (mode)
			{
			case PowerOffSoundMode.Disabled:
				Status = "Disabled";
				return null;
			case PowerOffSoundMode.Custom:
				Status = customStatus;
				return custom;
			default:
			{
				if (clips.TryGetValue(mode, out var value) && Object.op_Implicit((Object)(object)value) && owners.TryGetValue(mode, out var value2) && Object.op_Implicit(value2))
				{
					Status = mode.ToString() + ": " + ((Object)value).name;
					return value;
				}
				if (nextSearch.TryGetValue(mode, out var value3) && Time.unscaledTime < value3)
				{
					return null;
				}
				nextSearch[mode] = Time.unscaledTime + 2f;
				AudioClip val = null;
				Object value4 = null;
				switch (mode)
				{
				case PowerOffSoundMode.VanillaBreaker:
				{
					BreakerBox[] array3 = Object.FindObjectsOfType<BreakerBox>();
					foreach (BreakerBox val4 in array3)
					{
						val = ReadClip(val4, "switchPowerSFX");
						if (Object.op_Implicit((Object)(object)val))
						{
							value4 = (Object)(object)val4;
							break;
						}
					}
					break;
				}
				case PowerOffSoundMode.VanillaApparatusDisconnect:
				case PowerOffSoundMode.VanillaApparatusRemoval:
				{
					LungProp[] array2 = Object.FindObjectsOfType<LungProp>();
					foreach (LungProp val3 in array2)
					{
						val = ReadClip(val3, (mode == PowerOffSoundMode.VanillaApparatusDisconnect) ? "disconnectSFX" : "removeFromMachineSFX");
						if (Object.op_Implicit((Object)(object)val))
						{
							value4 = (Object)(object)val3;
							break;
						}
					}
					break;
				}
				case PowerOffSoundMode.VanillaLightOff:
				{
					AudioClip[] array = Resources.FindObjectsOfTypeAll<AudioClip>();
					foreach (AudioClip val2 in array)
					{
						if (Object.op_Implicit((Object)(object)val2) && !(((Object)val2).name != "LightOff"))
						{
							if (Object.op_Implicit((Object)(object)val) && (Object)(object)val != (Object)(object)val2)
							{
								Status = "Ambiguous LightOff clips; skipped";
								return null;
							}
							val = val2;
						}
					}
					value4 = (Object)(object)val;
					break;
				}
				}
				if (Object.op_Implicit((Object)(object)val))
				{
					clips[mode] = val;
					owners[mode] = value4;
					Status = mode.ToString() + ": " + ((Object)val).name;
					if (plugin.Options.Logs.Value)
					{
						plugin.Log("Power sound resolved: " + Status);
					}
				}
				else
				{
					Status = mode.ToString() + ": clip unavailable";
				}
				return val;
			}
			}
		}

		private void WarnOnce(PowerOffSoundMode mode, string reason)
		{
			Status = mode.ToString() + ": " + reason;
			if (warned.Add(mode))
			{
				plugin.Warn("Power-off sound skipped: " + Status + ". Shh and event logic remain enabled.");
			}
		}

		internal void Play()
		{
			Play(plugin.Options.PowerMode.Value);
		}

		private void Play(PowerOffSoundMode mode)
		{
			if (mode == PowerOffSoundMode.Disabled)
			{
				return;
			}
			try
			{
				AudioClip val = Resolve(mode);
				if (!Object.op_Implicit((Object)(object)val))
				{
					WarnOnce(mode, "no valid clip (" + Status + ")");
				}
				else
				{
					source.PlayOneShot(val, Settings.Safe(plugin.Options.PowerVolume.Value, 0.8f, 0f, 1f));
				}
			}
			catch (Exception ex)
			{
				WarnOnce(mode, ex.Message);
			}
		}

		internal void CyclePreview()
		{
			preview = (PowerOffSoundMode)((int)(preview + 1) % 6);
			plugin.Log("F11 power sound preview: " + preview.ToString() + ". This does not change saved settings.");
		}

		internal void Preview()
		{
			Play(preview);
			plugin.Log("Power preview " + preview.ToString() + ": " + Status);
		}

		internal IEnumerator LoadCustom()
		{
			if (plugin.Options.PowerMode.Value != PowerOffSoundMode.Custom)
			{
				yield break;
			}
			string path;
			try
			{
				string fullPath = Path.GetFullPath(Path.GetDirectoryName(((BaseUnityPlugin)plugin).Info.Location));
				string value = plugin.Options.PowerFile.Value;
				if (string.IsNullOrWhiteSpace(value) || Path.IsPathRooted(value))
				{
					throw new IOException("Use a relative filename");
				}
				path = Path.GetFullPath(Path.Combine(fullPath, value));
				if (!path.StartsWith(fullPath + Path.DirectorySeparatorChar, StringComparison.OrdinalIgnoreCase))
				{
					throw new IOException("Sound must be inside plugin folder");
				}
				if (!File.Exists(path) || new FileInfo(path).Length > 33554432)
				{
					throw new IOException("File missing or larger than 32 MiB");
				}
				string text = Path.GetExtension(path).ToLowerInvariant();
				if (text != ".wav" && text != ".ogg")
				{
					throw new IOException("Use WAV or OGG");
				}
			}
			catch (Exception ex)
			{
				customStatus = ex.Message;
				WarnOnce(PowerOffSoundMode.Custom, ex.Message);
				yield break;
			}
			UnityWebRequest request = UnityWebRequestMultimedia.GetAudioClip(new Uri(path).AbsoluteUri, (AudioType)(Path.GetExtension(path).Equals(".ogg", StringComparison.OrdinalIgnoreCase) ? 14 : 20));
			try
			{
				yield return request.SendWebRequest();
				try
				{
					if ((int)request.result != 1)
					{
						throw new IOException(request.error);
					}
					custom = DownloadHandlerAudioClip.GetContent(request);
					if (!Object.op_Implicit((Object)(object)custom) || custom.samples == 0 || custom.length > 30f)
					{
						throw new IOException("Invalid clip or longer than 30 seconds");
					}
					customStatus = "Custom ready: " + Path.GetFileName(path);
				}
				catch (Exception ex2)
				{
					if (Object.op_Implicit((Object)(object)custom))
					{
						Object.Destroy((Object)(object)custom);
					}
					custom = null;
					customStatus = ex2.Message;
					WarnOnce(PowerOffSoundMode.Custom, ex2.Message);
				}
			}
			finally
			{
				((IDisposable)request)?.Dispose();
			}
		}

		internal void Stop()
		{
			if (Object.op_Implicit((Object)(object)source))
			{
				source.Stop();
			}
		}

		internal void Dispose()
		{
			SceneManager.sceneLoaded -= OnSceneLoaded;
			SceneManager.sceneUnloaded -= OnSceneUnloaded;
			Stop();
			ResetCache();
			if (Object.op_Implicit((Object)(object)custom))
			{
				Object.Destroy((Object)(object)custom);
			}
			if (Object.op_Implicit((Object)(object)source))
			{
				Object.Destroy((Object)(object)source);
			}
		}
	}
	internal sealed class ReactionWindow
	{
		private double deadline = double.PositiveInfinity;

		internal static double ChooseGrace(double min, double max, double duration, double random)
		{
			return Math.Min(duration, min + random * (Math.Max(min, max) - min));
		}

		internal void Begin(double renderedAt, double start, double graceEnd, double end)
		{
			deadline = Math.Min(end, Math.Max(graceEnd, renderedAt + Math.Min(1.0, graceEnd - start)));
		}

		internal bool Lethal(double now, double end)
		{
			if (now >= deadline)
			{
				return now < end;
			}
			return false;
		}

		internal void Reset()
		{
			deadline = double.PositiveInfinity;
		}

		internal static bool Movement(bool input, float distance, bool airborneJump)
		{
			if (!airborneJump)
			{
				if (input)
				{
					return distance > 0.0005f;
				}
				return false;
			}
			return true;
		}
	}
	internal sealed class FreshVoiceFrame
	{
		private double armedAt = double.PositiveInfinity;

		private double frameStart;

		private double receivedAt;

		private float amplitude;

		private bool pending;

		internal void Arm(double now)
		{
			armedAt = now;
			pending = false;
		}

		internal void Disarm()
		{
			armedAt = double.PositiveInfinity;
			pending = false;
		}

		internal void Receive(double now, double duration, double latency, float value)
		{
			frameStart = now - duration - latency;
			receivedAt = now;
			amplitude = value;
			pending = true;
		}

		internal bool Consume(double now, bool vad, float threshold)
		{
			bool result = pending && frameStart >= armedAt && now >= receivedAt && now - receivedAt <= 0.25 && vad && !float.IsNaN(amplitude) && !float.IsInfinity(amplitude) && amplitude >= threshold;
			pending = false;
			return result;
		}
	}
	internal sealed class Settings
	{
		internal readonly ConfigEntry<bool> EnableMod;

		internal readonly ConfigEntry<bool> Natural;

		internal readonly ConfigEntry<bool> Movement;

		internal readonly ConfigEntry<bool> Voice;

		internal readonly ConfigEntry<bool> Sound;

		internal readonly ConfigEntry<bool> Logs;

		internal readonly ConfigEntry<bool> Hotkeys;

		internal readonly ConfigEntry<float> Chance;

		internal readonly ConfigEntry<float> Interval;

		internal readonly ConfigEntry<float> MinDuration;

		internal readonly ConfigEntry<float> MaxDuration;

		internal readonly ConfigEntry<float> Cooldown;

		internal readonly ConfigEntry<float> Threshold;

		internal readonly ConfigEntry<float> Volume;

		internal readonly ConfigEntry<float> GraceMin;

		internal readonly ConfigEntry<float> GraceMax;

		internal readonly ConfigEntry<string> SoundFile;

		internal readonly ConfigEntry<PowerOffSoundMode> PowerMode;

		internal readonly ConfigEntry<float> PowerVolume;

		internal readonly ConfigEntry<string> PowerFile;

		internal double ChanceProbability => EventConfigValues.Probability(Safe(Chance.Value, 0.5f, 0.1f, 100f));

		internal Settings(ConfigFile c, Action<string> warn)
		{
			Settings settings = this;
			Dictionary<string, string> raw = EventConfigValues.ReadEventLines(File.Exists(c.ConfigFilePath) ? ((IEnumerable<string>)File.ReadAllLines(c.ConfigFilePath)) : ((IEnumerable<string>)Array.Empty<string>()));
			bool saveOnConfigSet = c.SaveOnConfigSet;
			c.SaveOnConfigSet = false;
			ConfigEntry<int> obj = c.Bind<int>("Migration", "EventSettingsSchema", 0, "Internal one-time event settings migration marker. Do not reset.");
			bool migrate = obj.Value < 1;
			float value = EventConfigValues.Chance(Read("EventChancePercent"), Read("EventChance"), warn);
			float value2 = EventConfigValues.Default(Read("CheckInterval"), 25f, 1f, 1f, 3600f, migrate, "CheckInterval", warn);
			float value3 = EventConfigValues.Default(Read("MinimumDuration"), 8f, 5f, 1f, 300f, migrate, "MinimumDuration", warn);
			float value4 = EventConfigValues.Default(Read("MaximumDuration"), 12f, 10f, 1f, 300f, migrate, "MaximumDuration", warn);
			EnableMod = c.Bind<bool>("General", "EnableMod", true, "Enable Silence. All participants must install the mod.");
			Natural = c.Bind<bool>("General", "EnableNaturalSpawning", true, "Host: random events.");
			Chance = Number(c, "Event", "EventChancePercent", 0.5f, 0.1f, 100f, "Chance per check, expressed as a percentage (0.1 to 100). 0.5 means 0.5%, not 50%.");
			Interval = Number(c, "Event", "CheckInterval", 1f, 1f, 3600f, "Seconds between checks; first post-event check at cooldown expiry.");
			MinDuration = Number(c, "Event", "MinimumDuration", 5f, 1f, 300f, "Minimum seconds.");
			MaxDuration = Number(c, "Event", "MaximumDuration", 10f, 1f, 300f, "Maximum seconds; at least MinimumDuration.");
			Cooldown = Number(c, "Event", "CooldownSeconds", 60f, 0f, 7200f, "No natural checks during these seconds after event ends.");
			GraceMin = Number(c, "Event", "GracePeriodMin", 1f, 0f, 300f, "Host: minimum reaction seconds, included in blackout duration.");
			GraceMax = Number(c, "Event", "GracePeriodMax", 1.5f, 0f, 300f, "Host: maximum reaction seconds; at least GracePeriodMin, capped to event duration.");
			Movement = c.Bind<bool>("Detection", "DetectMovement", true, "Host: punish voluntary movement.");
			Voice = c.Bind<bool>("Detection", "DetectVoice", true, "Host: punish local microphone voice activity.");
			Threshold = Number(c, "Detection", "VoiceThreshold", 0.02f, 0.001f, 1f, "Local microphone amplitude plus Dissonance VAD; host setting.");
			Sound = c.Bind<bool>("Audio", "EnableShhSound", true, "Play local 2D sound at blackout.");
			Volume = Number(c, "Audio", "ShhSoundVolume", 0.8f, 0f, 1f, "Local sound volume.");
			SoundFile = c.Bind<string>("Audio", "ShhSoundFile", "silence_shh.wav", "WAV or OGG filename relative to Silence.dll; restart to reload.");
			PowerMode = c.Bind<PowerOffSoundMode>("Audio", "PowerOffSoundMode", PowerOffSoundMode.VanillaBreaker, "Blackout sound. Existing saved choices are preserved.");
			PowerVolume = Number(c, "Audio", "PowerOffSoundVolume", 0.8f, 0f, 1f, "Blackout sound volume, independent of the shh sound.");
			PowerFile = c.Bind<string>("Audio", "PowerOffSoundFile", "silence_poweroff.wav", "Custom mode only: WAV/OGG relative to Silence.dll; restart to reload.");
			Logs = c.Bind<bool>("Debug", "EnableDebugLogs", false, "Detection samples and event logging.");
			Hotkeys = c.Bind<bool>("Debug", "EnableDebugHotkeys", false, "F6 start (host), F7 stop (host), F8 status, F9 shh preview, F10 cycle power preview, F11 power preview, F12 kill LOCAL player with head-burst corpse (landed round only).");
			Chance.Value = value;
			Interval.Value = value2;
			MinDuration.Value = value3;
			MaxDuration.Value = value4;
			obj.Value = 1;
			Chance.SettingChanged += delegate
			{
				float num = Safe(settings.Chance.Value, 0.5f, 0.1f, 100f);
				if (float.IsNaN(settings.Chance.Value) || float.IsInfinity(settings.Chance.Value) || settings.Chance.Value != num)
				{
					warn("EventChancePercent invalid at runtime; using " + num);
					settings.Chance.Value = num;
				}
			};
			c.SaveOnConfigSet = saveOnConfigSet;
			c.Save();
			string Read(string key)
			{
				if (!raw.TryGetValue(key, out var value5))
				{
					return null;
				}
				return value5;
			}
		}

		private static ConfigEntry<float> Number(ConfigFile c, string section, string key, float value, float min, float max, string help)
		{
			//IL_0014: Unknown result type (might be due to invalid IL or missing references)
			//IL_001e: Expected O, but got Unknown
			return c.Bind<float>(section, key, value, new ConfigDescription(help, (AcceptableValueBase)(object)new AcceptableValueRange<float>(min, max), Array.Empty<object>()));
		}

		internal static float Safe(float value, float fallback, float min, float max)
		{
			if (!float.IsNaN(value) && !float.IsInfinity(value))
			{
				return Math.Max(min, Math.Min(max, value));
			}
			return fallback;
		}
	}
	internal sealed class VoiceMonitor : IVoiceActivationListener, IMicrophoneSubscriber
	{
		private DissonanceComms comms;

		private IMicrophoneCapture capture;

		private volatile bool active;

		private readonly object sync = new object();

		private readonly FreshVoiceFrame frames = new FreshVoiceFrame();

		private double latency;

		private static double Clock => (double)Stopwatch.GetTimestamp() / (double)Stopwatch.Frequency;

		internal float Amplitude { get; private set; }

		internal bool IsVoiceActive => active;

		public void VoiceActivationStart()
		{
			active = true;
		}

		public void VoiceActivationStop()
		{
			active = false;
		}

		internal void Bind(DissonanceComms next)
		{
			if ((Object)(object)comms != (Object)(object)next)
			{
				if (Object.op_Implicit((Object)(object)comms))
				{
					comms.UnsubscribeFromVoiceActivation((IVoiceActivationListener)(object)this);
				}
				comms = next;
				active = false;
				if (Object.op_Implicit((Object)(object)comms))
				{
					comms.SubcribeToVoiceActivation((IVoiceActivationListener)(object)this);
				}
			}
			IMicrophoneCapture val = (Object.op_Implicit((Object)(object)comms) ? comms.MicrophoneCapture : null);
			if (capture != val)
			{
				IMicrophoneCapture obj = capture;
				if (obj != null)
				{
					obj.Unsubscribe((IMicrophoneSubscriber)(object)this);
				}
				Disarm();
				capture = val;
				IMicrophoneCapture obj2 = capture;
				if (obj2 != null)
				{
					obj2.Subscribe((IMicrophoneSubscriber)(object)this);
				}
			}
			lock (sync)
			{
				latency = ((capture == null) ? 0.0 : Math.Max(0.0, capture.Latency.TotalSeconds));
			}
		}

		internal void Arm()
		{
			lock (sync)
			{
				frames.Arm(Clock);
			}
		}

		internal void Disarm()
		{
			lock (sync)
			{
				frames.Disarm();
				Amplitude = 0f;
			}
		}

		public void Reset()
		{
			lock (sync)
			{
				frames.Arm(Clock);
				Amplitude = 0f;
			}
		}

		public void ReceiveMicrophoneData(ArraySegment<float> data, WaveFormat format)
		{
			if (data.Array == null || data.Count == 0 || format.SampleRate <= 0 || format.Channels <= 0)
			{
				return;
			}
			double num = 0.0;
			for (int i = data.Offset; i < data.Offset + data.Count; i++)
			{
				num += (double)Math.Abs(data.Array[i]);
			}
			lock (sync)
			{
				Amplitude = (float)(num / (double)data.Count);
				frames.Receive(Clock, (double)data.Count / (double)format.SampleRate / (double)format.Channels, latency, Amplitude);
			}
		}

		internal bool Violates(float threshold)
		{
			if (!Object.op_Implicit((Object)(object)comms) || !((Behaviour)comms).isActiveAndEnabled || capture == null || !capture.IsRecording || string.IsNullOrEmpty(comms.LocalPlayerName))
			{
				return false;
			}
			VoicePlayerState val = comms.FindPlayer(comms.LocalPlayerName);
			if (val == null || !val.IsLocalPlayer)
			{
				return false;
			}
			lock (sync)
			{
				return frames.Consume(Clock, active, threshold);
			}
		}
	}
}