Decompiled source of PettingZoo v1.0.0

PettingZoo.dll

Decompiled 2 weeks ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using Jotunn.Managers;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("PettingZoo")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Riintouge")]
[assembly: AssemblyProduct("PettingZoo")]
[assembly: AssemblyCopyright("Copyright © 2024")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("94dd7542-a678-40b8-8337-51bd7ab36a22")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace PettingZoo;

[BepInPlugin("com.riintouge.pettingzoo", "Petting Zoo", "1.0.0")]
[BepInProcess("valheim.exe")]
public class PettingZoo : BaseUnityPlugin
{
	[HarmonyPatch(typeof(AnimalAI))]
	private class AnimalAIPatch
	{
		[HarmonyPatch("Awake")]
		[HarmonyPostfix]
		private static void AwakePostfix(AnimalAI __instance)
		{
			if (!IsEnabled.Value || Object.op_Implicit((Object)(object)((Component)__instance).gameObject.GetComponent<Petable>()))
			{
				return;
			}
			GameObject prefab = PrefabManager.Instance.GetPrefab("Halstein");
			Petable val = ((prefab != null) ? prefab.GetComponent<Petable>() : null);
			if (Object.op_Implicit((Object)(object)val))
			{
				Character component = ((Component)__instance).gameObject.GetComponent<Character>();
				if (Object.op_Implicit((Object)(object)component))
				{
					Petable obj = ((Component)__instance).gameObject.AddComponent<Petable>();
					obj.m_name = component.GetHoverName();
					obj.m_petEffect = val.m_petEffect;
					obj.m_randomPetTexts = val.m_randomPetTexts;
				}
			}
		}

		[HarmonyPatch("OnDamaged")]
		[HarmonyPostfix]
		private static void OnDamagedPostfix(AnimalAI __instance, Character attacker)
		{
			//IL_001e: Unknown result type (might be due to invalid IL or missing references)
			if (IsEnabled.Value)
			{
				new GrudgeHelper(((Component)__instance).gameObject.GetComponent<Character>()).AddAttacker(attacker.GetZDOID());
			}
		}
	}

	[HarmonyPatch(typeof(Petable))]
	private class PetablePatch
	{
		[HarmonyPatch("Interact")]
		[HarmonyPrefix]
		private static bool InteractPrefix(Petable __instance, Humanoid user)
		{
			//IL_0033: Unknown result type (might be due to invalid IL or missing references)
			if (!IsEnabled.Value)
			{
				return true;
			}
			if (!Object.op_Implicit((Object)(object)((Component)__instance).gameObject.GetComponent<Character>()))
			{
				return true;
			}
			if (!new GrudgeHelper(((Component)__instance).gameObject.GetComponent<Character>()).IsAttacker(((Character)user).GetZDOID()))
			{
				return true;
			}
			AnimalAI component = ((Component)__instance).gameObject.GetComponent<AnimalAI>();
			if (!Object.op_Implicit((Object)(object)component))
			{
				return true;
			}
			Traverse.Create((object)component).Method("OnDamaged", new Type[2]
			{
				typeof(float),
				typeof(Character)
			}, (object[])null).GetValue(new object[2] { 0, user });
			return false;
		}
	}

	public static ConfigEntry<bool> IsEnabled;

	public static ConfigEntry<bool> LoadOnStart;

	private readonly Harmony Harmony = new Harmony("com.riintouge.pettingzoo");

	private void Awake()
	{
		IsEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("0 - Core", "Enable", true, "Whether this plugin has any effect when loaded.");
		LoadOnStart = ((BaseUnityPlugin)this).Config.Bind<bool>("0 - Core", "LoadOnStart", true, "Whether this plugin loads on game start.");
		if (LoadOnStart.Value)
		{
			Harmony.PatchAll();
		}
	}
}
internal class GrudgeHelper
{
	public static readonly int GrudgesKeyHash = StringExtensionMethods.GetStableHashCode("pettingzoo_grudges");

	public ZDO zdo;

	public List<string> attackers;

	public GrudgeHelper(object znetViewHolder)
	{
		try
		{
			zdo = Traverse.Create(znetViewHolder).Field("m_nview").GetValue<ZNetView>()
				.GetZDO();
		}
		catch (Exception)
		{
		}
		attackers = ((zdo != null) ? new List<string>(zdo.GetString(GrudgesKeyHash, "").Split(new char[1] { ';' })) : new List<string>());
	}

	public unsafe void AddAttacker(ZDOID zdoID)
	{
		//IL_0009: Unknown result type (might be due to invalid IL or missing references)
		if (zdo != null && !IsAttacker(zdoID))
		{
			attackers.Add(((object)(*(ZDOID*)(&zdoID))/*cast due to .constrained prefix*/).ToString());
			zdo.Set(GrudgesKeyHash, string.Join(";", attackers));
		}
	}

	public unsafe bool IsAttacker(ZDOID zdoID)
	{
		return attackers.Contains(((object)(*(ZDOID*)(&zdoID))/*cast due to .constrained prefix*/).ToString());
	}
}