Decompiled source of FrizzQOL BeeNiceHive v0.1.0

FrizzQOL.BeeNiceHive.dll

Decompiled 12 hours ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyCompany("FrizzQOL.BeeNiceHive")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+0081fcf38e5957be6063f37129265ae949f2fd3c")]
[assembly: AssemblyProduct("FrizzQOL.BeeNiceHive")]
[assembly: AssemblyTitle("FrizzQOL.BeeNiceHive")]
[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;
		}
	}
}
public static class HiveRules
{
	public const float VanillaSecondsPerHoney = 1200f;

	public const int VanillaMaxHoney = 4;

	public static float ResolveSecondsPerHoney(float minutes, float vanillaSeconds)
	{
		float result = ((vanillaSeconds > 0f) ? vanillaSeconds : 1200f);
		if (float.IsNaN(minutes) || minutes <= 0f)
		{
			return result;
		}
		return minutes * 60f;
	}

	public static int ResolveMaxHoney(int honey, int vanillaMax)
	{
		int result = ((vanillaMax > 0) ? vanillaMax : 4);
		if (honey <= 0)
		{
			return result;
		}
		return honey;
	}

	public static bool IsFull(int level, int maxHoney, float secondsPerHoney, double progressSeconds)
	{
		Advance(level, maxHoney, secondsPerHoney, progressSeconds, out var levelAfter, out var _);
		if (maxHoney > 0)
		{
			return levelAfter >= maxHoney;
		}
		return true;
	}

	public static double RemainingSeconds(int level, int maxHoney, float secondsPerHoney, double progressSeconds)
	{
		Advance(level, maxHoney, secondsPerHoney, progressSeconds, out var levelAfter, out var progressAfter);
		if (maxHoney <= 0 || secondsPerHoney <= 0f || levelAfter >= maxHoney)
		{
			return 0.0;
		}
		int num = maxHoney - levelAfter;
		double num2 = (double)secondsPerHoney - progressAfter;
		if (num2 < 0.0)
		{
			num2 = 0.0;
		}
		return (double)(num - 1) * (double)secondsPerHoney + num2;
	}

	public static string StatusText(int level, int maxHoney, float secondsPerHoney, double progressSeconds)
	{
		if (IsFull(level, maxHoney, secondsPerHoney, progressSeconds))
		{
			return "Full";
		}
		return FormatTime(RemainingSeconds(level, maxHoney, secondsPerHoney, progressSeconds));
	}

	public static string FormatTime(double seconds)
	{
		if (double.IsNaN(seconds) || seconds <= 0.0)
		{
			return "0s";
		}
		int num = (int)Math.Ceiling(seconds);
		if (num < 1)
		{
			num = 1;
		}
		int num2 = num / 3600;
		int num3 = num % 3600 / 60;
		int num4 = num % 60;
		if (num2 > 0 && num3 > 0)
		{
			return num2 + "h " + num3 + "m";
		}
		if (num2 > 0)
		{
			return num2 + "h";
		}
		if (num3 > 0 && num4 > 0)
		{
			return num3 + "m " + num4 + "s";
		}
		if (num3 > 0)
		{
			return num3 + "m";
		}
		return num4 + "s";
	}

	private static void Advance(int level, int maxHoney, float secondsPerHoney, double progressSeconds, out int levelAfter, out double progressAfter)
	{
		levelAfter = ((level >= 0) ? level : 0);
		progressAfter = ((progressSeconds < 0.0 || double.IsNaN(progressSeconds)) ? 0.0 : progressSeconds);
		if (maxHoney <= 0 || secondsPerHoney <= 0f || float.IsNaN(secondsPerHoney))
		{
			return;
		}
		if (levelAfter >= maxHoney)
		{
			progressAfter = 0.0;
		}
		else if (!(progressAfter <= (double)secondsPerHoney))
		{
			int num = (int)(progressAfter / (double)secondsPerHoney);
			if (num < 1)
			{
				num = 1;
			}
			int num2 = maxHoney - levelAfter;
			if (num > num2)
			{
				num = num2;
			}
			levelAfter += num;
			progressAfter = 0.0;
		}
	}
}
namespace BeeNiceHive
{
	internal static class HiveApplier
	{
		private static bool _captured;

		private static float _vanillaSeconds = 1200f;

		private static int _vanillaMax = 4;

		public static void Apply(Beehive hive)
		{
			if (!((Object)(object)hive == (Object)null))
			{
				CaptureVanilla(hive);
				float minutes = ((Plugin.Minutes != null) ? Plugin.Minutes.Value : 0f);
				int honey = ((Plugin.Honey != null) ? Plugin.Honey.Value : 0);
				hive.m_secPerUnit = HiveRules.ResolveSecondsPerHoney(minutes, _vanillaSeconds);
				hive.m_maxHoney = HiveRules.ResolveMaxHoney(honey, _vanillaMax);
			}
		}

		public static void ApplyAll()
		{
			ZNetScene instance = ZNetScene.instance;
			if ((Object)(object)instance != (Object)null)
			{
				ApplyPrefabs(instance);
			}
			Beehive[] array = Object.FindObjectsByType<Beehive>((FindObjectsSortMode)0);
			if (array != null)
			{
				for (int i = 0; i < array.Length; i++)
				{
					Apply(array[i]);
				}
			}
		}

