Decompiled source of BackpackWeightFix v1.0.0

BackpackWeightFix.dll

Decompiled 3 days ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyVersion("0.0.0.0")]
namespace BackpackWeightFix;

[BepInPlugin("com.burks.backpackweightfix", "Backpack Weight Fix", "1.0.0")]
[BepInDependency(/*Could not decode attribute arguments.*/)]
public class BackpackWeightFixPlugin : BaseUnityPlugin
{
	public const string GUID = "com.burks.backpackweightfix";

	public const string NAME = "Backpack Weight Fix";

	public const string VERSION = "1.0.0";

	public static ConfigEntry<float> Interval;

	private static MethodInfo _getBackpackInv;

	private static readonly MethodInfo _updateWeight = AccessTools.Method(typeof(Inventory), "UpdateTotalWeight", (Type[])null, (Type[])null);

	private float _next;

	private Player _lastPlayer;

	private bool _loggedFix;

	private void Awake()
	{
		//IL_0024: Unknown result type (might be due to invalid IL or missing references)
		//IL_002a: Invalid comparison between Unknown and I4
		Interval = ((BaseUnityPlugin)this).Config.Bind<float>("General", "RecheckSeconds", 2f, "How often your carry weight is recounted (backpack contents included).");
		if ((int)SystemInfo.graphicsDeviceType != 4)
		{
			Type type = Type.GetType("AdventureBackpacks.API.ABAPI, AdventureBackpacks");
			if (type != null)
			{
				_getBackpackInv = AccessTools.Method(type, "GetEquippedBackpackInventory", new Type[1] { typeof(Player) }, (Type[])null);
			}
			if (_getBackpackInv == null)
			{
				((BaseUnityPlugin)this).Logger.LogWarning((object)"Adventure Backpacks API not found; only your main inventory weight will be recounted.");
			}
			if (_updateWeight == null)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)"Inventory.UpdateTotalWeight not found; doing nothing.");
				((Behaviour)this).enabled = false;
			}
			else
			{
				((BaseUnityPlugin)this).Logger.LogInfo((object)"Backpack Weight Fix 1.0.0 loaded.");
			}
		}
	}

	private void Update()
	{
		Player localPlayer = Player.m_localPlayer;
		if ((Object)(object)localPlayer == (Object)null || ((Character)localPlayer).IsDead())
		{
			_lastPlayer = null;
		}
		else if ((Object)(object)localPlayer != (Object)(object)_lastPlayer)
		{
			_lastPlayer = localPlayer;
			_next = Time.time + 1f;
		}
		else
		{
			if (Time.time < _next)
			{
				return;
			}
			_next = Time.time + Mathf.Max(0.5f, Interval.Value);
			try
			{
				Inventory inventory = ((Humanoid)localPlayer).GetInventory();
				if (inventory == null)
				{
					return;
				}
				float totalWeight = inventory.GetTotalWeight();
				if (_getBackpackInv != null)
				{
					object? obj = _getBackpackInv.Invoke(null, new object[1] { localPlayer });
					Inventory val = (Inventory)((obj is Inventory) ? obj : null);
					if (val != null)
					{
						_updateWeight.Invoke(val, null);
					}
				}
				_updateWeight.Invoke(inventory, null);
				float totalWeight2 = inventory.GetTotalWeight();
				if (!_loggedFix && Mathf.Abs(totalWeight2 - totalWeight) > 0.05f)
				{
					_loggedFix = true;
					((BaseUnityPlugin)this).Logger.LogInfo((object)$"Carry weight was off: {totalWeight:0.0} -> {totalWeight2:0.0}.");
				}
			}
			catch (Exception ex)
			{
				((BaseUnityPlugin)this).Logger.LogWarning((object)("Recount failed: " + ex.Message));
				_next = Time.time + 30f;
			}
		}
	}
}