Decompiled source of HallowedConceptsFixes v1.1.0

plugins/HallowedConceptsFixes/HallowedConceptsFixes.dll

Decompiled 2 weeks ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HallowedConceptsFixes.Patches;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using RoR2;
using RoR2.Karma;
using RoR2.Quests;
using RoR2.Quests.CompulsionQuests;
using UnityEngine;
using UnityEngine.Networking;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("HallowedConceptsFixes")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.1.0.0")]
[assembly: AssemblyInformationalVersion("1.1.0")]
[assembly: AssemblyProduct("HallowedConceptsFixes")]
[assembly: AssemblyTitle("HallowedConceptsFixes")]
[assembly: AssemblyVersion("1.1.0.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableAttribute : Attribute
	{
		public readonly byte[] NullableFlags;

		public NullableAttribute(byte P_0)
		{
			NullableFlags = new byte[1] { P_0 };
		}

		public NullableAttribute(byte[] P_0)
		{
			NullableFlags = P_0;
		}
	}
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableContextAttribute : Attribute
	{
		public readonly byte Flag;

		public NullableContextAttribute(byte P_0)
		{
			Flag = P_0;
		}
	}
	[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 HallowedConceptsFixes
{
	[BepInPlugin("com.jeremiah.hallowedconceptsfixes", "HallowedConceptsFixes", "1.1.0")]
	public sealed class HallowedConceptsFixesPlugin : BaseUnityPlugin
	{
		public const string PluginGuid = "com.jeremiah.hallowedconceptsfixes";

		public const string PluginName = "HallowedConceptsFixes";

		public const string PluginVersion = "1.1.0";

		private const string GeneralSection = "0. General";

		private const string HexesSection = "1. Hexes";

		private const string EclipseSection = "2. Eclipse & Karma";

		private const string WakeSection = "3. The Wake";

		private const string KarmaTriggersSection = "4. Karma Triggers";

		private const string CompulsionSection = "5. Shrines of Compulsion";

		private Harmony? _harmony;

		internal static ManualLogSource Log { get; private set; }

		internal static ConfigEntry<bool> EnableMod { get; private set; }

		internal static ConfigEntry<bool> RenameHexOfPatience { get; private set; }

		internal static ConfigEntry<string> HexOfPatienceNewName { get; private set; }

		internal static ConfigEntry<bool> EclipseStartKarmaAtZero { get; private set; }

		internal static ConfigEntry<bool> EclipseAllowToggleKarmaOff { get; private set; }

		internal static ConfigEntry<bool> ShortenWakeDuration { get; private set; }

		internal static ConfigEntry<float> WakeChargeDurationSeconds { get; private set; }

		internal static ConfigEntry<bool> DisableBeadsOfFealtyKarmaTrigger { get; private set; }

		internal static ConfigEntry<bool> DisableFastBossKillKarmaTrigger { get; private set; }

		internal static ConfigEntry<bool> DisableFiftyItemsKarmaTrigger { get; private set; }

		internal static ConfigEntry<bool> FixCommencementLoopKarma { get; private set; }

		internal static ConfigEntry<bool> RemoveCompulsionShrineOfOrder { get; private set; }

		internal static ConfigEntry<bool> RemoveCompulsionCaffeinatorSoda { get; private set; }

		private void Awake()
		{
			//IL_00fd: Unknown result type (might be due to invalid IL or missing references)
			//IL_0107: Expected O, but got Unknown
			//IL_01fd: Unknown result type (might be due to invalid IL or missing references)
			//IL_0207: Expected O, but got Unknown
			Log = ((BaseUnityPlugin)this).Logger;
			EnableMod = ((BaseUnityPlugin)this).Config.Bind<bool>("0. General", "EnableMod", true, "Master toggle to enable or disable all fixes in this mod.");
			RenameHexOfPatience = ((BaseUnityPlugin)this).Config.Bind<bool>("1. Hexes", "RenameHexOfPatience", true, "Entry #1 (#10): Rename 'Hex of Patience' (ITEM_OUTOFTPRADIUS_NAME) to 'Hex of Impatience'.");
			HexOfPatienceNewName = ((BaseUnityPlugin)this).Config.Bind<string>("1. Hexes", "HexOfPatienceNewName", "Hex of Impatience", "Replacement display name for ITEM_OUTOFTPRADIUS_NAME when RenameHexOfPatience is enabled.");
			EclipseStartKarmaAtZero = ((BaseUnityPlugin)this).Config.Bind<bool>("2. Eclipse & Karma", "EclipseStartKarmaAtZero", true, "Entry #2 (#71): Cap Eclipse starting Karma at 0 ('Karma.InitialLevel.0') instead of scaling initial Karma (1-8) with Eclipse level.");
			EclipseAllowToggleKarmaOff = ((BaseUnityPlugin)this).Config.Bind<bool>("2. Eclipse & Karma", "EclipseAllowToggleKarmaOff", true, "Entry #15 (#70): Allow toggling Karma off ('KarmaDisabled') or on in the Eclipse character-select lobby UI.");
			ShortenWakeDuration = ((BaseUnityPlugin)this).Config.Bind<bool>("3. The Wake", "ShortenWakeDuration", true, "Entry #3 (#23): Override The Wake holdout zone's baseChargeDuration (vanilla: 120s).");
			WakeChargeDurationSeconds = ((BaseUnityPlugin)this).Config.Bind<float>("3. The Wake", "WakeChargeDurationSeconds", 60f, new ConfigDescription("Target duration in seconds for The Wake event when ShortenWakeDuration is enabled (vanilla: 120).", (AcceptableValueBase)(object)new AcceptableValueRange<float>(5f, 300f), Array.Empty<object>()));
			DisableBeadsOfFealtyKarmaTrigger = ((BaseUnityPlugin)this).Config.Bind<bool>("4. Karma Triggers", "DisableBeadsOfFealtyKarmaTrigger", true, "Entry #4 (#75): Disable the 'PledgeLoyalty' (PledgeFealty) Karma trigger when picking up Beads of Fealty.");
			DisableFastBossKillKarmaTrigger = ((BaseUnityPlugin)this).Config.Bind<bool>("4. Karma Triggers", "DisableFastBossKillKarmaTrigger", true, "Entry #5 (#76): Disable the 'KillBossQuickly' (KillABossQuickly) Karma trigger for defeating a Teleporter boss within 10% teleporter charge.");
			DisableFiftyItemsKarmaTrigger = ((BaseUnityPlugin)this).Config.Bind<bool>("4. Karma Triggers", "DisableFiftyItemsKarmaTrigger", true, "Entry #6 (#73): Disable the 'CollectLotsOfItems' Karma trigger for holding 50+ permanent non-Hex items.");
			FixCommencementLoopKarma = ((BaseUnityPlugin)this).Config.Bind<bool>("4. Karma Triggers", "FixCommencementLoopKarma", true, "Entry #9 (#77): Pass nextScene instead of sceneDefForCurrentScene into Run.OnLoopBeginServer so advancing to Stage 6 Commencement ('moon2') does not trigger looping Karma.");
			RemoveCompulsionShrineOfOrder = ((BaseUnityPlugin)this).Config.Bind<bool>("5. Shrines of Compulsion", "RemoveCompulsionShrineOfOrder", true, "Entry #7 (#29): Exclude CompulsionQuestOrder (Shrine of Order) from the Shrine of Compulsion quest pool.");
			RemoveCompulsionCaffeinatorSoda = ((BaseUnityPlugin)this).Config.Bind<bool>("5. Shrines of Compulsion", "RemoveCompulsionCaffeinatorSoda", true, "Entry #8 (#30): Exclude CompulsionQuestDrinkSoda (Drink 20 Sodas at Caffeinator) from the Shrine of Compulsion quest pool.");
			LocalizationPatches.RebuildTokenOverrides();
			((BaseUnityPlugin)this).Config.SettingChanged += OnConfigSettingChanged;
			_harmony = new Harmony("com.jeremiah.hallowedconceptsfixes");
			_harmony.PatchAll(typeof(LocalizationPatches));
			_harmony.PatchAll(typeof(EclipseKarmaPatches));
			_harmony.PatchAll(typeof(WakeDurationPatches));
			_harmony.PatchAll(typeof(KarmaTriggerPatches));
			_harmony.PatchAll(typeof(StageLoopKarmaPatches));
			_harmony.PatchAll(typeof(CompulsionQuestPatches));
			((BaseUnityPlugin)this).Logger.LogInfo((object)"HallowedConceptsFixes v1.1.0 loaded (Entries 1-9 & 15 active).");
		}

		private void OnDestroy()
		{
			((BaseUnityPlugin)this).Config.SettingChanged -= OnConfigSettingChanged;
			Harmony? harmony = _harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
			LocalizationPatches.ClearTokenOverrides();
		}

		private static void OnConfigSettingChanged(object sender, SettingChangedEventArgs args)
		{
			LocalizationPatches.RebuildTokenOverrides();
			EclipseKarmaPatches.RefreshLobbyRulesIfActive();
			WakeDurationPatches.ApplyToActiveWakeIfPresent();
			KarmaTriggerPatches.SyncActiveRunTriggers();
		}
	}
}
namespace HallowedConceptsFixes.Patches
{
	internal static class CompulsionQuestPatches
	{
		private const string OrderQuestIdentifier = "CompulsionQuestOrder";

		private const string DrinkSodaQuestIdentifier = "CompulsionQuestDrinkSoda";

		[HarmonyPatch(typeof(CompulsionQuestOrder), "CanStart")]
		[HarmonyPrefix]
		private static bool CompulsionQuestOrderCanStartPrefix(ref bool __result)
		{
			if (HallowedConceptsFixesPlugin.EnableMod.Value && HallowedConceptsFixesPlugin.RemoveCompulsionShrineOfOrder.Value)
			{
				__result = false;
				return false;
			}
			return true;
		}

		[HarmonyPatch(typeof(CompulsionQuestDrinkSoda), "CanStart")]
		[HarmonyPrefix]
		private static bool CompulsionQuestDrinkSodaCanStartPrefix(ref bool __result)
		{
			if (HallowedConceptsFixesPlugin.EnableMod.Value && HallowedConceptsFixesPlugin.RemoveCompulsionCaffeinatorSoda.Value)
			{
				__result = false;
				return false;
			}
			return true;
		}

		[HarmonyPatch(typeof(CompulsionQuestTracker), "BeginQuest")]
		[HarmonyPrefix]
		private static void BeginQuestPrefix(ref RegisteredQuest? questOverride)
		{
			//IL_0016: Unknown result type (might be due to invalid IL or missing references)
			//IL_001b: Unknown result type (might be due to invalid IL or missing references)
			//IL_001c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0022: Unknown result type (might be due to invalid IL or missing references)
			if (questOverride.HasValue && HallowedConceptsFixesPlugin.EnableMod.Value)
			{
				RegisteredQuest value = questOverride.Value;
				if (IsQuestExcluded(value.questType, value.identifier))
				{
					questOverride = null;
				}
			}
		}

		private static bool IsQuestExcluded(Type? questType, string? identifier)
		{
			if (HallowedConceptsFixesPlugin.RemoveCompulsionShrineOfOrder.Value && (questType == typeof(CompulsionQuestOrder) || string.Equals(identifier, "CompulsionQuestOrder", StringComparison.Ordinal)))
			{
				return true;
			}
			if (HallowedConceptsFixesPlugin.RemoveCompulsionCaffeinatorSoda.Value && (questType == typeof(CompulsionQuestDrinkSoda) || string.Equals(identifier, "CompulsionQuestDrinkSoda", StringComparison.Ordinal)))
			{
				return true;
			}
			return false;
		}
	}
	internal static class EclipseKarmaPatches
	{
		private const string KarmaInitialLevelRuleName = "Karma.InitialLevel";

		private const string KarmaDisabledChoiceLocalName = "KarmaDisabled";

		private const string KarmaZeroChoiceLocalName = "0";

		[HarmonyPatch(typeof(EclipseRun), "OverrideRuleChoices")]
		[HarmonyPostfix]
		private static void OverrideRuleChoicesPostfix(RuleChoiceMask mustInclude, RuleChoiceMask mustExclude)
		{
			if (!HallowedConceptsFixesPlugin.EnableMod.Value)
			{
				return;
			}
			bool value = HallowedConceptsFixesPlugin.EclipseStartKarmaAtZero.Value;
			bool value2 = HallowedConceptsFixesPlugin.EclipseAllowToggleKarmaOff.Value;
			if (!value && !value2)
			{
				return;
			}
			RuleDef val = RuleCatalog.FindRuleDef("Karma.InitialLevel");
			if (val == null)
			{
				return;
			}
			RuleChoiceDef val2 = val.FindChoice("KarmaDisabled");
			RuleChoiceDef val3 = val.FindChoice("0");
			if (value2 && value)
			{
				foreach (RuleChoiceDef choice in val.choices)
				{
					((SerializableBitArray)mustInclude)[choice.globalIndex] = false;
					((SerializableBitArray)mustExclude)[choice.globalIndex] = true;
				}
				if (val2 != null)
				{
					((SerializableBitArray)mustInclude)[val2.globalIndex] = true;
					((SerializableBitArray)mustExclude)[val2.globalIndex] = false;
				}
				if (val3 != null)
				{
					((SerializableBitArray)mustInclude)[val3.globalIndex] = true;
					((SerializableBitArray)mustExclude)[val3.globalIndex] = false;
				}
				return;
			}
			if (value2)
			{
				foreach (RuleChoiceDef choice2 in val.choices)
				{
					((SerializableBitArray)mustInclude)[choice2.globalIndex] = false;
					((SerializableBitArray)mustExclude)[choice2.globalIndex] = false;
				}
				return;
			}
			if (!value || val3 == null)
			{
				return;
			}
			foreach (RuleChoiceDef choice3 in val.choices)
			{
				((SerializableBitArray)mustInclude)[choice3.globalIndex] = false;
				((SerializableBitArray)mustExclude)[choice3.globalIndex] = true;
			}
			((SerializableBitArray)mustInclude)[val3.globalIndex] = true;
			((SerializableBitArray)mustExclude)[val3.globalIndex] = false;
		}

		internal static void RefreshLobbyRulesIfActive()
		{
			if (NetworkServer.active && (Object)(object)PreGameController.instance != (Object)null)
			{
				PreGameController.instance.RecalculateModifierAvailability();
			}
		}
	}
	internal static class KarmaTriggerPatches
	{
		private const string PledgeLoyaltyIdentifier = "PledgeLoyalty";

		private const string KillBossQuicklyIdentifier = "KillBossQuickly";

		private const string CollectLotsOfItemsIdentifier = "CollectLotsOfItems";

		private static readonly FieldRef<KarmaManager, List<BaseKarmaTrigger>> RunTriggersRef = AccessTools.FieldRefAccess<KarmaManager, List<BaseKarmaTrigger>>("runTriggers");

		[HarmonyPatch(typeof(KarmaManager), "SelectValidTriggers")]
		[HarmonyPostfix]
		private static void SelectValidTriggersPostfix(KarmaManager __instance)
		{
			PruneDisabledTriggers(__instance);
		}

		[HarmonyPatch(typeof(KarmaManager), "OnKarmaTriggerFlipped")]
		[HarmonyPrefix]
		private static bool OnKarmaTriggerFlippedPrefix(BaseKarmaTrigger trigger)
		{
			if (trigger != null && IsTriggerDisabled(trigger))
			{
				trigger.Deactivate();
				return false;
			}
			return true;
		}

		internal static void SyncActiveRunTriggers()
		{
			KarmaManager instance = KarmaManager.instance;
			if (!((Object)(object)instance == (Object)null))
			{
				PruneDisabledTriggers(instance);
			}
		}

		private static void PruneDisabledTriggers(KarmaManager manager)
		{
			List<BaseKarmaTrigger> list = RunTriggersRef.Invoke(manager);
			if (list == null || list.Count == 0)
			{
				return;
			}
			for (int num = list.Count - 1; num >= 0; num--)
			{
				BaseKarmaTrigger val = list[num];
				if (val != null && IsTriggerDisabled(val))
				{
					val.Deactivate();
					list.RemoveAt(num);
				}
			}
		}

		private static bool IsTriggerDisabled(BaseKarmaTrigger trigger)
		{
			if (!HallowedConceptsFixesPlugin.EnableMod.Value)
			{
				return false;
			}
			string a = trigger.karmaTriggerDef?.identifier;
			if (HallowedConceptsFixesPlugin.DisableBeadsOfFealtyKarmaTrigger.Value && (trigger is PledgeFealty || string.Equals(a, "PledgeLoyalty", StringComparison.Ordinal)))
			{
				return true;
			}
			if (HallowedConceptsFixesPlugin.DisableFastBossKillKarmaTrigger.Value && (trigger is KillABossQuickly || string.Equals(a, "KillBossQuickly", StringComparison.Ordinal)))
			{
				return true;
			}
			if (HallowedConceptsFixesPlugin.DisableFiftyItemsKarmaTrigger.Value && (trigger is CollectLotsOfItems || string.Equals(a, "CollectLotsOfItems", StringComparison.Ordinal)))
			{
				return true;
			}
			return false;
		}
	}
	internal static class LocalizationPatches
	{
		public const string OutOfTpRadiusNameToken = "ITEM_OUTOFTPRADIUS_NAME";

		private static readonly Dictionary<string, string> ActiveTokenOverrides = new Dictionary<string, string>(StringComparer.Ordinal);

		internal static void RebuildTokenOverrides()
		{
			ActiveTokenOverrides.Clear();
			if (HallowedConceptsFixesPlugin.EnableMod.Value && HallowedConceptsFixesPlugin.RenameHexOfPatience.Value)
			{
				string value = HallowedConceptsFixesPlugin.HexOfPatienceNewName.Value;
				if (!string.IsNullOrWhiteSpace(value))
				{
					ActiveTokenOverrides["ITEM_OUTOFTPRADIUS_NAME"] = value;
				}
			}
		}

		internal static void ClearTokenOverrides()
		{
			ActiveTokenOverrides.Clear();
		}

		[HarmonyPatch(typeof(Language), "GetLocalizedStringByToken")]
		[HarmonyPrefix]
		private static bool GetLocalizedStringByTokenPrefix(string token, ref string __result)
		{
			if (!string.IsNullOrEmpty(token) && ActiveTokenOverrides.TryGetValue(token, out string value))
			{
				__result = value;
				return false;
			}
			return true;
		}
	}
	internal static class StageLoopKarmaPatches
	{
		[ThreadStatic]
		private static SceneDef? _advancingNextScene;

		[HarmonyPatch(typeof(Run), "AdvanceStage")]
		[HarmonyPrefix]
		private static void AdvanceStagePrefix(SceneDef nextScene)
		{
			_advancingNextScene = nextScene;
		}

		[HarmonyPatch(typeof(Run), "AdvanceStage")]
		[HarmonyFinalizer]
		private static void AdvanceStageFinalizer()
		{
			_advancingNextScene = null;
		}

		[HarmonyPatch(typeof(Run), "OnLoopBeginServer")]
		[HarmonyPrefix]
		private static void OnLoopBeginServerPrefix(ref SceneDef sceneDef)
		{
			if (HallowedConceptsFixesPlugin.EnableMod.Value && HallowedConceptsFixesPlugin.FixCommencementLoopKarma.Value && (Object)(object)_advancingNextScene != (Object)null)
			{
				sceneDef = _advancingNextScene;
			}
		}
	}
	internal static class WakeDurationPatches
	{
		private sealed class OriginalDurationBox
		{
			public float Value;
		}

		private static readonly ConditionalWeakTable<HoldoutZoneController, OriginalDurationBox> OriginalDurations = new ConditionalWeakTable<HoldoutZoneController, OriginalDurationBox>();

		[HarmonyPatch(typeof(WakeController), "Awake")]
		[HarmonyPostfix]
		private static void AwakePostfix(WakeController __instance)
		{
			ApplyDurationToWake(__instance);
		}

		internal static void ApplyToActiveWakeIfPresent()
		{
			if ((Object)(object)WakeController.instance != (Object)null)
			{
				ApplyDurationToWake(WakeController.instance);
			}
		}

		private static void ApplyDurationToWake(WakeController wakeController)
		{
			if ((Object)(object)wakeController == (Object)null)
			{
				return;
			}
			HoldoutZoneController component = ((Component)wakeController).GetComponent<HoldoutZoneController>();
			if (!((Object)(object)component == (Object)null))
			{
				if (!OriginalDurations.TryGetValue(component, out OriginalDurationBox value))
				{
					value = new OriginalDurationBox
					{
						Value = component.baseChargeDuration
					};
					OriginalDurations.Add(component, value);
				}
				if (HallowedConceptsFixesPlugin.EnableMod.Value && HallowedConceptsFixesPlugin.ShortenWakeDuration.Value)
				{
					component.baseChargeDuration = HallowedConceptsFixesPlugin.WakeChargeDurationSeconds.Value;
				}
				else
				{
					component.baseChargeDuration = value.Value;
				}
			}
		}
	}
}