		public static void ApplyPrefabs(ZNetScene scene)
		{
			if ((Object)(object)scene == (Object)null)
			{
				return;
			}
			if (!(AccessTools.Field(typeof(ZNetScene), "m_prefabs")?.GetValue(scene) is List<GameObject> list))
			{
				Plugin.LogWarning("ZNetScene prefab list missing. Hive settings apply when a hive wakes.");
				return;
			}
			for (int i = 0; i < list.Count; i++)
			{
				GameObject val = list[i];
				if (!((Object)(object)val == (Object)null))
				{
					Beehive component = val.GetComponent<Beehive>();
					if ((Object)(object)component != (Object)null)
					{
						Apply(component);
					}
				}
			}
		}

		private static void CaptureVanilla(Beehive hive)
		{
			if (!_captured)
			{
				if (hive.m_secPerUnit > 0f)
				{
					_vanillaSeconds = hive.m_secPerUnit;
				}
				if (hive.m_maxHoney > 0)
				{
					_vanillaMax = hive.m_maxHoney;
				}
				_captured = true;
			}
		}
	}
	internal static class HiveStatus
	{
		public static string Line(Beehive hive)
		{
			if ((Object)(object)hive == (Object)null)
			{
				return string.Empty;
			}
			int level = 0;
			double progressSeconds = 0.0;
			ZNetView component = ((Component)hive).GetComponent<ZNetView>();
			if ((Object)(object)component != (Object)null && component.IsValid())
			{
				ZDO zDO = component.GetZDO();
				if (zDO != null)
				{
					level = zDO.GetInt(ZDOVars.s_level, 0);
					progressSeconds = (double)zDO.GetFloat(ZDOVars.s_product, 0f) + ElapsedSeconds(zDO);
				}
			}
			string text = HiveRules.StatusText(level, hive.m_maxHoney, hive.m_secPerUnit, progressSeconds);
			return "\n<color=yellow>" + text + "</color>";
		}

		private static double ElapsedSeconds(ZDO zdo)
		{
			if ((Object)(object)ZNet.instance == (Object)null)
			{
				return 0.0;
			}
			DateTime time = ZNet.instance.GetTime();
			long num = zdo.GetLong(ZDOVars.s_lastTime, time.Ticks);
			if (num > 0)
			{
				DateTime maxValue = DateTime.MaxValue;
				if (num <= maxValue.Ticks)
				{
					double totalSeconds = (time - new DateTime(num)).TotalSeconds;
					if (double.IsNaN(totalSeconds) || totalSeconds < 0.0)
					{
						return 0.0;
					}
					return totalSeconds;
				}
			}
			return 0.0;
		}
	}
	[HarmonyPatch(typeof(Beehive), "Awake")]
	internal static class BeehiveAwakePatch
	{
		private static void Postfix(Beehive __instance)
		{
			HiveApplier.Apply(__instance);
		}
	}
	[HarmonyPatch(typeof(ZNetScene), "Awake")]
	internal static class ZNetSceneAwakePatch
	{
		private static void Postfix(ZNetScene __instance)
		{
			HiveApplier.ApplyPrefabs(__instance);
		}
	}
	[HarmonyPatch(typeof(Beehive), "GetHoverText")]
	internal static class BeehiveHoverPatch
	{
		private static void Postfix(Beehive __instance, ref string __result)
		{
			if (!((Object)(object)__instance == (Object)null) && !string.IsNullOrEmpty(__result))
			{
				__result += HiveStatus.Line(__instance);
			}
		}
	}
	[BepInPlugin("com.vhmod.beenicehive", "FrizzQOL.BeeNiceHive", "0.1.0")]
	public class Plugin : BaseUnityPlugin
	{
		public const string PluginGuid = "com.vhmod.beenicehive";

		public const string PluginName = "FrizzQOL.BeeNiceHive";

		public const string PluginVersion = "0.1.0";

		internal static Plugin Instance { get; private set; }

		internal static ConfigEntry<float> Minutes { get; private set; }

		internal static ConfigEntry<int> Honey { get; private set; }

		private void Awake()
		{
			//IL_0079: Unknown result type (might be due to invalid IL or missing references)
			Instance = this;
			Minutes = ((BaseUnityPlugin)this).Config.Bind<float>("General", "Minutes", 0f, "Real minutes for each honey. 0 keeps the normal time. Below 0 does the same.");
			Honey = ((BaseUnityPlugin)this).Config.Bind<int>("General", "Honey", 0, "How many honey a full hive holds. 0 keeps the normal amount. Below 0 does the same.");
			((BaseUnityPlugin)this).Config.SettingChanged += delegate
			{
				HiveApplier.ApplyAll();
			};
			new Harmony("com.vhmod.beenicehive").PatchAll();
			((BaseUnityPlugin)this).Logger.LogInfo((object)"FrizzQOL.BeeNiceHive 0.1.0 loaded");
		}

		internal static void LogInfo(string message)
		{
			if ((Object)(object)Instance != (Object)null)
			{
				((BaseUnityPlugin)Instance).Logger.LogInfo((object)message);
			}
		}

		internal static void LogWarning(string message)
		{
			if ((Object)(object)Instance != (Object)null)
			{
				((BaseUnityPlugin)Instance).Logger.LogWarning((object)message);
			}
		}
	}
}