Decompiled source of TNH Customizer v1.0.0

Nicole.TNHCustomizer.dll

Decompiled 11 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using FistVR;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyCompany("Nicole")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyDescription("New encryptions too hard but simple encryptions too boring? Don't like the Instutution constructs? This mod is for you.")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("Nicole.TNHCustomizer")]
[assembly: AssemblyTitle("TNH Customizer")]
[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 BepInEx
{
	[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
	[Conditional("CodeGeneration")]
	internal sealed class BepInAutoPluginAttribute : Attribute
	{
		public BepInAutoPluginAttribute(string id = null, string name = null, string version = null)
		{
		}
	}
}
namespace BepInEx.Preloader.Core.Patching
{
	[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
	[Conditional("CodeGeneration")]
	internal sealed class PatcherAutoPluginAttribute : Attribute
	{
		public PatcherAutoPluginAttribute(string id = null, string name = null, string version = null)
		{
		}
	}
}
namespace Nicole.TnhCustomizer
{
	internal static class Patches
	{
		[HarmonyPatch(typeof(TNH_Manager), "LoadFromSettings")]
		[HarmonyPostfix]
		public static void TNHManager_LoadFromSettings(TNH_Manager __instance)
		{
			//IL_004f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0061: Unknown result type (might be due to invalid IL or missing references)
			//IL_0062: Unknown result type (might be due to invalid IL or missing references)
			//IL_005a: Unknown result type (might be due to invalid IL or missing references)
			//IL_005f: 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)
			int num = TnhCustomizerPlugin.ConfigDisableEncryptionTypes.Values.Count((ConfigEntry<bool> x) => !x.Value);
			bool flag = !TnhCustomizerPlugin.ConfigDisableEncryptionTypes[(TNH_EncryptionType)0].Value;
			int num2 = num;
			TNHSetting_TargetMode targetMode = ((num2 == 0) ? ((TNHSetting_TargetMode)2) : ((num2 != 1 || !flag) ? __instance.TargetMode : ((TNHSetting_TargetMode)1)));
			__instance.TargetMode = targetMode;
			TnhCustomizerPlugin.ResetState();
		}

		[HarmonyPatch(typeof(TNH_HoldPoint), "IdentifyEncryption")]
		[HarmonyPatch(typeof(TNH_HoldPoint), "SpawnTargetGroup")]
		[HarmonyTranspiler]
		public static IEnumerable<CodeInstruction> HoldPoint_ReplacePhaseEncryptionType(IEnumerable<CodeInstruction> instructions)
		{
			//IL_0043: Unknown result type (might be due to invalid IL or missing references)
			//IL_005d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0063: Expected O, but got Unknown
			//IL_0071: Unknown result type (might be due to invalid IL or missing references)
			//IL_0077: Expected O, but got Unknown
			//IL_0085: Unknown result type (might be due to invalid IL or missing references)
			//IL_008b: Expected O, but got Unknown
			//IL_00b4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ba: Expected O, but got Unknown
			FieldInfo fieldInfo = AccessTools.Field(typeof(TNH_HoldPoint), "m_curPhase");
			FieldInfo fieldInfo2 = AccessTools.Field(typeof(Phase), "Encryption");
			MethodInfo methodInfo = AccessTools.Method(typeof(TnhCustomizerPlugin), "GetEncryptionTypeForPhase", (Type[])null, (Type[])null);
			return new CodeMatcher(instructions, (ILGenerator)null).MatchForward(false, (CodeMatch[])(object)new CodeMatch[3]
			{
				new CodeMatch((OpCode?)OpCodes.Ldarg_0, (object)null, (string)null),
				new CodeMatch((OpCode?)OpCodes.Ldfld, (object)fieldInfo, (string)null),
				new CodeMatch((OpCode?)OpCodes.Ldfld, (object)fieldInfo2, (string)null)
			}).ThrowIfInvalid("HoldPoint_ReplacePhaseEncryptionType IL match failed :c").Advance(1)
				.RemoveInstructions(2)
				.Insert((CodeInstruction[])(object)new CodeInstruction[1]
				{
					new CodeInstruction(OpCodes.Call, (object)methodInfo)
				})
				.InstructionEnumeration();
		}

		[HarmonyPatch(typeof(TNH_HoldPoint), "SpawnWarpInMarkers")]
		[HarmonyTranspiler]
		public static IEnumerable<CodeInstruction> TNHHoldPoint_SpawnWarpInMarkers(IEnumerable<CodeInstruction> instructions)
		{
			//IL_009e: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00be: Expected O, but got Unknown
			//IL_00cc: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d2: Expected O, but got Unknown
			//IL_00e0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e6: Expected O, but got Unknown
			//IL_0106: Unknown result type (might be due to invalid IL or missing references)
			//IL_010c: Expected O, but got Unknown
			//IL_0126: Unknown result type (might be due to invalid IL or missing references)
			//IL_012c: Expected O, but got Unknown
			//IL_013a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0140: Expected O, but got Unknown
			//IL_014e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0154: Expected O, but got Unknown
			//IL_0162: Unknown result type (might be due to invalid IL or missing references)
			//IL_0168: Expected O, but got Unknown
			//IL_0176: Unknown result type (might be due to invalid IL or missing references)
			//IL_017c: Expected O, but got Unknown
			//IL_018a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0190: Expected O, but got Unknown
			//IL_019e: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a4: Expected O, but got Unknown
			//IL_01cd: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d3: Expected O, but got Unknown
			//IL_01dc: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e2: Expected O, but got Unknown
			//IL_01ea: Unknown result type (might be due to invalid IL or missing references)
			//IL_01f0: Expected O, but got Unknown
			//IL_01f9: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ff: Expected O, but got Unknown
			FieldInfo fieldInfo = AccessTools.Field(typeof(TNH_HoldPoint), "m_curPhase");
			FieldInfo fieldInfo2 = AccessTools.Field(typeof(Phase), "Encryption");
			FieldInfo fieldInfo3 = AccessTools.Field(typeof(Phase), "MinTargets");
			FieldInfo fieldInfo4 = AccessTools.Field(typeof(Phase), "MaxTargets");
			MethodInfo methodInfo = AccessTools.Method(typeof(TnhCustomizerPlugin), "GetMinTargetsForPhase", (Type[])null, (Type[])null);
			MethodInfo methodInfo2 = AccessTools.Method(typeof(TnhCustomizerPlugin), "GetMaxTargetsForPhase", (Type[])null, (Type[])null);
			MethodInfo methodInfo3 = AccessTools.Method(typeof(TnhCustomizerPlugin), "GetEncryptionTypeForPhase", (Type[])null, (Type[])null);
			return new CodeMatcher(instructions, (ILGenerator)null).MatchForward(false, (CodeMatch[])(object)new CodeMatch[3]
			{
				new CodeMatch((OpCode?)OpCodes.Ldarg_0, (object)null, (string)null),
				new CodeMatch((OpCode?)OpCodes.Ldfld, (object)fieldInfo, (string)null),
				new CodeMatch((OpCode?)OpCodes.Ldfld, (object)fieldInfo2, (string)null)
			}).Advance(1).RemoveInstructions(2)
				.Insert((CodeInstruction[])(object)new CodeInstruction[1]
				{
					new CodeInstruction(OpCodes.Call, (object)methodInfo3)
				})
				.MatchForward(false, (CodeMatch[])(object)new CodeMatch[7]
				{
					new CodeMatch((OpCode?)OpCodes.Ldarg_0, (object)null, (string)null),
					new CodeMatch((OpCode?)OpCodes.Ldarg_0, (object)null, (string)null),
					new CodeMatch((OpCode?)OpCodes.Ldfld, (object)fieldInfo, (string)null),
					new CodeMatch((OpCode?)OpCodes.Ldfld, (object)fieldInfo3, (string)null),
					new CodeMatch((OpCode?)OpCodes.Ldarg_0, (object)null, (string)null),
					new CodeMatch((OpCode?)OpCodes.Ldfld, (object)fieldInfo, (string)null),
					new CodeMatch((OpCode?)OpCodes.Ldfld, (object)fieldInfo4, (string)null)
				})
				.ThrowIfInvalid("TNHHoldPoint_SpawnWarpInMarkers IL match failed :c")
				.Advance(1)
				.RemoveInstructions(6)
				.Insert((CodeInstruction[])(object)new CodeInstruction[4]
				{
					new CodeInstruction(OpCodes.Ldarg_0, (object)null),
					new CodeInstruction(OpCodes.Call, (object)methodInfo),
					new CodeInstruction(OpCodes.Ldarg_0, (object)null),
					new CodeInstruction(OpCodes.Call, (object)methodInfo2)
				})
				.InstructionEnumeration();
		}

		[HarmonyPatch(typeof(Construct_Blister_Volume), "SpawnConstructs")]
		[HarmonyPrefix]
		public static bool Prefix_ConstructBlisterVolume()
		{
			return !TnhCustomizerPlugin.ConfigDisableConstructBlister.Value;
		}

		[HarmonyPatch(typeof(Construct_Floater_Volume), "SpawnConstructs")]
		[HarmonyPrefix]
		public static bool Prefix_ConstructFloaterVolume()
		{
			return !TnhCustomizerPlugin.ConfigDisableConstructFloater.Value;
		}

		[HarmonyPatch(typeof(Construct_Iris_Volume), "SpawnConstructs")]
		[HarmonyPrefix]
		public static bool Prefix_ConstructIrisVolume()
		{
			return !TnhCustomizerPlugin.ConfigDisableConstructIris.Value;
		}

		[HarmonyPatch(typeof(Construct_Node_Volume), "SpawnConstructs")]
		[HarmonyPrefix]
		public static bool Prefix_ConstructNodeVolume()
		{
			return !TnhCustomizerPlugin.ConfigDisableConstructNode.Value;
		}

		[HarmonyPatch(typeof(Construct_Sentinel_Path), "SpawnConstructs")]
		[HarmonyPrefix]
		public static bool Prefix_ConstructSentinelPath()
		{
			return !TnhCustomizerPlugin.ConfigDisableConstructSentinel.Value;
		}
	}
	[BepInProcess("h3vr.exe")]
	[BepInPlugin("Nicole.TNHCustomizer", "TNH Customizer", "1.0.0")]
	public class TnhCustomizerPlugin : BaseUnityPlugin
	{
		private class TnhPhaseOverride
		{
			public TNH_EncryptionType Type { get; }

			public int MinEncryptions { get; }

			public int MaxEncryptions { get; }

			public TnhPhaseOverride(TNH_EncryptionType type, int minEncryptions, int maxEncryptions)
			{
				//IL_0007: Unknown result type (might be due to invalid IL or missing references)
				//IL_0008: Unknown result type (might be due to invalid IL or missing references)
				Type = type;
				MinEncryptions = minEncryptions;
				MaxEncryptions = maxEncryptions;
			}
		}

		internal static readonly Dictionary<TNH_EncryptionType, ConfigEntry<bool>> ConfigDisableEncryptionTypes = new Dictionary<TNH_EncryptionType, ConfigEntry<bool>>();

		internal static ConfigEntry<bool> ConfigDisableConstructSentinel = null;

		internal static ConfigEntry<bool> ConfigDisableConstructBlister = null;

		internal static ConfigEntry<bool> ConfigDisableConstructFloater = null;

		internal static ConfigEntry<bool> ConfigDisableConstructIris = null;

		internal static ConfigEntry<bool> ConfigDisableConstructNode = null;

		private static readonly Dictionary<TNH_EncryptionType, int> _encryptionTierLookup = new Dictionary<TNH_EncryptionType, int>
		{
			[(TNH_EncryptionType)0] = 0,
			[(TNH_EncryptionType)1] = 1,
			[(TNH_EncryptionType)2] = 1,
			[(TNH_EncryptionType)3] = 1,
			[(TNH_EncryptionType)4] = 2,
			[(TNH_EncryptionType)5] = 2,
			[(TNH_EncryptionType)6] = 3,
			[(TNH_EncryptionType)7] = 2,
			[(TNH_EncryptionType)8] = 2,
			[(TNH_EncryptionType)9] = 3,
			[(TNH_EncryptionType)10] = 3
		};

		private static readonly Dictionary<int, List<TNH_EncryptionType>> _encryptionTierLookupInverse = new Dictionary<int, List<TNH_EncryptionType>>();

		private static readonly Dictionary<Phase, TnhPhaseOverride> _tnhPhases = new Dictionary<Phase, TnhPhaseOverride>();

		public const string Id = "Nicole.TNHCustomizer";

		internal static ManualLogSource Logger { get; private set; }

		public static string Name => "TNH Customizer";

		public static string Version => "1.0.0";

		private void Awake()
		{
			//IL_002b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0030: 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_008a: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b9: Unknown result type (might be due to invalid IL or missing references)
			Logger = ((BaseUnityPlugin)this).Logger;
			foreach (TNH_EncryptionType value in Enum.GetValues(typeof(TNH_EncryptionType)))
			{
				TNH_EncryptionType val = value;
				ConfigDisableEncryptionTypes[val] = ((BaseUnityPlugin)this).Config.Bind<bool>("Encryptions", "Disable" + ((object)(TNH_EncryptionType)(ref val)).ToString() + "Encryption", false, "Prevents the " + ((object)(TNH_EncryptionType)(ref val)).ToString() + " encryption from spawning during TNH.");
				int key = _encryptionTierLookup[val];
				if (!_encryptionTierLookupInverse.ContainsKey(key))
				{
					_encryptionTierLookupInverse[key] = new List<TNH_EncryptionType>();
				}
				_encryptionTierLookupInverse[key].Add(val);
			}
			ConfigDisableConstructSentinel = ((BaseUnityPlugin)this).Config.Bind<bool>("Institution Constructs", "DisableSentinel", false, "Prevents the Sentinel constructs from spawning on Institution.");
			ConfigDisableConstructBlister = ((BaseUnityPlugin)this).Config.Bind<bool>("Institution Constructs", "DisableBlister", false, "Prevents the Blister constructs from spawning on Institution.");
			ConfigDisableConstructFloater = ((BaseUnityPlugin)this).Config.Bind<bool>("Institution Constructs", "DisableFloater", false, "Prevents the Floater constructs from spawning on Institution.");
			ConfigDisableConstructIris = ((BaseUnityPlugin)this).Config.Bind<bool>("Institution Constructs", "DisableIris", false, "Prevents the Iris constructs from spawning on Institution.");
			ConfigDisableConstructNode = ((BaseUnityPlugin)this).Config.Bind<bool>("Institution Constructs", "DisableNode", false, "Prevents the Node constructs from spawning on Institution.");
			Harmony.CreateAndPatchAll(typeof(Patches), "Nicole.TNHCustomizer");
		}

		internal static void ResetState()
		{
			_tnhPhases.Clear();
		}

		internal static TNH_EncryptionType GetEncryptionTypeForPhase(TNH_HoldPoint holdPoint)
		{
			//IL_000d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0015: Unknown result type (might be due to invalid IL or missing references)
			//IL_0044: Unknown result type (might be due to invalid IL or missing references)
			Phase curPhase = holdPoint.m_curPhase;
			if ((int)holdPoint.M.TargetMode != 0)
			{
				return curPhase.Encryption;
			}
			if (!_tnhPhases.ContainsKey(curPhase))
			{
				_tnhPhases.Add(curPhase, GetHoldPhaseOverride(holdPoint));
			}
			return _tnhPhases[curPhase].Type;
		}

		internal static int GetMinTargetsForPhase(TNH_HoldPoint holdPoint)
		{
			//IL_000d: Unknown result type (might be due to invalid IL or missing references)
			Phase curPhase = holdPoint.m_curPhase;
			if ((int)holdPoint.M.TargetMode != 0)
			{
				return curPhase.MinTargets;
			}
			if (!_tnhPhases.ContainsKey(curPhase))
			{
				_tnhPhases.Add(curPhase, GetHoldPhaseOverride(holdPoint));
			}
			return _tnhPhases[curPhase].MinEncryptions;
		}

		internal static int GetMaxTargetsForPhase(TNH_HoldPoint holdPoint)
		{
			//IL_000d: Unknown result type (might be due to invalid IL or missing references)
			Phase curPhase = holdPoint.m_curPhase;
			if ((int)holdPoint.M.TargetMode != 0)
			{
				return curPhase.MaxTargets;
			}
			if (!_tnhPhases.ContainsKey(curPhase))
			{
				_tnhPhases.Add(curPhase, GetHoldPhaseOverride(holdPoint));
			}
			return _tnhPhases[curPhase].MaxEncryptions;
		}

		private static TnhPhaseOverride GetHoldPhaseOverride(TNH_HoldPoint holdPoint)
		{
			//IL_000d: 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)
			//IL_001f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0051: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a8: Unknown result type (might be due to invalid IL or missing references)
			//IL_0123: Unknown result type (might be due to invalid IL or missing references)
			Phase curPhase = holdPoint.m_curPhase;
			if (!ConfigDisableEncryptionTypes[curPhase.Encryption].Value)
			{
				return new TnhPhaseOverride(curPhase.Encryption, curPhase.MinTargets, curPhase.MaxTargets);
			}
			int num = _encryptionTierLookup[curPhase.Encryption];
			if (TryGetEncryptionFromTier(num, out var type))
			{
				return new TnhPhaseOverride(type, curPhase.MinTargets, curPhase.MaxTargets);
			}
			int maxTargsInHold = holdPoint.GetMaxTargsInHold();
			int[] array = new int[4] { 0, 2, 4, 7 };
			_ = new int[4] { 0, -2, -4, -7 };
			for (int num2 = num - 1; num2 >= 0; num2--)
			{
				int num3 = num - num2;
				if (TryGetEncryptionFromTier(num2, out type))
				{
					return new TnhPhaseOverride(type, Mathf.Min(curPhase.MinTargets + array[num3], maxTargsInHold), Mathf.Min(curPhase.MaxTargets + array[num3], maxTargsInHold));
				}
			}
			for (int i = num + 1; i < 4; i++)
			{
				int num4 = i - num;
				if (TryGetEncryptionFromTier(i, out type))
				{
					int minEncryptions = Mathf.Max(curPhase.MinTargets - num4, 1);
					int maxEncryptions = Mathf.Max(curPhase.MaxTargets - num4, 1);
					if (i == 3)
					{
						minEncryptions = 1;
						maxEncryptions = 1;
					}
					return new TnhPhaseOverride(type, minEncryptions, maxEncryptions);
				}
			}
			Logger.LogError((object)"Somehow, no valid encryption types were found for the current settings.");
			return new TnhPhaseOverride((TNH_EncryptionType)0, 2, maxTargsInHold);
		}

		private static bool TryGetEncryptionFromTier(int tier, out TNH_EncryptionType type)
		{
			if (!_encryptionTierLookupInverse.ContainsKey(tier))
			{
				type = (TNH_EncryptionType)0;
				return false;
			}
			TNH_EncryptionType[] array = _encryptionTierLookupInverse[tier].Where((TNH_EncryptionType t) => !ConfigDisableEncryptionTypes[t].Value).ToArray();
			if (array.Length == 0)
			{
				type = (TNH_EncryptionType)0;
				return false;
			}
			type = array[Random.Range(0, array.Length + 1)];
			return true;
		}
	}
}