Decompiled source of SurprisingHazards v1.0.3

SurprisingHazards.dll

Decompiled 3 months 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 BepInEx;
using BepInEx.Configuration;
using GameNetcodeStuff;
using HarmonyLib;
using LethalConfig;
using LethalConfig.ConfigItems;
using LethalConfig.ConfigItems.Options;
using LethalLib.Modules;
using Microsoft.CodeAnalysis;
using StaticNetcodeLib;
using SurprisingHazards.Patches;
using SurprisingHazards.Scripts;
using Unity.Netcode;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("SurprisingHazards")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyDescription("SurprisingHazards")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+ed913f9982fab70bb776367a00c1aebba21ecbfb")]
[assembly: AssemblyProduct("SurprisingHazards")]
[assembly: AssemblyTitle("SurprisingHazards")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableAttribute : Attribute
	{
		public readonly byte[] NullableFlags;

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

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

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

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace ColorfulJarOfPickles.Utils
{
	public class RegisterUtil
	{
		public static void RegisterEnemyWithConfig(string configMoonRarity, EnemyType enemy, TerminalNode terminalNode, TerminalKeyword terminalKeyword, float powerLevel, int spawnCount)
		{
			enemy.MaxCount = spawnCount;
			enemy.PowerLevel = powerLevel;
			var (dictionary, dictionary2) = ConfigParsing(configMoonRarity);
			Enemies.RegisterEnemy(enemy, dictionary, dictionary2, terminalNode, terminalKeyword);
		}

		public static void RegisterScrapWithConfig(string configMoonRarity, Item scrap)
		{
			var (dictionary, dictionary2) = ConfigParsing(configMoonRarity);
			Items.RegisterScrap(scrap, dictionary, dictionary2);
		}

		public static void RegisterShopItemWithConfig(bool enabledScrap, Item item, TerminalNode terminalNode, int itemCost, string configMoonRarity)
		{
			Items.RegisterShopItem(item, (TerminalNode)null, (TerminalNode)null, terminalNode, itemCost);
			if (enabledScrap)
			{
				RegisterScrapWithConfig(configMoonRarity, item);
			}
		}

		public static (Dictionary<LevelTypes, int> spawnRateByLevelType, Dictionary<string, int> spawnRateByCustomLevelType) ConfigParsing(string configMoonRarity)
		{
			//IL_0099: 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)
			Dictionary<LevelTypes, int> dictionary = new Dictionary<LevelTypes, int>();
			Dictionary<string, int> dictionary2 = new Dictionary<string, int>();
			foreach (string item in from s in configMoonRarity.Split(',')
				select s.Trim())
			{
				string[] array = item.Split(':');
				if (array.Length != 2)
				{
					continue;
				}
				string text = array[0];
				if (!int.TryParse(array[1], out var result))
				{
					continue;
				}
				if (Enum.TryParse<LevelTypes>(text, ignoreCase: true, out LevelTypes result2))
				{
					dictionary[result2] = result;
					continue;
				}
				string value = text + "Level";
				if (Enum.TryParse<LevelTypes>(value, ignoreCase: true, out result2))
				{
					dictionary[result2] = result;
				}
				else
				{
					dictionary2[text] = result;
				}
			}
			return (dictionary, dictionary2);
		}
	}
}
namespace SurprisingHazards
{
	[BepInPlugin("wexop.surprising_hazards", "SurprisingHazards", "1.0.3")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class SurprisingHazardsPlugin : BaseUnityPlugin
	{
		private const string GUID = "wexop.surprising_hazards";

		private const string NAME = "SurprisingHazards";

		private const string VERSION = "1.0.3";

		public static SurprisingHazardsPlugin instance;

		public GameObject surprisingGameObject;

		public Dictionary<ulong, RegisteredHazard> RegisteredHazards = new Dictionary<ulong, RegisteredHazard>();

		public ConfigEntry<bool> affectVanilla;

		public ConfigEntry<bool> affectModded;

		public ConfigEntry<float> visibleRange;

		public ConfigEntry<string> customVisibleRange;

		public ConfigEntry<string> disabledHazards;

		public ConfigEntry<float> audioVolume;

		public ConfigEntry<string> hazardsNameList;

		private void Awake()
		{
			instance = this;
			((BaseUnityPlugin)this).Logger.LogInfo((object)"SurprisingHazards starting....");
			string text = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "surprisinghazards");
			AssetBundle val = AssetBundle.LoadFromFile(text);
			LoadConfigs();
			surprisingGameObject = val.LoadAsset<GameObject>("Assets/LethalCompany/Mods/SurprisingHazards/SurprisingHazard.prefab");
			Debug.Log((object)((Object)surprisingGameObject).name);
			Harmony.CreateAndPatchAll(typeof(TurretPatch), (string)null);
			Harmony.CreateAndPatchAll(typeof(LandminePatch), (string)null);
			Harmony.CreateAndPatchAll(typeof(SpikeTrapPatch), (string)null);
			Harmony.CreateAndPatchAll(typeof(RoundManagerPatch), (string)null);
			MapObjects.mapObjects.ForEach(delegate(RegisteredMapObject m)
			{
				string name = ((Object)m.mapObject.prefabToSpawn).name;
				if (!name.Contains("TurretContainer") && !name.Contains("Landmine") && !name.Contains("SpikeRoofTrapHazard"))
				{
					m.mapObject.prefabToSpawn.gameObject.AddComponent<SurprisingHazardSetUp>();
				}
			});
			((BaseUnityPlugin)this).Logger.LogInfo((object)"SurprisingHazards is ready!");
		}

		public static void RegisterHazard(NetworkBehaviour networkBehaviour)
		{
			//IL_004b: 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)
			if (instance.affectVanilla.Value && !HazardDisabled(((Object)((Component)((Component)networkBehaviour).transform.parent).gameObject).name))
			{
				GameObject val = Object.Instantiate<GameObject>(instance.surprisingGameObject, Vector3.zero, Quaternion.identity, ((Component)networkBehaviour).transform.parent);
				SurprisingHazardBehavior component = val.GetComponent<SurprisingHazardBehavior>();
				component.parent = ((Component)((Component)networkBehaviour).transform.parent).gameObject;
				component.networkId = networkBehaviour.NetworkObjectId;
				component.isServer = networkBehaviour.IsServer;
				RegisteredHazard registeredHazard = new RegisteredHazard();
				registeredHazard.gameObject = ((Component)((Component)networkBehaviour).transform.parent).gameObject;
				registeredHazard.networkId = networkBehaviour.NetworkObjectId;
				registeredHazard.surprisingHazardBehavior = component;
				instance.RegisteredHazards.TryAdd(networkBehaviour.NetworkObjectId, registeredHazard);
			}
		}

		public static void RegisterModdedHazard(NetworkBehaviour networkBehaviour)
		{
			//IL_003c: 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)
			if (instance.affectModded.Value && !HazardDisabled(((Object)networkBehaviour).name))
			{
				GameObject val = Object.Instantiate<GameObject>(instance.surprisingGameObject, Vector3.zero, Quaternion.identity, ((Component)networkBehaviour).transform);
				SurprisingHazardBehavior component = val.GetComponent<SurprisingHazardBehavior>();
				component.parent = ((Component)networkBehaviour).gameObject;
				component.networkId = networkBehaviour.NetworkObjectId;
				component.isServer = networkBehaviour.IsServer;
				RegisteredHazard registeredHazard = new RegisteredHazard();
				registeredHazard.gameObject = ((Component)networkBehaviour).gameObject;
				registeredHazard.networkId = networkBehaviour.NetworkObjectId;
				registeredHazard.surprisingHazardBehavior = component;
				instance.RegisteredHazards.TryAdd(networkBehaviour.NetworkObjectId, registeredHazard);
			}
		}

		public float GetCustomDistance(string name)
		{
			float distance = visibleRange.Value;
			string nameSearch = name.ToLower();
			while (nameSearch.Contains(" "))
			{
				nameSearch = nameSearch.Replace(" ", "");
			}
			customVisibleRange.Value.Split(",").ToList().ForEach(delegate(string hazard)
			{
				string[] array = hazard.Split(":");
				string text = array[0].ToLower();
				while (text.Contains(" "))
				{
					text = text.Replace(" ", "");
				}
				if (nameSearch.Contains(text))
				{
					distance = float.Parse(array[1]);
				}
			});
			return distance;
		}

		public static string ConditionalString(string value)
		{
			string text = value.ToLower();
			while (text.Contains(" "))
			{
				text = text.Replace(" ", "");
			}
			return text;
		}

		public static bool HazardDisabled(string name)
		{
			if (instance.disabledHazards.Value == "")
			{
				return false;
			}
			string nameSearch = ConditionalString(name);
			bool disable = false;
			instance.disabledHazards.Value.Split(",").ToList().ForEach(delegate(string hazard)
			{
				string value = ConditionalString(hazard);
				if (nameSearch.Contains(value))
				{
					disable = true;
				}
			});
			return disable;
		}

		public string GetHazardsNames()
		{
			string hazardsNames = "TurretContainer,Landmine,SpikeRoofTrapHazard,";
			MapObjects.mapObjects.ForEach(delegate(RegisteredMapObject mapObject)
			{
				hazardsNames = hazardsNames + ((Object)mapObject.mapObject.prefabToSpawn).name + ",";
			});
			return hazardsNames;
		}

		private void LoadConfigs()
		{
			affectVanilla = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "affectVanilla", true, "Enable to affect vanilla map hazards. Every player must have the same value. No need to restart the game :)");
			CreateBoolConfig(affectVanilla);
			affectModded = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "affectModded", true, "Enable to affect modded map hazards. Every player must have the same value. No need to restart the game :)");
			CreateBoolConfig(affectModded);
			visibleRange = ((BaseUnityPlugin)this).Config.Bind<float>("General", "visibleRange", 8f, "Hazards visible range. Every player must have the same value, or this will cause desync. No need to restart the game :)");
			CreateFloatConfig(visibleRange);
			customVisibleRange = ((BaseUnityPlugin)this).Config.Bind<string>("General", "customVisibleRange", "turret:15,spikeRoofTrap:8,landmine:8", "Hazards visible range. The name must be the hazard prefab name, it works with modded hazards too. Every player must have the same value, or this will cause desync. No need to restart the game :)");
			CreateStringConfig(customVisibleRange);
			disabledHazards = ((BaseUnityPlugin)this).Config.Bind<string>("General", "disabledHazards", "", "Disabled hazards, write a list like : turret,landmine. The name must be the hazard prefab name, it works with modded hazards too. Every player must have the same value, or this will cause desync. No need to restart the game :)");
			CreateStringConfig(disabledHazards);
			audioVolume = ((BaseUnityPlugin)this).Config.Bind<float>("General", "audioVolume", 1f, "Hazards voice lines volume. No need to restart the game :)");
			CreateFloatConfig(audioVolume, 0f, 2f);
			hazardsNameList = ((BaseUnityPlugin)this).Config.Bind<string>("NotConfig", "hazardsNameList", GetHazardsNames(), "Current map hazards detected in your mod pack, this is not a config. You can refresh to default to refresh list data.");
			CreateStringConfig(hazardsNameList);
		}

		private void CreateFloatConfig(ConfigEntry<float> configEntry, float min = 0f, float max = 100f)
		{
			//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_000e: Expected O, but got Unknown
			//IL_000f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0016: Expected O, but got Unknown
			//IL_0017: Unknown result type (might be due to invalid IL or missing references)
			//IL_0024: Expected O, but got Unknown
			//IL_001f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0025: Expected O, but got Unknown
			FloatSliderOptions val = new FloatSliderOptions();
			((BaseRangeOptions<float>)val).Min = min;
			((BaseRangeOptions<float>)val).Max = max;
			((BaseOptions)val).RequiresRestart = false;
			FloatSliderConfigItem val2 = new FloatSliderConfigItem(configEntry, val);
			LethalConfigManager.AddConfigItem((BaseConfigItem)(object)val2);
		}

		private void CreateIntConfig(ConfigEntry<int> configEntry, int min = 0, int max = 100)
		{
			//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_000e: Expected O, but got Unknown
			//IL_000f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0016: Expected O, but got Unknown
			//IL_0017: Unknown result type (might be due to invalid IL or missing references)
			//IL_0024: Expected O, but got Unknown
			//IL_001f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0025: Expected O, but got Unknown
			IntSliderOptions val = new IntSliderOptions();
			((BaseRangeOptions<int>)val).Min = min;
			((BaseRangeOptions<int>)val).Max = max;
			((BaseOptions)val).RequiresRestart = false;
			IntSliderConfigItem val2 = new IntSliderConfigItem(configEntry, val);
			LethalConfigManager.AddConfigItem((BaseConfigItem)(object)val2);
		}

		private void CreateStringConfig(ConfigEntry<string> configEntry, bool requireRestart = false)
		{
			//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_0014: Expected O, but got Unknown
			//IL_000f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0015: Expected O, but got Unknown
			TextInputFieldConfigItem val = new TextInputFieldConfigItem(configEntry, new TextInputFieldOptions
			{
				RequiresRestart = requireRestart
			});
			LethalConfigManager.AddConfigItem((BaseConfigItem)(object)val);
		}

		private void CreateBoolConfig(ConfigEntry<bool> configEntry, bool requireRestart = false)
		{
			//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_0014: Expected O, but got Unknown
			//IL_000f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0015: Expected O, but got Unknown
			BoolCheckBoxConfigItem val = new BoolCheckBoxConfigItem(configEntry, new BoolCheckBoxOptions
			{
				RequiresRestart = requireRestart
			});
			LethalConfigManager.AddConfigItem((BaseConfigItem)(object)val);
		}
	}
	public static class PluginInfo
	{
		public const string PLUGIN_GUID = "SurprisingHazards";

		public const string PLUGIN_NAME = "SurprisingHazards";

		public const string PLUGIN_VERSION = "1.0.0";
	}
}
namespace SurprisingHazards.Scripts
{
	[StaticNetcode]
	public class NetworkSurprisingHazard
	{
		[ClientRpc]
		public static void ChangeClipClientRpc(ulong networkId, int audioClip)
		{
			if (SurprisingHazardsPlugin.instance.RegisteredHazards.ContainsKey(networkId))
			{
				RegisteredHazard registeredHazard = SurprisingHazardsPlugin.instance.RegisteredHazards?[networkId];
				if (registeredHazard == null)
				{
					Debug.Log((object)$"OBJECT NOT FOUND {networkId}");
				}
				else
				{
					registeredHazard.surprisingHazardBehavior.SetClip(audioClip);
				}
			}
		}
	}
	public class RegisteredHazard
	{
		public GameObject gameObject;

