Decompiled source of ZenBeehive v0.2.3

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.Compatibility;
using Zen.Config;
using Zen.Lib;
using Zen.Logging;

[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 BeehivePatches
	{
		private static Beehive? _beehive;

		private static bool IsHoneyOpen => Object.op_Implicit((Object)(object)_beehive);

		internal static void SetupPrefab()
		{
			GameObject prefab = ZNetScene.instance.GetPrefab("piece_beehive");
			Beehive val = default(Beehive);
			if (prefab.TryGetComponent<Beehive>(ref val))
			{
				Container orAddComponent = ExposedGameObjectExtension.GetOrAddComponent<Container>(prefab);
				orAddComponent.m_name = val.m_name;
				orAddComponent.m_width = 1;
				orAddComponent.m_height = 1;
				val.m_honeyItem = ObjectDB.instance.GetItemPrefab(Configs.HoneyItem.Value).GetComponent<ItemDrop>();
				val.m_maxHoney = Mathf.Min(val.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);
		}

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

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

		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);
				}
			}
		}

		[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 (container != null && ((Component)container).TryGetComponent<Beehive>(ref beehive))
			{
				_beehive = beehive;
				InventoryGuiExt.ShowStackAllButton(InventoryGui.instance, false);
			}
		}

		[HarmonyPrefix]
		[HarmonyPatch(typeof(Humanoid), "UseItem")]
		private static void Humanoid_UseItem(Player __instance, Inventory inventory, bool fromInventoryGui, ref bool __runOriginal)
		{
			if (IsHoneyOpen && fromInventoryGui && inventory == InventoryGui.instance.ContainerGrid.GetInventory())
			{
				__runOriginal = false;
			}
		}

		[HarmonyPrefix]
		[HarmonyPatch(typeof(InventoryGui), "OnTakeAll")]
		private static void InventoryGui_OnTakeAll(InventoryGui __instance, ref bool __runOriginal)
		{
			if (IsHoneyOpen)
			{
				Inventory inventory = InventoryGui.instance.m_playerGrid.GetInventory();
				ItemData item = __instance.ContainerGrid.GetInventory().GetItem(0);
				if (inventory.CanAddItem(item.m_dropPrefab, item.m_stack))
				{
					_beehive.ResetLevel();
				}
				else
				{
					__runOriginal = false;
				}
			}
		}

		[HarmonyPrefix]
		[HarmonyPatch(typeof(Inventory), "StackAll")]
		private static void Inventory_StackAll(ref bool __runOriginal)
		{
			__runOriginal = !IsHoneyOpen;
		}

		[HarmonyPrefix]
		[HarmonyPatch(typeof(InventoryGui), "OnDropOutside")]
		private static void InventoryGui_OnDropOutside(InventoryGui __instance, ref bool __runOriginal)
		{
			if (IsHoneyOpen && __instance.m_dragInventory == __instance.ContainerGrid.GetInventory())
			{
				_beehive.ResetLevel();
			}
		}

		[HarmonyPrefix]
		[HarmonyPatch(typeof(InventoryGui), "OnSelectedItem")]
		private static void InventoryGui_OnSelectedItem(InventoryGui __instance, InventoryGrid grid, ItemData item, Vector2i pos, Modifier mod, ref bool __runOriginal)
		{
			//IL_0016: 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)
			//IL_0088: Invalid comparison between Unknown and I4
			//IL_0023: Unknown result type (might be due to invalid IL or missing references)
			//IL_0026: Invalid comparison between Unknown and I4
			//IL_001a: Unknown result type (might be due to invalid IL or missing references)
			//IL_001d: Invalid comparison between Unknown and I4
			//IL_008e: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bb: Unknown result type (might be due to invalid IL or missing references)
			if (!IsHoneyOpen)
			{
				return;
			}
			if ((Object)(object)grid == (Object)(object)__instance.ContainerGrid)
			{
				if ((int)mod != 0 && (int)mod != 2)
				{
					__runOriginal = false;
				}
				if ((int)mod == 2)
				{
					ItemData item2 = grid.GetInventory().GetItem(0);
					if (((Humanoid)Player.m_localPlayer).GetInventory().CanAddItem(item2.m_dropPrefab, item2.m_stack))
					{
						_beehive.ResetLevel();
					}
					else
					{
						__runOriginal = false;
					}
				}
				if (__instance.m_dragItem != null)
				{
					__instance.SetupDragItem((ItemData)null, (Inventory)null, 0);
					__runOriginal = false;
				}
			}
			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())
				{
					_beehive.ResetLevel();
				}
				else
				{
					__runOriginal = false;
				}
			}
		}

		[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<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.2.3")]
	[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.2.3";

		public const string PluginGUID = "ZenDragon.ZenBeehive";

		protected override void Setup()
		{
			((ZenMod)this).ConfigSync += BeehivePatches.SetupPrefab;
		}

		protected override void TitleScene(bool isFirstBoot)
		{
		}

		protected override void WorldStart()
		{
		}

		protected override void Shutdown()
		{
		}
	}
}