Decompiled source of ZenBeehive v0.3.0

plugins\ZenBeehive.dll

Decompiled a month ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using Jotunn;
using Jotunn.Utils;
using Microsoft.CodeAnalysis;
using UnityEngine;
using Zen;
using Zen.Compatibility;
using Zen.Lib;
using Zen.Lib.Config;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("ZenBeehive")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("ZenBeehive")]
[assembly: AssemblyCopyright("Copyright \ufffd  2021")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("e3243d22-4307-4008-ba36-9f326008cde5")]
[assembly: AssemblyFileVersion("0.0.1.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.0.1.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableAttribute : Attribute
	{
		public readonly byte[] NullableFlags;

		public NullableAttribute(byte P_0)
		{
			NullableFlags = new byte[1] { P_0 };
		}

		public NullableAttribute(byte[] P_0)
		{
			NullableFlags = P_0;
		}
	}
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableContextAttribute : Attribute
	{
		public readonly byte Flag;

		public NullableContextAttribute(byte P_0)
		{
			Flag = P_0;
		}
	}
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.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 ZenBeehive
{
	[HarmonyPatch]
	internal static class BeehiveRules
	{
		private static Beehive? _beehive;

		private static bool IsHoneyOpen()
		{
			return Object.op_Implicit((Object)(object)_beehive);
		}

		private static bool IsHoneyOpen(out Beehive beehive)
		{
			beehive = _beehive;
			return IsHoneyOpen();
		}

		internal static void SetupPrefab()
		{
			Logging<Plugin>.Info((object)"Setting up beehive prefab container...", 0);
			GameObject prefab = ZNetScene.instance.GetPrefab("piece_beehive");
			Beehive component = prefab.GetComponent<Beehive>();
			Container orAddComponent = ExposedGameObjectExtension.GetOrAddComponent<Container>(prefab);
			orAddComponent.m_name = component.m_name;
			orAddComponent.m_width = 1;
			orAddComponent.m_height = 1;
			GameObject itemPrefab = ObjectDB.instance.GetItemPrefab(Configs.HoneyItem.Value);
			if (!Object.op_Implicit((Object)(object)itemPrefab))
			{
				Logging<Plugin>.Error((object)("Item not found: " + Configs.HoneyItem.Value), (ushort)0);
			}
			else
			{
				component.m_honeyItem = itemPrefab.GetComponent<ItemDrop>();
			}
			component.m_maxHoney = Mathf.Min(component.m_honeyItem.m_itemData.m_shared.m_maxStackSize, Configs.MaxHoney.Value);
		}

		private static GameObject GetHoneyItemPrefab(this Beehive beehive)
		{
			return ObjectDB.instance.GetItemPrefab(((Object)beehive.m_honeyItem).name);
		}

		private static int UpdateHoneyFromContainer(this Beehive beehive)
		{
			InventoryGrid containerGrid = InventoryGui.instance.ContainerGrid;
			Inventory val = ((containerGrid != null) ? containerGrid.GetInventory() : null) ?? ((Component)beehive).GetComponent<Container>().GetInventory();
			return beehive.SetHoneyLevel(val.NrOfItemsIncludingStacks());
		}

		private static int SetHoneyLevel(this Beehive beehive, int level)
		{
			if (level == beehive.GetHoneyLevel())
			{
				return level;
			}
			beehive.m_nview.ClaimOwnership();
			beehive.m_nview.GetZDO().Set(ZDOVars.s_level, level, false);
			return level;
		}

		[HarmonyPrefix]
		[HarmonyPatch(typeof(Beehive), "Extract")]
		private static void Beehive_Extract(Beehive __instance, ref bool __runOriginal)
		{
			Logging<Plugin>.Info((object)"Extracting ... opening beehive container.", 0);
			((Component)__instance).GetComponent<Container>().Interact((Humanoid)(object)Player.m_localPlayer, false, false);
			__runOriginal = false;
		}

		private static void FillContainer(this Beehive beehive, Container container)
		{
			Inventory inventory = container.GetInventory();
			ItemData val = ((inventory.NrOfItems() > 0) ? inventory.GetItem(0) : null);
			int honeyLevel = beehive.GetHoneyLevel();
			if (val == null || val.m_stack != honeyLevel)
			{
				GameObject honeyItemPrefab = beehive.GetHoneyItemPrefab();
				if (inventory.NrOfItems() > 0)
				{
					inventory.RemoveAll();
				}
				if (honeyLevel > 0)
				{
					inventory.AddItem(honeyItemPrefab, honeyLevel);
				}
			}
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(Beehive), "UpdateBees")]
		private static void Beehive_UpdateBees(Beehive __instance)
		{
			if (__instance.m_nview.IsOwner())
			{
				__instance.FillContainer(((Component)__instance).GetComponent<Container>());
			}
		}

		[HarmonyPrefix]
		[HarmonyPatch(typeof(InventoryGui), "Hide")]
		private static void InventoryGui_Hide(InventoryGui __instance)
		{
			_beehive = null;
		}

		[HarmonyPrefix]
		[HarmonyPatch(typeof(InventoryGui), "Show")]
		private static void InventoryGui_Show(InventoryGui __instance, Container? container)
		{
			Beehive beehive = default(Beehive);
			if (!((Object)(object)container == (Object)null) && ((Component)container).TryGetComponent<Beehive>(ref beehive))
			{
				_beehive = beehive;
				InventoryGuiExt.ShowStackAllButton(InventoryGui.instance, false);
			}
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(Humanoid), "UseItem")]
		private static void Humanoid_UseItem()
		{
			if (IsHoneyOpen(out Beehive beehive))
			{
				beehive.UpdateHoneyFromContainer();
			}
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(InventoryGui), "OnTakeAll")]
		private static void InventoryGui_OnTakeAll(InventoryGui __instance)
		{
			if (IsHoneyOpen(out Beehive beehive) && beehive.UpdateHoneyFromContainer() > 0)
			{
				((Character)Player.m_localPlayer).Message((MessageType)2, "$inventory_full", 0, (Sprite)null);
			}
		}

		[HarmonyPrefix]
		[HarmonyPatch(typeof(Inventory), "StackAll")]
		private static void Inventory_StackAll(ref bool __runOriginal)
		{
			if (IsHoneyOpen())
			{
				__runOriginal = false;
				if (Configs.HoldToTakeAll.Value)
				{
					InventoryGui.instance.OnTakeAll();
				}
			}
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(InventoryGui), "OnDropOutside")]
		private static void InventoryGui_OnDropOutside()
		{
			if (IsHoneyOpen(out Beehive beehive))
			{
				beehive.UpdateHoneyFromContainer();
			}
		}

		[HarmonyPrefix]
		[HarmonyPatch(typeof(InventoryGui), "OnSelectedItem")]
		private static void InventoryGui_OnSelectedItem_Prefix(InventoryGui __instance, InventoryGrid grid, ItemData item, Vector2i pos, Modifier mod, ref bool __runOriginal)
		{
			//IL_003c: Unknown result type (might be due to invalid IL or missing references)
			//IL_003f: Invalid comparison between Unknown and I4
			//IL_0045: Unknown result type (might be due to invalid IL or missing references)
			//IL_006c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0072: Unknown result type (might be due to invalid IL or missing references)
			if (!IsHoneyOpen(out Beehive beehive))
			{
				return;
			}
			if ((Object)(object)grid == (Object)(object)__instance.ContainerGrid)
			{
				if (__instance.m_dragItem != null)
				{
					__instance.SetupDragItem((ItemData)null, (Inventory)null, 0);
					__runOriginal = false;
				}
			}
			else
			{
				if (!((Object)(object)grid == (Object)(object)__instance.m_playerGrid))
				{
					return;
				}
				if ((int)mod == 2)
				{
					__runOriginal = false;
				}
				if ((int)mod == 0 && __instance.m_dragItem != null && __instance.m_dragInventory != __instance.m_playerGrid.GetInventory())
				{
					ItemData itemAt = grid.GetInventory().GetItemAt(pos.x, pos.y);
					if (itemAt != null && !((Object)(object)itemAt.m_dropPrefab == (Object)(object)beehive.GetHoneyItemPrefab()))
					{
						__runOriginal = false;
					}
				}
			}
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(InventoryGui), "OnSelectedItem")]
		private static void InventoryGui_OnSelectedItem_Postfix()
		{
			if (IsHoneyOpen(out Beehive beehive))
			{
				beehive.UpdateHoneyFromContainer();
			}
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(InventoryGrid), "UpdateGui")]
		private static void InventoryGrid_UpdateGui(InventoryGrid __instance)
		{
			if (IsHoneyOpen() && __instance.GetInventory().NrOfItems() != 0)
			{
				ItemData item = __instance.GetInventory().GetItem(0);
				int num = Math.Min(item.m_shared.m_maxStackSize, Configs.MaxHoney.Value);
				__instance.m_elements[0].m_amount.text = $"{item.m_stack}/{num}";
			}
		}
	}
	public static class Configs
	{
		private const string SECTION_BEEHIVE = "Beehive";

		public static readonly ConfigEntry<bool> HoldToTakeAll = Config.Define<bool>(true, "Beehive", "Hold To Take All", true, "Hold down the interact key when opening a beehive to automatically take all the honey.");

		public static readonly ConfigEntry<int> MaxHoney = Config.Define<int>(true, "Beehive", "Max Capacity", 4, Config.AcceptRange<int>(1, 999), "Max honey that a beehive can hold. Logout for changes to take effect. (Vanilla: 4)\r\nNote: Max capacity can not exceed the stack size for the Item Prefab.\r\nExample: If the Item Prefab's max stack size is 50 then it won't matter if you put 999 here.\r\nIt will never go above the item's max stack size limit.");

		public static readonly ConfigEntry<string> HoneyItem = Config.Define<string>(true, "Beehive", "Item Prefab", "Honey", "The prefab name of the item that grows inside a beehive. The Honey Item. Logout for changes to take effect.");
	}
	[Disable(new Type[] { typeof(Beehive) })]
	[BepInPlugin("ZenDragon.ZenBeehive", "ZenBeehive", "0.3.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[NetworkCompatibility(/*Could not decode attribute arguments.*/)]
	internal class Plugin : ZenMod<Plugin>
	{
		public const string PluginName = "ZenBeehive";

		public const string PluginVersion = "0.3.0";

		public const string PluginGUID = "ZenDragon.ZenBeehive";

		protected override void Setup()
		{
			base.ConfigSync += BeehiveRules.SetupPrefab;
		}

		protected override void TitleScene(bool isFirstBoot)
		{
		}

		protected override void WorldStart()
		{
		}

		protected override void Shutdown()
		{
		}
	}
}