Decompiled source of KikisHug v1.4.2

plugins/KikisHug.dll

Decompiled 4 days ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Text;
using BepInEx;
using ExitGames.Client.Photon;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Photon.Pun;
using Photon.Realtime;
using UnityEngine;
using UnityEngine.Animations.Rigging;
using Zorro.Core;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("KikisHug")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.4.2.0")]
[assembly: AssemblyInformationalVersion("1.4.2")]
[assembly: AssemblyProduct("KikisHug")]
[assembly: AssemblyTitle("KikisHug")]
[assembly: AssemblyVersion("1.4.2.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace KHUG
{
	[BepInPlugin("kikishug.peak.khug", "Kikis Hug", "1.4.2")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class Plugin : BaseUnityPlugin
	{
		private sealed class Hug
		{
			public Character Target;

			public bool Active;

			public float Blend;

			public bool Close;

			public float CloseBlend;

			public float StartTime;

			public float Dist = 999f;

			public bool OwnPose;

			public float Sep;

			public bool RotSaved;

			public float RotL;

			public float RotR;

			public bool LeanChecked;

			public bool HasLean;

			public bool NestChecked;

			public bool Nested;

			public bool WasMutual;

			public float MutualSince;
		}

		private sealed class Pair
		{
			public Character A;

			public Character B;

			public Collider[] CA;

			public Collider[] CB;

			public bool Close;

			public int Seen;

			public float ReleaseBy;
		}

		private sealed class Pose
		{
			public Vector3 ItemPos = new Vector3(0f, -0.3f, 0.45f);

			public Vector3 HandL = new Vector3(-0.08f, 0f, 0f);

			public Vector3 HandR = new Vector3(0.08f, 0f, 0f);

			public Vector3 LFwd;

			public Vector3 LUp;

			public Vector3 RFwd;

			public Vector3 RUp;

			public bool HasRot;

			public string Source = "built-in numbers";
		}

		public const string GUID = "kikishug.peak.khug";

		public const string NAME = "Kikis Hug";

		public const string VERSION = "1.4.2";

		private const string PROP = "khug";

		private const float NOTE_COOLDOWN = 5f;

		private const string POSE_ITEM = "Passport";

		private const float BLEND_SECONDS = 0.3f;

		private const float CLOSE_SECONDS = 0.5f;

		private const float SCAN_SECONDS = 0.1f;

		private const float NEAR_PICK = 1.5f;

		private const float PICK_ANGLE = 50f;

		private const float REMOTE_MAX = 8f;

		private const float LEAN_FAR = 1.4f;

		private const float LEAN_NEAR = 0.7f;

		private const float CLASP_K = 150f;

		private const float CLASP_MAX = 50f;

		private const float PULL_ACC = 6f;

		private const float PULL_REST = 0.5f;

		private const float PULL_RANGE = 0.5f;

		private const float PULL_MUTUAL = 2f;

		private const float HOLD_K = 25f;

		private const float HOLD_DAMP = 4f;

		private const float HOLD_MAX = 8f;

		private const float SEPARATE = 0.68f;

		private const float RELEASE_SECONDS = 2.5f;

		private const float RELEASE_ACC = 6f;

		private static readonly int LEAN_HASH = Animator.StringToHash("Floor Lean Forward");

		internal static Plugin Instance;

		private static readonly Dictionary<Character, Hug> Hugs = new Dictionary<Character, Hug>();

		private static readonly List<Pair> Pairs = new List<Pair>();

		private static FieldRef<CharacterAnimations, Character> _animChar;

		private static int _errors;

		private readonly List<Character> _keys = new List<Character>();

		private Pose _pose;

		private float _scan;

		private int _propValue;

		private bool _chatHook;

		private int _noteActor;

		private float _noteTime = -100f;

		private bool _camOn;

		private static FieldRef<MainCameraMovement, bool> _isRocketing;

		private static FieldRef<MainCameraMovement, float> _spectateZoom;

		private const int HUG_SECONDS = 12;

		private const int MAX_DIST_CM = 450;

		private const int PICK_DIST_CM = 400;

		private const int POSE_FORWARD_CM = 0;

		private const int POSE_HEIGHT_CM = 0;

		private const int POSE_SPREAD_PCT = 100;

		private const int POSE_LEAN_PCT = 40;

		private const int CLASP_PCT = 100;

		private const int PULL_PCT = 100;

		private const int EMB_DIST_CM = 36;

		private const int EMB_BACK_CM = 15;

		private const int EMB_SIDE_CM = 14;

		private const int EMB_RIGHT_UP_CM = -6;

		private const int EMB_LEFT_UP_CM = -5;

		private const int EMB_LEAN_DEG = 2;

		private const int EMB_TILT_DEG = 40;

		private const int EMB_NOD_DEG = 8;

		private const int CAMERA_CM = 250;

		private void Awake()
		{
			//IL_00ab: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b1: Expected O, but got Unknown
			//IL_0189: Unknown result type (might be due to invalid IL or missing references)
			//IL_0197: Expected O, but got Unknown
			Instance = this;
			try
			{
				_animChar = AccessTools.FieldRefAccess<CharacterAnimations, Character>("character");
			}
			catch (Exception ex)
			{
				((BaseUnityPlugin)this).Logger.LogWarning((object)("[KHUG] field 'character' not reachable (" + ex.Message + ") - using GetComponent instead"));
			}
			try
			{
				_isRocketing = AccessTools.FieldRefAccess<MainCameraMovement, bool>("isRocketing");
			}
			catch (Exception ex2)
			{
				((BaseUnityPlugin)this).Logger.LogWarning((object)("[KHUG] camera field 'isRocketing' not reachable (" + ex2.Message + ")"));
			}
			try
			{
				_spectateZoom = AccessTools.FieldRefAccess<MainCameraMovement, float>("spectateZoom");
			}
			catch (Exception ex3)
			{
				((BaseUnityPlugin)this).Logger.LogWarning((object)("[KHUG] camera field 'spectateZoom' not reachable (" + ex3.Message + ") - the camera distance stays at the game's value"));
			}
			Harmony val = new Harmony("kikishug.peak.khug");
			int num = 0;
			num += Patch(val, typeof(CharacterAnimations), "ConfigureIK", "ConfigureIKPostfix");
			num += Patch(val, typeof(CharacterAnimations), "HandleIK", "HandleIKPostfix");
			num += Patch(val, typeof(CharacterAnimations), "Update", "AnimUpdatePostfix");
			num += Patch(val, typeof(Character), "SetRotation", "SetRotationPostfix");
			try
			{
				Type type = AccessTools.TypeByName("PeakTextChat.TextChatManager");
				MethodInfo methodInfo = ((type != null) ? AccessTools.Method(type, "SendChatMessage", new Type[1] { typeof(string) }, (Type[])null) : null);
				if (methodInfo != null)
				{
					val.Patch((MethodBase)methodInfo, new HarmonyMethod(typeof(Plugin).GetMethod("SendPrefix", BindingFlags.Static | BindingFlags.NonPublic)), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
					_chatHook = true;
				}
			}
			catch (Exception ex4)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)("[KHUG] chat hook failed: " + ex4.Message));
			}
			((BaseUnityPlugin)this).Logger.LogInfo((object)("[KHUG] Kikis Hug 1.4.2 loaded - hooks " + num + " of 4 | " + (_chatHook ? "/hug: PeakTextChat hook ok" : "PeakTextChat NOT found - /hug is off")));
		}

		private int Patch(Harmony h, Type type, string method, string postfix)
		{
			//IL_0073: Unknown result type (might be due to invalid IL or missing references)
			//IL_0080: Expected O, but got Unknown
			try
			{
				MethodInfo methodInfo = AccessTools.Method(type, method, (Type[])null, (Type[])null);
				MethodInfo method2 = typeof(Plugin).GetMethod(postfix, BindingFlags.Static | BindingFlags.NonPublic);
				if (methodInfo == null || method2 == null)
				{
					((BaseUnityPlugin)this).Logger.LogError((object)("[KHUG] " + type.Name + "." + method + " not found - that part is off"));
					return 0;
				}
				h.Patch((MethodBase)methodInfo, (HarmonyMethod)null, new HarmonyMethod(method2), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
				return 1;
			}
			catch (Exception ex)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)("[KHUG] hook on " + type.Name + "." + method + " failed: " + ex.Message));
				return 0;
			}
		}

		private static bool SendPrefix(string message)
		{
			try
			{
				if ((Object)(object)Instance == (Object)null || string.IsNullOrEmpty(message))
				{
					return true;
				}
				string text = message.Trim();
				if (!text.StartsWith("/hug", StringComparison.OrdinalIgnoreCase))
				{
					return true;
				}
				if (text.Length > 4 && !char.IsWhiteSpace(text[4]))
				{
					return true;
				}
				Instance.Command();
				return false;
			}
			catch (Exception ex)
			{
				if ((Object)(object)Instance != (Object)null)
				{
					((BaseUnityPlugin)Instance).Logger.LogError((object)("[KHUG] /hug failed: " + ex.Message));
				}
				return true;
			}
		}

		private void Command()
		{
			if (_pose != null && !_pose.HasRot)
			{
				_pose = null;
			}
			Character localCharacter = Character.localCharacter;
			if (!PhotonNetwork.InRoom || (Object)(object)localCharacter == (Object)null)
			{
				((BaseUnityPlugin)this).Logger.LogInfo((object)"[KHUG] /hug: not in a lobby - nothing started");
				return;
			}
			if (Hugs.TryGetValue(localCharacter, out var value) && value.Active)
			{
				StopLocal(localCharacter, value, "/hug typed again");
				return;
			}
			string text = WhyNot(localCharacter);
			if (text != null)
			{
				((BaseUnityPlugin)this).Logger.LogInfo((object)("[KHUG] /hug: not started - " + text));
				Note("You can't hug right now.");
				return;
			}
			Character val = PickTarget(localCharacter);
			if ((Object)(object)val == (Object)null)
			{
				((BaseUnityPlugin)this).Logger.LogInfo((object)"[KHUG] /hug: not started - nobody within reach");
				Note("Nobody close enough to hug.");
				return;
			}
			if (value == null)
			{
				value = new Hug();
				Hugs[localCharacter] = value;
			}
			value.Target = val;
			value.Active = true;
			value.StartTime = Time.time;
			value.Dist = 999f;
			value.WasMutual = false;
			SetProp(val.refs.view.ViewID);
			GetPose();
			((BaseUnityPlugin)this).Logger.LogInfo((object)("[KHUG] hug start: -> " + NameOf(val) + (((Object)(object)localCharacter.data.currentItem != (Object)null) ? " (holding an item: the item pose stays, no embrace)" : "")));
		}

		private void Incoming(Character from)
		{
			try
			{
				Player owner = from.refs.view.Owner;
				int num = ((owner != null) ? owner.ActorNumber : 0);
				if (num != _noteActor || !(Time.time - _noteTime < 5f))
				{
					_noteActor = num;
					_noteTime = Time.time;
					Note(NameOf(from) + " is coming in with a hug");
					((BaseUnityPlugin)this).Logger.LogInfo((object)("[KHUG] chat line shown: " + NameOf(from) + " is coming in with a hug"));
				}
			}
			catch (Exception ex)
			{
				((BaseUnityPlugin)this).Logger.LogWarning((object)("[KHUG] chat line not shown: " + ex.Message));
			}
		}

		private static string WhyNot(Character c)
		{
			CharacterData data = c.data;
			if ((Object)(object)data == (Object)null)
			{
				return "no character data";
			}
			if (data.dead)
			{
				return "dead";
			}
			if (data.passedOut || data.fullyPassedOut)
			{
				return "passed out";
			}
			if (data.fallSeconds > 0f)
			{
				return "fallen over";
			}
			if (data.isClimbing || data.isRopeClimbing || data.isVineClimbing)
			{
				return "climbing";
			}
			if ((Object)(object)data.carrier != (Object)null)
			{
				return "being carried";
			}
			return null;
		}

		private Character PickTarget(Character me)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_000d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0012: 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_00c4: 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)
			//IL_00ca: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e9: Unknown result type (might be due to invalid IL or missing references)
			Vector3 head = me.Head;
			Vector3 lookDirection = me.data.lookDirection;
			float num = (float)Mathf.Max(50, 400) * 0.01f;
			float num2 = float.MaxValue;
			float num3 = 1.5f;
			Character val = null;
			Character result = null;
			List<Character> allCharacters = Character.AllCharacters;
			for (int i = 0; i < allCharacters.Count; i++)
			{
				Character val2 = allCharacters[i];
				if ((Object)(object)val2 == (Object)null || (Object)(object)val2 == (Object)(object)me || val2.isBot || (Object)(object)val2.data == (Object)null || val2.data.dead || val2.refs == null || (Object)(object)val2.refs.view == (Object)null || !Chest(val2, out var pos))
				{
					continue;
				}
				Vector3 val3 = pos - head;
				float magnitude = ((Vector3)(ref val3)).magnitude;
				if (magnitude > num)
				{
					continue;
				}
				if (magnitude < num3)
				{
					num3 = magnitude;
					result = val2;
				}
				float num4 = Vector3.Angle(lookDirection, val3);
				if (!(num4 > 50f))
				{
					float num5 = num4 + magnitude * 8f;
					if (num5 < num2)
					{
						num2 = num5;
						val = val2;
					}
				}
			}
			if (!((Object)(object)val != (Object)null))
			{
				return result;
			}
			return val;
		}

		private void StopLocal(Character me, Hug h, string why)
		{
			h.Active = false;
			SetProp(0);
			((BaseUnityPlugin)this).Logger.LogInfo((object)("[KHUG] hug end: " + why));
		}

		private void SetProp(int viewId)
		{
			//IL_001e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0023: Unknown result type (might be due to invalid IL or missing references)
			//IL_0034: Expected O, but got Unknown
			//IL_003b: Expected O, but got Unknown
			try
			{
				if (_propValue != viewId)
				{
					_propValue = viewId;
					if (PhotonNetwork.LocalPlayer != null)
					{
						Player localPlayer = PhotonNetwork.LocalPlayer;
						Hashtable val = new Hashtable();
						((Dictionary<object, object>)val).Add((object)"khug", (object)viewId);
						localPlayer.SetCustomProperties(val, (Hashtable)null, (WebFlags)null);
					}
				}
			}
			catch (Exception ex)
			{
				((BaseUnityPlugin)this).Logger.LogWarning((object)("[KHUG] could not set the player property: " + ex.Message));
			}
		}

		private static void Note(string msg)
		{
			try
			{
				Type type = AccessTools.TypeByName("PeakTextChat.TextChatDisplay");
				object obj = ((type != null) ? AccessTools.Field(type, "instance").GetValue(null) : null);
				MethodInfo methodInfo = ((type != null) ? AccessTools.Method(type, "AddMessage", new Type[1] { typeof(string) }, (Type[])null) : null);
				if (obj != null && methodInfo != null)
				{
					methodInfo.Invoke(obj, new object[1] { "<color=#FFA500>" + msg + "</color>" });
				}
			}
			catch
			{
			}
		}

		private Pose GetPose()
		{
			//IL_00d0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d5: 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_00e2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ea: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fb: Unknown result type (might be due to invalid IL or missing references)
			//IL_0100: 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_0114: 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_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_0132: Unknown result type (might be due to invalid IL or missing references)
			//IL_0141: Unknown result type (might be due to invalid IL or missing references)
			//IL_0146: Unknown result type (might be due to invalid IL or missing references)
			//IL_014b: Unknown result type (might be due to invalid IL or missing references)
			//IL_015a: Unknown result type (might be due to invalid IL or missing references)
			//IL_015f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0164: Unknown result type (might be due to invalid IL or missing references)
			//IL_0183: Unknown result type (might be due to invalid IL or missing references)
			//IL_0199: Unknown result type (might be due to invalid IL or missing references)
			//IL_01af: Unknown result type (might be due to invalid IL or missing references)
			if (_pose != null)
			{
				return _pose;
			}
			Pose pose = null;
			try
			{
				ItemDatabase instance = SingletonAsset<ItemDatabase>.Instance;
				if ((Object)(object)instance != (Object)null && instance.itemLookup != null)
				{
					foreach (KeyValuePair<ushort, Item> item in instance.itemLookup)
					{
						Item value = item.Value;
						if (!((Object)(object)value == (Object)null) && !(((Object)((Component)value).gameObject).name != "Passport"))
						{
							Transform val = value.transform.Find("Hand_L");
							Transform val2 = value.transform.Find("Hand_R");
							if ((Object)(object)val == (Object)null || (Object)(object)val2 == (Object)null)
							{
								((BaseUnityPlugin)this).Logger.LogWarning((object)"[KHUG] item 'Passport' has no two hand grips");
								break;
							}
							pose = new Pose();
							pose.ItemPos = value.defaultPos;
							pose.HandR = val2.localPosition;
							pose.HandL = Vector3.Scale(val.localPosition, value.transform.lossyScale);
							pose.RFwd = value.transform.InverseTransformDirection(val2.forward);
							pose.RUp = value.transform.InverseTransformDirection(val2.up);
							pose.LFwd = value.transform.InverseTransformDirection(val.forward);
							pose.LUp = value.transform.InverseTransformDirection(val.up);
							pose.HasRot = true;
							pose.Source = "item 'Passport' (pos " + V(pose.ItemPos) + ", Hand_L " + V(pose.HandL) + ", Hand_R " + V(pose.HandR) + ", right-hand only " + value.rightHandOnly + ")";
							break;
						}
					}
				}
			}
			catch (Exception ex)
			{
				((BaseUnityPlugin)this).Logger.LogWarning((object)("[KHUG] could not read the pose item: " + ex.Message));
				pose = null;
			}
			if (pose == null)
			{
				pose = new Pose();
			}
			_pose = pose;
			((BaseUnityPlugin)this).Logger.LogInfo((object)("[KHUG] one-sided pose from " + pose.Source));
			return pose;
		}

		private static string V(Vector3 v)
		{
			return "(" + v.x.ToString("0.00") + " " + v.y.ToString("0.00") + " " + v.z.ToString("0.00") + ")";
		}

		private void Update()
		{
			try
			{
				if (!PhotonNetwork.InRoom)
				{
					if (Hugs.Count > 0)
					{
						Hugs.Clear();
					}
					if (Pairs.Count > 0)
					{
						ReleaseAllPairs("left the lobby");
					}
					if (_propValue != 0)
					{
						SetProp(0);
					}
					TickCamera();
				}
				else
				{
					TickLocal();
					_scan -= Time.deltaTime;
					if (_scan <= 0f)
					{
						_scan = 0.1f;
						ScanOthers();
					}
					TickPairs();
					TickBlends(Time.deltaTime);
					TickCamera();
				}
			}
			catch (Exception e)
			{
				Fail("update", e);
			}
		}

		private void TickCamera()
		{
			try
			{
				Character localCharacter = Character.localCharacter;
				Hug value = null;
				bool flag = (Object)(object)localCharacter != (Object)null && (Object)(object)localCharacter.data != (Object)null && !localCharacter.data.dead && !localCharacter.data.fullyPassedOut && Hugs.TryGetValue(localCharacter, out value) && value.Active && value.Close;
				if (flag == _camOn)
				{
					return;
				}
				MainCameraMovement instance = Singleton<MainCameraMovement>.Instance;
				if ((Object)(object)instance == (Object)null)
				{
					_camOn = false;
				}
				else if (flag)
				{
					if (_isRocketing == null || !_isRocketing.Invoke(instance))
					{
						instance.StartRocketing();
						if (_spectateZoom != null)
						{
							_spectateZoom.Invoke(instance) = Mathf.Clamp(2.5f, instance.spectateZoomMin, instance.spectateZoomMax);
						}
						_camOn = true;
						((BaseUnityPlugin)this).Logger.LogInfo((object)("[KHUG] camera: pulled back (" + 250 + " cm)"));
					}
				}
				else
				{
					_camOn = false;
					if ((Object)(object)localCharacter != (Object)null)
					{
						instance.StopRocketing();
					}
					((BaseUnityPlugin)this).Logger.LogInfo((object)"[KHUG] camera: back to normal");
				}
			}
			catch (Exception e)
			{
				_camOn = false;
				Fail("camera", e);
			}
		}

		private void FixedUpdate()
		{
			//IL_00c0: 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_00c4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fb: Unknown result type (might be due to invalid IL or missing references)
			//IL_0100: 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_0123: Unknown result type (might be due to invalid IL or missing references)
			//IL_0142: Unknown result type (might be due to invalid IL or missing references)
			//IL_0147: Unknown result type (might be due to invalid IL or missing references)
			//IL_017a: Unknown result type (might be due to invalid IL or missing references)
			try
			{
				if (Pairs.Count == 0)
				{
					return;
				}
				Character localCharacter = Character.localCharacter;
				if ((Object)(object)localCharacter == (Object)null || localCharacter.refs == null || (Object)(object)localCharacter.refs.ragdoll == (Object)null)
				{
					return;
				}
				for (int i = 0; i < Pairs.Count; i++)
				{
					Pair pair = Pairs[i];
					if (pair.Close || ((Object)(object)pair.A != (Object)(object)localCharacter && (Object)(object)pair.B != (Object)(object)localCharacter))
					{
						continue;
					}
					Character val = (((Object)(object)pair.A == (Object)(object)localCharacter) ? pair.B : pair.A);
					if ((Object)(object)val == (Object)null || !Chest(localCharacter, out var pos) || !Chest(val, out var pos2))
					{
						continue;
					}
					Vector3 val2 = pos - pos2;
					val2.y = 0f;
					float magnitude = ((Vector3)(ref val2)).magnitude;
					if (magnitude >= 0.68f)
					{
						continue;
					}
					if (magnitude < 0.02f)
					{
						val2 = -localCharacter.data.lookDirection_Flat;
						val2.y = 0f;
					}
					if (((Vector3)(ref val2)).sqrMagnitude < 0.0001f)
					{
						continue;
					}
					Vector3 val3 = ((Vector3)(ref val2)).normalized * (6f * (1f - magnitude / 0.68f) + 1f);
					List<Bodypart> partList = localCharacter.refs.ragdoll.partList;
					for (int j = 0; j < partList.Count; j++)
					{
						if ((Object)(object)partList[j] != (Object)null)
						{
							partList[j].AddForce(val3, (ForceMode)5);
						}
					}
				}
			}
			catch (Exception e)
			{
				Fail("release", e);
			}
		}

		private void TickLocal()
		{
			//IL_019f: 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)
			Character localCharacter = Character.localCharacter;
			if ((Object)(object)localCharacter == (Object)null || !Hugs.TryGetValue(localCharacter, out var value) || !value.Active)
			{
				if ((Object)(object)localCharacter == (Object)null && _propValue != 0)
				{
					SetProp(0);
				}
				return;
			}
			string text = null;
			bool flag = Mutual(localCharacter, value);
			if (flag && !value.WasMutual)
			{
				value.WasMutual = true;
				value.MutualSince = Time.time;
				value.StartTime = Time.time;
				((BaseUnityPlugin)this).Logger.LogInfo((object)("[KHUG] both hug now - my time starts again (" + 12 + " s)"));
			}
			else if (!flag && value.WasMutual)
			{
				value.WasMutual = false;
				if (Time.time - value.MutualSince > 0.5f)
				{
					text = "the other scout let go";
				}
			}
			float num = Time.time - value.StartTime;
			if (text == null)
			{
				if ((Object)(object)value.Target == (Object)null)
				{
					text = "the other scout is gone";
				}
				else if ((Object)(object)value.Target.data == (Object)null || value.Target.data.dead)
				{
					text = "the other scout died";
				}
				else if ((text = WhyNot(localCharacter)) == null)
				{
					Vector3 pos;
					Vector3 pos2;
					if (num > (float)Mathf.Max(1, 12))
					{
						text = "time is up (" + 12 + " s)";
					}
					else if (num > 0.5f && (Object)(object)localCharacter.input != (Object)null && localCharacter.input.jumpWasPressed)
					{
						text = "jump";
					}
					else if (Chest(localCharacter, out pos) && Chest(value.Target, out pos2))
					{
						float num2 = Vector3.Distance(pos, pos2);
						if (num2 > (float)Mathf.Max(100, 450) * 0.01f)
						{
							text = "too far apart (" + num2.ToString("0.0") + " m)";
						}
					}
				}
			}
			if (text != null)
			{
				StopLocal(localCharacter, value, text);
			}
		}

		private void ScanOthers()
		{
			//IL_0112: Unknown result type (might be due to invalid IL or missing references)
			//IL_0114: Unknown result type (might be due to invalid IL or missing references)
			List<Character> allCharacters = Character.AllCharacters;
			for (int i = 0; i < allCharacters.Count; i++)
			{
				Character val = allCharacters[i];
				if ((Object)(object)val == (Object)null || val.IsLocal || val.isBot || val.refs == null || (Object)(object)val.refs.view == (Object)null)
				{
					continue;
				}
				Player owner = val.refs.view.Owner;
				if (owner == null)
				{
					continue;
				}
				int num = 0;
				if (owner.CustomProperties != null && ((Dictionary<object, object>)(object)owner.CustomProperties).TryGetValue((object)"khug", out object value) && value is int)
				{
					num = (int)value;
				}
				Hugs.TryGetValue(val, out var value2);
				Character val2 = ((num > 0) ? FindByView(num) : null);
				if ((Object)(object)val2 != (Object)null && ((Object)(object)val2 == (Object)(object)val || (Object)(object)val2.data == (Object)null || val2.data.dead))
				{
					val2 = null;
				}
				if ((Object)(object)val2 != (Object)null && (!Chest(val, out var pos) || !Chest(val2, out var pos2) || Vector3.Distance(pos, pos2) > 8f))
				{
					val2 = null;
				}
				if ((Object)(object)val2 == (Object)null)
				{
					if (value2 != null && value2.Active)
					{
						value2.Active = false;
						((BaseUnityPlugin)this).Logger.LogInfo((object)("[KHUG] " + NameOf(val) + " stopped hugging"));
					}
					continue;
				}
				if (value2 == null)
				{
					value2 = new Hug();
					Hugs[val] = value2;
				}
				if (!value2.Active || (Object)(object)value2.Target != (Object)(object)val2)
				{
					value2.Target = val2;
					value2.Active = true;
					value2.StartTime = Time.time;
					value2.Dist = 999f;
					((BaseUnityPlugin)this).Logger.LogInfo((object)("[KHUG] " + NameOf(val) + " hugs " + NameOf(val2)));
					if (val2.IsLocal)
					{
						Incoming(val);
					}
				}
			}
		}

		private static Character FindByView(int id)
		{
			List<Character> allCharacters = Character.AllCharacters;
			for (int i = 0; i < allCharacters.Count; i++)
			{
				Character val = allCharacters[i];
				if ((Object)(object)val != (Object)null && !val.isBot && val.refs != null && (Object)(object)val.refs.view != (Object)null && val.refs.view.ViewID == id)
				{
					return val;
				}
			}
			return null;
		}

		private void TickBlends(float dt)
		{
			if (Hugs.Count == 0)
			{
				return;
			}
			_keys.Clear();
			foreach (KeyValuePair<Character, Hug> hug2 in Hugs)
			{
				_keys.Add(hug2.Key);
			}
			for (int i = 0; i < _keys.Count; i++)
			{
				Character val = _keys[i];
				Hug hug = Hugs[val];
				if ((Object)(object)val == (Object)null)
				{
					Hugs.Remove(val);
					continue;
				}
				if ((Object)(object)hug.Target == (Object)null)
				{
					hug.Active = false;
				}
				hug.Blend = Mathf.MoveTowards(hug.Blend, hug.Active ? 1f : 0f, dt / 0.3f);
				hug.CloseBlend = Mathf.MoveTowards(hug.CloseBlend, (hug.Active && hug.Close) ? 1f : 0f, dt / 0.5f);
				if (!hug.Active && hug.Blend <= 0f)
				{
					Restore(val, hug);
					Hugs.Remove(val);
				}
			}
		}

		private static void Restore(Character c, Hug h)
		{
			try
			{
				if (h.RotSaved && c.refs != null)
				{
					if ((Object)(object)c.refs.ikLeft != (Object)null)
					{
						((TwoBoneIKConstraintData)(ref ((RigConstraint<TwoBoneIKConstraintJob, TwoBoneIKConstraintData, TwoBoneIKConstraintJobBinder<TwoBoneIKConstraintData>>)(object)c.refs.ikLeft).data)).targetRotationWeight = h.RotL;
					}
					if ((Object)(object)c.refs.ikRight != (Object)null)
					{
						((TwoBoneIKConstraintData)(ref ((RigConstraint<TwoBoneIKConstraintJob, TwoBoneIKConstraintData, TwoBoneIKConstraintJobBinder<TwoBoneIKConstraintData>>)(object)c.refs.ikRight).data)).targetRotationWeight = h.RotR;
					}
				}
			}
			catch
			{
			}
		}

		private static bool Mutual(Character c, Hug h)
		{
			if ((Object)(object)h.Target != (Object)null && Hugs.TryGetValue(h.Target, out var value) && value.Active)
			{
				return (Object)(object)value.Target == (Object)(object)c;
			}
			return false;
		}

		private void TickPairs()
		{
			//IL_022b: Unknown result type (might be due to invalid IL or missing references)
			//IL_022d: Unknown result type (might be due to invalid IL or missing references)
			//IL_022f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0234: Unknown result type (might be due to invalid IL or missing references)
			int frameCount = Time.frameCount;
			foreach (KeyValuePair<Character, Hug> hug in Hugs)
			{
				Character key = hug.Key;
				Hug value = hug.Value;
				if (!(value.Close = (Object)(object)key != (Object)null && value.Active && (Object)(object)value.Target != (Object)null && (Object)(object)key.data != (Object)null && (Object)(object)value.Target.data != (Object)null && (Object)(object)key.data.currentItem == (Object)null && (Object)(object)value.Target.data.currentItem == (Object)null && Mutual(key, value)))
				{
					continue;
				}
				Pair pair = FindPair(key, value.Target);
				if (pair == null)
				{
					pair = Engage(key, value.Target);
				}
				if (pair == null)
				{
					value.Close = false;
					continue;
				}
				if (!pair.Close)
				{
					((BaseUnityPlugin)this).Logger.LogInfo((object)("[KHUG] embrace again: " + NameOf(pair.A) + " + " + NameOf(pair.B)));
				}
				pair.Close = true;
				pair.Seen = frameCount;
			}
			for (int num = Pairs.Count - 1; num >= 0; num--)
			{
				Pair pair2 = Pairs[num];
				if (pair2.Close && pair2.Seen != frameCount)
				{
					pair2.Close = false;
					pair2.ReleaseBy = Time.time + 2.5f;
				}
				if (!pair2.Close)
				{
					string text = null;
					Vector3 pos;
					Vector3 pos2;
					if ((Object)(object)pair2.A == (Object)null || (Object)(object)pair2.B == (Object)null)
					{
						text = "one of them is gone";
					}
					else if (Time.time > pair2.ReleaseBy)
					{
						text = "after " + 2.5f.ToString("0.0") + " s";
					}
					else if (Chest(pair2.A, out pos) && Chest(pair2.B, out pos2))
					{
						Vector3 val = pos - pos2;
						val.y = 0f;
						if (((Vector3)(ref val)).magnitude > 0.68f)
						{
							text = "the two are " + ((Vector3)(ref val)).magnitude.ToString("0.00") + " m apart";
						}
					}
					if (text != null)
					{
						Collide(pair2, on: true);
						((BaseUnityPlugin)this).Logger.LogInfo((object)("[KHUG] embrace off: " + NameOf(pair2.A) + " + " + NameOf(pair2.B) + " - their bodies collide again (" + text + ")"));
						Pairs.RemoveAt(num);
					}
				}
			}
		}

		private static Pair FindPair(Character a, Character b)
		{
			for (int i = 0; i < Pairs.Count; i++)
			{
				Pair pair = Pairs[i];
				if (((Object)(object)pair.A == (Object)(object)a && (Object)(object)pair.B == (Object)(object)b) || ((Object)(object)pair.A == (Object)(object)b && (Object)(object)pair.B == (Object)(object)a))
				{
					return pair;
				}
			}
			return null;
		}

		private Pair Engage(Character a, Character b)
		{
			try
			{
				Pair pair = new Pair
				{
					A = a,
					B = b,
					CA = Solid(a),
					CB = Solid(b)
				};
				if (pair.CA.Length == 0 || pair.CB.Length == 0)
				{
					return null;
				}
				Collide(pair, on: false);
				Pairs.Add(pair);
				((BaseUnityPlugin)this).Logger.LogInfo((object)("[KHUG] embrace on: " + NameOf(a) + " + " + NameOf(b) + " - their bodies do not collide with each other now (" + pair.CA.Length + " x " + pair.CB.Length + " colliders)"));
				return pair;
			}
			catch (Exception e)
			{
				Fail("embrace", e);
				return null;
			}
		}

		private static Collider[] Solid(Character c)
		{
			Collider[] componentsInChildren = ((Component)c).GetComponentsInChildren<Collider>(true);
			List<Collider> list = new List<Collider>(componentsInChildren.Length);
			for (int i = 0; i < componentsInChildren.Length; i++)
			{
				if ((Object)(object)componentsInChildren[i] != (Object)null && !componentsInChildren[i].isTrigger)
				{
					list.Add(componentsInChildren[i]);
				}
			}
			return list.ToArray();
		}

		private static void Collide(Pair p, bool on)
		{
			for (int i = 0; i < p.CA.Length; i++)
			{
				Collider val = p.CA[i];
				if ((Object)(object)val == (Object)null)
				{
					continue;
				}
				for (int j = 0; j < p.CB.Length; j++)
				{
					Collider val2 = p.CB[j];
					if (!((Object)(object)val2 == (Object)null) && !((Object)(object)val == (Object)(object)val2))
					{
						try
						{
							Physics.IgnoreCollision(val, val2, !on);
						}
						catch
						{
						}
					}
				}
			}
		}

		private void ReleaseAllPairs(string why)
		{
			for (int i = 0; i < Pairs.Count; i++)
			{
				Collide(Pairs[i], on: true);
			}
			((BaseUnityPlugin)this).Logger.LogInfo((object)("[KHUG] " + Pairs.Count + " embrace(s) ended - bodies collide again (" + why + ")"));
			Pairs.Clear();
		}

		private static Character CharOf(CharacterAnimations a)
		{
			if (_animChar == null)
			{
				return ((Component)a).GetComponent<Character>();
			}
			return _animChar.Invoke(a);
		}

		private static bool Running(Character c, out Hug h)
		{
			h = null;
			if (Hugs.Count == 0 || (Object)(object)Instance == (Object)null || (Object)(object)c == (Object)null || !Hugs.TryGetValue(c, out h))
			{
				return false;
			}
			if (h.Blend > 0f && (Object)(object)h.Target != (Object)null && c.refs != null)
			{
				return (Object)(object)c.data != (Object)null;
			}
			return false;
		}

		private static void SetRotationPostfix(Character __instance)
		{
			//IL_0051: 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_0053: Unknown result type (might be due to invalid IL or missing references)
			//IL_0058: Unknown result type (might be due to invalid IL or missing references)
			//IL_0082: Unknown result type (might be due to invalid IL or missing references)
			//IL_0087: Unknown result type (might be due to invalid IL or missing references)
			//IL_0089: 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_00a0: Unknown result type (might be due to invalid IL or missing references)
			try
			{
				if (!Running(__instance, out var h) || h.CloseBlend <= 0f)
				{
					return;
				}
				CharacterRefs refs = __instance.refs;
				if (!((Object)(object)refs.rigCreator == (Object)null) && Chest(__instance, out var pos) && Chest(h.Target, out var pos2))
				{
					Vector3 val = pos2 - pos;
					val.y = 0f;
					if (!(((Vector3)(ref val)).sqrMagnitude < 0.0004f))
					{
						Transform transform = ((Component)refs.rigCreator).transform;
						transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(val, Vector3.up), h.CloseBlend * h.Blend);
					}
				}
			}
			catch (Exception e)
			{
				Fail("SetRotation", e);
			}
		}

		private static void ConfigureIKPostfix(CharacterAnimations __instance)
		{
			//IL_00a0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a9: 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_0105: Unknown result type (might be due to invalid IL or missing references)
			//IL_010a: Unknown result type (might be due to invalid IL or missing references)
			//IL_010c: 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_011e: Unknown result type (might be due to invalid IL or missing references)
			//IL_012e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0133: 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_01d3: 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_01da: Unknown result type (might be due to invalid IL or missing references)
			//IL_01df: Unknown result type (might be due to invalid IL or missing references)
			//IL_01f3: Unknown result type (might be due to invalid IL or missing references)
			//IL_01f8: Unknown result type (might be due to invalid IL or missing references)
			//IL_01fc: Unknown result type (might be due to invalid IL or missing references)
			//IL_0210: Unknown result type (might be due to invalid IL or missing references)
			//IL_0215: Unknown result type (might be due to invalid IL or missing references)
			//IL_021a: Unknown result type (might be due to invalid IL or missing references)
			//IL_021f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0226: Unknown result type (might be due to invalid IL or missing references)
			//IL_022b: Unknown result type (might be due to invalid IL or missing references)
			//IL_022f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0234: Unknown result type (might be due to invalid IL or missing references)
			//IL_0250: Unknown result type (might be due to invalid IL or missing references)
			//IL_0251: Unknown result type (might be due to invalid IL or missing references)
			//IL_0253: Unknown result type (might be due to invalid IL or missing references)
			//IL_0255: Unknown result type (might be due to invalid IL or missing references)
			//IL_025a: Unknown result type (might be due to invalid IL or missing references)
			//IL_025f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0261: Unknown result type (might be due to invalid IL or missing references)
			//IL_0263: Unknown result type (might be due to invalid IL or missing references)
			//IL_0265: Unknown result type (might be due to invalid IL or missing references)
			//IL_026a: Unknown result type (might be due to invalid IL or missing references)
			//IL_026f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0497: Unknown result type (might be due to invalid IL or missing references)
			//IL_036c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0379: Unknown result type (might be due to invalid IL or missing references)
			//IL_027d: Unknown result type (might be due to invalid IL or missing references)
			//IL_027f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0286: Unknown result type (might be due to invalid IL or missing references)
			//IL_028b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0290: Unknown result type (might be due to invalid IL or missing references)
			//IL_0292: Unknown result type (might be due to invalid IL or missing references)
			//IL_029e: 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_02a3: 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_02ad: Unknown result type (might be due to invalid IL or missing references)
			//IL_02b7: Unknown result type (might be due to invalid IL or missing references)
			//IL_02bc: Unknown result type (might be due to invalid IL or missing references)
			//IL_02c1: Unknown result type (might be due to invalid IL or missing references)
			//IL_02c3: Unknown result type (might be due to invalid IL or missing references)
			//IL_02c7: Unknown result type (might be due to invalid IL or missing references)
			//IL_02cc: Unknown result type (might be due to invalid IL or missing references)
			//IL_02d1: Unknown result type (might be due to invalid IL or missing references)
			//IL_02db: Unknown result type (might be due to invalid IL or missing references)
			//IL_02e0: Unknown result type (might be due to invalid IL or missing references)
			//IL_02e5: Unknown result type (might be due to invalid IL or missing references)
			//IL_03e2: Unknown result type (might be due to invalid IL or missing references)
			//IL_03e4: Unknown result type (might be due to invalid IL or missing references)
			//IL_038f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0393: Unknown result type (might be due to invalid IL or missing references)
			//IL_0398: Unknown result type (might be due to invalid IL or missing references)
			//IL_039d: Unknown result type (might be due to invalid IL or missing references)
			//IL_03a1: Unknown result type (might be due to invalid IL or missing references)
			//IL_03a6: Unknown result type (might be due to invalid IL or missing references)
			//IL_03ab: Unknown result type (might be due to invalid IL or missing references)
			//IL_03bb: Unknown result type (might be due to invalid IL or missing references)
			//IL_03bf: 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_03c9: Unknown result type (might be due to invalid IL or missing references)
			//IL_03cd: Unknown result type (might be due to invalid IL or missing references)
			//IL_03d2: Unknown result type (might be due to invalid IL or missing references)
			//IL_03d7: Unknown result type (might be due to invalid IL or missing references)
			//IL_0344: Unknown result type (might be due to invalid IL or missing references)
			//IL_0346: Unknown result type (might be due to invalid IL or missing references)
			//IL_034e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0353: Unknown result type (might be due to invalid IL or missing references)
			//IL_0355: Unknown result type (might be due to invalid IL or missing references)
			//IL_0357: Unknown result type (might be due to invalid IL or missing references)
			//IL_035f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0364: Unknown result type (might be due to invalid IL or missing references)
			//IL_041d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0422: Unknown result type (might be due to invalid IL or missing references)
			//IL_02fb: Unknown result type (might be due to invalid IL or missing references)
			//IL_02fd: Unknown result type (might be due to invalid IL or missing references)
			//IL_0302: 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_0310: 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_0314: Unknown result type (might be due to invalid IL or missing references)
			//IL_0316: Unknown result type (might be due to invalid IL or missing references)
			//IL_0318: Unknown result type (might be due to invalid IL or missing references)
			//IL_031a: Unknown result type (might be due to invalid IL or missing references)
			//IL_031f: 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_0331: Unknown result type (might be due to invalid IL or missing references)
			//IL_0333: 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_033d: 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_0461: Unknown result type (might be due to invalid IL or missing references)
			//IL_0466: Unknown result type (might be due to invalid IL or missing references)
			//IL_0486: Unknown result type (might be due to invalid IL or missing references)
			//IL_048b: Unknown result type (might be due to invalid IL or missing references)
			try
			{
				Character val = ((Hugs.Count == 0) ? null : CharOf(__instance));
				if (!Running(val, out var h))
				{
					return;
				}
				CharacterRefs refs = val.refs;
				if ((Object)(object)refs.IKHandTargetLeft == (Object)null || (Object)(object)refs.IKHandTargetRight == (Object)null || (Object)(object)refs.hip == (Object)null || (Object)(object)refs.hip.Rig == (Object)null || (Object)(object)refs.head == (Object)null)
				{
					return;
				}
				Plugin instance = Instance;
				if (!Chest(val, out var pos) || !Chest(h.Target, out var pos2))
				{
					return;
				}
				Vector3 val2 = pos2 - pos;
				val2.y = 0f;
				h.Dist = ((Vector3)(ref val2)).magnitude;
				if (((Vector3)(ref val2)).sqrMagnitude < 0.0004f)
				{
					val2 = val.data.lookDirection_Flat;
					val2.y = 0f;
				}
				if (((Vector3)(ref val2)).sqrMagnitude < 0.0001f)
				{
					return;
				}
				((Vector3)(ref val2)).Normalize();
				Vector3 val3 = Vector3.Cross(Vector3.up, val2);
				Vector3 val4 = refs.hip.transform.position - refs.hip.Rig.position;
				float num = h.CloseBlend * h.Blend;
				Bodypart p = null;
				float num2 = 0f;
				if (num > 0f && (Body(val, (BodypartType)1, out p) || Body(val, (BodypartType)2, out p)))
				{
					num2 = 2f * num;
					if (!h.NestChecked)
					{
						h.NestChecked = true;
						h.Nested = HangsOn(val, (BodypartType)10, p) && HangsOn(val, (BodypartType)4, p);
					}
				}
				h.OwnPose = (Object)(object)val.data.currentItem == (Object)null;
				if (h.OwnPose)
				{
					Pose pose = instance.GetPose();
					Quaternion val5 = Quaternion.LookRotation(val2, Vector3.up);
					float num3 = 1f;
					Vector3 val6 = refs.head.transform.position + val5 * (pose.ItemPos + new Vector3(0f, 0f, 0f));
					Vector3 handR = pose.HandR;
					Vector3 handL = pose.HandL;
					handR.x *= num3;
					handL.x *= num3;
					Vector3 val7 = val6 + val5 * handR;
					Vector3 val8 = val6 + val5 * handL;
					if (num > 0f)
					{
						Vector3 val9 = pos2 + val2 * 0.14999999f + val4;
						float num4 = 0.14f;
						Vector3 val10 = val9 + val3 * num4 + Vector3.up * -0.06f;
						Vector3 val11 = val9 - val3 * num4 + Vector3.up * -0.049999997f;
						if (num2 != 0f && h.Nested)
						{
							Quaternion val12 = Quaternion.AngleAxis(0f - num2, val3);
							Vector3 position = p.transform.position;
							val10 = position + val12 * (val10 - position);
							val11 = position + val12 * (val11 - position);
						}
						val7 = Vector3.Lerp(val7, val10, h.CloseBlend);
						val8 = Vector3.Lerp(val8, val11, h.CloseBlend);
					}
					refs.IKHandTargetRight.position = val7;
					refs.IKHandTargetLeft.position = val8;
					if (pose.HasRot)
					{
						refs.IKHandTargetRight.rotation = Quaternion.LookRotation(val5 * pose.RFwd, val5 * pose.RUp);
						refs.IKHandTargetLeft.rotation = Quaternion.LookRotation(val5 * pose.LFwd, val5 * pose.LUp);
					}
					h.Sep = Vector3.Distance(val7, val8);
				}
				if (num > 0f)
				{
					if (num2 != 0f && (Object)(object)p != (Object)null)
					{
						p.transform.RotateAround(p.transform.position, val3, num2);
					}
					if (Body(val, (BodypartType)4, out var p2))
					{
						float num5 = 40f * num;
						float num6 = 8f * num;
						if (num5 != 0f)
						{
							p2.transform.RotateAround(p2.transform.position, val2, num5);
						}
						if (num6 != 0f)
						{
							p2.transform.RotateAround(p2.transform.position, val3, num6);
						}
					}
				}
				instance.Feel(val, h, val2);
			}
			catch (Exception e)
			{
				Fail("ConfigureIK", e);
			}
		}

		private static bool HangsOn(Character c, BodypartType part, Bodypart on)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			if (Body(c, part, out var p))
			{
				return p.transform.IsChildOf(on.transform);
			}
			return false;
		}

		private void Feel(Character c, Hug h, Vector3 toTarget)
		{
			//IL_0046: Unknown result type (might be due to invalid IL or missing references)
			//IL_0051: Unknown result type (might be due to invalid IL or missing references)
			//IL_0056: Unknown result type (might be due to invalid IL or missing references)
			//IL_005b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0083: Unknown result type (might be due to invalid IL or missing references)
			//IL_0087: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a7: 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)
			//IL_00af: 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_00ba: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e7: 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_01ef: 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_0155: Unknown result type (might be due to invalid IL or missing references)
			//IL_015a: Unknown result type (might be due to invalid IL or missing references)
			//IL_015f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0222: Unknown result type (might be due to invalid IL or missing references)
			if (h.Blend < 0.5f)
			{
				return;
			}
			int num = 100;
			if (h.OwnPose && num > 0 && Body(c, (BodypartType)7, out var p) && Body(c, (BodypartType)10, out var p2))
			{
				Vector3 val = p2.Rig.position - p.Rig.position;
				float magnitude = ((Vector3)(ref val)).magnitude;
				float num2 = magnitude - h.Sep;
				if (num2 > 0.03f && magnitude > 0.001f)
				{
					Vector3 val2 = val / magnitude * (Mathf.Min(150f * num2, 50f) * (float)num * 0.01f);
					p.AddForce(val2, (ForceMode)5);
					p2.AddForce(-val2, (ForceMode)5);
				}
			}
			int num3 = 100;
			if (!c.IsLocal || !h.Active || num3 <= 0)
			{
				return;
			}
			float num6;
			if (h.Close)
			{
				float num4 = (float)Mathf.Max(5, 36) * 0.01f;
				float num5 = 0f;
				CharacterRefs refs = h.Target.refs;
				if (refs != null && (Object)(object)refs.hip != (Object)null && (Object)(object)refs.hip.Rig != (Object)null)
				{
					num5 = Vector3.Dot(c.refs.hip.Rig.linearVelocity - refs.hip.Rig.linearVelocity, toTarget);
				}
				num6 = Mathf.Clamp(25f * (h.Dist - num4) - 4f * num5, -8f, 8f) * (float)num3 * 0.01f;
			}
			else
			{
				float num7 = Mathf.Clamp01((h.Dist - 0.5f) / 0.5f);
				num6 = 6f * (float)num3 * 0.01f * num7 * (Mutual(c, h) ? 2f : 1f);
			}
			if (num6 == 0f)
			{
				return;
			}
			Vector3 val3 = toTarget * num6;
			List<Bodypart> partList = c.refs.ragdoll.partList;
			for (int i = 0; i < partList.Count; i++)
			{
				if ((Object)(object)partList[i] != (Object)null)
				{
					partList[i].AddForce(val3, (ForceMode)5);
				}
			}
		}

		private static void HandleIKPostfix(CharacterAnimations __instance)
		{
			try
			{
				Character val = ((Hugs.Count == 0) ? null : CharOf(__instance));
				if (!Running(val, out var h) || (Object)(object)val.data.currentItem != (Object)null)
				{
					return;
				}
				CharacterRefs refs = val.refs;
				if ((Object)(object)refs.ikRig == (Object)null || (Object)(object)refs.ikLeft == (Object)null || (Object)(object)refs.ikRight == (Object)null)
				{
					return;
				}
				float blend = h.Blend;
				refs.ikRig.weight = Mathf.Max(refs.ikRig.weight, blend);
				((RigConstraint<TwoBoneIKConstraintJob, TwoBoneIKConstraintData, TwoBoneIKConstraintJobBinder<TwoBoneIKConstraintData>>)(object)refs.ikLeft).weight = Mathf.Max(((RigConstraint<TwoBoneIKConstraintJob, TwoBoneIKConstraintData, TwoBoneIKConstraintJobBinder<TwoBoneIKConstraintData>>)(object)refs.ikLeft).weight, blend);
				((RigConstraint<TwoBoneIKConstraintJob, TwoBoneIKConstraintData, TwoBoneIKConstraintJobBinder<TwoBoneIKConstraintData>>)(object)refs.ikRight).weight = Mathf.Max(((RigConstraint<TwoBoneIKConstraintJob, TwoBoneIKConstraintData, TwoBoneIKConstraintJobBinder<TwoBoneIKConstraintData>>)(object)refs.ikRight).weight, blend);
				float num = ((Instance._pose != null && !Instance._pose.HasRot) ? 1f : h.CloseBlend);
				if (num > 0f || h.RotSaved)
				{
					if (!h.RotSaved)
					{
						h.RotL = ((TwoBoneIKConstraintData)(ref ((RigConstraint<TwoBoneIKConstraintJob, TwoBoneIKConstraintData, TwoBoneIKConstraintJobBinder<TwoBoneIKConstraintData>>)(object)refs.ikLeft).data)).targetRotationWeight;
						h.RotR = ((TwoBoneIKConstraintData)(ref ((RigConstraint<TwoBoneIKConstraintJob, TwoBoneIKConstraintData, TwoBoneIKConstraintJobBinder<TwoBoneIKConstraintData>>)(object)refs.ikRight).data)).targetRotationWeight;
						h.RotSaved = true;
					}
					((TwoBoneIKConstraintData)(ref ((RigConstraint<TwoBoneIKConstraintJob, TwoBoneIKConstraintData, TwoBoneIKConstraintJobBinder<TwoBoneIKConstraintData>>)(object)refs.ikLeft).data)).targetRotationWeight = Mathf.Lerp(h.RotL, 0f, num * blend);
					((TwoBoneIKConstraintData)(ref ((RigConstraint<TwoBoneIKConstraintJob, TwoBoneIKConstraintData, TwoBoneIKConstraintJobBinder<TwoBoneIKConstraintData>>)(object)refs.ikRight).data)).targetRotationWeight = Mathf.Lerp(h.RotR, 0f, num * blend);
				}
			}
			catch (Exception e)
			{
				Fail("HandleIK", e);
			}
		}

		private static void AnimUpdatePostfix(CharacterAnimations __instance)
		{
			try
			{
				Character val = ((Hugs.Count == 0) ? null : CharOf(__instance));
				if (!Running(val, out var h))
				{
					return;
				}
				int num = 40;
				if (num == 0)
				{
					return;
				}
				Animator animator = val.refs.animator;
				if ((Object)(object)animator == (Object)null)
				{
					return;
				}
				if (!h.LeanChecked)
				{
					h.LeanChecked = true;
					h.HasLean = HasParam(animator, LEAN_HASH);
				}
				if (h.HasLean)
				{
					float num2 = h.Blend * (1f - h.CloseBlend) * Mathf.InverseLerp(1.4f, 0.7f, h.Dist);
					if (!(num2 <= 0f))
					{
						animator.SetFloat(LEAN_HASH, Mathf.Lerp(animator.GetFloat(LEAN_HASH), (float)num * 0.01f, num2));
					}
				}
			}
			catch (Exception e)
			{
				Fail("lean", e);
			}
		}

		private static bool HasParam(Animator a, int hash)
		{
			try
			{
				AnimatorControllerParameter[] parameters = a.parameters;
				for (int i = 0; i < parameters.Length; i++)
				{
					if (parameters[i].nameHash == hash)
					{
						return true;
					}
				}
			}
			catch
			{
			}
			return false;
		}

		private static bool Chest(Character c, out Vector3 pos)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_0035: Unknown result type (might be due to invalid IL or missing references)
			//IL_0036: Unknown result type (might be due to invalid IL or missing references)
			//IL_0037: Unknown result type (might be due to invalid IL or missing references)
			//IL_0041: Unknown result type (might be due to invalid IL or missing references)
			//IL_0046: Unknown result type (might be due to invalid IL or missing references)
			//IL_0095: 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)
			pos = Vector3.zero;
			if ((Object)(object)c == (Object)null || c.refs == null)
			{
				return false;
			}
			if (Part(c, (BodypartType)5, out var pos2) && Part(c, (BodypartType)8, out var pos3))
			{
				pos = (pos2 + pos3) * 0.5f;
				return true;
			}
			if (Part(c, (BodypartType)2, out pos))
			{
				return true;
			}
			if ((Object)(object)c.refs.hip != (Object)null && (Object)(object)c.refs.hip.Rig != (Object)null)
			{
				pos = c.refs.hip.Rig.position;
				return true;
			}
			return false;
		}

		private static bool Body(Character c, BodypartType type, out Bodypart p)
		{
			//IL_0026: Unknown result type (might be due to invalid IL or missing references)
			p = null;
			CharacterRagdoll ragdoll = c.refs.ragdoll;
			if ((Object)(object)ragdoll != (Object)null && ragdoll.partDict != null && ragdoll.partDict.TryGetValue(type, out p) && (Object)(object)p != (Object)null)
			{
				return (Object)(object)p.Rig != (Object)null;
			}
			return false;
		}

		private static bool Part(Character c, BodypartType type, out Vector3 pos)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//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_000c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			if (!Body(c, type, out var p))
			{
				pos = Vector3.zero;
				return false;
			}
			pos = p.Rig.position;
			return true;
		}

		private static string NameOf(Character c)
		{
			try
			{
				string obj = c.refs.view.Owner.NickName ?? "?";
				StringBuilder stringBuilder = new StringBuilder(obj.Length);
				bool flag = false;
				string text = obj;
				foreach (char c2 in text)
				{
					switch (c2)
					{
					case '<':
						flag = true;
						continue;
					case '>':
						flag = false;
						continue;
					}
					if (!flag)
					{
						stringBuilder.Append(c2);
					}
				}
				return stringBuilder.ToString();
			}
			catch
			{
				return "?";
			}
		}

		private static void Fail(string where, Exception e)
		{
			if (_errors < 5 && !((Object)(object)Instance == (Object)null))
			{
				_errors++;
				((BaseUnityPlugin)Instance).Logger.LogError((object)("[KHUG] " + where + ": " + e.GetType().Name + " - " + e.Message + ((_errors == 5) ? " (further errors are not logged)" : "")));
			}
		}
	}
}