Decompiled source of LightweightInventory v1.0.2

plugins/LightweightInventory.dll

Decompiled a week ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Bootstrap;
using BepInEx.Configuration;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("LightweightInventory")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.2.0")]
[assembly: AssemblyInformationalVersion("1.0.2+8e19140e0829594acc5690a67946011717375d6b")]
[assembly: AssemblyProduct("LightweightInventory")]
[assembly: AssemblyTitle("LightweightInventory")]
[assembly: AssemblyVersion("1.0.2.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 LightweightInventory
{
	[HarmonyPatch(typeof(InventoryGui), "UpdateInventory")]
	internal static class FeatherOverlay
	{
		private static Sprite featherSprite;

		private static bool loadAttempted;

		private static Material originalMaterial;

		private static Vector3 originalScale = Vector3.zero;

		[HarmonyPriority(0)]
		private static void Postfix(InventoryGui __instance)
		{
			//IL_0109: Unknown result type (might be due to invalid IL or missing references)
			//IL_010e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0130: Unknown result type (might be due to invalid IL or missing references)
			//IL_0135: Unknown result type (might be due to invalid IL or missing references)
			//IL_018b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0195: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a1: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ba: Unknown result type (might be due to invalid IL or missing references)
			//IL_01bf: Unknown result type (might be due to invalid IL or missing references)
			//IL_01f1: Unknown result type (might be due to invalid IL or missing references)
			//IL_01f6: Unknown result type (might be due to invalid IL or missing references)
			//IL_0209: Unknown result type (might be due to invalid IL or missing references)
			//IL_0202: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)__instance == (Object)null || (Object)(object)__instance.m_playerGrid == (Object)null)
			{
				return;
			}
			Player localPlayer = Player.m_localPlayer;
			if ((Object)(object)localPlayer == (Object)null)
			{
				return;
			}
			Inventory inventory = ((Humanoid)localPlayer).GetInventory();
			if (inventory == null)
			{
				return;
			}
			if (!loadAttempted)
			{
				LoadFeather();
			}
			if ((Object)(object)featherSprite == (Object)null)
			{
				return;
			}
			int width = inventory.GetWidth();
			int playableInventoryHeight = LightweightInventoryPlugin.GetPlayableInventoryHeight(inventory);
			if (!(AccessTools.Field(typeof(InventoryGrid), "m_elements")?.GetValue(__instance.m_playerGrid) is List<InventoryElement> list) || width <= 0 || playableInventoryHeight <= 0)
			{
				return;
			}
			int num = Math.Min(list.Count, width * playableInventoryHeight);
			for (int i = 0; i < num; i++)
			{
				InventoryElement val = list[i];
				if ((Object)(object)val == (Object)null || (Object)(object)val.m_icon == (Object)null)
				{
					continue;
				}
				Image icon = val.m_icon;
				if ((Object)(object)originalMaterial == (Object)null && (Object)(object)((Graphic)icon).material != (Object)null)
				{
					originalMaterial = ((Graphic)icon).material;
				}
				if (originalScale == Vector3.zero && (Object)(object)((Graphic)icon).material != (Object)null)
				{
					originalScale = ((Component)icon).transform.localScale;
				}
				if (LightweightInventoryPlugin.Enabled != null && LightweightInventoryPlugin.Enabled.Value && LightweightInventoryPlugin.IsAffected(i / width, playableInventoryHeight) && !val.m_used)
				{
					((Behaviour)icon).enabled = true;
					((Graphic)icon).material = null;
					icon.sprite = featherSprite;
					((Component)icon).transform.localScale = Vector3.one * 0.7f;
					((Graphic)icon).color = Color.grey - new Color(0f, 0f, 0f, 0.7f);
				}
				else if ((Object)(object)icon.sprite == (Object)(object)featherSprite)
				{
					((Graphic)icon).material = originalMaterial;
					((Component)icon).transform.localScale = ((originalScale == Vector3.zero) ? Vector3.one : originalScale);
					if (!val.m_used)
					{
						((Behaviour)icon).enabled = false;
					}
				}
			}
		}

		private static void LoadFeather()
		{
			//IL_0048: Unknown result type (might be due to invalid IL or missing references)
			//IL_004e: Expected O, but got Unknown
			//IL_0080: Unknown result type (might be due to invalid IL or missing references)
			//IL_0085: Unknown result type (might be due to invalid IL or missing references)
			loadAttempted = true;
			string text = Path.Combine(Path.GetDirectoryName(typeof(FeatherOverlay).Assembly.Location), "lightenedslot.png");
			if (!File.Exists(text))
			{
				Debug.LogWarning((object)("[LightweightInventory] Missing feather PNG: " + text));
				return;
			}
			try
			{
				Texture2D val = new Texture2D(2, 2, (TextureFormat)4, false);
				if (!ImageConversion.LoadImage(val, File.ReadAllBytes(text)))
				{
					throw new InvalidDataException("Failed to decode feather PNG.");
				}
				featherSprite = Sprite.Create(val, new Rect(0f, 0f, (float)((Texture)val).width, (float)((Texture)val).height), Vector2.zero);
				Debug.Log((object)"[LightweightInventory] Feather icon loaded.");
			}
			catch (Exception ex)
			{
				Debug.LogError((object)("[LightweightInventory] Feather load error: " + ex));
			}
		}
	}
	[BepInPlugin("christian.lightweightinventory", "Lightweight Inventory", "1.0.2")]
	[BepInIncompatibility("shudnal.ExtraSlots")]
	public sealed class LightweightInventoryPlugin : BaseUnityPlugin
	{
		[HarmonyPatch(typeof(Player), "OnSpawned")]
		private static class PlayerSpawnedPatch
		{
			private static void Postfix(Player __instance)
			{
				if (!((Object)(object)__instance == (Object)null) && __instance == Player.m_localPlayer)
				{
					Inventory inventory = ((Humanoid)__instance).GetInventory();
					if (inventory != null)
					{
						AccessTools.Method(typeof(Inventory), "UpdateTotalWeight", (Type[])null, (Type[])null)?.Invoke(inventory, null);
					}
				}
			}
		}

		[HarmonyPatch(typeof(Inventory), "UpdateTotalWeight")]
		private static class UpdateTotalWeightPatch
		{
			internal static bool InWeightUpdate;

			private static void Prefix(Inventory __instance, out bool __state)
			{
				__state = InWeightUpdate;
				Player localPlayer = Player.m_localPlayer;
				InWeightUpdate = (Object)(object)localPlayer != (Object)null && __instance != null && __instance == ((Humanoid)localPlayer).GetInventory();
			}

			private static void Postfix(bool __state)
			{
				InWeightUpdate = __state;
			}

			private static Exception Finalizer(Exception __exception, bool __state)
			{
				InWeightUpdate = __state;
				return __exception;
			}
		}

		[HarmonyPatch(typeof(ItemData), "GetWeight")]
		private static class ItemWeightPatch
		{
			private static void Postfix(ItemData __instance, ref float __result)
			{
				if (__instance == null)
				{
					return;
				}
				Player localPlayer = Player.m_localPlayer;
				if ((Object)(object)localPlayer == (Object)null)
				{
					return;
				}
				Inventory inventory = ((Humanoid)localPlayer).GetInventory();
				if (inventory != null)
				{
					bool flag = IsAffected(__instance.m_gridPos.y, GetPlayableInventoryHeight(inventory));
					if (UpdateTotalWeightPatch.InWeightUpdate && flag && WeightMultiplier != null && !(WeightMultiplier.Value >= 1f))
					{
						__result *= WeightMultiplier.Value;
					}
				}
			}
		}

		public const string PluginGuid = "christian.lightweightinventory";

		public const string PluginName = "Lightweight Inventory";

		public const string PluginVersion = "1.0.2";

		internal static ConfigEntry<bool> Enabled;

		internal static ConfigEntry<int> FirstRowIndex;

		internal static ConfigEntry<float> WeightMultiplier;

		private Harmony _harmony;

		internal static int GetPlayableInventoryHeight(Inventory inventory)
		{
			if (inventory == null)
			{
				return 0;
			}
			int height = inventory.GetHeight();
			bool flag = Chainloader.PluginInfos.ContainsKey("seneaL.valheim.ui");
			return Math.Max(0, height - (flag ? 5 : 0));
		}

		private void Awake()
		{
			//IL_0045: Unknown result type (might be due to invalid IL or missing references)
			//IL_004f: Expected O, but got Unknown
			//IL_0082: Unknown result type (might be due to invalid IL or missing references)
			//IL_008c: Expected O, but got Unknown
			//IL_0106: Unknown result type (might be due to invalid IL or missing references)
			//IL_0110: Expected O, but got Unknown
			Enabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "Enable reduced carried weight for items located in selected inventory rows.");
			FirstRowIndex = ((BaseUnityPlugin)this).Config.Bind<int>("Lightweight Rows", "FirstRowIndex", -2, new ConfigDescription("Negative = number of bottom rows (-2 means bottom two); zero = disabled; positive = 1-based first affected row through the bottom.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(-9, 9), Array.Empty<object>()));
			WeightMultiplier = ((BaseUnityPlugin)this).Config.Bind<float>("Lightweight Rows", "WeightMultiplier", 0.5f, new ConfigDescription("Item weight multiplier in affected rows: 0.5 = half weight; 1 = normal weight.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 1f), Array.Empty<object>()));
			Enabled.SettingChanged += OnSettingChanged;
			FirstRowIndex.SettingChanged += OnSettingChanged;
			WeightMultiplier.SettingChanged += OnSettingChanged;
			_harmony = new Harmony("christian.lightweightinventory");
			_harmony.PatchAll(typeof(LightweightInventoryPlugin).Assembly);
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Lightweight Inventory v1.0.2 loaded");
		}

		private static void OnSettingChanged(object sender, EventArgs args)
		{
			Player localPlayer = Player.m_localPlayer;
			if (!((Object)(object)localPlayer == (Object)null))
			{
				Inventory inventory = ((Humanoid)localPlayer).GetInventory();
				if (inventory != null)
				{
					AccessTools.Method(typeof(Inventory), "UpdateTotalWeight", (Type[])null, (Type[])null)?.Invoke(inventory, null);
				}
			}
		}

		private void OnDestroy()
		{
			if (_harmony != null)
			{
				_harmony.UnpatchSelf();
			}
		}

		internal static bool IsAffected(int y, int height)
		{
			if (Enabled == null || !Enabled.Value || FirstRowIndex == null || height <= 0 || y < 0 || y >= height)
			{
				return false;
			}
			int value = FirstRowIndex.Value;
			if (value == 0)
			{
				return false;
			}
			if (value >= 0)
			{
				return y >= value - 1;
			}
			return y >= Math.Max(0, height + value);
		}
	}
}