Decompiled source of AlwaysJetpack v1.0.0

Mods/AlwaysJetpack.dll

Decompiled 12 hours 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 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.0.0", "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 float _next;

		public override void OnInitializeMelon()
		{
			//IL_0064: Unknown result type (might be due to invalid IL or missing references)
			//IL_0071: Expected O, but got Unknown
			Log = ((MelonBase)this).LoggerInstance;
			_enabled = MelonPreferences.CreateCategory("AlwaysJetpack").CreateEntry<bool>("AlwaysAllowJetpack", true, "Allow the jetpack in every area, including gravity zones (true = always)", (string)null, false, false, (ValueValidator)null, (string)null);
			try
			{
				MethodInfo method = typeof(GUI_SetMouseSettings).GetMethod("OnEnable", BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public);
				((MelonBase)this).HarmonyInstance.Patch((MethodBase)method, (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 ex)
			{
				((MelonBase)this).LoggerInstance.Error("Settings tab patch failed: " + ex);
			}
			((MelonBase)this).LoggerInstance.Msg("AlwaysJetpack loaded. Enabled: " + _enabled.Value);
		}

		public override void OnUpdate()
		{
			if (Time.unscaledTime < _next)
			{
				return;
			}
			_next = Time.unscaledTime + 1f;
			try
			{
				Apply(_enabled.Value);
			}
			catch (Exception ex)
			{
				Log.Warning("Apply failed: " + ex.Message);
			}
		}

		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);
				}
			}
		}

		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");
			}
		}
	}
}