Decompiled source of BackpackAtStations v1.0.1

BackpackAtStations.dll

Decompiled 21 hours ago
using System;
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.6.2", FrameworkDisplayName = ".NET Framework 4.6.2")]
[assembly: AssemblyCompany("BackpackAtStations")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("BackpackAtStations")]
[assembly: AssemblyTitle("BackpackAtStations")]
[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 BackpackAtStations
{
	[BepInPlugin("pieter.valheim.backpackatstations", "Backpack At Stations", "1.0.1")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class Plugin : BaseUnityPlugin
	{
		[HarmonyPatch(typeof(InventoryGui), "Show", new Type[]
		{
			typeof(Container),
			typeof(int)
		})]
		private static class ShowPatch
		{
			private static void Postfix(InventoryGui __instance, Container container, int activeGroup)
			{
				if (_opening || (Object)(object)container != (Object)null || activeGroup != 3)
				{
					return;
				}
				Player localPlayer = Player.m_localPlayer;
				if ((Object)(object)localPlayer == (Object)null || (Object)(object)localPlayer.GetCurrentCraftingStation() == (Object)null || !_openWithInventory.Value || (bool)_backpackIsOpen.GetValue(null) || !(bool)_canOpenBackpack.Invoke(null, new object[1] { localPlayer }))
				{
					return;
				}
				_opening = true;
				try
				{
					_openBackpack.Invoke(null, new object[2] { localPlayer, __instance });
					_setActiveGroup?.Invoke(__instance, new object[2] { activeGroup, false });
				}
				catch (Exception ex)
				{
					Logger.CreateLogSource("Backpack At Stations").LogWarning((object)("Could not open backpack at station: " + ex.Message));
				}
				finally
				{
					_opening = false;
				}
			}
		}

		public const string Guid = "pieter.valheim.backpackatstations";

		public const string Name = "Backpack At Stations";

		public const string Version = "1.0.1";

		private const string AdventureBackpacksGuid = "vapok.mods.adventurebackpacks";

		private static FieldInfo _backpackIsOpen;

		private static MethodInfo _openBackpack;

		private static MethodInfo _canOpenBackpack;

		private static ConfigEntry<bool> _openWithInventory;

		private static MethodInfo _setActiveGroup;

		private static bool _opening;

		private void Awake()
		{
			//IL_0122: Unknown result type (might be due to invalid IL or missing references)
			_backpackIsOpen = AccessTools.Field(AccessTools.TypeByName("AdventureBackpacks.Patches.InventoryGuiPatches"), "BackpackIsOpen");
			Type type = AccessTools.TypeByName("AdventureBackpacks.Extensions.PlayerExtensions");
			_openBackpack = AccessTools.Method(type, "OpenBackpack", new Type[2]
			{
				typeof(Player),
				typeof(InventoryGui)
			}, (Type[])null);
			_canOpenBackpack = AccessTools.Method(type, "CanOpenBackpack", new Type[1] { typeof(Player) }, (Type[])null);
			_openWithInventory = AccessTools.Field(AccessTools.TypeByName("AdventureBackpacks.Configuration.ConfigRegistry"), "OpenWithInventory")?.GetValue(null) as ConfigEntry<bool>;
			_setActiveGroup = AccessTools.Method(typeof(InventoryGui), "SetActiveGroup", new Type[2]
			{
				typeof(int),
				typeof(bool)
			}, (Type[])null);
			if (_backpackIsOpen == null || _openBackpack == null || _canOpenBackpack == null || _openWithInventory == null)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)"Disabled, Adventure Backpacks' backpack functions were not found.");
				return;
			}
			new Harmony("pieter.valheim.backpackatstations").PatchAll(typeof(ShowPatch));
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Backpack now also opens at crafting stations.");
		}
	}
}