Decompiled source of Swap Starting Weapon v0.0.2

SwapStartingWeapon.dll

Decompiled 2 months ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using Assets.Scripts.Inventory__Items__Pickups.Weapons;
using Assets.Scripts.Utility;
using BepInEx;
using BepInEx.Logging;
using BepInEx.Unity.IL2CPP;
using HarmonyLib;
using Il2CppSystem.Collections.Generic;
using MelonLoader;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("SwapStartingWeapon")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("SwapStartingWeapon")]
[assembly: AssemblyCopyright("Copyright ©  2025")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("a052f8b9-b678-40b0-a576-d1c5418579fb")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace SaveLoadWeapons
{
	[BepInPlugin("mike9k1.megabonk.swapstartingweapon", "Swap Starting Weapon", "0.0.2")]
	public class SwapStartingWeaponClass : MelonMod
	{
		public new const string PLUGIN_GUID = "mike9k1.megabonk.swapstartingweapon";

		public new const string PLUGIN_NAME = "Swap Starting Weapon";

		public new const string PLUGIN_VERSION = "0.0.2";

		private string PlayerTypeName = "Assets.Scripts.Utility.MyTime";

		private string TimerMethodName = "stageTimer";

		private string LevelTypeName = "LevelupScreen";

		private string LevelMethodName = "GetRefreshes";

		private string RefreshClickTypeName = "LevelupScreen";

		private string RefreshClickMethodName = "Refresh";

		private string RefreshTypeName = "PlayerInventory";

		private string RefreshMethodName = "Update";

		private string InventoryTypeName = "Assets.Scripts.Inventory__Items__Pickups.Progression.UnlockUtility";

		private string InventoryMethodName = "GetAvailableTomes";

		private static int newRefreshCount = 2;

		private static int initialRefreshValue = -1;

		private static int lastPlayerLevel = 0;

		private static int newPlayerLevel = 0;

		private static int totalAvailableTomeSlots = 2;

		private static PlayerInventory playerInventory;

		private static WeaponInventory origWeaponInventory;

		private static WeaponInventory clearWeaponInventory = new WeaponInventory();

		private LevelupScreen lvlScreen;

		private float lastTime;

		private static void RefreshPostfix()
		{
			if (newPlayerLevel == 1)
			{
				playerInventory.weaponInventory = origWeaponInventory;
				MelonLogger.Msg("Refresh clicked. Original weapons restored.");
			}
		}

		private static void LevelupPostfix(PlayerInventory __instance)
		{
			//IL_0020: Unknown result type (might be due to invalid IL or missing references)
			//IL_002a: Expected O, but got Unknown
			//IL_002a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0034: Expected O, but got Unknown
			//IL_0059: Unknown result type (might be due to invalid IL or missing references)
			playerInventory = __instance;
			newPlayerLevel = __instance.GetCharacterLevel();
			if (newPlayerLevel == 1 && lastPlayerLevel == 0)
			{
				clearWeaponInventory = new WeaponInventory();
				origWeaponInventory = new WeaponInventory();
				Enumerator<EWeapon, WeaponBase> enumerator = __instance.weaponInventory.weapons.GetEnumerator();
				while (enumerator.MoveNext())
				{
					KeyValuePair<EWeapon, WeaponBase> current = enumerator.Current;
					origWeaponInventory.weapons.Add(current.Key, current.Value);
				}
				__instance.weaponInventory = clearWeaponInventory;
				MelonLogger.Msg("Starting weapon cleared for swap");
			}
			lastPlayerLevel = newPlayerLevel;
		}

		private static bool InventoryPrefix(ref HashSet<TomeData> __result)
		{
			HashSet<TomeData> val = new HashSet<TomeData>();
			if ((newPlayerLevel == 1 && lastPlayerLevel == 0) || newPlayerLevel == 0)
			{
				MelonLogger.Msg("Tomes removed from rewards lineup (for weapon swap)");
				__result = val;
				return false;
			}
			return true;
		}

		private static Type FindType(string fullName)
		{
			Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
			foreach (Assembly assembly in assemblies)
			{
				try
				{
					Type type = assembly.GetType(fullName, throwOnError: false);
					if (type != null)
					{
						return type;
					}
				}
				catch
				{
				}
			}
			return null;
		}

		private void TryPatch()
		{
			//IL_0094: Unknown result type (might be due to invalid IL or missing references)
			//IL_009a: Expected O, but got Unknown
			//IL_017c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0183: Expected O, but got Unknown
			//IL_026b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0272: Expected O, but got Unknown
			Type type = FindType(RefreshTypeName);
			if (type == null)
			{
				MelonLogger.Error("Type not found: " + RefreshTypeName);
				return;
			}
			MethodInfo method = type.GetMethod(RefreshMethodName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
			if (method == null)
			{
				MelonLogger.Error("Method not found: " + type.FullName + "." + RefreshMethodName + "()");
				return;
			}
			HarmonyMethod val = new HarmonyMethod(typeof(SwapStartingWeaponClass).GetMethod("LevelupPostfix", BindingFlags.Static | BindingFlags.NonPublic));
			HarmonyInstance.Patch((MethodBase)method, (HarmonyMethod)null, val, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			MelonLogger.Msg("Patched " + type.FullName + "." + RefreshMethodName + "()");
			Type type2 = FindType(InventoryTypeName);
			if (type2 == null)
			{
				MelonLogger.Error("Type not found: " + InventoryTypeName);
				return;
			}
			MethodInfo method2 = type2.GetMethod(InventoryMethodName, BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
			if (method2 == null)
			{
				MelonLogger.Error("Method not found: " + type2.FullName + "." + InventoryMethodName + "()");
				return;
			}
			HarmonyMethod val2 = new HarmonyMethod(typeof(SwapStartingWeaponClass).GetMethod("InventoryPrefix", BindingFlags.Static | BindingFlags.NonPublic));
			HarmonyInstance.Patch((MethodBase)method2, val2, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			MelonLogger.Msg("Patched " + type2.FullName + "." + InventoryMethodName + "()");
			Type type3 = FindType(RefreshClickTypeName);
			if (type3 == null)
			{
				MelonLogger.Error("Type not found: " + RefreshClickTypeName);
				return;
			}
			MethodInfo method3 = type3.GetMethod(RefreshClickMethodName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
			if (method3 == null)
			{
				MelonLogger.Error("Method not found: " + type3.FullName + "." + RefreshClickMethodName + "()");
			}
			else
			{
				HarmonyMethod val3 = new HarmonyMethod(typeof(SwapStartingWeaponClass).GetMethod("RefreshPostfix", BindingFlags.Static | BindingFlags.NonPublic));
				HarmonyInstance.Patch((MethodBase)method3, (HarmonyMethod)null, val3, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
				MelonLogger.Msg("Patched " + type3.FullName + "." + RefreshClickMethodName + "()");
			}
		}

		public override void OnInitializeMelon()
		{
			TryPatch();
		}
	}
}
namespace MelonLoader
{
	public class MelonMod : BasePlugin
	{
		public Harmony HarmonyInstance;

		public static MelonMod melonModRef;

		public virtual string PLUGIN_GUID => "author.game.modname";

		public virtual string PLUGIN_NAME => "Mod Name Here";

		public virtual string PLUGIN_VERSION => "0.0.1";

		public MelonMod()
		{
			//IL_000d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0017: Expected O, but got Unknown
			HarmonyInstance = new Harmony(PLUGIN_GUID);
			melonModRef = this;
		}

		public virtual void OnInitializeMelon()
		{
		}

		public virtual void OnLateInitializeMelon()
		{
		}

		public virtual void OnFixedUpdate()
		{
		}

		public virtual void OnUpdate()
		{
		}

		public override void Load()
		{
			//IL_001a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0020: Expected O, but got Unknown
			//IL_0059: Unknown result type (might be due to invalid IL or missing references)
			//IL_005f: Expected O, but got Unknown
			OnInitializeMelon();
			HarmonyMethod val = new HarmonyMethod(typeof(MelonMod).GetMethod("FixedUpdate"));
			HarmonyInstance.Patch((MethodBase)typeof(MyTime).GetMethod("FixedUpdate"), (HarmonyMethod)null, val, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			HarmonyMethod val2 = new HarmonyMethod(typeof(MelonMod).GetMethod("Update"));
			HarmonyInstance.Patch((MethodBase)typeof(MyTime).GetMethod("Update"), (HarmonyMethod)null, val2, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
		}

		public void Awake()
		{
			OnLateInitializeMelon();
		}

		public static void FixedUpdate()
		{
			melonModRef.OnFixedUpdate();
		}

		public static void Update()
		{
			melonModRef.OnUpdate();
		}
	}
	public class MelonLogger
	{
		private static ManualLogSource log;

		private static bool logAdded;

		public static void Msg(string message)
		{
			//IL_0028: Unknown result type (might be due to invalid IL or missing references)
			//IL_0032: Expected O, but got Unknown
			if (!logAdded)
			{
				log = new ManualLogSource(((AssemblyTitleAttribute)Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyTitleAttribute), inherit: false)[0]).Title);
				Logger.Sources.Add((ILogSource)(object)log);
				logAdded = true;
			}
			log.LogInfo((object)message);
		}

		public static void Error(string message)
		{
			//IL_0028: Unknown result type (might be due to invalid IL or missing references)
			//IL_0032: Expected O, but got Unknown
			if (!logAdded)
			{
				log = new ManualLogSource(((AssemblyTitleAttribute)Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyTitleAttribute), inherit: false)[0]).Title);
				Logger.Sources.Add((ILogSource)(object)log);
				logAdded = true;
			}
			log.LogError((object)message);
		}
	}
}