		public ulong networkId;

		public SurprisingHazardBehavior surprisingHazardBehavior;
	}
	public class SurprisingHazardBehavior : NetworkBehaviour
	{
		public List<AudioClip> audioClips;

		public AudioSource audioSource;

		public GameObject parent;

		private Vector3 baseScale;

		private float animationTimer;

		private float animationDuration = 0.5f;

		public ulong networkId;

		public bool isServer;

		private bool animationGrow;

		private int state;

		private int lastState;

		private float visibleRange;

		public void Start()
		{
			//IL_0028: Unknown result type (might be due to invalid IL or missing references)
			//IL_002d: Unknown result type (might be due to invalid IL or missing references)
			visibleRange = SurprisingHazardsPlugin.instance.GetCustomDistance(((Object)parent).name);
			baseScale = parent.transform.localScale;
			audioSource.maxDistance = visibleRange + 5f;
			((Component)this).transform.parent = null;
			state = 0;
		}

		private void Update()
		{
			//IL_0090: Unknown result type (might be due to invalid IL or missing references)
			//IL_0095: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c5: Unknown result type (might be due to invalid IL or missing references)
			//IL_0134: Unknown result type (might be due to invalid IL or missing references)
			//IL_0155: Unknown result type (might be due to invalid IL or missing references)
			//IL_010b: Unknown result type (might be due to invalid IL or missing references)
			//IL_012c: Unknown result type (might be due to invalid IL or missing references)
			state = 0;
			StartOfRound.Instance?.allPlayerScripts?.ToList().ForEach(delegate(PlayerControllerB player)
			{
				//IL_0007: Unknown result type (might be due to invalid IL or missing references)
				//IL_0012: Unknown result type (might be due to invalid IL or missing references)
				if (Vector3.Distance(((Component)player).transform.position, ((Component)this).transform.position) <= visibleRange)
				{
					state = 1;
				}
			});
			if (state != lastState)
			{
				if (state == 1)
				{
					GetRandomClip();
					animationTimer = 0f;
					animationGrow = true;
				}
				else
				{
					animationTimer = 0f;
					animationGrow = false;
				}
			}
			Vector3 position = ((Component)this).transform.position;
			if (!((Vector3)(ref position)).Equals(parent.transform.position))
			{
				((Component)this).transform.position = parent.transform.position;
			}
			if (animationTimer < animationDuration)
			{
				animationTimer += Time.deltaTime;
				parent.transform.localScale = (animationGrow ? (baseScale * Mathf.Clamp(animationTimer / animationDuration, 0f, 1f)) : (baseScale * Mathf.Clamp(animationDuration - animationTimer, 0f, 1f)));
			}
			lastState = state;
		}

