Decompiled source of JuvenileGrowthTweaks v1.0.0

JuvenileGrowthTweaks.dll

Decompiled 2 weeks ago
using System;
using System.Diagnostics;
using System.Globalization;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.6.2", FrameworkDisplayName = ".NET Framework 4.6.2")]
[assembly: AssemblyCompany("JuvenileGrowthTweaks")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("JuvenileGrowthTweaks")]
[assembly: AssemblyTitle("JuvenileGrowthTweaks")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace JuvenileGrowthTweaks;

internal static class GrowthMath
{
	internal const float VanillaCheckInterval = 10f;

	internal const float MinSpeed = 0.1f;

	internal const float MaxSpeed = 1000f;

	internal const float MinCheckInterval = 0.5f;

	internal const float MaxCheckInterval = 60f;

	internal const float FirstCheckFactorMin = 0.5f;

	internal const float FirstCheckFactorMax = 1.5f;

	internal static float ClampSpeed(float speed)
	{
		return Clamp(speed, 0.1f, 1000f);
	}

	internal static float ClampCheckInterval(float seconds)
	{
		return Clamp(seconds, 0.5f, 60f);
	}

	internal static float EffectiveGrowTime(float vanillaGrowTime, float speed)
	{
		if (vanillaGrowTime <= 0f)
		{
			return vanillaGrowTime;
		}
		return vanillaGrowTime / ClampSpeed(speed);
	}

	private static float Clamp(float value, float min, float max)
	{
		if (value < min)
		{
			return min;
		}
		if (!(value > max))
		{
			return value;
		}
		return max;
	}
}
[BepInPlugin("soulemper0r.juvenilegrowthtweaks", "JuvenileGrowthTweaks", "1.0.0")]
[BepInProcess("valheim.exe")]
public sealed class Plugin : BaseUnityPlugin
{
	[HarmonyPatch(typeof(Growup), "Start")]
	internal static class GrowupStartPatch
	{
		[HarmonyPostfix]
		private static void Postfix(Growup __instance)
		{
			if (!IsEnabled || (Object)(object)__instance == (Object)null)
			{
				return;
			}
			try
			{
				Apply(__instance);
			}
			catch (Exception ex)
			{
				_log.LogWarning((object)("Could not adjust juvenile growth: " + ex.Message));
			}
		}

		private static void Apply(Growup growup)
		{
			float growthSpeed = GrowthSpeed;
			float growTime = growup.m_growTime;
			float num = GrowthMath.EffectiveGrowTime(growTime, growthSpeed);
			if (!Mathf.Approximately(num, growTime))
			{
				growup.m_growTime = num;
			}
			((MonoBehaviour)growup).CancelInvoke("GrowUpdate");
			float checkInterval = CheckInterval;
			float num2 = checkInterval * Random.Range(0.5f, 1.5f);
			((MonoBehaviour)growup).InvokeRepeating("GrowUpdate", num2, checkInterval);
			if (DetailedLogging)
			{
				string text = (((Object)(object)growup.m_grownPrefab != (Object)null) ? ((Object)growup.m_grownPrefab).name : ((Object)((Component)growup).gameObject).name);
				_log.LogInfo((object)("'" + text + "': grow time " + Fmt(growTime) + " s -> " + Fmt(num) + " s, growth check every " + Fmt(checkInterval) + " s (first in " + Fmt(num2) + " s)."));
			}
		}
	}

	public const string PluginGuid = "soulemper0r.juvenilegrowthtweaks";

	public const string PluginName = "JuvenileGrowthTweaks";

	public const string PluginVersion = "1.0.0";

	private const string SectionGeneral = "General";

	private const string SectionDebug = "Debug";

	private const string GrowMethodName = "GrowUpdate";

	private static ConfigEntry<bool> _enabled;

	private static ConfigEntry<float> _growthSpeed;

	private static ConfigEntry<float> _checkInterval;

	private static ConfigEntry<bool> _detailedLog;

	private static ManualLogSource _log;

	private Harmony _harmony;

	internal static bool IsEnabled
	{
		get
		{
			if (_enabled != null)
			{
				return _enabled.Value;
			}
			return false;
		}
	}

	internal static float GrowthSpeed
	{
		get
		{
			if (_growthSpeed != null)
			{
				return GrowthMath.ClampSpeed(_growthSpeed.Value);
			}
			return 1f;
		}
	}

	internal static float CheckInterval
	{
		get
		{
			if (_checkInterval != null)
			{
				return GrowthMath.ClampCheckInterval(_checkInterval.Value);
			}
			return 10f;
		}
	}

	internal static bool DetailedLogging
	{
		get
		{
			if (_detailedLog != null)
			{
				return _detailedLog.Value;
			}
			return false;
		}
	}

	private void Awake()
	{
		//IL_0059: Unknown result type (might be due to invalid IL or missing references)
		//IL_0063: Expected O, but got Unknown
		//IL_0096: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a0: Expected O, but got Unknown
		//IL_010d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0117: Expected O, but got Unknown
		_log = ((BaseUnityPlugin)this).Logger;
		_enabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "Turns the mod on/off. Off = vanilla grow time and vanilla 10 s growth checks.");
		_growthSpeed = ((BaseUnityPlugin)this).Config.Bind<float>("General", "GrowthSpeedMultiplier", 10f, new ConfigDescription("How many times faster juveniles grow: the creature's grow time is divided by this value. 10 = a 3000 s grow time becomes 300 s, 1 = vanilla, 0.5 = twice as slow.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0.1f, 1000f), Array.Empty<object>()));
		_checkInterval = ((BaseUnityPlugin)this).Config.Bind<float>("General", "CheckIntervalSeconds", 5f, new ConfigDescription("How often the game checks whether a juvenile is ready to grow up (vanilla: 10 s, first check after 10-15 s). Smaller = the moment of growing up is more precise, bigger = less frequent checks.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0.5f, 60f), Array.Empty<object>()));
		_detailedLog = ((BaseUnityPlugin)this).Config.Bind<bool>("Debug", "DetailedLogging", false, "Logs every creature whose growth timer was adjusted (one line per spawned juvenile).");
		_enabled.SettingChanged += OnSettingChanged;
		_growthSpeed.SettingChanged += OnSettingChanged;
		_checkInterval.SettingChanged += OnSettingChanged;
		_harmony = new Harmony("soulemper0r.juvenilegrowthtweaks");
		try
		{
			_harmony.PatchAll(typeof(Plugin).Assembly);
			LogSettings("loaded");
		}
		catch (Exception ex)
		{
			_harmony.UnpatchSelf();
			_harmony = null;
			_log.LogError((object)("Could not install Growup patch: " + ex));
		}
	}

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

	private void OnSettingChanged(object sender, EventArgs e)
	{
		LogSettings("settings changed");
	}

	private void LogSettings(string reason)
	{
		float growthSpeed = GrowthSpeed;
		float checkInterval = CheckInterval;
		string text = Fmt(GrowthMath.EffectiveGrowTime(60f, growthSpeed));
		_log.LogInfo((object)("1.0.0 " + reason + ": growth speed x" + Fmt(growthSpeed) + ", growth check every " + Fmt(checkInterval) + " s (vanilla " + Fmt(10f) + " s; vanilla 60 s grow time -> " + text + " s)."));
	}

	private static string Fmt(float value)
	{
		return value.ToString("0.##", CultureInfo.InvariantCulture);
	}
}