Decompiled source of BetterTames v0.0.3

plugins/BetterTames.dll

Decompiled 2 weeks ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Serialization;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BetterTames.Patches;
using BetterTames.Utils;
using HarmonyLib;
using JetBrains.Annotations;
using Microsoft.CodeAnalysis;
using ServerSync;
using TMPro;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("Koro.BetterTames")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("TeamKoro")]
[assembly: AssemblyProduct("Koro.BetterTames")]
[assembly: AssemblyCopyright("TeamKoro")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("c7a9c4aa-0b32-47f7-a590-a68e94a2511e")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8.1", FrameworkDisplayName = ".NET Framework 4.8.1")]
[assembly: AssemblyVersion("1.0.0.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.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 BetterTames
{
	public enum DebugFeature
	{
		None,
		MakeCommandable,
		TeleportFollow,
		PetProtection,
		Initialization
	}
	[BepInPlugin("Koro.bettertames", "BetterTames", "0.0.3")]
	public class BetterTamesPlugin : BaseUnityPlugin
	{
		public readonly struct Peer
		{
			private readonly ZNetPeer _peer;

			public long m_uid => _peer?.m_uid ?? 0;

			public Vector3 m_refPos => (Vector3)(((??)_peer?.m_refPos) ?? default(Vector3));

			public ZDOID m_characterID => (ZDOID)(((??)_peer?.m_characterID) ?? default(ZDOID));

			public bool IsConnected
			{
				get
				{
					ZNetPeer peer = _peer;
					bool? obj;
					if (peer == null)
					{
						obj = null;
					}
					else
					{
						ISocket socket = peer.m_socket;
						obj = ((socket != null) ? new bool?(socket.IsConnected()) : null);
					}
					bool? flag = obj;
					return flag.GetValueOrDefault();
				}
			}

			public bool IsDefault => _peer == null;

			public Peer(ZNetPeer peer)
			{
				_peer = peer;
			}

			public string GetHostName()
			{
				ZNetPeer peer = _peer;
				object obj;
				if (peer == null)
				{
					obj = null;
				}
				else
				{
					ISocket socket = peer.m_socket;
					obj = ((socket != null) ? socket.GetHostName() : null);
				}
				if (obj == null)
				{
					obj = "";
				}
				return (string)obj;
			}

			public override bool Equals(object obj)
			{
				if (_peer != null)
				{
					return ((object)_peer).Equals(obj);
				}
				return obj == null;
			}

			public override int GetHashCode()
			{
				return ((object)_peer)?.GetHashCode() ?? 0;
			}
		}

		public const string PluginId = "Koro.bettertames";

		public const string PluginName = "BetterTames";

		public const string PluginVersion = "0.0.3";

		public static ServerSync.ConfigSync _configSync;

		private Harmony _harmony;

		public const string RPC_REQUEST_PET_PROTECTION = "BT_RequestPetProtection";

		public const string RPC_PET_PROTECTION_SYNC = "BT_PetProtectionSync";

		public const string RPC_TELEPORT_SYNC = "BT_TeleportSync";

		private static bool _corePatchesAppliedSession = false;

		public const string RPC_PREPARE_PETS_FOR_TELEPORT = "BT_PreparePetsForTeleport";

		public const string RPC_RECREATE_PETS_AT_DESTINATION = "BT_RecreatePetsAtDest";

		private static readonly Dictionary<ZDOID, List<ZDOID>> serverPetTeleportCache = new Dictionary<ZDOID, List<ZDOID>>();

		private static readonly int groundLayerMask = LayerMask.GetMask(new string[7] { "Default", "static_solid", "Default_small", "piece", "terrain", "blocker", "vehicle" });

		public static ConfigSync ConfigInstance { get; private set; }

		public static BetterTamesPlugin Instance { get; private set; }

		public static void LogIfDebug(string message, DebugFeature feature = DebugFeature.Initialization)
		{
			if (ConfigInstance != null)
			{
				bool flag = false;
				if (feature switch
				{
					DebugFeature.MakeCommandable => ConfigInstance.Tames.DebugMakeCommandable.Value, 
					DebugFeature.TeleportFollow => ConfigInstance.Tames.DebugTeleportFollow.Value, 
					DebugFeature.PetProtection => ConfigInstance.Tames.DebugPetProtection.Value, 
					DebugFeature.Initialization => false, 
					_ => false, 
				})
				{
					((BaseUnityPlugin)Instance).Logger.LogWarning((object)$"[{feature}] {message}");
				}
			}
		}

		private void Awake()
		{
			//IL_0059: Unknown result type (might be due to invalid IL or missing references)
			//IL_0063: Expected O, but got Unknown
			Instance = this;
			_configSync = new ServerSync.ConfigSync("Koro.bettertames")
			{
				DisplayName = "BetterTames",
				CurrentVersion = "0.0.3",
				MinimumRequiredVersion = "0.0.3",
				ModRequired = true
			};
			ConfigInstance = new ConfigSync(this);
			LogIfDebug("AWAKE: ConfigInstance und _configSync initialisiert.");
			_harmony = new Harmony("Koro.bettertames");
			PetProtectionPatch.UpdateExceptionPrefabs(ConfigInstance.Tames.PetProtectionExceptionPrefabs.Value);
			ConfigInstance.Tames.PetProtectionExceptionPrefabs.SettingChanged += OnExceptionPrefabsSettingChanged;
			try
			{
				LogIfDebug("Applying Initialization and Pet Protection patches...");
				_harmony.PatchAll(typeof(PetProtectionPatch));
				_harmony.PatchAll(typeof(InitializationPatches));
				LogIfDebug("AWAKE: InitializationPatches and Pet Protection applied.");
			}
			catch (Exception arg)
			{
				LogIfDebug($"AWAKE: CRITICAL ERROR applying initial patches: {arg}");
			}
		}

		private void OnExceptionPrefabsSettingChanged(object sender, EventArgs e)
		{
			PetProtectionPatch.UpdateExceptionPrefabs(ConfigInstance.Tames.PetProtectionExceptionPrefabs.Value);
		}

		public static void OnZNetReady()
		{
			LogIfDebug("OnZNetReady: ZNet is ready.");
			try
			{
				if (ZRoutedRpc.instance == null)
				{
					LogIfDebug("ZRoutedRpc.instance is still null in OnZNetReady!");
					LogIfDebug("ZRoutedRpc.instance is null in OnZNetReady. Cannot register RPCs.");
					return;
				}
				TryRegisterRpc<string, ZPackage>("BT_PetProtectionSync", Instance.RPC_PetProtectionSync_Client_Router);
				TryRegisterRpc<string, ZPackage>("BT_TeleportSync", Instance.RPC_TeleportSync_Client_Router);
				if (ZNet.instance.IsServer())
				{
					TryRegisterRpc<string>("BT_RequestPetProtection", Instance.RPC_RequestPetProtection_Server_Router);
					ZRoutedRpc.instance.Register<ZDOID, ZPackage>("BT_PreparePetsForTeleport", (Action<long, ZDOID, ZPackage>)Instance.RPC_PreparePetsForTeleport_Server);
					ZRoutedRpc.instance.Register<ZPackage>("BT_RecreatePetsAtDest", (Action<long, ZPackage>)Instance.RPC_RecreatePetsAtDestination_Server);
				}
				LogIfDebug("Spiel-spezifische RPCs registration attempted in OnZNetReady.");
			}
			catch (Exception ex)
			{
				LogIfDebug("Exception during game-specific RPC registration: " + ex.Message + "\n" + ex.StackTrace);
			}
		}

		private void RPC_PreparePetsForTeleport_Server(long senderPeerID, ZDOID teleportingPlayerZDOID, ZPackage pkg)
		{
			//IL_0020: Unknown result type (might be due to invalid IL or missing references)
			//IL_003d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0069: Unknown result type (might be due to invalid IL or missing references)
			//IL_006e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0075: Unknown result type (might be due to invalid IL or missing references)
			//IL_0090: Unknown result type (might be due to invalid IL or missing references)
			//IL_012d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0144: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fc: Unknown result type (might be due to invalid IL or missing references)
			if (!ZNet.instance.IsServer())
			{
				return;
			}
			int num = pkg.ReadInt();
			List<ZDOID> list = new List<ZDOID>();
			for (int i = 0; i < num; i++)
			{
				list.Add(pkg.ReadZDOID());
			}
			LogIfDebug($"SERVER: Empfange {num} Tier-ZDOIDs von Spieler {teleportingPlayerZDOID} (Peer: {senderPeerID}) für Teleport-Vorbereitung.", DebugFeature.TeleportFollow);
			List<ZDOID> list2 = new List<ZDOID>();
			foreach (ZDOID item in list)
			{
				ZDO zDO = ZDOMan.instance.GetZDO(item);
				if (zDO == null || !zDO.IsValid())
				{
					LogIfDebug($"SERVER: ZDO {item} nicht gefunden oder ungültig. Überspringe.", DebugFeature.TeleportFollow);
					continue;
				}
				zDO.SetOwner(ZNet.GetUID());
				zDO.Set("tempOwner", ((ZDOID)(ref teleportingPlayerZDOID)).UserID);
				ZNetView val = ZNetScene.instance.FindInstance(zDO);
				if ((Object)(object)val != (Object)null && (Object)(object)((Component)val).gameObject != (Object)null)
				{
					ZNetScene.instance.Destroy(((Component)val).gameObject);
				}
				list2.Add(item);
			}
			if (list2.Count > 0)
			{
				serverPetTeleportCache[teleportingPlayerZDOID] = list2;
				LogIfDebug($"SERVER: {list2.Count} Tiere für Spieler {teleportingPlayerZDOID} im Cache gespeichert.", DebugFeature.TeleportFollow);
			}
			else if (list.Count > 0)
			{
				LogIfDebug("SERVER: WARNUNG: successfullyPreparedZDOs war leer, aber petZDOIDsFromClient hatte Einträge. Cache mit petZDOIDsFromClient.", DebugFeature.TeleportFollow);
			}
		}

		private void RPC_RecreatePetsAtDestination_Server(long senderPeerID, ZPackage pkg)
		{
			//IL_000e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0013: Unknown result type (might be due to invalid IL or missing references)
			//IL_0015: Unknown result type (might be due to invalid IL or missing references)
			//IL_001a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0042: Unknown result type (might be due to invalid IL or missing references)
			//IL_0047: Unknown result type (might be due to invalid IL or missing references)
			//IL_004d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0065: Unknown result type (might be due to invalid IL or missing references)
			//IL_008e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0094: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00eb: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f2: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d3: Unknown result type (might be due to invalid IL or missing references)
			//IL_010f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0110: Unknown result type (might be due to invalid IL or missing references)
			//IL_0128: Unknown result type (might be due to invalid IL or missing references)
			//IL_012d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0131: Unknown result type (might be due to invalid IL or missing references)
			//IL_0138: Unknown result type (might be due to invalid IL or missing references)
			//IL_0139: Unknown result type (might be due to invalid IL or missing references)
			//IL_013b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0140: Unknown result type (might be due to invalid IL or missing references)
			//IL_0144: Unknown result type (might be due to invalid IL or missing references)
			//IL_0149: Unknown result type (might be due to invalid IL or missing references)
			//IL_014e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0152: Unknown result type (might be due to invalid IL or missing references)
			//IL_015b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0196: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a8: Unknown result type (might be due to invalid IL or missing references)
			if (!ZNet.instance.IsServer())
			{
				return;
			}
			Vector3 val = pkg.ReadVector3();
			Quaternion playerRot = pkg.ReadQuaternion();
			ZNetPeer peer = ZNet.instance.GetPeer(senderPeerID);
			if (peer == null)
			{
				LogIfDebug($"SERVER: Konnte Peer für senderPeerID {senderPeerID} nicht finden. Breche Tier-Neuerstellung ab.", DebugFeature.TeleportFollow);
				return;
			}
			ZDOID characterID = peer.m_characterID;
			if (!serverPetTeleportCache.TryGetValue(characterID, out var value) || value.Count == 0)
			{
				LogIfDebug($"SERVER: Keine Tiere im Cache für Spieler {characterID} (Peer: {senderPeerID}) oder Liste leer. Nichts zu tun.", DebugFeature.TeleportFollow);
				return;
			}
			LogIfDebug($"SERVER: Erstelle {value.Count} Tiere für Spieler {characterID} an Position {val} neu.", DebugFeature.TeleportFollow);
			float baseDistance = 10f;
			float angularSpread = 120f;
			int maxPetsPerArcLayer = 7;
			float layerDepthStep = 2.5f;
			float minSpacing = 1.8f;
			float verticalOffsetOnFloor = 0.3f;
			List<Vector3> list = new List<Vector3>();
			GetPlayerFromServerList(characterID);
			for (int i = 0; i < value.Count; i++)
			{
				ZDOID val2 = value[i];
				ZDO zDO = ZDOMan.instance.GetZDO(val2);
				if (zDO != null && zDO.IsValid())
				{
					Vector3 val3 = CalculateDistributedSpawnPos_Server(val, playerRot, i, value.Count, baseDistance, angularSpread, maxPetsPerArcLayer, layerDepthStep, minSpacing, list, verticalOffsetOnFloor);
					list.Add(val3);
					Vector3 val4 = val - val3;
					Quaternion rotation = Quaternion.LookRotation(((Vector3)(ref val4)).normalized);
					zDO.SetPosition(val3);
					zDO.SetRotation(rotation);
					if (zDO.GetOwner() != ZNet.GetUID())
					{
						zDO.SetOwner(ZNet.GetUID());
					}
					zDO.Created = false;
					zDO.SetOwner(peer.m_uid);
					LogIfDebug($"SERVER: ZDO {val2} an Spieler {peer.m_uid} übergeben. Position: {val3}, Created: false.", DebugFeature.TeleportFollow);
				}
			}
			serverPetTeleportCache.Remove(characterID);
		}

		private Player GetPlayerFromServerList(ZDOID playerZDOID)
		{
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			if (((ZDOID)(ref playerZDOID)).IsNone())
			{
				return null;
			}
			GameObject obj = ZNetScene.instance.FindInstance(playerZDOID);
			if (obj == null)
			{
				return null;
			}
			return obj.GetComponent<Player>();
		}

		private Vector3 CalculateDistributedSpawnPos_Server(Vector3 playerPos, Quaternion playerRot, int petIndexForSpawning, int totalPetsToSpawnThisCycle, float baseDistance, float angularSpread, int maxPetsPerArcLayer, float layerDepthStep, float minSpacing, List<Vector3> occupiedPointsCache, float verticalOffsetOnFloor)
		{
			//IL_0003: Unknown result type (might be due to invalid IL or missing references)
			//IL_0008: Unknown result type (might be due to invalid IL or missing references)
			//IL_0209: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a1: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a6: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a7: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ac: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b1: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b6: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b8: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b9: Unknown result type (might be due to invalid IL or missing references)
			//IL_01bd: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c2: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c7: Unknown result type (might be due to invalid IL or missing references)
			//IL_01cd: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ce: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d8: Unknown result type (might be due to invalid IL or missing references)
			//IL_01dd: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bc: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bd: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00cc: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ce: Unknown result type (might be due to invalid IL or missing references)
			//IL_00cf: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00dd: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f5: Unknown result type (might be due to invalid IL or missing references)
			//IL_01fb: Unknown result type (might be due to invalid IL or missing references)
			//IL_0105: Unknown result type (might be due to invalid IL or missing references)
			//IL_0111: Unknown result type (might be due to invalid IL or missing references)
			//IL_012d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0132: Unknown result type (might be due to invalid IL or missing references)
			//IL_0134: Unknown result type (might be due to invalid IL or missing references)
			//IL_0136: Unknown result type (might be due to invalid IL or missing references)
			//IL_0163: Unknown result type (might be due to invalid IL or missing references)
			//IL_0165: Unknown result type (might be due to invalid IL or missing references)
			int num = 10;
			Vector3 val = Vector3.zero;
			bool flag = false;
			float num6 = default(float);
			Vector3 val4 = default(Vector3);
			for (int i = 0; i < num; i++)
			{
				float num2 = Mathf.FloorToInt((float)petIndexForSpawning / (float)maxPetsPerArcLayer);
				float num3 = baseDistance + num2 * layerDepthStep + Random.Range(-1f, 1f);
				int num4 = Mathf.Min(maxPetsPerArcLayer, totalPetsToSpawnThisCycle - (int)num2 * maxPetsPerArcLayer);
				float num5 = ((totalPetsToSpawnThisCycle == 1) ? 0f : ((num4 > 1) ? ((0f - angularSpread) / 2f + (float)(petIndexForSpawning % maxPetsPerArcLayer) * (angularSpread / ((float)num4 - 1f))) : Random.Range((0f - angularSpread) / 5f, angularSpread / 5f)));
				num5 += Random.Range(-10f, 10f);
				Vector3 val2 = Quaternion.Euler(0f, num5, 0f) * (playerRot * Vector3.forward);
				Vector3 val3 = playerPos + val2 * num3;
				if (!ZoneSystem.instance.FindFloor(val3 + Vector3.up * 3f, ref num6))
				{
					continue;
				}
				((Vector3)(ref val4))..ctor(val3.x, num6 + verticalOffsetOnFloor, val3.z);
				bool flag2 = false;
				foreach (Vector3 item in occupiedPointsCache)
				{
					if (Vector3.Distance(val4, item) < minSpacing)
					{
						flag2 = true;
						break;
					}
				}
				if (!flag2)
				{
					val = val4;
					flag = true;
					break;
				}
			}
			if (!flag)
			{
				float num7 = (float)petIndexForSpawning * (360f / (float)totalPetsToSpawnThisCycle) % angularSpread - angularSpread / 2f;
				Vector3 val5 = Quaternion.Euler(0f, num7, 0f) * (playerRot * Vector3.forward);
				val = playerPos + val5 * baseDistance;
				float num8 = default(float);
				if (ZoneSystem.instance.FindFloor(val + Vector3.up * 3f, ref num8))
				{
					val.y = num8 + verticalOffsetOnFloor;
				}
				else
				{
					val.y = playerPos.y + verticalOffsetOnFloor;
				}
			}
			return val;
		}

		private static void TryRegisterRpc<T1, T2>(string name, Action<long, T1, T2> action)
		{
			try
			{
				ZRoutedRpc.instance.Register<T1, T2>(name, action);
				LogIfDebug("Spiel-RPC '" + name + "' registriert.");
			}
			catch (Exception ex)
			{
				LogIfDebug("Exception during game-specific RPC registration for '" + name + "': " + ex.Message + "\n" + ex.StackTrace);
			}
		}

		private static void TryRegisterRpc<T>(string name, Action<long, T> action)
		{
			try
			{
				ZRoutedRpc.instance.Register<T>(name, action);
				LogIfDebug("Spiel-RPC '" + name + "' registriert.");
			}
			catch (Exception ex)
			{
				LogIfDebug("Exception during game-specific RPC registration for '" + name + "': " + ex.Message + "\n" + ex.StackTrace);
			}
		}

		private void RPC_PetProtectionSync_Client_Router(long sender, string zdoID_str, ZPackage pkg)
		{
			RPC_PetProtectionSync_Client(sender, zdoID_str, pkg);
		}

		private void RPC_TeleportSync_Client_Router(long sender, string zdoID_str, ZPackage pkg)
		{
			RPC_TeleportSync_Client(sender, zdoID_str, pkg);
		}

		private void RPC_RequestPetProtection_Server_Router(long senderPeerId, string zdoID_str_fromClient)
		{
			RPC_RequestPetProtection_Server(senderPeerId, zdoID_str_fromClient);
		}

		public static void OnLocalPlayerReady()
		{
			LogIfDebug("OnLocalPlayerReady: Local player is set.");
			if (!_corePatchesAppliedSession)
			{
				ApplyCorePatches();
				_corePatchesAppliedSession = true;
			}
		}

		public static void ApplyCorePatches()
		{
			//IL_0016: Unknown result type (might be due to invalid IL or missing references)
			//IL_0020: Expected O, but got Unknown
			if (Instance._harmony == null)
			{
				Instance._harmony = new Harmony("Koro.bettertames");
			}
			try
			{
				LogIfDebug("Applying all BetterTames patches from assembly...");
				Instance._harmony.PatchAll(typeof(MakeCommandablePatch));
				Instance._harmony.PatchAll(typeof(TeleportFollowPatch));
				Instance._harmony.PatchAll(typeof(AnimationHelper));
				Instance._harmony.PatchAll(typeof(Billboard));
				Instance._harmony.PatchAll(typeof(MonsterAI_StunBehaviorPatches));
				LogIfDebug("All BetterTames patches applied successfully.");
			}
			catch (Exception arg)
			{
				LogIfDebug($"Exception during core patching: {arg}");
			}
		}

		private void RPC_RequestPetProtection_Server(long senderPeerId, string zdoID_str_fromClient)
		{
			//IL_0012: Unknown result type (might be due to invalid IL or missing references)
			//IL_0017: Unknown result type (might be due to invalid IL or missing references)
			//IL_002b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0054: Unknown result type (might be due to invalid IL or missing references)
			//IL_008d: Unknown result type (might be due to invalid IL or missing references)
			try
			{
				if (!ZNet.instance.IsServer())
				{
					return;
				}
				ZDOID val = ParseZDOID(zdoID_str_fromClient);
				if (((ZDOID)(ref val)).IsNone())
				{
					return;
				}
				ZDO zDO = ZDOMan.instance.GetZDO(val);
				if (zDO == null || !zDO.IsValid())
				{
					return;
				}
				if (zDO.GetOwner() != ZNet.GetUID())
				{
					LogIfDebug($"SERVER: ZDO {val} not owned by server. Current owner: {zDO.GetOwner()}. Taking ownership.", DebugFeature.PetProtection);
					zDO.SetOwner(ZNet.GetUID());
					if (zDO.GetOwner() != ZNet.GetUID())
					{
						LogIfDebug($"SERVER: Failed to take ownership for ZDO {val}. Cannot proceed with PetProtection logic for this ZDO from sender {senderPeerId}.");
						return;
					}
				}
				ZNetView val2 = ZNetScene.instance.FindInstance(zDO);
				if (!((Object)(object)val2 == (Object)null))
				{
					Character component = ((Component)val2).gameObject.GetComponent<Character>();
					if (!((Object)(object)component == (Object)null))
					{
						PetProtectionPatch.ApplyPetProtectionLogic(component, val2, zDO);
					}
				}
			}
			catch (Exception ex)
			{
				LogIfDebug("Exception in RPC_RequestPetProtection_Server: " + ex.Message + "\n" + ex.StackTrace);
			}
		}

		private void RPC_TeleportSync_Client(long sender, string zdoID_str, ZPackage pkg)
		{
			//IL_001f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0024: Unknown result type (might be due to invalid IL or missing references)
			//IL_0038: Unknown result type (might be due to invalid IL or missing references)
			//IL_004f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0066: Unknown result type (might be due to invalid IL or missing references)
			//IL_006b: Unknown result type (might be due to invalid IL or missing references)
			//IL_006d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0072: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c5: Unknown result type (might be due to invalid IL or missing references)
			try
			{
				if ((Object)(object)ZNet.instance == (Object)null || ZNet.instance.IsServer())
				{
					return;
				}
				ZDOID val = ParseZDOID(zdoID_str);
				if (((ZDOID)(ref val)).IsNone())
				{
					return;
				}
				ZDO zDO = ZDOMan.instance.GetZDO(val);
				if (zDO == null || !zDO.IsValid())
				{
					LogIfDebug($"TeleportSync_Client: No valid ZDO for '{val}'", DebugFeature.TeleportFollow);
					return;
				}
				Vector3 position = pkg.ReadVector3();
				Quaternion rotation = pkg.ReadQuaternion();
				ZNetView val2 = ZNetScene.instance.FindInstance(zDO);
				if (!((Object)(object)val2 != (Object)null))
				{
					return;
				}
				GameObject gameObject = ((Component)val2).gameObject;
				Character component = gameObject.GetComponent<Character>();
				if ((Object)(object)component != (Object)null && !component.IsTeleporting())
				{
					gameObject.transform.position = position;
					gameObject.transform.rotation = rotation;
					Rigidbody component2 = gameObject.GetComponent<Rigidbody>();
					if ((Object)(object)component2 != (Object)null)
					{
						component2.WakeUp();
					}
				}
			}
			catch (Exception ex)
			{
				LogIfDebug("Exception in RPC_TeleportSync_Client: " + ex.Message + "\n" + ex.StackTrace, DebugFeature.TeleportFollow);
			}
		}

		private void RPC_PetProtectionSync_Client(long sender, string zdoID_str, ZPackage pkg)
		{
			//IL_001f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0024: 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)
			//IL_0065: Unknown result type (might be due to invalid IL or missing references)
			try
			{
				if ((Object)(object)ZNet.instance == (Object)null || ZNet.instance.IsServer())
				{
					return;
				}
				ZDOID val = ParseZDOID(zdoID_str);
				if (((ZDOID)(ref val)).IsNone())
				{
					LogIfDebug("PetProtectionSync_Client: Parsed ZDOID is None for '" + zdoID_str + "'.", DebugFeature.PetProtection);
					return;
				}
				ZDO zDO = ZDOMan.instance.GetZDO(val);
				if (zDO == null || !zDO.IsValid())
				{
					LogIfDebug($"PetProtectionSync_Client: No valid ZDO for '{val}'", DebugFeature.PetProtection);
					return;
				}
				pkg.ReadBool();
				pkg.ReadSingle();
				bool flag = pkg.ReadBool();
				pkg.ReadSingle();
				float speed = pkg.ReadSingle();
				float walkSpeed = pkg.ReadSingle();
				float runSpeed = pkg.ReadSingle();
				float swimSpeed = pkg.ReadSingle();
				string text = pkg.ReadString();
				ZNetView val2 = ZNetScene.instance.FindInstance(zDO);
				if (!((Object)(object)val2 != (Object)null))
				{
					return;
				}
				Character component = ((Component)val2).gameObject.GetComponent<Character>();
				if (!((Object)(object)component != (Object)null))
				{
					return;
				}
				ZSyncAnimation component2 = ((Component)component).GetComponent<ZSyncAnimation>();
				if ((Object)(object)component2 != (Object)null)
				{
					Animator componentInChildren = ((Component)component2).GetComponentInChildren<Animator>();
					component2.SetSpeed(1f);
					if (flag)
					{
						if (!string.IsNullOrEmpty(text) && (Object)(object)componentInChildren != (Object)null)
						{
							component2.SetTrigger(text);
						}
						else if ((Object)(object)componentInChildren != (Object)null && !PetProtectionPatch.IsAnimatorPlayingBool(componentInChildren, "sleeping", expectedValue: true))
						{
							component2.SetBool("sleeping", true);
						}
					}
					else if ((Object)(object)componentInChildren != (Object)null && PetProtectionPatch.IsAnimatorPlayingBool(componentInChildren, "sleeping", expectedValue: true))
					{
						component2.SetBool("sleeping", false);
					}
				}
				component.m_speed = speed;
				component.m_walkSpeed = walkSpeed;
				component.m_runSpeed = runSpeed;
				component.m_swimSpeed = swimSpeed;
			}
			catch (Exception ex)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)("Exception in RPC_PetProtectionSync_Client: " + ex.Message + "\n" + ex.StackTrace));
			}
		}

		private static ZDOID ParseZDOID(string zdoID_str)
		{
			//IL_0008: Unknown result type (might be due to invalid IL or missing references)
			//IL_0026: Unknown result type (might be due to invalid IL or missing references)
			//IL_0038: Unknown result type (might be due to invalid IL or missing references)
			//IL_0052: Unknown result type (might be due to invalid IL or missing references)
			//IL_004a: Unknown result type (might be due to invalid IL or missing references)
			if (string.IsNullOrEmpty(zdoID_str))
			{
				return ZDOID.None;
			}
			string[] array = zdoID_str.Split(new char[1] { ':' });
			if (array.Length != 2)
			{
				return ZDOID.None;
			}
			if (!long.TryParse(array[0], out var result))
			{
				return ZDOID.None;
			}
			if (!uint.TryParse(array[1], out var result2))
			{
				return ZDOID.None;
			}
			return new ZDOID(result, result2);
		}

		private void OnDestroy()
		{
			LogIfDebug("BetterTamesPlugin.OnDestroy() called.");
			if (_harmony != null)
			{
				_harmony.UnpatchSelf();
				LogIfDebug("Harmony patches unapplied.");
			}
			ConfigInstance = null;
			_configSync = null;
			Instance = null;
			LogIfDebug("BetterTamesPlugin.OnDestroy() completed.");
		}
	}
	[HarmonyPatch]
	internal static class InitializationPatches
	{
		private static bool _zNetReadyCalled;

		private static bool _localPlayerReadyCalled;

		public static void ResetInitializationFlags()
		{
			BetterTamesPlugin.LogIfDebug("INITPATCH: Resetting initialization flags (_zNetReadyCalled, _localPlayerReadyCalled) to false.");
			_zNetReadyCalled = false;
			_localPlayerReadyCalled = false;
		}

		[HarmonyPatch(typeof(ZNet), "Awake")]
		[HarmonyPostfix]
		private static void ZNet_Awake_Postfix(ZNet __instance)
		{
			if (_zNetReadyCalled)
			{
				BetterTamesPlugin.LogIfDebug($"INITPATCH: ZNet.Awake_Postfix CALLED (Instance ID: {((Object)__instance).GetInstanceID()}), but _zNetReadyCalled is already true. Skipping OnZNetReady call.");
				return;
			}
			if ((Object)(object)ZNet.instance != (Object)(object)__instance || (Object)(object)ZNet.instance == (Object)null)
			{
				BetterTamesPlugin.LogIfDebug($"INITPATCH: ZNet.Awake_Postfix CALLED for an instance (ID: {((Object)__instance).GetInstanceID()}) that is not the current ZNet.instance OR ZNet.instance is null. Skipping OnZNetReady call for this instance.");
				return;
			}
			BetterTamesPlugin.LogIfDebug($"INITPATCH: ZNet.Awake_Postfix CALLED (Instance ID: {((Object)__instance).GetInstanceID()}). Attempting to call OnZNetReady.");
			try
			{
				BetterTamesPlugin.LogIfDebug("[BetterTames] ZnetAwake in aktion");
				BetterTamesPlugin.OnZNetReady();
			}
			catch (Exception arg)
			{
				BetterTamesPlugin.LogIfDebug($"[BetterTames] Error in OnZNetReady from ZNet_Awake_Postfix: {arg}");
			}
			_zNetReadyCalled = true;
		}

		[HarmonyPatch(typeof(Player), "SetLocalPlayer")]
		[HarmonyPostfix]
		private static void Player_SetLocalPlayer_Postfix()
		{
			if (_localPlayerReadyCalled)
			{
				return;
			}
			if ((Object)(object)Player.m_localPlayer == (Object)null)
			{
				BetterTamesPlugin.LogIfDebug("INITPATCH: Player.SetLocalPlayer_Postfix CALLED, but Player.m_localPlayer is NULL. Skipping OnLocalPlayerReady call.");
				return;
			}
			BetterTamesPlugin.LogIfDebug("INITPATCH: Player.SetLocalPlayer_Postfix CALLED for " + Player.m_localPlayer.GetPlayerName() + ". Attempting to call OnLocalPlayerReady.");
			try
			{
				BetterTamesPlugin.LogIfDebug("[BetterTames] OnLocalPlayerReady in aktion");
				BetterTamesPlugin.OnLocalPlayerReady();
			}
			catch (Exception arg)
			{
				BetterTamesPlugin.LogIfDebug($"[BetterTames] Error in OnLocalPlayerReady from Player_SetLocalPlayer_Postfix: {arg}");
			}
			_localPlayerReadyCalled = true;
		}
	}
	public class ConfigSync
	{
		public class TamesConfig
		{
			public ConfigEntry<bool> ServerConfigLocked { get; private set; }

			public ConfigEntry<bool> DebugMakeCommandable { get; private set; }

			public ConfigEntry<bool> TeleportFollowEnabled { get; private set; }

			public ConfigEntry<float> TeleportOnDistanceMaxRange { get; private set; }

			public ConfigEntry<bool> DebugTeleportFollow { get; private set; }

			public ConfigEntry<bool> PetProtectionEnabled { get; private set; }

			public ConfigEntry<float> PetProtectionStunDuration { get; private set; }

			public ConfigEntry<float> PetProtectionHealPercentage { get; private set; }

			public ConfigEntry<string> PetProtectionExceptionPrefabs { get; private set; }

			public ConfigEntry<string> PetProtectionStunIconItemPrefab { get; private set; }

			public ConfigEntry<float> PetProtectionStunIconScale { get; private set; }

			public ConfigEntry<float> PetProtectionStunIconHeightFactor { get; private set; }

			public ConfigEntry<bool> DebugPetProtection { get; private set; }

			public ConfigEntry<bool> ShowTamingProgressEnabled { get; private set; }

			public TamesConfig(ConfigFile cfg)
			{
				//IL_00a6: Unknown result type (might be due to invalid IL or missing references)
				//IL_00b0: Expected O, but got Unknown
				//IL_0133: Unknown result type (might be due to invalid IL or missing references)
				//IL_013d: Expected O, but got Unknown
				//IL_0179: Unknown result type (might be due to invalid IL or missing references)
				//IL_0183: Expected O, but got Unknown
				//IL_0219: Unknown result type (might be due to invalid IL or missing references)
				//IL_0223: Expected O, but got Unknown
				//IL_025f: Unknown result type (might be due to invalid IL or missing references)
				//IL_0269: Expected O, but got Unknown
				ServerConfigLocked = cfg.Bind<bool>("1. General ServerSync", "Lock Configuration", true, "If true on the server, this configuration file will be locked and synced to clients. ONLY an Admin can change this on a Server.");
				BetterTamesPlugin._configSync.AddLockingConfigEntry<bool>(ServerConfigLocked);
				string text = "2.MakeCommandable";
				DebugMakeCommandable = cfg.Bind<bool>(text, "Debug Logging", false, "Enables debug logging for this feature.");
				string text2 = "3.TeleportFollow";
				TeleportFollowEnabled = cfg.Bind<bool>(text2, "Enable", true, "Enables pets to teleport to the player if they get too far or the player uses a portal/teleports.");
				BetterTamesPlugin._configSync.AddConfigEntry<bool>(TeleportFollowEnabled);
				TeleportOnDistanceMaxRange = cfg.Bind<float>(text2, "Max Distance For AutoTeleport", 64f, new ConfigDescription("Maximum distance a pet can be from its owner before it attempts to teleport (if not in combat). Min: 20, Max: 64.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(20f, 64f), Array.Empty<object>()));
				BetterTamesPlugin._configSync.AddConfigEntry<float>(TeleportOnDistanceMaxRange);
				DebugTeleportFollow = cfg.Bind<bool>(text2, "Debug Logging", false, "Enables debug logging for teleport features.");
				string text3 = "4.PetProtection";
				PetProtectionEnabled = cfg.Bind<bool>(text3, "Enable", true, "Prevents tamed creatures from dying by knocking them out instead. They recover after a set time.");
				BetterTamesPlugin._configSync.AddConfigEntry<bool>(PetProtectionEnabled);
				PetProtectionStunDuration = cfg.Bind<float>(text3, "Stun Duration", 10f, new ConfigDescription("How long the pet stays stunned (seconds). Min: 1, Max: 300.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(1f, 300f), Array.Empty<object>()));
				BetterTamesPlugin._configSync.AddConfigEntry<float>(PetProtectionStunDuration);
				PetProtectionHealPercentage = cfg.Bind<float>(text3, "Heal After Stun Pct", 25f, new ConfigDescription("Percentage of max HP the pet recovers after stun. (0 = 1HP). Min: 0, Max: 100.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 100f), Array.Empty<object>()));
				BetterTamesPlugin._configSync.AddConfigEntry<float>(PetProtectionHealPercentage);
				PetProtectionExceptionPrefabs = cfg.Bind<string>(text3, "Exception Prefabs", "SummonedGolem_TW,SummonedSurtling_TW,SummonedSeeker_TW,SummonedImp_TW,Troll_Summoned,Charred_Twitcher_Summoned,Skeleton_Friendly,JC_Skeleton", "A comma-separated list of prefab names that should NOT receive pet protection. Example: Hatchling,SummonedGolem_TW,SummonedSurtling_TW,SummonedSeeker_TW,SummonedImp_TW,Troll_Summoned,Charred_Twitcher_Summoned,Skeleton_Friendly,JC_Skeleton,ArcticWolf_TW");
				BetterTamesPlugin._configSync.AddConfigEntry<string>(PetProtectionExceptionPrefabs);
				PetProtectionStunIconItemPrefab = cfg.Bind<string>(text3, "Stun Icon Item Prefab", "SledgeStagbreaker", "Prefab name of an item whose icon is used for the stun visual. Leave empty for no icon.");
				BetterTamesPlugin._configSync.AddConfigEntry<string>(PetProtectionStunIconItemPrefab);
				PetProtectionStunIconScale = cfg.Bind<float>(text3, "Stun Icon Scale", 1f, new ConfigDescription("Scale of the stun icon.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0.1f, 2f), Array.Empty<object>()));
				BetterTamesPlugin._configSync.AddConfigEntry<float>(PetProtectionStunIconScale);
				PetProtectionStunIconHeightFactor = cfg.Bind<float>(text3, "Stun Icon Height Factor", 2f, new ConfigDescription("Height factor for the stun icon, relative to character's visual top (e.g., 0.9 means 90% of height).", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0.1f, 3f), Array.Empty<object>()));
				BetterTamesPlugin._configSync.AddConfigEntry<float>(PetProtectionStunIconHeightFactor);
				DebugPetProtection = cfg.Bind<bool>(text3, "Debug Logging", false, "Enables debug logging for this feature.");
				string text4 = "5.Taming";
				ShowTamingProgressEnabled = cfg.Bind<bool>(text4, "Show Taming Progress", true, "Shows a taming progress percentage above creatures being tamed.");
				BetterTamesPlugin._configSync.AddConfigEntry<bool>(ShowTamingProgressEnabled);
			}
		}

		public TamesConfig Tames { get; private set; }

		public ConfigSync(BetterTamesPlugin pluginInstance)
		{
			Tames = new TamesConfig(((BaseUnityPlugin)pluginInstance).Config);
		}
	}
	public static class PetTeleporterLogic
	{
		public const float PET_PLACEMENT_BUFFER = 0.15f;

		public static string GetPrefabName(Character c)
		{
			return ((c != null) ? ((Object)c).name.Replace("(Clone)", "") : null) ?? "UnknownCharacter";
		}

		public static float GetPetHeight(Character character)
		{
			//IL_0036: Unknown result type (might be due to invalid IL or missing references)
			//IL_0075: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ac: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)character == (Object)null)
			{
				BetterTamesPlugin.LogIfDebug("GetPetHeight: Pet nicht gefunden. Fallback auf Höhe 1f.", DebugFeature.TeleportFollow);
				return 1f;
			}
			CapsuleCollider component = ((Component)character).GetComponent<CapsuleCollider>();
			if ((Object)(object)component != (Object)null)
			{
				float num = component.height * ((Component)character).transform.localScale.y;
				BetterTamesPlugin.LogIfDebug($"GetPetHeight für {GetPrefabName(character)}: CapsuleCollider gefunden. Höhe = {num:F2} (capsule.height={component.height:F2} * scale.y={((Component)character).transform.localScale.y:F2})", DebugFeature.TeleportFollow);
				return num;
			}
			Collider component2 = ((Component)character).GetComponent<Collider>();
			if ((Object)(object)component2 != (Object)null)
			{
				Bounds bounds = component2.bounds;
				float y = ((Bounds)(ref bounds)).size.y;
				BetterTamesPlugin.LogIfDebug($"GetPetHeight für {GetPrefabName(character)}: Fallback auf generischen Collider ({((object)component2).GetType().Name}) gefunden. Höhe (bounds.size.y) = {y:F2}", DebugFeature.TeleportFollow);
				return y;
			}
			BetterTamesPlugin.LogIfDebug("GetPetHeight für " + GetPrefabName(character) + ": Keinen Collider gefunden. Fallback auf Höhe 1f.", DebugFeature.TeleportFollow);
			return 1f;
		}

		public static void TeleportPetToActualPosition(Character petCharacter, Vector3 targetPosition, Quaternion targetRotation, Player teleportingPlayer)
		{
			//IL_0030: Unknown result type (might be due to invalid IL or missing references)
			//IL_0038: Unknown result type (might be due to invalid IL or missing references)
			//IL_0053: Unknown result type (might be due to invalid IL or missing references)
			//IL_005f: Unknown result type (might be due to invalid IL or missing references)
			//IL_008f: Unknown result type (might be due to invalid IL or missing references)
			//IL_009a: Unknown result type (might be due to invalid IL or missing references)
			//IL_010b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0113: Unknown result type (might be due to invalid IL or missing references)
			//IL_0119: Unknown result type (might be due to invalid IL or missing references)
			//IL_0120: Expected O, but got Unknown
			//IL_0122: Unknown result type (might be due to invalid IL or missing references)
			//IL_012a: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)petCharacter == (Object)null)
			{
				return;
			}
			ZNetView component = ((Component)petCharacter).GetComponent<ZNetView>();
			if ((Object)(object)component == (Object)null || !component.IsValid())
			{
				return;
			}
			string prefabName = GetPrefabName(petCharacter);
			BetterTamesPlugin.LogIfDebug($"TeleportPetToActualPosition: Setting {prefabName} to {targetPosition} with rotation {((Quaternion)(ref targetRotation)).eulerAngles}", DebugFeature.TeleportFollow);
			((Component)petCharacter).transform.position = targetPosition;
			((Component)petCharacter).transform.rotation = targetRotation;
			Rigidbody component2 = ((Component)petCharacter).GetComponent<Rigidbody>();
			if ((Object)(object)component2 != (Object)null)
			{
				if (!component2.isKinematic)
				{
					BetterTamesPlugin.LogIfDebug(prefabName + " ist nicht kinematisch, Velocity wird zurückgesetzt.", DebugFeature.TeleportFollow);
					component2.velocity = Vector3.zero;
					component2.angularVelocity = Vector3.zero;
				}
				else
				{
					BetterTamesPlugin.LogIfDebug(prefabName + " ist kinematisch, Velocity-Änderungen übersprungen.", DebugFeature.TeleportFollow);
				}
			}
			Tameable component3 = ((Component)petCharacter).GetComponent<Tameable>();
			if ((Object)(object)component3 != (Object)null)
			{
				component3.m_unsummonDistance = 0f;
			}
			MonsterAI component4 = ((Component)petCharacter).GetComponent<MonsterAI>();
			if ((Object)(object)component4 != (Object)null)
			{
				((BaseAI)component4).StopMoving();
				if ((Object)(object)teleportingPlayer != (Object)null)
				{
					component4.SetFollowTarget(((Component)teleportingPlayer).gameObject);
				}
			}
			ZDO zDO = component.GetZDO();
			zDO.SetPosition(targetPosition);
			zDO.SetRotation(targetRotation);
			ZPackage val = new ZPackage();
			val.Write(targetPosition);
			val.Write(targetRotation);
			string text = $"{((ZDOID)(ref zDO.m_uid)).UserID}:{((ZDOID)(ref zDO.m_uid)).ID}";
			ZRoutedRpc.instance.InvokeRoutedRPC(ZRoutedRpc.Everybody, "BT_TeleportSync", new object[2] { text, val });
			BetterTamesPlugin.LogIfDebug("TeleportPetToActualPosition: RPC_TELEPORT_SYNC sent for " + prefabName + ".", DebugFeature.TeleportFollow);
		}
	}
}
namespace BetterTames.Patches
{
	[HarmonyPatch]
	public static class MonsterAI_StunBehaviorPatches
	{
		[HarmonyPatch(typeof(MonsterAI), "UpdateAI")]
		[HarmonyPrefix]
		public static bool PreventAIUpdateWhenStunned(MonsterAI __instance)
		{
			ConfigSync configInstance = BetterTamesPlugin.ConfigInstance;
			if (configInstance == null || !(configInstance.Tames?.PetProtectionEnabled?.Value).GetValueOrDefault())
			{
				return true;
			}
			ZNetView component = ((Component)__instance).GetComponent<ZNetView>();
			if ((Object)(object)component == (Object)null || !component.IsValid())
			{
				return true;
			}
			ZDO zDO = component.GetZDO();
			Character component2 = ((Component)__instance).GetComponent<Character>();
			if (zDO != null && (Object)(object)component2 != (Object)null && component2.IsTamed() && zDO.GetBool("isRecoveringFromStun", false))
			{
				if (component2.m_speed != 0f)
				{
					component2.m_speed = 0f;
				}
				if (component2.m_walkSpeed != 0f)
				{
					component2.m_walkSpeed = 0f;
				}
				if (component2.m_runSpeed != 0f)
				{
					component2.m_runSpeed = 0f;
				}
				if (component2.m_swimSpeed != 0f)
				{
					component2.m_swimSpeed = 0f;
				}
				return false;
			}
			return true;
		}

		[HarmonyPatch(typeof(Humanoid), "StartAttack")]
		[HarmonyPrefix]
		public static bool PreventAttackWhenStunned_Humanoid(Humanoid __instance)
		{
			ConfigSync configInstance = BetterTamesPlugin.ConfigInstance;
			if (configInstance == null || !(configInstance.Tames?.PetProtectionEnabled?.Value).GetValueOrDefault() || !((Character)__instance).IsTamed())
			{
				return true;
			}
			ZNetView component = ((Component)__instance).GetComponent<ZNetView>();
			if ((Object)(object)component != (Object)null && component.IsValid())
			{
				ZDO zDO = component.GetZDO();
				if (zDO != null && zDO.GetBool("isRecoveringFromStun", false))
				{
					BetterTamesPlugin.LogIfDebug("Attack attempt by " + ((Character)__instance).m_name + " (Humanoid) blocked due to PetProtection stun.", DebugFeature.PetProtection);
					return false;
				}
			}
			return true;
		}
	}
	[HarmonyPatch(typeof(Tameable), "Interact")]
	public static class MakeCommandablePatch
	{
		[HarmonyPrefix]
		public static bool Prefix(Tameable __instance, Humanoid user, bool hold, bool alt, ref bool __result)
		{
			if (1 == 0 || hold || alt)
			{
				return true;
			}
			ZNetView component = ((Component)__instance).GetComponent<ZNetView>();
			Character component2 = ((Component)__instance).GetComponent<Character>();
			if (!component.IsValid() || !component2.IsTamed())
			{
				return true;
			}
			BetterTamesPlugin.LogIfDebug("Interacting with tamed creature: " + __instance.GetHoverName(), DebugFeature.MakeCommandable);
			__instance.Command(user, true);
			MonsterAI component3 = ((Component)__instance).GetComponent<MonsterAI>();
			BetterTamesPlugin.LogIfDebug("Command issued to " + __instance.GetHoverName() + ": " + (((Object)(object)component3.GetFollowTarget() != (Object)null) ? "Follow" : "Stay"), DebugFeature.MakeCommandable);
			__result = true;
			return false;
		}
	}
	[HarmonyPatch(typeof(MonsterAI), "UpdateAI")]
	public static class TeleportFollowPatch
	{
		private static readonly int groundLayerMask = LayerMask.GetMask(new string[7] { "Default", "static_solid", "Default_small", "piece", "terrain", "blocker", "vehicle" });

		private static readonly Dictionary<ZDOID, float> nextTeleportCheckTime = new Dictionary<ZDOID, float>();

		private const float TELEPORT_CHECK_INTERVAL = 3f;

		[HarmonyPostfix]
		public static void Postfix(MonsterAI __instance, float dt)
		{
			//IL_00b0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bd: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00cc: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ce: Unknown result type (might be due to invalid IL or missing references)
			//IL_0131: Unknown result type (might be due to invalid IL or missing references)
			//IL_0155: Unknown result type (might be due to invalid IL or missing references)
			//IL_0185: Unknown result type (might be due to invalid IL or missing references)
			//IL_0187: Unknown result type (might be due to invalid IL or missing references)
			//IL_0189: Unknown result type (might be due to invalid IL or missing references)
			//IL_018e: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d0: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d5: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d7: Unknown result type (might be due to invalid IL or missing references)
			//IL_020e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0213: Unknown result type (might be due to invalid IL or missing references)
			//IL_0214: Unknown result type (might be due to invalid IL or missing references)
			//IL_0219: Unknown result type (might be due to invalid IL or missing references)
			//IL_021e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0220: Unknown result type (might be due to invalid IL or missing references)
			//IL_0225: Unknown result type (might be due to invalid IL or missing references)
			//IL_022a: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ea: Unknown result type (might be due to invalid IL or missing references)
			//IL_0252: Unknown result type (might be due to invalid IL or missing references)
			//IL_0262: Unknown result type (might be due to invalid IL or missing references)
			//IL_0309: Unknown result type (might be due to invalid IL or missing references)
			//IL_030b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0312: Unknown result type (might be due to invalid IL or missing references)
			//IL_0317: Unknown result type (might be due to invalid IL or missing references)
			//IL_031b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0320: Unknown result type (might be due to invalid IL or missing references)
			//IL_0322: Unknown result type (might be due to invalid IL or missing references)
			//IL_0324: Unknown result type (might be due to invalid IL or missing references)
			//IL_0329: Unknown result type (might be due to invalid IL or missing references)
			//IL_032b: Unknown result type (might be due to invalid IL or missing references)
			//IL_032d: Unknown result type (might be due to invalid IL or missing references)
			//IL_032f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0334: Unknown result type (might be due to invalid IL or missing references)
			//IL_0336: Unknown result type (might be due to invalid IL or missing references)
			//IL_0338: Unknown result type (might be due to invalid IL or missing references)
			//IL_0342: Unknown result type (might be due to invalid IL or missing references)
			//IL_0347: Unknown result type (might be due to invalid IL or missing references)
			//IL_034c: Unknown result type (might be due to invalid IL or missing references)
			//IL_028a: Unknown result type (might be due to invalid IL or missing references)
			//IL_028f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0293: Unknown result type (might be due to invalid IL or missing references)
			//IL_029f: Unknown result type (might be due to invalid IL or missing references)
			//IL_02a4: Unknown result type (might be due to invalid IL or missing references)
			//IL_02a8: Unknown result type (might be due to invalid IL or missing references)
			//IL_0381: Unknown result type (might be due to invalid IL or missing references)
			//IL_0368: Unknown result type (might be due to invalid IL or missing references)
			//IL_03a8: Unknown result type (might be due to invalid IL or missing references)
			//IL_03aa: Unknown result type (might be due to invalid IL or missing references)
			//IL_03af: Unknown result type (might be due to invalid IL or missing references)
			//IL_03b7: Unknown result type (might be due to invalid IL or missing references)
			//IL_03c4: Unknown result type (might be due to invalid IL or missing references)
			//IL_03cc: Unknown result type (might be due to invalid IL or missing references)
			//IL_03d4: Unknown result type (might be due to invalid IL or missing references)
			//IL_03ff: Unknown result type (might be due to invalid IL or missing references)
			//IL_0411: Unknown result type (might be due to invalid IL or missing references)
			//IL_0418: Expected O, but got Unknown
			//IL_041a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0423: Unknown result type (might be due to invalid IL or missing references)
			ConfigSync configInstance = BetterTamesPlugin.ConfigInstance;
			if (configInstance == null || !(configInstance.Tames?.TeleportFollowEnabled?.Value).GetValueOrDefault() || (Object)(object)ZNet.instance == (Object)null)
			{
				return;
			}
			ZNetView component = ((Component)__instance).GetComponent<ZNetView>();
			if ((Object)(object)component == (Object)null || !component.IsValid())
			{
				return;
			}
			ZDO zDO = component.GetZDO();
			if (zDO == null)
			{
				return;
			}
			Character component2 = ((Component)__instance).GetComponent<Character>();
			if ((Object)(object)component2 == (Object)null || !component2.IsTamed())
			{
				return;
			}
			GameObject followTarget = __instance.GetFollowTarget();
			if ((Object)(object)followTarget == (Object)null)
			{
				return;
			}
			Vector3 position = ((Component)component2).transform.position;
			Vector3 position2 = followTarget.transform.position;
			if (component.IsOwner())
			{
				float num = Vector3.Distance(position, position2);
				float num2 = 5f;
				Character targetCreature = ((BaseAI)__instance).GetTargetCreature();
				StaticTarget staticTarget = __instance.GetStaticTarget();
				bool flag = ((Object)(object)targetCreature != (Object)null && BaseAI.IsEnemy(component2, targetCreature)) || (Object)(object)staticTarget != (Object)null;
				if (num < num2 && !flag)
				{
					((BaseAI)__instance).StopMoving();
				}
			}
			if (!component.IsOwner())
			{
				return;
			}
			float time = Time.time;
			if (!nextTeleportCheckTime.TryGetValue(zDO.m_uid, out var value))
			{
				value = 0f;
			}
			if (!(time >= value))
			{
				return;
			}
			nextTeleportCheckTime[zDO.m_uid] = time + 3f;
			float num3 = Mathf.Max(BetterTamesPlugin.ConfigInstance.Tames.TeleportOnDistanceMaxRange.Value, 10f);
			Vector3 val = position - position2;
			float sqrMagnitude = ((Vector3)(ref val)).sqrMagnitude;
			float num4 = num3 * num3;
			if (!(sqrMagnitude > num4))
			{
				return;
			}
			BetterTamesPlugin.LogIfDebug($"DistanceSqr {sqrMagnitude:F1} > {num4:F1}. Attempting teleport for {component2.m_name}.", DebugFeature.TeleportFollow);
			Vector3 position3 = followTarget.transform.position;
			if (position3.y > 2000f)
			{
				BetterTamesPlugin.LogIfDebug($"Player Y position {position3.y:F1} is > 2000. Preventing pet teleport for {component2.m_name} to avoid dungeon issue.", DebugFeature.TeleportFollow);
				return;
			}
			Quaternion rotation = followTarget.transform.rotation;
			Vector3 val2 = rotation * Vector3.forward;
			Vector3 val3 = rotation * Vector3.right;
			float num5 = 1f;
			CapsuleCollider component3 = ((Component)component2).GetComponent<CapsuleCollider>();
			if ((Object)(object)component3 != (Object)null)
			{
				num5 = component3.radius * Mathf.Max(((Component)component2).transform.localScale.x, ((Component)component2).transform.localScale.z);
			}
			else
			{
				Collider component4 = ((Component)component2).GetComponent<Collider>();
				if ((Object)(object)component4 != (Object)null)
				{
					Bounds bounds = component4.bounds;
					float x = ((Bounds)(ref bounds)).extents.x;
					bounds = component4.bounds;
					num5 = Mathf.Max(x, ((Bounds)(ref bounds)).extents.z);
				}
			}
			float num6 = Mathf.Max(10f, num5 + 0.5f);
			float num7 = num6 + 5f;
			float num8 = Mathf.Max(5f, num5 * 1.5f);
			float num9 = Random.Range(num6, num7);
			float num10 = Random.Range((0f - num8) / 2f, num8 / 2f);
			Vector3 val4 = -val2 * num9;
			Vector3 val5 = val3 * num10;
			Vector3 val6 = val4 + val5;
			Vector3 val7 = position3 + val6;
			RaycastHit val8 = default(RaycastHit);
			if (Physics.Raycast(val7 + Vector3.up * 5f, Vector3.down, ref val8, 10f, groundLayerMask))
			{
				val7.y = ((RaycastHit)(ref val8)).point.y + 1f;
			}
			else
			{
				val7.y = position3.y;
				BetterTamesPlugin.LogIfDebug("No ground found via Raycast for auto-teleport of " + component2.m_name + ", using target Y position.", DebugFeature.TeleportFollow);
			}
			Quaternion val9 = Quaternion.LookRotation(val2);
			((Component)component2).transform.position = val7;
			((Component)component2).transform.rotation = val9;
			zDO.SetPosition(val7);
			zDO.SetRotation(val9);
			Rigidbody component5 = ((Component)component2).GetComponent<Rigidbody>();
			if ((Object)(object)component5 != (Object)null)
			{
				component5.WakeUp();
			}
			BetterTamesPlugin.LogIfDebug($"Teleported {component2.m_name} to {val7} (behind followTarget). Sending RPC.", DebugFeature.TeleportFollow);
			ZPackage val10 = new ZPackage();
			val10.Write(val7);
			val10.Write(val9);
			string text = $"{((ZDOID)(ref zDO.m_uid)).UserID}:{((ZDOID)(ref zDO.m_uid)).ID}";
			ZRoutedRpc.instance.InvokeRoutedRPC(ZRoutedRpc.Everybody, "BT_TeleportSync", new object[2] { text, val10 });
		}
	}
	[HarmonyPatch]
	public static class PetProtectionPatch
	{
		public const string StunKeyConstant = "isRecoveringFromStun";

		public const string TimeSinceStunKeyConstant = "timeSinceStun";

		public const string OriginalSpeedKeyConstant = "original_speed";

		public const string OriginalWalkSpeedKeyConstant = "original_walkSpeed";

		public const string OriginalRunSpeedKeyConstant = "original_runSpeed";

		public const string OriginalSwimSpeedKeyConstant = "original_swimSpeed";

		public const string DEFAULT_STUN_ICON_ITEM_PREFAB_NAME_CONFIG = "SledgeStagbreaker";

		private static Dictionary<Character, GameObject> stunIconObjects = new Dictionary<Character, GameObject>();

		private static Sprite stunSpriteInstance;

		private static string currentLoadedIconPrefabName = null;

		private static Material defaultSpriteMaterial;

		public static HashSet<string> s_exceptionPrefabs = new HashSet<string>();

		public static void InitializeStunIconAssets(string itemPrefabNameFromConfig)
		{
			//IL_0139: Unknown result type (might be due to invalid IL or missing references)
			//IL_0143: Expected O, but got Unknown
			string text = (string.IsNullOrEmpty(itemPrefabNameFromConfig) ? "SledgeStagbreaker" : itemPrefabNameFromConfig);
			if ((Object)(object)stunSpriteInstance != (Object)null && (Object)(object)defaultSpriteMaterial != (Object)null && currentLoadedIconPrefabName == text)
			{
				return;
			}
			BetterTamesPlugin.LogIfDebug("Initializing/Reloading StunIconAssets with: " + text, DebugFeature.PetProtection);
			currentLoadedIconPrefabName = text;
			stunSpriteInstance = null;
			if ((Object)(object)ObjectDB.instance == (Object)null)
			{
				BetterTamesPlugin.LogIfDebug("[PetProtectionPatch] ObjectDB missing for StunIconSprite", DebugFeature.PetProtection);
				return;
			}
			GameObject itemPrefab = ObjectDB.instance.GetItemPrefab(text);
			if ((Object)(object)itemPrefab != (Object)null)
			{
				ItemDrop component = itemPrefab.GetComponent<ItemDrop>();
				if ((Object)(object)component != (Object)null && component.m_itemData != null)
				{
					stunSpriteInstance = component.m_itemData.GetIcon();
					if ((Object)(object)stunSpriteInstance == (Object)null)
					{
						BetterTamesPlugin.LogIfDebug("[PetProtectionPatch] Sprite für '" + text + "' nicht geladen.", DebugFeature.PetProtection);
					}
					else
					{
						BetterTamesPlugin.LogIfDebug("[PetProtectionPatch] Stun-Icon-Sprite '" + text + "' geladen.", DebugFeature.PetProtection);
					}
				}
				else
				{
					BetterTamesPlugin.LogIfDebug("[PetProtectionPatch] ItemDrop/ItemData nicht auf '" + text + "'-Prefab gefunden.", DebugFeature.PetProtection);
				}
			}
			else
			{
				BetterTamesPlugin.LogIfDebug("[PetProtectionPatch] '" + text + "'-Prefab nicht in ObjectDB gefunden.", DebugFeature.PetProtection);
			}
			if ((Object)(object)defaultSpriteMaterial == (Object)null)
			{
				Shader val = Shader.Find("Sprites/Default");
				if ((Object)(object)val != (Object)null)
				{
					defaultSpriteMaterial = new Material(val);
					BetterTamesPlugin.LogIfDebug("[PetProtectionPatch] Standard-Sprite-Material erstellt.", DebugFeature.PetProtection);
				}
				else
				{
					BetterTamesPlugin.LogIfDebug("[PetProtectionPatch] 'Sprites/Default' Shader nicht gefunden.", DebugFeature.PetProtection);
				}
			}
		}

		public static void UpdateExceptionPrefabs(string exceptionPrefabString)
		{
			s_exceptionPrefabs.Clear();
			if (!string.IsNullOrWhiteSpace(exceptionPrefabString))
			{
				foreach (string item in (from s in exceptionPrefabString.Split(new char[1] { ',' }, StringSplitOptions.RemoveEmptyEntries)
					select s.Trim().ToLower()).ToList())
				{
					s_exceptionPrefabs.Add(item);
				}
				BetterTamesPlugin.LogIfDebug("[BetterTames] Geladene Ausnahme-Prefabs: " + string.Join(", ", s_exceptionPrefabs), DebugFeature.PetProtection);
			}
			else
			{
				BetterTamesPlugin.LogIfDebug("[BetterTames] Keine Ausnahme-Prefabs in der Konfiguration definiert.", DebugFeature.PetProtection);
			}
		}

		private static bool ShouldApplyPetProtection(Character character)
		{
			if (!character.IsTamed())
			{
				return false;
			}
			ZNetView component = ((Component)character).GetComponent<ZNetView>();
			if ((Object)(object)component == (Object)null || !component.IsValid())
			{
				return false;
			}
			ZDO zDO = component.GetZDO();
			if (zDO == null)
			{
				return false;
			}
			string text = ((Object)ZNetScene.instance.GetPrefab(zDO.GetPrefab())).name.ToLower();
			BetterTamesPlugin.LogIfDebug("[BetterTames] " + text + " ist der Name des Pets.", DebugFeature.PetProtection);
			BetterTamesPlugin.LogIfDebug("[PetProtectionPatch] HashSet-Inhalt beim Check: " + string.Join(", ", s_exceptionPrefabs), DebugFeature.PetProtection);
			if (s_exceptionPrefabs.Contains(text))
			{
				BetterTamesPlugin.LogIfDebug("[BetterTames] " + character.m_name + " (" + text + ") ist ein Ausnahme-Prefab. Kein Schutz angewendet.", DebugFeature.PetProtection);
				return false;
			}
			return true;
		}

		[HarmonyPatch(typeof(Character), "ApplyDamage")]
		[HarmonyPrefix]
		public static bool ApplyDamagePrefix(Character __instance, HitData hit, bool showDamageText, bool triggerEffects, DamageModifier mod)
		{
			if (!BetterTamesPlugin.ConfigInstance.Tames.PetProtectionEnabled.Value || (Object)(object)ZNet.instance == (Object)null || !ShouldApplyPetProtection(__instance))
			{
				return true;
			}
			Character attacker = hit.GetAttacker();
			if ((Object)(object)attacker != (Object)null)
			{
				Player val = (Player)(object)((attacker is Player) ? attacker : null);
				if (val != null)
				{
					ItemData currentWeapon = ((Humanoid)val).GetCurrentWeapon();
					if (currentWeapon != null && (Object)(object)currentWeapon.m_dropPrefab != (Object)null && ((Object)currentWeapon.m_dropPrefab).name == "KnifeButcher")
					{
						return true;
					}
				}
			}
			ZNetView component = ((Component)__instance).GetComponent<ZNetView>();
			if ((Object)(object)component == (Object)null || !component.IsValid())
			{
				return true;
			}
			ZDO zDO = component.GetZDO();
			if (zDO == null)
			{
				return true;
			}
			if (zDO.GetBool("isRecoveringFromStun", false))
			{
				BetterTamesPlugin.LogIfDebug(__instance.m_name + " is already stunned. Damage ignored.", DebugFeature.PetProtection);
				return false;
			}
			if (zDO.GetFloat(ZDOVars.s_health, __instance.GetMaxHealth()) - hit.GetTotalDamage() > 0f)
			{
				return true;
			}
			if (component.IsOwner())
			{
				BetterTamesPlugin.LogIfDebug("OWNER (" + __instance.m_name + "): Applying PetProtection directly.", DebugFeature.PetProtection);
				ApplyPetProtectionLogic(__instance, component, zDO);
			}
			else
			{
				BetterTamesPlugin.LogIfDebug("CLIENT (" + __instance.m_name + "): Requesting PetProtection from server.", DebugFeature.PetProtection);
				string text = $"{((ZDOID)(ref zDO.m_uid)).UserID}:{((ZDOID)(ref zDO.m_uid)).ID}";
				ZRoutedRpc.instance.InvokeRoutedRPC(0L, "BT_RequestPetProtection", new object[1] { text });
			}
			return false;
		}

		public static void ApplyPetProtectionLogic(Character character, ZNetView nview, ZDO zdo)
		{
			//IL_0161: Unknown result type (might be due to invalid IL or missing references)
			//IL_0167: Expected O, but got Unknown
			if ((Object)(object)character == (Object)null || !character.IsTamed() || (Object)(object)nview == (Object)null || !nview.IsValid() || !nview.IsOwner() || zdo == null || zdo.GetBool("isRecoveringFromStun", false))
			{
				return;
			}
			BetterTamesPlugin.LogIfDebug("OWNER/SERVER: Executing ApplyPetProtectionLogic for " + character.m_name + ".", DebugFeature.PetProtection);
			ZSyncAnimation component = ((Component)character).GetComponent<ZSyncAnimation>();
			BaseAI component2 = ((Component)character).GetComponent<BaseAI>();
			if ((Object)(object)component2 != (Object)null)
			{
				component2.StopMoving();
				if (component2.IsCharging())
				{
					component2.ChargeStop();
				}
			}
			zdo.Set("isRecoveringFromStun", true);
			zdo.Set("timeSinceStun", 0f);
			zdo.Set(ZDOVars.s_health, 1f);
			zdo.Set(ZDOVars.s_sleeping, true);
			zdo.Set("original_speed", character.m_speed);
			zdo.Set("original_walkSpeed", character.m_walkSpeed);
			zdo.Set("original_runSpeed", character.m_runSpeed);
			zdo.Set("original_swimSpeed", character.m_swimSpeed);
			character.m_speed = 0f;
			character.m_walkSpeed = 0f;
			character.m_runSpeed = 0f;
			character.m_swimSpeed = 0f;
			string text = "stagger";
			if ((Object)(object)component != (Object)null)
			{
				component.SetSpeed(1f);
				if (!string.IsNullOrEmpty(text))
				{
					component.SetTrigger(text);
				}
				component.SetBool("sleeping", true);
			}
			ZPackage val = new ZPackage();
			val.Write(true);
			val.Write(0f);
			val.Write(true);
			val.Write(1f);
			val.Write(character.m_speed);
			val.Write(character.m_walkSpeed);
			val.Write(character.m_runSpeed);
			val.Write(character.m_swimSpeed);
			val.Write(text);
			string text2 = $"{((ZDOID)(ref zdo.m_uid)).UserID}:{((ZDOID)(ref zdo.m_uid)).ID}";
			ZRoutedRpc.instance.InvokeRoutedRPC(ZRoutedRpc.Everybody, "BT_PetProtectionSync", new object[2] { text2, val });
		}

		[HarmonyPatch(typeof(MonsterAI), "UpdateAI")]
		[HarmonyPostfix]
		public static void UpdateAIPostfix(MonsterAI __instance, float dt)
		{
			//IL_0282: Unknown result type (might be due to invalid IL or missing references)
			//IL_0289: Expected O, but got Unknown
			ConfigSync configInstance = BetterTamesPlugin.ConfigInstance;
			if (configInstance == null || !(configInstance.Tames?.PetProtectionEnabled?.Value).GetValueOrDefault() || (Object)(object)ZNet.instance == (Object)null)
			{
				return;
			}
			Character component = ((Component)__instance).GetComponent<Character>();
			if ((Object)(object)component == (Object)null || !component.IsTamed())
			{
				return;
			}
			ZNetView component2 = ((Component)component).GetComponent<ZNetView>();
			if ((Object)(object)component2 == (Object)null || !component2.IsValid())
			{
				return;
			}
			ZDO zDO = component2.GetZDO();
			if (zDO == null)
			{
				return;
			}
			bool @bool = zDO.GetBool("isRecoveringFromStun", false);
			HandleStunIcon(component, @bool);
			if (!component2.IsOwner())
			{
				return;
			}
			ZSyncAnimation component3 = ((Component)component).GetComponent<ZSyncAnimation>();
			Animator val = null;
			if ((Object)(object)component3 != (Object)null)
			{
				val = ((Component)component3).GetComponentInChildren<Animator>();
			}
			if (!@bool)
			{
				if (zDO.GetBool(ZDOVars.s_sleeping, false))
				{
					zDO.Set(ZDOVars.s_sleeping, false);
					if ((Object)(object)component3 != (Object)null && (Object)(object)val != (Object)null && IsAnimatorPlayingBool(val, "sleeping", expectedValue: true))
					{
						component3.SetBool("sleeping", false);
					}
					if ((Object)(object)component3 != (Object)null)
					{
						component3.SetSpeed(1f);
					}
				}
				return;
			}
			float num = zDO.GetFloat("timeSinceStun", 0f) + dt;
			zDO.Set("timeSinceStun", num);
			if (num >= BetterTamesPlugin.ConfigInstance.Tames.PetProtectionStunDuration.Value)
			{
				zDO.Set("isRecoveringFromStun", false);
				zDO.Set("timeSinceStun", 0f);
				zDO.Set(ZDOVars.s_sleeping, false);
				component.m_speed = zDO.GetFloat("original_speed", component.m_speed);
				component.m_walkSpeed = zDO.GetFloat("original_walkSpeed", component.m_walkSpeed);
				component.m_runSpeed = zDO.GetFloat("original_runSpeed", component.m_runSpeed);
				component.m_swimSpeed = zDO.GetFloat("original_swimSpeed", component.m_swimSpeed);
				float maxHealth = component.GetMaxHealth();
				float value = BetterTamesPlugin.ConfigInstance.Tames.PetProtectionHealPercentage.Value;
				float num2 = Mathf.Clamp(maxHealth * (value / 100f), 1f, maxHealth);
				zDO.Set(ZDOVars.s_health, num2);
				if ((Object)(object)component3 != (Object)null && (Object)(object)val != (Object)null && IsAnimatorPlayingBool(val, "sleeping", expectedValue: true))
				{
					component3.SetBool("sleeping", false);
				}
				if ((Object)(object)component3 != (Object)null)
				{
					component3.SetSpeed(1f);
				}
				ZPackage val2 = new ZPackage();
				val2.Write(false);
				val2.Write(0f);
				val2.Write(false);
				val2.Write(num2);
				val2.Write(component.m_speed);
				val2.Write(component.m_walkSpeed);
				val2.Write(component.m_runSpeed);
				val2.Write(component.m_swimSpeed);
				val2.Write(string.Empty);
				string text = $"{((ZDOID)(ref zDO.m_uid)).UserID}:{((ZDOID)(ref zDO.m_uid)).ID}";
				ZRoutedRpc.instance.InvokeRoutedRPC(ZRoutedRpc.Everybody, "BT_PetProtectionSync", new object[2] { text, val2 });
				return;
			}
			component.m_speed = 0f;
			component.m_walkSpeed = 0f;
			component.m_runSpeed = 0f;
			component.m_swimSpeed = 0f;
			if ((Object)(object)component3 != (Object)null && (Object)(object)val != (Object)null)
			{
				if (!IsAnimatorPlayingBool(val, "sleeping", expectedValue: true))
				{
					component3.SetBool("sleeping", true);
				}
				if (val.speed != 1f)
				{
					component3.SetSpeed(1f);
				}
			}
		}

		public static bool IsAnimatorPlayingBool(Animator animator, string boolName, bool expectedValue)
		{
			//IL_0034: Unknown result type (might be due to invalid IL or missing references)
			//IL_003a: Invalid comparison between Unknown and I4
			if ((Object)(object)animator == (Object)null)
			{
				return !expectedValue;
			}
			try
			{
				int num = Animator.StringToHash(boolName);
				bool flag = false;
				AnimatorControllerParameter[] parameters = animator.parameters;
				foreach (AnimatorControllerParameter val in parameters)
				{
					if (val.nameHash == num && (int)val.type == 4)
					{
						flag = true;
						break;
					}
				}
				if (flag)
				{
					return animator.GetBool(num) == expectedValue;
				}
			}
			catch (Exception ex)
			{
				BetterTamesPlugin.LogIfDebug("Error checking animator bool '" + boolName + "': " + ex.Message);
			}
			return !expectedValue;
		}

		private static void HandleStunIcon(Character character, bool isStunned)
		{
			//IL_012a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0162: Unknown result type (might be due to invalid IL or missing references)
			//IL_0168: Unknown result type (might be due to invalid IL or missing references)
			//IL_016d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0172: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a5: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ac: Expected O, but got Unknown
			//IL_01b3: Unknown result type (might be due to invalid IL or missing references)
			//IL_01f5: Unknown result type (might be due to invalid IL or missing references)
			//IL_022c: Unknown result type (might be due to invalid IL or missing references)
			//IL_023c: Unknown result type (might be due to invalid IL or missing references)
			ConfigSync configInstance = BetterTamesPlugin.ConfigInstance;
			if (configInstance == null || !(configInstance.Tames?.PetProtectionEnabled?.Value).GetValueOrDefault())
			{
				if (stunIconObjects.TryGetValue(character, out var value) && (Object)(object)value != (Object)null)
				{
					Object.Destroy((Object)(object)value);
					stunIconObjects.Remove(character);
				}
				return;
			}
			if (!((Object)(object)stunSpriteInstance == (Object)null) && !((Object)(object)defaultSpriteMaterial == (Object)null))
			{
				Texture2D texture = stunSpriteInstance.texture;
				if (!(((texture != null) ? ((Object)texture).name : null) != BetterTamesPlugin.ConfigInstance.Tames.PetProtectionStunIconItemPrefab.Value + "_dummy_for_name_check") || !(((Object)stunSpriteInstance).name != BetterTamesPlugin.ConfigInstance.Tames.PetProtectionStunIconItemPrefab.Value))
				{
					goto IL_0129;
				}
			}
			InitializeStunIconAssets(BetterTamesPlugin.ConfigInstance.Tames.PetProtectionStunIconItemPrefab.Value);
			if ((Object)(object)stunSpriteInstance == (Object)null || (Object)(object)defaultSpriteMaterial == (Object)null)
			{
				return;
			}
			goto IL_0129;
			IL_0129:
			Vector3 centerPoint = character.GetCenterPoint();
			float height = character.GetHeight();
			float value2 = BetterTamesPlugin.ConfigInstance.Tames.PetProtectionStunIconHeightFactor.Value;
			float value3 = BetterTamesPlugin.ConfigInstance.Tames.PetProtectionStunIconScale.Value;
			float num = height * value2;
			Vector3 position = centerPoint + Vector3.up * num;
			GameObject value5;
			if (isStunned)
			{
				if (!stunIconObjects.TryGetValue(character, out var value4) || (Object)(object)value4 == (Object)null)
				{
					value4 = new GameObject(character.m_name + "_StunIcon");
					value4.transform.position = position;
					SpriteRenderer obj = value4.AddComponent<SpriteRenderer>();
					obj.sprite = stunSpriteInstance;
					((Renderer)obj).material = defaultSpriteMaterial;
					((Renderer)obj).sortingLayerName = "Default";
					((Renderer)obj).sortingOrder = 5000;
					value4.transform.localScale = new Vector3(value3, value3, value3);
					if ((Object)(object)value4.GetComponent<Billboard>() == (Object)null)
					{
						value4.AddComponent<Billboard>();
					}
					stunIconObjects[character] = value4;
				}
				else
				{
					value4.transform.position = position;
					value4.transform.localScale = new Vector3(value3, value3, value3);
					if (!value4.activeSelf)
					{
						value4.SetActive(true);
					}
				}
			}
			else if (stunIconObjects.TryGetValue(character, out value5) && (Object)(object)value5 != (Object)null)
			{
				Object.Destroy((Object)(object)value5);
				stunIconObjects.Remove(character);
			}
			if (Time.frameCount % 300 != 0)
			{
				return;
			}
			foreach (Character item in (from kvp in stunIconObjects
				where (Object)(object)kvp.Key == (Object)null || (Object)(object)kvp.Value == (Object)null
				select kvp.Key).ToList())
			{
				if (stunIconObjects.TryGetValue(item, out var value6) && (Object)(object)value6 != (Object)null)
				{
					Object.Destroy((Object)(object)value6);
				}
				stunIconObjects.Remove(item);
			}
		}
	}
}
namespace BetterTames.Utils
{
	public class MonsterAI_StunBillboardPatch : MonoBehaviour
	{
		private Camera mainCamera;

		private void Awake()
		{
			mainCamera = Camera.main;
			if ((Object)(object)mainCamera == (Object)null)
			{
				BetterTamesPlugin.LogIfDebug("Billboard konnte mainCamera nicht finden.", DebugFeature.PetProtection);
			}
		}

		private void LateUpdate()
		{
			//IL_001a: Unknown result type (might be due to invalid IL or missing references)
			//IL_002a: Unknown result type (might be due to invalid IL or missing references)
			//IL_002f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0034: Unknown result type (might be due to invalid IL or missing references)
			//IL_0039: 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)
			//IL_0053: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)mainCamera != (Object)null)
			{
				((Component)this).transform.LookAt(((Component)this).transform.position + ((Component)mainCamera).transform.rotation * Vector3.forward, ((Component)mainCamera).transform.rotation * Vector3.up);
			}
			else
			{
				mainCamera = Camera.main;
			}
		}
	}
	public static class AnimationHelper
	{
		public static void SetKnockoutAnimation(Character character, bool active)
		{
			ZNetView component = ((Component)character).GetComponent<ZNetView>();
			ZDO zDO = component.GetZDO();
			ZSyncAnimation component2 = ((Component)character).GetComponent<ZSyncAnimation>();
			if (!component.IsValid() || (Object)(object)component2 == (Object)null)
			{
				BetterTamesPlugin.LogIfDebug("Failed to set sleeping animation for " + character.m_name + ": Invalid nview or ZSyncAnimation missing.", DebugFeature.PetProtection);
				return;
			}
			component2.SetBool("sleeping", active);
			if (component.IsOwner())
			{
				zDO.Set(ZDOVars.s_sleeping, active);
			}
			BetterTamesPlugin.LogIfDebug($"Set sleeping animation for {character.m_name}: {active}", DebugFeature.PetProtection);
		}
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableAttribute : Attribute
	{
		public readonly byte[] NullableFlags;

		public NullableAttribute(byte P_0)
		{
			NullableFlags = new byte[1] { P_0 };
		}

		public NullableAttribute(byte[] P_0)
		{
			NullableFlags = P_0;
		}
	}
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableContextAttribute : Attribute
	{
		public readonly byte Flag;

		public NullableContextAttribute(byte P_0)
		{
			Flag = P_0;
		}
	}
}
namespace ServerSync
{
	[PublicAPI]
	public abstract class OwnConfigEntryBase
	{
		public object? LocalBaseValue;

		public bool SynchronizedConfig = true;

		public abstract ConfigEntryBase BaseConfig { get; }
	}
	[PublicAPI]
	public class SyncedConfigEntry<T> : OwnConfigEntryBase
	{
		public readonly ConfigEntry<T> SourceConfig;

		public override ConfigEntryBase BaseConfig => (ConfigEntryBase)(object)SourceConfig;

		public T Value
		{
			get
			{
				return SourceConfig.Value;
			}
			set
			{
				SourceConfig.Value = value;
			}
		}

		public SyncedConfigEntry(ConfigEntry<T> sourceConfig)
		{
			SourceConfig = sourceConfig;
			base..ctor();
		}

		public void AssignLocalValue(T value)
		{
			if (LocalBaseValue == null)
			{
				Value = value;
			}
			else
			{
				LocalBaseValue = value;
			}
		}
	}
	public abstract class CustomSyncedValueBase
	{
		public object? LocalBaseValue;

		public readonly string Identifier;

		public readonly Type Type;

		private object? boxedValue;

		protected bool localIsOwner;

		public readonly int Priority;

		public object? BoxedValue
		{
			get
			{
				return boxedValue;
			}
			set
			{
				boxedValue = value;
				this.ValueChanged?.Invoke();
			}
		}

		public event Action? ValueChanged;

		protected CustomSyncedValueBase(ConfigSync configSync, string identifier, Type type, int priority)
		{
			Priority = priority;
			Identifier = identifier;
			Type = type;
			configSync.AddCustomValue(this);
			localIsOwner = configSync.IsSourceOfTruth;
			configSync.SourceOfTruthChanged += delegate(bool truth)
			{
				localIsOwner = truth;
			};
		}
	}
	[PublicAPI]
	public sealed class CustomSyncedValue<T> : CustomSyncedValueBase
	{
		public T Value
		{
			get
			{
				return (T)base.BoxedValue;
			}
			set
			{
				base.BoxedValue = value;
			}
		}

		public CustomSyncedValue(ConfigSync configSync, string identifier, T value = default(T), int priority = 0)
			: base(configSync, identifier, typeof(T), priority)
		{
			Value = value;
		}

		public void AssignLocalValue(T value)
		{
			if (localIsOwner)
			{
				Value = value;
			}
			else
			{
				LocalBaseValue = value;
			}
		}
	}
	internal class ConfigurationManagerAttributes
	{
		[UsedImplicitly]
		public bool? ReadOnly = false;
	}
	[PublicAPI]
	public class ConfigSync
	{
		[HarmonyPatch(typeof(ZRpc), "HandlePackage")]
		private static class SnatchCurrentlyHandlingRPC
		{
			public static ZRpc? currentRpc;

			[HarmonyPrefix]
			private static void Prefix(ZRpc __instance)
			{
				currentRpc = __instance;
			}
		}

		[HarmonyPatch(typeof(ZNet), "Awake")]
		internal static class RegisterRPCPatch
		{
			[HarmonyPostfix]
			private static void Postfix(ZNet __instance)
			{
				isServer = __instance.IsServer();
				foreach (ConfigSync configSync2 in configSyncs)
				{
					ZRoutedRpc.instance.Register<ZPackage>(configSync2.Name + " ConfigSync", (Action<long, ZPackage>)configSync2.RPC_FromOtherClientConfigSync);
					if (isServer)
					{
						configSync2.InitialSyncDone = true;
						Debug.Log((object)("Registered '" + configSync2.Name + " ConfigSync' RPC - waiting for incoming connections"));
					}
				}
				if (isServer)
				{
					((MonoBehaviour)__instance).StartCoroutine(WatchAdminListChanges());
				}
				static void SendAdmin(List<ZNetPeer> peers, bool isAdmin)
				{
					ZPackage package = ConfigsToPackage(null, null, new PackageEntry[1]
					{
						new PackageEntry
						{
							section = "Internal",
							key = "lockexempt",
							type = typeof(bool),
							value = isAdmin
						}
					});
					ConfigSync configSync = configSyncs.First();
					if (configSync != null)
					{
						((MonoBehaviour)ZNet.instance).StartCoroutine(configSync.sendZPackage(peers, package));
					}
				}
				static IEnumerator WatchAdminListChanges()
				{
					MethodInfo listContainsId = AccessTools.DeclaredMethod(typeof(ZNet), "ListContainsId", (Type[])null, (Type[])null);
					SyncedList adminList = (SyncedList)AccessTools.DeclaredField(typeof(ZNet), "m_adminList").GetValue(ZNet.instance);
					List<string> CurrentList = new List<string>(adminList.GetList());
					while (true)
					{
						yield return (object)new WaitForSeconds(30f);
						if (!adminList.GetList().SequenceEqual(CurrentList))
						{
							CurrentList = new List<string>(adminList.GetList());
							List<ZNetPeer> adminPeer = ZNet.instance.GetPeers().Where(delegate(ZNetPeer p)
							{
								string hostName = p.m_rpc.GetSocket().GetHostName();
								return ((object)listContainsId == null) ? adminList.Contains(hostName) : ((bool)listContainsId.Invoke(ZNet.instance, new object[2] { adminList, hostName }));
							}).ToList();
							List<ZNetPeer> nonAdminPeer = ZNet.instance.GetPeers().Except(adminPeer).ToList();
							SendAdmin(nonAdminPeer, isAdmin: false);
							SendAdmin(adminPeer, isAdmin: true);
						}
					}
				}
			}
		}

		[HarmonyPatch(typeof(ZNet), "OnNewConnection")]
		private static class RegisterClientRPCPatch
		{
			[HarmonyPostfix]
			private static void Postfix(ZNet __instance, ZNetPeer peer)
			{
				if (__instance.IsServer())
				{
					return;
				}
				foreach (ConfigSync configSync in configSyncs)
				{
					peer.m_rpc.Register<ZPackage>(configSync.Name + " ConfigSync", (Action<ZRpc, ZPackage>)configSync.RPC_FromServerConfigSync);
				}
			}
		}

		private class ParsedConfigs
		{
			public readonly Dictionary<OwnConfigEntryBase, object?> configValues = new Dictionary<OwnConfigEntryBase, object>();

			public readonly Dictionary<CustomSyncedValueBase, object?> customValues = new Dictionary<CustomSyncedValueBase, object>();
		}

		[HarmonyPatch(typeof(ZNet), "Shutdown")]
		private class ResetConfigsOnShutdown
		{
			[HarmonyPostfix]
			private static void Postfix()
			{
				ProcessingServerUpdate = true;
				foreach (ConfigSync configSync in configSyncs)
				{
					configSync.resetConfigsFromServer();
					configSync.IsSourceOfTruth = true;
					configSync.InitialSyncDone = false;
				}
				ProcessingServerUpdate = false;
			}
		}

		[HarmonyPatch(typeof(ZNet), "RPC_PeerInfo")]
		private class SendConfigsAfterLogin
		{
			private class BufferingSocket : ZPlayFabSocket, ISocket
			{
				public volatile bool finished = false;

				public volatile int versionMatchQueued = -1;

				public readonly List<ZPackage> Package = new List<ZPackage>();

				public readonly ISocket Original;

				public BufferingSocket(ISocket original)
				{
					Original = original;
					((ZPlayFabSocket)this)..ctor();
				}

				public bool IsConnected()
				{
					return Original.IsConnected();
				}

				public ZPackage Recv()
				{
					return Original.Recv();
				}

				public int GetSendQueueSize()
				{
					return Original.GetSendQueueSize();
				}

				public int GetCurrentSendRate()
				{
					return Original.GetCurrentSendRate();
				}

				public bool IsHost()
				{
					return Original.IsHost();
				}

				public void Dispose()
				{
					Original.Dispose();
				}

				public bool GotNewData()
				{
					return Original.GotNewData();
				}

				public void Close()
				{
					Original.Close();
				}

				public string GetEndPointString()
				{
					return Original.GetEndPointString();
				}

				public void GetAndResetStats(out int totalSent, out int totalRecv)
				{
					Original.GetAndResetStats(ref totalSent, ref totalRecv);
				}

				public void GetConnectionQuality(out float localQuality, out float remoteQuality, out int ping, out float outByteSec, out float inByteSec)
				{
					Original.GetConnectionQuality(ref localQuality, ref remoteQuality, ref ping, ref outByteSec, ref inByteSec);
				}

				public ISocket Accept()
				{
					return Original.Accept();
				}

				public int GetHostPort()
				{
					return Original.GetHostPort();
				}

				public bool Flush()
				{
					return Original.Flush();
				}

				public string GetHostName()
				{
					return Original.GetHostName();
				}

				public void VersionMatch()
				{
					if (finished)
					{
						Original.VersionMatch();
					}
					else
					{
						versionMatchQueued = Package.Count;
					}
				}

				public void Send(ZPackage pkg)
				{
					//IL_0057: Unknown result type (might be due to invalid IL or missing references)
					//IL_005d: Expected O, but got Unknown
					int pos = pkg.GetPos();
					pkg.SetPos(0);
					int num = pkg.ReadInt();
					if ((num == StringExtensionMethods.GetStableHashCode("PeerInfo") || num == StringExtensionMethods.GetStableHashCode("RoutedRPC") || num == StringExtensionMethods.GetStableHashCode("ZDOData")) && !finished)
					{
						ZPackage val = new ZPackage(pkg.GetArray());
						val.SetPos(pos);
						Package.Add(val);
					}
					else
					{
						pkg.SetPos(pos);
						Original.Send(pkg);
					}
				}
			}

			[HarmonyPriority(800)]
			[HarmonyPrefix]
			private static void Prefix(ref Dictionary<Assembly, BufferingSocket>? __state, ZNet __instance, ZRpc rpc)
			{
				//IL_0078: Unknown result type (might be due to invalid IL or missing references)
				//IL_007e: Invalid comparison between Unknown and I4
				if (!__instance.IsServer())
				{
					return;
				}
				BufferingSocket bufferingSocket = new BufferingSocket(rpc.GetSocket());
				AccessTools.DeclaredField(typeof(ZRpc), "m_socket").SetValue(rpc, bufferingSocket);
				object? obj = AccessTools.DeclaredMethod(typeof(ZNet), "GetPeer", new Type[1] { typeof(ZRpc) }, (Type[])null).Invoke(__instance, new object[1] { rpc });
				ZNetPeer val = (ZNetPeer)((obj is ZNetPeer) ? obj : null);
				if (val != null && (int)ZNet.m_onlineBackend > 0)
				{
					FieldInfo fieldInfo = AccessTools.DeclaredField(typeof(ZNetPeer), "m_socket");
					object? value = fieldInfo.GetValue(val);
					ZPlayFabSocket val2 = (ZPlayFabSocket)((value is ZPlayFabSocket) ? value : null);
					if (val2 != null)
					{
						typeof(ZPlayFabSocket).GetField("m_remotePlayerId").SetValue(bufferingSocket, val2.m_remotePlayerId);
					}
					fieldInfo.SetValue(val, bufferingSocket);
				}
				if (__state == null)
				{
					__state = new Dictionary<Assembly, BufferingSocket>();
				}
				__state[Assembly.GetExecutingAssembly()] = bufferingSocket;
			}

			[HarmonyPostfix]
			private static void Postfix(Dictionary<Assembly, BufferingSocket> __state, ZNet __instance, ZRpc rpc)
			{
				ZRpc rpc2 = rpc;
				ZNet __instance2 = __instance;
				Dictionary<Assembly, BufferingSocket> __state2 = __state;
				ZNetPeer peer;
				if (__instance2.IsServer())
				{
					object obj = AccessTools.DeclaredMethod(typeof(ZNet), "GetPeer", new Type[1] { typeof(ZRpc) }, (Type[])null).Invoke(__instance2, new object[1] { rpc2 });
					peer = (ZNetPeer)((obj is ZNetPeer) ? obj : null);
					if (peer == null)
					{
						SendBufferedData();
					}
					else
					{
						((MonoBehaviour)__instance2).StartCoroutine(sendAsync());
					}
				}
				void SendBufferedData()
				{
					if (rpc2.GetSocket() is BufferingSocket bufferingSocket)
					{
						AccessTools.DeclaredField(typeof(ZRpc), "m_socket").SetValue(rpc2, bufferingSocket.Original);
						object? obj2 = AccessTools.DeclaredMethod(typeof(ZNet), "GetPeer", new Type[1] { typeof(ZRpc) }, (Type[])null).Invoke(__instance2, new object[1] { rpc2 });
						ZNetPeer val = (ZNetPeer)((obj2 is ZNetPeer) ? obj2 : null);
						if (val != null)
						{
							AccessTools.DeclaredField(typeof(ZNetPeer), "m_socket").SetValue(val, bufferingSocket.Original);
						}
					}
					BufferingSocket bufferingSocket2 = __state2[Assembly.GetExecutingAssembly()];
					bufferingSocket2.finished = true;
					for (int i = 0; i < bufferingSocket2.Package.Count; i++)
					{
						if (i == bufferingSocket2.versionMatchQueued)
						{
							bufferingSocket2.Original.VersionMatch();
						}
						bufferingSocket2.Original.Send(bufferingSocket2.Package[i]);
					}
					if (bufferingSocket2.Package.Count == bufferingSocket2.versionMatchQueued)
					{
						bufferingSocket2.Original.VersionMatch();
					}
				}
				IEnumerator sendAsync()
				{
					foreach (ConfigSync configSync in configSyncs)
					{
						List<PackageEntry> entries = new List<PackageEntry>();
						if (configSync.CurrentVersion != null)
						{
							entries.Add(new PackageEntry
							{
								section = "Internal",
								key = "serverversion",
								type = typeof(string),
								value = configSync.CurrentVersion
							});
						}
						MethodInfo listContainsId = AccessTools.DeclaredMethod(typeof(ZNet), "ListContainsId", (Type[])null, (Type[])null);
						SyncedList adminList = (SyncedList)AccessTools.DeclaredField(typeof(ZNet), "m_adminList").GetValue(ZNet.instance);
						entries.Add(new PackageEntry
						{
							section = "Internal",
							key = "lockexempt",
							type = typeof(bool),
							value = (((object)listContainsId == null) ? ((object)adminList.Contains(rpc2.GetSocket().GetHostName())) : listContainsId.Invoke(ZNet.instance, new object[2]
							{
								adminList,
								rpc2.GetSocket().GetHostName()
							}))
						});
						ZPackage package = ConfigsToPackage(configSync.allConfigs.Select((OwnConfigEntryBase c) => c.BaseConfig), configSync.allCustomValues, entries, partial: false);
						yield return ((MonoBehaviour)__instance2).StartCoroutine(configSync.sendZPackage(new List<ZNetPeer> { peer }, package));
					}
					SendBufferedData();
				}
			}
		}

		private class PackageEntry
		{
			public string section = null;

			public string key = null;

			public Type type = null;

			public object? value;
		}

		[HarmonyPatch(typeof(ConfigEntryBase), "GetSerializedValue")]
		private static class PreventSavingServerInfo
		{
			[HarmonyPrefix]
			private static bool Prefix(ConfigEntryBase __instance, ref string __result)
			{
				OwnConfigEntryBase ownConfigEntryBase = configData(__instance);
				if (ownConfigEntryBase == null || isWritableConfig(ownConfigEntryBase))
				{
					return true;
				}
				__result = TomlTypeConverter.ConvertToString(ownConfigEntryBase.LocalBaseValue, __instance.SettingType);
				return false;
			}
		}

		[HarmonyPatch(typeof(ConfigEntryBase), "SetSerializedValue")]
		private static class PreventConfigRereadChangingValues
		{
			[HarmonyPrefix]
			private static bool Prefix(ConfigEntryBase __instance, string value)
			{
				OwnConfigEntryBase ownConfigEntryBase = configData(__instance);
				if (ownConfigEntryBase == null || ownConfigEntryBase.LocalBaseValue == null)
				{
					return true;
				}
				try
				{
					ownConfigEntryBase.LocalBaseValue = TomlTypeConverter.ConvertToValue(value, __instance.SettingType);
				}
				catch (Exception ex)
				{
					Debug.LogWarning((object)$"Config value of setting \"{__instance.Definition}\" could not be parsed and will be ignored. Reason: {ex.Message}; Value: {value}");
				}
				return false;
			}
		}

		private class InvalidDeserializationTypeException : Exception
		{
			public string expected = null;

			public string received = null;

			public string field = "";
		}

		public static bool ProcessingServerUpdate;

		public readonly string Name;

		public string? DisplayName;

		public string? CurrentVersion;

		public string? MinimumRequiredVersion;

		public bool ModRequired = false;

		private bool? forceConfigLocking;

		private bool isSourceOfTruth = true;

		private static readonly HashSet<ConfigSync> configSyncs;

		private readonly HashSet<OwnConfigEntryBase> allConfigs = new HashSet<OwnConfigEntryBase>();

		private HashSet<CustomSyncedValueBase> allCustomValues = new HashSet<CustomSyncedValueBase>();

		private static bool isServer;

		private static bool lockExempt;

		private OwnConfigEntryBase? lockedConfig = null;

		private const byte PARTIAL_CONFIGS = 1;

		private const byte FRAGMENTED_CONFIG = 2;

		private const byte COMPRESSED_CONFIG = 4;

		private readonly Dictionary<string, SortedDictionary<int, byte[]>> configValueCache = new Dictionary<string, SortedDictionary<int, byte[]>>();

		private readonly List<KeyValuePair<long, string>> cacheExpirations = new List<KeyValuePair<long, string>>();

		private static long packageCounter;

		public bool IsLocked
		{
			get
			{
				bool? flag = forceConfigLocking;
				bool num;
				if (!flag.HasValue)
				{
					if (lockedConfig == null)
					{
						goto IL_0052;
					}
					num = ((IConvertible)lockedConfig.BaseConfig.BoxedValue).ToInt32(CultureInfo.InvariantCulture) != 0;
				}
				else
				{
					num = flag.GetValueOrDefault();
				}
				if (!num)
				{
					goto IL_0052;
				}
				int result = ((!lockExempt) ? 1 : 0);
				goto IL_0053;
				IL_0053:
				return (byte)result != 0;
				IL_0052:
				result = 0;
				goto IL_0053;
			}
			set
			{
				forceConfigLocking = value;
			}
		}

		public bool IsAdmin => lockExempt || isSourceOfTruth;

		public bool IsSourceOfTruth
		{
			get
			{
				return isSourceOfTruth;
			}
			private set
			{
				if (value != isSourceOfTruth)
				{
					isSourceOfTruth = value;
					this.SourceOfTruthChanged?.Invoke(value);
				}
			}
		}

		public bool InitialSyncDone { get; private set; } = false;


		public event Action<bool>? SourceOfTruthChanged;

		private event Action? lockedConfigChanged;

		static ConfigSync()
		{
			ProcessingServerUpdate = false;
			configSyncs = new HashSet<ConfigSync>();
			lockExempt = false;
			packageCounter = 0L;
			RuntimeHelpers.RunClassConstructor(typeof(VersionCheck).TypeHandle);
		}

		public ConfigSync(string name)
		{
			Name = name;
			configSyncs.Add(this);
			new VersionCheck(this);
		}

		public SyncedConfigEntry<T> AddConfigEntry<T>(ConfigEntry<T> configEntry)
		{
			ConfigEntry<T> configEntry2 = configEntry;
			OwnConfigEntryBase ownConfigEntryBase = configData((ConfigEntryBase)(object)configEntry2);
			SyncedConfigEntry<T> syncedEntry = ownConfigEntryBase as SyncedConfigEntry<T>;
			if (syncedEntry == null)
			{
				syncedEntry = new SyncedConfigEntry<T>(configEntry2);
				AccessTools.DeclaredField(typeof(ConfigDescription), "<Tags>k__BackingField").SetValue(((ConfigEntryBase)configEntry2).Description, new object[1]
				{
					new ConfigurationManagerAttributes()
				}.Concat(((ConfigEntryBase)configEntry2).Description.Tags ?? Array.Empty<object>()).Concat(new SyncedConfigEntry<T>[1] { syncedEntry }).ToArray());
				configEntry2.SettingChanged += delegate
				{
					if (!ProcessingServerUpdate && syncedEntry.SynchronizedConfig)
					{
						Broadcast(ZRoutedRpc.Everybody, (ConfigEntryBase)configEntry2);
					}
				};
				allConfigs.Add(syncedEntry);
			}
			return syncedEntry;
		}

		public SyncedConfigEntry<T> AddLockingConfigEntry<T>(ConfigEntry<T> lockingConfig) where T : IConvertible
		{
			if (lockedConfig != null)
			{
				throw new Exception("Cannot initialize locking ConfigEntry twice");
			}
			lockedConfig = AddConfigEntry<T>(lockingConfig);
			lockingConfig.SettingChanged += delegate
			{
				this.lockedConfigChanged?.Invoke();
			};
			return (SyncedConfigEntry<T>)lockedConfig;
		}

		internal void AddCustomValue(CustomSyncedValueBase customValue)
		{
			CustomSyncedValueBase customValue2 = customValue;
			if (allCustomValues.Select((CustomSyncedValueBase v) => v.Identifier).Concat(new string[1] { "serverversion" }).Contains(customValue2.Identifier))
			{
				throw new Exception("Cannot have multiple settings with the same name or with a reserved name (serverversion)");
			}
			allCustomValues.Add(customValue2);
			allCustomValues = new HashSet<CustomSyncedValueBase>(allCustomValues.OrderByDescending((CustomSyncedValueBase v) => v.Priority));
			customValue2.ValueChanged += delegate
			{
				if (!ProcessingServerUpdate)
				{
					Broadcast(ZRoutedRpc.Everybody, customValue2);
				}
			};
		}

		private void RPC_FromServerConfigSync(ZRpc rpc, ZPackage package)
		{
			lockedConfigChanged += serverLockedSettingChanged;
			IsSourceOfTruth = false;
			if (HandleConfigSyncRPC(0L, package, clientUpdate: false))
			{
				InitialSyncDone = true;
			}
		}

		private void RPC_FromOtherClientConfigSync(long sender, ZPackage package)
		{
			HandleConfigSyncRPC(sender, package, clientUpdate: true);
		}

		private bool HandleConfigSyncRPC(long sender, ZPackage package, bool clientUpdate)
		{
			//IL_0076: Unknown result type (might be due to invalid IL or missing references)
			//IL_007d: Expected O, but got Unknown
			//IL_0250: Unknown result type (might be due to invalid IL or missing references)
			//IL_0257: Expected O, but got Unknown
			//IL_01ea: Unknown result type (might be due to invalid IL or missing references)
			//IL_01f1: Expected O, but got Unknown
			try
			{
				if (isServer && IsLocked)
				{
					ZRpc? currentRpc = SnatchCurrentlyHandlingRPC.currentRpc;
					object obj;
					if (currentRpc == null)
					{
						obj = null;
					}
					else
					{
						ISocket socket = currentRpc.GetSocket();
						obj = ((socket != null) ? socket.GetHostName() : null);
					}
					string text = (string)obj;
					if (text != null)
					{
						MethodInfo methodInfo = AccessTools.DeclaredMethod(typeof(ZNet), "ListContainsId", (Type[])null, (Type[])null);
						SyncedList val = (SyncedList)AccessTools.DeclaredField(typeof(ZNet), "m_adminList").GetValue(ZNet.instance);
						if (!(((object)methodInfo == null) ? val.Contains(text) : ((bool)methodInfo.Invoke(ZNet.instance, new object[2] { val, text }))))
						{
							return false;
						}
					}
				}
				cacheExpirations.RemoveAll(delegate(KeyValuePair<long, string> kv)
				{
					if (kv.Key < DateTimeOffset.Now.Ticks)
					{
						configValueCache.Remove(kv.Value);
						return true;
					}
					return false;
				});
				byte b = package.ReadByte();
				if ((b & 2u) != 0)
				{
					long num = package.ReadLong();
					string text2 = sender.ToString() + num;
					if (!configValueCache.TryGetValue(text2, out SortedDictionary<int, byte[]> value))
					{
						value = new SortedDictionary<int, byte[]>();
						configValueCache[text2] = value;
						cacheExpirations.Add(new KeyValuePair<long, string>(DateTimeOf