		public void SetClip(int audioClip)
		{
			audioSource.clip = audioClips[audioClip];
			audioSource.volume = SurprisingHazardsPlugin.instance.audioVolume.Value;
			audioSource.Play();
		}

		public void GetRandomClip()
		{
			if (isServer)
			{
				NetworkSurprisingHazard.ChangeClipClientRpc(networkId, Random.Range(0, audioClips.Count));
			}
		}
	}
	public class SurprisingHazardSetUp : NetworkBehaviour
	{
		public void Start()
		{
			SurprisingHazardsPlugin.RegisterModdedHazard((NetworkBehaviour)(object)this);
		}
	}
}
namespace SurprisingHazards.Patches
{
	[HarmonyPatch(typeof(Landmine))]
	public class LandminePatch
	{
		[HarmonyPatch("Start")]
		[HarmonyPostfix]
		public static void PatchStart(Landmine __instance)
		{
			SurprisingHazardsPlugin.RegisterHazard((NetworkBehaviour)(object)__instance);
		}
	}
	[HarmonyPatch(typeof(RoundManager))]
	public class RoundManagerPatch
	{
		[HarmonyPatch("GenerateNewLevelClientRpc")]
		[HarmonyPrefix]
		public static void PatchLoadLevel(RoundManager __instance)
		{
			SurprisingHazardsPlugin.instance.RegisteredHazards.Clear();
		}
	}
	[HarmonyPatch(typeof(SpikeRoofTrap))]
	public class SpikeTrapPatch
	{
		[HarmonyPatch("Start")]
		[HarmonyPostfix]
		public static void PatchStart(SpikeRoofTrap __instance)
		{
			SurprisingHazardsPlugin.RegisterHazard((NetworkBehaviour)(object)__instance);
		}
	}
	[HarmonyPatch(typeof(Turret))]
	public class TurretPatch
	{
		[HarmonyPatch("Start")]
		[HarmonyPostfix]
		public static void PatchStart(Turret __instance)
		{
			SurprisingHazardsPlugin.RegisterHazard((NetworkBehaviour)(object)__instance);
		}
	}
}