Decompiled source of Meltdown Chance v2.6.0

den.meltdownchance.dll

Decompiled 2 months ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using FacilityMeltdown.MeltdownSequence.Behaviours;
using HarmonyLib;
using LethalLib.Modules;
using MeltdownChance.Configs;
using MeltdownChance.Patches;
using Microsoft.CodeAnalysis;
using Unity.Netcode;
using UnityEngine;
using den.meltdownchance.NetcodePatcher;

[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("den")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("2.6.0.0")]
[assembly: AssemblyInformationalVersion("2.6.0+4049d06c0dda792d0ebbf08b3b16786cb7b4789a")]
[assembly: AssemblyProduct("MeltdownChance")]
[assembly: AssemblyTitle("den.meltdownchance")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("2.6.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
[module: NetcodePatchedAssembly]
internal class <Module>
{
	static <Module>()
	{
		NetworkVariableSerializationTypes.InitializeSerializer_UnmanagedByMemcpy<bool>();
		NetworkVariableSerializationTypes.InitializeEqualityChecker_UnmanagedIEquatable<bool>();
	}
}
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableAttribute : Attribute
	{
		public readonly byte[] NullableFlags;

		public NullableAttribute(byte P_0)
		{
			NullableFlags = new byte[1] { P_0 };
		}

		public NullableAttribute(byte[] P_0)
		{
			NullableFlags = P_0;
		}
	}
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableContextAttribute : Attribute
	{
		public readonly byte Flag;

		public NullableContextAttribute(byte P_0)
		{
			Flag = P_0;
		}
	}
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
	[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
	internal sealed class IgnoresAccessChecksToAttribute : Attribute
	{
		public IgnoresAccessChecksToAttribute(string assemblyName)
		{
		}
	}
}
namespace MeltdownChance
{
	internal class MeltdownChanceBehaviour : NetworkBehaviour
	{
		private readonly NetworkVariable<bool> _isMeltdown = new NetworkVariable<bool>(false, (NetworkVariableReadPermission)0, (NetworkVariableWritePermission)0)
		{
			Value = false
		};

		public static MeltdownChanceBehaviour? Instance { get; private set; }

		public bool IsMeltdown
		{
			get
			{
				return _isMeltdown.Value;
			}
			internal set
			{
				_isMeltdown.Value = value;
			}
		}

		public override void OnNetworkSpawn()
		{
			if ((Object)(object)Instance == (Object)null)
			{
				Instance = this;
			}
			MeltdownChanceBase.logger.LogInfo((object)"Spawned MeltdownChanceBehaviour");
			((NetworkBehaviour)this).OnNetworkSpawn();
		}

		public override void OnDestroy()
		{
			MeltdownChanceBase.logger.LogInfo((object)"Destroyed MeltdownChanceBehaviour");
			if (Instance == this)
			{
				Instance = null;
			}
			((NetworkBehaviour)this).OnDestroy();
		}

		protected override void __initializeVariables()
		{
			if (_isMeltdown == null)
			{
				throw new Exception("MeltdownChanceBehaviour._isMeltdown cannot be null. All NetworkVariableBase instances must be initialized.");
			}
			((NetworkVariableBase)_isMeltdown).Initialize((NetworkBehaviour)(object)this);
			((NetworkBehaviour)this).__nameNetworkVariable((NetworkVariableBase)(object)_isMeltdown, "_isMeltdown");
			base.NetworkVariableFields.Add((NetworkVariableBase)(object)_isMeltdown);
			((NetworkBehaviour)this).__initializeVariables();
		}

		[MethodImpl(MethodImplOptions.NoInlining)]
		protected internal override string? __getTypeName()
		{
			return "MeltdownChanceBehaviour";
		}
	}
	[BepInPlugin("den.meltdownchance", "MeltdownChance", "2.6.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[BepInDependency("evaisa.lethallib", "0.15.1")]
	public class MeltdownChanceBase : BaseUnityPlugin
	{
		public readonly Harmony harmony = new Harmony("den.meltdownchance");

		internal static ManualLogSource logger = Logger.CreateLogSource("den.meltdownchance");

		public static bool EnableMeltdown;

		public static bool FirstPickUp;

		public static bool isCompany;

		public static int configChanceValue;

		public static bool configMessageValue;

		public static bool isHost;

		internal static MeltdownChanceBase? instance;

		public static GameObject MeltdownChanceManagerPrefab = null;

		public static MeltdownChanceConfig MyConfig { get; internal set; }

		private void Awake()
		{
			if ((Object)(object)instance == (Object)null)
			{
				instance = this;
				MyConfig = new MeltdownChanceConfig(((BaseUnityPlugin)this).Config);
				NetcodePatcher();
				InitializePrefabs();
				configChanceValue = Math.Max(0, Math.Min(MeltdownChanceConfig.configChance.Value, 100));
				configMessageValue = MeltdownChanceConfig.configMessage.Value;
				ResetMeltdownChance();
				ApplyPatches();
			}
		}

		private void InitializePrefabs()
		{
			MeltdownChanceManagerPrefab = NetworkPrefabs.CreateNetworkPrefab("MeltdownChance Manager");
			MeltdownChanceManagerPrefab.AddComponent<MeltdownChanceBehaviour>();
		}

		private static void NetcodePatcher()
		{
			Type[] types = Assembly.GetExecutingAssembly().GetTypes();
			Type[] array = types;
			foreach (Type type in array)
			{
				MethodInfo[] methods = type.GetMethods(BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic);
				MethodInfo[] array2 = methods;
				foreach (MethodInfo methodInfo in array2)
				{
					object[] customAttributes = methodInfo.GetCustomAttributes(typeof(RuntimeInitializeOnLoadMethodAttribute), inherit: false);
					if (customAttributes.Length != 0)
					{
						methodInfo.Invoke(null, null);
					}
				}
			}
		}

		internal static void ResetMeltdownChance()
		{
			EnableMeltdown = true;
			FirstPickUp = false;
			isCompany = false;
		}

		internal void ApplyPatches()
		{
			TryPatches(typeof(StartOfRoundPatch), "StartOfRound");
			TryPatches(typeof(MeltdownHandlerPatch), "FacilityMeltdown");
			TryPatches(typeof(EquipApparaticePatch), "EquipApparatice");
		}

		internal void TryPatches(Type patchType, string name)
		{
			try
			{
				harmony.PatchAll(patchType);
				logger.LogInfo((object)(name + " successfully patched!"));
			}
			catch (Exception arg)
			{
				logger.LogError((object)$"Couldn't patch {name}!!!:\n{arg}");
			}
		}
	}
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "den.meltdownchance";

		public const string PLUGIN_NAME = "MeltdownChance";

		public const string PLUGIN_VERSION = "2.6.0";
	}
}
namespace MeltdownChance.Patches
{
	[HarmonyPatch(typeof(LungProp))]
	internal class EquipApparaticePatch
	{
		[HarmonyPatch("EquipItem")]
		[HarmonyPostfix]
		internal static void DisplayMessage(LungProp __instance)
		{
			bool isInFactory = ((GrabbableObject)__instance).isInFactory;
			bool flag = false;
			if (MeltdownChanceBase.isHost)
			{
				flag = MeltdownChanceBase.EnableMeltdown;
			}
			else
			{
				MeltdownChanceBehaviour instance = MeltdownChanceBehaviour.Instance;
				if (instance != null)
				{
					flag = instance.IsMeltdown;
				}
				else
				{
					MeltdownChanceBase.logger.LogWarning((object)"MeltdownChanceBehaviour instance is null, it might not have been instantiated or is otherwise unavailable. IsMeltdown flag cannot be read. Client players won't see the correct message when the apparatus is pulled.");
				}
			}
			MeltdownChanceBase.logger.LogDebug((object)$"Is player host: {MeltdownChanceBase.isHost} | Is apparatus inside Facility?: {isInFactory} | Has meltdown started?: {flag} | Is level CompanyBuilding? {MeltdownChanceBase.isCompany}");
			if (MeltdownChanceBase.FirstPickUp && isInFactory && !MeltdownChanceBase.isCompany)
			{
				MeltdownChanceBase.FirstPickUp = false;
				string text = (flag ? "<color=red>Reactor unstable!</color>" : "<color=green>Reactor stable!</color>");
				string text2 = (flag ? "Meltdown imminent! Evacuate facility immediately!" : "Leaks detected. Radiation levels rising!");
				if (MeltdownChanceBase.configMessageValue)
				{
					HUDManager.Instance.DisplayTip(text, text2, flag, false, "LC_Tip1");
				}
			}
		}
	}
	[HarmonyPatch(typeof(MeltdownHandler))]
	internal class MeltdownHandlerPatch
	{
		[HarmonyPatch("MeltdownReadyServerRpc")]
		[HarmonyPrefix]
		private static bool MeltdownReadyServerRpcPatch()
		{
			if (MeltdownChanceBase.isHost)
			{
				return MeltdownChanceBase.EnableMeltdown;
			}
			return true;
		}

		[HarmonyPatch("StartMeltdownClientRpc")]
		[HarmonyPrefix]
		private static bool StartMeltdownClientRpcPrePatch()
		{
			if (MeltdownChanceBase.isHost)
			{
				return MeltdownChanceBase.EnableMeltdown;
			}
			return true;
		}
	}
	[HarmonyPatch(typeof(StartOfRound))]
	internal class StartOfRoundPatch
	{
		private static readonly Random random = new Random();

		private static int rand;

		[HarmonyPatch("Start")]
		[HarmonyPostfix]
		public static void StartPatch(StartOfRound __instance)
		{
			if (!((NetworkBehaviour)__instance).IsOwner)
			{
				return;
			}
			try
			{
				GameObject val = Object.Instantiate<GameObject>(MeltdownChanceBase.MeltdownChanceManagerPrefab, ((Component)__instance).transform);
				((Object)val).hideFlags = (HideFlags)0;
				val.GetComponent<NetworkObject>().Spawn(false);
			}
			catch (Exception arg)
			{
				MeltdownChanceBase.logger.LogError((object)$"Failed to spawn MeltdownChanceBehaviour:\n{arg}");
			}
		}

		[HarmonyPatch("OnShipLandedMiscEvents")]
		[HarmonyPrefix]
		private static void OnShipLandedMiscEventsPrePatch(StartOfRound __instance)
		{
			MeltdownChanceBase.ResetMeltdownChance();
			MeltdownChanceBase.isCompany = __instance.currentLevel.levelID == 3;
			MeltdownChanceBase.FirstPickUp = true;
			MeltdownChanceBase.isHost = GameNetworkManager.Instance.isHostingGame;
			if (MeltdownChanceBase.isHost && !MeltdownChanceBase.isCompany)
			{
				rand = random.Next(0, 100);
				bool flag = (MeltdownChanceBase.EnableMeltdown = rand <= MeltdownChanceBase.configChanceValue);
				MeltdownChanceBase.logger.LogDebug((object)string.Format("Expect {0}meltdown this round! Meltdown Threshold: {1}, Random Roll: {2}", flag ? "a " : "no ", MeltdownChanceBase.configChanceValue, rand));
				MeltdownChanceBehaviour instance = MeltdownChanceBehaviour.Instance;
				if (instance == null)
				{
					MeltdownChanceBase.logger.LogWarning((object)"MeltdownChanceBehaviour instance is null, it might not have been instantiated or is otherwise unavailable. IsMeltdown flag cannot be set. Client players won't see the correct message when the apparatus is pulled.");
				}
				else
				{
					instance.IsMeltdown = flag;
				}
			}
		}

		[HarmonyPatch("ShipHasLeft")]
		[HarmonyPrefix]
		private static void ShipHasLeftPatch()
		{
			MeltdownChanceBase.ResetMeltdownChance();
			MeltdownChanceBase.logger.LogInfo((object)"Ship has left, resetting flags.");
		}
	}
}
namespace MeltdownChance.Configs
{
	public class MeltdownChanceConfig
	{
		public static ConfigEntry<int> configChance;

		public static ConfigEntry<bool> configMessage;

		public MeltdownChanceConfig(ConfigFile cfg)
		{
			configChance = cfg.Bind<int>("General", "MeltdownChance", 100, "Chance in percent (0 - 100) at which Meltdowns should occur");
			configMessage = cfg.Bind<bool>("General", "DisplayPopup", true, "Display Meltdown Chance popup when picking up the Apparatus");
		}
	}
}
namespace den.meltdownchance.NetcodePatcher
{
	[AttributeUsage(AttributeTargets.Module)]
	internal class NetcodePatchedAssemblyAttribute : Attribute
	{
	}
}