Decompiled source of GrowTime v1.0.1

plugins/GrowTime.dll

Decompiled 2 weeks 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 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("GrowTime")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.1.0")]
[assembly: AssemblyInformationalVersion("1.0.1+0b1c869c04853479ea1705b9d4e4451f1b246baf")]
[assembly: AssemblyProduct("GrowTime")]
[assembly: AssemblyTitle("GrowTime")]
[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 GrowTime
{
	[HarmonyPatch(typeof(Character), "GetHoverText")]
	internal static class HoverTextPatch
	{
		private static void Postfix(Character __instance, ref string __result)
		{
			if (!Plugin.Enabled.Value || !Object.op_Implicit((Object)(object)ZNet.instance))
			{
				return;
			}
			Growup component = ((Component)__instance).GetComponent<Growup>();
			if (!Object.op_Implicit((Object)(object)component) || component.m_growTime <= 0f)
			{
				return;
			}
			ZNetView component2 = ((Component)__instance).GetComponent<ZNetView>();
			if (!Object.op_Implicit((Object)(object)component2) || !component2.IsValid())
			{
				return;
			}
			long num = component2.GetZDO().GetLong(ZDOVars.s_spawnTime, 0L);
			if (num != 0L)
			{
				double num2 = (double)(ZNet.instance.GetTime().Ticks - num) / 10000000.0;
				double num3 = (double)component.m_growTime - num2;
				Localization instance = Localization.instance;
				string text = ((num3 > 0.0) ? TimeFormat.Format(num3) : instance.Localize("$growtime_soon"));
				string text2 = instance.Localize("$growtime_grows_in") + ": " + text;
				if (Plugin.ShowPercentage.Value)
				{
					text2 = text2 + " (" + TimeFormat.Percent(num2, component.m_growTime) + "%)";
				}
				__result = (string.IsNullOrEmpty(__result) ? (__instance.GetHoverName() + "\n" + text2) : (__result + "\n" + text2));
			}
		}
	}
	[BepInPlugin("paskutny.growtime", "GrowTime", "1.0.1")]
	public class Plugin : BaseUnityPlugin
	{
		public const string Guid = "paskutny.growtime";

		public const string Name = "GrowTime";

		internal static ConfigEntry<bool> Enabled;

		internal static ConfigEntry<bool> ShowPercentage;

		internal static string TranslationsDir;

		private void Awake()
		{
			//IL_0064: Unknown result type (might be due to invalid IL or missing references)
			Enabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "Show the remaining grow time when hovering over a young animal.");
			ShowPercentage = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "ShowPercentage", false, "Also show the growth progress in percent, e.g. \"Grows in: 12m 34s (58%)\".");
			TranslationsDir = Path.Combine(Path.GetDirectoryName(((BaseUnityPlugin)this).Info.Location), "Translations");
			new Harmony("paskutny.growtime").PatchAll();
			if (AccessTools.Field(typeof(Localization), "m_instance").GetValue(null) != null)
			{
				Translations.Inject(Localization.instance, Localization.instance.GetSelectedLanguage());
			}
		}
	}
	internal static class TimeFormat
	{
		public static string Format(double seconds)
		{
			int num = (int)Math.Ceiling(Math.Max(0.0, seconds));
			int num2 = num / 3600;
			int num3 = num / 60 % 60;
			int num4 = num % 60;
			if (num2 <= 0)
			{
				return $"{num3}m {num4:00}s";
			}
			return $"{num2}h {num3:00}m";
		}

		public static int Percent(double elapsed, double total)
		{
			return (int)Math.Max(0.0, Math.Min(100.0, Math.Floor(elapsed / total * 100.0)));
		}
	}
	[HarmonyPatch(typeof(Localization), "SetupLanguage")]
	internal static class Translations
	{
		private static readonly MethodInfo AddWord = AccessTools.Method(typeof(Localization), "AddWord", (Type[])null, (Type[])null);

		private static void Postfix(Localization __instance, string language)
		{
			Inject(__instance, language);
		}

		internal static void Inject(Localization localization, string language)
		{
			Load(localization, "English");
			if (language != "English")
			{
				Load(localization, language);
			}
		}

		private static void Load(Localization localization, string language)
		{
			string path = Path.Combine(Plugin.TranslationsDir, language + ".txt");
			if (!File.Exists(path))
			{
				return;
			}
			string[] array = File.ReadAllLines(path);
			foreach (string text in array)
			{
				int num = text.IndexOf('=');
				if (num > 0 && !text.TrimStart().StartsWith("#"))
				{
					AddWord.Invoke(localization, new object[2]
					{
						text.Substring(0, num).Trim(),
						text.Substring(num + 1).Trim()
					});
				}
			}
		}
	}
	internal static class PluginInfo
	{
		internal const string Version = "1.0.1";
	}
}