Decompiled source of PetKeeper v1.0.0

PetKeeper.dll

Decompiled 12 hours 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 HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyCompany("PetKeeper")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("PetKeeper")]
[assembly: AssemblyTitle("PetKeeper")]
[assembly: AssemblyVersion("1.0.0.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 PetKeeper
{
	[BepInPlugin("com.example.petkeeper", "PetKeeper", "1.0.0")]
	public class PetKeeperPlugin : BaseUnityPlugin
	{
		public const string PluginGuid = "com.example.petkeeper";

		public const string PluginName = "PetKeeper";

		public const string PluginVersion = "1.0.0";

		internal static ConfigEntry<KeyboardShortcut> FollowKey;

		internal static ConfigEntry<KeyboardShortcut> PetFollowKey;

		internal static ConfigEntry<KeyboardShortcut> PetGrowKey;

		internal static ConfigEntry<float> FollowRadius;

		internal static ConfigEntry<bool> Invincible;

		internal static ConfigEntry<bool> PassivePets;

		internal const string NoGrowKey = "PetKeeper_NoGrow";

		private const float SelectRange = 6f;

		private void Awake()
		{
			//IL_001b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0049: Unknown result type (might be due to invalid IL or missing references)
			//IL_0077: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f4: Unknown result type (might be due to invalid IL or missing references)
			FollowKey = ((BaseUnityPlugin)this).Config.Bind<KeyboardShortcut>("Keys", "ToggleFollow", new KeyboardShortcut((KeyCode)289, Array.Empty<KeyCode>()), "Toggles follow/stay for all of your tamed pets within FollowRadius.");
			PetFollowKey = ((BaseUnityPlugin)this).Config.Bind<KeyboardShortcut>("Keys", "ToggleFollowSinglePet", new KeyboardShortcut((KeyCode)287, Array.Empty<KeyCode>()), "Toggles follow/stay for ONE pet: the one under your crosshair, or the closest tamed pet within a few meters.");
			PetGrowKey = ((BaseUnityPlugin)this).Config.Bind<KeyboardShortcut>("Keys", "ToggleGrowthSinglePet", new KeyboardShortcut((KeyCode)288, Array.Empty<KeyCode>()), "Toggles growth freeze for ONE pet: the one under your crosshair, or the closest tamed pet within a few meters.");
			FollowRadius = ((BaseUnityPlugin)this).Config.Bind<float>("Follow", "Radius", 40f, "Only pets within this many meters are affected by the follow toggle.");
			Invincible = ((BaseUnityPlugin)this).Config.Bind<bool>("Pets", "Invincible", true, "Tamed creatures take no damage.");
			PassivePets = ((BaseUnityPlugin)this).Config.Bind<bool>("Pets", "CannotAttack", true, "Tamed creatures never pick targets, start attacks, or deal damage.");
			new Harmony("com.example.petkeeper").PatchAll();
		}

		private static bool InputBlocked()
		{
			return (Object)(object)Player.m_localPlayer == (Object)null || ((Object)(object)Chat.instance != (Object)null && Chat.instance.HasFocus()) || Console.IsVisible() || Menu.IsVisible() || TextInput.IsVisible() || Minimap.IsOpen() || InventoryGui.IsVisible() || StoreGui.IsVisible();
		}

		private void Update()
		{
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			//IL_0016: Unknown result type (might be due to invalid IL or missing references)
			//IL_002d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0032: Unknown result type (might be due to invalid IL or missing references)
			//IL_0049: Unknown result type (might be due to invalid IL or missing references)
			//IL_004e: Unknown result type (might be due to invalid IL or missing references)
			if (!InputBlocked())
			{
				KeyboardShortcut value = FollowKey.Value;
				if (((KeyboardShortcut)(ref value)).IsDown())
				{
					ToggleFollow();
				}
				value = PetFollowKey.Value;
				if (((KeyboardShortcut)(ref value)).IsDown())
				{
					ToggleFollowSingle();
				}
				value = PetGrowKey.Value;
				if (((KeyboardShortcut)(ref value)).IsDown())
				{
					ToggleGrowthSingle();
				}
			}
		}

		private static Character GetTargetPet(Player player)
		{
			//IL_006d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0078: Unknown result type (might be due to invalid IL or missing references)
			Character hoverCreature = player.GetHoverCreature();
			if ((Object)(object)hoverCreature != (Object)null && hoverCreature.IsTamed())
			{
				return hoverCreature;
			}
			Character result = null;
			float num = 6f;
			foreach (Character allCharacter in Character.GetAllCharacters())
			{
				if (!((Object)(object)allCharacter == (Object)null) && allCharacter.IsTamed())
				{
					float num2 = Vector3.Distance(((Component)allCharacter).transform.position, ((Component)player).transform.position);
					if (num2 < num)
					{
						num = num2;
						result = allCharacter;
					}
				}
			}
			return result;
		}

		private static void ToggleFollowSingle()
		{
			Player localPlayer = Player.m_localPlayer;
			Character targetPet = GetTargetPet(localPlayer);
			if ((Object)(object)targetPet == (Object)null || (Object)(object)((Component)targetPet).GetComponent<Tameable>() == (Object)null || (Object)(object)((Component)targetPet).GetComponent<MonsterAI>() == (Object)null)
			{
				((Character)localPlayer).Message((MessageType)1, "Look at a tamed pet (or stand next to one)", 0, (Sprite)null, false);
				return;
			}
			((Component)targetPet).GetComponent<Tameable>().Command((Humanoid)(object)localPlayer, false);
			bool flag = (Object)(object)((Component)targetPet).GetComponent<MonsterAI>().GetFollowTarget() != (Object)null;
			((Character)localPlayer).Message((MessageType)1, targetPet.GetHoverName() + ": " + (flag ? "following" : "staying"), 0, (Sprite)null, false);
		}

		private static void ToggleGrowthSingle()
		{
			Player localPlayer = Player.m_localPlayer;
			Character targetPet = GetTargetPet(localPlayer);
			ZNetView val = (((Object)(object)targetPet != (Object)null) ? ((Component)targetPet).GetComponent<ZNetView>() : null);
			if ((Object)(object)val == (Object)null || !val.IsValid())
			{
				((Character)localPlayer).Message((MessageType)1, "Look at a tamed pet (or stand next to one)", 0, (Sprite)null, false);
				return;
			}
			val.ClaimOwnership();
			bool flag = !val.GetZDO().GetBool("PetKeeper_NoGrow", false);
			val.GetZDO().Set("PetKeeper_NoGrow", flag);
			((Character)localPlayer).Message((MessageType)1, targetPet.GetHoverName() + ": " + (flag ? "growth FROZEN" : "growing normally"), 0, (Sprite)null, false);
		}

		private static void ToggleFollow()
		{
			//IL_004f: Unknown result type (might be due to invalid IL or missing references)
			//IL_005a: Unknown result type (might be due to invalid IL or missing references)
			Player localPlayer = Player.m_localPlayer;
			List<Character> list = new List<Character>();
			foreach (Character allCharacter in Character.GetAllCharacters())
			{
				if (!((Object)(object)allCharacter == (Object)null) && allCharacter.IsTamed() && !(Vector3.Distance(((Component)allCharacter).transform.position, ((Component)localPlayer).transform.position) > FollowRadius.Value) && !((Object)(object)((Component)allCharacter).GetComponent<Tameable>() == (Object)null) && !((Object)(object)((Component)allCharacter).GetComponent<MonsterAI>() == (Object)null))
				{
					list.Add(allCharacter);
				}
			}
			if (list.Count == 0)
			{
				((Character)localPlayer).Message((MessageType)1, "No tamed pets nearby", 0, (Sprite)null, false);
				return;
			}
			bool flag = false;
			foreach (Character item in list)
			{
				if ((Object)(object)((Component)item).GetComponent<MonsterAI>().GetFollowTarget() != (Object)null)
				{
					flag = true;
					break;
				}
			}
			bool flag2 = !flag;
			foreach (Character item2 in list)
			{
				bool flag3 = (Object)(object)((Component)item2).GetComponent<MonsterAI>().GetFollowTarget() != (Object)null;
				if (flag3 != flag2)
				{
					((Component)item2).GetComponent<Tameable>().Command((Humanoid)(object)localPlayer, false);
				}
			}
			((Character)localPlayer).Message((MessageType)1, flag2 ? "Pets: follow" : "Pets: stay", 0, (Sprite)null, false);
		}
	}
	[HarmonyPatch(typeof(Growup), "GrowUpdate")]
	internal static class Growup_GrowUpdate_Patch
	{
		private static bool Prefix(Growup __instance)
		{
			ZNetView component = ((Component)__instance).GetComponent<ZNetView>();
			if (!((Object)(object)component != (Object)null) || !component.IsValid() || !component.GetZDO().GetBool("PetKeeper_NoGrow", false))
			{
				return true;
			}
			Character component2 = ((Component)__instance).GetComponent<Character>();
			return (Object)(object)component2 == (Object)null || !component2.IsTamed();
		}
	}
	[HarmonyPatch(typeof(Character), "RPC_Damage")]
	internal static class Character_RPC_Damage_Patch
	{
		private static bool Prefix(Character __instance, HitData hit)
		{
			if (PetKeeperPlugin.Invincible.Value && __instance.IsTamed())
			{
				return false;
			}
			if (PetKeeperPlugin.PassivePets.Value && hit != null)
			{
				Character attacker = hit.GetAttacker();
				if ((Object)(object)attacker != (Object)null && attacker.IsTamed())
				{
					return false;
				}
			}
			return true;
		}
	}
	[HarmonyPatch(typeof(Character), "Damage")]
	internal static class Character_Damage_Patch
	{
		private static bool Prefix(Character __instance, HitData hit)
		{
			if (PetKeeperPlugin.Invincible.Value && __instance.IsTamed())
			{
				return false;
			}
			if (PetKeeperPlugin.PassivePets.Value && hit != null)
			{
				Character attacker = hit.GetAttacker();
				if ((Object)(object)attacker != (Object)null && attacker.IsTamed())
				{
					return false;
				}
			}
			return true;
		}
	}
	[HarmonyPatch(typeof(BaseAI), "FindEnemy")]
	internal static class BaseAI_FindEnemy_Patch
	{
		private static bool Prefix(BaseAI __instance, ref Character __result)
		{
			if (!PetKeeperPlugin.PassivePets.Value)
			{
				return true;
			}
			Character component = ((Component)__instance).GetComponent<Character>();
			if ((Object)(object)component != (Object)null && component.IsTamed())
			{
				__result = null;
				return false;
			}
			return true;
		}
	}
	[HarmonyPatch(typeof(Humanoid), "StartAttack")]
	internal static class Humanoid_StartAttack_Patch
	{
		private static bool Prefix(Humanoid __instance, ref bool __result)
		{
			if (PetKeeperPlugin.PassivePets.Value && ((Character)__instance).IsTamed())
			{
				__result = false;
				return false;
			}
			return true;
		}
	}
}