Decompiled source of FrizzQOL Fermenter Control v0.1.0

FrizzQOL.FermenterControl.dll

Decompiled a day ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
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.FermenterControl")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+0d9c352d8291c477c5863a7efae08634f1dfe792")]
[assembly: AssemblyProduct("FrizzQOL.FermenterControl")]
[assembly: AssemblyTitle("FrizzQOL.FermenterControl")]
[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;
		}
	}
}
namespace FermenterControl
{
	public static class FermentTime
	{
		public const string FallbackColor = "orange";

		public static float ResolveSeconds(float configMinutes, float vanillaSeconds)
		{
			if (configMinutes <= 0f)
			{
				return vanillaSeconds;
			}
			return configMinutes * 60f;
		}

		public static string FormatRemaining(float seconds)
		{
			if (seconds <= 0f)
			{
				return "0m";
			}
			int num = (int)Math.Ceiling((double)seconds / 60.0);
			int num2 = num / 60;
			int num3 = num % 60;
			if (num2 > 0)
			{
				return num2.ToString(CultureInfo.InvariantCulture) + "h " + num3.ToString(CultureInfo.InvariantCulture) + "m";
			}
			return num.ToString(CultureInfo.InvariantCulture) + "m";
		}

		public static string SanitizeColor(string color)
		{
			if (string.IsNullOrWhiteSpace(color))
			{
				return "orange";
			}
			string text = color.Trim();
			if (text.Length == 0 || text.Length > 32)
			{
				return "orange";
			}
			foreach (char c in text)
			{
				if ((c < 'a' || c > 'z') && (c < 'A' || c > 'Z') && (c < '0' || c > '9') && c != '#')
				{
					return "orange";
				}
			}
			return text;
		}

		public static string WithRemaining(string hover, bool show, float elapsedSeconds, float durationSeconds, string color)
		{
			if (!show || string.IsNullOrEmpty(hover))
			{
				return hover ?? "";
			}
			float num = durationSeconds - elapsedSeconds;
			if (num <= 0f)
			{
				return hover;
			}
			string text = SanitizeColor(color);
			return hover + "\n<color=" + text + ">" + FormatRemaining(num) + "</color>";
		}
	}
	internal static class DurationApplier
	{
		private static readonly Dictionary<int, float> VanillaSeconds = new Dictionary<int, float>();

		internal static void Apply(Fermenter fermenter)
		{
			if (!((Object)(object)fermenter == (Object)null))
			{
				int instanceID = ((Object)fermenter).GetInstanceID();
				if (!VanillaSeconds.ContainsKey(instanceID))
				{
					VanillaSeconds[instanceID] = fermenter.m_fermentationDuration;
				}
				fermenter.m_fermentationDuration = FermentTime.ResolveSeconds(Plugin.ConfiguredMinutes(), VanillaSeconds[instanceID]);
			}
		}

		internal static void ApplyAll()
		{
			Fermenter[] array = Object.FindObjectsByType<Fermenter>((FindObjectsSortMode)0);
			for (int i = 0; i < array.Length; i++)
			{
				Apply(array[i]);
			}
		}
	}
	[HarmonyPatch(typeof(Fermenter), "Awake")]
	internal static class FermenterAwakePatch
	{
		private static void Postfix(Fermenter __instance)
		{
			DurationApplier.Apply(__instance);
		}
	}
	[HarmonyPatch(typeof(Fermenter), "GetHoverText")]
	internal static class FermenterHoverPatch
	{
		private static readonly MethodInfo StatusMethod = AccessTools.Method(typeof(Fermenter), "GetStatus", (Type[])null, (Type[])null);

		private static readonly MethodInfo ElapsedMethod = AccessTools.Method(typeof(Fermenter), "GetFermentationTime", (Type[])null, (Type[])null);

		private static bool _loggedHover;

		private static void Postfix(Fermenter __instance, ref string __result)
		{
			if ((Object)(object)__instance == (Object)null || string.IsNullOrEmpty(__result))
			{
				return;
			}
			try
			{
				if (IsBrewing(__instance))
				{
					float elapsedSeconds = Elapsed(__instance);
					__result = FermentTime.WithRemaining(__result, show: true, elapsedSeconds, __instance.m_fermentationDuration, Plugin.ConfiguredColor());
				}
			}
			catch (Exception ex)
			{
				if (!_loggedHover)
				{
					_loggedHover = true;
					Plugin.LogWarning("Could not add the fermenter time: " + ex.Message);
				}
			}
		}

		private static bool IsBrewing(Fermenter fermenter)
		{
			if (StatusMethod == null)
			{
				return false;
			}
			object obj = StatusMethod.Invoke(fermenter, null);
			if (obj == null)
			{
				return false;
			}
			string text = obj.ToString();
			if (!(text == "Fermenting"))
			{
				return text == "Exposed";
			}
			return true;
		}

		private static float Elapsed(Fermenter fermenter)
		{
			if (ElapsedMethod == null)
			{
				return 0f;
			}
			object obj = ElapsedMethod.Invoke(fermenter, null);
			if (obj is double num)
			{
				return (float)num;
			}
			if (obj is float)
			{
				return (float)obj;
			}
			return 0f;
		}
	}
	[BepInPlugin("com.frizzqol.fermentercontrol", "FrizzQOL Fermenter Control", "0.1.0")]
	public class Plugin : BaseUnityPlugin
	{
		public const string PluginGuid = "com.frizzqol.fermentercontrol";

		public const string PluginName = "FrizzQOL Fermenter Control";

		public const string PluginVersion = "0.1.0";

		internal static ConfigEntry<float> Minutes;

		internal static ConfigEntry<string> TimeColor;

		private static bool _loggedMinutes;

		private static string _loggedColor;

		private Harmony _harmony;

		internal static Plugin Instance { get; private set; }

		private void Awake()
		{
			//IL_00a6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b0: Expected O, but got Unknown
			Instance = this;
			Minutes = ((BaseUnityPlugin)this).Config.Bind<float>("Time", "Minutes", 0f, "Real minutes until a batch is ready. 0 keeps the normal Valheim time. Below 0 does the same.");
			TimeColor = ((BaseUnityPlugin)this).Config.Bind<string>("Time", "TimeColor", "orange", "Color of the remaining time when you look at a fermenter. A Unity color name, or #RRGGBB.");
			Minutes.SettingChanged += delegate
			{
				DurationApplier.ApplyAll();
			};
			TimeColor.SettingChanged += delegate
			{
				NoteColor();
			};
			_harmony = new Harmony("com.frizzqol.fermentercontrol");
			try
			{
				_harmony.PatchAll();
				((BaseUnityPlugin)this).Logger.LogInfo((object)"FrizzQOL Fermenter Control 0.1.0 loaded");
			}
			catch (Exception ex)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)("Harmony patch failed: " + ex.Message));
			}
		}

		internal static float ConfiguredMinutes()
		{
			float num = ((Minutes != null) ? Minutes.Value : 0f);
			if (num < 0f && !_loggedMinutes)
			{
				_loggedMinutes = true;
				LogWarning("Minutes below 0 keeps the normal fermenter time.");
			}
			return num;
		}

		internal static string ConfiguredColor()
		{
			string text = ((TimeColor != null) ? TimeColor.Value : "orange");
			string text2 = FermentTime.SanitizeColor(text);
			if (text2 != text && _loggedColor != text)
			{
				_loggedColor = text;
				LogWarning("TimeColor '" + text + "' is not a color name or #RRGGBB. Using " + text2 + ".");
			}
			return text2;
		}

		private static void NoteColor()
		{
			ConfiguredColor();
		}

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