Decompiled source of Malfunctions v1.10.0

Malfunctions.dll

Decompiled 3 weeks 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.InteropServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using GameNetcodeStuff;
using HarmonyLib;
using LethalNetworkAPI;
using Malfunctions.Helpers;
using TMPro;
using Unity.Netcode;
using UnityEngine;
using UnityEngine.VFX;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("Malfunctions")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Malfunctions")]
[assembly: AssemblyCopyright("Copyright © zealsprince 2024")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("8c966d9e-bf7c-4f8b-a94b-b87b3b65b941")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
namespace Malfunctions
{
	internal class Assets
	{
		public enum LoadStatusCode : ushort
		{
			Success,
			Failed,
			Exists
		}

		public static AssetBundle Bundle;

		public static Dictionary<string, GameObject> Prefabs = new Dictionary<string, GameObject>();

		public static readonly Dictionary<string, string> Manifest = new Dictionary<string, string> { { "sparks", "assets/exported/malfunctions/effects/sparks.prefab" } };

		public static LoadStatusCode Load()
		{
			if ((Object)(object)Bundle != (Object)null)
			{
				return LoadStatusCode.Exists;
			}
			string text = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Malfunctions");
			Bundle = AssetBundle.LoadFromFile(text);
			if ((Object)(object)Bundle == (Object)null)
			{
				Plugin.logger.LogInfo((object)("Failed to load asset bundle from path: " + text));
				return LoadStatusCode.Failed;
			}
			Plugin.logger.LogInfo((object)("Loaded asset bundle from path: " + text));
			string[] allAssetNames = Bundle.GetAllAssetNames();
			foreach (string text2 in allAssetNames)
			{
				Plugin.logger.LogDebug((object)("Loaded asset: " + text2));
			}
			foreach (KeyValuePair<string, string> item in Manifest)
			{
				Prefabs.Add(item.Key, Bundle.LoadAsset<GameObject>(item.Value));
			}
			return LoadStatusCode.Success;
		}

		public static GameObject SpawnPrefab(string name, Vector3 position)
		{
			//IL_0032: Unknown result type (might be due to invalid IL or missing references)
			//IL_0033: Unknown result type (might be due to invalid IL or missing references)
			try
			{
				if (Prefabs.ContainsKey(name))
				{
					Plugin.logger.LogDebug((object)("Loading prefab '" + name + "'"));
					return Object.Instantiate<GameObject>(Prefabs[name], position, Quaternion.identity);
				}
				Plugin.logger.LogWarning((object)("Prefab " + name + " not found!"));
			}
			catch (Exception ex) when (ex is ArgumentException || ex is NullReferenceException)
			{
				Plugin.logger.LogError((object)ex.Message);
			}
			return null;
		}
	}
	public class Config
	{
		public static ConfigEntry<double> MalfunctionChanceNavigation;

		public static ConfigEntry<double> MalfunctionChanceTeleporter;

		public static ConfigEntry<double> MalfunctionChanceDistortion;

		public static ConfigEntry<double> MalfunctionChanceDoor;

		public static ConfigEntry<double> MalfunctionChanceLever;

		public static ConfigEntry<double> MalfunctionChancePower;

		public static ConfigEntry<int> MalfunctionPassedDaysNavigation;

		public static ConfigEntry<int> MalfunctionPassedDaysTeleporter;

		public static ConfigEntry<int> MalfunctionPassedDaysDistortion;

		public static ConfigEntry<int> MalfunctionPassedDaysDoor;

		public static ConfigEntry<int> MalfunctionPassedDaysLever;

		public static ConfigEntry<int> MalfunctionPassedDaysPower;

		public static ConfigEntry<bool> MalfunctionPenaltyEnabled;

		public static ConfigEntry<bool> MalfunctionPenaltyOnly;

		public static ConfigEntry<double> MalfunctionPenaltyMultiplier;

		public static ConfigEntry<bool> MalfunctionPowerBlockLever;

		public static ConfigEntry<double> MalfunctionPowerBlockLeverChance;

		public static ConfigEntry<bool> MalfunctionMiscAllowConsecutive;

		public static ConfigEntry<bool> MalfunctionVFXDisableSparks;

		public static ConfigEntry<bool> MalfunctionVFXDisableSparksSound;

		public static void Load()
		{
			//IL_0039: Unknown result type (might be due to invalid IL or missing references)
			//IL_0043: Expected O, but got Unknown
			//IL_0081: Unknown result type (might be due to invalid IL or missing references)
			//IL_008b: Expected O, but got Unknown
			//IL_00c9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d3: Expected O, but got Unknown
			//IL_0111: Unknown result type (might be due to invalid IL or missing references)
			//IL_011b: Expected O, but got Unknown
			//IL_0159: Unknown result type (might be due to invalid IL or missing references)
			//IL_0163: Expected O, but got Unknown
			//IL_01a1: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ab: Expected O, but got Unknown
			//IL_01cb: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d5: Expected O, but got Unknown
			//IL_01f6: Unknown result type (might be due to invalid IL or missing references)
			//IL_0200: Expected O, but got Unknown
			//IL_0220: Unknown result type (might be due to invalid IL or missing references)
			//IL_022a: Expected O, but got Unknown
			//IL_024a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0254: Expected O, but got Unknown
			//IL_0274: Unknown result type (might be due to invalid IL or missing references)
			//IL_027e: Expected O, but got Unknown
			//IL_029f: Unknown result type (might be due to invalid IL or missing references)
			//IL_02a9: Expected O, but got Unknown
			//IL_02c9: Unknown result type (might be due to invalid IL or missing references)
			//IL_02d3: Expected O, but got Unknown
			//IL_02f3: Unknown result type (might be due to invalid IL or missing references)
			//IL_02fd: Expected O, but got Unknown
			//IL_0325: Unknown result type (might be due to invalid IL or missing references)
			//IL_032f: Expected O, but got Unknown
			//IL_034f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0359: Expected O, but got Unknown
			//IL_0397: Unknown result type (might be due to invalid IL or missing references)
			//IL_03a1: Expected O, but got Unknown
			//IL_03c1: Unknown result type (might be due to invalid IL or missing references)
			//IL_03cb: Expected O, but got Unknown
			//IL_03eb: Unknown result type (might be due to invalid IL or missing references)
			//IL_03f5: Expected O, but got Unknown
			//IL_0415: Unknown result type (might be due to invalid IL or missing references)
			//IL_041f: Expected O, but got Unknown
			MalfunctionChanceNavigation = Plugin.config.Bind<double>("Chances", "MalfunctionChanceNavigation", 7.5, new ConfigDescription("Set the chance of the navigation malfunction happening - this will force the ship to route to a random moon with no regard to cost", (AcceptableValueBase)(object)new AcceptableValueRange<double>(0.0, 100.0), Array.Empty<object>()));
			MalfunctionChanceTeleporter = Plugin.config.Bind<double>("Chances", "MalfunctionChanceTeleporter", 7.5, new ConfigDescription("Set the chance of the teleporter malfunction happening - this will cause teleporters to disable themselves either at landing or after a random interval into the match", (AcceptableValueBase)(object)new AcceptableValueRange<double>(0.0, 100.0), Array.Empty<object>()));
			MalfunctionChanceDistortion = Plugin.config.Bind<double>("Chances", "MalfunctionChanceDistortion", 5.0, new ConfigDescription("Set the chance of the distortion malfunction happening - this will cause the map and terminal displays as well as walkies to become unusable either at landing or after a random interval into the match", (AcceptableValueBase)(object)new AcceptableValueRange<double>(0.0, 100.0), Array.Empty<object>()));
			MalfunctionChanceDoor = Plugin.config.Bind<double>("Chances", "MalfunctionChanceDoor", 3.0, new ConfigDescription("Set the chance of the door malfunction happening - this will disable ship door controls either at landing or after a random interval into the match", (AcceptableValueBase)(object)new AcceptableValueRange<double>(0.0, 100.0), Array.Empty<object>()));
			MalfunctionChanceLever = Plugin.config.Bind<double>("Chances", "MalfunctionChanceLever", 3.0, new ConfigDescription("Set the chance of the lever malfunction happening - this will disable ship lever after a random but announced delay", (AcceptableValueBase)(object)new AcceptableValueRange<double>(0.0, 100.0), Array.Empty<object>()));
			MalfunctionChancePower = Plugin.config.Bind<double>("Chances", "MalfunctionChancePower", 1.5, new ConfigDescription("Set the chance of the power malfunction happening - this will make the entire ship to go dark after landing, disabling battery charging, door controls, terminal and map displays", (AcceptableValueBase)(object)new AcceptableValueRange<double>(0.0, 100.0), Array.Empty<object>()));
			MalfunctionPassedDaysNavigation = Plugin.config.Bind<int>("Passed Days", "MalfunctionPassedDaysNavigation", 3, new ConfigDescription("Set how many days must have passed for the navigation malfunction to enable", (AcceptableValueBase)null, Array.Empty<object>()));
			MalfunctionPassedDaysTeleporter = Plugin.config.Bind<int>("Passed Days", "MalfunctionPassedDaysTeleporter", 11, new ConfigDescription("Set how many days must have passed for the teleporter malfunction to enable", (AcceptableValueBase)null, Array.Empty<object>()));
			MalfunctionPassedDaysDistortion = Plugin.config.Bind<int>("Passed Days", "MalfunctionPassedDaysDistortion", 3, new ConfigDescription("Set how many days must have passed for the distortion malfunction to enable", (AcceptableValueBase)null, Array.Empty<object>()));
			MalfunctionPassedDaysDoor = Plugin.config.Bind<int>("Passed Days", "MalfunctionPassedDaysDoor", 7, new ConfigDescription("Set how many days must have passed for the door malfunction to enable", (AcceptableValueBase)null, Array.Empty<object>()));
			MalfunctionPassedDaysLever = Plugin.config.Bind<int>("Passed Days", "MalfunctionPassedDaysLever", 0, new ConfigDescription("Set how many days must have passed for the lever malfunction to enable", (AcceptableValueBase)null, Array.Empty<object>()));
			MalfunctionPassedDaysPower = Plugin.config.Bind<int>("Passed Days", "MalfunctionPassedDaysPower", 11, new ConfigDescription("Set how many days must have passed for the power malfunction to enable", (AcceptableValueBase)null, Array.Empty<object>()));
			MalfunctionPenaltyEnabled = Plugin.config.Bind<bool>("Penalty", "MalfunctionPenaltyEnabled", true, new ConfigDescription("Enable the penalty system that increases malfunction chances after not recovering a player", (AcceptableValueBase)null, Array.Empty<object>()));
			MalfunctionPenaltyOnly = Plugin.config.Bind<bool>("Penalty", "MalfunctionPenaltyOnly", false, new ConfigDescription("Only enable malfunctions when a player has not been recovered", (AcceptableValueBase)null, Array.Empty<object>()));
			MalfunctionPenaltyMultiplier = Plugin.config.Bind<double>("Penalty", "MalfunctionPenaltyMultiplier", 2.0, new ConfigDescription("Set the multiplier on triggering a malfunction after not recovering a player", (AcceptableValueBase)null, Array.Empty<object>()));
			MalfunctionPowerBlockLever = Plugin.config.Bind<bool>("Power Malfunction", "MalfunctionPowerBlockLever", true, new ConfigDescription("Enable a chance of pulling the lever and taking off blocking when the power malfunction is active", (AcceptableValueBase)null, Array.Empty<object>()));
			MalfunctionPowerBlockLeverChance = Plugin.config.Bind<double>("Power Malfunction", "MalfunctionPowerBlockLeverChance", 50.0, new ConfigDescription("Chance that pulling the lever will not cause take-off", (AcceptableValueBase)(object)new AcceptableValueRange<double>(0.0, 100.0), Array.Empty<object>()));
			MalfunctionMiscAllowConsecutive = Plugin.config.Bind<bool>("Miscellaneous", "MalfunctionMiscAllowConsecutive", false, new ConfigDescription("Allow malfunctions to trigger consecutively - by default if a malfunction is triggered it can not repeat the next day", (AcceptableValueBase)null, Array.Empty<object>()));
			MalfunctionVFXDisableSparks = Plugin.config.Bind<bool>("VFX", "MalfunctionVFXDisableSparks", false, new ConfigDescription("Disable spark effects appearing on objects if they are having a malfunction", (AcceptableValueBase)null, Array.Empty<object>()));
			MalfunctionVFXDisableSparksSound = Plugin.config.Bind<bool>("VFX", "MalfunctionVFXDisableSparksSound", false, new ConfigDescription("Disable the loud snapping spark sound effects", (AcceptableValueBase)null, Array.Empty<object>()));
		}
	}
	internal class Malfunction
	{
		public bool Active;

		public bool Notified;

		public bool RollSucceeded;

		protected List<GameObject> effects;

		public Malfunction()
		{
			Active = false;
			Notified = false;
			RollSucceeded = false;
			effects = new List<GameObject>();
		}

		public void AssignChild(GameObject child)
		{
			effects.Add(child);
		}

		public void DestroyChildren()
		{
			foreach (GameObject effect in effects)
			{
				Object.Destroy((Object)(object)effect);
			}
			effects.Clear();
		}

		public virtual void Reset()
		{
			Active = false;
			Notified = false;
			DestroyChildren();
		}
	}
	internal class MalfunctionWithTrigger : Malfunction
	{
		public bool Triggered;

		public MalfunctionWithTrigger()
		{
			Triggered = false;
		}

		public override void Reset()
		{
			base.Reset();
			Triggered = false;
		}
	}
	internal class MalfunctionWithDelay : MalfunctionWithTrigger
	{
		public int Delay;

		public MalfunctionWithDelay()
		{
			Delay = 0;
		}

		public override void Reset()
		{
			base.Reset();
			Delay = 0;
		}
	}
	internal class State
	{
		public static int PreviousMoon;

		public static Malfunction MalfunctionNavigation;

		public static MalfunctionWithDelay MalfunctionTeleporter;

		public static MalfunctionWithDelay MalfunctionDistortion;

		public static MalfunctionWithDelay MalfunctionDoor;

		public static MalfunctionWithDelay MalfunctionLever;

		public static MalfunctionWithDelay MalfunctionPower;

		public static void Load()
		{
			PreviousMoon = -1;
			MalfunctionNavigation = new Malfunction();
			MalfunctionTeleporter = new MalfunctionWithDelay();
			MalfunctionDistortion = new MalfunctionWithDelay();
			MalfunctionDoor = new MalfunctionWithDelay();
			MalfunctionLever = new MalfunctionWithDelay();
			MalfunctionPower = new MalfunctionWithDelay();
		}

		public static void Reset()
		{
			MalfunctionNavigation.Reset();
			MalfunctionTeleporter.Reset();
			MalfunctionDistortion.Reset();
			MalfunctionDoor.Reset();
			MalfunctionLever.Reset();
			MalfunctionPower.Reset();
		}
	}
	[BepInPlugin("com.zealsprince.malfunctions", "Malfunctions", "1.10.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class Plugin : BaseUnityPlugin
	{
		public const string ModGUID = "com.zealsprince.malfunctions";

		public const string ModName = "Malfunctions";

		public const string ModVersion = "1.10.0";

		public static ManualLogSource logger;

		public static ConfigFile config;

		private readonly Harmony harmony = new Harmony("com.zealsprince.malfunctions");

		private void Awake()
		{
			logger = ((BaseUnityPlugin)this).Logger;
			config = ((BaseUnityPlugin)this).Config;
			Config.Load();
			if (Assets.Load() == Assets.LoadStatusCode.Success)
			{
				State.Load();
				harmony.PatchAll();
			}
		}
	}
}
namespace Malfunctions.Patches
{
	[HarmonyPatch(typeof(HangarShipDoor))]
	internal class HangarShipDoorPatches
	{
		private static float lockdownTime;

		[HarmonyPostfix]
		[HarmonyPatch("Update")]
		private static void OverwriteDoorPower(HangarShipDoor __instance)
		{
			if (!State.MalfunctionPower.Active && State.MalfunctionDoor.Active && State.MalfunctionDoor.Triggered)
			{
				__instance.doorPower = 1f;
				lockdownTime = (lockdownTime + Time.deltaTime) % 6f;
				((TMP_Text)__instance.doorPowerDisplay).text = ((lockdownTime > 3f) ? "LOCKED" : "OPEN 10PM") ?? "";
			}
		}
	}
	[HarmonyPatch(typeof(StartMatchLever))]
	internal class StartMatchLeverPatches
	{
		[HarmonyPrefix]
		[HarmonyPatch("PullLever")]
		private static bool InterruptPullLever(StartMatchLever __instance)
		{
			if (State.MalfunctionPower.Active && State.MalfunctionPower.Triggered)
			{
				if (Config.MalfunctionPowerBlockLever.Value && State.MalfunctionPower.Delay != 0)
				{
					((TMP_Text)HUDManager.Instance.globalNotificationText).text = "SHIP CORE DEPLETION:\nAWAIT 12AM EMERGENCY AUTOPILOT";
					HUDManager.Instance.globalNotificationAnimator.SetTrigger("TriggerNotif");
					HUDManager.Instance.UIAudio.PlayOneShot(HUDManager.Instance.radiationWarningAudio, 1f);
					__instance.triggerScript.disabledHoverTip = "[No power to hydraulics]";
					return false;
				}
			}
			else if (State.MalfunctionLever.Active && State.MalfunctionLever.Triggered)
			{
				((TMP_Text)HUDManager.Instance.globalNotificationText).text = "SHIP LEVER HYDRAULICS JAM:\nAWAIT 12AM EMERGENCY AUTOPILOT";
				HUDManager.Instance.globalNotificationAnimator.SetTrigger("TriggerNotif");
				HUDManager.Instance.UIAudio.PlayOneShot(HUDManager.Instance.radiationWarningAudio, 1f);
				return false;
			}
			return true;
		}
	}
	[HarmonyPatch(typeof(WalkieTalkie))]
	internal class WalkieTalkiePatches
	{
		[HarmonyPrefix]
		[HarmonyPatch("ItemActivate")]
		private static bool InterruptItemActivate(WalkieTalkie __instance)
		{
			if (State.MalfunctionDistortion.Active && State.MalfunctionDistortion.Triggered)
			{
				__instance.thisAudio.PlayOneShot(__instance.playerDieOnWalkieTalkieSFX);
				((GrabbableObject)__instance).UseUpBatteries();
				return false;
			}
			return true;
		}
	}
	[HarmonyPatch(typeof(ShipLights))]
	internal class ShipLightsPatches
	{
		[HarmonyPrefix]
		[HarmonyPatch("ToggleShipLights")]
		private static bool InterruptToggleShipLights()
		{
			if (State.MalfunctionPower.Active && State.MalfunctionPower.Triggered)
			{
				return false;
			}
			return true;
		}
	}
	[HarmonyPatch(typeof(ItemCharger))]
	internal class ItemChargerPatches
	{
		[HarmonyPrefix]
		[HarmonyPatch("ChargeItem")]
		private static bool InterruptChargeItem()
		{
			if (State.MalfunctionPower.Active && State.MalfunctionPower.Triggered)
			{
				return false;
			}
			return true;
		}
	}
	[HarmonyPatch(typeof(ManualCameraRenderer))]
	internal class ManualCameraRendererPatches
	{
		[HarmonyPrefix]
		[HarmonyPatch("SwitchScreenButton")]
		private static bool InterruptSwitchScreenButton()
		{
			if ((State.MalfunctionPower.Active && State.MalfunctionPower.Triggered) || (State.MalfunctionDistortion.Active && State.MalfunctionDistortion.Triggered))
			{
				return false;
			}
			return true;
		}

		[HarmonyPrefix]
		[HarmonyPatch("SwitchRadarTargetClientRpc")]
		private static bool InterruptSwitchCameraView()
		{
			if ((State.MalfunctionPower.Active && State.MalfunctionPower.Triggered) || (State.MalfunctionDistortion.Active && State.MalfunctionDistortion.Triggered))
			{
				return false;
			}
			return true;
		}
	}
	[HarmonyPatch(typeof(ShipTeleporter))]
	internal class ShipTeleporterPatches
	{
		[HarmonyPrefix]
		[HarmonyPatch("PressTeleportButtonOnLocalClient")]
		private static bool InterruptPressTeleportButton(ShipTeleporter __instance)
		{
			if ((State.MalfunctionTeleporter.Active && State.MalfunctionTeleporter.Triggered) || (State.MalfunctionPower.Active && State.MalfunctionPower.Triggered))
			{
				__instance.buttonAnimator.SetTrigger("press");
				__instance.buttonAudio.PlayOneShot(__instance.buttonPressSFX);
				return false;
			}
			return true;
		}

		[HarmonyPostfix]
		[HarmonyPatch("PressTeleportButtonOnLocalClient")]
		private static void RandomizePressTeleportButton()
		{
			if (State.MalfunctionDistortion.Active && State.MalfunctionDistortion.Triggered)
			{
				PlayerControllerB[] array = (from player in Object.FindObjectsByType<PlayerControllerB>((FindObjectsSortMode)0)
					where player.isPlayerControlled
					select player).ToArray();
				if (array.Length != 0)
				{
					StartOfRound.Instance.mapScreen.targetedPlayer = array[Random.Range(0, array.Length)];
				}
			}
		}
	}
	[HarmonyPatch(typeof(Terminal))]
	internal class TerminalPatches
	{
		[HarmonyPrefix]
		[HarmonyPatch("LoadNewNodeIfAffordable")]
		private static bool BlockMoonConfirmNode(Terminal __instance, TerminalNode node)
		{
			if (State.MalfunctionNavigation.Active && node.buyRerouteToMoon > -1)
			{
				node.displayText = "ROUTE TABLE OFFSET: NAVIGATION ERROR\n\nIT IS ADVISED TO LAND THE SHIP TO ALLOW RECALIBRATION OF SYSTEMS AND RESTORATION OF FUNCTIONALITY\n\n";
				node.terminalEvent = "ERROR";
				__instance.PlayTerminalAudioServerRpc(3);
				__instance.LoadNewNode(node);
				return false;
			}
			return true;
		}
	}
	[HarmonyPatch(typeof(TimeOfDay))]
	internal class TimeOfDayPatches
	{
		[HarmonyPostfix]
		[HarmonyPatch("Update")]
		private static void CheckMalfunctionTeleporterTrigger(TimeOfDay __instance)
		{
			//IL_010a: Unknown result type (might be due to invalid IL or missing references)
			if (!State.MalfunctionPower.Active && State.MalfunctionTeleporter.Active && !State.MalfunctionTeleporter.Triggered && ((Object)__instance.currentLevel).name != "CompanyBuildingLevel" && __instance.currentDayTimeStarted && __instance.hour >= 1 + State.MalfunctionTeleporter.Delay)
			{
				Plugin.logger.LogDebug((object)"Triggered teleporter malfunction!");
				State.MalfunctionTeleporter.Triggered = true;
				if (!State.MalfunctionTeleporter.Notified)
				{
					((TMP_Text)HUDManager.Instance.globalNotificationText).text = "SHIP TELEPORTER MALFUNCTION:\nTIMEOUT FROM ATOMIC MISALIGNMENT";
					HUDManager.Instance.globalNotificationAnimator.SetTrigger("TriggerNotif");
					HUDManager.Instance.UIAudio.PlayOneShot(HUDManager.Instance.radiationWarningAudio, 1f);
					State.MalfunctionTeleporter.Notified = true;
				}
				ShipTeleporter val = Object.FindObjectOfType<ShipTeleporter>();
				if ((Object)(object)val == (Object)null)
				{
					Plugin.logger.LogError((object)"Failed to find a teleporter object.");
					return;
				}
				GameObject val2 = Assets.SpawnPrefab("sparks", ((Component)val).transform.position);
				val2.transform.SetParent(((Component)val).transform, true);
				State.MalfunctionTeleporter.AssignChild(val2);
			}
		}

		[HarmonyPostfix]
		[HarmonyPatch("Update")]
		private static void CheckMalfunctionDistortionTrigger(TimeOfDay __instance)
		{
			//IL_012c: Unknown result type (might be due to invalid IL or missing references)
			if (!State.MalfunctionPower.Active && State.MalfunctionDistortion.Active && !State.MalfunctionDistortion.Triggered && ((Object)__instance.currentLevel).name != "CompanyBuildingLevel" && __instance.currentDayTimeStarted && __instance.hour >= 1 + State.MalfunctionDistortion.Delay)
			{
				Plugin.logger.LogDebug((object)"Triggered distortion malfunction!");
				State.MalfunctionDistortion.Triggered = true;
				if (!State.MalfunctionDistortion.Notified)
				{
					((TMP_Text)HUDManager.Instance.globalNotificationText).text = "SHIP COMS DISTURBANCE:\nELECTROMAGNETIC ANOMALY";
					HUDManager.Instance.globalNotificationAnimator.SetTrigger("TriggerNotif");
					HUDManager.Instance.UIAudio.PlayOneShot(HUDManager.Instance.radiationWarningAudio, 1f);
					State.MalfunctionDistortion.Notified = true;
				}
				__instance.playersManager.mapScreen.SwitchScreenOn(false);
				Terminal val = Object.FindObjectOfType<Terminal>();
				if ((Object)(object)val == (Object)null)
				{
					Plugin.logger.LogError((object)"Failed to find the terminal object.");
					return;
				}
				((Component)val).gameObject.GetComponent<InteractTrigger>().interactable = false;
				GameObject val2 = Assets.SpawnPrefab("sparks", ((Component)val).transform.position);
				val2.transform.SetParent(((Component)val).transform, true);
				State.MalfunctionDistortion.AssignChild(val2);
			}
		}

		[HarmonyPostfix]
		[HarmonyPatch("Update")]
		private static void CheckMalfunctionDoorTrigger(TimeOfDay __instance)
		{
			//IL_015c: Unknown result type (might be due to invalid IL or missing references)
			if (!State.MalfunctionPower.Active && State.MalfunctionDoor.Active && !State.MalfunctionDoor.Triggered && ((Object)__instance.currentLevel).name != "CompanyBuildingLevel")
			{
				if (!__instance.currentDayTimeStarted || __instance.hour < 1 + State.MalfunctionDoor.Delay || __instance.hour >= 16)
				{
					return;
				}
				Plugin.logger.LogDebug((object)"Triggered door malfunction!");
				State.MalfunctionDoor.Triggered = true;
				if (!State.MalfunctionDoor.Notified)
				{
					((TMP_Text)HUDManager.Instance.globalNotificationText).text = "SHIP DOOR LOCK FAIL:\nCRACKDOWN PROTOCOL ACTIVE";
					HUDManager.Instance.globalNotificationAnimator.SetTrigger("TriggerNotif");
					HUDManager.Instance.UIAudio.PlayOneShot(HUDManager.Instance.radiationWarningAudio, 1f);
					State.MalfunctionDoor.Notified = true;
				}
				HangarShipDoor val = Object.FindAnyObjectByType<HangarShipDoor>();
				if ((Object)(object)val != (Object)null)
				{
					val.SetDoorClosed();
					val.PlayDoorAnimation(true);
				}
				GameObject val2 = GameObject.Find("HangarDoorButtonPanel");
				if ((Object)(object)val2 == (Object)null)
				{
					Plugin.logger.LogError((object)"Failed to find door panel object.");
					return;
				}
				InteractTrigger[] componentsInChildren = val2.GetComponentsInChildren<InteractTrigger>(true);
				for (int i = 0; i < componentsInChildren.Length; i++)
				{
					componentsInChildren[i].interactable = false;
				}
				GameObject val3 = Assets.SpawnPrefab("sparks", val2.transform.position);
				val3.transform.SetParent(val2.transform, true);
				State.MalfunctionDoor.AssignChild(val3);
			}
			else
			{
				if (State.MalfunctionPower.Active || !State.MalfunctionDoor.Active || !State.MalfunctionDoor.Triggered || __instance.hour < 16)
				{
					return;
				}
				State.MalfunctionDoor.Triggered = false;
				HangarShipDoor val4 = Object.FindAnyObjectByType<HangarShipDoor>();
				if ((Object)(object)val4 != (Object)null)
				{
					val4.SetDoorOpen();
					val4.PlayDoorAnimation(false);
				}
				GameObject val5 = GameObject.Find("HangarDoorButtonPanel");
				if ((Object)(object)val5 != (Object)null)
				{
					InteractTrigger[] componentsInChildren = val5.GetComponentsInChildren<InteractTrigger>(true);
					for (int i = 0; i < componentsInChildren.Length; i++)
					{
						componentsInChildren[i].interactable = true;
					}
				}
			}
		}

		[HarmonyPostfix]
		[HarmonyPatch("Update")]
		private static void CheckMalfunctionLeverTrigger(TimeOfDay __instance)
		{
			//IL_018e: Unknown result type (might be due to invalid IL or missing references)
			if (State.MalfunctionPower.Active || State.MalfunctionDoor.Active || !State.MalfunctionLever.Active || !(((Object)__instance.currentLevel).name != "CompanyBuildingLevel"))
			{
				return;
			}
			if (!State.MalfunctionLever.Notified && __instance.currentDayTimeStarted && __instance.hour >= 3 + State.MalfunctionLever.Delay && __instance.hour < 16)
			{
				Plugin.logger.LogDebug((object)"Notified lever malfunction!");
				int num = State.MalfunctionLever.Delay;
				if (num == 0)
				{
					num = 12;
				}
				((TMP_Text)HUDManager.Instance.globalNotificationText).text = $"MANUAL HYDRAULICS FAILURE IMMINENT:\nLEAVE BEFORE {num}PM OR AWAIT AUTOPILOT";
				HUDManager.Instance.globalNotificationAnimator.SetTrigger("TriggerNotif");
				HUDManager.Instance.UIAudio.PlayOneShot(HUDManager.Instance.radiationWarningAudio, 1f);
				State.MalfunctionLever.Notified = true;
			}
			else if (!State.MalfunctionLever.Triggered && __instance.currentDayTimeStarted && __instance.hour >= 6 + State.MalfunctionLever.Delay && __instance.hour < 16)
			{
				Plugin.logger.LogDebug((object)"Triggered lever malfunction!");
				State.MalfunctionLever.Triggered = true;
				StartMatchLever val = Object.FindObjectOfType<StartMatchLever>();
				if ((Object)(object)val == (Object)null)
				{
					Plugin.logger.LogError((object)"Failed to find lever device object.");
					return;
				}
				val.triggerScript.disabledHoverTip = "[The lever is jammed]";
				GameObject val2 = Assets.SpawnPrefab("sparks", ((Component)val).transform.position);
				val2.transform.SetParent(((Component)val).transform, true);
				State.MalfunctionLever.AssignChild(val2);
			}
		}
	}
	[HarmonyPatch(typeof(StartOfRound))]
	internal class StartOfRoundPatches
	{
		public class MalfunctionNavigationNetworkData
		{
			public bool result;

			public int levelId;

			public MalfunctionNavigationNetworkData(bool result, int levelId)
			{
				this.result = result;
				this.levelId = levelId;
			}
		}

		public class MalfunctionTeleporterNetworkData
		{
			public bool result;

			public int delay;

			public MalfunctionTeleporterNetworkData(bool result, int delay)
			{
				this.result = result;
				this.delay = delay;
			}
		}

		public class MalfunctionDistortionNetworkData
		{
			public bool result;

			public int delay;

			public MalfunctionDistortionNetworkData(bool result, int delay)
			{
				this.result = result;
				this.delay = delay;
			}
		}

		public class MalfunctionDoorNetworkData
		{
			public bool result;

			public int delay;

			public MalfunctionDoorNetworkData(bool result, int delay)
			{
				this.result = result;
				this.delay = delay;
			}
		}

		public class MalfunctionLeverNetworkData
		{
			public bool result;

			public int delay;

			public MalfunctionLeverNetworkData(bool result, int delay)
			{
				this.result = result;
				this.delay = delay;
			}
		}

		public class MalfunctionPowerNetworkData
		{
			public bool result;

			public bool blockResult;

			public MalfunctionPowerNetworkData(bool result, bool blockResult)
			{
				this.result = result;
				this.blockResult = blockResult;
			}
		}

		private static GameObject elevatorPanelScreen;

		private static GameObject floodlight1;

		private static GameObject floodlight2;

		private static Material floodlightMaterial;

		private static bool hadUnrecoveredDeadPlayers;

		private static string originalLeverDisableTooltip = "";

		public static LethalClientEvent MalfunctionResetNetworkClientMessage = new LethalClientEvent("MALFUNCTION_RESET", (Action)null, (Action<ulong>)null);

		public static LethalServerMessage<MalfunctionNavigationNetworkData> MalfunctionNavigationNetworkServerMessage = new LethalServerMessage<MalfunctionNavigationNetworkData>("MALFUNCTION_NAVIGATION", (Action<MalfunctionNavigationNetworkData, ulong>)null);

		public static LethalClientMessage<MalfunctionNavigationNetworkData> MalfunctionNavigationNetworkClientMessage = new LethalClientMessage<MalfunctionNavigationNetworkData>("MALFUNCTION_NAVIGATION", (Action<MalfunctionNavigationNetworkData>)null, (Action<MalfunctionNavigationNetworkData, ulong>)null);

		public static LethalServerMessage<MalfunctionTeleporterNetworkData> MalfunctionTeleporterNetworkServerMessage = new LethalServerMessage<MalfunctionTeleporterNetworkData>("MALFUNCTION_TELEPORTER", (Action<MalfunctionTeleporterNetworkData, ulong>)null);

		public static LethalClientMessage<MalfunctionTeleporterNetworkData> MalfunctionTeleporterNetworkClientMessage = new LethalClientMessage<MalfunctionTeleporterNetworkData>("MALFUNCTION_TELEPORTER", (Action<MalfunctionTeleporterNetworkData>)null, (Action<MalfunctionTeleporterNetworkData, ulong>)null);

		public static LethalServerMessage<MalfunctionDistortionNetworkData> MalfunctionDistortionNetworkServerMessage = new LethalServerMessage<MalfunctionDistortionNetworkData>("MALFUNCTION_DISTORTION", (Action<MalfunctionDistortionNetworkData, ulong>)null);

		public static LethalClientMessage<MalfunctionDistortionNetworkData> MalfunctionDistortionNetworkClientMessage = new LethalClientMessage<MalfunctionDistortionNetworkData>("MALFUNCTION_DISTORTION", (Action<MalfunctionDistortionNetworkData>)null, (Action<MalfunctionDistortionNetworkData, ulong>)null);

		public static LethalServerMessage<MalfunctionDoorNetworkData> MalfunctionDoorNetworkServerMessage = new LethalServerMessage<MalfunctionDoorNetworkData>("MALFUNCTION_DOOR", (Action<MalfunctionDoorNetworkData, ulong>)null);

		public static LethalClientMessage<MalfunctionDoorNetworkData> MalfunctionDoorNetworkClientMessage = new LethalClientMessage<MalfunctionDoorNetworkData>("MALFUNCTION_DOOR", (Action<MalfunctionDoorNetworkData>)null, (Action<MalfunctionDoorNetworkData, ulong>)null);

		public static LethalServerMessage<MalfunctionLeverNetworkData> MalfunctionLeverNetworkServerMessage = new LethalServerMessage<MalfunctionLeverNetworkData>("MALFUNCTION_LEVER", (Action<MalfunctionLeverNetworkData, ulong>)null);

		public static LethalClientMessage<MalfunctionLeverNetworkData> MalfunctionLeverNetworkClientMessage = new LethalClientMessage<MalfunctionLeverNetworkData>("MALFUNCTION_LEVER", (Action<MalfunctionLeverNetworkData>)null, (Action<MalfunctionLeverNetworkData, ulong>)null);

		public static LethalServerMessage<MalfunctionPowerNetworkData> MalfunctionPowerNetworkServerMessage = new LethalServerMessage<MalfunctionPowerNetworkData>("MALFUNCTION_POWER", (Action<MalfunctionPowerNetworkData, ulong>)null);

		public static LethalClientMessage<MalfunctionPowerNetworkData> MalfunctionPowerNetworkClientMessage = new LethalClientMessage<MalfunctionPowerNetworkData>("MALFUNCTION_POWER", (Action<MalfunctionPowerNetworkData>)null, (Action<MalfunctionPowerNetworkData, ulong>)null);

		public static bool NetworkHandlersRegistered = false;

		public static void MalfunctionResetNetworkHandler()
		{
			Plugin.logger.LogDebug((object)"Received networking malfunction reset broadcast!");
			RestoreAfterMalfunctions();
		}

		public static void MalfunctionNavigationNetworkHandler(MalfunctionNavigationNetworkData message)
		{
			Plugin.logger.LogDebug((object)$"Received network message for malfunction navigation roll result: {message.result} (levelId: {message.levelId})");
			HandleRollNavigation(message.result, message.levelId);
		}

		public static void MalfunctionTeleporterNetworkHandler(MalfunctionTeleporterNetworkData message)
		{
			Plugin.logger.LogDebug((object)$"Received network message for malfunction teleporter roll result: {message.result} (Delay: {message.delay})");
			HandleRollTeleporter(message.result, message.delay);
		}

		public static void MalfunctionDistortionNetworkHandler(MalfunctionDistortionNetworkData message)
		{
			Plugin.logger.LogDebug((object)$"Received network message for malfunction distortion roll result: {message.result} (Delay: {message.delay})");
			HandleRollDistortion(message.result, message.delay);
		}

		public static void MalfunctionDoorNetworkHandler(MalfunctionDoorNetworkData message)
		{
			Plugin.logger.LogDebug((object)$"Received network message for malfunction door roll result: {message.result} (Delay: {message.delay})");
			HandleRollDoor(message.result, message.delay);
		}

		public static void MalfunctionLeverNetworkHandler(MalfunctionLeverNetworkData message)
		{
			Plugin.logger.LogDebug((object)$"Received network message for malfunction lever roll result: {message.result} (Delay: {message.delay})");
			HandleRollLever(message.result, message.delay);
		}

		public static void MalfunctionPowerNetworkHandler(MalfunctionPowerNetworkData message)
		{
			Plugin.logger.LogDebug((object)$"Received network message for malfunction power roll result: {message.result} (Block lever: {message.blockResult})");
			HandleRollPower(message.result, message.blockResult);
		}

		[HarmonyPostfix]
		[HarmonyPatch("Start")]
		public static void RegisterNetworkHandlers()
		{
			if (!NetworkHandlersRegistered)
			{
				MalfunctionResetNetworkClientMessage.OnReceived += MalfunctionResetNetworkHandler;
				MalfunctionNavigationNetworkClientMessage.OnReceived += MalfunctionNavigationNetworkHandler;
				MalfunctionTeleporterNetworkClientMessage.OnReceived += MalfunctionTeleporterNetworkHandler;
				MalfunctionDistortionNetworkClientMessage.OnReceived += MalfunctionDistortionNetworkHandler;
				MalfunctionDoorNetworkClientMessage.OnReceived += MalfunctionDoorNetworkHandler;
				MalfunctionLeverNetworkClientMessage.OnReceived += MalfunctionLeverNetworkHandler;
				MalfunctionPowerNetworkClientMessage.OnReceived += MalfunctionPowerNetworkHandler;
				NetworkHandlersRegistered = true;
				Plugin.logger.LogDebug((object)"Registered network handlers!");
			}
		}

		[HarmonyPostfix]
		[HarmonyPatch("Start")]
		public static void DebugShowLevelIds()
		{
		}

		[HarmonyPrefix]
		[HarmonyPatch("EndOfGame")]
		private static void CheckDeadPlayers(StartOfRound __instance, int bodiesInsured)
		{
			int num = __instance.connectedPlayersAmount + 1 - __instance.livingPlayers;
			if (num > bodiesInsured)
			{
				hadUnrecoveredDeadPlayers = true;
				Plugin.logger.LogDebug((object)$"There were unrecovered players! (Players: {__instance.connectedPlayersAmount + 1} / Dead: {num} / Recovered: {bodiesInsured})");
			}
			else
			{
				hadUnrecoveredDeadPlayers = false;
				Plugin.logger.LogDebug((object)$"All players are alive or recovered! (Players: {__instance.connectedPlayersAmount + 1} / Dead: {num} / Recovered: {bodiesInsured})");
			}
		}

		[HarmonyPostfix]
		[HarmonyPatch("ReviveDeadPlayers")]
		private static void RollMalfunctions(StartOfRound __instance)
		{
			RestoreAfterMalfunctions();
			if (Config.MalfunctionMiscAllowConsecutive.Value)
			{
				State.Reset();
			}
			int num = (int)(DateTime.Parse(DateTime.UtcNow.ToString("yyyy-MM-dd")) - new DateTime(1970, 1, 1)).TotalSeconds;
			int num2 = __instance.randomMapSeed + num;
			Random random = new Random(num2);
			Plugin.logger.LogDebug((object)$"Got random synced seed: {num2}");
			Plugin.logger.LogDebug((object)$"Elapsed days: {__instance.gameStats.daysSpent} / Days to next deadline: {TimeOfDay.Instance.daysUntilDeadline}");
			if (!((NetworkBehaviour)__instance).IsServer)
			{
				return;
			}
			double num3 = random.NextDouble() * 100.0;
			double num4 = random.NextDouble() * 100.0;
			double num5 = random.NextDouble() * 100.0;
			double num6 = random.NextDouble() * 100.0;
			double num7 = random.NextDouble() * 100.0;
			double num8 = random.NextDouble() * 100.0;
			double num9 = 1.0;
			if (Config.MalfunctionPenaltyEnabled.Value)
			{
				Plugin.logger.LogDebug((object)"Penalty multiplier active. Checking for unrecovered players.");
				if (hadUnrecoveredDeadPlayers)
				{
					num9 = Config.MalfunctionPenaltyMultiplier.Value;
					Plugin.logger.LogDebug((object)"Had unrecovered players. Increasing malfunction multiplier for this round.");
				}
				else if (Config.MalfunctionPenaltyOnly.Value)
				{
					num9 = 0.0;
					Plugin.logger.LogDebug((object)"No unrecovered players. Setting probability to zero as penalty mode only is enabled.");
				}
			}
			bool flag = Config.MalfunctionChanceNavigation.Value != 0.0 && num3 < Config.MalfunctionChanceNavigation.Value * num9;
			bool flag2 = Config.MalfunctionChanceTeleporter.Value != 0.0 && num4 < Config.MalfunctionChanceTeleporter.Value * num9;
			bool flag3 = Config.MalfunctionChanceDistortion.Value != 0.0 && num5 < Config.MalfunctionChanceDistortion.Value * num9;
			bool flag4 = Config.MalfunctionChanceDoor.Value != 0.0 && num8 < Config.MalfunctionChanceDoor.Value * num9;
			bool flag5 = Config.MalfunctionChanceLever.Value != 0.0 && num7 < Config.MalfunctionChanceLever.Value * num9;
			bool flag6 = Config.MalfunctionChancePower.Value != 0.0 && num6 < Config.MalfunctionChancePower.Value * num9;
			Plugin.logger.LogDebug((object)string.Format("Malfunction Navigation Roll: {0} < {1} ({2})", num3, Config.MalfunctionChanceNavigation.Value * num9, flag ? "SUCCESS" : "FAIL"));
			Plugin.logger.LogDebug((object)string.Format("Malfunction Teleporter Roll: {0} < {1} ({2})", num4, Config.MalfunctionChanceTeleporter.Value * num9, flag2 ? "SUCCESS" : "FAIL"));
			Plugin.logger.LogDebug((object)string.Format("Malfunction Distortion Roll: {0} < {1} ({2})", num5, Config.MalfunctionChanceDistortion.Value * num9, flag3 ? "SUCCESS" : "FAIL"));
			Plugin.logger.LogDebug((object)string.Format("Malfunction Door Roll: {0} < {1} ({2})", num8, Config.MalfunctionChanceDoor.Value * num9, flag4 ? "SUCCESS" : "FAIL"));
			Plugin.logger.LogDebug((object)string.Format("Malfunction Lever Roll: {0} < {1} ({2})", num7, Config.MalfunctionChanceLever.Value * num9, flag5 ? "SUCCESS" : "FAIL"));
			Plugin.logger.LogDebug((object)string.Format("Malfunction Power Roll: {0} < {1} ({2})", num6, Config.MalfunctionChancePower.Value * num9, flag6 ? "SUCCESS" : "FAIL"));
			double num10 = random.NextDouble() * 100.0;
			bool flag7 = Config.MalfunctionPowerBlockLeverChance.Value != 0.0 && num10 < Config.MalfunctionPowerBlockLeverChance.Value;
			Plugin.logger.LogDebug((object)string.Format("Malfunction Power - Block Lever Roll : {0} < {1} ({2})", num10, Config.MalfunctionPowerBlockLeverChance.Value, flag7 ? "SUCCESS" : "FAIL"));
			if (StartOfRound.Instance.gameStats.daysSpent <= Config.MalfunctionPassedDaysNavigation.Value)
			{
				Plugin.logger.LogDebug((object)$"Blocking navigation malfunction because elapsed days hasn't passed required threshold: {StartOfRound.Instance.gameStats.daysSpent} / {Config.MalfunctionPassedDaysNavigation.Value}");
				flag = false;
			}
			SelectableLevel[] array = StartOfRound.Instance.levels.Where((SelectableLevel level) => ((Object)level).name != "CompanyBuildingLevel" && ((Object)level).name != "LiquidationLevel").ToArray();
			SelectableLevel val = random.NextFromCollection(array);
			if (flag)
			{
				SelectableLevel[] array2 = array;
				foreach (SelectableLevel val2 in array2)
				{
					Plugin.logger.LogDebug((object)$"Level ID: {val2.levelID} / Level Name: {((Object)val2).name}");
				}
				Plugin.logger.LogDebug((object)$"Selected Level ID: {val.levelID}");
			}
			MalfunctionNavigationNetworkServerMessage.SendAllClients(new MalfunctionNavigationNetworkData(flag, val.levelID), true);
			if (StartOfRound.Instance.gameStats.daysSpent <= Config.MalfunctionPassedDaysTeleporter.Value)
			{
				Plugin.logger.LogDebug((object)$"Blocking teleporter malfunction because elapsed days hasn't passed required threshold: {StartOfRound.Instance.gameStats.daysSpent} / {Config.MalfunctionPassedDaysTeleporter.Value}");
				flag2 = false;
			}
			int delay = 1 + random.Next(11);
			MalfunctionTeleporterNetworkServerMessage.SendAllClients(new MalfunctionTeleporterNetworkData(flag2, delay), true);
			if (StartOfRound.Instance.gameStats.daysSpent <= Config.MalfunctionPassedDaysDistortion.Value)
			{
				Plugin.logger.LogDebug((object)$"Blocking distortion malfunction because elapsed days hasn't passed required threshold: {StartOfRound.Instance.gameStats.daysSpent} / {Config.MalfunctionPassedDaysDistortion.Value}");
				flag3 = false;
			}
			int delay2 = random.Next(12);
			MalfunctionDistortionNetworkServerMessage.SendAllClients(new MalfunctionDistortionNetworkData(flag3, delay2), true);
			if (StartOfRound.Instance.gameStats.daysSpent <= Config.MalfunctionPassedDaysDoor.Value)
			{
				Plugin.logger.LogDebug((object)$"Blocking door malfunction because elapsed days hasn't passed required threshold: {StartOfRound.Instance.gameStats.daysSpent} / {Config.MalfunctionPassedDaysDoor.Value}");
				flag4 = false;
			}
			int delay3 = 4 + random.Next(8);
			MalfunctionDoorNetworkServerMessage.SendAllClients(new MalfunctionDoorNetworkData(flag4, delay3), true);
			if (StartOfRound.Instance.gameStats.daysSpent <= Config.MalfunctionPassedDaysLever.Value)
			{
				Plugin.logger.LogDebug((object)$"Blocking lever malfunction because elapsed days hasn't passed required threshold: {StartOfRound.Instance.gameStats.daysSpent} / {Config.MalfunctionPassedDaysLever.Value}");
				flag5 = false;
			}
			int delay4 = random.Next(4);
			MalfunctionLeverNetworkServerMessage.SendAllClients(new MalfunctionLeverNetworkData(flag5, delay4), true);
			if (StartOfRound.Instance.gameStats.daysSpent <= Config.MalfunctionPassedDaysPower.Value)
			{
				Plugin.logger.LogDebug((object)$"Blocking power malfunction because elapsed days hasn't passed required threshold: {StartOfRound.Instance.gameStats.daysSpent} / {Config.MalfunctionPassedDaysPower.Value}");
				flag6 = false;
			}
			MalfunctionPowerNetworkServerMessage.SendAllClients(new MalfunctionPowerNetworkData(flag6, flag7), true);
		}

		private static void RestoreAfterMalfunctions()
		{
			StartMatchLever val = Object.FindObjectOfType<StartMatchLever>();
			if ((Object)(object)val == (Object)null)
			{
				Plugin.logger.LogWarning((object)"Failed to find lever device object.");
			}
			else
			{
				val.triggerScript.disabledHoverTip = originalLeverDisableTooltip;
				Plugin.logger.LogDebug((object)"Reset lever");
			}
			Terminal val2 = Object.FindObjectOfType<Terminal>();
			if ((Object)(object)val2 != (Object)null)
			{
				((Component)val2).gameObject.GetComponent<InteractTrigger>().interactable = true;
				Plugin.logger.LogDebug((object)"Reset terminal");
			}
			if ((Object)(object)elevatorPanelScreen == (Object)null)
			{
				Plugin.logger.LogWarning((object)"Failed to find door panel screen from previous reference.");
			}
			else
			{
				elevatorPanelScreen.SetActive(true);
				Plugin.logger.LogDebug((object)"Reset door screen");
			}
			GameObject val3 = GameObject.Find("HangarDoorButtonPanel");
			if ((Object)(object)val3 != (Object)null)
			{
				InteractTrigger[] componentsInChildren = val3.GetComponentsInChildren<InteractTrigger>(true);
				for (int i = 0; i < componentsInChildren.Length; i++)
				{
					componentsInChildren[i].interactable = true;
				}
				Plugin.logger.LogDebug((object)"Reset door panel");
			}
			if ((Object)(object)floodlight1 != (Object)null && (Object)(object)floodlight2 != (Object)null)
			{
				Light[] componentsInChildren2 = floodlight1.GetComponentsInChildren<Light>(true);
				Light[] componentsInChildren3 = floodlight2.GetComponentsInChildren<Light>(true);
				Light[] array = componentsInChildren2;
				for (int i = 0; i < array.Length; i++)
				{
					((Behaviour)array[i]).enabled = true;
				}
				array = componentsInChildren3;
				for (int i = 0; i < array.Length; i++)
				{
					((Behaviour)array[i]).enabled = true;
				}
				MeshRenderer component = floodlight1.GetComponent<MeshRenderer>();
				MeshRenderer component2 = floodlight2.GetComponent<MeshRenderer>();
				if ((Object)(object)component != (Object)null && (Object)(object)component2 != (Object)null)
				{
					Material[] materials = ((Renderer)component).materials;
					materials[2] = floodlightMaterial;
					((Renderer)component).materials = materials;
					((Renderer)component2).materials = materials;
				}
				Plugin.logger.LogDebug((object)"Restored lights");
			}
		}

		private static void HandleRollNavigation(bool result, int level)
		{
			if (State.MalfunctionNavigation.Active || TimeOfDay.Instance.daysUntilDeadline < 2)
			{
				State.MalfunctionNavigation.Reset();
			}
			else if (TimeOfDay.Instance.daysUntilDeadline >= 2 && result)
			{
				StartOfRound.Instance.ChangeLevel(level);
				State.MalfunctionNavigation.Active = true;
			}
		}

		private static void HandleRollTeleporter(bool result, int delay)
		{
			if (State.MalfunctionTeleporter.Active || TimeOfDay.Instance.daysUntilDeadline < 2)
			{
				State.MalfunctionTeleporter.Reset();
			}
			else if (!State.MalfunctionPower.Active && TimeOfDay.Instance.daysUntilDeadline >= 2 && result)
			{
				if ((Object)(object)Object.FindObjectOfType<ShipTeleporter>() != (Object)null)
				{
					State.MalfunctionTeleporter.Delay = delay;
					State.MalfunctionTeleporter.Active = true;
					Plugin.logger.LogDebug((object)$"Teleporter malfunction will trigger at {7 + State.MalfunctionTeleporter.Delay}:00");
				}
				else
				{
					Plugin.logger.LogDebug((object)"Didn't find a teleporter! Skipping teleporter malfunction.");
				}
			}
		}

		public static void HandleRollDistortion(bool result, int delay)
		{
			if (State.MalfunctionDistortion.Active || TimeOfDay.Instance.daysUntilDeadline < 2)
			{
				State.MalfunctionDistortion.Reset();
			}
			else if (!State.MalfunctionPower.Active && TimeOfDay.Instance.daysUntilDeadline >= 2 && result)
			{
				State.MalfunctionDistortion.Delay = delay;
				State.MalfunctionDistortion.Active = true;
				Plugin.logger.LogDebug((object)$"Distortion malfunction will trigger at {7 + State.MalfunctionDistortion.Delay}:00");
			}
		}

		public static void HandleRollDoor(bool result, int delay)
		{
			if (State.MalfunctionDoor.Active || TimeOfDay.Instance.daysUntilDeadline < 2)
			{
				State.MalfunctionDoor.Reset();
			}
			else if (!State.MalfunctionPower.Active && TimeOfDay.Instance.daysUntilDeadline >= 2 && result)
			{
				State.MalfunctionDoor.Delay = delay;
				State.MalfunctionDoor.Active = true;
				Plugin.logger.LogDebug((object)$"Door malfunction will trigger at {7 + State.MalfunctionDoor.Delay}:00");
			}
		}

		public static void HandleRollLever(bool result, int delay)
		{
			if (State.MalfunctionLever.Active || TimeOfDay.Instance.daysUntilDeadline < 2)
			{
				State.MalfunctionLever.Reset();
			}
			else if (!State.MalfunctionPower.Active && !State.MalfunctionDoor.Active && TimeOfDay.Instance.daysUntilDeadline >= 2 && result)
			{
				State.MalfunctionLever.Delay = delay;
				State.MalfunctionLever.Active = true;
				Plugin.logger.LogDebug((object)$"Lever malfunction will trigger at {12 + State.MalfunctionLever.Delay}:00");
			}
		}

		public static void HandleRollPower(bool result, bool blockLever)
		{
			if (((Object)RoundManager.Instance.currentLevel).name == "CompanyBuildingLevel")
			{
				State.MalfunctionPower.Reset();
			}
			else if (State.MalfunctionPower.Active || TimeOfDay.Instance.daysUntilDeadline < 2)
			{
				State.MalfunctionPower.Reset();
			}
			else if (TimeOfDay.Instance.daysUntilDeadline >= 2 && result)
			{
				State.MalfunctionPower.Active = true;
				if (Config.MalfunctionPowerBlockLever.Value && blockLever)
				{
					State.MalfunctionPower.Delay = 1;
				}
				else
				{
					State.MalfunctionPower.Delay = 0;
				}
			}
		}

		[HarmonyPostfix]
		[HarmonyPatch("SetMapScreenInfoToCurrentLevel")]
		[HarmonyAfter(new string[] { "jamil.corporate_restructure", "WeatherTweaks" })]
		private static void OverwriteMapScreenInfo(StartOfRound __instance)
		{
			//IL_0121: Unknown result type (might be due to invalid IL or missing references)
			StartMatchLever val = Object.FindObjectOfType<StartMatchLever>();
			if ((Object)(object)val == (Object)null)
			{
				Plugin.logger.LogError((object)"Failed to find lever device object.");
			}
			else
			{
				originalLeverDisableTooltip = val.triggerScript.disabledHoverTip;
			}
			if (((Object)__instance.currentLevel).name != "CompanyBuildingLevel" && State.MalfunctionNavigation.Active && !State.MalfunctionNavigation.Notified)
			{
				((TMP_Text)HUDManager.Instance.globalNotificationText).text = "SHIP NAVIGATION MALFUNCTION:\nROUTE TABLE OFFSET ERROR";
				HUDManager.Instance.globalNotificationAnimator.SetTrigger("TriggerNotif");
				HUDManager.Instance.UIAudio.PlayOneShot(HUDManager.Instance.radiationWarningAudio, 1f);
				State.MalfunctionNavigation.Notified = true;
				((TMP_Text)__instance.screenLevelDescription).text = "SHIP NAVIGATION MALFUNCTION\nOrbiting: Unknown\nWeather: Unknown\nBE ADVISED THE ROUTE TABLE WILL RECALIBRATE AFTER LANDING";
				((Behaviour)__instance.screenLevelVideoReel).enabled = false;
				__instance.screenLevelVideoReel.clip = null;
				((Component)__instance.screenLevelVideoReel).gameObject.SetActive(false);
				__instance.screenLevelVideoReel.Stop();
				if ((Object)(object)val == (Object)null)
				{
					Plugin.logger.LogError((object)"Failed to find lever device object.");
					return;
				}
				GameObject val2 = Assets.SpawnPrefab("sparks", ((Component)val).transform.position);
				val2.transform.SetParent(((Component)val).transform, true);
				State.MalfunctionNavigation.AssignChild(val2);
			}
		}

		[HarmonyPostfix]
		[HarmonyPatch("openingDoorsSequence")]
		private static void HandleLevelStart(StartOfRound __instance)
		{
			State.PreviousMoon = __instance.currentLevel.levelID;
			if (!State.MalfunctionPower.Active || !(((Object)__instance.currentLevel).name != "CompanyBuildingLevel"))
			{
				return;
			}
			Plugin.logger.LogDebug((object)"Triggered power malfunction!");
			State.MalfunctionPower.Triggered = true;
			if (State.MalfunctionPower.Notified)
			{
				return;
			}
			if ((Object)(object)floodlight1 != (Object)null && (Object)(object)floodlight2 != (Object)null)
			{
				Light[] componentsInChildren = floodlight1.GetComponentsInChildren<Light>(true);
				Light[] componentsInChildren2 = floodlight2.GetComponentsInChildren<Light>(true);
				Light[] array = componentsInChildren;
				for (int i = 0; i < array.Length; i++)
				{
					((Behaviour)array[i]).enabled = false;
				}
				array = componentsInChildren2;
				for (int i = 0; i < array.Length; i++)
				{
					((Behaviour)array[i]).enabled = false;
				}
				MeshRenderer component = floodlight1.GetComponent<MeshRenderer>();
				MeshRenderer component2 = floodlight2.GetComponent<MeshRenderer>();
				if ((Object)(object)component != (Object)null && (Object)(object)component2 != (Object)null)
				{
					Material[] materials = ((Renderer)component).materials;
					materials[2] = ((Renderer)component).materials[0];
					((Renderer)component).materials = materials;
					((Renderer)component2).materials = materials;
				}
			}
			Terminal val = Object.FindObjectOfType<Terminal>();
			if ((Object)(object)val != (Object)null)
			{
				((Component)val).gameObject.GetComponent<InteractTrigger>().interactable = false;
			}
			GameObject obj = elevatorPanelScreen;
			if (obj != null)
			{
				obj.SetActive(false);
			}
			GameObject val2 = GameObject.Find("HangarDoorButtonPanel");
			if ((Object)(object)val2 != (Object)null)
			{
				InteractTrigger[] componentsInChildren3 = val2.GetComponentsInChildren<InteractTrigger>(true);
				for (int i = 0; i < componentsInChildren3.Length; i++)
				{
					componentsInChildren3[i].interactable = false;
				}
			}
			__instance.PowerSurgeShip();
			StormyWeather val3 = Object.FindObjectOfType<StormyWeather>(true);
			RoundManager.PlayRandomClip(val.terminalAudio, val3.distantThunderSFX, true, 1f, 0, 1000);
			((TMP_Text)HUDManager.Instance.globalNotificationText).text = "SHIP CORE SURGE:\nRUNNING ON EMERGENCY POWER";
			HUDManager.Instance.globalNotificationAnimator.SetTrigger("TriggerNotif");
			HUDManager.Instance.UIAudio.PlayOneShot(HUDManager.Instance.radiationWarningAudio, 1f);
			State.MalfunctionPower.Notified = true;
		}

		[HarmonyPostfix]
		[HarmonyPatch("OnShipLandedMiscEvents")]
		private static void CaptureGameObjects()
		{
			elevatorPanelScreen = GameObject.Find("ElevatorPanelScreen");
			if (!((Object)(object)floodlightMaterial == (Object)null))
			{
				return;
			}
			floodlight1 = GameObject.Find("Floodlight1");
			floodlight2 = GameObject.Find("Floodlight2");
			if ((Object)(object)floodlight1 == (Object)null || (Object)(object)floodlight2 == (Object)null)
			{
				Plugin.logger.LogWarning((object)"Failed to capture floodlight gameobjects!");
				return;
			}
			MeshRenderer component = floodlight1.GetComponent<MeshRenderer>();
			if ((Object)(object)component != (Object)null)
			{
				floodlightMaterial = ((Renderer)component).materials[2];
			}
		}

		[HarmonyPostfix]
		[HarmonyPatch("Start")]
		private static void Reset()
		{
			State.Reset();
		}
	}
}
namespace Malfunctions.MonoBehaviours
{
	public class Sparks : MonoBehaviour
	{
		public float delayMin = 0.5f;

		public float delayMax = 10f;

		public float maxIntensity = 40000f;

		private float currentIntensity;

		private float now;

		private float next;

		private bool sentStop;

		private AudioSource audioSource;

		private Light pointLight;

		private VisualEffect visualEffect;

		private static VFXExposedProperty beginBurstTrigger;

		private static VFXExposedProperty stopBurstTrigger;

		private void Start()
		{
			beginBurstTrigger.name = "BeginBurst";
			stopBurstTrigger.name = "StopBurst";
			audioSource = ((Component)this).GetComponent<AudioSource>();
			pointLight = ((Component)this).GetComponent<Light>();
			visualEffect = ((Component)this).GetComponent<VisualEffect>();
			GameObject val = GameObject.Find("Player");
			Camera val2 = ((!((Object)(object)val == (Object)null)) ? val.GetComponentInChildren<Camera>() : Object.FindAnyObjectByType<Camera>());
			((Component)val2).gameObject.tag = "MainCamera";
		}

		private void Update()
		{
			now = Time.time;
			if (now > next)
			{
				next = now + Random.Range(delayMin, delayMax);
				if (!Config.MalfunctionVFXDisableSparks.Value)
				{
					sentStop = false;
					((Behaviour)pointLight).enabled = true;
					currentIntensity = maxIntensity;
					if (!Config.MalfunctionVFXDisableSparksSound.Value)
					{
						audioSource.pitch = Random.Range(0.7f, 2f);
						audioSource.Play();
					}
					visualEffect.SendEvent(beginBurstTrigger.name);
				}
			}
			else if (!sentStop)
			{
				visualEffect.SendEvent(stopBurstTrigger.name);
				sentStop = true;
			}
			pointLight.intensity = currentIntensity;
			currentIntensity = Mathf.Lerp(currentIntensity, 0f, Time.deltaTime * 100f);
		}
	}
}
namespace Malfunctions.Helpers
{
	public static class RandomHelper
	{
		public static TSource NextFromCollection<TSource>(this Random rand, IList<TSource> collection)
		{
			int index = rand.Next(collection.Count());
			return collection[index];
		}
	}
}