Decompiled source of AlwaysJetpack v1.1.3

Mods/AlwaysJetpack.dll

Decompiled 2 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using AlwaysJetpack;
using HarmonyLib;
using Il2CppGameGUI.Options;
using Il2CppInterop.Runtime.InteropTypes.Arrays;
using Il2CppKeepsake;
using Il2CppSystem;
using Il2CppSystem.Reflection;
using Il2CppTMPro;
using MelonLoader;
using MelonLoader.Preferences;
using Microsoft.CodeAnalysis;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: MelonInfo(typeof(Mod), "AlwaysJetpack", "1.1.3", "Isaac", null)]
[assembly: MelonGame(null, null)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("AlwaysJetpack")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("AlwaysJetpack")]
[assembly: AssemblyTitle("AlwaysJetpack")]
[assembly: AssemblyVersion("1.0.0.0")]
[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 AlwaysJetpack
{
	public class Mod : MelonMod
	{
		internal static Instance Log;

		private static MelonPreferences_Entry<bool> _enabled;

		private static readonly Dictionary<int, bool> _original = new Dictionary<int, bool>();

		private static bool _applied;

		private static MelonPreferences_Entry<float> _hold;

		private static float _vanillaHold = 0.5f;

		private float _next;

		private const float MinHold = 0.05f;

		private static float _saveAt = -1f;

		private static bool _holdCaptured;

		private static float _logAt;

		private static float _prevE;

		public override void OnInitializeMelon()
		{
			//IL_0082: Unknown result type (might be due to invalid IL or missing references)
			//IL_009d: Unknown result type (might be due to invalid IL or missing references)
			//IL_00aa: Expected O, but got Unknown
			//IL_00aa: Expected O, but got Unknown
			//IL_0118: Unknown result type (might be due to invalid IL or missing references)
			//IL_0125: Expected O, but got Unknown
			Log = ((MelonBase)this).LoggerInstance;
			MelonPreferences_Category obj = MelonPreferences.CreateCategory("AlwaysJetpack");
			_enabled = obj.CreateEntry<bool>("AlwaysAllowJetpack", true, "Allow the jetpack in every area, including gravity zones (true = always)", (string)null, false, false, (ValueValidator)null, (string)null);
			_hold = obj.CreateEntry<float>("JetpackHoldSeconds", -1f, "Seconds you must hold sprint in the air to start the jetpack (-1 = game default)", (string)null, false, false, (ValueValidator)null, (string)null);
			try
			{
				MethodInfo method = typeof(JetpackActivationRequirementAbility).GetMethod("TickActive", BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public);
				((MelonBase)this).HarmonyInstance.Patch((MethodBase)method, new HarmonyMethod(typeof(Mod).GetMethod("TickPrefix", BindingFlags.Static | BindingFlags.NonPublic)), new HarmonyMethod(typeof(Mod).GetMethod("TickPostfix", BindingFlags.Static | BindingFlags.NonPublic)), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
				((MelonBase)this).LoggerInstance.Msg("Patched TickActive");
			}
			catch (Exception ex)
			{
				((MelonBase)this).LoggerInstance.Error("TickActive patch failed: " + ex);
			}
			try
			{
				MethodInfo method2 = typeof(GUI_SetMouseSettings).GetMethod("OnEnable", BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public);
				((MelonBase)this).HarmonyInstance.Patch((MethodBase)method2, (HarmonyMethod)null, new HarmonyMethod(typeof(Mod).GetMethod("GeneralTabPostfix", BindingFlags.Static | BindingFlags.NonPublic)), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
				((MelonBase)this).LoggerInstance.Msg("Patched General settings tab");
			}
			catch (Exception ex2)
			{
				((MelonBase)this).LoggerInstance.Error("Settings tab patch failed: " + ex2);
			}
			((MelonBase)this).LoggerInstance.Msg("AlwaysJetpack loaded. Enabled: " + _enabled.Value);
		}

		public override void OnUpdate()
		{
			if (_saveAt > 0f && Time.unscaledTime >= _saveAt)
			{
				_saveAt = -1f;
				MelonPreferences.Save();
			}
			if (Time.unscaledTime < _next)
			{
				return;
			}
			_next = Time.unscaledTime + 1f;
			try
			{
				Apply(_enabled.Value);
			}
			catch (Exception ex)
			{
				Log.Warning("Apply failed: " + ex.Message);
			}
			try
			{
				ApplyHold();
			}
			catch (Exception ex2)
			{
				Log.Warning("Hold apply failed: " + ex2.Message);
			}
		}

		private static void ApplyHold()
		{
			if (!_holdCaptured)
			{
				_vanillaHold = JetpackActivationRequirementAbility.TimeToEnter;
				_holdCaptured = true;
				Log.Msg("Jetpack TimeToEnter default = " + _vanillaHold + ", EnterJetpackPressInterval = " + JetpackActivationRequirementAbility.EnterJetpackPressInterval);
			}
		}

		private static void TickPrefix(JetpackActivationRequirementAbility __instance)
		{
			try
			{
				_prevE = __instance.m_Elapsed;
			}
			catch
			{
			}
		}

		private static void TickPostfix(JetpackActivationRequirementAbility __instance)
		{
			try
			{
				float elapsed = __instance.m_Elapsed;
				float num = elapsed - _prevE;
				if (_hold.Value >= 0f && num > 0f && num < 0.5f)
				{
					float num2 = Mathf.Max(0.05f, _hold.Value);
					float num3 = (__instance.m_Elapsed = _prevE + num * (_vanillaHold / num2));
					if (Time.unscaledTime >= _logAt)
					{
						_logAt = Time.unscaledTime + 0.25f;
						Log.Msg("[hold] elapsed " + _prevE.ToString("0.000") + " -> " + elapsed.ToString("0.000") + " scaled " + num3.ToString("0.000") + " (target " + num2 + ")");
					}
				}
				else if (elapsed > 0f && Time.unscaledTime >= _logAt)
				{
					_logAt = Time.unscaledTime + 0.5f;
					Log.Msg("[hold] elapsed " + _prevE.ToString("0.000") + " -> " + elapsed.ToString("0.000") + " (no change; hold=" + _hold.Value + ")");
				}
			}
			catch
			{
			}
		}

		private static void Apply(bool enable)
		{
			if (!enable && !_applied)
			{
				return;
			}
			int num = 0;
			foreach (GravityData item in Resources.FindObjectsOfTypeAll<GravityData>())
			{
				if ((Object)(object)item == (Object)null)
				{
					continue;
				}
				int instanceID = ((Object)item).GetInstanceID();
				bool value;
				if (enable)
				{
					if (!_original.ContainsKey(instanceID))
					{
						_original[instanceID] = item.m_AllowJetpackMode;
					}
					if (!item.m_AllowJetpackMode)
					{
						item.m_AllowJetpackMode = true;
						num++;
					}
				}
				else if (_original.TryGetValue(instanceID, out value) && item.m_AllowJetpackMode != value)
				{
					item.m_AllowJetpackMode = value;
					num++;
				}
			}
			if (!enable)
			{
				_applied = false;
			}
			else
			{
				_applied = true;
			}
			if (num > 0)
			{
				Log.Msg((enable ? "Jetpack allowed" : "Jetpack restored to game defaults") + " on " + num + " gravity profile(s)");
			}
		}

		private static void GeneralTabPostfix(GUI_SetMouseSettings __instance)
		{
			try
			{
				InjectToggle(__instance);
			}
			catch (Exception ex)
			{
				Instance log = Log;
				if (log != null)
				{
					log.Error("Failed to add jetpack toggle: " + ex);
				}
			}
			try
			{
				InjectSlider(__instance);
			}
			catch (Exception ex2)
			{
				Instance log2 = Log;
				if (log2 != null)
				{
					log2.Error("Failed to add jetpack hold slider: " + ex2);
				}
			}
		}

		private static void Dump(Transform t, string indent)
		{
			List<string> list = new List<string>();
			foreach (Component component in ((Component)t).GetComponents<Component>())
			{
				if ((Object)(object)component != (Object)null)
				{
					list.Add(((MemberInfo)((Object)component).GetIl2CppType()).Name);
				}
			}
			Instance log = Log;
			if (log != null)
			{
				log.Msg(indent + ((Object)t).name + " [" + string.Join(",", list) + "]");
			}
			for (int i = 0; i < t.childCount; i++)
			{
				Dump(t.GetChild(i), indent + "  ");
			}
		}

		private static void InjectSlider(GUI_SetMouseSettings page)
		{
			Transform val = ((Component)page).transform.Find("ScrollView/VBox");
			if ((Object)(object)val == (Object)null || (Object)(object)val.Find("AlwaysJetpack_HoldSlider") != (Object)null)
			{
				return;
			}
			HSSlider cameraShakeSlider = page.m_CameraShakeSlider;
			if ((Object)(object)cameraShakeSlider == (Object)null)
			{
				Instance log = Log;
				if (log != null)
				{
					log.Warning("No template slider found");
				}
				return;
			}
			Transform val2 = ((Component)cameraShakeSlider).transform;
			while ((Object)(object)val2.parent != (Object)null && (Object)(object)val2.parent != (Object)(object)val)
			{
				val2 = val2.parent;
			}
			if ((Object)(object)val2.parent != (Object)(object)val)
			{
				Instance log2 = Log;
				if (log2 != null)
				{
					log2.Warning("Slider row not found under VBox");
				}
				Dump(((Component)cameraShakeSlider).transform.root, "");
				return;
			}
			GameObject val3 = Object.Instantiate<GameObject>(((Component)val2).gameObject, val);
			((Object)val3).name = "AlwaysJetpack_HoldSlider";
			val3.SetActive(true);
			foreach (Component componentsInChild in val3.GetComponentsInChildren<Component>(true))
			{
				if (!((Object)(object)componentsInChild == (Object)null))
				{
					switch (((MemberInfo)((Object)componentsInChild).GetIl2CppType()).Name)
					{
					case "LocalizeStringEvent":
					case "GUI_OptionResetter":
					case "GUI_OptionSetCameraShakeIntensity":
					case "GUI_SliderValue":
						Object.Destroy((Object)(object)componentsInChild);
						break;
					}
				}
			}
			Slider componentInChildren = val3.GetComponentInChildren<Slider>(true);
			Il2CppArrayBase<TextMeshProUGUI> componentsInChildren = val3.GetComponentsInChildren<TextMeshProUGUI>(true);
			TextMeshProUGUI val4 = null;
			TextMeshProUGUI value = null;
			foreach (TextMeshProUGUI item in componentsInChildren)
			{
				if ((Object)(object)val4 == (Object)null && (((Object)item).name.IndexOf("Label", StringComparison.OrdinalIgnoreCase) >= 0 || ((Object)item).name.IndexOf("Title", StringComparison.OrdinalIgnoreCase) >= 0))
				{
					val4 = item;
				}
				else if ((Object)(object)value == (Object)null && ((Object)item).name.IndexOf("Value", StringComparison.OrdinalIgnoreCase) >= 0)
				{
					value = item;
				}
			}
			if ((Object)(object)val4 == (Object)null && componentsInChildren.Length > 0)
			{
				val4 = componentsInChildren[0];
			}
			if ((Object)(object)value == (Object)null)
			{
				foreach (TextMeshProUGUI item2 in componentsInChildren)
				{
					if ((Object)(object)item2 != (Object)(object)val4)
					{
						value = item2;
						break;
					}
				}
			}
			if ((Object)(object)val4 != (Object)null)
			{
				((TMP_Text)val4).text = "Jetpack Hold Time (seconds)";
			}
			if ((Object)(object)componentInChildren == (Object)null)
			{
				Instance log3 = Log;
				if (log3 != null)
				{
					log3.Warning("Cloned row has no slider");
				}
				Dump(val3.transform, "");
				return;
			}
			float num = ((_hold.Value >= 0f) ? Mathf.Max(0.05f, _hold.Value) : _vanillaHold);
			((UnityEventBase)componentInChildren.onValueChanged).m_PersistentCalls.Clear();
			((UnityEventBase)componentInChildren.onValueChanged).RemoveAllListeners();
			componentInChildren.wholeNumbers = false;
			componentInChildren.minValue = 0.05f;
			componentInChildren.maxValue = Mathf.Max(2f, _vanillaHold * 3f);
			componentInChildren.SetValueWithoutNotify(Mathf.Clamp(num, componentInChildren.minValue, componentInChildren.maxValue));
			Action<float> show = delegate(float v)
			{
				if ((Object)(object)value != (Object)null)
				{
					((TMP_Text)value).text = v.ToString("0.00") + " s";
				}
			};
			show(componentInChildren.value);
			((UnityEvent<float>)(object)componentInChildren.onValueChanged).AddListener(UnityAction<float>.op_Implicit((Action<float>)delegate(float v)
			{
				v = Mathf.Round(v * 20f) / 20f;
				_hold.Value = Mathf.Max(0.05f, v);
				_saveAt = Time.unscaledTime + 0.7f;
				show(v);
			}));
			val3.transform.SetSiblingIndex(val2.GetSiblingIndex() + 1);
			Instance log4 = Log;
			if (log4 != null)
			{
				log4.Msg("Added 'Jetpack Hold Time' slider to General settings (default " + _vanillaHold + " s)");
			}
		}

		private static void InjectToggle(GUI_SetMouseSettings page)
		{
			Transform val = ((Component)page).transform.Find("ScrollView/VBox");
			if ((Object)(object)val == (Object)null)
			{
				Instance log = Log;
				if (log != null)
				{
					log.Warning("General tab VBox not found");
				}
			}
			else
			{
				if ((Object)(object)val.Find("AlwaysJetpack_Toggle") != (Object)null)
				{
					return;
				}
				Transform val2 = val.Find("Head Bobbing") ?? val.Find("Toggle ADS");
				if ((Object)(object)val2 == (Object)null)
				{
					Instance log2 = Log;
					if (log2 != null)
					{
						log2.Warning("No template toggle found");
					}
					return;
				}
				GameObject val3 = Object.Instantiate<GameObject>(((Component)val2).gameObject, val);
				((Object)val3).name = "AlwaysJetpack_Toggle";
				val3.SetActive(true);
				foreach (Component component3 in val3.GetComponents<Component>())
				{
					if (!((Object)(object)component3 == (Object)null))
					{
						string name = ((MemberInfo)((Object)component3).GetIl2CppType()).Name;
						if (name == "LocalizeStringEvent" || name == "GUI_OptionResetter")
						{
							Object.Destroy((Object)(object)component3);
						}
					}
				}
				Transform val4 = val3.transform.Find("Label");
				if ((Object)(object)val4 != (Object)null)
				{
					TextMeshProUGUI component = ((Component)val4).GetComponent<TextMeshProUGUI>();
					if ((Object)(object)component != (Object)null)
					{
						((TMP_Text)component).text = "Always Allow Jetpack";
					}
				}
				Toggle component2 = val3.GetComponent<Toggle>();
				((UnityEventBase)component2.onValueChanged).m_PersistentCalls.Clear();
				((UnityEventBase)component2.onValueChanged).RemoveAllListeners();
				component2.SetIsOnWithoutNotify(_enabled.Value);
				((UnityEvent<bool>)(object)component2.onValueChanged).AddListener(UnityAction<bool>.op_Implicit((Action<bool>)OnToggled));
				val3.transform.SetSiblingIndex(val2.GetSiblingIndex() + 1);
				Instance log3 = Log;
				if (log3 != null)
				{
					log3.Msg("Added 'Always Allow Jetpack' toggle to General settings");
				}
			}
		}

		private static void OnToggled(bool on)
		{
			_enabled.Value = on;
			MelonPreferences.Save();
			Apply(on);
			Instance log = Log;
			if (log != null)
			{
				log.Msg("Always Allow Jetpack " + (on ? "on" : "off") + " via settings menu");
			}
		}
	}
}