Decompiled source of SpawnWeightOverrides v1.0.0

SpawnWeightOverrides.dll

Decompiled a day 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 BepInEx.Logging;
using HarmonyLib;
using LethalLib.Modules;
using Microsoft.CodeAnalysis;
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("SpawnWeightOverrides")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+2664facdac8ab07d4d1ce8abe8bb895a04038a1d")]
[assembly: AssemblyProduct("SpawnWeightOverrides")]
[assembly: AssemblyTitle("SpawnWeightOverrides")]
[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.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace SpawnWeightOverrides
{
	public static class PluginMetadata
	{
		public const string PLUGIN_VERSION = "1.0.0";

		public const string PLUGIN_NAME = "SpawnWeightOverrides";

		public const string PLUGIN_AUTHOR = "hoffr";

		public const string PLUGIN_GUID = "hoffr.SpawnWeightOverrides";
	}
	[BepInPlugin("hoffr.SpawnWeightOverrides", "SpawnWeightOverrides", "1.0.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[BepInProcess("Lethal Company.exe")]
	public class Plugin : BaseUnityPlugin
	{
		public static Plugin Instance;

		public ManualLogSource Log = Logger.CreateLogSource("SpawnWeightOverrides");

		private ConfigFile configFile;

		private void Awake()
		{
			//IL_0022: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)Instance == (Object)null)
			{
				Instance = this;
			}
			MakeConfig();
			new Harmony("hoffr.SpawnWeightOverrides").PatchAll(Assembly.GetExecutingAssembly());
			Log.LogInfo((object)"SpawnWeightOverrides v1.0.0 loaded.");
		}

		public void MakeConfig()
		{
			//IL_0012: Unknown result type (might be due to invalid IL or missing references)
			//IL_001c: Expected O, but got Unknown
			configFile = new ConfigFile(Path.Combine(Paths.ConfigPath, "SpawnWeightOverrides.cfg"), true);
		}

		public void ReregisterScrap()
		{
			//IL_00f8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fd: Unknown result type (might be due to invalid IL or missing references)
			//IL_0102: Unknown result type (might be due to invalid IL or missing references)
			//IL_0122: Unknown result type (might be due to invalid IL or missing references)
			int num = 0;
			int num2 = 0;
			List<string> list = Items.scrapItems.Select((ScrapItem item) => item.modName).Distinct().ToList();
			foreach (string modName in list)
			{
				int num3 = MakeScrapPercentageConfig(modName);
				if (num3 == -1)
				{
					continue;
				}
				foreach (ScrapItem item in Items.scrapItems.Where((ScrapItem item) => item.modName == modName))
				{
					Dictionary<LevelTypes, int> dictionary = new Dictionary<LevelTypes, int>(item.levelRarities);
					Dictionary<string, int> dictionary2 = new Dictionary<string, int>(item.customLevelRarities);
					int num4 = 0;
					foreach (LevelTypes item2 in dictionary.Keys.ToList())
					{
						num4 = (int)Math.Floor((double)dictionary[item2] * ((double)num3 / 100.0));
						dictionary[item2] = num4;
					}
					foreach (string item3 in dictionary2.Keys.ToList())
					{
						num4 = (int)Math.Floor((double)dictionary2[item3] * ((double)num3 / 100.0));
						dictionary2[item3] = num4;
					}
					item.levelRarities.Clear();
					item.customLevelRarities.Clear();
					Items.RegisterScrap(item.item, dictionary, dictionary2);
					Log.LogInfo((object)("Re-registered scrap [" + item.modName + "] " + item.item.itemName));
					num2++;
				}
				num++;
			}
			Log.LogInfo((object)$"Re-registered {num2} scrap items for {num} mods");
		}

		public int MakeScrapPercentageConfig(string modName)
		{
			ConfigEntry<int> val = configFile.Bind<int>("(2. Scrap) " + modName + ": Spawn weights", "Percentage of original spawn weights", 100, "Percentage of the original spawn weights for all scrap items in mod: " + modName);
			if (val.Value == 100)
			{
				Log.LogDebug((object)("Default scrap rarity config entry used for '" + modName + "', using this item's defaults"));
				return val.Value;
			}
			if (val.Value >= 0)
			{
				return val.Value;
			}
			Log.LogWarning((object)("Invalid scrap rarity config entry for '" + modName + "', using this item's defaults"));
			return -1;
		}

		public void ReregisterEnemies()
		{
			//IL_023d: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e7: Unknown result type (might be due to invalid IL or missing references)
			if (Enemies.spawnableEnemies == null || !Enemies.spawnableEnemies.Any())
			{
				return;
			}
			List<SpawnableEnemy> list = new List<SpawnableEnemy>(Enemies.spawnableEnemies);
			foreach (SpawnableEnemy item in list)
			{
				int num = MakeEnemyRarityConfig(item);
				int num2 = MakePowerLevelConfig(item);
				int num3 = MakeMaxCountConfig(item);
				int num4 = MakeGroupSizeConfig(item);
				Enemies.RemoveEnemyFromLevels(item.enemy, (LevelTypes)1, (string[])null);
				Enemies.spawnableEnemies.Remove(item);
				int num5 = 0;
				if (num2 != -1)
				{
					Log.LogDebug((object)$"Setting power level for {item.enemy.enemyName} to {num2}, was {item.enemy.PowerLevel}");
					item.enemy.PowerLevel = num2;
					num5++;
				}
				if (num3 != -1)
				{
					Log.LogDebug((object)$"Setting max count for {item.enemy.enemyName} to {num3}, was {item.enemy.MaxCount}");
					item.enemy.MaxCount = num3;
					num5++;
				}
				if (num4 != -1)
				{
					Log.LogDebug((object)$"Setting group size for {item.enemy.enemyName} to {num4}, was {item.enemy.spawnInGroupsOf}");
					item.enemy.spawnInGroupsOf = num4;
					num5++;
				}
				if (num != -1)
				{
					Log.LogDebug((object)$"Setting rarity for {item.enemy.enemyName} to {num}");
					if (num5 + 1 != 0)
					{
						Enemies.RegisterEnemy(item.enemy, num, (LevelTypes)(-1), item.spawnType, (string[])null, item.terminalNode, item.infoKeyword);
						Log.LogInfo((object)("Re-registered enemy " + item.enemy.enemyName + "."));
					}
				}
				else if (num5 > 0)
				{
					Enemies.RegisterEnemy(item.enemy, item.spawnType, item.levelRarities, item.customLevelRarities, item.terminalNode, item.infoKeyword);
					Log.LogInfo((object)("Re-registered enemy " + item.enemy.enemyName + "."));
				}
				Log.LogInfo((object)("Re-registered enemy " + item.enemy.enemyName));
			}
		}

		public int MakeEnemyRarityConfig(SpawnableEnemy spawnableEnemy)
		{
			ConfigEntry<int> val = configFile.Bind<int>("(1. Enemy) " + spawnableEnemy.enemy.enemyName, "Spawn weight for this enemy for all moons", -1, "Leave at -1 for default, any positive (or 0) integer otherwise");
			if (val.Value == -1)
			{
				Log.LogDebug((object)("Default rarity config entry used for '" + spawnableEnemy.enemy.enemyName + "', using this EnemyType's defaults"));
				return val.Value;
			}
			if (val.Value >= 0)
			{
				return val.Value;
			}
			Log.LogWarning((object)("Invalid rarity config entry for '" + spawnableEnemy.enemy.enemyName + "', using this EnemyType's defaults"));
			return -1;
		}

		public int MakePowerLevelConfig(SpawnableEnemy spawnableEnemy)
		{
			ConfigEntry<int> val = configFile.Bind<int>("(1. Enemy) " + spawnableEnemy.enemy.enemyName, "Power level (number that affects how often all enemies spawn)", -1, "Leave blank for default, any positive nonzero integer otherwise.\n3 is standard for powerful enemies. Higher generally means you'll see less of this enemy, and less enemies in general");
			if (val.Value == -1)
			{
				Log.LogDebug((object)("Default power level config entry used for '" + spawnableEnemy.enemy.enemyName + "', using this EnemyType's default"));
				return val.Value;
			}
			if (val.Value > 0)
			{
				return val.Value;
			}
			Log.LogWarning((object)("Invalid power level config entry for '" + spawnableEnemy.enemy.enemyName + "', using this EnemyType's default"));
			return -1;
		}

		public int MakeGroupSizeConfig(SpawnableEnemy spawnableEnemy)
		{
			ConfigEntry<int> val = configFile.Bind<int>("(1. Enemy) " + spawnableEnemy.enemy.enemyName, "Group size (how many in each spawn)", -1, "Leave blank for default, any positive nonzero integer otherwise.\nNumber of this enemy that will spawn in each group\nOnly affects outdoor enemies");
			if (val.Value == -1)
			{
				Log.LogDebug((object)("Default group size config entry used for '" + spawnableEnemy.enemy.enemyName + "', using this EnemyType's default"));
				return val.Value;
			}
			if (val.Value > 0)
			{
				return val.Value;
			}
			Log.LogWarning((object)("Invalid group size config entry for '" + spawnableEnemy.enemy.enemyName + "', using this EnemyType's default"));
			return -1;
		}

		public int MakeMaxCountConfig(SpawnableEnemy spawnableEnemy)
		{
			ConfigEntry<int> val = configFile.Bind<int>("(1. Enemy) " + spawnableEnemy.enemy.enemyName, "Max count (how many groups to spawn)", -1, "Leave blank for default, any positive nonzero integer otherwise\nMaximum number of groups of this enemy that can spawn in a day");
			if (val.Value == -1)
			{
				Log.LogDebug((object)("Default max count config entry used for '" + spawnableEnemy.enemy.enemyName + "', using this EnemyType's default"));
				return val.Value;
			}
			if (val.Value > 0)
			{
				return val.Value;
			}
			Log.LogWarning((object)("Invalid max count config entry for '" + spawnableEnemy.enemy.enemyName + "', using this EnemyType's default"));
			return -1;
		}
	}
	[HarmonyPatch(typeof(StartOfRound))]
	public class StartOfRoundPatch
	{
		[HarmonyPostfix]
		[HarmonyPatch("Awake")]
		public static void OnStartOfRoundStart()
		{
			Plugin.Instance.ReregisterEnemies();
			Plugin.Instance.ReregisterScrap();
		}
	}
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "SpawnWeightOverrides";

		public const string PLUGIN_NAME = "SpawnWeightOverrides";

		public const string PLUGIN_VERSION = "1.0.0";
	}
}