Decompiled source of GTFOClientSidePrediction v0.5.1

ClientSidePrediction.dll

Decompiled 3 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using API;
using Agents;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using BepInEx.Unity.IL2CPP;
using ClientSidePrediction.BepInEx;
using Enemies;
using GameData;
using HarmonyLib;
using Il2CppInterop.Runtime.Injection;
using Il2CppInterop.Runtime.InteropTypes;
using Il2CppInterop.Runtime.InteropTypes.Arrays;
using Il2CppSystem;
using Il2CppSystem.Collections.Generic;
using Microsoft.CodeAnalysis;
using Player;
using SNetwork;
using ShaderValueAnimation;
using TMPro;
using UnityEngine;
using UnityEngine.AI;
using UnityEngine.Analytics;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETCoreApp,Version=v6.0", FrameworkDisplayName = ".NET 6.0")]
[assembly: AssemblyCompany("ClientSidePrediction")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+834709890df40aa7a4c2f7252e9a8a22c7b2ab19")]
[assembly: AssemblyProduct("ClientSidePrediction")]
[assembly: AssemblyTitle("ClientSidePrediction")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
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 API
{
	[HarmonyPatch(typeof(GameDataInit))]
	internal class GameDataInit_Patches
	{
		[HarmonyPatch("Initialize")]
		[HarmonyWrapSafe]
		[HarmonyPostfix]
		public static void Initialize_Postfix()
		{
			Analytics.enabled = false;
		}
	}
	internal static class APILogger
	{
		private static readonly ManualLogSource logger;

		static APILogger()
		{
			//IL_0005: Unknown result type (might be due to invalid IL or missing references)
			//IL_000f: Expected O, but got Unknown
			logger = new ManualLogSource("Rand-API");
			Logger.Sources.Add((ILogSource)(object)logger);
		}

		private static string Format(string module, object msg)
		{
			return $"[{module}]: {msg}";
		}

		public static void Info(string module, object data)
		{
			logger.LogMessage((object)Format(module, data));
		}

		public static void Verbose(string module, object data)
		{
		}

		public static void Log(object data)
		{
			logger.LogDebug((object)Format("ClientSidePrediction", data));
		}

		public static void Debug(object data)
		{
			if (ConfigManager.Debug)
			{
				Log(data);
			}
		}

		public static void Warn(object data)
		{
			logger.LogWarning((object)Format("ClientSidePrediction", data));
		}

		public static void Error(object data)
		{
			logger.LogError((object)Format("ClientSidePrediction", data));
		}
	}
}
namespace ClientSidePrediction
{
	[HarmonyPatch]
	internal class Forecast
	{
		private static float lerpFactor = 7.5f;

		private static Vector3 sentPos = Vector3.zero;

		private static Vector3 prevPos = Vector3.zero;

		private static Vector3 oldVel = Vector3.zero;

		private static CharacterController? characterController = null;

		private static long prevTimestamp = 0L;

		[HarmonyPatch(typeof(PlayerSync), "SendLocomotion")]
		[HarmonyPrefix]
		private static void Prefix_SendLocomotion(PlayerSync __instance, PLOC_State state, ref Vector3 pos, Vector3 lookDir, float velFwd, float velRight)
		{
			//IL_0008: Unknown result type (might be due to invalid IL or missing references)
			//IL_000a: Invalid comparison between Unknown and I4
			//IL_000c: Unknown result type (might be due to invalid IL or missing references)
			//IL_000f: Invalid comparison between Unknown and I4
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			//IL_0014: Invalid comparison between Unknown and I4
			//IL_0036: Unknown result type (might be due to invalid IL or missing references)
			//IL_003b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0040: 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_004b: 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_0052: Unknown result type (might be due to invalid IL or missing references)
			//IL_0086: Unknown result type (might be due to invalid IL or missing references)
			//IL_008b: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ad: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b2: 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_00a1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a6: 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_00d9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00df: 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_00eb: 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_00cd: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d2: 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_0139: 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_0161: Unknown result type (might be due to invalid IL or missing references)
			//IL_0166: Unknown result type (might be due to invalid IL or missing references)
			//IL_016b: Unknown result type (might be due to invalid IL or missing references)
			//IL_017b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0180: Unknown result type (might be due to invalid IL or missing references)
			//IL_018f: 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_019b: Unknown result type (might be due to invalid IL or missing references)
			if (SNet.IsMaster || (int)state == 7 || (int)state == 9 || (int)state == 15)
			{
				return;
			}
			long now = LatencyTracker.Now;
			float num = Mathf.Min((float)(now - prevTimestamp), 1000f) / 1000f;
			Vector3 val = (pos - prevPos) / num;
			prevPos = pos;
			prevTimestamp = now;
			float num2 = Mathf.Min(LatencyTracker.Ping, 1f) / 2f;
			if (num2 <= 0f)
			{
				return;
			}
			Vector3 val2 = __instance.m_agent.Locomotion.m_input;
			if ((double)((Vector3)(ref val2)).sqrMagnitude <= 0.01)
			{
				sentPos = pos;
				return;
			}
			val2 = pos - prevPos;
			if (((Vector3)(ref val2)).sqrMagnitude > 9f)
			{
				sentPos = pos;
				return;
			}
			oldVel = val;
			sentPos = pos + val * num2;
			if ((Object)(object)characterController == (Object)null)
			{
				characterController = ((Il2CppObjectBase)__instance.m_agent).Cast<LocalPlayerAgent>().m_playerCharacterController.m_characterController;
			}
			if (!((Object)(object)characterController == (Object)null))
			{
				Vector3 position = ((Component)characterController).transform.position;
				if (!characterController.isGrounded)
				{
					sentPos.y = pos.y;
				}
				characterController.Move(sentPos - pos);
				sentPos = ((Component)characterController).transform.position;
				((Component)characterController).transform.position = position;
				pos = sentPos;
			}
		}
	}
	[HarmonyPatch]
	internal class LatencyTracker : MonoBehaviour
	{
		internal static float ping;

		private static long sendTime;

		private static int index;

		private static bool expected;

		private static bool running;

		private static pNavMarkerState visibleState;

		private static float markerHideTimer;

		private static bool show;

		private static float timer;

		public static float Ping
		{
			get
			{
				if (!((Object)(object)SNet.Master == (Object)null) && !SNet.IsMaster)
				{
					return ping / 1000f;
				}
				return 0f;
			}
		}

		public static long Now => ((DateTimeOffset)DateTime.Now).ToUnixTimeMilliseconds();

		public static void OnGameplayStarted()
		{
			//IL_0016: Unknown result type (might be due to invalid IL or missing references)
			timer = 0f;
			running = true;
			visibleState.status = (eNavMarkerStatus)1;
		}

		[HarmonyPatch(typeof(RundownManager), "EndGameSession")]
		[HarmonyPrefix]
		private static void EndGameSession()
		{
			running = false;
		}

		[HarmonyPatch(typeof(SNet_SessionHub), "LeaveHub")]
		[HarmonyPrefix]
		private static void LeaveHub()
		{
			running = false;
		}

		[HarmonyPatch(typeof(GuiManager), "AttemptSetPlayerPingStatus")]
		[HarmonyPrefix]
		private static void GuiManagerHide(PlayerAgent sourceAgent, bool visible, Vector3 worldPos, eNavMarkerStyle style)
		{
			//IL_0019: Unknown result type (might be due to invalid IL or missing references)
			if (!visible)
			{
				markerHideTimer = Clock.Time - 1f;
				visibleState.status = (eNavMarkerStatus)1;
			}
			else
			{
				show = false;
			}
		}

		[HarmonyPatch(typeof(LocalPlayerAgent), "Update")]
		[HarmonyPrefix]
		private static void Update(LocalPlayerAgent __instance)
		{
			//IL_006d: Unknown result type (might be due to invalid IL or missing references)
			//IL_007e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0084: Invalid comparison between Unknown and I4
			//IL_0088: Unknown result type (might be due to invalid IL or missing references)
			//IL_0091: 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 (SNet.IsMaster || !running)
			{
				return;
			}
			index = ((PlayerAgent)__instance).Owner.PlayerSlotIndex();
			SyncedNavMarkerWrapper val = ((Il2CppArrayBase<SyncedNavMarkerWrapper>)(object)GuiManager.Current.m_playerPings)[index];
			timer += Time.deltaTime;
			if (!(timer > 1f))
			{
				return;
			}
			timer = 0f;
			if (Clock.Time > markerHideTimer)
			{
				visibleState.status = (eNavMarkerStatus)1;
				if (!expected && (int)visibleState.status == 1)
				{
					pNavMarkerInteraction val2 = default(pNavMarkerInteraction);
					val2.type = (eNavMarkerInteractionType)1;
					sendTime = Now;
					expected = true;
					val.m_stateReplicator.AttemptInteract(val2);
				}
			}
		}

		[HarmonyPatch(typeof(SyncedNavMarkerWrapper), "OnStateChange")]
		[HarmonyPrefix]
		private static void OnRecievePingStatus(SyncedNavMarkerWrapper __instance, pNavMarkerState oldState, pNavMarkerState newState, bool isDropinState)
		{
			//IL_0031: Unknown result type (might be due to invalid IL or missing references)
			//IL_0032: Unknown result type (might be due to invalid IL or missing references)
			//IL_0070: Unknown result type (might be due to invalid IL or missing references)
			//IL_0071: Unknown result type (might be due to invalid IL or missing references)
			//IL_0077: Invalid comparison between Unknown and I4
			//IL_0039: Unknown result type (might be due to invalid IL or missing references)
			//IL_003a: Unknown result type (might be due to invalid IL or missing references)
			//IL_007e: 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)
			//IL_0096: 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_00a7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b3: 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_00d4: Unknown result type (might be due to invalid IL or missing references)
			if (!SNet.IsMaster && running && __instance.m_playerIndex == index)
			{
				if (!show && Clock.Time > markerHideTimer && (int)newState.status == 0)
				{
					visibleState = newState;
					markerHideTimer = Clock.Time + __instance.AutoHideDelay;
					show = true;
					expected = false;
				}
				else if (show && Clock.Time <= markerHideTimer && (int)newState.status == 1 && (int)visibleState.status == 0)
				{
					expected = false;
					pNavMarkerInteraction val = default(pNavMarkerInteraction);
					val.type = (eNavMarkerInteractionType)0;
					val.style = visibleState.style;
					val.worldPos = visibleState.worldPos;
					val.terminalItemId = visibleState.terminalItemId;
					__instance.m_stateReplicator.AttemptInteract(val);
				}
				else if (expected)
				{
					expected = false;
					float num = Now - sendTime;
					ping = Mathf.Clamp(0.5f * ping + 0.5f * num, 0f, 1000f);
				}
			}
		}

		[HarmonyPatch(typeof(PUI_LocalPlayerStatus), "UpdateBPM")]
		[HarmonyWrapSafe]
		[HarmonyPostfix]
		private static void Initialize_Postfix(PUI_LocalPlayerStatus __instance)
		{
			TextMeshPro pulseText = __instance.m_pulseText;
			((TMP_Text)pulseText).text = ((TMP_Text)pulseText).text + " | " + (SNet.IsMaster ? "IsHost" : $"{(int)ping} ms");
		}
	}
	[HarmonyPatch]
	internal class Prediction
	{
		public class EnemyPredict : MonoBehaviour
		{
			public EnemyAgent agent;

			public NavMeshAgent navMeshAgent;

			public EnemyAI ai;

			public int lastAnimIndex;

			public float lastReceivedAttack;

			public float triggeredTongue;

			public float lastSound;

			public bool predictTongue;

			public AgentAbility type = (AgentAbility)1;

			public Vector3 targetPos;

			public long prevTimestamp;

			public uint lastReceivedTick = uint.MaxValue;

			public Vector3 vel = Vector3.zero;

			public void Setup(EnemyAgent agent)
			{
				//IL_0052: 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_0095: Unknown result type (might be due to invalid IL or missing references)
				this.agent = agent;
				ai = agent.AI;
				navMeshAgent = ((Il2CppObjectBase)((AgentAI)agent.AI).m_navMeshAgent).Cast<NavMeshAgentProxy>().m_agent;
				predictTongue = CheckAbilityTypeHasTongue((AgentAbility)1);
				if (!predictTongue)
				{
					predictTongue = CheckAbilityTypeHasTongue((AgentAbility)2);
					type = (AgentAbility)2;
				}
				if (predictTongue && ConfigManager.DisableTonguePredictOnEnemiesWithMelee && (CheckAbilityTypeHasMelee((AgentAbility)1) || CheckAbilityTypeHasMelee((AgentAbility)2)))
				{
					predictTongue = false;
					APILogger.Debug("Disabled tongue for enemy as it has a melee ability.");
				}
				targetPos = ((Component)agent).transform.position;
			}

			private void FixedUpdate()
			{
				//IL_000b: Unknown result type (might be due to invalid IL or missing references)
				//IL_0011: Invalid comparison between Unknown and I4
				//IL_002f: Unknown result type (might be due to invalid IL or missing references)
				//IL_003f: Unknown result type (might be due to invalid IL or missing references)
				//IL_0045: 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_005e: Unknown result type (might be due to invalid IL or missing references)
				if ((int)agent.Locomotion.CurrentStateEnum != 2)
				{
					((Component)agent).transform.position = NavMeshMove(navMeshAgent, ((Component)agent).transform.position, ExpDecay(((Component)agent).transform.position, targetPos, 2.5f, Time.fixedDeltaTime));
				}
			}

			private bool CheckAbilityTypeHasTongue(AgentAbility type)
			{
				//IL_000b: Unknown result type (might be due to invalid IL or missing references)
				//IL_0031: Unknown result type (might be due to invalid IL or missing references)
				//IL_0037: Invalid comparison between Unknown and I4
				EnemyAbility ability = agent.Abilities.GetAbility(type, 0);
				if ((Object)(object)ability == (Object)null)
				{
					return false;
				}
				EAB_MovingEnemeyTentacle val = ((Il2CppObjectBase)ability).TryCast<EAB_MovingEnemeyTentacle>();
				if ((Object)(object)val == (Object)null)
				{
					return false;
				}
				return (int)val.m_type == 0;
			}

			private bool CheckAbilityTypeHasMelee(AgentAbility type)
			{
				//IL_000b: Unknown result type (might be due to invalid IL or missing references)
				EnemyAbility ability = agent.Abilities.GetAbility(type, 0);
				if ((Object)(object)ability == (Object)null)
				{
					return false;
				}
				if ((Object)(object)((Il2CppObjectBase)ability).TryCast<EAB_StrikerMelee>() == (Object)null)
				{
					return false;
				}
				return true;
			}
		}

		public static Dictionary<IntPtr, EnemyPredict> map = new Dictionary<IntPtr, EnemyPredict>();

		[HarmonyPatch(typeof(RundownManager), "EndGameSession")]
		[HarmonyPrefix]
		private static void EndGameSession()
		{
			map.Clear();
		}

		[HarmonyPatch(typeof(SNet_SessionHub), "LeaveHub")]
		[HarmonyPrefix]
		private static void LeaveHub()
		{
			map.Clear();
		}

		[HarmonyPatch(typeof(Vector4Transition), "FeedMaterial")]
		[HarmonyPrefix]
		private static void FeedMaterial(Vector4Transition __instance, List<ISVA_MaterialHolder> mats, Vector4 value, ref float t)
		{
			if (float.IsNaN(t))
			{
				t = 0f;
			}
		}

		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		private static Vector3 ExpDecay(Vector3 a, Vector3 b, float decay, float dt)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_0003: 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)
			//IL_0016: Unknown result type (might be due to invalid IL or missing references)
			return b + (a - b) * Mathf.Exp((0f - decay) * dt);
		}

		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		private static float ExpDecay(float a, float b, float decay, float dt)
		{
			return b + (a - b) * Mathf.Exp((0f - decay) * dt);
		}

		private static Vector3 NavMeshMove(NavMeshAgent agent, Vector3 start, Vector3 target, float maxDistance = 5f)
		{
			//IL_0000: 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_0013: Unknown result type (might be due to invalid IL or missing references)
			//IL_0018: Unknown result type (might be due to invalid IL or missing references)
			//IL_0019: 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_0032: Unknown result type (might be due to invalid IL or missing references)
			//IL_002c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0031: Unknown result type (might be due to invalid IL or missing references)
			NavMeshHit val = default(NavMeshHit);
			if (NavMesh.SamplePosition(target, ref val, maxDistance, agent.areaMask))
			{
				Vector3 position = ((NavMeshHit)(ref val)).position;
				if (NavMesh.Raycast(start, position, ref val, agent.areaMask))
				{
					position = ((NavMeshHit)(ref val)).position;
				}
				return position;
			}
			return start;
		}

		[HarmonyPatch(typeof(ES_EnemyAttackBase), "RecieveAttackStart")]
		[HarmonyPrefix]
		private static void ES_StrikerAttack_RecieveAttackStart(ES_EnemyAttackBase __instance, pES_EnemyAttackData attackData)
		{
			if (SNet.IsMaster || ((Il2CppObjectBase)__instance).TryCast<ES_StrikerAttack>() == null)
			{
				return;
			}
			ES_PathMove val = ((Il2CppObjectBase)((ES_Base)__instance).m_enemyAgent.Locomotion.PathMove).TryCast<ES_PathMove>();
			if (val == null)
			{
				return;
			}
			IntPtr pointer = ((Il2CppObjectBase)val.m_positionBuffer).Pointer;
			if (map.ContainsKey(pointer))
			{
				EnemyPredict enemyPredict = map[pointer];
				if (!(enemyPredict.lastReceivedAttack < 0f))
				{
					enemyPredict.lastReceivedAttack = Clock.Time;
				}
			}
		}

		[HarmonyPatch(typeof(ES_StrikerAttack), "OnAttackWindUp")]
		[HarmonyPrefix]
		private static bool ES_StrikerAttack_OnAttackWindUp(ES_StrikerAttack __instance, int attackIndex, AgentAbility abilityType, int abilityIndex)
		{
			//IL_004f: 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_00cf: Unknown result type (might be due to invalid IL or missing references)
			if (SNet.IsMaster)
			{
				return true;
			}
			ES_PathMove val = ((Il2CppObjectBase)((ES_Base)__instance).m_enemyAgent.Locomotion.PathMove).TryCast<ES_PathMove>();
			if (val == null)
			{
				return true;
			}
			IntPtr pointer = ((Il2CppObjectBase)val.m_positionBuffer).Pointer;
			if (!map.ContainsKey(pointer))
			{
				return true;
			}
			EAB_MovingEnemeyTentacle val2 = ((Il2CppObjectBase)((ES_Base)__instance).m_ai.m_enemyAgent.Abilities.GetAbility(abilityType, 0)).TryCast<EAB_MovingEnemeyTentacle>();
			if ((Object)(object)val2 == (Object)null)
			{
				return true;
			}
			EnemyPredict enemyPredict = map[pointer];
			if (Clock.Time < enemyPredict.lastSound + 0.5f)
			{
				__instance.m_tentacleAbility = val2;
				((ES_Base)__instance).m_locomotion.m_animator.CrossFadeInFixedTime(((Il2CppArrayBase<int>)(object)EnemyLocomotion.s_hashAbilityFires)[attackIndex], ((ES_Base)__instance).m_enemyAgent.EnemyMovementData.BlendIntoAttackAnim);
				((ES_Base)__instance).m_enemyAgent.Appearance.InterpolateGlow(Color.op_Implicit(__instance.m_attackGlowColor), __instance.m_attackGlowLocationEnd, ((ES_EnemyAttackBase)__instance).m_attackWindupDuration * 1.2f);
				return false;
			}
			enemyPredict.lastSound = Clock.Time;
			return true;
		}

		[HarmonyPatch(typeof(EnemySync), "OnSpawn")]
		[HarmonyPostfix]
		private static void OnSpawn(EnemySync __instance)
		{
			if (!SNet.IsMaster && ((Il2CppObjectBase)__instance.m_agent.Locomotion.PathMove).TryCast<ES_PathMove>() != null)
			{
				EnemyPredict enemyPredict = ((Component)__instance.m_agent).gameObject.AddComponent<EnemyPredict>();
				enemyPredict.Setup(__instance.m_agent);
				map.Add(((Il2CppObjectBase)((Il2CppObjectBase)__instance.m_agent.Locomotion.PathMove).Cast<ES_PathMove>().m_positionBuffer).Pointer, enemyPredict);
			}
		}

		[HarmonyPatch(typeof(EnemySync), "OnDespawn")]
		[HarmonyPostfix]
		private static void OnDespawn(EnemySync __instance)
		{
			if (!SNet.IsMaster)
			{
				ES_PathMove val = ((Il2CppObjectBase)__instance.m_agent.Locomotion.PathMove).TryCast<ES_PathMove>();
				if (val != null)
				{
					map.Remove(((Il2CppObjectBase)val.m_positionBuffer).Pointer);
				}
			}
		}

		[HarmonyPatch(typeof(EnemyBehaviour), "ChangeState", new Type[] { typeof(EB_States) })]
		[HarmonyPrefix]
		private static void Behaviour_ChangeState(EnemyBehaviour __instance, EB_States state)
		{
			//IL_0009: Unknown result type (might be due to invalid IL or missing references)
			//IL_000e: 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)
			//IL_0014: Invalid comparison between Unknown and I4
			if (!SNet.IsMaster && __instance.m_currentStateName != state && (int)state == 19)
			{
				ES_PathMove val = ((Il2CppObjectBase)__instance.m_ai.m_enemyAgent.Locomotion.PathMove).TryCast<ES_PathMove>();
				if (val != null)
				{
					map.Remove(((Il2CppObjectBase)val.m_positionBuffer).Pointer);
				}
			}
		}

		[HarmonyPatch(typeof(ES_PathMove), "RecieveStateData")]
		[HarmonyPrefix]
		private static bool ES_PathMove_RecieveStateData(ES_PathMove __instance, pES_PathMoveData incomingData)
		{
			//IL_0050: 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_00be: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c3: Unknown result type (might be due to invalid IL or missing references)
			//IL_0059: Unknown result type (might be due to invalid IL or missing references)
			//IL_0143: Unknown result type (might be due to invalid IL or missing references)
			//IL_0097: Unknown result type (might be due to invalid IL or missing references)
			//IL_0098: Unknown result type (might be due to invalid IL or missing references)
			//IL_009e: 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_00aa: 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)
			if (SNet.IsMaster)
			{
				return true;
			}
			float num = Mathf.Min(LatencyTracker.Ping, 1f);
			if (num <= 0f)
			{
				return true;
			}
			IntPtr pointer = ((Il2CppObjectBase)__instance.m_positionBuffer).Pointer;
			if (!map.ContainsKey(pointer))
			{
				return true;
			}
			EnemyPredict enemyPredict = map[pointer];
			if (enemyPredict.lastReceivedTick != incomingData.Tick)
			{
				enemyPredict.lastReceivedTick = incomingData.Tick;
				long now = LatencyTracker.Now;
				if (enemyPredict.prevTimestamp == 0L)
				{
					enemyPredict.prevTimestamp = now;
				}
				float num2 = (float)(now - enemyPredict.prevTimestamp) / 1000f;
				if (num2 > 0f)
				{
					enemyPredict.vel = (incomingData.Position - enemyPredict.targetPos) / num2;
					enemyPredict.prevTimestamp = now;
				}
			}
			enemyPredict.targetPos = incomingData.Position;
			float num3 = enemyPredict.agent.Locomotion.AnimHandle.TentacleAttackWindUpLen / enemyPredict.agent.Locomotion.AnimSpeedOrg;
			float num4 = enemyPredict.triggeredTongue + num3;
			if (Clock.Time < num4)
			{
				if (enemyPredict.lastReceivedAttack < 0f && Clock.Time - enemyPredict.triggeredTongue > num)
				{
					APILogger.Debug("Mispredicted tongue animation!");
					enemyPredict.triggeredTongue = num4;
					enemyPredict.lastReceivedAttack = 0f;
					return true;
				}
				__instance.m_positionBuffer.Push(incomingData);
				return false;
			}
			return true;
		}

		[HarmonyPatch(typeof(ES_PathMove), "SyncUpdate")]
		[HarmonyPostfix]
		private static void ES_PathMove_SyncUpdate(ES_PathMove __instance)
		{
			//IL_004c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0052: Invalid comparison between Unknown and I4
			//IL_009e: 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_00ab: Unknown result type (might be due to invalid IL or missing references)
			//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_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_0157: Unknown result type (might be due to invalid IL or missing references)
			//IL_016e: Unknown result type (might be due to invalid IL or missing references)
			//IL_017e: Unknown result type (might be due to invalid IL or missing references)
			//IL_018b: Unknown result type (might be due to invalid IL or missing references)
			//IL_01bf: 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_01d2: 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_01e4: Unknown result type (might be due to invalid IL or missing references)
			//IL_021c: 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)
			if (SNet.IsMaster || (int)LatencyTracker.ping < ConfigManager.TonguePredictThreshold)
			{
				return;
			}
			IntPtr pointer = ((Il2CppObjectBase)__instance.m_positionBuffer).Pointer;
			if (!map.ContainsKey(pointer))
			{
				return;
			}
			EnemyPredict enemyPredict = map[pointer];
			if (!enemyPredict.predictTongue)
			{
				return;
			}
			PlayerAgent localPlayerAgent = PlayerManager.GetLocalPlayerAgent();
			float num = (((int)enemyPredict.type == 1) ? enemyPredict.agent.EnemyBehaviorData.MeleeAttackDistance.Max : enemyPredict.agent.EnemyBehaviorData.RangedAttackDistance.Max);
			float num2 = Mathf.Min(LatencyTracker.Ping, 1f) / 2f;
			Vector3 val = ((Agent)enemyPredict.agent).EyePosition + enemyPredict.vel * num2;
			Vector3 val2 = ((Agent)localPlayerAgent).AimTarget.position - val;
			float num3 = num * 1.05f;
			if (((Vector3)(ref val2)).sqrMagnitude > num3 * num3 && enemyPredict.triggeredTongue != 0f)
			{
				enemyPredict.triggeredTongue = 0f;
			}
			if (enemyPredict.triggeredTongue == 0f && !((AgentAI)enemyPredict.ai).m_navMeshAgent.isOnOffMeshLink && ((AgentAI)enemyPredict.ai).m_target != null && (Object)(object)((AgentAI)enemyPredict.ai).m_target.m_agent == (Object)(object)localPlayerAgent && ((Vector3)(ref val2)).sqrMagnitude < num * num)
			{
				val2.y = 0.0001f;
				((Vector3)(ref val2)).Normalize();
				float num4 = Vector3.Dot(((Vector3)(ref val2)).normalized, ((Component)enemyPredict.agent).transform.forward);
				bool flag = Physics.Linecast(val, ((Agent)localPlayerAgent).EyePosition, LayerManager.MASK_WORLD);
				if ((((Vector3)(ref val2)).sqrMagnitude < 4f || num4 > 0.45f) && !flag)
				{
					pES_EnemyAttackData val3 = default(pES_EnemyAttackData);
					val3.Position = enemyPredict.agent.Position;
					val3.TargetPosition = ((Agent)localPlayerAgent).AimTarget.position;
					((pAgent)(ref val3.TargetAgent)).Set((Agent)(object)localPlayerAgent);
					val3.AnimIndex = (byte)enemyPredict.agent.Locomotion.GetUniqueAnimIndex(EnemyLocomotion.s_hashAbilityFires, ref enemyPredict.lastAnimIndex);
					val3.AbilityType = (AgentAbility)1;
					enemyPredict.lastReceivedAttack = -1f;
					((ES_EnemyAttackBase)enemyPredict.agent.Locomotion.StrikerAttack).RecieveAttackStart(val3);
					enemyPredict.triggeredTongue = Clock.Time;
				}
			}
		}
	}
}
namespace ClientSidePrediction.BepInEx
{
	public static class Module
	{
		public const string GUID = "randomuserhi.ClientSidePrediction";

