Decompiled source of XpTomeOnLevel1 v1.1.1

BepInEx/plugins/MegabonkXpTomeMod.dll

Decompiled a month ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using Assets.Scripts._Data;
using Assets.Scripts._Data.Tomes;
using BepInEx;
using BepInEx.Core.Logging.Interpolation;
using BepInEx.Logging;
using BepInEx.Unity.IL2CPP;
using HarmonyLib;
using Il2CppInterop.Runtime.InteropTypes;
using Il2CppSystem;
using Inventory__Items__Pickups.Xp_and_Levels;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETCoreApp,Version=v6.0", FrameworkDisplayName = ".NET 6.0")]
[assembly: AssemblyCompany("MegabonkXpTomeMod")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("MegabonkXpTomeMod")]
[assembly: AssemblyTitle("MegabonkXpTomeMod")]
[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 XpTomeOnLevel1
{
	[BepInPlugin("com.rangermikefps.megabonk.xptomeonlevel1", "XP Tome on Level 1", "1.1.0")]
	public class XpTomeOnLevel1Mod : BasePlugin
	{
		public const string PluginGuid = "com.rangermikefps.megabonk.xptomeonlevel1";

		public const string PluginName = "XP Tome on Level 1";

		public const string PluginVersion = "1.1.0";

		internal static ManualLogSource Log;

		internal static bool hasOfferedXpTome;

		internal static int currentPlayerLevel;

		internal static int upgradeButtonsSetThisLevelUp;

		public override void Load()
		{
			//IL_0015: Unknown result type (might be due to invalid IL or missing references)
			//IL_001b: Expected O, but got Unknown
			//IL_00bc: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c2: Expected O, but got Unknown
			Log = ((BasePlugin)this).Log;
			ManualLogSource log = Log;
			bool flag = default(bool);
			BepInExInfoLogInterpolatedStringHandler val = new BepInExInfoLogInterpolatedStringHandler(16, 2, ref flag);
			if (flag)
			{
				((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>("XP Tome on Level 1");
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral(" v");
				((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>("1.1.0");
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral(" initializing.");
			}
			log.LogInfo(val);
			Harmony.CreateAndPatchAll(typeof(XpTomeOnLevel1Mod), (string)null);
			Harmony.CreateAndPatchAll(typeof(UpgradeButton_SetUpgrade_Patch), (string)null);
			PlayerXp.A_LevelUp += Action<int>.op_Implicit((Action<int>)OnPlayerLevelUp);
			GameManager.A_RunStarted += Action.op_Implicit((Action)OnRunStarted);
			ManualLogSource log2 = Log;
			val = new BepInExInfoLogInterpolatedStringHandler(13, 1, ref flag);
			if (flag)
			{
				((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>("XP Tome on Level 1");
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral(" initialized.");
			}
			log2.LogInfo(val);
		}

		private void OnRunStarted()
		{
			Log.LogInfo((object)"New run started → resetting XP Tome flags and level tracking.");
			hasOfferedXpTome = false;
			currentPlayerLevel = 0;
			upgradeButtonsSetThisLevelUp = 0;
		}

		private void OnPlayerLevelUp(int newLevel)
		{
			//IL_0019: Unknown result type (might be due to invalid IL or missing references)
			//IL_001f: Expected O, but got Unknown
			try
			{
				currentPlayerLevel = newLevel;
				upgradeButtonsSetThisLevelUp = 0;
			}
			catch (Exception ex)
			{
				ManualLogSource log = Log;
				bool flag = default(bool);
				BepInExErrorLogInterpolatedStringHandler val = new BepInExErrorLogInterpolatedStringHandler(30, 1, ref flag);
				if (flag)
				{
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral("Exception in OnPlayerLevelUp: ");
					((BepInExLogInterpolatedStringHandler)val).AppendFormatted<Exception>(ex);
				}
				log.LogError(val);
			}
		}
	}
	[HarmonyPatch(typeof(UpgradeButton), "SetUpgrade")]
	public class UpgradeButton_SetUpgrade_Patch
	{
		[HarmonyPrefix]
		public static bool Prefix(UpgradeButton __instance, ref IUpgradable upgradable)
		{
			//IL_008b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0092: Expected O, but got Unknown
			try
			{
				XpTomeOnLevel1Mod.upgradeButtonsSetThisLevelUp++;
				if (XpTomeOnLevel1Mod.currentPlayerLevel == 1 && !XpTomeOnLevel1Mod.hasOfferedXpTome && XpTomeOnLevel1Mod.upgradeButtonsSetThisLevelUp == 1)
				{
					XpTomeOnLevel1Mod.Log.LogInfo((object)"First level-up detected! Replacing first upgrade option with XP Tome...");
					TomeData tome = DataManager.Instance.GetTome((ETome)14);
					if ((Object)(object)tome == (Object)null)
					{
						XpTomeOnLevel1Mod.Log.LogError((object)"Could not find XP Tome data!");
						return true;
					}
					upgradable = ((Il2CppObjectBase)tome).Cast<IUpgradable>();
					XpTomeOnLevel1Mod.Log.LogInfo((object)"Successfully replaced upgrade with XP Tome!");
					XpTomeOnLevel1Mod.hasOfferedXpTome = true;
				}
				return true;
			}
			catch (Exception ex)
			{
				ManualLogSource log = XpTomeOnLevel1Mod.Log;
				if (log != null)
				{
					bool flag = default(bool);
					BepInExErrorLogInterpolatedStringHandler val = new BepInExErrorLogInterpolatedStringHandler(34, 1, ref flag);
					if (flag)
					{
						((BepInExLogInterpolatedStringHandler)val).AppendLiteral("Exception in UpgradeButton patch: ");
						((BepInExLogInterpolatedStringHandler)val).AppendFormatted<Exception>(ex);
					}
					log.LogError(val);
				}
				return true;
			}
		}
	}
}