Decompiled source of LethalPresents v1.0.7

LethalPresents.dll

Decompiled a month ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
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.Logging;
using Microsoft.CodeAnalysis;
using On;
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("LethalPresents")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyDescription("LethalPresents mod")]
[assembly: AssemblyFileVersion("1.0.7.0")]
[assembly: AssemblyInformationalVersion("1.0.7")]
[assembly: AssemblyProduct("LethalPresents")]
[assembly: AssemblyTitle("LethalPresents")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.7.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.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace LethalPresents
{
	[BepInPlugin("LethalPresents", "LethalPresents", "1.0.7")]
	public class LethalPresentsPlugin : BaseUnityPlugin
	{
		public static ManualLogSource mls;

		private static int spawnChance = 5;

		private static string[] disabledEnemies = new string[0];

		private static bool IsAllowlist = false;

		private static bool ShouldSpawnMines = false;

		private static bool ShouldSpawnTurrets = false;

		private static bool AllowInsideSpawnOutside = false;

		private static bool AllowOutsideSpawnInside = false;

		private static bool isHost => ((NetworkBehaviour)RoundManager.Instance).NetworkManager.IsHost;

		private static SelectableLevel currentLevel => RoundManager.Instance.currentLevel;

		internal static T GetPrivateField<T>(object instance, string fieldName)
		{
			FieldInfo field = instance.GetType().GetField(fieldName, BindingFlags.Instance | BindingFlags.NonPublic);
			return (T)field.GetValue(instance);
		}

		private void Awake()
		{
			//IL_002a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0034: Expected O, but got Unknown
			//IL_003c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0046: Expected O, but got Unknown
			mls = ((BaseUnityPlugin)this).Logger;
			mls.LogInfo((object)"Plugin LethalPresents is loaded!");
			loadConfig();
			RoundManager.AdvanceHourAndSpawnNewBatchOfEnemies += new hook_AdvanceHourAndSpawnNewBatchOfEnemies(updateCurrentLevelInfo);
			GiftBoxItem.OpenGiftBoxServerRpc += new hook_OpenGiftBoxServerRpc(spawnRandomEntity);
		}

		private void TryLog(object obj)
		{
			try
			{
				mls.LogInfo(obj);
			}
			catch (Exception)
			{
			}
		}

		private void updateCurrentLevelInfo(orig_AdvanceHourAndSpawnNewBatchOfEnemies orig, RoundManager self)
		{
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			//IL_001a: Expected O, but got Unknown
			orig.Invoke(self);
			RoundManager.AdvanceHourAndSpawnNewBatchOfEnemies -= new hook_AdvanceHourAndSpawnNewBatchOfEnemies(updateCurrentLevelInfo);
			SelectableLevel[] levels = StartOfRound.Instance.levels;
			foreach (SelectableLevel val in levels)
			{
				try
				{
					mls.LogInfo((object)("Moon: " + val.PlanetName + " (" + ((Object)val).name + ")"));
					mls.LogInfo((object)"List of spawnable enemies (inside):");
					val.Enemies.ForEach(delegate(SpawnableEnemyWithRarity e)
					{
						TryLog(((Object)e.enemyType).name);
					});
					mls.LogInfo((object)"List of spawnable enemies (outside):");
					val.OutsideEnemies.ForEach(delegate(SpawnableEnemyWithRarity e)
					{
						TryLog(((Object)e.enemyType).name);
					});
					val.DaytimeEnemies.ForEach(delegate(SpawnableEnemyWithRarity e)
					{
						TryLog(((Object)e.enemyType).name);
					});
				}
				catch (Exception)
				{
				}
			}
		}

		private void loadConfig()
		{
			spawnChance = ((BaseUnityPlugin)this).Config.Bind<int>("General", "SpawnChance", 5, "Chance of spawning an enemy when opening a present [0-100]").Value;
			disabledEnemies = ((BaseUnityPlugin)this).Config.Bind<string>("General", "EnemyBlocklist", "", "Enemy blocklist separated by , and without whitespaces").Value.Split(",");
			IsAllowlist = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "IsAllowlist", false, "Turns blocklist into allowlist, blocklist must contain at least one inside and one outside enemy, use at your own risk").Value;
			ShouldSpawnMines = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "ShouldSpawnMines", true, "Add mines to the spawn pool").Value;
			ShouldSpawnTurrets = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "ShouldSpawnTurrets", true, "Add turrets to the spawn pool").Value;
			AllowInsideSpawnOutside = ((BaseUnityPlugin)this).Config.Bind<bool>("Extra", "AllowInsideSpawnOutside", true, "Allow spawning inside enemies when outside the building. CAN CAUSE LAG WITHOUT PROPER AI MOD").Value;
			AllowOutsideSpawnInside = ((BaseUnityPlugin)this).Config.Bind<bool>("Extra", "AllowOutsideSpawnInside", true, "Allow spawning outside enemies when inside the building. CAN CAUSE LAG WITHOUT PROPER AI MOD").Value;
			if (IsAllowlist)
			{
				mls.LogInfo((object)"Only following enemies can spawn from the gift:");
			}
			else
			{
				mls.LogInfo((object)"Following enemies wont be spawned from the gift:");
			}
			string[] array = disabledEnemies;
			foreach (string text in array)
			{
				mls.LogInfo((object)text);
			}
		}

		private void spawnRandomEntity(orig_OpenGiftBoxServerRpc orig, GiftBoxItem self)
		{
			//IL_00fe: Unknown result type (might be due to invalid IL or missing references)
			//IL_010e: Unknown result type (might be due to invalid IL or missing references)
			NetworkManager networkManager = ((NetworkBehaviour)self).NetworkManager;
			if (networkManager == null || !networkManager.IsListening)
			{
				orig.Invoke(self);
				return;
			}
			int privateField = GetPrivateField<int>(self, "__rpc_exec_stage");
			mls.LogInfo((object)("IsServer:" + networkManager.IsServer + " IsHost:" + networkManager.IsHost + " __rpc_exec_stage:" + privateField));
			if (privateField != 1 || !isHost)
			{
				orig.Invoke(self);
				return;
			}
			int num = Random.Range(1, 100);
			mls.LogInfo((object)("Player's fortune:" + num));
			if (num >= spawnChance)
			{
				orig.Invoke(self);
				return;
			}
			chooseAndSpawnEnemy(((GrabbableObject)self).isInFactory, ((Component)self).transform.position, ((Component)self.previousPlayerHeldBy).transform.position);
			orig.Invoke(self);
		}

		private static void chooseAndSpawnEnemy(bool inside, Vector3 pos, Vector3 player_pos)
		{
			//IL_000b: Unknown result type (might be due to invalid IL or missing references)
			//IL_000c: Unknown result type (might be due to invalid IL or missing references)
			//IL_013b: Unknown result type (might be due to invalid IL or missing references)
			//IL_013c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0146: Unknown result type (might be due to invalid IL or missing references)
			//IL_014b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0150: Unknown result type (might be due to invalid IL or missing references)
			//IL_0159: Unknown result type (might be due to invalid IL or missing references)
			//IL_015a: Unknown result type (might be due to invalid IL or missing references)
			//IL_016d: Unknown result type (might be due to invalid IL or missing references)
			//IL_017b: Unknown result type (might be due to invalid IL or missing references)
			//IL_017c: Unknown result type (might be due to invalid IL or missing references)
			//IL_017d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0182: Unknown result type (might be due to invalid IL or missing references)
			//IL_0185: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a8: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a9: Unknown result type (might be due to invalid IL or missing references)
			//IL_0214: Unknown result type (might be due to invalid IL or missing references)
			//IL_0215: Unknown result type (might be due to invalid IL or missing references)
			//IL_021f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0224: Unknown result type (might be due to invalid IL or missing references)
			//IL_0229: Unknown result type (might be due to invalid IL or missing references)
			//IL_0232: Unknown result type (might be due to invalid IL or missing references)
			//IL_0233: Unknown result type (might be due to invalid IL or missing references)
			//IL_0246: Unknown result type (might be due to invalid IL or missing references)
			//IL_0263: Unknown result type (might be due to invalid IL or missing references)
			//IL_0286: Unknown result type (might be due to invalid IL or missing references)
			//IL_0287: Unknown result type (might be due to invalid IL or missing references)
			//IL_0362: Unknown result type (might be due to invalid IL or missing references)
			//IL_0363: Unknown result type (might be due to invalid IL or missing references)
			//IL_036d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0372: Unknown result type (might be due to invalid IL or missing references)
			//IL_0377: Unknown result type (might be due to invalid IL or missing references)
			//IL_0394: Unknown result type (might be due to invalid IL or missing references)
			//IL_0395: Unknown result type (might be due to invalid IL or missing references)
			//IL_03b0: Unknown result type (might be due to invalid IL or missing references)
			ManualLogSource obj = mls;
			Vector3 val = player_pos;
			obj.LogInfo((object)("Player pos " + ((object)(Vector3)(ref val)).ToString()));
			List<SpawnableEnemyWithRarity> list = currentLevel.Enemies.Where((SpawnableEnemyWithRarity e) => disabledEnemies.Contains(((Object)e.enemyType).name) ? IsAllowlist : (!IsAllowlist)).ToList();
			List<SpawnableEnemyWithRarity> list2 = (from e in currentLevel.OutsideEnemies.Concat(currentLevel.DaytimeEnemies)
				where disabledEnemies.Contains(((Object)e.enemyType).name) ? IsAllowlist : (!IsAllowlist)
				select e).ToList();
			int num = Random.Range(1, 2 + (list2.Count + list.Count) / 2);
			if (num == 1 && !ShouldSpawnTurrets)
			{
				num++;
			}
			if (num == 2 && !ShouldSpawnMines)
			{
				num++;
			}
			switch (num)
			{
			case 1:
			{
				SpawnableMapObject[] spawnableMapObjects = currentLevel.spawnableMapObjects;
				foreach (SpawnableMapObject val2 in spawnableMapObjects)
				{
					if (!((Object)(object)val2.prefabToSpawn.GetComponentInChildren<Turret>() == (Object)null))
					{
						pos -= Vector3.up * 1.8f;
						GameObject val3 = Object.Instantiate<GameObject>(val2.prefabToSpawn, pos, Quaternion.identity);
						val3.transform.position = pos;
						Transform transform = val3.transform;
						val = player_pos - pos;
						transform.forward = ((Vector3)(ref val)).normalized;
						val3.GetComponent<NetworkObject>().Spawn(true);
						ManualLogSource obj2 = mls;
						val = pos;
						obj2.LogInfo((object)("Tried spawning a turret at " + ((object)(Vector3)(ref val)).ToString()));
						break;
					}
				}
				return;
			}
			case 2:
			{
				SpawnableMapObject[] spawnableMapObjects2 = currentLevel.spawnableMapObjects;
				foreach (SpawnableMapObject val4 in spawnableMapObjects2)
				{
					if (!((Object)(object)val4.prefabToSpawn.GetComponentInChildren<Landmine>() == (Object)null))
					{
						pos -= Vector3.up * 1.8f;
						GameObject val5 = Object.Instantiate<GameObject>(val4.prefabToSpawn, pos, Quaternion.identity);
						val5.transform.position = pos;
						val5.transform.forward = new Vector3(1f, 0f, 0f);
						val5.GetComponent<NetworkObject>().Spawn(true);
						ManualLogSource obj3 = mls;
						val = pos;
						obj3.LogInfo((object)("Tried spawning a mine at " + ((object)(Vector3)(ref val)).ToString()));
						break;
					}
				}
				return;
			}
			}
			SpawnableEnemyWithRarity val6;
			if (inside)
			{
				if (AllowOutsideSpawnInside)
				{
					list.AddRange(list2);
				}
				if (list.Count < 1)
				{
					mls.LogInfo((object)"Cant spawn enemy - no other enemies present to copy from");
					return;
				}
				val6 = list[Random.Range(0, list.Count - 1)];
			}
			else
			{
				if (AllowInsideSpawnOutside)
				{
					list2.AddRange(list);
				}
				if (list2.Count < 1)
				{
					mls.LogInfo((object)"Cant spawn enemy - no other enemies present to copy from");
					return;
				}
				val6 = list2[Random.Range(0, list2.Count - 1)];
			}
			pos += Vector3.up * 0.25f;
			ManualLogSource obj4 = mls;
			string enemyName = val6.enemyType.enemyName;
			val = pos;
			obj4.LogInfo((object)("Spawning " + enemyName + " at " + ((object)(Vector3)(ref val)).ToString()));
			SpawnEnemy(val6, pos, 0f);
		}

		private static void SpawnEnemy(SpawnableEnemyWithRarity enemy, Vector3 pos, float rot)
		{
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_000f: Unknown result type (might be due to invalid IL or missing references)
			RoundManager.Instance.SpawnEnemyGameObject(pos, rot, -1, enemy.enemyType);
		}
	}
	public static class PluginInfo
	{
		public const string PLUGIN_GUID = "LethalPresents";

		public const string PLUGIN_NAME = "LethalPresents";

		public const string PLUGIN_VERSION = "1.0.7";
	}
}