Decompiled source of PestControl v1.0.1

BepInEx/plugins/PestControl.dll

Decompiled 11 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("PestControl")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("PestControl")]
[assembly: AssemblyCopyright("Copyright ©  2023")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("d2469a2d-0fe3-463e-9f8c-ba7b095962cf")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace PestControl;

internal static class Config
{
	private static ConfigFile _configFile;

	private static string _configPath = Path.Combine(Paths.ConfigPath, "PestControl.cfg");

	public static Dictionary<string, Vector2> GetWeights(Terminal terminal)
	{
		//IL_0161: Unknown result type (might be due to invalid IL or missing references)
		//IL_018e: Unknown result type (might be due to invalid IL or missing references)
		//IL_01a0: Unknown result type (might be due to invalid IL or missing references)
		CheckFile();
		HashSet<EnemyType> enemies = new HashSet<EnemyType>();
		SelectableLevel[] moonsCatalogueList = terminal.moonsCatalogueList;
		foreach (SelectableLevel obj in moonsCatalogueList)
		{
			Action<EnemyType> action = delegate(EnemyType x)
			{
				if (!enemies.TryGetValue(x, out var _))
				{
					enemies.Add(x);
				}
			};
			obj.Enemies.Select((SpawnableEnemyWithRarity x) => x.enemyType).ToList().ForEach(action);
			obj.DaytimeEnemies.Select((SpawnableEnemyWithRarity x) => x.enemyType).ToList().ForEach(action);
			obj.OutsideEnemies.Select((SpawnableEnemyWithRarity x) => x.enemyType).ToList().ForEach(action);
		}
		Dictionary<string, Vector2> dictionary = new Dictionary<string, Vector2>();
		foreach (EnemyType item in enemies)
		{
			string enemyName = item.enemyName;
			dictionary[enemyName] = new Vector2(_configFile.Bind<float>(enemyName, "Multiplier", 1f, "Scales spawn weight. Set to 0 to override with Addition.").Value, _configFile.Bind<float>(enemyName, "Addition", 0f, "Adjusts weight after Multiplier. Use for precise control.").Value);
			Plugin.Log.LogInfo((object)(enemyName + ":"));
			Plugin.Log.LogInfo((object)$"Multiplier:{dictionary[enemyName].x} Addition:{dictionary[enemyName].y}");
		}
		return dictionary;
	}

	private static void CheckFile()
	{
		//IL_000d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0017: Expected O, but got Unknown
		if (_configFile == null)
		{
			_configFile = new ConfigFile(_configPath, true);
		}
	}
}
internal static class Patch
{
	private static Dictionary<string, Vector2> weights = new Dictionary<string, Vector2>();

	[HarmonyPatch(typeof(RoundManager), "LoadNewLevel")]
	[HarmonyTranspiler]
	private static IEnumerable<CodeInstruction> LoadNewLevel(IEnumerable<CodeInstruction> instructions)
	{
		//IL_0063: Unknown result type (might be due to invalid IL or missing references)
		//IL_0069: Expected O, but got Unknown
		Plugin.Log.LogInfo((object)"Started Transpiling [LoadNewLevel].");
		List<CodeInstruction> list = new List<CodeInstruction>(instructions);
		int num = list.FindIndex((CodeInstruction x) => x.opcode == OpCodes.Stfld);
		list.InsertRange(num + 1, (IEnumerable<CodeInstruction>)(object)new CodeInstruction[1]
		{
			new CodeInstruction(OpCodes.Call, (object)typeof(Patch).GetMethod("SetWeights", BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public))
		});
		Plugin.Log.LogInfo((object)"Transpiled method successfully.");
		return list;
	}

	[HarmonyPatch(typeof(Terminal), "Start")]
	[HarmonyPostfix]
	private static void Start(Terminal __instance)
	{
		weights = Config.GetWeights(__instance);
	}

	public static void SetWeights()
	{
		//IL_0096: 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)
		SelectableLevel currentLevel = RoundManager.Instance.currentLevel;
		Plugin.Log.LogInfo((object)"Setting adjusted weights!");
		foreach (SpawnableEnemyWithRarity item in currentLevel.Enemies.Concat(currentLevel.DaytimeEnemies).Concat(currentLevel.OutsideEnemies).ToList())
		{
			foreach (KeyValuePair<string, Vector2> weight in weights)
			{
				string key = weight.Key;
				if (key == item.enemyType.enemyName)
				{
					int rarity = item.rarity;
					item.rarity = (int)((float)item.rarity * weight.Value.x + weight.Value.y);
					Plugin.Log.LogInfo((object)$"{key}, old rarity: {rarity}, new rariy: {item.rarity}");
					break;
				}
			}
		}
	}
}
[BepInPlugin("niceh.PestControl", "PestControl", "1.0.1")]
public class Plugin : BaseUnityPlugin
{
	public const string GUID = "niceh.PestControl";

	public const string Name = "PestControl";

	public static Plugin Instance { get; private set; }

	public static ManualLogSource Log { get; private set; }

	private void Awake()
	{
		//IL_0016: Unknown result type (might be due to invalid IL or missing references)
		Instance = this;
		Log = ((BaseUnityPlugin)this).Logger;
		new Harmony("niceh.PestControl").PatchAll(typeof(Patch));
	}
}