		public const string Name = "ClientSidePrediction";

		public const string Version = "0.0.1";
	}
	public static class ConfigManager
	{
		public static ConfigFile configFile;

		private static ConfigEntry<bool> debug;

		private static ConfigEntry<int> tonguePredictThreshold;

		private static ConfigEntry<bool> disableTonguePredictOnEnemiesWithMelee;

		public static bool Debug
		{
			get
			{
				return debug.Value;
			}
			set
			{
				debug.Value = value;
			}
		}

		public static int TonguePredictThreshold
		{
			get
			{
				return tonguePredictThreshold.Value;
			}
			set
			{
				tonguePredictThreshold.Value = value;
			}
		}

		public static bool DisableTonguePredictOnEnemiesWithMelee
		{
			get
			{
				return disableTonguePredictOnEnemiesWithMelee.Value;
			}
			set
			{
				disableTonguePredictOnEnemiesWithMelee.Value = value;
			}
		}

		static ConfigManager()
		{
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			//IL_001a: Expected O, but got Unknown
			configFile = new ConfigFile(Path.Combine(Paths.ConfigPath, "ClientSidePrediction.cfg"), true);
			debug = configFile.Bind<bool>("Debug", "enable", false, "Enables debug messages when true.");
			tonguePredictThreshold = configFile.Bind<int>("Settings", "TonguePredictThreshold", 150, "When ping (in ms) exceeds this value enemy tongue windup animations are predicted.");
			disableTonguePredictOnEnemiesWithMelee = configFile.Bind<bool>("Settings", "DisableTonguePredictOnEnemiesWithMelee", false, "Disables tongue prediction on enemies that have melee abilities to mitigate desync on mispredict.");
		}
	}
	[BepInPlugin("randomuserhi.ClientSidePrediction", "ClientSidePrediction", "0.0.1")]
	public class Plugin : BasePlugin
	{
		private static Harmony? harmony;

		public override void Load()
		{
			//IL_000f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0019: Expected O, but got Unknown
			//IL_006f: Unknown result type (might be due to invalid IL or missing references)
			APILogger.Log("Plugin is loaded!");
			harmony = new Harmony("randomuserhi.ClientSidePrediction");
			harmony.PatchAll();
			APILogger.Log("Debug is " + (ConfigManager.Debug ? "Enabled" : "Disabled"));
			ClassInjector.RegisterTypeInIl2Cpp<LatencyTracker>();
			ClassInjector.RegisterTypeInIl2Cpp<Prediction.EnemyPredict>();
			RundownManager.OnExpeditionGameplayStarted += Action.op_Implicit((Action)LatencyTracker.OnGameplayStarted);
			new GameObject().AddComponent<LatencyTracker>();
			new Prediction();
		}
	}
}