Decompiled source of OhBeehive v1.0.1

OhBeehive.dll

Decompiled a month ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
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: AssemblyTitle("OhBeehive")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("OhBeehive")]
[assembly: AssemblyCopyright("Copyright © 2023")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("da885ebe-3d04-4039-8a85-58847b91c3a0")]
[assembly: AssemblyFileVersion("1.0.1")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.1.0")]
[module: UnverifiableCode]
[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.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace OhBeehive
{
	public static class PluginConfig
	{
		public static ConfigEntry<bool> IsModEnabled { get; private set; }

		public static ConfigEntry<float> AutoExtractRange { get; private set; }

		public static void BindConfig(ConfigFile config)
		{
			//IL_0044: Unknown result type (might be due to invalid IL or missing references)
			//IL_004e: Expected O, but got Unknown
			IsModEnabled = config.Bind<bool>("_Global", "IsModEnabled", true, "Enable or Disable this mod");
			AutoExtractRange = config.Bind<float>("Extract", "AutoExtractRange", 1f, new ConfigDescription("Range to extract Honey.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0.875f, 3f), Array.Empty<object>()));
		}
	}
	[BepInPlugin("EardwulfDoesMods.valheim.OhBeehive", "OhBeehive", "1.0.1")]
	public class OhBeehive : BaseUnityPlugin
	{
		public const string PluginGuid = "EardwulfDoesMods.valheim.OhBeehive";

		public const string PluginName = "OhBeehive";

		public const string PluginVersion = "1.0.1";

		public static ManualLogSource _logger;

		public void Awake()
		{
			_logger = ((BaseUnityPlugin)this).Logger;
			PluginConfig.BindConfig(((BaseUnityPlugin)this).Config);
			Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly(), "EardwulfDoesMods.valheim.OhBeehive");
		}

		public static void LogInfo(object obj)
		{
			_logger.LogInfo((object)$"[{DateTime.Now.ToString(DateTimeFormatInfo.InvariantInfo)}] {obj}");
		}

		public static void LogError(object obj)
		{
			_logger.LogError((object)$"[{DateTime.Now.ToString(DateTimeFormatInfo.InvariantInfo)}] {obj}");
		}
	}
	public sealed class BeehiveMonitor : MonoBehaviour
	{
		private Beehive _beehive;

		private void Awake()
		{
			_beehive = ((Component)this).GetComponent<Beehive>();
		}

		private void OnDestroy()
		{
			if ((Object)(object)HoneyExtractionManager.Instance != (Object)null)
			{
				HoneyExtractionManager.Instance.UnregisterBeehive(_beehive);
				OhBeehive._logger.LogInfo((object)"A Beehive has unregistered via its monitor.");
			}
		}
	}
	[HarmonyPatch(typeof(Beehive))]
	internal static class BeehivePatch
	{
		[HarmonyPostfix]
		[HarmonyPatch("Awake")]
		private static void AwakePostFix(Beehive __instance)
		{
			if (PluginConfig.IsModEnabled.Value)
			{
				((Component)__instance).gameObject.AddComponent<BeehiveMonitor>();
				HoneyExtractionManager.Instance.RegisterBeehive(__instance);
				OhBeehive._logger.LogInfo((object)"A Beehive has registered itself to the manager.");
			}
		}
	}
	public class HoneyExtractionManager : MonoBehaviour
	{
		private static HoneyExtractionManager _instance;

		private List<Beehive> _allBeehives = new List<Beehive>();

		private static readonly object _lock = new object();

		public static HoneyExtractionManager Instance
		{
			get
			{
				//IL_004b: Unknown result type (might be due to invalid IL or missing references)
				lock (_lock)
				{
					if ((Object)(object)_instance != (Object)null)
					{
						return _instance;
					}
					_instance = Object.FindObjectOfType<HoneyExtractionManager>();
					if ((Object)(object)_instance == (Object)null)
					{
						ZLog.Log((object)"HoneyExtractionManager instance is required. Creating a new one.");
						_instance = new GameObject("HoneyExtractionManager").AddComponent<HoneyExtractionManager>();
					}
					return _instance;
				}
			}
		}

		private void Awake()
		{
			if ((Object)(object)_instance == (Object)null)
			{
				_instance = this;
				Object.DontDestroyOnLoad((Object)(object)((Component)this).gameObject);
			}
			else if ((Object)(object)_instance != (Object)(object)this)
			{
				ZLog.LogWarning((object)"Duplicate HoneyExtractionManager detected. Destroying the duplicate.");
				Object.Destroy((Object)(object)((Component)this).gameObject);
			}
		}

		public void RegisterBeehive(Beehive beehive)
		{
			if (!_allBeehives.Contains(beehive))
			{
				_allBeehives.Add(beehive);
			}
		}

		public void UnregisterBeehive(Beehive beehive)
		{
			_allBeehives.Remove(beehive);
		}

		private void Start()
		{
			if (PluginConfig.IsModEnabled.Value)
			{
				((MonoBehaviour)this).InvokeRepeating("LootBeehive", 1f, 1f);
			}
		}

		public void LootBeehive()
		{
			//IL_0048: Unknown result type (might be due to invalid IL or missing references)
			//IL_0057: Unknown result type (might be due to invalid IL or missing references)
			//IL_007b: Unknown result type (might be due to invalid IL or missing references)
			if (!PluginConfig.IsModEnabled.Value || !Object.op_Implicit((Object)(object)Player.m_localPlayer))
			{
				return;
			}
			foreach (Beehive allBeehive in _allBeehives)
			{
				if (((Component)allBeehive).gameObject.activeInHierarchy && !(Vector3.Distance(((Component)allBeehive).transform.position, ((Component)Player.m_localPlayer).transform.position) > PluginConfig.AutoExtractRange.Value) && allBeehive.GetHoneyLevel() != 0)
				{
					if (!PrivateArea.CheckAccess(((Component)allBeehive).transform.position, 0f, true, false))
					{
						((Terminal)Chat.m_instance).AddString("You are not on the ward for this area.");
						OhBeehive._logger.LogInfo((object)"You are not in the ward for this area.");
					}
					else
					{
						allBeehive.Extract();
						OhBeehive._logger.LogInfo((object)"Attempting Honey Extraction");
					}
				}
			}
		}
	}
}