Decompiled source of Tamed Wolf Balance v1.5.1

BepInEx/plugins/TamedWolfBalance/TamedWolfBalance.dll

Decompiled a day ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyFileVersion("1.5.0.0")]
[assembly: AssemblyVersion("1.5.0.0")]
namespace Fai.WolfBalance;

[BepInPlugin("fai.tamedwolfbalance", "TamedWolfBalance", "1.5.0")]
public sealed class Plugin : BaseUnityPlugin
{
	internal struct TameSnapshot
	{
		internal bool Eligible;

		internal float Maximum;
	}

	[HarmonyPatch(typeof(Tameable), "Tame", new Type[] { })]
	internal static class TamePatch
	{
		[HarmonyPriority(800)]
		internal static void Prefix(Tameable __instance, out TameSnapshot __state)
		{
			__state = default(TameSnapshot);
			Character component = ((Component)__instance).GetComponent<Character>();
			if (!IsWolf(component) || component.IsTamed() || !component.IsOwner() || component.IsDead())
			{
				return;
			}
			ZDO val = Data(component);
			if (val != null && !val.GetBool("fai_twb_newtame_v150", false))
			{
				float maxHealth = component.GetMaxHealth();
				if (Positive(maxHealth) && Positive(maxHealth * 3f))
				{
					__state = new TameSnapshot
					{
						Eligible = true,
						Maximum = maxHealth
					};
				}
			}
		}

		[HarmonyPriority(0)]
		internal static void Postfix(Tameable __instance, TameSnapshot __state)
		{
			//IL_00e7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ec: Unknown result type (might be due to invalid IL or missing references)
			if (!__state.Eligible)
			{
				return;
			}
			Character component = ((Component)__instance).GetComponent<Character>();
			if ((Object)(object)component == (Object)null || !component.IsOwner() || component.IsDead())
			{
				return;
			}
			if (!component.IsTamed())
			{
				if (Log != null)
				{
					Log.LogWarning((object)"TWB: tame did not complete; wolf was not modified.");
				}
				return;
			}
			ZDO val = Data(component);
			if (val == null || val.GetBool("fai_twb_newtame_v150", false))
			{
				return;
			}
			float maxHealth = component.GetMaxHealth();
			float health = component.GetHealth();
			if (Positive(health) && Positive(maxHealth))
			{
				float num = __state.Maximum * 3f;
				val.Set("fai_twb_newtame_maxhp_v150", num);
				val.Set("fai_twb_newtame_v150", true);
				component.SetMaxHealth(num);
				component.SetHealth(Math.Min(1f, health / maxHealth) * num);
				if (Log != null)
				{
					Log.LogInfo((object)("TWB NEW TAME APPLIED | ZDO=" + ((object)component.GetZDOID()/*cast due to .constrained prefix*/).ToString() + " | normalMax=" + __state.Maximum.ToString("0.##") + " | HP=" + component.GetHealth().ToString("0.##") + "/" + component.GetMaxHealth().ToString("0.##") + " | damage=x0.3333333"));
				}
			}
		}
	}

	[HarmonyPatch(typeof(Character), "SetMaxHealth", new Type[] { typeof(float) })]
	internal static class WriteMaxPatch
	{
		[HarmonyPriority(0)]
		internal static void Prefix(Character __instance, ref float __0)
		{
			if (Target(__instance, out var maximum))
			{
				__0 = maximum;
			}
		}
	}

	[HarmonyPatch(typeof(Character), "GetMaxHealth", new Type[] { })]
	internal static class ReadMaxPatch
	{
		[HarmonyPriority(0)]
		internal static void Postfix(Character __instance, ref float __result)
		{
			if (Target(__instance, out var maximum))
			{
				__result = maximum;
			}
		}
	}

	[HarmonyPatch(typeof(Attack), "ModifyDamage", new Type[]
	{
		typeof(HitData),
		typeof(float)
	})]
	internal static class DamagePatch
	{
		private static readonly ConditionalWeakTable<HitData, object> adjusted = new ConditionalWeakTable<HitData, object>();

		[HarmonyPriority(0)]
		internal static void Postfix(Character ___m_character, HitData __0)
		{
			if (__0 != null && Target(___m_character, out var _) && !adjusted.TryGetValue(__0, out var _))
			{
				adjusted.Add(__0, new object());
				((DamageTypes)(ref __0.m_damage)).Modify(1f / 3f);
			}
		}
	}

	public const string Id = "fai.tamedwolfbalance";

	public const string Version = "1.5.0";

	internal const string Marker = "fai_twb_newtame_v150";

	internal const string MaxKey = "fai_twb_newtame_maxhp_v150";

	internal const float DamageFactor = 1f / 3f;

	internal static ManualLogSource Log;

	private Harmony harmony;

	private void Awake()
	{
		//IL_003a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0044: Expected O, but got Unknown
		Log = ((BaseUnityPlugin)this).Logger;
		((BaseUnityPlugin)this).Logger.LogInfo((object)("TWB 1.5.0 LOADING: " + typeof(Plugin).Assembly.Location));
		harmony = new Harmony("fai.tamedwolfbalance");
		try
		{
			harmony.PatchAll(typeof(Plugin).Assembly);
			((BaseUnityPlugin)this).Logger.LogInfo((object)"TWB 1.5.0 READY | new tames only | HP x3 | damage x0.3333333 | 4 patches installed");
		}
		catch (Exception ex)
		{
			harmony.UnpatchSelf();
			((BaseUnityPlugin)this).Logger.LogError((object)("TWB 1.5.0 PATCH FAILED; no balance patches active: " + ex));
		}
	}

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

	internal static bool Positive(float value)
	{
		if (value > 0f && !float.IsNaN(value))
		{
			return !float.IsInfinity(value);
		}
		return false;
	}

	internal static bool IsWolf(Character c)
	{
		if ((Object)(object)c != (Object)null)
		{
			return string.Equals(Utils.GetPrefabName(((Component)c).gameObject), "Wolf", StringComparison.Ordinal);
		}
		return false;
	}

	internal static ZDO Data(Character c)
	{
		if ((Object)(object)c == (Object)null)
		{
			return null;
		}
		ZNetView component = ((Component)c).GetComponent<ZNetView>();
		if (!((Object)(object)component != (Object)null) || !component.IsValid())
		{
			return null;
		}
		return component.GetZDO();
	}

	internal static bool Target(Character c, out float maximum)
	{
		maximum = 0f;
		if (!IsWolf(c) || !c.IsTamed())
		{
			return false;
		}
		ZDO val = Data(c);
		if (val == null || !val.GetBool("fai_twb_newtame_v150", false))
		{
			return false;
		}
		maximum = val.GetFloat("fai_twb_newtame_maxhp_v150", 0f);
		return Positive(maximum);
	}
}