Decompiled source of BlackBox v0.1.1

BlackBox.dll

Decompiled 37 minutes ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using System.Text;
using System.Threading;
using BepInEx;
using BepInEx.Bootstrap;
using BepInEx.Configuration;
using BepInEx.Logging;
using Dissonance;
using Dissonance.Audio.Capture;
using GameNetcodeStuff;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using ModMenu;
using NAudio.Wave;
using TMPro;
using Unity.Collections;
using Unity.Netcode;
using UnityEngine;
using UnityEngine.Experimental.Rendering;
using UnityEngine.Rendering;
using UnityEngine.Rendering.HighDefinition;
using UnityEngine.UI;

[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("BlackBox")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("0.1.1.0")]
[assembly: AssemblyInformationalVersion("0.1.1+7580bc23ef579f194089ab813cc8dfc65404bab2")]
[assembly: AssemblyProduct("BlackBox")]
[assembly: AssemblyTitle("BlackBox")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.1.1.0")]
[module: UnverifiableCode]
[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 BlackBox
{
	internal static class Coroner
	{
		private static bool _looked;

		private static MethodInfo _get;

		private static MethodInfo _str;

		public static string TryGetCause(int playerIndex)
		{
			try
			{
				if (!_looked)
				{
					_looked = true;
					Type type = null;
					Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
					foreach (Assembly assembly in assemblies)
					{
						if (assembly.GetName().Name == "Coroner")
						{
							type = assembly.GetType("Coroner.API");
							break;
						}
					}
					if (type == null)
					{
						return null;
					}
					MethodInfo[] methods = type.GetMethods(BindingFlags.Static | BindingFlags.Public);
					foreach (MethodInfo methodInfo in methods)
					{
						ParameterInfo[] parameters = methodInfo.GetParameters();
						if (methodInfo.Name == "GetCauseOfDeath" && parameters.Length == 1 && parameters[0].ParameterType == typeof(int))
						{
							_get = methodInfo;
						}
						if (methodInfo.Name == "StringifyCauseOfDeath" && parameters.Length == 2)
						{
							_str = methodInfo;
						}
					}
				}
				if (_get == null || _str == null)
				{
					return null;
				}
				object obj = _get.Invoke(null, new object[1] { playerIndex });
				if (obj == null)
				{
					return null;
				}
				return _str.Invoke(null, new object[2]
				{
					obj,
					new Random(playerIndex)
				}) as string;
			}
			catch (Exception ex)
			{
				Plugin.Debug("Coroner lookup failed: " + ex.Message);
				_get = null;
				return null;
			}
		}
	}
	internal sealed class MediaTrack
	{
		public const int AudioRate = 16000;

		public int Width;

		public int Height;

		public readonly List<float> FrameTimes = new List<float>();

		public readonly List<byte[]> Frames = new List<byte[]>();

		public float AudioStart;

		public int AudioSamples;

		public byte[] Adpcm = new byte[0];

		public byte[] Serialize()
		{
			using MemoryStream memoryStream = new MemoryStream();
			using BinaryWriter binaryWriter = new BinaryWriter(memoryStream);
			binaryWriter.Write(827867714);
			binaryWriter.Write(Width);
			binaryWriter.Write(Height);
			binaryWriter.Write(Frames.Count);
			for (int i = 0; i < Frames.Count; i++)
			{
				binaryWriter.Write(FrameTimes[i]);
				binaryWriter.Write(Frames[i].Length);
				binaryWriter.Write(Frames[i]);
			}
			binaryWriter.Write(AudioStart);
			binaryWriter.Write(AudioSamples);
			binaryWriter.Write(Adpcm.Length);
			binaryWriter.Write(Adpcm);
			return memoryStream.ToArray();
		}

		public static MediaTrack Deserialize(byte[] data)
		{
			try
			{
				MemoryStream ms = new MemoryStream(data);
				try
				{
					using BinaryReader binaryReader = new BinaryReader(ms);
					if (binaryReader.ReadInt32() != 827867714)
					{
						return null;
					}
					MediaTrack mediaTrack = new MediaTrack
					{
						Width = binaryReader.ReadInt32(),
						Height = binaryReader.ReadInt32()
					};
					if (mediaTrack.Width < 16 || mediaTrack.Width > 1024 || mediaTrack.Height < 16 || mediaTrack.Height > 1024)
					{
						return null;
					}
					int num = binaryReader.ReadInt32();
					if (num < 0 || num > 2000)
					{
						return null;
					}
					for (int i = 0; i < num; i++)
					{
						float num2 = binaryReader.ReadSingle();
						int num3 = binaryReader.ReadInt32();
						if (num3 < 0 || num3 > Left() || float.IsNaN(num2) || num2 < -120f || num2 > 10f)
						{
							return null;
						}
						mediaTrack.FrameTimes.Add(num2);
						mediaTrack.Frames.Add(binaryReader.ReadBytes(num3));
					}
					mediaTrack.AudioStart = binaryReader.ReadSingle();
					mediaTrack.AudioSamples = binaryReader.ReadInt32();
					int num4 = binaryReader.ReadInt32();
					if (num4 < 0 || num4 > Left())
					{
						return null;
					}
					if (mediaTrack.AudioSamples < 0 || mediaTrack.AudioSamples > 640000 || mediaTrack.AudioSamples > (long)num4 * 2L + 2)
					{
						return null;
					}
					if (float.IsNaN(mediaTrack.AudioStart) || mediaTrack.AudioStart < -120f || mediaTrack.AudioStart > 10f)
					{
						return null;
					}
					mediaTrack.Adpcm = binaryReader.ReadBytes(num4);
					return mediaTrack;
				}
				finally
				{
					if (ms != null)
					{
						((IDisposable)ms).Dispose();
					}
				}
				long Left()
				{
					return ms.Length - ms.Position;
				}
			}
			catch (Exception ex)
			{
				Plugin.Log.LogWarning((object)("Black Box: bad media data: " + ex.Message));
				return null;
			}
		}

		public AudioClip MakeClip()
		{
			if (AudioSamples <= 0)
			{
				return null;
			}
			float[] array = Adpcm4.Decode(Adpcm, AudioSamples);
			AudioClip obj = AudioClip.Create("BlackBoxTape", AudioSamples, 1, 16000, false);
			obj.SetData(array, 0);
			return obj;
		}
	}
	internal static class MediaRecorder
	{
		private sealed class Frame
		{
			public float Time;

			public byte[] Jpg;
		}

		private static readonly LinkedList<Frame> _frames = new LinkedList<Frame>();

		private static readonly object _lock = new object();

		private static RenderTexture _small;

		private static float _nextFrame;

		private static int _inFlight;

		private static AudioTap _tap;

		private static VoiceTap _voice;

		private static readonly Dictionary<int, MediaTrack> _local = new Dictionary<int, MediaTrack>();

		private static int _sealKey = -1;

		private static float _sealAt;

		private static float _deathTime;

		private static bool High => Plugin.VideoQuality.Value == "High";

		private static int W
		{
			get
			{
				if (!High)
				{
					return 320;
				}
				return 480;
			}
		}

		private static int H
		{
			get
			{
				if (!High)
				{
					return 192;
				}
				return 288;
			}
		}

		private static int Fps
		{
			get
			{
				if (!High)
				{
					return 15;
				}
				return 20;
			}
		}

		private static float Keep => Plugin.TapeSeconds.Value + 1f;

		public static MediaTrack TakeLocal(int key)
		{
			if (!_local.TryGetValue(key, out var value))
			{
				return null;
			}
			_local.Remove(key);
			return value;
		}

		public static void OnLocalDeath(int victimIndex, int deathCount)
		{
			_sealKey = victimIndex * 16 + (deathCount & 0xF);
			_deathTime = Time.time;
			_sealAt = Time.time + 0.4f;
		}

		public static void Tick()
		{
			//IL_0198: Unknown result type (might be due to invalid IL or missing references)
			//IL_019d: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ad: Expected O, but got Unknown
			//IL_0203: Unknown result type (might be due to invalid IL or missing references)
			PlayerControllerB val = (((Object)(object)GameNetworkManager.Instance != (Object)null) ? GameNetworkManager.Instance.localPlayerController : null);
			if (!Recorder.Active || (Object)(object)val == (Object)null)
			{
				lock (_lock)
				{
					_frames.Clear();
				}
				AudioTap.Detach(ref _tap);
				VoiceTap.Detach(ref _voice);
				_sealKey = -1;
				return;
			}
			if (_sealKey >= 0 && Time.time >= _sealAt)
			{
				SealLocal();
				_sealKey = -1;
			}
			if (val.isPlayerDead)
			{
				return;
			}
			if (Plugin.RecordMyAudio.Value)
			{
				AudioTap.Attach(ref _tap);
			}
			else
			{
				AudioTap.Detach(ref _tap);
			}
			if (Plugin.RecordMyVoice.Value)
			{
				VoiceTap.Attach(ref _voice);
			}
			else
			{
				VoiceTap.Detach(ref _voice);
			}
			if (!Plugin.RecordMyView.Value || Time.time < _nextFrame || _inFlight > 3 || !SystemInfo.supportsAsyncGPUReadback)
			{
				return;
			}
			Camera gameplayCamera = val.gameplayCamera;
			RenderTexture val2 = (((Object)(object)gameplayCamera != (Object)null) ? gameplayCamera.targetTexture : null);
			if ((Object)(object)val2 == (Object)null)
			{
				return;
			}
			_nextFrame = Time.time + 1f / (float)Fps;
			if ((Object)(object)_small == (Object)null || ((Texture)_small).width != W || ((Texture)_small).height != H)
			{
				if ((Object)(object)_small != (Object)null)
				{
					_small.Release();
				}
				_small = new RenderTexture(W, H, 0, (RenderTextureFormat)0)
				{
					name = "BlackBoxGrab"
				};
				_small.Create();
			}
			Graphics.Blit((Texture)(object)val2, _small);
			float t = Time.time;
			int w = W;
			int h = H;
			_inFlight++;
			AsyncGPUReadback.Request((Texture)(object)_small, 0, (TextureFormat)4, (Action<AsyncGPUReadbackRequest>)delegate(AsyncGPUReadbackRequest req)
			{
				//IL_0027: Unknown result type (might be due to invalid IL or missing references)
				//IL_002c: Unknown result type (might be due to invalid IL or missing references)
				_inFlight--;
				if (!((AsyncGPUReadbackRequest)(ref req)).hasError)
				{
					byte[] raw = ((AsyncGPUReadbackRequest)(ref req)).GetData<byte>(0).ToArray();
					ThreadPool.QueueUserWorkItem(delegate
					{
						try
						{
							byte[] jpg = ImageConversion.EncodeArrayToJPG((Array)raw, (GraphicsFormat)8, (uint)w, (uint)h, 0u, 55);
							lock (_lock)
							{
								LinkedListNode<Frame> linkedListNode = _frames.Last;
								while (linkedListNode != null && linkedListNode.Value.Time > t)
								{
									linkedListNode = linkedListNode.Previous;
								}
								if (linkedListNode == null)
								{
									_frames.AddFirst(new Frame
									{
										Time = t,
										Jpg = jpg
									});
								}
								else
								{
									_frames.AddAfter(linkedListNode, new Frame
									{
										Time = t,
										Jpg = jpg
									});
								}
								while (_frames.Count > 0 && _frames.First.Value.Time < t - Keep)
								{
									_frames.RemoveFirst();
								}
							}
						}
						catch (Exception ex)
						{
							Plugin.Debug("JPEG encode failed: " + ex.Message);
						}
					});
				}
			});
		}

		private static void SealLocal()
		{
			int sealKey = _sealKey;
			if (!Plugin.RecordMyView.Value && !Plugin.RecordMyAudio.Value && !Plugin.RecordMyVoice.Value)
			{
				TapeStore.MarkUnavailable(sealKey);
				Net.SendMediaNone(sealKey);
				return;
			}
			float value = Plugin.TapeSeconds.Value;
			MediaTrack mediaTrack = new MediaTrack
			{
				Width = W,
				Height = H
			};
			lock (_lock)
			{
				foreach (Frame frame in _frames)
				{
					float num = frame.Time - _deathTime;
					if (!(num < 0f - value) && !(num > 0.05f))
					{
						mediaTrack.FrameTimes.Add(num);
						mediaTrack.Frames.Add(frame.Jpg);
					}
				}
				_frames.Clear();
			}
			float[] array = (((Object)(object)_tap != (Object)null) ? _tap.Take(value, Time.time - _deathTime) : null);
			float[] array2 = ((_voice != null) ? _voice.Take(value, Time.time - _deathTime) : null);
			int num2 = Math.Max((array != null) ? array.Length : 0, (array2 != null) ? array2.Length : 0);
			if (num2 > 0)
			{
				float[] array3 = new float[num2];
				for (int i = 0; i < num2; i++)
				{
					float num3 = ((array != null && i < array.Length) ? array[i] : 0f) + ((array2 != null && i < array2.Length) ? (array2[i] * 0.9f) : 0f);
					array3[i] = Mathf.Clamp(num3, -1f, 1f);
				}
				mediaTrack.AudioSamples = num2;
				mediaTrack.AudioStart = (float)(-num2) / 16000f;
				mediaTrack.Adpcm = Adpcm4.Encode(array3);
			}
			if (mediaTrack.Frames.Count == 0 && mediaTrack.AudioSamples == 0)
			{
				TapeStore.MarkUnavailable(sealKey);
				Net.SendMediaNone(sealKey);
				return;
			}
			Tape tape = TapeStore.Get(sealKey);
			if (tape != null)
			{
				tape.Media = mediaTrack;
			}
			else
			{
				_local[sealKey] = mediaTrack;
			}
			byte[] array4 = mediaTrack.Serialize();
			Plugin.Debug($"MediaRecorder: sealed {sealKey}: {mediaTrack.Frames.Count} frames, {mediaTrack.AudioSamples} samples, {array4.Length} bytes.");
			Net.SendMedia(sealKey, array4);
		}
	}
	internal sealed class AudioTap : MonoBehaviour
	{
		private float[] _ring;

		private long _written;

		private int _outRate;

		private double _acc;

		private float _sum;

		private int _sumN;

		private readonly object _l = new object();

		public static void Attach(ref AudioTap tap)
		{
			AudioListener val = (((Object)(object)StartOfRound.Instance != (Object)null) ? StartOfRound.Instance.audioListener : null);
			if (!((Object)(object)val == (Object)null) && (!((Object)(object)tap != (Object)null) || !((Object)(object)((Component)tap).gameObject == (Object)(object)((Component)val).gameObject)))
			{
				Detach(ref tap);
				tap = ((Component)val).gameObject.AddComponent<AudioTap>();
			}
		}

		public static void Detach(ref AudioTap tap)
		{
			if ((Object)(object)tap != (Object)null)
			{
				Object.Destroy((Object)(object)tap);
			}
			tap = null;
		}

		private void Awake()
		{
			_outRate = AudioSettings.outputSampleRate;
			_ring = new float[640000];
		}

		private void OnAudioFilterRead(float[] data, int channels)
		{
			double num = 16000.0 / (double)_outRate;
			lock (_l)
			{
				for (int i = 0; i < data.Length; i += channels)
				{
					float num2 = 0f;
					for (int j = 0; j < channels; j++)
					{
						num2 += data[i + j];
					}
					_sum += num2 / (float)channels;
					_sumN++;
					_acc += num;
					if (_acc >= 1.0)
					{
						_acc -= 1.0;
						_ring[_written % _ring.Length] = _sum / (float)_sumN;
						_written++;
						_sum = 0f;
						_sumN = 0;
					}
				}
			}
		}

		public float[] Take(float before, float sinceDeath)
		{
			lock (_l)
			{
				long num = _written - (long)(sinceDeath * 16000f);
				long num2 = Math.Max(Math.Max(0L, num - (long)(before * 16000f)), _written - _ring.Length);
				if (num <= num2)
				{
					return null;
				}
				float[] array = new float[num - num2];
				for (long num3 = num2; num3 < num; num3++)
				{
					array[num3 - num2] = _ring[num3 % _ring.Length];
				}
				return array;
			}
		}
	}
	internal sealed class VoiceTap : IMicrophoneSubscriber
	{
		private readonly float[] _ring = new float[640000];

		private long _written;

		private readonly object _l = new object();

		private double _acc;

		private volatile bool _speaking;

		private long _lastFill;

		private float _lastFillTime;

		private DissonanceComms _comms;

		public static void Attach(ref VoiceTap tap)
		{
			DissonanceComms val = (((Object)(object)StartOfRound.Instance != (Object)null) ? StartOfRound.Instance.voiceChatModule : null);
			if (!((Object)(object)val == (Object)null))
			{
				if (tap != null && (Object)(object)tap._comms == (Object)(object)val)
				{
					tap.Update();
					return;
				}
				Detach(ref tap);
				tap = new VoiceTap
				{
					_comms = val,
					_lastFillTime = Time.unscaledTime
				};
				val.SubscribeToRecordedAudio((IMicrophoneSubscriber)(object)tap);
			}
		}

		public static void Detach(ref VoiceTap tap)
		{
			if (tap != null && (Object)(object)tap._comms != (Object)null)
			{
				try
				{
					tap._comms.UnsubscribeFromRecordedAudio((IMicrophoneSubscriber)(object)tap);
				}
				catch
				{
				}
			}
			tap = null;
		}

		private void Update()
		{
			VoicePlayerState val = _comms.FindPlayer(_comms.LocalPlayerName);
			_speaking = val != null && val.IsSpeaking && !_comms.IsMuted;
			float unscaledTime = Time.unscaledTime;
			long num = (long)((unscaledTime - _lastFillTime) * 16000f);
			lock (_l)
			{
				for (long num2 = _written - _lastFill; num2 < num; num2++)
				{
					_ring[_written++ % _ring.Length] = 0f;
				}
				_lastFill = _written;
				_lastFillTime = unscaledTime;
			}
		}

		public void ReceiveMicrophoneData(ArraySegment<float> buffer, WaveFormat format)
		{
			double num = 16000.0 / (double)format.SampleRate;
			int num2 = Math.Max(1, format.Channels);
			bool speaking = _speaking;
			lock (_l)
			{
				for (int i = buffer.Offset; i + num2 <= buffer.Offset + buffer.Count; i += num2)
				{
					_acc += num;
					if (!(_acc < 1.0))
					{
						_acc -= 1.0;
						_ring[_written++ % _ring.Length] = (speaking ? buffer.Array[i] : 0f);
					}
				}
			}
		}

		public void Reset()
		{
		}

		public float[] Take(float before, float sinceDeath)
		{
			lock (_l)
			{
				long num = _written - (long)(sinceDeath * 16000f);
				long num2 = Math.Max(Math.Max(0L, num - (long)(before * 16000f)), _written - _ring.Length);
				if (num <= num2)
				{
					return null;
				}
				float[] array = new float[num - num2];
				for (long num3 = num2; num3 < num; num3++)
				{
					array[num3 - num2] = _ring[num3 % _ring.Length];
				}
				return array;
			}
		}
	}
	internal static class Adpcm4
	{
		private static readonly int[] Steps = new int[89]
		{
			7, 8, 9, 10, 11, 12, 13, 14, 16, 17,
			19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
			50, 55, 60, 66, 73, 80, 88, 97, 107, 118,
			130, 143, 157, 173, 190, 209, 230, 253, 279, 307,
			337, 371, 408, 449, 494, 544, 598, 658, 724, 796,
			876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066,
			2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358,
			5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
			15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
		};

		private static readonly int[] Index = new int[16]
		{
			-1, -1, -1, -1, 2, 4, 6, 8, -1, -1,
			-1, -1, 2, 4, 6, 8
		};

		public static byte[] Encode(float[] pcm)
		{
			byte[] array = new byte[(pcm.Length + 1) / 2];
			int num = 0;
			int num2 = 0;
			for (int i = 0; i < pcm.Length; i++)
			{
				int num3 = (int)(Mathf.Clamp(pcm[i], -1f, 1f) * 32767f);
				int num4 = Steps[num2];
				int num5 = num3 - num;
				int num6 = 0;
				if (num5 < 0)
				{
					num6 = 8;
					num5 = -num5;
				}
				int num7 = num4 >> 3;
				if (num5 >= num4)
				{
					num6 |= 4;
					num5 -= num4;
					num7 += num4;
				}
				num4 >>= 1;
				if (num5 >= num4)
				{
					num6 |= 2;
					num5 -= num4;
					num7 += num4;
				}
				num4 >>= 1;
				if (num5 >= num4)
				{
					num6 |= 1;
					num7 += num4;
				}
				num += (((num6 & 8) != 0) ? (-num7) : num7);
				num = Mathf.Clamp(num, -32768, 32767);
				num2 = Mathf.Clamp(num2 + Index[num6], 0, 88);
				if ((i & 1) == 0)
				{
					array[i >> 1] = (byte)num6;
				}
				else
				{
					array[i >> 1] |= (byte)(num6 << 4);
				}
			}
			return array;
		}

		public static float[] Decode(byte[] data, int samples)
		{
			float[] array = new float[samples];
			int num = 0;
			int num2 = 0;
			for (int i = 0; i < samples && i >> 1 < data.Length; i++)
			{
				int num3 = (((i & 1) == 0) ? (data[i >> 1] & 0xF) : (data[i >> 1] >> 4));
				int num4 = Steps[num2];
				int num5 = num4 >> 3;
				if ((num3 & 4) != 0)
				{
					num5 += num4;
				}
				if ((num3 & 2) != 0)
				{
					num5 += num4 >> 1;
				}
				if ((num3 & 1) != 0)
				{
					num5 += num4 >> 2;
				}
				num += (((num3 & 8) != 0) ? (-num5) : num5);
				num = Mathf.Clamp(num, -32768, 32767);
				num2 = Mathf.Clamp(num2 + Index[num3], 0, 88);
				array[i] = (float)num / 32768f;
			}
			return array;
		}
	}
	internal static class MenuHooks
	{
		internal const string ModMenuGuid = "daniel.lethalcompany.modmenu";

		private const string Tab = "Black Box";

		[MethodImpl(MethodImplOptions.NoInlining)]
		internal static void Register()
		{
			MenuApi.Button("Black Box", "Play newest tape", (Func<string>)delegate
			{
				Tape newest = TapeStore.Newest;
				if (newest == null)
				{
					return (string)null;
				}
				Net.RequestPlay(newest.Key);
				return "played " + newest.VictimName + "'s tape on the monitor";
			}, false, false, (Func<string>)delegate
			{
				object obj = NotInGame();
				if (obj == null)
				{
					if (TapeStore.Newest != null)
					{
						return (string)null;
					}
					obj = "no tapes yet (a crewmate has to die first)";
				}
				return (string)obj;
			}, "on the ship's main monitor, like BLACKBOX on the terminal");
			MenuApi.Button("Black Box", "Stop the tape", (Func<string>)delegate
			{
				Net.RequestStop();
				return "stopped the tape";
			}, false, false, (Func<string>)delegate
			{
				object obj = NotInGame();
				if (obj == null)
				{
					if (MonitorView.Replaying)
					{
						return (string)null;
					}
					obj = "no tape playing";
				}
				return (string)obj;
			}, (string)null);
			MenuApi.Button("Black Box", "Next layout", (Func<string>)delegate
			{
				Layout layout = MonitorView.NextLayout();
				Net.RequestLayout(layout);
				return $"monitor layout {layout}";
			}, false, false, (Func<string>)delegate
			{
				object obj = NotInGame();
				if (obj == null)
				{
					if (MonitorView.Replaying)
					{
						return (string)null;
					}
					obj = "no tape playing";
				}
				return (string)obj;
			}, "split, map or video");
			MenuApi.Toggle("Black Box", "Auto play tapes", (Func<bool>)(() => Plugin.AutoPlay.Value), (Func<bool, string>)delegate(bool on)
			{
				Plugin.AutoPlay.Value = on;
				return (!on) ? "tapes wait for BLACKBOX" : "tapes play by themselves when ready";
			}, true, false, (Func<string>)null, "host: a tape plays on the monitor as soon as it's ready");
		}

		private static string NotInGame()
		{
			if (!((Object)(object)StartOfRound.Instance == (Object)null) && !((Object)(object)StartOfRound.Instance.localPlayerController == (Object)null))
			{
				return null;
			}
			return "not in a game";
		}
	}
	internal static class MonitorView
	{
		private enum Mode
		{
			Off,
			Alert,
			Replay
		}

		private sealed class Icon
		{
			public RectTransform Root;

			public Image Dot;

			public Image Arrow;

			public Image Ring;

			public TextMeshProUGUI Label;

			public UiLine Trail;

			public bool Used;
		}

		private static Mode _mode;

		private static Tape _tape;

		private static float _startReal;

		private static float _holdUntil;

		private static Layout _layout;

		private static Tape _alertTape;

		private static float _alertUntil;

		public static Vector3 FocusPos;

		public static bool FocusInside;

		private static GameObject _root;

		private static Camera _uiCam;

		private static RenderTexture _rt;

		private static RectTransform _canvasRt;

		private static RectTransform _header;

		private static RectTransform _footer;

		private static RectTransform _mapPanel;

		private static RectTransform _videoPanel;

		private static RectTransform _audioPanel;

		private static RectTransform _overlay;

		private static RectTransform _banner;

		private static RawImage _mapImg;

		private static RawImage _videoImg;

		private static TextMeshProUGUI _title;

		private static TextMeshProUGUI _who;

		private static TextMeshProUGUI _tapeNo;

		private static TextMeshProUGUI _videoLabel;

		private static TextMeshProUGUI _videoTime;

		private static TextMeshProUGUI _videoStatus;

		private static TextMeshProUGUI _tStart;

		private static TextMeshProUGUI _tEnd;

		private static TextMeshProUGUI _hint;

		private static TextMeshProUGUI _bannerText;

		private static TextMeshProUGUI _mapLabel;

		private static Image _bar;

		private static Image _barFill;

		private static Image _playhead;

		private static Image _recDot;

		private static Image _bannerDot;

		private static readonly List<Image> _ticks = new List<Image>();

		private static UiLine _wave;

		private static Texture2D _videoTex;

		private static Texture2D _noiseTex;

		private static int _videoFrame = -1;

		private static AudioSource _speaker;

		private static AudioClip _clip;

		private static bool _audioStarted;

		private static MediaTrack _audioFor;

		private static MeshRenderer _mesh;

		private static Material[] _origMats;

		private static Material _swapMat;

		private static readonly Dictionary<int, Icon> _icons = new Dictionary<int, Icon>();

		private static readonly List<TextMeshProUGUI> _eventLabels = new List<TextMeshProUGUI>();

		private static readonly Color Red = new Color(1f, 0.35f, 0.3f);

		private static readonly Color Blue = new Color(0.35f, 0.75f, 1f);

		private static readonly Color Amber = new Color(0.95f, 0.75f, 0.25f);

		private static readonly Color Grey = new Color(0.6f, 0.6f, 0.6f);

		private static readonly Color Green = new Color(0.45f, 0.75f, 0.55f);

		private static Rect _uv;

		public static bool Replaying
		{
			get
			{
				if (_mode == Mode.Replay)
				{
					return _tape != null;
				}
				return false;
			}
		}

		public static Layout CurrentLayout => _layout;

		private static ManualCameraRenderer MapScreen
		{
			get
			{
				if (!((Object)(object)StartOfRound.Instance != (Object)null))
				{
					return null;
				}
				return StartOfRound.Instance.mapScreen;
			}
		}

		public static RenderTexture Picture => _rt;

		private static float Playhead => 0f - _tape.Before + (Time.time - _startReal);

		public static void OnTapeSealed(Tape tape)
		{
			if (!Replaying)
			{
				_alertTape = tape;
				_alertUntil = Time.time + 10f;
			}
		}

		public static void StartReplay(int key, Layout layout)
		{
			Tape tape = TapeStore.Get(key);
			if (tape == null)
			{
				Plugin.Log.LogInfo((object)$"Black Box: no tape {key} on this client yet.");
			}
			else
			{
				if ((Object)(object)MapScreen == (Object)null)
				{
					return;
				}
				Build();
				_tape = tape;
				_mode = Mode.Replay;
				_startReal = Time.time;
				_holdUntil = 0f;
				_layout = layout;
				_videoFrame = -1;
				_audioStarted = false;
				_audioFor = null;
				_alertTape = null;
				foreach (Icon value in _icons.Values)
				{
					((Component)value.Root).gameObject.SetActive(false);
				}
				MapPatch.BeginReplay(MapScreen);
				ApplyLayout();
				Plugin.Debug($"MonitorView: replay {key} ({tape.VictimName}), layout {layout}.");
			}
		}

		public static void StopReplay(bool keepAlertOff)
		{
			if (_mode == Mode.Replay)
			{
				MapPatch.EndReplay();
			}
			if ((Object)(object)_speaker != (Object)null)
			{
				_speaker.Stop();
			}
			_mode = Mode.Off;
			_tape = null;
			if (keepAlertOff)
			{
				_alertTape = null;
			}
			RestoreMonitor();
		}

		public static void SetLayout(Layout l)
		{
			_layout = l;
			if (_mode == Mode.Replay)
			{
				ApplyLayout();
			}
		}

		public static Layout NextLayout()
		{
			if (_tape == null || _tape.MediaUnavailable)
			{
				return Layout.Map;
			}
			return (Layout)((int)(_layout + 1) % 3);
		}

		public static void Tick()
		{
			//IL_0030: Unknown result type (might be due to invalid IL or missing references)
			//IL_0035: Unknown result type (might be due to invalid IL or missing references)
			if (_mode == Mode.Replay && _tape != null)
			{
				float playhead = Playhead;
				if (Sample(_tape, _tape.VictimId, playhead, out var st))
				{
					FocusPos = st.Pos;
					FocusInside = (st.Flags & 1) != 0;
				}
			}
		}

		public static void LateTick()
		{
			ManualCameraRenderer mapScreen = MapScreen;
			if ((Object)(object)mapScreen == (Object)null || StartOfRound.Instance.inShipPhase)
			{
				if (_mode != Mode.Off)
				{
					StopReplay(keepAlertOff: true);
				}
				return;
			}
			if (_mode == Mode.Replay)
			{
				UpdateReplay();
				ShowOnMonitor();
			}
			else if (_alertTape != null && Time.time < _alertUntil && MapPatch.ScreenOn.Invoke(mapScreen))
			{
				Build();
				if (_mode != Mode.Alert)
				{
					_mode = Mode.Alert;
					ApplyLayout();
				}
				UpdateAlert();
				ShowOnMonitor();
			}
			else if (_mode == Mode.Alert)
			{
				_mode = Mode.Off;
				_alertTape = null;
				RestoreMonitor();
			}
			if ((Object)(object)_uiCam != (Object)null)
			{
				((Behaviour)_uiCam).enabled = _mode != Mode.Off;
			}
		}

		private static void Build()
		{
			//IL_0068: Unknown result type (might be due to invalid IL or missing references)
			//IL_006d: Unknown result type (might be due to invalid IL or missing references)
			//IL_007d: Expected O, but got Unknown
			//IL_008d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0097: Expected O, but got Unknown
			//IL_00c5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d9: 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)
			//IL_014e: Unknown result type (might be due to invalid IL or missing references)
			//IL_017c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0183: Unknown result type (might be due to invalid IL or missing references)
			//IL_0188: Unknown result type (might be due to invalid IL or missing references)
			//IL_0190: Unknown result type (might be due to invalid IL or missing references)
			//IL_0195: Unknown result type (might be due to invalid IL or missing references)
			//IL_01bf: Unknown result type (might be due to invalid IL or missing references)
			//IL_01cd: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d5: Expected I4, but got Unknown
			//IL_01dc: Unknown result type (might be due to invalid IL or missing references)
			//IL_020a: Unknown result type (might be due to invalid IL or missing references)
			//IL_020f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0216: Unknown result type (might be due to invalid IL or missing references)
			//IL_022c: Unknown result type (might be due to invalid IL or missing references)
			//IL_024e: Unknown result type (might be due to invalid IL or missing references)
			//IL_025f: Unknown result type (might be due to invalid IL or missing references)
			//IL_026e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0278: Expected O, but got Unknown
			//IL_0282: Unknown result type (might be due to invalid IL or missing references)
			//IL_02d7: Unknown result type (might be due to invalid IL or missing references)
			//IL_0383: Unknown result type (might be due to invalid IL or missing references)
			//IL_03f6: Unknown result type (might be due to invalid IL or missing references)
			//IL_047a: Unknown result type (might be due to invalid IL or missing references)
			//IL_04ca: Unknown result type (might be due to invalid IL or missing references)
			//IL_0510: Unknown result type (might be due to invalid IL or missing references)
			//IL_0556: Unknown result type (might be due to invalid IL or missing references)
			//IL_05ba: Unknown result type (might be due to invalid IL or missing references)
			//IL_05f1: Unknown result type (might be due to invalid IL or missing references)
			//IL_0653: Unknown result type (might be due to invalid IL or missing references)
			//IL_0680: Unknown result type (might be due to invalid IL or missing references)
			//IL_0685: Unknown result type (might be due to invalid IL or missing references)
			//IL_06b3: Unknown result type (might be due to invalid IL or missing references)
			//IL_06f4: Unknown result type (might be due to invalid IL or missing references)
			//IL_0736: Unknown result type (might be due to invalid IL or missing references)
			//IL_079a: Unknown result type (might be due to invalid IL or missing references)
			//IL_07ef: Unknown result type (might be due to invalid IL or missing references)
			//IL_0844: Unknown result type (might be due to invalid IL or missing references)
			//IL_087b: Unknown result type (might be due to invalid IL or missing references)
			//IL_08c1: Unknown result type (might be due to invalid IL or missing references)
			//IL_0911: Unknown result type (might be due to invalid IL or missing references)
			//IL_0952: Unknown result type (might be due to invalid IL or missing references)
			//IL_0993: Unknown result type (might be due to invalid IL or missing references)
			//IL_09c5: Unknown result type (might be due to invalid IL or missing references)
			//IL_09de: Unknown result type (might be due to invalid IL or missing references)
			//IL_0a65: Unknown result type (might be due to invalid IL or missing references)
			//IL_0aa6: Unknown result type (might be due to invalid IL or missing references)
			//IL_0af6: Unknown result type (might be due to invalid IL or missing references)
			//IL_0b40: Unknown result type (might be due to invalid IL or missing references)
			//IL_0b45: Unknown result type (might be due to invalid IL or missing references)
			//IL_0b51: Expected O, but got Unknown
			//IL_0b57: Unknown result type (might be due to invalid IL or missing references)
			//IL_0b5c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0b68: Expected O, but got Unknown
			if (!((Object)(object)_root != (Object)null))
			{
				RenderTexture val = (((Object)(object)MapScreen.mapCamera != (Object)null) ? MapScreen.mapCamera.targetTexture : null);
				float num = (((Object)(object)val != (Object)null) ? ((float)((Texture)val).width / (float)((Texture)val).height) : 1.6f);
				int num2 = 1024;
				int num3 = Mathf.RoundToInt((float)num2 / num);
				_rt = new RenderTexture(num2, num3, 16, (RenderTextureFormat)0)
				{
					name = "BlackBoxMonitor"
				};
				_rt.Create();
				_root = new GameObject("BlackBox.Monitor");
				Object.DontDestroyOnLoad((Object)(object)_root);
				_root.layer = 5;
				_root.transform.position = new Vector3(0f, -4000f, 0f);
				GameObject val2 = new GameObject("BlackBox.UICam");
				val2.transform.SetParent(_root.transform, false);
				_uiCam = val2.AddComponent<Camera>();
				_uiCam.orthographic = true;
				_uiCam.orthographicSize = 5f;
				_uiCam.nearClipPlane = 0.1f;
				_uiCam.farClipPlane = 20f;
				_uiCam.cullingMask = 32;
				_uiCam.clearFlags = (CameraClearFlags)2;
				_uiCam.backgroundColor = Color.black;
				_uiCam.targetTexture = _rt;
				((Behaviour)_uiCam).enabled = false;
				HDAdditionalCameraData val3 = val2.AddComponent<HDAdditionalCameraData>();
				val3.clearColorMode = (ClearColorMode)1;
				val3.backgroundColorHDR = Color.black;
				val3.volumeLayerMask = LayerMask.op_Implicit(0);
				val3.customRenderingSettings = true;
				FrameSettingsField[] array = new FrameSettingsField[6];
				RuntimeHelpers.InitializeArray(array, (RuntimeFieldHandle)/*OpCode not supported: LdMemberToken*/);
				FrameSettingsField[] array2 = (FrameSettingsField[])(object)array;
				foreach (FrameSettingsField val4 in array2)
				{
					((BitArray128)(ref val3.renderingPathCustomFrameSettingsOverrideMask.mask))[(uint)(int)val4] = true;
					((FrameSettings)(ref val3.renderingPathCustomFrameSettings)).SetEnabled(val4, false);
				}
				GameObject val5 = new GameObject("BlackBox.Canvas", new Type[1] { typeof(RectTransform) })
				{
					layer = 5
				};
				val5.transform.SetParent(_root.transform, false);
				Canvas obj = val5.AddComponent<Canvas>();
				obj.renderMode = (RenderMode)1;
				obj.worldCamera = _uiCam;
				obj.planeDistance = 5f;
				CanvasScaler obj2 = val5.AddComponent<CanvasScaler>();
				obj2.uiScaleMode = (ScaleMode)1;
				obj2.referenceResolution = new Vector2((float)num2, (float)num3);
				_canvasRt = (RectTransform)val5.transform;
				UiKit.Place(((Graphic)UiKit.Box("Bg", (Transform)(object)_canvasRt, Color.black)).rectTransform, 0f, 0f, 1f, 1f);
				_mapPanel = UiKit.Rect("Map", (Transform)(object)_canvasRt);
				UiKit.Place(((Graphic)UiKit.Box("MapBg", (Transform)(object)_mapPanel, new Color(0.02f, 0.05f, 0.04f))).rectTransform, 0f, 0f, 1f, 1f);
				RectTransform obj3 = UiKit.Rect("MapImg", (Transform)(object)_mapPanel);
				UiKit.Place(obj3, 0f, 0f, 1f, 1f);
				_mapImg = ((Component)obj3).gameObject.AddComponent<RawImage>();
				((Graphic)_mapImg).raycastTarget = false;
				_overlay = UiKit.Rect("Overlay", (Transform)(object)_mapPanel);
				UiKit.Place(_overlay, 0f, 0f, 1f, 1f);
				_mapLabel = UiKit.Text("MapLabel", (Transform)(object)_mapPanel, 16f, Green, (TextAlignmentOptions)513);
				UiKit.Place(((TMP_Text)_mapLabel).rectTransform, 0.02f, 0.92f, 1f, 1f);
				((TMP_Text)_mapLabel).text = "MAP  //  TOP-DOWN";
				_videoPanel = UiKit.Rect("Video", (Transform)(object)_canvasRt);
				UiKit.Place(((Graphic)UiKit.Box("VideoBg", (Transform)(object)_videoPanel, new Color(0.03f, 0.03f, 0.03f))).rectTransform, 0f, 0f, 1f, 1f);
				RectTransform obj4 = UiKit.Rect("VideoImg", (Transform)(object)_videoPanel);
				UiKit.Place(obj4, 0f, 0f, 1f, 1f);
				_videoImg = ((Component)obj4).gameObject.AddComponent<RawImage>();
				((Graphic)_videoImg).raycastTarget = false;
				_recDot = UiKit.Box("Rec", (Transform)(object)_videoPanel, new Color(0.9f, 0.12f, 0.12f));
				_recDot.sprite = UiKit.Circle;
				UiKit.Place(((Graphic)_recDot).rectTransform, 0.015f, 0.915f, 0.04f, 0.965f);
				_videoLabel = UiKit.Text("VideoLabel", (Transform)(object)_videoPanel, 18f, Color.white, (TextAlignmentOptions)513);
				UiKit.Place(((TMP_Text)_videoLabel).rectTransform, 0.05f, 0.9f, 0.8f, 0.98f);
				_videoTime = UiKit.Text("VideoTime", (Transform)(object)_videoPanel, 18f, Color.white, (TextAlignmentOptions)516);
				UiKit.Place(((TMP_Text)_videoTime).rectTransform, 0.6f, 0.9f, 0.98f, 0.98f);
				_videoStatus = UiKit.Text("VideoStatus", (Transform)(object)_videoPanel, 26f, Color.white, (TextAlignmentOptions)514);
				UiKit.Place(((TMP_Text)_videoStatus).rectTransform, 0f, 0.4f, 1f, 0.6f);
				_audioPanel = UiKit.Rect("Audio", (Transform)(object)_canvasRt);
				UiKit.Place(((Graphic)UiKit.Box("AudioBg", (Transform)(object)_audioPanel, new Color(0.03f, 0.05f, 0.04f))).rectTransform, 0f, 0f, 1f, 1f);
				TextMeshProUGUI obj5 = UiKit.Text("AudioLabel", (Transform)(object)_audioPanel, 13f, Green, (TextAlignmentOptions)513);
				UiKit.Place(((TMP_Text)obj5).rectTransform, 0.01f, 0.62f, 1f, 1f);
				((TMP_Text)obj5).text = "SOUND  (what he heard + his voice)";
				RectTransform obj6 = UiKit.Rect("Wave", (Transform)(object)_audioPanel);
				UiKit.Place(obj6, 0.005f, 0.05f, 0.995f, 0.65f);
				obj6.pivot = Vector2.zero;
				_wave = ((Component)obj6).gameObject.AddComponent<UiLine>();
				_wave.Width = 2f;
				_wave.Head = Green;
				((Graphic)_wave).raycastTarget = false;
				_header = UiKit.Rect("Header", (Transform)(object)_canvasRt);
				UiKit.Place(((Graphic)UiKit.Box("HeaderBg", (Transform)(object)_header, Color.black)).rectTransform, 0f, 0f, 1f, 1f);
				Image obj7 = UiKit.Box("Dot", (Transform)(object)_header, new Color(0.9f, 0.12f, 0.12f));
				obj7.sprite = UiKit.Circle;
				UiKit.Place(((Graphic)obj7).rectTransform, 0.012f, 0.25f, 0.035f, 0.75f);
				_title = UiKit.Text("Title", (Transform)(object)_header, 24f, Red, (TextAlignmentOptions)513);
				UiKit.Place(((TMP_Text)_title).rectTransform, 0.045f, 0f, 0.4f, 1f);
				((TMP_Text)_title).alignment = (TextAlignmentOptions)4097;
				((TMP_Text)_title).text = "BLACK BOX  //  REPLAY";
				_who = UiKit.Text("Who", (Transform)(object)_header, 19f, Color.white, (TextAlignmentOptions)513);
				UiKit.Place(((TMP_Text)_who).rectTransform, 0.38f, 0f, 0.86f, 1f);
				((TMP_Text)_who).alignment = (TextAlignmentOptions)4097;
				_tapeNo = UiKit.Text("TapeNo", (Transform)(object)_header, 17f, Grey, (TextAlignmentOptions)4100);
				UiKit.Place(((TMP_Text)_tapeNo).rectTransform, 0.84f, 0f, 0.985f, 1f);
				_footer = UiKit.Rect("Footer", (Transform)(object)_canvasRt);
				UiKit.Place(((Graphic)UiKit.Box("FooterBg", (Transform)(object)_footer, Color.black)).rectTransform, 0f, 0f, 1f, 1f);
				_tStart = UiKit.Text("TStart", (Transform)(object)_footer, 18f, Grey, (TextAlignmentOptions)4097);
				UiKit.Place(((TMP_Text)_tStart).rectTransform, 0.012f, 0.1f, 0.12f, 0.75f);
				_tEnd = UiKit.Text("TEnd", (Transform)(object)_footer, 18f, Grey, (TextAlignmentOptions)4100);
				UiKit.Place(((TMP_Text)_tEnd).rectTransform, 0.88f, 0.1f, 0.988f, 0.75f);
				_bar = UiKit.Box("Bar", (Transform)(object)_footer, new Color(0.3f, 0.3f, 0.3f));
				UiKit.Place(((Graphic)_bar).rectTransform, 0.13f, 0.38f, 0.87f, 0.46f);
				_barFill = UiKit.Box("Fill", (Transform)(object)((Graphic)_bar).rectTransform, Red);
				UiKit.Place(((Graphic)_barFill).rectTransform, 0f, 0f, 0f, 1f);
				_playhead = UiKit.Box("Head", (Transform)(object)((Graphic)_bar).rectTransform, Red);
				_playhead.sprite = UiKit.Circle;
				((Graphic)_playhead).rectTransform.sizeDelta = new Vector2(18f, 18f);
				_hint = UiKit.Text("Hint", (Transform)(object)_footer, 12f, Green, (TextAlignmentOptions)257);
				UiKit.Place(((TMP_Text)_hint).rectTransform, 0.13f, 0.62f, 0.87f, 1f);
				_banner = UiKit.Rect("Banner", (Transform)(object)_canvasRt);
				UiKit.Place(_banner, 0f, 0.86f, 1f, 1f);
				UiKit.Place(((Graphic)UiKit.Box("BannerBg", (Transform)(object)_banner, new Color(0f, 0f, 0f, 0.85f))).rectTransform, 0f, 0f, 1f, 1f);
				_bannerDot = UiKit.Box("BannerDot", (Transform)(object)_banner, new Color(0.9f, 0.12f, 0.12f));
				_bannerDot.sprite = UiKit.Circle;
				UiKit.Place(((Graphic)_bannerDot).rectTransform, 0.015f, 0.25f, 0.04f, 0.75f);
				_bannerText = UiKit.Text("BannerText", (Transform)(object)_banner, 22f, Red, (TextAlignmentOptions)513);
				UiKit.Place(((TMP_Text)_bannerText).rectTransform, 0.05f, 0f, 1f, 1f);
				((TMP_Text)_bannerText).alignment = (TextAlignmentOptions)4097;
				_videoTex = new Texture2D(2, 2, (TextureFormat)3, false)
				{
					filterMode = (FilterMode)1
				};
				_noiseTex = new Texture2D(96, 60, (TextureFormat)3, false)
				{
					filterMode = (FilterMode)0
				};
			}
		}

		private static void ApplyLayout()
		{
			bool flag = _mode == Mode.Alert;
			Layout layout = ((_tape == null || _tape.MediaUnavailable) ? Layout.Map : _layout);
			((Component)_header).gameObject.SetActive(!flag);
			((Component)_footer).gameObject.SetActive(!flag);
			((Component)_banner).gameObject.SetActive(flag);
			UiKit.Place(_header, 0f, 0.925f, 1f, 1f);
			UiKit.Place(_footer, 0f, 0f, 1f, 0.1f);
			if (flag)
			{
				UiKit.Place(_mapPanel, 0f, 0f, 1f, 1f);
				((Component)_mapPanel).gameObject.SetActive(true);
				((Component)_videoPanel).gameObject.SetActive(false);
				((Component)_audioPanel).gameObject.SetActive(false);
				((Component)_overlay).gameObject.SetActive(false);
				((Component)_mapLabel).gameObject.SetActive(false);
				return;
			}
			((Component)_overlay).gameObject.SetActive(true);
			((Component)_mapLabel).gameObject.SetActive(layout == Layout.Split);
			switch (layout)
			{
			case Layout.Split:
				UiKit.Place(_videoPanel, 0.012f, 0.25f, 0.615f, 0.915f);
				UiKit.Place(_audioPanel, 0.012f, 0.11f, 0.615f, 0.235f);
				UiKit.Place(_mapPanel, 0.627f, 0.11f, 0.988f, 0.915f);
				break;
			case Layout.Map:
				UiKit.Place(_mapPanel, 0.012f, 0.11f, 0.988f, 0.915f);
				break;
			case Layout.Video:
				UiKit.Place(_videoPanel, 0.012f, 0.11f, 0.988f, 0.915f);
				break;
			}
			((Component)_mapPanel).gameObject.SetActive(layout != Layout.Video);
			((Component)_videoPanel).gameObject.SetActive(layout != Layout.Map);
			((Component)_audioPanel).gameObject.SetActive(layout == Layout.Split);
			Canvas.ForceUpdateCanvases();
		}

		private static void ShowOnMonitor()
		{
			//IL_0080: Unknown result type (might be due to invalid IL or missing references)
			//IL_0085: Unknown result type (might be due to invalid IL or missing references)
			//IL_0095: Expected O, but got Unknown
			ManualCameraRenderer mapScreen = MapScreen;
			if ((Object)(object)mapScreen == (Object)null || (Object)(object)mapScreen.mesh == (Object)null || (Object)(object)_rt == (Object)null)
			{
				return;
			}
			if ((Object)(object)_mesh != (Object)(object)mapScreen.mesh)
			{
				RestoreMonitor();
				_mesh = mapScreen.mesh;
			}
			Material[] sharedMaterials = ((Renderer)_mesh).sharedMaterials;
			int num = Mathf.Clamp(mapScreen.materialIndex, 0, sharedMaterials.Length - 1);
			if ((Object)(object)_swapMat == (Object)null)
			{
				_swapMat = new Material(mapScreen.onScreenMat)
				{
					name = "BlackBoxScreen"
				};
				RenderTexture val = (((Object)(object)mapScreen.mapCamera != (Object)null) ? mapScreen.mapCamera.targetTexture : null);
				bool flag = false;
				string[] texturePropertyNames = _swapMat.GetTexturePropertyNames();
				foreach (string text in texturePropertyNames)
				{
					Texture texture = _swapMat.GetTexture(text);
					if ((Object)(object)texture != (Object)null && ((Object)(object)texture == (Object)(object)val || texture is RenderTexture))
					{
						_swapMat.SetTexture(text, (Texture)(object)_rt);
						flag = true;
					}
				}
				if (!flag)
				{
					_swapMat.mainTexture = (Texture)(object)_rt;
				}
			}
			if (!((Object)(object)sharedMaterials[num] == (Object)(object)_swapMat))
			{
				if (_origMats == null)
				{
					_origMats = (Material[])sharedMaterials.Clone();
				}
				sharedMaterials[num] = _swapMat;
				((Renderer)_mesh).sharedMaterials = sharedMaterials;
			}
		}

		private static void RestoreMonitor()
		{
			if ((Object)(object)_mesh != (Object)null && _origMats != null)
			{
				ManualCameraRenderer mapScreen = MapScreen;
				Material[] sharedMaterials = ((Renderer)_mesh).sharedMaterials;
				int num = (((Object)(object)mapScreen != (Object)null) ? Mathf.Clamp(mapScreen.materialIndex, 0, sharedMaterials.Length - 1) : 0);
				if ((Object)(object)sharedMaterials[num] == (Object)(object)_swapMat)
				{
					sharedMaterials[num] = ((!((Object)(object)mapScreen != (Object)null)) ? _origMats[num] : (MapPatch.ScreenOn.Invoke(mapScreen) ? mapScreen.onScreenMat : mapScreen.offScreenMat));
					((Renderer)_mesh).sharedMaterials = sharedMaterials;
				}
			}
			_origMats = null;
			if ((Object)(object)_uiCam != (Object)null)
			{
				((Behaviour)_uiCam).enabled = false;
			}
		}

		private static void UpdateAlert()
		{
			//IL_0045: Unknown result type (might be due to invalid IL or missing references)
			ManualCameraRenderer mapScreen = MapScreen;
			_mapImg.texture = (Texture)(object)(((Object)(object)mapScreen.mapCamera != (Object)null) ? mapScreen.mapCamera.targetTexture : null);
			_mapImg.uvRect = new Rect(0f, 0f, 1f, 1f);
			bool enabled = Mathf.Repeat(Time.time, 1f) < 0.6f;
			((Behaviour)_bannerDot).enabled = enabled;
			((TMP_Text)_bannerText).text = "BLACK BOX: " + _alertTape.Label + "   -   type BLACKBOX on the terminal";
		}

		private static void UpdateReplay()
		{
			Tape tape = _tape;
			float num = Playhead;
			float after = tape.After;
			if (num > after)
			{
				num = after;
				if (_holdUntil == 0f)
				{
					_holdUntil = Time.time + 2f;
				}
				else if (Time.time > _holdUntil)
				{
					StopReplay(keepAlertOff: true);
					return;
				}
			}
			if (tape.MediaUnavailable && _layout != Layout.Map)
			{
				_layout = Layout.Map;
				ApplyLayout();
			}
			int num2 = TapeStore.Tapes.IndexOf(tape);
			((TMP_Text)_who).text = tape.Label + "  -  " + tape.CauseText;
			((TMP_Text)_tapeNo).text = $"TAPE {num2 + 1}/{TapeStore.Tapes.Count}";
			((TMP_Text)_tStart).text = $"-{tape.Before:0.0}s";
			((TMP_Text)_tEnd).text = $"+{tape.After:0.0}s";
			((TMP_Text)_hint).text = "view: " + _layout.ToString().ToUpperInvariant() + "    switch button: change view    power button: stop";
			UpdateTimeline(tape, num);
			if (((Component)_mapPanel).gameObject.activeSelf)
			{
				UpdateMap(tape, num);
			}
			UpdateVideo(tape, num);
			UpdateAudio(tape, num);
		}

		private static void UpdateTimeline(Tape tape, float t)
		{
			//IL_002e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0059: 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_006f: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c0: 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_011d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0122: Unknown result type (might be due to invalid IL or missing references)
			//IL_0190: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c3: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c8: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c9: Unknown result type (might be due to invalid IL or missing references)
			//IL_01cf: Unknown result type (might be due to invalid IL or missing references)
			//IL_01dc: Unknown result type (might be due to invalid IL or missing references)
			//IL_0140: Unknown result type (might be due to invalid IL or missing references)
			//IL_015a: Unknown result type (might be due to invalid IL or missing references)
			float num = tape.Before + tape.After;
			float num2 = Mathf.Clamp01((t + tape.Before) / num);
			((Graphic)_barFill).rectTransform.anchorMax = new Vector2(num2, 1f);
			RectTransform rectTransform = ((Graphic)_playhead).rectTransform;
			RectTransform rectTransform2 = ((Graphic)_playhead).rectTransform;
			Vector2 val = default(Vector2);
			((Vector2)(ref val))..ctor(num2, 0.5f);
			rectTransform2.anchorMax = val;
			rectTransform.anchorMin = val;
			((Graphic)_playhead).rectTransform.anchoredPosition = Vector2.zero;
			int num3 = 0;
			Color val2 = default(Color);
			foreach (TapeEvent @event in tape.Events)
			{
				EventType type = @event.Type;
				if (type <= EventType.Death)
				{
					if (type != EventType.Damage)
					{
						if (type != EventType.Death)
						{
							goto IL_011d;
						}
						val2 = Color.white;
					}
					else
					{
						if (@event.Entity != tape.VictimId)
						{
							continue;
						}
						((Color)(ref val2))..ctor(1f, 0.55f, 0.25f);
					}
				}
				else
				{
					if (type == EventType.ItemDropped)
					{
						continue;
					}
					if (type != EventType.Grabbed)
					{
						goto IL_011d;
					}
					if (@event.Entity != tape.VictimId)
					{
						continue;
					}
					((Color)(ref val2))..ctor(1f, 0.55f, 0.25f);
				}
				goto IL_0124;
				IL_011d:
				val2 = Amber;
				goto IL_0124;
				IL_0124:
				if (num3 >= _ticks.Count)
				{
					Image val3 = UiKit.Box("Tick", (Transform)(object)((Graphic)_bar).rectTransform, val2);
					((Graphic)val3).rectTransform.sizeDelta = new Vector2(4f, 22f);
					_ticks.Add(val3);
				}
				Image val4 = _ticks[num3++];
				((Component)val4).gameObject.SetActive(true);
				((Graphic)val4).color = val2;
				float num4 = Mathf.Clamp01((@event.Time + tape.Before) / num);
				val = (((Graphic)val4).rectTransform.anchorMin = (((Graphic)val4).rectTransform.anchorMax = new Vector2(num4, 0.5f)));
				((Graphic)val4).rectTransform.anchoredPosition = Vector2.zero;
			}
			for (int i = num3; i < _ticks.Count; i++)
			{
				((Component)_ticks[i]).gameObject.SetActive(false);
			}
			((Component)_playhead).transform.SetAsLastSibling();
		}

		private static void UpdateMap(Tape tape, float t)
		{
			//IL_0038: Unknown result type (might be due to invalid IL or missing references)
			//IL_003d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0041: Unknown result type (might be due to invalid IL or missing references)
			//IL_0046: Unknown result type (might be due to invalid IL or missing references)
			//IL_0047: Unknown result type (might be due to invalid IL or missing references)
			//IL_0052: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b8: Unknown result type (might be due to invalid IL or missing references)
			//IL_0093: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bd: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c7: Unknown result type (might be due to invalid IL or missing references)
			//IL_0172: Unknown result type (might be due to invalid IL or missing references)
			//IL_017e: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c7: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ca: Unknown result type (might be due to invalid IL or missing references)
			//IL_020a: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b0: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b7: Unknown result type (might be due to invalid IL or missing references)
			//IL_0266: Unknown result type (might be due to invalid IL or missing references)
			//IL_026b: Unknown result type (might be due to invalid IL or missing references)
			//IL_025f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0258: Unknown result type (might be due to invalid IL or missing references)
			//IL_0242: Unknown result type (might be due to invalid IL or missing references)
			//IL_0297: Unknown result type (might be due to invalid IL or missing references)
			//IL_02ae: Unknown result type (might be due to invalid IL or missing references)
			//IL_02d4: Unknown result type (might be due to invalid IL or missing references)
			//IL_02db: Unknown result type (might be due to invalid IL or missing references)
			//IL_030f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0308: Unknown result type (might be due to invalid IL or missing references)
			//IL_05f9: Unknown result type (might be due to invalid IL or missing references)
			//IL_0605: Unknown result type (might be due to invalid IL or missing references)
			//IL_05e1: Unknown result type (might be due to invalid IL or missing references)
			//IL_05e4: Unknown result type (might be due to invalid IL or missing references)
			//IL_0335: Unknown result type (might be due to invalid IL or missing references)
			//IL_0642: Unknown result type (might be due to invalid IL or missing references)
			//IL_06d2: Unknown result type (might be due to invalid IL or missing references)
			//IL_036e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0371: Unknown result type (might be due to invalid IL or missing references)
			//IL_0387: Unknown result type (might be due to invalid IL or missing references)
			//IL_038c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0391: Unknown result type (might be due to invalid IL or missing references)
			//IL_0396: Unknown result type (might be due to invalid IL or missing references)
			//IL_06ea: Unknown result type (might be due to invalid IL or missing references)
			//IL_06ef: Unknown result type (might be due to invalid IL or missing references)
			//IL_06f0: Unknown result type (might be due to invalid IL or missing references)
			//IL_06f7: Unknown result type (might be due to invalid IL or missing references)
			//IL_070f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0720: Unknown result type (might be due to invalid IL or missing references)
			//IL_072c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0731: Unknown result type (might be due to invalid IL or missing references)
			//IL_03a4: Unknown result type (might be due to invalid IL or missing references)
			//IL_03a6: Unknown result type (might be due to invalid IL or missing references)
			//IL_03a8: Unknown result type (might be due to invalid IL or missing references)
			//IL_03ad: Unknown result type (might be due to invalid IL or missing references)
			//IL_03c5: Unknown result type (might be due to invalid IL or missing references)
			//IL_03cc: Unknown result type (might be due to invalid IL or missing references)
			//IL_03df: Unknown result type (might be due to invalid IL or missing references)
			//IL_03f7: Unknown result type (might be due to invalid IL or missing references)
			//IL_0404: Unknown result type (might be due to invalid IL or missing references)
			//IL_06bc: Unknown result type (might be due to invalid IL or missing references)
			//IL_06b5: Unknown result type (might be due to invalid IL or missing references)
			//IL_0449: Unknown result type (might be due to invalid IL or missing references)
			//IL_0468: Unknown result type (might be due to invalid IL or missing references)
			//IL_046a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0506: Unknown result type (might be due to invalid IL or missing references)
			//IL_04b6: Unknown result type (might be due to invalid IL or missing references)
			//IL_04b9: Unknown result type (might be due to invalid IL or missing references)
			//IL_04d3: Unknown result type (might be due to invalid IL or missing references)
			//IL_04d5: Unknown result type (might be due to invalid IL or missing references)
			//IL_04d7: Unknown result type (might be due to invalid IL or missing references)
			Camera mapCamera = MapScreen.mapCamera;
			RenderTexture val = (((Object)(object)mapCamera != (Object)null) ? mapCamera.targetTexture : null);
			_mapImg.texture = (Texture)(object)val;
			if ((Object)(object)val == (Object)null)
			{
				return;
			}
			Rect rect = _mapPanel.rect;
			Vector2 size = ((Rect)(ref rect)).size;
			float num = size.x / Mathf.Max(1f, size.y);
			float num2 = (float)((Texture)val).width / (float)((Texture)val).height;
			_uv = ((num < num2) ? new Rect((1f - num / num2) / 2f, 0f, num / num2, 1f) : new Rect(0f, (1f - num2 / num) / 2f, 1f, num2 / num));
			_mapImg.uvRect = _uv;
			foreach (Icon value in _icons.Values)
			{
				value.Used = false;
			}
			Sample(tape, tape.VictimId, t, out var st);
			bool flag = (st.Flags & 1) != 0;
			foreach (EntityInfo value2 in tape.Entities.Values)
			{
				if (!Sample(tape, value2.Id, t, out var st2) || (st2.Flags & 1) != 0 != flag || Mathf.Abs(st2.Pos.y - st.Pos.y) > 6f || (value2.Kind == EntityKind.Enemy && !Plugin.ShowUnseenEnemies.Value && !CouldSee(st.Pos, st2.Pos)) || !Project(mapCamera, size, st2.Pos, out var p))
				{
					continue;
				}
				Icon icon = GetIcon(value2.Id);
				icon.Used = true;
				((Component)icon.Root).gameObject.SetActive(true);
				icon.Root.anchoredPosition = p;
				bool flag2 = value2.Id == tape.VictimId;
				bool flag3 = (st2.Flags & 2) != 0;
				Color val2 = (Color)((value2.Kind == EntityKind.Enemy) ? Red : (flag3 ? Grey : (flag2 ? new Color(0.55f, 0.85f, 1f) : Blue)));
				float num3 = ((value2.Kind == EntityKind.Enemy) ? 18f : (flag2 ? 22f : 18f));
				((Graphic)icon.Dot).color = val2;
				((Graphic)icon.Dot).rectTransform.sizeDelta = new Vector2(num3, num3);
				((Component)icon.Ring).gameObject.SetActive(flag2 || (value2.Kind == EntityKind.Enemy && Vector3.Distance(st2.Pos, st.Pos) < 4f));
				((Graphic)icon.Ring).color = ((value2.Kind == EntityKind.Enemy) ? Red : Color.white);
				((Graphic)icon.Ring).rectTransform.sizeDelta = new Vector2(num3 * 2.2f, num3 * 2.2f);
				((Component)icon.Arrow).gameObject.SetActive(value2.Kind == EntityKind.Player && !flag3);
				if (value2.Kind == EntityKind.Player && Project(mapCamera, size, st2.Pos + Quaternion.Euler(0f, st2.Yaw, 0f) * Vector3.forward, out var p2))
				{
					Vector2 val3 = p2 - p;
					((Transform)((Graphic)icon.Arrow).rectTransform).localRotation = Quaternion.Euler(0f, 0f, (0f - Mathf.Atan2(val3.x, val3.y)) * 57.29578f);
					((Graphic)icon.Arrow).rectTransform.anchoredPosition = ((Vector2)(ref val3)).normalized * (num3 * 0.75f);
				}
				((TMP_Text)icon.Label).text = (flag3 ? (value2.Name.ToUpperInvariant() + "  X") : value2.Name.ToUpperInvariant());
				((Graphic)icon.Label).color = val2;
				icon.Trail.Points.Clear();
				icon.Trail.Head = val2;
				foreach (Snapshot snap in tape.Snaps)
				{
					if (!(snap.Time < t - 3f))
					{
						if (snap.Time > t)
						{
							break;
						}
						if (snap.TryGet(value2.Id, out var st3) && Project(mapCamera, size, st3.Pos, out var p3))
						{
							icon.Trail.Points.Add(p3 - p);
						}
					}
				}
				icon.Trail.Points.Add(Vector2.zero);
				icon.Trail.Rebuild();
			}
			foreach (Icon value3 in _icons.Values)
			{
				if (!value3.Used)
				{
					((Component)value3.Root).gameObject.SetActive(false);
				}
			}
			int num4 = 0;
			Vector2 p4 = default(Vector2);
			foreach (TapeEvent @event in tape.Events)
			{
				if (@event.Time > t || @event.Time < t - 4f)
				{
					continue;
				}
				string text = EventText(@event, tape);
				if (text == null)
				{
					continue;
				}
				if (@event.HasPos)
				{
					if (!Project(mapCamera, size, @event.Pos, out p4))
					{
						continue;
					}
				}
				else
				{
					((Vector2)(ref p4))..ctor(size.x * 0.5f, size.y * 0.12f + (float)num4 * 20f);
				}
				if (num4 >= _eventLabels.Count)
				{
					_eventLabels.Add(UiKit.Text("Ev", (Transform)(object)_overlay, 14f, Amber, (TextAlignmentOptions)514));
				}
				TextMeshProUGUI val4 = _eventLabels[num4++];
				((Component)val4).gameObject.SetActive(true);
				((TMP_Text)val4).text = $"{text}  {@event.Time:0.0}s";
				((Graphic)val4).color = (Color)((@event.Type == EventType.Damage || @event.Type == EventType.Grabbed) ? new Color(1f, 0.6f, 0.35f) : ((@event.Type == EventType.Death) ? Color.white : Amber));
				RectTransform rectTransform = ((TMP_Text)val4).rectTransform;
				Vector2 anchorMin = (((TMP_Text)val4).rectTransform.anchorMax = Vector2.zero);
				rectTransform.anchorMin = anchorMin;
				((TMP_Text)val4).rectTransform.sizeDelta = new Vector2(200f, 20f);
				((TMP_Text)val4).rectTransform.anchoredPosition = p4 + new Vector2(0f, 22f);
				((TMP_Text)val4).transform.SetAsLastSibling();
			}
			for (int i = num4; i < _eventLabels.Count; i++)
			{
				((Component)_eventLabels[i]).gameObject.SetActive(false);
			}
		}

		private static string EventText(TapeEvent ev, Tape tape)
		{
			switch (ev.Type)
			{
			case EventType.Damage:
				if (ev.Entity != tape.VictimId)
				{
					return null;
				}
				return $"-{ev.Value} HP";
			case EventType.Death:
				if (ev.Entity != tape.VictimId)
				{
					return "DIED";
				}
				return "KILLED";
			case EventType.DoorOpened:
				return "DOOR OPENED";
			case EventType.DoorClosed:
				return "DOOR CLOSED";
			case EventType.BigDoorOpened:
				return "BIG DOOR OPEN";
			case EventType.BigDoorClosed:
				return "BIG DOOR SHUT";
			case EventType.LightsFlicker:
				return "LIGHTS FLICKER";
			case EventType.PowerOff:
				return "POWER OFF";
			case EventType.PowerOn:
				return "POWER ON";
			case EventType.ItemDropped:
				return "DROPPED ITEM";
			case EventType.MineExploded:
				return "MINE!";
			case EventType.TurretFiring:
				return "TURRET FIRING";
			case EventType.Grabbed:
				return "GRABBED";
			default:
				return null;
			}
		}

		private static bool Project(Camera cam, Vector2 size, Vector3 world, out Vector2 p)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			//IL_0009: Unknown result type (might be due to invalid IL or missing references)
			//IL_0025: Unknown result type (might be due to invalid IL or missing references)
			//IL_002c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0048: Unknown result type (might be due to invalid IL or missing references)
			//IL_004f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0054: Unknown result type (might be due to invalid IL or missing references)
			//IL_0079: Unknown result type (might be due to invalid IL or missing references)
			//IL_008d: Unknown result type (might be due to invalid IL or missing references)
			Vector3 val = cam.WorldToViewportPoint(world);
			p = new Vector2((val.x - ((Rect)(ref _uv)).x) / ((Rect)(ref _uv)).width * size.x, (val.y - ((Rect)(ref _uv)).y) / ((Rect)(ref _uv)).height * size.y);
			if (p.x > -40f && p.y > -40f && p.x < size.x + 40f)
			{
				return p.y < size.y + 40f;
			}
			return false;
		}

		private static bool CouldSee(Vector3 from, Vector3 to)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			//IL_001b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0020: Unknown result type (might be due to invalid IL or missing references)
			//IL_0025: Unknown result type (might be due to invalid IL or missing references)
			//IL_0026: Unknown result type (might be due to invalid IL or missing references)
			//IL_0030: Unknown result type (might be due to invalid IL or missing references)
			//IL_0035: Unknown result type (might be due to invalid IL or missing references)
			if (Vector3.Distance(from, to) > 30f)
			{
				return false;
			}
			return !Physics.Linecast(from + Vector3.up * 1.6f, to + Vector3.up * 1f, StartOfRound.Instance.collidersAndRoomMask, (QueryTriggerInteraction)1);
		}

		private static Icon GetIcon(int id)
		{
			//IL_0044: Unknown result type (might be due to invalid IL or missing references)
			//IL_0049: Unknown result type (might be due to invalid IL or missing references)
			//IL_004a: 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_005c: 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_0097: Unknown result type (might be due to invalid IL or missing references)
			//IL_00da: Unknown result type (might be due to invalid IL or missing references)
			//IL_0105: Unknown result type (might be due to invalid IL or missing references)
			//IL_0130: Unknown result type (might be due to invalid IL or missing references)
			//IL_0164: Unknown result type (might be due to invalid IL or missing references)
			//IL_017f: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ba: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c0: Unknown result type (might be due to invalid IL or missing references)
			//IL_01db: Unknown result type (might be due to invalid IL or missing references)
			//IL_01fa: Unknown result type (might be due to invalid IL or missing references)
			//IL_0219: Unknown result type (might be due to invalid IL or missing references)
			if (_icons.TryGetValue(id, out var value))
			{
				return value;
			}
			value = new Icon
			{
				Root = UiKit.Rect("E" + id, (Transform)(object)_overlay)
			};
			Vector2 val = (value.Root.anchorMin = (value.Root.anchorMax = Vector2.zero));
			value.Root.sizeDelta = Vector2.zero;
			RectTransform val3 = UiKit.Rect("Trail", (Transform)(object)value.Root);
			((Vector2)(ref val))..ctor(0.5f, 0.5f);
			val3.anchorMax = val;
			val3.anchorMin = val;
			val3.sizeDelta = Vector2.zero;
			value.Trail = ((Component)val3).gameObject.AddComponent<UiLine>();
			value.Trail.Width = 3f;
			((Graphic)value.Trail).raycastTarget = false;
			value.Ring = UiKit.Box("Ring", (Transform)(object)value.Root, Color.white);
			value.Ring.sprite = UiKit.Ring;
			value.Dot = UiKit.Box("Dot", (Transform)(object)value.Root, Color.white);
			value.Dot.sprite = UiKit.Circle;
			value.Arrow = UiKit.Box("Arrow", (Transform)(object)value.Root, Color.white);
			value.Arrow.sprite = UiKit.Arrow;
			((Graphic)value.Arrow).rectTransform.sizeDelta = new Vector2(12f, 12f);
			value.Label = UiKit.Text("Label", (Transform)(object)value.Root, 14f, Color.white, (TextAlignmentOptions)513);
			RectTransform rectTransform = ((TMP_Text)value.Label).rectTransform;
			RectTransform rectTransform2 = ((TMP_Text)value.Label).rectTransform;
			((Vector2)(ref val))..ctor(0.5f, 0.5f);
			rectTransform2.anchorMax = val;
			rectTransform.anchorMin = val;
			((TMP_Text)value.Label).rectTransform.pivot = new Vector2(0f, 0.5f);
			((TMP_Text)value.Label).rectTransform.sizeDelta = new Vector2(180f, 18f);
			((TMP_Text)value.Label).rectTransform.anchoredPosition = new Vector2(16f, 14f);
			_icons[id] = value;
			return value;
		}

		private static bool Sample(Tape tape, int id, float t, out EntityState st)
		{
			//IL_00d5: 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_00f0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fe: 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)
			st = default(EntityState);
			List<Snapshot> snaps = tape.Snaps;
			if (snaps.Count == 0)
			{
				return false;
			}
			int i;
			for (i = 0; i < snaps.Count && snaps[i].Time < t; i++)
			{
			}
			if (i == 0)
			{
				return snaps[0].TryGet(id, out st);
			}
			if (i >= snaps.Count)
			{
				return snaps[snaps.Count - 1].TryGet(id, out st);
			}
			Snapshot snapshot = snaps[i - 1];
			Snapshot snapshot2 = snaps[i];
			EntityState st2;
			bool flag = snapshot.TryGet(id, out st2);
			EntityState st3;
			bool flag2 = snapshot2.TryGet(id, out st3);
			if (!flag && !flag2)
			{
				return false;
			}
			if (!flag)
			{
				st = st3;
				return true;
			}
			if (!flag2)
			{
				st = st2;
				return true;
			}
			float num = Mathf.InverseLerp(snapshot.Time, snapshot2.Time, t);
			st = st2;
			if (Vector3.Distance(st2.Pos, st3.Pos) < 8f)
			{
				st.Pos = Vector3.Lerp(st2.Pos, st3.Pos, num);
			}
			else if (num > 0.5f)
			{
				st = st3;
			}
			st.Yaw = Mathf.LerpAngle(st2.Yaw, st3.Yaw, num);
			return true;
		}

		private static void UpdateVideo(Tape tape, float t)
		{
			//IL_0172: Unknown result type (might be due to invalid IL or missing references)
			//IL_0257: Unknown result type (might be due to invalid IL or missing references)
			//IL_025c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0260: Unknown result type (might be due to invalid IL or missing references)
			//IL_0265: Unknown result type (might be due to invalid IL or missing references)
			//IL_0266: Unknown result type (might be due to invalid IL or missing references)
			//IL_0271: Unknown result type (might be due to invalid IL or missing references)
			//IL_02e2: Unknown result type (might be due to invalid IL or missing references)
			//IL_02bd: Unknown result type (might be due to invalid IL or missing references)
			if (!((Component)_videoPanel).gameObject.activeSelf)
			{
				return;
			}
			((TMP_Text)_videoLabel).text = tape.Label + "  //  FIRST PERSON";
			((TMP_Text)_videoTime).text = ((t <= 0f) ? $"{t:0.0}s" : $"+{t:0.0}s");
			MediaTrack media = tape.Media;
			if (media == null)
			{
				_videoImg.texture = null;
				((Component)_videoStatus).gameObject.SetActive(true);
				((TMP_Text)_videoStatus).text = (tape.MediaUnavailable ? "NO VIDEO" : ((tape.MediaExpectedBytes > 0) ? $"RECEIVING {100 * tape.MediaReceivedBytes / Mathf.Max(1, tape.MediaExpectedBytes)}%" : "WAITING FOR SIGNAL"));
				((Behaviour)_recDot).enabled = false;
				return;
			}
			((Behaviour)_recDot).enabled = t <= 0f && Mathf.Repeat(Time.time, 1f) < 0.6f;
			if (t > 0f || media.Frames.Count == 0 || t > media.FrameTimes[media.FrameTimes.Count - 1] + 0.3f)
			{
				Noise();
				_videoImg.texture = (Texture)(object)_noiseTex;
				_videoImg.uvRect = new Rect(0f, 0f, 1f, 1f);
				((Component)_videoStatus).gameObject.SetActive(true);
				((TMP_Text)_videoStatus).text = ((t > 0f) ? "SIGNAL LOST" : "NO SIGNAL");
				return;
			}
			((Component)_videoStatus).gameObject.SetActive(false);
			int i;
			for (i = 0; i + 1 < media.FrameTimes.Count && media.FrameTimes[i + 1] <= t; i++)
			{
			}
			if (t < media.FrameTimes[0])
			{
				_videoImg.texture = null;
				((Component)_videoStatus).gameObject.SetActive(true);
				((TMP_Text)_videoStatus).text = "NO SIGNAL";
				return;
			}
			if (i != _videoFrame)
			{
				_videoFrame = i;
				ImageConversion.LoadImage(_videoTex, media.Frames[i]);
			}
			_videoImg.texture = (Texture)(object)_videoTex;
			Rect rect = _videoPanel.rect;
			Vector2 size = ((Rect)(ref rect)).size;
			float num = size.x / Mathf.Max(1f, size.y);
			float num2 = (float)media.Width / (float)Mathf.Max(1, media.Height);
			_videoImg.uvRect = ((num < num2) ? new Rect((1f - num / num2) / 2f, 0f, num / num2, 1f) : new Rect(0f, (1f - num2 / num) / 2f, 1f, num2 / num));
		}

		private static void Noise()
		{
			//IL_0027: Unknown result type (might be due to invalid IL or missing references)
			//IL_002c: Unknown result type (might be due to invalid IL or missing references)
			Color32[] pixels = _noiseTex.GetPixels32();
			for (int i = 0; i < pixels.Length; i++)
			{
				byte b = (byte)Random.Range(10, 140);
				pixels[i] = new Color32(b, b, b, byte.MaxValue);
			}
			_noiseTex.SetPixels32(pixels);
			_noiseTex.Apply(false);
		}

		private static void UpdateAudio(Tape tape, float t)
		{
			MediaTrack media = tape.Media;
			if (media == null || media.AudioSamples == 0)
			{
				if ((Object)(object)_speaker != (Object)null && _speaker.isPlaying)
				{
					_speaker.Stop();
				}
				_wave.Points.Clear();
				_wave.Rebuild();
				return;
			}
			if (_audioFor != media)
			{
				_audioFor = media;
				if ((Object)(object)_clip != (Object)null)
				{
					Object.Destroy((Object)(object)_clip);
				}
				_clip = media.MakeClip();
				_audioStarted = false;
				BuildWave(media);
			}
			EnsureSpeaker();
			if (!((Object)(object)_speaker == (Object)null) && !((Object)(object)_clip == (Object)null))
			{
				float num = t - media.AudioStart;
				if (!_audioStarted && num >= 0f && num < _clip.length - 0.05f)
				{
					_speaker.clip = _clip;
					_speaker.time = num;
					_speaker.Play();
					_audioStarted = true;
				}
				_speaker.volume = Plugin.ReplayVolume.Value;
				if (t > 0.2f && _speaker.isPlaying)
				{
					_speaker.Stop();
				}
			}
		}

		private static void BuildWave(MediaTrack m)
		{
			//IL_0030: Unknown result type (might be due to invalid IL or missing references)
			//IL_0035: Unknown result type (might be due to invalid IL or missing references)
			//IL_0039: 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_003f: Unknown result type (might be due to invalid IL or missing references)
			//IL_010c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0127: Unknown result type (might be due to invalid IL or missing references)
			//IL_0135: Unknown result type (might be due to invalid IL or missing references)
			//IL_0143: Unknown result type (might be due to invalid IL or missing references)
			//IL_0159: Unknown result type (might be due to invalid IL or missing references)
			//IL_0169: Unknown result type (might be due to invalid IL or missing references)
			//IL_0177: Unknown result type (might be due to invalid IL or missing references)
			//IL_0185: Unknown result type (might be due to invalid IL or missing references)
			_wave.Points.Clear();
			float[] array = Adpcm4.Decode(m.Adpcm, m.AudioSamples);
			Canvas.ForceUpdateCanvases();
			Rect rect = ((Graphic)_wave).rectTransform.rect;
			Vector2 size = ((Rect)(ref rect)).size;
			if (size.x < 10f)
			{
				((Vector2)(ref size))..ctor(600f, 40f);
			}
			int num = 160;
			float num2 = _tape.Before + _tape.After;
			for (int i = 0; i < num; i++)
			{
				float num3 = 0f - _tape.Before + num2 * (float)i / (float)num;
				float num4 = 0f - _tape.Before + num2 * (float)(i + 1) / (float)num;
				int num5 = Mathf.Clamp((int)((num3 - m.AudioStart) * 16000f), 0, array.Length);
				int num6 = Mathf.Clamp((int)((num4 - m.AudioStart) * 16000f), 0, array.Length);
				float num7 = 0f;
				for (int j = num5; j < num6; j++)
				{
					num7 = Mathf.Max(num7, Mathf.Abs(array[j]));
				}
				float num8 = size.x * (float)i / (float)num;
				_wave.Points.Add(new Vector2(num8, size.y * 0.5f + num7 * size.y * 0.5f));
				_wave.Points.Add(new Vector2(num8 + size.x / (float)num * 0.5f, size.y * 0.5f - num7 * size.y * 0.5f));
			}
			_wave.Rebuild();
		}

		private static void EnsureSpeaker()
		{
			//IL_0030: Unknown result type (might be due to invalid IL or missing references)
			//IL_0035: Unknown result type (might be due to invalid IL or missing references)
			ManualCameraRenderer mapScreen = MapScreen;
			if (!((Object)(object)_speaker != (Object)null) && !((Object)(object)mapScreen == (Object)null) && !((Object)(object)mapScreen.mesh == (Object)null))
			{
				GameObject val = new GameObject("BlackBox.Speaker");
				val.transform.SetParent(((Component)mapScreen.mesh).transform, false);
				_speaker = val.AddComponent<AudioSource>();
				_speaker.spatialBlend = 1f;
				_speaker.rolloffMode = (AudioRolloffMode)1;
				_speaker.minDistance = 3f;
				_speaker.maxDistance = 22f;
				_speaker.playOnAwake = false;
				_speaker.dopplerLevel = 0f;
			}
		}
	}
	internal static class Net
	{
		private sealed class Incoming
		{
			public byte[] Data;

			public int Received;

			public ulong From;
		}

		private struct Out
		{
			public string Name;

			public ulong To;

			public byte[] Payload;
		}

		[CompilerGenerated]
		private static class <>O
		{
			public static HandleNamedMessageDelegate <0>__OnMediaBegin;

			public static HandleNamedMessageDelegate <1>__OnMediaChunk;

			public static HandleNamedMessageDelegate <2>__OnMediaNone;
		}

		[Serializable]
		[CompilerGenerated]
		private sealed class <>c
		{
			public static readonly <>c <>9 = new <>c();

			public static HandleNamedMessageDelegate <>9__23_0;

			public static HandleNamedMessageDelegate <>9__23_1;

			public static HandleNamedMessageDelegate <>9__23_2;

			public static HandleNamedMessageDelegate <>9__23_3;

			public static HandleNamedMessageDelegate <>9__23_4;

			public static HandleNamedMessageDelegate <>9__23_5;

			public static Action<FastBufferWriter> <>9__25_0;

			public static Action<FastBufferWriter> <>9__28_0;

			internal void <EnsureRegistered>b__23_0(ulong s, FastBufferReader r)
			{
				//IL_000d: Unknown result type (might be due to invalid IL or missing references)
				//IL_0013: Unknown result type (might be due to invalid IL or missing references)
				if (IsHost)
				{
					int key = default(int);
					((FastBufferReader)(ref r)).ReadValueSafe<int>(ref key, default(ForPrimitives));
					HostPlay(key);
				}
			}

			internal void <EnsureRegistered>b__23_1(ulong s, FastBufferReader r)
			{
				if (IsHost)
				{
					HostStop();
				}
			}

			internal void <EnsureRegistered>b__23_2(ulong s, FastBufferReader r)
			{
				//IL_000d: Unknown result type (might be due to invalid IL or missing references)
				//IL_0013: Unknown result type (might be due to invalid IL or missing references)
				if (IsHost)
				{
					byte l = default(byte);
					((FastBufferReader)(ref r)).ReadValueSafe<byte>(ref l, default(ForPrimitives));
					HostLayout((Layout)l);
				}
			}

			internal void <EnsureRegistered>b__23_3(ulong s, FastBufferReader r)
			{
				//IL_0006: Unknown result type (might be due to invalid IL or missing references)
				//IL_000c: Unknown result type (might be due to invalid IL or missing references)
				//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)
				int key = default(int);
				((FastBufferReader)(ref r)).ReadValueSafe<int>(ref key, default(ForPrimitives));
				byte layout = default(byte);
				((FastBufferReader)(ref r)).ReadValueSafe<byte>(ref layout, default(ForPrimitives));
				MonitorView.StartReplay(key, (Layout)layout);
			}

			internal void <EnsureRegistered>b__23_4(ulong s, FastBufferReader r)
			{
				MonitorView.StopReplay(keepAlertOff: true);
			}

			internal void <EnsureRegistered>b__23_5(ulong s, FastBufferReader r)
			{
				//IL_0006: Unknown result type (might be due to invalid IL or missing references)
				//IL_000c: Unknown result type (might be due to invalid IL or missing references)
				byte layout = default(byte);
				((FastBufferReader)(ref r)).ReadValueSafe<byte>(ref layout, default(ForPrimitives));
				MonitorView.SetLayout((Layout)layout);
			}

			internal void <RequestStop>b__25_0(FastBufferWriter w)
			{
			}

			internal void <HostStop>b__28_0(FastBufferWriter w)
			{
			}
		}

		private const string P = "daniel.blackbox.";

		private const string MsgPlayReq = "daniel.blackbox.playreq";

		private const string MsgPlay = "daniel.blackbox.play";

		private const string MsgStopReq = "daniel.blackbox.stopreq";

		private const string MsgStop = "daniel.blackbox.stop";

		private const string MsgLayoutReq = "daniel.blackbox.layoutreq";

		private const string MsgLayout = "daniel.blackbox.layout";

		private const string MsgMediaBegin = "daniel.blackbox.mbegin";

		private const string MsgMediaChunk = "daniel.blackbox.mchunk";

		private const string MsgMediaNone = "daniel.blackbox.mnone";

		private static readonly string[] All = new string[9] { "daniel.blackbox.playreq", "daniel.blackbox.play", "daniel.blackbox.stopreq", "daniel.blackbox.stop", "daniel.blackbox.layoutreq", "daniel.blackbox.layout", "daniel.blackbox.mbegin", "daniel.blackbox.mchunk", "daniel.blackbox.mnone" };

		private const int ChunkSize = 12288;

		private const int BytesPerSecond = 307200;

		private static NetworkManager _registeredOn;

		private static readonly Dictionary<int, Incoming> _incoming = new Dictionary<int, Incoming>();

		private static readonly Queue<Out> _queue = new Queue<Out>();

		private static float _budget;

		private const int MaxMediaBytes = 8388608;

		private const int MaxIncomingBytes = 25165824;

		public static bool IsHost
		{
			get
			{
				if ((Object)(object)NetworkManager.Singleton != (Object)null)
				{
					return NetworkManager.Singleton.IsServer;
				}
				return false;
			}
		}

		private static bool Connected
		{
			get
			{
				if ((Object)(object)NetworkManager.Singleton != (Object)null && NetworkManager.Singleton.IsListening)
				{
					return NetworkManager.Singleton.CustomMessagingManager != null;
				}
				return false;
			}
		}

		public static void EnsureRegistered()
		{
			//IL_0099: Unknown result type (might be due to invalid IL or missing references)
			//IL_009e: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a4: Expected O, but got Unknown
			//IL_00c3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ce: Expected O, but got Unknown
			//IL_00ed: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f8: Expected O, but got Unknown
			//IL_0117: Unknown result type (might be due to invalid IL or missing references)
			//IL_011c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0122: Expected O, but got Unknown
			//IL_0141: Unknown result type (might be due to invalid IL or missing references)
			//IL_0146: Unknown result type (might be due to invalid IL or missing references)
			//IL_014c: Expected O, but got Unknown
			//IL_016b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0170: Unknown result type (might be due to invalid IL or missing references)
			//IL_0176: Expected O, but got Unknown
			//IL_0191: Unknown result type (might be due to invalid IL or missing references)
			//IL_0196: Unknown result type (might be due to invalid IL or missing references)
			//IL_019c: Expected O, but got Unknown
			//IL_01b7: Unknown result type (might be due to invalid IL or missing references)
			//IL_01bc: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c2: Expected O, but got Unknown
			//IL_01dd: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e2: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e8: Expected O, but got Unknown
			NetworkManager singleton = NetworkManager.Singleton;
			if ((Object)(object)singleton == (Object)null || !singleton.IsListening || singleton.CustomMessagingManager == null)
			{
				if ((Object)(object)_registeredOn != (Object)null)
				{
					_registeredOn = null;
					_incoming.Clear();
					_queue.Clear();
				}
				return;
			}
			if ((Object)(object)_registeredOn != (Object)(object)singleton)
			{
				CustomMessagingManager customMessagingManager = singleton.CustomMessagingManager;
				string[] all = All;
				foreach (string text in all)
				{
					customMessagingManager.UnregisterNamedMessageHandler(text);
				}
				object obj = <>c.<>9__23_0;
				if (obj == null)
				{
					HandleNamedMessageDelegate val = delegate(ulong s, FastBufferReader r)
					{
						//IL_000d: Unknown result type (might be due to invalid IL or missing references)
						//IL_0013: Unknown result type (might be due to invalid IL or missing references)
						if (IsHost)
						{
							int key = default(int);
							((FastBufferReader)(ref r)).ReadValueSafe<int>(ref key, default(ForPrimitives));
							HostPlay(key);
						}
					};
					<>c.<>9__23_0 = val;
					obj = (object)val;
				}
				customMessagingManager.RegisterNamedMessageHandler("daniel.blackbox.playreq", (HandleNamedMessageDelegate)obj);
				object obj2 = <>c.<>9__23_1;
				if (obj2 == null)
				{
					HandleNamedMessageDelegate val2 = delegate
					{
						if (IsHost)
						{
							HostStop();
						}
					};
					<>c.<>9__23_1 = val2;
					obj2 = (object)val2;
				}
				customMessagingManager.RegisterNamedMessageHandler("daniel.blackbox.stopreq", (HandleNamedMessageDelegate)obj2);
				object obj3 = <>c.<>9__23_2;
				if (obj3 == null)
				{
					HandleNamedMessageDelegate val3 = delegate(ulong s, FastBufferReader r)
					{
						//IL_000d: Unknown result type (might be due to invalid IL or missing references)
						//IL_0013: Unknown result type (might be due to invalid IL or missing references)
						if (IsHost)
						{
							byte l = default(byte);
							((FastBufferReader)(ref r)).ReadValueSafe<byte>(ref l, default(ForPrimitives));
							HostLayout((Layout)l);
						}
					};
					<>c.<>9__23_2 = val3;
					obj3 = (object)val3;
				}
				customMessagingManager.RegisterNamedMessageHandler("daniel.blackbox.layoutreq", (HandleNamedMessageDelegate)obj3);
				object obj4 = <>c.<>9__23_3;
				if (obj4 == null)
				{
					HandleNamedMessageDelegate val4 = delegate(ulong s, FastBufferReader r)
					{
						//IL_0006: Unknown result type (might be due to invalid IL or missing references)
						//IL_000c: Unknown result type (might be due to invalid IL or missing references)
						//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)
						int key = default(int);
						((FastBufferReader)(ref r)).ReadValueSafe<int>(ref key, default(ForPrimitives));
						byte layout = default(byte);
						((FastBufferReader)(ref r)).ReadValueSafe<byte>(ref layout, default(ForPrimitives));
						MonitorView.StartReplay(key, (Layout)layout);
					};
					<>c.<>9__23_3 = val4;
					obj4 = (object)val4;
				}
				customMessagingManager.RegisterNamedMessageHandler("daniel.blackbox.play", (HandleNamedMessageDelegate)obj4);
				object obj5 = <>c.<>9__23_4;
				if (obj5 == null)
				{
					HandleNamedMessageDelegate val5 = delegate
					{
						MonitorView.StopReplay(keepAlertOff: true);
					};
					<>c.<>9__23_4 = val5;
					obj5 = (object)val5;
				}
				customMessagingManager.RegisterNamedMessageHandler("daniel.blackbox.stop", (HandleNamedMessageDelegate)obj5);
				object obj6 = <>c.<>9__23_5;
				if (obj6 == null)
				{
					HandleNamedMessageDelegate val6 = delegate(ulong s, FastBufferReader r)
					{
						//IL_0006: Unknown result type (might be due to invalid IL or missing references)
						//IL_000c: Unknown result type (might be due to invalid IL or missing references)
						byte layout = default(byte);
						((FastBufferReader)(ref r)).ReadValueSafe<byte>(ref layout, default(ForPrimitives));
						MonitorView.SetLayout((Layout)layout);
					};
					<>c.<>9__23_5 = val6;
					obj6 = (object)val6;
				}
				customMessagingManager.RegisterNamedMessageHandler("daniel.blackbox.layout", (HandleNamedMessageDelegate)obj6);
				object obj7 = <>O.<0>__OnMediaBegin;
				if (obj7 == null)
				{
					HandleNamedMessageDelegate val7 = OnMediaBegin;
					<>O.<0>__OnMediaBegin = val7;
					obj7 = (object)val7;
				}
				customMessagingManager.RegisterNamedMessageHandler("daniel.blackbox.mbegin", (HandleNamedMessageDelegate)obj7);
				object obj8 = <>O.<1>__OnMediaChunk;
				if (obj8 == null)
				{
					HandleNamedMessageDelegate val8 = OnMediaChunk;
					<>O.<1>__OnMediaChunk = val8;
					obj8 = (object)val8;
				}
				customMessagingManager.RegisterNamedMessageHandler("daniel.blackbox.mchunk", (HandleNamedMessageDelegate)obj8);
				object obj9 = <>O.<2>__OnMediaNone;
				if (obj9 == null)
				{
					HandleNamedMessageDelegate val9 = OnMediaNone;
					<>O.<2>__OnMediaNone = val9;
					obj9 = (object)val9;
				}
				customMessagingManager.RegisterNamedMessageHandler("daniel.blackbox.mnone", (HandleNamedMessageDelegate)obj9);
				_registeredOn = singleton;
				Plugin.Debug("Net: handlers registered (" + (singleton.IsServer ? "host" : "client") + ").");
			}
			Pump();
		}

		public static void RequestPlay(int key)
		{
			if (IsHost)
			{
				HostPlay(key);
				return;
			}
			SendSmall("daniel.blackbox.playreq", 0uL, delegate(FastBufferWriter w)
			{
				//IL_000a: Unknown result type (might be due to invalid IL or missing references)
				//IL_0010: Unknown result type (might be due to invalid IL or missing references)
				((FastBufferWriter)(ref w)).WriteValueSafe<int>(ref key, default(ForPrimitives));
			});
		}

		public static void RequestStop()
		{
			if (IsHost)
			{
				HostStop();
			}
			else
			{
				SendSmall("daniel.blackbox.stopreq", 0uL, delegate
				{
				});
			}
		}

		public static void RequestLayout(Layout l)
		{
			if (IsHost)
			{
				HostLayout(l);
				return;
			}
			SendSmall("daniel.blackbox.layoutreq", 0uL, delegate(FastBufferWriter w)
			{
				//IL_000e: Unknown result type (might be due to invalid IL or missing references)
				//IL_0014: Unknown result type (might be due to invalid IL or missing references)
				byte b = (byte)l;
				((FastBufferWriter)(ref w)).WriteValueSafe<byte>(ref b, default(ForPrimitives));
			});
		}

		private static void HostPlay(int key)
		{
			byte layout = (byte)Plugin.DefaultLayout.Value;
			MonitorView.StartReplay(key, (Layout)layout);
			foreach (ulong item in Others(ulong.MaxValue))
			{
				SendSmall("daniel.blackbox.play", item, delegate(FastBufferWriter w)
				{
					//IL_000a: Unknown result type (might be due to invalid IL or missing references)
					//IL_0010: Unknown result type (might be due to invalid IL or missing references)
					//IL_0020: Unknown result type (might be due to invalid IL or missing references)
					//IL_0026: Unknown result type (might be due to invalid IL or missing references)
					((FastBufferWriter)(ref w)).WriteValueSafe<int>(ref key, default(ForPrimitives));
					((FastBufferWriter)(ref w)).WriteValueSafe<byte>(ref layout, default(ForPrimitives));
				});
			}
		}

		private static void HostStop()
		{
			MonitorView.StopReplay(keepAlertOff: true);
			foreach (ulong item in Others(ulong.MaxValue))
			{
				SendSmall("daniel.blackbox.stop", item, delegate
				{
				});
			}
		}

		private static void HostLayout(Layout l)
		{
			MonitorView.SetLayout(l);
			foreach (ulong item in Others(ulong.MaxValue))
			{
				SendSmall("daniel.blackbox.layout", item, delegate(FastBufferWriter w)
				{
					//IL_000e: Unknown result type (might be due to invalid IL or missing references)
					//IL_0014: Unknown result type (might be due to invalid IL or missing references)
					byte b = (byte)l;
					((FastBufferWriter)(ref w)).WriteValueSafe<byte>(ref b, default(ForPrimitives));
				});
			}
		}

		private static IEnumerable<ulong> Others(ulong except = ulong.MaxValue)
		{
			NetworkManager singleton = NetworkManager.Singleton;
			foreach (ulong connectedClientsId in singleton.ConnectedClientsIds)
			{
				if (connectedClientsId != 0L && connectedClientsId != except)
				{
					yield return connectedClientsId;
				}
			}
		}

		private unsafe static void SendSmall(string name, ulong to, Action<FastBufferWriter> write)
		{
			//IL_0014: Unknown result type (might be due to invalid IL or missing references)
			//IL_0026: Unknown result type (might be due to invalid IL or missing references)
			if (!Connected)
			{
				return;
			}
			FastBufferWriter val = default(FastBufferWriter);
			((FastBufferWriter)(ref val))..ctor(64, (Allocator)2, -1);
			try
			{
				write(val);
				NetworkManager.Singleton.CustomMessagingManager.SendNamedMessage(name, to, val, (NetworkDelivery)2);
			}
			finally
			{
				((IDisposable)(*(FastBufferWriter*)(&val))/*cast due to .constrained prefix*/).Dispose();
			}
		}

		public static void SendMedia(int key, byte[] data)
		{
			if (Connected)
			{
				if (IsHost)
				{
					Relay(key, data, 0uL);
				}
				else
				{
					QueueMedia(key, data, 0uL);
				}
			}
		}

		public static void SendMediaNone(int key)
		{
			if (!Connected)
			{
				return;
			}
			if (IsHost)
			{
				foreach (ulong item in Others(ulong.MaxValue))
				{
					SendSmall("daniel.blackbox.mnone", item, delegate(FastBufferWriter w)
					{
						//IL_000a: Unknown result type (might be due to invalid IL or missing references)
						//IL_0010: Unknown result type (might be due to invalid IL or missing references)
						((FastBufferWriter)(ref w)).WriteValueSafe<int>(ref key, default(ForPrimitives));
					});
				}
				return;
			}
			SendSmall("daniel.blackbox.mnone", 0uL, delegate(FastBufferWriter w)
			{
				//IL_000a: Unknown result type (might be due to invalid IL or missing references)
				//IL_0010: Unknown result type (might be due to invalid IL or missing references)
				((FastBufferWriter)(ref w)).WriteValueSafe<int>(ref key, default(ForPrimitives));
			});
		}

		private static void Relay(int key, byte[] data, ulong from)
		{
			foreach (ulong item in Others(from))
			{
				QueueMedia(key, data, item);
			}
		}

		private static void QueueMedia(int key, byte[] data, ulong to)
		{
			byte[] array = new byte[8];
			BitConverter.GetBytes(key).CopyTo(array, 0);
			BitConverter.GetBytes(data.Length).CopyTo(array, 4);
			_queue.Enqueue(new Out
			{
				Name = "daniel.blackbox.mbegin",
				To = to,
				Payload = array
			});
			for (int i = 0; i < data.Length; i += 12288)
			{
				int num = Math.Min(12288, data.Length - i);
				byte[] array2 = new byte[8 + num];
				BitConverter.GetBytes(key).CopyTo(array2, 0);
				BitConverter.GetBytes(i).CopyTo(array2, 4);
				Buffer.BlockCopy(data, i, array2, 8, num);
				_queue.Enqueue(new Out
				{
					Name = "daniel.blackbox.mchunk",
					To = to,
					Payload = array2
				});
			}
		}

		private unsafe static void Pump()
		{
			//IL_00d1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d7: 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)
			if (_queue.Count == 0)
			{
				_budget = 0f;
				return;
			}
			int num = ((!IsHost) ? 1 : Math.Max(1, NetworkManager.Singleton.ConnectedClientsIds.Count - 1));
			float num2 = Math.Min(307200 * num, 1572864);
			_budget = Mathf.Min(_budget + num2 * Time.unscaledDeltaTime, (float)(49152 * num));
			FastBufferWriter val = default(FastBufferWriter);
			while (_queue.Count > 0 && _budget >= (float)Math.Min(12288, _queue.Peek().Payload.Length))
			{
				Out obj = _queue.Dequeue();
				_budget -= obj.Payload.Length;
				if (NetworkManager.Singleton.ConnectedClientsIds.Contains(obj.To) || obj.To == 0L)
				{
					((FastBufferWriter)(ref val))..ctor(obj.Payload.Length + 8, (Allocator)2, -1);
					try
					{
						int num3 = obj.Payload.Length;
						((FastBufferWriter)(ref val)).WriteValueSafe<int>(ref num3, default(ForPrimitives));
						((FastBufferWriter)(ref val)).WriteBytesSafe(obj.Payload, -1, 0);
						NetworkManager.Singleton.CustomMessagingManager.SendNamedMessage(obj.Name, obj.To, val, (NetworkDelivery)4);
					}
					finally
					{
						((IDisposable)(*(FastBufferWriter*)(&val))/*cast due to .constrained prefix*/).Dispose();
					}
				}
			}
		}

		private static byte[] ReadPayload(FastBufferReader r)
		{
			//IL_0012: Unknown result type (might be due to invalid IL or missing references)
			//IL_0018: Unknown result type (might be due to invalid IL or missing references)
			if (!((FastBufferReader)(ref r)).TryBeginRead(4))
			{
				return null;
			}
			int num = default(int);
			((FastBufferReader)(ref r)).ReadValue<int>(ref num, default(ForPrimitives));
			if (num < 8 || num > 12296 || num > ((FastBufferReader)(ref r)).Length - ((FastBufferReader)(ref r)).Position)
			{
				return null;
			}
			byte[] result = new byte[num];
			((FastBufferReader)(ref r)).ReadBytesSafe(ref result, num, 0);
			return result;
		}

		private static bool Allowed(ulong sender, int key)
		{
			if (!IsHost)
			{
				return sender == 0;
			}
			StartOfRound instance = StartOfRound.Instance;
			if ((Object)(object)instance == (Object)null || instance.ClientPlayerList == null || !instance.ClientPlayerList.TryGetValue(sender, out var value))
			{
				return false;
			}
			return key / 16 == value;
		}

		private static void OnMediaBegin(ulong sender, FastBufferReader r)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			byte[] array = ReadPayload(r);
			if (array == null)
			{
				return;
			}
			int num = BitConverter.ToInt32(array, 0);
			int num2 = BitConverter.ToInt32(array, 4);
			if (num2 <= 0 || num2 > 8388608 || !Allowed(sender, num))
			{
				Plugin.Debug($"Net: refused media {num} ({num2} bytes) from {sender}.");
				return;
			}
			Tape tape = TapeStore.Get(num);
			if (tape != null && tape.Media != null)
			{
				return;
			}
			List<int> list = new List<int>();
			long num3 = 0L;
			foreach (KeyValuePair<int, Incoming> item in _incoming)
			{
				if (item.Value.From == sender)
				{
					list.Add(item.Key);
				}
				else
				{
					num3 += item.Value.Data.Length;
				}
			}
			foreach (int item2 in list)
			{
				_incoming.Remove(item2);
			}
			if (num3 + num2 > 25165824)
			{
				Plugin.Debug($"Net: refused media {num}: too much in flight.");
				return;
			}
			_incoming[num] = new Incoming
			{
				Data = new byte[num2],
				From = sender
			};
			TapeStore.SetProgress(num, 0, num2);
			Plugin.Debug($"Net: media {num} begins, {num2} bytes from {sender}.");
		}

		private static void OnMediaChunk(ulong sender, FastBufferReader r)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			byte[] array = ReadPayload(r);
			if (array == null)
			{
				return;
			}
			int key = BitConverter.ToInt32(array, 0);
			int num = BitConverter.ToInt32(array, 4);
			if (!_incoming.TryGetValue(key, out var value) || value.From != sender)
			{
				return;
			}
			int num2 = array.Length - 8;
			if (num < 0 || num + num2 > value.Data.Length)
			{
				return;
			}
			Buffer.BlockCopy(array, 8, value.Data, num, num2);
			value.Received += num2;
			TapeStore.SetProgress(key, value.Received, value.Data.Length);
			if (value.Received < value.Data.Length)
			{
				return;
			}
			_incoming.Remove(key);
			MediaTrack mediaTrack = MediaTrack.Deserialize(value.Data);
			if (mediaTrack == null)
			{
				TapeStore.MarkUnavailable(key);
				return;
			}
			if (IsHost)
			{
				Relay(key, value.Data, value.From);
			}
			Tape tape = TapeStore.Get(key);
			if (tape == null || tape.Media == null)
			{
				TapeStore.AttachMedia(key, mediaTrack);
			}
		}

		private static void OnMediaNone(ulong sender, FastBufferReader r)
		{
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			//IL_0016: Unknown result type (might be due to invalid IL or missing references)
			int key = default(int);
			((FastBufferReader)(ref r)).ReadValueSafe<int>(ref key, default(ForPrimitives));
			if (!Allowed(sender, key))
			{
				return;
			}
			Tape tape = TapeStore.Get(key);
			if (tape != null && tape.Media != null)
			{
				return;
			}
			TapeStore.MarkUnavailable(key);
			if (!IsHost)
			{
				return;
			}
			foreach (ulong item in Others(sender))
			{
				SendSmall("daniel.blackbox.mnone", item, delegate(FastBufferWriter w)
				{
					//IL_000a: Unknown result type (might be due to invalid IL or miss