Decompiled source of SneakBotMania v2.1.1

SneakBotMania.dll

Decompiled 6 months ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using Receiver2;
using Receiver2.SneakBot;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("SneakBotMania")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("SneakBotMania")]
[assembly: AssemblyCopyright("Copyright ©  2021")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("c7cf4d90-6553-49e8-8351-932c03d4a5be")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.6.1", FrameworkDisplayName = "")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace SneakBotMania;

[BepInProcess("Receiver2.exe")]
[BepInPlugin("SmilingLizard.plugins.sneakbotmania", "SneakBot Mania", "2.1.1")]
public class SneakBotMania : BaseUnityPlugin
{
	private ConfigEntry<KeyboardShortcut> manualSpawnCfg;

	private ConfigEntry<int> minimumSpawnCfg;

	private ConfigEntry<int> maximumSpawnCfg;

	private ConfigEntry<float> initialTimerCfg;

	private ConfigEntry<float> minTimerCfg;

	private ConfigEntry<float> maxTimerCfg;

	private ConfigEntry<bool> enableTimeCfg;

	private ConfigEntry<bool> unlimitVanilla;

	private ConfigEntry<bool> alternateGong;

	private Random rng;

	private float timer = -1f;

	private static int count = 10;

	private static SneakBotMania Instance { get; set; }

	private int MinAmount => minimumSpawnCfg.Value;

	private int MaxAmount => maximumSpawnCfg.Value;

	private bool RandomAmount => maximumSpawnCfg.Value > MinAmount;

	private int SpawnAmount
	{
		get
		{
			if (!RandomAmount)
			{
				return MinAmount;
			}
			return GetRandomizedAmount();
		}
	}

	private float MinTimer => minTimerCfg.Value;

	private float Maxtimer => maxTimerCfg.Value;

	private bool TimerEnabled
	{
		get
		{
			if (enableTimeCfg.Value)
			{
				return maxTimerCfg.Value > 0f;
			}
			return false;
		}
	}

	private bool TimerRandom
	{
		get
		{
			if (0f < MinTimer)
			{
				return MinTimer < maxTimerCfg.Value;
			}
			return false;
		}
	}

	public void Awake()
	{
		//IL_001d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0055: Unknown result type (might be due to invalid IL or missing references)
		//IL_005f: Expected O, but got Unknown
		//IL_0088: Unknown result type (might be due to invalid IL or missing references)
		//IL_0092: Expected O, but got Unknown
		//IL_00c6: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d0: Expected O, but got Unknown
		//IL_0104: Unknown result type (might be due to invalid IL or missing references)
		//IL_010e: Expected O, but got Unknown
		//IL_0142: Unknown result type (might be due to invalid IL or missing references)
		//IL_014c: Expected O, but got Unknown
		Instance = this;
		manualSpawnCfg = ((BaseUnityPlugin)this).Config.Bind<KeyboardShortcut>("Keybind", "Manual Spawn", new KeyboardShortcut((KeyCode)0, Array.Empty<KeyCode>()), "Spawns SneakBots; amount determined same as normal; circumvents any spawn limits.");
		minimumSpawnCfg = ((BaseUnityPlugin)this).Config.Bind<int>("Spawn Amount", "Minimum", 1, new ConfigDescription("The minimum amount of SneakBots to spawn whenever any are spawned.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(1, 50), Array.Empty<object>()));
		maximumSpawnCfg = ((BaseUnityPlugin)this).Config.Bind<int>("Spawn Amount", "Maximum", 1, new ConfigDescription("The maximum amount of SneakBots to spawn whenever any are spawned. If this is equal or smaller than the minimum amount, then the amount spawned isn't random but equal the minimum value instead.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(1, 50), Array.Empty<object>()));
		initialTimerCfg = ((BaseUnityPlugin)this).Config.Bind<float>("Timer", "Timer Initial Duration", 0f, new ConfigDescription("The length of the timer is randomly set whenever it elapses. This is the duration it starts with. Set to zero to determine inital duration by normal rules.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 600f), Array.Empty<object>()));
		minTimerCfg = ((BaseUnityPlugin)this).Config.Bind<float>("Timer", "Timer Minimum Duration", 0f, new ConfigDescription("This is the minimum duration it can be set to. Set to zero or a value grater than the maximum to make the timer not random.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 600f), Array.Empty<object>()));
		maxTimerCfg = ((BaseUnityPlugin)this).Config.Bind<float>("Timer", "Timer Maximum Duration", 0f, new ConfigDescription("This is the maximum duration it can be set to. Set to zero to disable the timer.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 600f), Array.Empty<object>()));
		enableTimeCfg = ((BaseUnityPlugin)this).Config.Bind<bool>("Timer", "Enable Timer", false, "Enables the timer. Use this to disable it so that you can change its durations without immediately spawning a SneakBot.");
		unlimitVanilla = ((BaseUnityPlugin)this).Config.Bind<bool>("Overwrite", "Natural Limit", false, "Removes the limit of 1 per level from normal means of spawning sneakbots. (\"Alternate Gong Mechanic\" recommended)");
		alternateGong = ((BaseUnityPlugin)this).Config.Bind<bool>("Overwrite", "Alternate Gong Mechanic", false, "Changes the way the gong decides to spawn sneakbots to be more compatible with \"Natural Limit\".");
		timer = ((initialTimerCfg.Value > 0f) ? initialTimerCfg.Value : GetNextTimerDuration());
		rng = new Random();
		Harmony.CreateAndPatchAll(typeof(SneakBotMania), (string)null);
	}

	public void Update()
	{
		//IL_005e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0063: Unknown result type (might be due to invalid IL or missing references)
		if (TimerEnabled)
		{
			if (RuntimeTileLevelGenerator.instance == null)
			{
				timer = initialTimerCfg.Value;
			}
			else
			{
				timer -= Time.deltaTime;
				if (timer <= 0f)
				{
					timer = GetNextTimerDuration();
					RuntimeTileLevelGenerator.instance.CreateSneakBot();
				}
			}
		}
		KeyboardShortcut value = manualSpawnCfg.Value;
		if (((KeyboardShortcut)(ref value)).IsDown())
		{
			if (RuntimeTileLevelGenerator.instance == null)
			{
				((BaseUnityPlugin)this).Logger.LogWarning((object)"Failed to spawn SneakBots because no RuntimeTileLevelGenerator instance was in the scene.");
			}
			else
			{
				SneakBotLoop();
			}
		}
	}

	private float GetNextTimerDuration()
	{
		if (!TimerRandom)
		{
			return MinTimer;
		}
		return (float)rng.NextDouble() * Maxtimer + MinTimer;
	}

	private int GetRandomizedAmount()
	{
		if (!RandomAmount)
		{
			return MinAmount;
		}
		return rng.Next(MinAmount, MaxAmount + 1);
	}

	public void SneakBotLoop()
	{
		if (RuntimeTileLevelGenerator.instance == null)
		{
			((BaseUnityPlugin)this).Logger.LogWarning((object)"Failed to spawn SneakBots because no RuntimeTileLevelGenerator instance was in the scene.");
			return;
		}
		int spawnAmount = SpawnAmount;
		((BaseUnityPlugin)this).Logger.LogInfo((object)$"Spawning {spawnAmount} SneakBots. Enjoy!");
		for (int i = 0; i < spawnAmount - 1; i++)
		{
			OriginalSneakBot(RuntimeTileLevelGenerator.instance);
		}
	}

	[HarmonyPostfix]
	[HarmonyPatch(typeof(RuntimeTileLevelGenerator), "CreateSneakBot")]
	[HarmonyAfter(new string[] { "OriginalSneakBot" })]
	public static void SneakBotPatch()
	{
		Instance.SneakBotLoop();
	}

	[HarmonyReversePatch(/*Could not decode attribute arguments.*/)]
	[HarmonyPatch(typeof(RuntimeTileLevelGenerator), "CreateSneakBot")]
	public static ActiveEnemy OriginalSneakBot(RuntimeTileLevelGenerator instance)
	{
		throw new NotImplementedException("This method failed to be replaced with CreateSneakBot().");
	}

	[HarmonyPrefix]
	[HarmonyPatch(typeof(SneakBotSpawner), "TrySpawnSneakBot")]
	public static bool UnlimitSpawner()
	{
		if (Instance.unlimitVanilla.Value)
		{
			RuntimeTileLevelGenerator instance = RuntimeTileLevelGenerator.instance;
			if ((Object)(object)instance != (Object)null)
			{
				RankingProgressionGameMode val = (RankingProgressionGameMode)((ReceiverCoreScript.Instance() == null) ? null : /*isinst with value type is only supported in some contexts*/);
				if ((Object)(object)val != (Object)null && !val.progression_data.has_picked_up_sneakbot_tape && !val.progression_data.has_picked_up_mindcontrol_tape)
				{
					RankingProgressionGameMode.can_unlock_sneak_bot_note = true;
				}
				ReceiverOnScreenMessage.QueueMessage(Locale.GetUIString((LocaleUIString)395));
				instance.CreateSneakBot();
				return false;
			}
		}
		return true;
	}

	[HarmonyPrefix]
	[HarmonyPatch(typeof(CountTrigger), "Increment")]
	public static bool StaticCount(CountTrigger __instance)
	{
		if (Instance.alternateGong.Value)
		{
			SneakBotSpawner component = ((Component)__instance).gameObject.GetComponent<SneakBotSpawner>();
			if (component != null)
			{
				if (--count <= 0)
				{
					count = Instance.rng.Next(1, 12);
					if (Instance.rng.Next(100) == 0)
					{
						component.TrySpawnSneakBotInstanced();
						component.TrySpawnSneakBotInstanced();
						component.TrySpawnSneakBotInstanced();
					}
					else
					{
						component.TrySpawnSneakBotInstanced();
					}
				}
				return false;
			}
		}
		return true;
	}
}