Decompiled source of RulebookUnlocker v1.0.4

plugins/RulebookUnlocker.dll

Decompiled 4 months ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using HG.Reflection;
using Microsoft.CodeAnalysis;
using RoR2;
using RoR2.UI;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("Rulebook Unlocker")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Rulebook Unlocker")]
[assembly: AssemblyCopyright("")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: AssemblyFileVersion("1.0.4")]
[assembly: OptIn]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.4.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
[module: UnverifiableCode]
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 RuleBookEditor
{
	internal static class Config
	{
		internal static ConfigEntry<bool> unlockItemCatalog;

		internal static ConfigEntry<bool> unlockEquipmentCatalog;

		internal static ConfigEntry<bool> unlockMiscMoneyRule;

		internal static ConfigEntry<bool> unlockMiscStageOrder;

		internal static ConfigEntry<bool> unlockMiscKeepMoneyBetweenStages;

		internal static ConfigEntry<bool> unlockMiscAllowDropin;

		internal static void Initialize()
		{
			unlockItemCatalog = ((BaseUnityPlugin)RuleBookUnlocker.instance).Config.Bind<bool>("RuleBookUnlocker :: Pre-Run Item Catalog", "Enabled", true, "Forces the game to show up the item catalog while in a Lobby for enabling / disabling items.");
			unlockEquipmentCatalog = ((BaseUnityPlugin)RuleBookUnlocker.instance).Config.Bind<bool>("RuleBookUnlocker :: Pre-Run Equipment Catalog", "Enabled", true, "Forces the game to show up the equipment catalog while in a Lobby for enabling / disabling equipment.");
			unlockMiscMoneyRule = ((BaseUnityPlugin)RuleBookUnlocker.instance).Config.Bind<bool>("RuleBookUnlocker :: Pre-Run Misc Money Rules", "Enabled", true, "Forces the game to show up the misc rule of starting money.");
			unlockMiscStageOrder = ((BaseUnityPlugin)RuleBookUnlocker.instance).Config.Bind<bool>("RuleBookUnlocker :: Pre-Run Misc Stage Order Rules", "Enabled", true, "Forces the game to show up the misc rule of the run's stage order.");
			unlockMiscKeepMoneyBetweenStages = ((BaseUnityPlugin)RuleBookUnlocker.instance).Config.Bind<bool>("RuleBookUnlocker :: Pre-Run Misc Money between stages rule", "Enabled", true, "Forces the game to show up the misc rule of keeping money between stages.");
			unlockMiscAllowDropin = ((BaseUnityPlugin)RuleBookUnlocker.instance).Config.Bind<bool>("RuleBookUnlocker :: Pre-Run Misc Allow Dropin", "Enabled", true, "Forces the game to show up the misc rule of allowing new players to join the game. If enabled, will only show up if current run does not allow new participants.");
		}
	}
	internal class Rulebook
	{
		private static bool ConfigItemCatalog()
		{
			return !Config.unlockItemCatalog.Value;
		}

		private static bool ConfigEquipmentCatalog()
		{
			return !Config.unlockEquipmentCatalog.Value;
		}

		private static bool ReturnFalse()
		{
			return false;
		}

		private static bool ReturnTrue()
		{
			return true;
		}

		[SystemInitializer(new Type[] { typeof(RuleCatalog) })]
		public static void Initialize()
		{
			PreGameController.onPreGameControllerSetRuleBookGlobal += RuleBookGlobal;
			PreGameController.onPreGameControllerSetRuleBookServerGlobal += ServerRuleBook;
			PreGameController.onServerRecalculatedModifierAvailability += onServerRecalculatedAvailabiltiy;
			PreGameRuleVoteController.onVotesUpdated += OnVotesUpdated;
		}

		private static bool allowRuleChanging(PreGameController preGameController = null)
		{
			//IL_0024: Unknown result type (might be due to invalid IL or missing references)
			//IL_002a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0036: Unknown result type (might be due to invalid IL or missing references)
			//IL_003c: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)preGameController == (Object)null)
			{
				if ((Object)(object)PreGameController.instance == (Object)null)
				{
					return false;
				}
				preGameController = PreGameController.instance;
			}
			if (GameModeCatalog.FindGameModeIndex("ClassicRun") != preGameController.gameModeIndex)
			{
				return GameModeCatalog.FindGameModeIndex("InfiniteTowerRun") == preGameController.gameModeIndex;
			}
			return true;
		}

		private static void onServerRecalculatedAvailabiltiy(PreGameController obj)
		{
			FixRules();
		}

		private static void OnVotesUpdated()
		{
			if (allowRuleChanging())
			{
				FixRules();
			}
		}

		private static void ServerRuleBook(PreGameController arg1, RuleBook arg2)
		{
			if (allowRuleChanging(arg1))
			{
				arg1.networkRuleBookComponent.onRuleBookUpdated += NetworkRuleBookComponent_onRuleBookUpdated;
				ExposeRulebookCategoryDefs(arg1.networkRuleBookComponent.ruleBook);
				FixRules();
			}
			else
			{
				UndoRulebookCategoryDefExposure(arg1.networkRuleBookComponent.ruleBook);
			}
		}

		private static void NetworkRuleBookComponent_onRuleBookUpdated(NetworkRuleBook obj)
		{
			FixRules();
		}

		private static void RuleBookGlobal(PreGameController arg1, RuleBook arg2)
		{
			if (allowRuleChanging(arg1))
			{
				ExposeRulebookCategoryDefs(arg1.networkRuleBookComponent.ruleBook);
				FixRules();
			}
			else
			{
				UndoRulebookCategoryDefExposure(arg1.networkRuleBookComponent.ruleBook);
			}
		}

		private static void ExposeRulebookCategoryDefs(RuleBook rulebook)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_0009: Unknown result type (might be due to invalid IL or missing references)
			//IL_000e: Unknown result type (might be due to invalid IL or missing references)
			ChoicesEnumerable choices = rulebook.choices;
			Enumerator enumerator = ((ChoicesEnumerable)(ref choices)).GetEnumerator();
			try
			{
				while (((Enumerator)(ref enumerator)).MoveNext())
				{
					RuleCategoryDef category = ((Enumerator)(ref enumerator)).Current.ruleDef.category;
					if (category.displayToken == "RULE_HEADER_ITEMS")
					{
						category.hiddenTest = ConfigItemCatalog;
					}
					if (category.displayToken == "RULE_HEADER_EQUIPMENT")
					{
						category.hiddenTest = ConfigEquipmentCatalog;
					}
					if (!(category.displayToken == "RULE_HEADER_MISC"))
					{
						continue;
					}
					foreach (RuleDef child in category.children)
					{
						switch (child.displayToken)
						{
						case "RULE_MISC_STARTING_MONEY":
							if (!Config.unlockMiscMoneyRule.Value)
							{
								break;
							}
							foreach (RuleChoiceDef choice in child.choices)
							{
								choice.spritePath = "Textures/MiscIcons/texRuleBonusStartingMoney";
								choice.excludeByDefault = false;
							}
							break;
						case "RULE_MISC_STAGE_ORDER":
							if (!Config.unlockMiscStageOrder.Value)
							{
								break;
							}
							foreach (RuleChoiceDef choice2 in child.choices)
							{
								choice2.spritePath = "Textures/MiscIcons/texRuleMapIsRandom";
								choice2.excludeByDefault = false;
							}
							break;
						case "RULE_MISC_KEEP_MONEY_BETWEEN_STAGES":
							if (!Config.unlockMiscKeepMoneyBetweenStages.Value)
							{
								break;
							}
							foreach (RuleChoiceDef choice3 in child.choices)
							{
								choice3.excludeByDefault = false;
							}
							break;
						case "RULE_MISC_ALLOW_DROP_IN":
							if (!Config.unlockMiscAllowDropin.Value)
							{
								break;
							}
							foreach (RuleChoiceDef choice4 in child.choices)
							{
								choice4.excludeByDefault = false;
							}
							break;
						}
					}
				}
			}
			finally
			{
				((IDisposable)(Enumerator)(ref enumerator)).Dispose();
			}
		}

		private static void UndoRulebookCategoryDefExposure(RuleBook rulebook)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_0009: Unknown result type (might be due to invalid IL or missing references)
			//IL_000e: Unknown result type (might be due to invalid IL or missing references)
			ChoicesEnumerable choices = rulebook.choices;
			Enumerator enumerator = ((ChoicesEnumerable)(ref choices)).GetEnumerator();
			try
			{
				while (((Enumerator)(ref enumerator)).MoveNext())
				{
					RuleCategoryDef category = ((Enumerator)(ref enumerator)).Current.ruleDef.category;
					if (category.displayToken == "RULE_HEADER_ITEMS")
					{
						category.hiddenTest = RuleCatalog.HiddenTestItemsConvar;
					}
					if (category.displayToken == "RULE_HEADER_EQUIPMENT")
					{
						category.hiddenTest = RuleCatalog.HiddenTestItemsConvar;
					}
					if (!(category.displayToken == "RULE_HEADER_MISC"))
					{
						continue;
					}
					foreach (RuleDef child in category.children)
					{
						switch (child.displayToken)
						{
						case "RULE_MISC_STARTING_MONEY":
							if (!Config.unlockMiscMoneyRule.Value)
							{
								break;
							}
							foreach (RuleChoiceDef choice in child.choices)
							{
								choice.excludeByDefault = true;
							}
							break;
						case "RULE_MISC_STAGE_ORDER":
							if (!Config.unlockMiscStageOrder.Value)
							{
								break;
							}
							foreach (RuleChoiceDef choice2 in child.choices)
							{
								choice2.excludeByDefault = true;
							}
							break;
						case "RULE_MISC_KEEP_MONEY_BETWEEN_STAGES":
							if (!Config.unlockMiscKeepMoneyBetweenStages.Value)
							{
								break;
							}
							foreach (RuleChoiceDef choice3 in child.choices)
							{
								choice3.excludeByDefault = true;
							}
							break;
						case "RULE_MISC_ALLOW_DROP_IN":
							if (!Config.unlockMiscAllowDropin.Value)
							{
								break;
							}
							foreach (RuleChoiceDef choice4 in child.choices)
							{
								choice4.excludeByDefault = true;
							}
							break;
						}
					}
				}
			}
			finally
			{
				((IDisposable)(Enumerator)(ref enumerator)).Dispose();
			}
		}

		private static void FixRules()
		{
			foreach (RuleChoiceController instances in RuleChoiceController.instancesList)
			{
				if (instances.choiceDef.ruleDef.choices.Count > 1)
				{
					instances.canVote = true;
				}
			}
		}
	}
	[BepInPlugin("com.Anreol.RuleBookUnlocker", "RuleBookUnlocker", "1.0.4")]
	public class RuleBookUnlocker : BaseUnityPlugin
	{
		internal const string ModVer = "1.0.4";

		internal const string ModIdentifier = "RuleBookUnlocker";

		internal const string ModGuid = "com.Anreol.RuleBookUnlocker";

		public static RuleBookUnlocker instance;

		public void Awake()
		{
			instance = this;
			Config.Initialize();
		}
	}
}