Decompiled source of White Knuckle Randomizer v1.0.2

plugins/WhiteKnuckleRandomizer.dll

Decompiled an hour ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
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 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(".NETFramework,Version=v4.7.1", FrameworkDisplayName = ".NET Framework 4.7.1")]
[assembly: AssemblyCompany("WhiteKnuckleRandomizer")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("White Knuckle Randomizer")]
[assembly: AssemblyTitle("WhiteKnuckleRandomizer")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[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 WhiteKnuckleRandomizer
{
	public class ExcludeFromSwap : MonoBehaviour
	{
	}
	[BepInPlugin("whiteknucklerandomizermod", "White Knuckle Randomizer", "1.0.2")]
	public class Plugin : BaseUnityPlugin
	{
		[HarmonyPatch(typeof(UI_GamemodeScreen_Panel), "FillSettings")]
		private static class InjectSetting
		{
			private static RandomizeSetting gamemodeSetting;

			[HarmonyPostfix]
			private static void Postfix(UI_GamemodeScreen_Panel __instance)
			{
				//IL_0051: Unknown result type (might be due to invalid IL or missing references)
				//IL_0056: Unknown result type (might be due to invalid IL or missing references)
				try
				{
					if ((Object)(object)gamemodeSetting == (Object)null)
					{
						gamemodeSetting = ScriptableObject.CreateInstance<RandomizeSetting>();
						((GamemodeSetting)gamemodeSetting).id = "rndm";
						((GamemodeSetting)gamemodeSetting).title = "Randomizer";
						((GamemodeSetting)gamemodeSetting).description = "Randomizes items and denizens";
						((GamemodeSetting)gamemodeSetting).color = Color.blue;
						((GamemodeSetting)gamemodeSetting).exclusiveSettings = new List<string>(1) { "competetive" };
						((GamemodeSetting)gamemodeSetting).nameAppend = "";
						((GamemodeSetting)gamemodeSetting).leaderboardAppendID = "";
						((GamemodeSetting)gamemodeSetting).allowDownstream = false;
						((GamemodeSetting)gamemodeSetting).downstreamId = "";
						((GamemodeSetting)gamemodeSetting).unlock = ScriptableObject.CreateInstance<ProgressionUnlock>();
						((GamemodeSetting)gamemodeSetting).unlock.unlockRequirements = new List<UnlockRequirement>();
					}
					if ((Object)(object)gamemodeSetting == (Object)null)
					{
						throw new Exception("We fucked up");
					}
					UI_GamemodeSetting val = Object.Instantiate<UI_GamemodeSetting>(__instance.gamemodeSettingAsset, __instance.settingRoot);
					val.Initialize((GamemodeSetting)(object)gamemodeSetting, __instance);
				}
				catch (Exception ex)
				{
					Logger.LogError((object)ex);
				}
			}
		}

		[HarmonyPatch(typeof(CL_GameManager), "Start")]
		private static class GameStart
		{
			[HarmonyPrefix]
			private static void Prefix()
			{
				try
				{
					if (CL_GameManager.gamemode.HasActiveSetting("rndm"))
					{
						Randomize = true;
						CommandConsole.hasCheated = true;
					}
					else
					{
						Randomize = false;
						CommandConsole.hasCheated = false;
					}
					Logger.LogMessage((object)("Randomization is " + (Randomize ? "On" : "Off")));
				}
				catch (Exception ex)
				{
					Logger.LogError((object)ex);
				}
			}

			[HarmonyPostfix]
			private static void Postfix()
			{
				if (Initialized)
				{
					return;
				}
				try
				{
					prefabs = new List<GameObject>();
					prefabs.AddRange(CL_AssetManager.baseDatabase.denizenPrefabs);
					prefabs.AddRange(CL_AssetManager.baseDatabase.itemPrefabs);
					if (removeMass.Value && !evilMode.Value)
					{
						List<GameObject> list = new List<GameObject>();
						for (int i = 0; i < prefabs.Count; i++)
						{
							if (!((Object)prefabs[i]).name.Contains("Denizen_Hunter") || !((Object)prefabs[i]).name.Contains("Denizen_Face"))
							{
								list.Add(prefabs[i]);
							}
						}
						prefabs = list;
					}
					else if (evilMode.Value)
					{
						GameObject val = null;
						for (int j = 0; j < prefabs.Count; j++)
						{
							if (((Object)prefabs[j]).name.Contains("Hunter"))
							{
								val = prefabs[j];
							}
						}
						if ((Object)(object)val == (Object)null)
						{
							Logger.LogWarning((object)"Couldnt find Hunter Denizen");
							evilMode.Value = false;
							Initialized = true;
							return;
						}
						prefabs = new List<GameObject> { val, val, val };
					}
					Initialized = true;
				}
				catch (Exception ex)
				{
					Logger.LogFatal((object)ex);
				}
			}
		}

		[HarmonyPatch(typeof(HandItem_Food), "Initialize")]
		private static class EatenItemFixer
		{
			[HarmonyPostfix]
			private static void Postfix(HandItem_Food __instance)
			{
				((Component)__instance.usedObject).gameObject.AddComponent<ExcludeFromSwap>();
			}
		}

		[HarmonyPatch(typeof(Item_Object), "Start")]
		private static class ItemReplacer
		{
			[HarmonyPrefix]
			private static bool Prepatch(Item_Object __instance)
			{
				return ReplacerLogic((MonoBehaviour)(object)__instance, isDenizen: false);
			}
		}

		[HarmonyPatch(typeof(SaveData), "LoadDataIntoPlayer")]
		private static class PreventInventoryItemChange
		{
			[HarmonyPostfix]
			private static void Postfix(ref ENT_Player player, bool loadInventory = true)
			{
				if (loadInventory)
				{
					object? value = typeof(ENT_Player).GetField("inventory", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(player);
					Inventory val = (Inventory)((value is Inventory) ? value : null);
					Item_Object[] componentsInChildren = ((Component)val.itemRoot).GetComponentsInChildren<Item_Object>();
					for (int i = 0; i < componentsInChildren.Length; i++)
					{
						((Component)componentsInChildren[i]).gameObject.AddComponent<ExcludeFromSwap>();
					}
				}
			}
		}

		[HarmonyPatch(typeof(Denizen), "Start")]
		private static class DenizenReplacer
		{
			[HarmonyPrefix]
			private static bool Prepatch(Denizen __instance)
			{
				return ReplacerLogic((MonoBehaviour)(object)__instance, isDenizen: true);
			}
		}

		internal static ManualLogSource Logger;

		public static bool Randomize;

		public static ConfigEntry<bool> limitMass;

		public static ConfigEntry<bool> removeMass;

		public static ConfigEntry<bool> rareTrinkets;

		public static ConfigEntry<bool> evilMode;

		public static List<GameObject> prefabs;

		public static bool Initialized;

		protected static Harmony harmony;

		private void SetupConfig()
		{
			limitMass = ((BaseUnityPlugin)this).Config.Bind<bool>("Random Spawning", "Limit mass spawns", true, "Limits mass replacements to abyss and beyond.");
			removeMass = ((BaseUnityPlugin)this).Config.Bind<bool>("Random Spawning", "Remove all mass replacements", false, "Removes mass from the random denizen pool.");
			rareTrinkets = ((BaseUnityPlugin)this).Config.Bind<bool>("Random Spawning", "Make trinkets rare", false, "Make trinkets and artifacts spawn less. This should balance the early game a bit more.");
			evilMode = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Easy Mode", false, "");
		}

		private void Awake()
		{
			//IL_0028: Unknown result type (might be due to invalid IL or missing references)
			//IL_0032: Expected O, but got Unknown
			SetupConfig();
			Logger = ((BaseUnityPlugin)this).Logger;
			Logger.LogInfo((object)"Plugin whiteknucklerandomizermod is loaded!");
			harmony = new Harmony("whiteknucklerandomizermod");
			harmony.PatchAll(Assembly.GetExecutingAssembly());
			Object.DontDestroyOnLoad((Object)(object)this);
		}

		private void Start()
		{
		}

		private void OnDestroy()
		{
			harmony.UnpatchSelf();
			Logger.LogFatal((object)"Plugin is being Destroyed");
		}

		private static GameObject EnsureNoRareItem()
		{
			GameObject val = null;
			for (int i = 0; i < 300; i++)
			{
				val = prefabs[Random.Range(0, prefabs.Count)];
				Component[] components = val.GetComponents(typeof(HandItem));
				Item_Object component = val.gameObject.GetComponent<Item_Object>();
				if ((Object)(object)component != (Object)null)
				{
					if (((Object)val).name.Contains("Trinket"))
					{
						continue;
					}
					bool flag = false;
					for (int j = 0; j < component.itemData.executionModules.Count; j++)
					{
						if (((object)component.itemData.executionModules[j]).GetType() == typeof(ItemExecutionModule_PerkAdder))
						{
							flag = true;
						}
					}
					if (flag)
					{
						continue;
					}
					Component[] array = components;
					foreach (Component val2 in array)
					{
						Type type = ((object)val2).GetType();
						if (!(type == typeof(HandItem_Rewind)) && !(type == typeof(HandItem_Glove)) && !(type == typeof(HandItem_Melee_Rapier)) && !(type == typeof(HandItem_BlinkEye)) && !(type == typeof(HandItem_GrapplingHook)))
						{
							return val;
						}
					}
					continue;
				}
				return val;
			}
			if ((Object)(object)val == (Object)null)
			{
				Logger.LogFatal((object)"Somehow no valid object found");
				return prefabs[Random.Range(0, prefabs.Count)];
			}
			return val;
		}

		private static bool ReplacerLogic(MonoBehaviour __instance, bool isDenizen)
		{
			//IL_0045: Unknown result type (might be due to invalid IL or missing references)
			//IL_03f4: Unknown result type (might be due to invalid IL or missing references)
			//IL_03d9: Unknown result type (might be due to invalid IL or missing references)
			try
			{
				if ((Object)(object)((Component)__instance).GetComponent<ExcludeFromSwap>() != (Object)null)
				{
					return true;
				}
				if (!Randomize)
				{
					return true;
				}
				if (__instance is Item_Object && !isDenizen && (Object)(object)((Item_Object)__instance).itemData.inventory != (Object)null)
				{
					return true;
				}
				if (((Object)__instance).name.Contains("Denizen_Hunter") || ((Object)__instance).name.Contains("Denizen_Face") || ((Object)__instance).name.Contains("Mother"))
				{
					((Component)__instance).gameObject.AddComponent<ExcludeFromSwap>();
					return true;
				}
				GameObject val = Object.Instantiate<GameObject>(prefabs[Random.Range(0, prefabs.Count)]);
				if (rareTrinkets.Value)
				{
					HandItem component = val.GetComponent<HandItem>();
					bool flag = false;
					Item_Object component2 = val.gameObject.GetComponent<Item_Object>();
					if ((Object)(object)component2 != (Object)null)
					{
						if (((Object)val).name.Contains("Trinket"))
						{
							if (Random.Range(0, 10) <= 9)
							{
								Object.Destroy((Object)(object)val);
								val = EnsureNoRareItem();
							}
							flag = true;
						}
						if (!flag)
						{
							for (int i = 0; i < component2.itemData.executionModules.Count; i++)
							{
								if (((object)component2.itemData.executionModules[i]).GetType() == typeof(ItemExecutionModule_PerkAdder))
								{
									if (Random.Range(0, 10) <= 9)
									{
										Object.Destroy((Object)(object)val);
										val = EnsureNoRareItem();
									}
									flag = true;
									break;
								}
							}
						}
						if (!flag && (Object)(object)component != (Object)null)
						{
							Type type = ((object)component).GetType();
							if ((type == typeof(HandItem_Rewind) || type == typeof(HandItem_Glove) || type == typeof(HandItem_Melee_Rapier) || type == typeof(HandItem_BlinkEye) || type == typeof(HandItem_GrapplingHook)) && Random.Range(0, 10) <= 9)
							{
								Object.Destroy((Object)(object)val);
								val = EnsureNoRareItem();
							}
						}
					}
				}
				if ((((Object)val).name.Contains("Denizen_Hunter") || ((Object)val).name.Contains("Denizen_Face")) && !CL_EventManager.currentRegion.regionName.Contains("abyss") && !CL_EventManager.currentRegion.regionName.Contains("nest") && limitMass.Value && !evilMode.Value)
				{
					bool flag2 = false;
					Object.Destroy((Object)(object)val);
					val = Object.Instantiate<GameObject>(prefabs[Random.Range(0, prefabs.Count)]);
					while (!flag2)
					{
						if (((Object)val).name.Contains("Denizen_Hunter") || ((Object)val).name.Contains("Denizen_Face"))
						{
							Object.Destroy((Object)(object)val);
							val = Object.Instantiate<GameObject>(prefabs[Random.Range(0, prefabs.Count)]);
							continue;
						}
						flag2 = true;
						break;
					}
				}
				val.AddComponent<ExcludeFromSwap>();
				if ((Object)(object)val.GetComponent<Denizen>() != (Object)null)
				{
					Denizen component3 = val.GetComponent<Denizen>();
					((GameEntity)component3).Teleport(((Component)__instance).transform.position);
				}
				else
				{
					val.transform.position = ((Component)__instance).transform.position;
				}
				((Object)val).name = ((Object)__instance).name;
				val.transform.parent = ((Component)__instance).transform.parent;
				Object.Destroy((Object)(object)((Component)__instance).gameObject);
				return false;
			}
			catch (Exception ex)
			{
				Logger.LogError((object)ex);
				Logger.LogError((object)ex.Source);
				return true;
			}
		}
	}
	public class PluginInfo
	{
		public const string PLUGIN_GUID = "whiteknucklerandomizermod";

		public const string PLUGIN_NAME = "White Knuckle Randomizer";

		public const string PLUGIN_VERSION = "1.0.2";
	}
	public class RandomizeSetting : GamemodeSetting
	{
		public RandomizeSetting()
		{
			//IL_002a: Unknown result type (might be due to invalid IL or missing references)
			//IL_002f: Unknown result type (might be due to invalid IL or missing references)
			base.id = "rndm";
			base.title = "Randomizer";
			base.description = "Randomizes items and denizens";
			base.color = Color.blue;
			base.exclusiveSettings = new List<string>(1) { "competetive" };
			base.nameAppend = "";
			base.leaderboardAppendID = "";
			base.allowDownstream = false;
			base.downstreamId = "";
		}
	}
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "WhiteKnuckleRandomizer";

		public const string PLUGIN_NAME = "White Knuckle Randomizer";

		public const string PLUGIN_VERSION = "1.0.0";
	}
}