Decompiled source of FermenterUtilities v1.1.3

FermenterUtilities.dll

Decompiled a month 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 BepInEx.Logging;
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("FermenterUtilities")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyCopyright("Copyright © 2025  KompjoeFriek")]
[assembly: AssemblyDescription("Valheim mod: Fermenter Utilities")]
[assembly: AssemblyFileVersion("1.1.3.0")]
[assembly: AssemblyInformationalVersion("1.1.3+653f60bc0b502cb830e9b3fe5f8c6bf49e0036e5")]
[assembly: AssemblyProduct("Fermenter Utilities")]
[assembly: AssemblyTitle("FermenterUtilities")]
[assembly: AssemblyMetadata("RepositoryUrl", "https://github.com/kompjoefriek/FermenterUtilities")]
[assembly: AssemblyVersion("1.1.3.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.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace FermenterUtilities
{
	[BepInPlugin("kompjoefriek.FermenterUtilities", "Fermenter Utilities", "1.1.3")]
	[HarmonyPatch]
	public class FermenterUtilitiesPlugin : BaseUnityPlugin
	{
		internal const string _modVersion = "1.1.3";

		internal const string _modDescription = "Fermenter Utilities";

		internal const string _modUid = "kompjoefriek.FermenterUtilities";

		internal static ManualLogSource Logger;

		private Harmony _harmony;

		private static ConfigEntry<bool> _configEnableMod;

		private static ConfigEntry<bool> _configEnableLogging;

		private static ConfigEntry<bool> _configShowPercentage;

		private static ConfigEntry<bool> _configShowColorPercentage;

		private static ConfigEntry<bool> _configShowTime;

		private static ConfigEntry<bool> _configCustomFermentTime;

		private static ConfigEntry<bool> _configNoCover;

		private static ConfigEntry<int> _configAmountOfDecimals;

		private static ConfigEntry<int> _configNewFermentTime;

		private static object _logObject;

		private static DateTime _lastLogTime;

		private void Awake()
		{
			//IL_0157: Unknown result type (might be due to invalid IL or missing references)
			//IL_015d: Expected O, but got Unknown
			//IL_0167: Unknown result type (might be due to invalid IL or missing references)
			//IL_016d: Expected O, but got Unknown
			Logger = ((BaseUnityPlugin)this).Logger;
			_configEnableMod = ((BaseUnityPlugin)this).Config.Bind<bool>("1 - Global", "Enable Mod", true, "Enable or disable this mod");
			_configEnableLogging = ((BaseUnityPlugin)this).Config.Bind<bool>("1 - Global", "Enable Mod Logging", false, "Enable or disable logging for this mod");
			_configShowPercentage = ((BaseUnityPlugin)this).Config.Bind<bool>("2 - Progress", "Show Percentage", true, "Shows the fermentation progress as a percentage when you hover over the fermenter");
			_configShowColorPercentage = ((BaseUnityPlugin)this).Config.Bind<bool>("2 - Progress", "Show Percentage Color", true, "Makes it so the percentage changes color depending on the progress");
			_configAmountOfDecimals = ((BaseUnityPlugin)this).Config.Bind<int>("2 - Progress", "Show Percentage Decimal Places", 2, "The amount of decimal places to show for the percentage");
			_configShowTime = ((BaseUnityPlugin)this).Config.Bind<bool>("2 - Progress", "Show Time", false, "Show the time when done");
			_configCustomFermentTime = ((BaseUnityPlugin)this).Config.Bind<bool>("3 - Time", "Custom Time", false, "Enables the custom time for fermentation");
			_configNewFermentTime = ((BaseUnityPlugin)this).Config.Bind<int>("3 - Time", "Fermentation Time", 5, "The amount of minutes fermentation takes (Default 40)");
			_configNoCover = ((BaseUnityPlugin)this).Config.Bind<bool>("4 - Cover", "Work Without Cover", false, "Allow the Fermenter to work without any cover");
			Dictionary<ConfigDefinition, string> entries = Traverse.Create((object)((BaseUnityPlugin)this).Config).Property("OrphanedEntries", (object[])null).GetValue<Dictionary<ConfigDefinition, string>>();
			if (entries != null)
			{
				ConfigDefinition val = new ConfigDefinition("2 - Progress", "Show Color Percentage");
				ConfigDefinition val2 = new ConfigDefinition("2 - Progress", "Amount of Decimal Places");
				string value;
				bool num = entries.TryGetValue(val, out value);
				string value2;
				bool flag = entries.TryGetValue(val2, out value2);
				bool flag2 = false;
				if (num)
				{
					((ConfigEntryBase)_configShowColorPercentage).SetSerializedValue(value);
					flag2 |= RemoveDeprecatedConfigDefinition(ref entries, val);
				}
				if (flag)
				{
					((ConfigEntryBase)_configAmountOfDecimals).SetSerializedValue(value2);
					flag2 |= RemoveDeprecatedConfigDefinition(ref entries, val2);
				}
				if (_configEnableLogging.Value)
				{
					foreach (KeyValuePair<ConfigDefinition, string> item in entries)
					{
						Logger.LogWarning((object)("Orphaned config: " + ((object)item.Key).ToString()));
					}
				}
				if (flag2)
				{
					((BaseUnityPlugin)this).Config.Save();
				}
			}
			_lastLogTime = DateTime.Now;
			if (_configEnableMod.Value)
			{
				_harmony = Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly(), (string)null);
			}
		}

		private void OnDestroy()
		{
			Harmony harmony = _harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
		}

		private static bool RemoveDeprecatedConfigDefinition(ref Dictionary<ConfigDefinition, string> entries, ConfigDefinition definition)
		{
			if (!entries.Remove(definition))
			{
				Logger.LogWarning((object)("Failed to remove deprecated config: " + ((object)definition).ToString()));
				return false;
			}
			return true;
		}

		private static string GetColorStringFromPercentage(double percentage)
		{
			if (!_configShowColorPercentage.Value)
			{
				return "white";
			}
			if (percentage >= 75.0)
			{
				return "green";
			}
			if (percentage >= 50.0)
			{
				return "yellow";
			}
			if (percentage >= 25.0)
			{
				return "orange";
			}
			return "red";
		}

		private static string GetValueAsColoredString(string color, double value)
		{
			return $"<color={color}>{value}%</color>";
		}

		private static string FormatSecondsAsString(double seconds)
		{
			int num = (int)(seconds / 3600.0);
			double num2 = seconds - (double)num * 3600.0;
			int num3 = (int)(num2 / 60.0);
			int num4 = (int)(num2 - (double)num3 * 60.0);
			if (num >= 1)
			{
				return $"{num:D2}:{num3:D2}:{num4:D2}";
			}
			return $"{num3:D2}:{num4:D2}";
		}

		private static void LogInfoThrottled(object obj, string message)
		{
			if (!_configEnableLogging.Value)
			{
				return;
			}
			if (object.Equals(_logObject, obj))
			{
				if ((DateTime.Now - _lastLogTime).TotalSeconds >= 1.0)
				{
					_logObject = obj;
					_lastLogTime = DateTime.Now;
					Logger.LogInfo((object)message);
				}
			}
			else
			{
				_logObject = obj;
				_lastLogTime = DateTime.Now;
				Logger.LogInfo((object)message);
			}
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(Fermenter), "Awake")]
		public static void FermenterAwake_Patch(Fermenter __instance)
		{
			if (!((Object)(object)__instance == (Object)null))
			{
				if (_configCustomFermentTime.Value)
				{
					__instance.m_fermentationDuration = _configNewFermentTime.Value * 60;
				}
				if (_configNoCover.Value)
				{
					Traverse.Create((object)__instance).Field("m_updateCoverTimer").SetValue((object)(-100f));
				}
			}
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(Fermenter), "GetHoverText")]
		public static string FermenterGetHoverText_Patch(string __result, Fermenter __instance)
		{
			if ((Object)(object)__instance == (Object)null)
			{
				return __result;
			}
			object value = ((object)__instance).GetType().GetNestedType("Status", BindingFlags.NonPublic).GetField("Fermenting")
				.GetValue(__instance);
			if (!Traverse.Create((object)__instance).Method("GetStatus", Array.Empty<object>()).GetValue<object>()
				.Equals(value))
			{
				return __result;
			}
			if (_configShowPercentage.Value)
			{
				double value2 = Traverse.Create((object)__instance).Method("GetFermentationTime", Array.Empty<object>()).GetValue<double>();
				if (value2 >= (double)__instance.m_fermentationDuration)
				{
					return __result;
				}
				double num = value2 / (double)__instance.m_fermentationDuration * 100.0;
				string text = GetValueAsColoredString(GetColorStringFromPercentage(num), Math.Round(num, _configAmountOfDecimals.Value, MidpointRounding.AwayFromZero));
				string text2 = "Fermenter percentage: " + num + ", time since start: " + value2 + ", fermentation duration: " + __instance.m_fermentationDuration;
				if (_configShowTime.Value)
				{
					double seconds = (double)__instance.m_fermentationDuration - value2;
					string text3 = FormatSecondsAsString(seconds);
					text = text + ", " + text3;
					text2 = text2 + "\nFermenter timeRemaining: " + seconds + " (seconds), formatted: " + text3;
				}
				LogInfoThrottled(__instance, text2);
				string oldValue = Localization.instance.Localize("$piece_fermenter_fermenting");
				return __result.Replace(oldValue, text);
			}
			if (_configShowTime.Value)
			{
				double value3 = Traverse.Create((object)__instance).Method("GetFermentationTime", Array.Empty<object>()).GetValue<double>();
				if (value3 >= (double)__instance.m_fermentationDuration)
				{
					return __result;
				}
				double seconds2 = (double)__instance.m_fermentationDuration - value3;
				string text4 = FormatSecondsAsString(seconds2);
				LogInfoThrottled(__instance, "Fermenter timeRemaining: " + seconds2 + " (seconds), formatted: " + text4);
				string oldValue2 = Localization.instance.Localize("$piece_fermenter_fermenting");
				return __result.Replace(oldValue2, text4);
			}
			return __result;
		}
	}
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "FermenterUtilities";

		public const string PLUGIN_NAME = "Fermenter Utilities";

		public const string PLUGIN_VERSION = "1.1.3";
	}
}