Decompiled source of FermenterTimer Ymiron v1.0.1

FermenterTimer.dll

Decompiled 2 hours ago
using System;
using System.Diagnostics;
using System.IO;
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(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("FermenterTimer")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.1.0")]
[assembly: AssemblyInformationalVersion("1.0.1")]
[assembly: AssemblyProduct("FermenterTimer")]
[assembly: AssemblyTitle("FermenterTimer")]
[assembly: AssemblyVersion("1.0.1.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 FermenterTimer
{
	internal static class ModConfig
	{
		internal static ConfigEntry<string> Label;

		internal static ConfigEntry<bool> ShowPercentage;

		internal static ConfigEntry<string> Color;

		internal static void Bind(ConfigFile cfg)
		{
			Label = cfg.Bind<string>("1 - Display", "Label", "Ready in", "Text shown before the remaining time (e.g. \"Pronto tra\" for Italian).");
			ShowPercentage = cfg.Bind<bool>("1 - Display", "ShowPercentage", true, "Also show the fermentation progress as a percentage.");
			Color = cfg.Bind<string>("1 - Display", "Color", "#FFD966", "Color of the timer line: a hex code (#RRGGBB) or a Unity color name (yellow, orange, white...).");
		}
	}
	[HarmonyPatch(typeof(Fermenter), "GetHoverText")]
	internal static class FermenterHoverPatch
	{
		private static readonly FieldRef<Fermenter, ZNetView> NView = AccessTools.FieldRefAccess<Fermenter, ZNetView>("m_nview");

		private static readonly FieldRef<Fermenter, bool> HasRoof = AccessTools.FieldRefAccess<Fermenter, bool>("m_hasRoof");

		private static readonly FieldRef<Fermenter, bool> Exposed = AccessTools.FieldRefAccess<Fermenter, bool>("m_exposed");

		private static readonly Func<Fermenter, double> GetFermentationTime = AccessTools.MethodDelegate<Func<Fermenter, double>>(AccessTools.Method(typeof(Fermenter), "GetFermentationTime", (Type[])null, (Type[])null), (object)null, true);

		private static void Postfix(Fermenter __instance, ref string __result)
		{
			//IL_0055: Unknown result type (might be due to invalid IL or missing references)
			ZNetView val = NView.Invoke(__instance);
			if ((Object)(object)val == (Object)null || val.GetZDO() == null || val.GetZDO().GetInt(ZDOVars.s_content, 0) == 0 || !HasRoof.Invoke(__instance) || Exposed.Invoke(__instance) || !PrivateArea.CheckAccess(((Component)__instance).transform.position, 0f, false, false))
			{
				return;
			}
			double num = GetFermentationTime(__instance);
			double num2 = __instance.m_fermentationDuration;
			if (!(num < 0.0) && !(num > num2))
			{
				string text = ModConfig.Label.Value + " " + FormatTime(num2 - num);
				if (ModConfig.ShowPercentage.Value && num2 > 0.0)
				{
					text += $" ({(int)(num / num2 * 100.0)}%)";
				}
				__result = __result + "\n<color=" + ModConfig.Color.Value + ">" + text + "</color>";
			}
		}

		private static string FormatTime(double seconds)
		{
			TimeSpan timeSpan = TimeSpan.FromSeconds(Math.Ceiling(seconds));
			if (!(timeSpan.TotalHours >= 1.0))
			{
				return $"{timeSpan.Minutes}:{timeSpan.Seconds:00}";
			}
			return $"{(int)timeSpan.TotalHours}:{timeSpan.Minutes:00}:{timeSpan.Seconds:00}";
		}
	}
	[BepInPlugin("ymiron.valheim.fermentertimer", "FermenterTimer-Ymiron", "1.0.1")]
	[BepInProcess("valheim.exe")]
	public class FermenterTimerPlugin : BaseUnityPlugin
	{
		public const string PluginGuid = "ymiron.valheim.fermentertimer";

		public const string PluginName = "FermenterTimer-Ymiron";

		public const string PluginVersion = "1.0.1";

		private Harmony _harmony;

		private FileSystemWatcher _watcher;

		private volatile bool _configChanged;

		internal static ManualLogSource Log { get; private set; }

		private void Awake()
		{
			//IL_001c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0026: Expected O, but got Unknown
			Log = ((BaseUnityPlugin)this).Logger;
			ModConfig.Bind(((BaseUnityPlugin)this).Config);
			_harmony = new Harmony("ymiron.valheim.fermentertimer");
			_harmony.PatchAll(typeof(FermenterTimerPlugin).Assembly);
			WatchConfigFile();
			Log.LogInfo((object)"FermenterTimer-Ymiron 1.0.1 loaded.");
		}

		private void WatchConfigFile()
		{
			_watcher = new FileSystemWatcher(Path.GetDirectoryName(((BaseUnityPlugin)this).Config.ConfigFilePath), Path.GetFileName(((BaseUnityPlugin)this).Config.ConfigFilePath))
			{
				NotifyFilter = (NotifyFilters.FileName | NotifyFilters.Size | NotifyFilters.LastWrite),
				IncludeSubdirectories = false
			};
			_watcher.Changed += delegate
			{
				_configChanged = true;
			};
			_watcher.Created += delegate
			{
				_configChanged = true;
			};
			_watcher.Renamed += delegate
			{
				_configChanged = true;
			};
			_watcher.EnableRaisingEvents = true;
		}

		private void Update()
		{
			if (!_configChanged)
			{
				return;
			}
			_configChanged = false;
			try
			{
				((BaseUnityPlugin)this).Config.Reload();
				Log.LogInfo((object)"Config reloaded.");
			}
			catch (IOException)
			{
				_configChanged = true;
			}
		}

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