Decompiled source of SwiftPickupMagnetFix v2.0.0

SwiftPickupFullInventory.dll

Decompiled 2 weeks ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyCompany("SwiftPickupFullInventory")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("SwiftPickupFullInventory")]
[assembly: AssemblyTitle("SwiftPickupFullInventory")]
[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 SwiftPickupFullInventory
{
	[BepInPlugin("saru.swiftpickup.tweaks", "SwiftPickup Magnet Fix", "2.0.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class Plugin : BaseUnityPlugin
	{
		public const string GUID = "saru.swiftpickup.tweaks";

		public const string SwiftPickupGUID = "user.swiftpickup";

		internal static ManualLogSource Log;

		internal static ConfigEntry<bool> Enabled;

		private Harmony _harmony;

		private void Awake()
		{
			//IL_0049: Unknown result type (might be due to invalid IL or missing references)
			//IL_0053: Expected O, but got Unknown
			Log = ((BaseUnityPlugin)this).Logger;
			Enabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "Coupe le magnet de SwiftPickup pour les objets que l'inventaire ne peut pas accepter (slots/piles pleins, ou poids depasse).");
			if (!SwiftPickupRef.Resolve())
			{
				((BaseUnityPlugin)this).Logger.LogError((object)"Internes de SwiftPickup introuvables. Patch non applique.");
				return;
			}
			_harmony = new Harmony("saru.swiftpickup.tweaks");
			_harmony.PatchAll(Assembly.GetExecutingAssembly());
		}

		private void OnDestroy()
		{
			Harmony harmony = _harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
		}
	}
	internal static class SwiftPickupRef
	{
		internal static MethodInfo MoveMagnets;

		private static FieldInfo _magnetItemsField;

		internal static List<ItemDrop> MagnetItems => _magnetItemsField?.GetValue(null) as List<ItemDrop>;

		internal static bool Resolve()
		{
			Type type = AccessTools.TypeByName("SwiftPickup.SwiftPickupPlugin");
			if (type == null)
			{
				return false;
			}
			MoveMagnets = AccessTools.Method(type, "MoveMagnets", (Type[])null, (Type[])null);
			_magnetItemsField = AccessTools.Field(type, "MagnetItems");
			if (MoveMagnets == null)
			{
				Plugin.Log.LogWarning((object)"MoveMagnets introuvable.");
			}
			if (_magnetItemsField == null)
			{
				Plugin.Log.LogWarning((object)"MagnetItems introuvable.");
			}
			if (MoveMagnets != null)
			{
				return _magnetItemsField != null;
			}
			return false;
		}
	}
	[HarmonyPatch]
	internal static class MoveMagnetsPatch
	{
		private static MethodBase TargetMethod()
		{
			return SwiftPickupRef.MoveMagnets;
		}

		private static void Prefix(Player player)
		{
			if (!Plugin.Enabled.Value)
			{
				return;
			}
			List<ItemDrop> magnetItems = SwiftPickupRef.MagnetItems;
			if (magnetItems == null || magnetItems.Count == 0)
			{
				return;
			}
			Inventory val = ((player != null) ? ((Humanoid)player).GetInventory() : null);
			if (val == null)
			{
				return;
			}
			for (int num = magnetItems.Count - 1; num >= 0; num--)
			{
				ItemDrop val2 = magnetItems[num];
				if ((Object)(object)val2 == (Object)null || val2.m_itemData == null || !Fits(player, val, val2))
				{
					magnetItems.RemoveAt(num);
				}
			}
		}

		private static bool Fits(Player player, Inventory inv, ItemDrop drop)
		{
			if (!inv.CanAddItem(drop.m_itemData, -1))
			{
				return false;
			}
			return inv.GetTotalWeight() + drop.m_itemData.GetWeight(-1) <= player.GetMaxCarryWeight();
		}
	}
}