Decompiled source of ZenBeehive v1.0.3

plugins/ZenBeehive.dll

Decompiled 2 weeks ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Security.Permissions;
using System.Text;
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]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[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]
	[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]
	[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
{
	internal class BeehiveInventory : VirtualInventory
	{
		private Beehive? _beehive;

		private bool _allowAddItem;

		internal Beehive Beehive
		{
			get
			{
				if (!Object.op_Implicit((Object)(object)_beehive))
				{
					return _beehive = ((Component)this).GetComponent<Beehive>();
				}
				return _beehive;
			}
		}

		protected override Inventory Inventory { get; } = new Inventory("$piece_beehive", (Sprite)null, 1, 1);

		protected override ZNetView GetZNetView()
		{
			return Beehive.m_nview;
		}

		protected override bool IsAddItemValid(ItemData item)
		{
			return _allowAddItem;
		}

		protected override bool IsWarded()
		{
			return !Configs.IgnoreWards.Value;
		}

		protected override bool IsStackAllButtonVisible()
		{
			return false;
		}

		protected override bool OnHoldToStack()
		{
			if (!Configs.HoldToTakeAll.Value)
			{
				return false;
			}
			TakeAll();
			return true;
		}

		protected override void LoadInventory()
		{
			int honeyLevel = Beehive.GetHoneyLevel();
			GameObject honeyItemPrefab = Beehive.GetHoneyItemPrefab();
			if (honeyLevel > 0)
			{
				_allowAddItem = true;
				((VirtualInventory)this).Inventory.AddItem(honeyItemPrefab, honeyLevel);
				_allowAddItem = false;
			}
		}

		public void UpdateHoneyFromInventory()
		{
			SetHoneyLevel(((VirtualInventory)this).Inventory.NrOfItemsIncludingStacks());
		}

		private void SetHoneyLevel(int level)
		{
			if (level != Beehive.GetHoneyLevel())
			{
				Beehive.m_nview.ClaimOwnership();
				Beehive.ResetLevel();
				Beehive.IncreseLevel(level);
			}
		}

		public void TakeAll()
		{
			InventoryGui.instance.TakeAll();
			UpdateHoneyFromInventory();
		}

		protected override bool OnSelectedItemBefore(InventoryGui gui, InventoryGrid grid, ItemData? item, Modifier mod, Vector2i pos)
		{
			//IL_002f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0032: Invalid comparison between Unknown and I4
			//IL_0036: Unknown result type (might be due to invalid IL or missing references)
			//IL_005f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0066: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)grid == (Object)(object)gui.ContainerGrid)
			{
				if (gui.m_dragItem != null)
				{
					gui.SetupDragItem((ItemData)null, (Inventory)null, 0);
					return false;
				}
			}
			else if ((Object)(object)grid == (Object)(object)gui.m_playerGrid)
			{
				if ((int)mod == 2)
				{
					return false;
				}
				if ((int)mod == 0)
				{
					if (gui.m_dragItem == null)
					{
						return true;
					}
					if (gui.m_dragInventory == gui.m_playerGrid.GetInventory())
					{
						return true;
					}
					ItemData itemAt = grid.GetInventory().GetItemAt(pos.x, pos.y);
					if (itemAt == null)
					{
						return true;
					}
					if ((Object)(object)itemAt.m_dropPrefab == (Object)(object)Beehive.GetHoneyItemPrefab())
					{
						return true;
					}
					return false;
				}
			}
			return true;
		}

		protected override void OnSelectedItemAfter(InventoryGui gui, InventoryGrid grid, ItemData? item, Modifier mod, Vector2i pos)
		{
			UpdateHoneyFromInventory();
		}

		protected override void OnDropOutsideAfter(InventoryGui gui)
		{
			UpdateHoneyFromInventory();
		}
	}
	[HarmonyPatch]
	internal static class BeehiveUI
	{
		private static bool IsHoneyOpen(out BeehiveInventory beeInv)
		{
			return VirtualInventory.TryGetOpened<BeehiveInventory>(ref beeInv);
		}

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

		[HarmonyPrefix]
		[HarmonyPatch(typeof(Beehive), "Extract")]
		private static bool Beehive_Extract(Beehive __instance)
		{
			return !VirtualInventory.TryRequestOpenFrom(((Component)__instance).gameObject);
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(Humanoid), "UseItem")]
		private static void Humanoid_UseItem()
		{
			if (IsHoneyOpen(out BeehiveInventory beeInv))
			{
				beeInv.UpdateHoneyFromInventory();
			}
		}

		[HarmonyReversePatch(/*Could not decode attribute arguments.*/)]
		[HarmonyPatch(typeof(InventoryGui), "OnTakeAll")]
		public static void TakeAll(this InventoryGui __instance)
		{
			throw new NotImplementedException("Placeholder for original method");
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(InventoryGui), "OnTakeAll")]
		private static void InventoryGui_OnTakeAll_Postfix(InventoryGui __instance)
		{
			if (IsHoneyOpen(out BeehiveInventory beeInv))
			{
				beeInv.UpdateHoneyFromInventory();
			}
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(InventoryGrid), "UpdateGui")]
		private static void InventoryGrid_UpdateGui(InventoryGrid __instance)
		{
			if (IsHoneyOpen(out BeehiveInventory beeInv))
			{
				Beehive beehive = beeInv.Beehive;
				Inventory inventory = __instance.GetInventory();
				if (((VirtualInventory)beeInv).IsInventory(inventory) && inventory.NrOfItems() != 0)
				{
					ItemData item = __instance.GetInventory().GetItem(0);
					__instance.m_elements[0].m_amount.text = $"{item.m_stack}/{beehive.m_maxHoney}";
				}
			}
		}

		[HarmonyTranspiler]
		[HarmonyPatch(typeof(Beehive), "Interact")]
		[HarmonyPriority(100)]
		private static IEnumerable<CodeInstruction> Beehive_Interact(IEnumerable<CodeInstruction> codes)
		{
			MethodInfo methodInfo = AccessTools.DeclaredMethod(typeof(PrivateArea), "CheckAccess", (Type[])null, (Type[])null);
			Func<Vector3, float, bool, bool, bool> func = InterceptCheckAccess;
			return Transpilers.MethodReplacer(codes, (MethodBase)methodInfo, (MethodBase)func.Method);
			static bool InterceptCheckAccess(Vector3 point, float radius, bool flash, bool wardCheck)
			{
				//IL_000c: Unknown result type (might be due to invalid IL or missing references)
				if (!Configs.IgnoreWards.Value)
				{
					return PrivateArea.CheckAccess(point, radius, flash, wardCheck);
				}
				return true;
			}
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(Beehive), "IncreseLevel")]
		private static void Beehive_IncreaseLevel(Beehive __instance)
		{
			if (IsHoneyOpen(out BeehiveInventory beeInv) && (Object)(object)beeInv.Beehive == (Object)(object)__instance)
			{
				((VirtualInventory)beeInv).RefreshInventory();
			}
		}
	}
	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 take all the honey at once.");

		public static readonly ConfigEntry<int> MaxHoney = Config.Define<int>(true, "Beehive", "Max Capacity", 0, Config.AcceptRange<int>(0, 999), "Max honey that a beehive can hold. Logout for changes to take effect. (Vanilla: 4)\r\nSet to 0 to disable this config.\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.\r\n[restart game for changes to take effect]");

		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. \r\n[logout for changes to take effect]");

		public static readonly ConfigEntry<int> SecPerUnit = Config.Define<int>(true, "Beehive", "Seconds Per Unit", 0, Config.AcceptRange<int>(0, 10000), "The number of seconds it takes to grow one unit of honey. (Vanilla: 1200)\r\nSet to 0 to disable this config.\r\n[restart game for changes to take effect]");

		public static readonly ConfigEntry<bool> IgnoreWards = Config.Define<bool>(true, "Beehive", "Ignore Wards", true, "Ignore warded areas (vanilla: false)\r\nIt's just a honey box that generates new honey over time, not a pot of gold.\r\nMost of the time it just goes unharvested since people are busy doing other things.\r\nTrust your neighbors, or share extra, or build a wall and gate to guard your honey.");
	}
	[Disable(new Type[] { typeof(Beehive) })]
	[BepInPlugin("ZenDragon.ZenBeehive", "ZenBeehive", "1.0.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 = "1.0.3";

		public const string PluginGUID = "ZenDragon.ZenBeehive";

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

		protected override void TitleScene(bool isFirstBoot)
		{
		}

		protected override void WorldStart()
		{
		}

		protected override void Shutdown()
		{
		}

		protected override void HotRestore()
		{
			Beehive[] array = Object.FindObjectsByType<Beehive>((FindObjectsSortMode)0);
			for (int i = 0; i < array.Length; i++)
			{
				PrefabInit.Init(array[i]);
			}
		}

		protected override HelpInfo? GetHelpInfo()
		{
			//IL_0031: Unknown result type (might be due to invalid IL or missing references)
			//IL_0037: Expected O, but got Unknown
			StringBuilder stringBuilder = new StringBuilder();
			if (Configs.HoldToTakeAll.Value)
			{
				stringBuilder.AppendLine(HelpInfo.Format<bool>(Configs.HoldToTakeAll, false, true, true));
			}
			return new HelpInfo("Beehive", stringBuilder.ToString());
		}
	}
	internal static class PrefabInit
	{
		public static void SetupPrefab()
		{
			Logging<Plugin>.Info("Setting up beehive prefab container...", 0);
			Init(GlobalStatic.GetPrefab<Beehive>("piece_beehive", false));
		}

		public static void Init(Beehive beehive)
		{
			BeehiveInventory orAddComponent = ExposedGameObjectExtension.GetOrAddComponent<BeehiveInventory>(((Component)beehive).gameObject);
			((ZenMod<Plugin>)(object)ZenMod<Plugin>.Instance).HotLoad.Track((Object)(object)orAddComponent);
			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
			{
				beehive.m_honeyItem = itemPrefab.GetComponent<ItemDrop>();
			}
			if (Configs.MaxHoney.Value > 0)
			{
				beehive.m_maxHoney = Mathf.Min(beehive.m_honeyItem.m_itemData.m_shared.m_maxStackSize, Configs.MaxHoney.Value);
			}
			if (Configs.SecPerUnit.Value > 0)
			{
				beehive.m_secPerUnit = Configs.SecPerUnit.Value;
			}
		}
	}
}