Decompiled source of GTFOClientSidePrediction v0.3.0

ClientSidePrediction.dll

Decompiled 9 hours ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
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 BepInEx.Unity.IL2CPP.Hook;
using ClientSidePrediction.BepInEx;
using Enemies;
using GameData;
using HarmonyLib;
using Il2CppInterop.Runtime;
using Il2CppInterop.Runtime.Injection;
using Il2CppInterop.Runtime.InteropTypes;
using Il2CppInterop.Runtime.InteropTypes.Arrays;
using Il2CppInterop.Runtime.Runtime;
using Il2CppInterop.Runtime.Runtime.VersionSpecific.Class;
using Il2CppInterop.Runtime.Runtime.VersionSpecific.MethodInfo;
using Il2CppSystem;
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+f96bb0bb109a5a42041a1effee343e600467347c")]
[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;

		[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);
		}

		[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_007c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0081: 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_009c: 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_00b9: 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_00c0: 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_00c7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00cc: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d1: 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_00d7: 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_00e3: 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_00fd: Unknown result type (might be due to invalid IL or missing references)
			//IL_0102: 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_00b4: 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_016e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0174: Unknown result type (might be due to invalid IL or missing references)
			//IL_0179: 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_018e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0193: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a2: Unknown result type (might be due to invalid IL or missing references)
			//IL_01aa: 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 (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 = pos - prevPos;
			if (((Vector3)(ref val2)).sqrMagnitude > 5f)
			{
				return;
			}
			if (Vector3.Dot(oldVel, val) < 0f)
			{
				sentPos = pos;
			}
			oldVel = val;
			Vector3 val3 = pos + val * num2;
			Vector3 a = sentPos;
			float num3 = lerpFactor;
			val2 = sentPos - val3;
			sentPos = ExpDecay(a, val3, num3 * Mathf.Max(((Vector3)(ref val2)).magnitude, 1f), num);
			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
	{
		private 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 + $" | {ping} ms";
		}
	}
	[HarmonyPatch]
	internal class Prediction
	{
		public class EnemyData
		{
			public EnemyAgent agent;

			public NavMeshAgent navMeshAgent;

			public EnemyAI ai;

			public Vector3 prevPos = Vector3.zero;

			public Vector3 vel = Vector3.zero;

			public long prevTimestamp;

			public int lastAnimIndex;

			public float triggeredTongue;

			public float lastSound;

			public EnemyData(EnemyAgent agent)
			{
				//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_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)
				this.agent = agent;
				ai = agent.AI;
				navMeshAgent = ((Il2CppObjectBase)((AgentAI)agent.AI).m_navMeshAgent).Cast<NavMeshAgentProxy>().m_agent;
			}
		}

		private unsafe delegate Vector3* Interpolate(Vector3* _Vector3Ptr, IntPtr _thisPtr, float t, Il2CppMethodInfo* _);

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

		private unsafe static Interpolate Patch_Interpolate = Patch;

		private static Interpolate Original_Interpolate;

		private static float lerpFactor = 5f;

		[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")]
		[HarmonyPostfix]
		private static void FeedMaterial(Vector4Transition __instance)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0049: Unknown result type (might be due to invalid IL or missing references)
			//IL_0013: Unknown result type (might be due to invalid IL or missing references)
			//IL_0025: 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)
			if (float.IsNaN(__instance.m_start.x) || float.IsNaN(__instance.m_start.y) || float.IsNaN(__instance.m_start.z) || float.IsNaN(__instance.m_start.w))
			{
				__instance.m_start = Vector4.zero;
			}
		}

		[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);
		}

		[HarmonyPatch(typeof(ES_StrikerAttack), "OnAttackWindUp")]
		[HarmonyPrefix]
		private static bool ES_StrikerAttack_OnAttackWindUp(ES_StrikerAttack __instance, int attackIndex, AgentAbility abilityType, int abilityIndex)
		{
			//IL_006f: 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_00c2: 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;
			}
			EnemyData enemyData = map[pointer];
			if (Clock.Time < enemyData.lastSound + 0.5f)
			{
				__instance.m_tentacleAbility = ((Il2CppObjectBase)((ES_Base)__instance).m_ai.m_enemyAgent.Abilities.GetAbility(abilityType, 0)).Cast<EAB_MovingEnemeyTentacle>();
				((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;
			}
			enemyData.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)
			{
				map.Add(((Il2CppObjectBase)((Il2CppObjectBase)__instance.m_agent.Locomotion.PathMove).Cast<ES_PathMove>().m_positionBuffer).Pointer, new EnemyData(__instance.m_agent));
			}
		}

		[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);
				}
			}
		}

		public unsafe Prediction()
		{
			INativeClassStruct val = UnityVersionHandler.Wrap((Il2CppClass*)(void*)Il2CppClassPointerStore<PositionSnapshotBuffer<pES_PathMoveData>>.NativeClassPtr);
			for (int i = 0; i < val.MethodCount; i++)
			{
				INativeMethodInfoStruct val2 = UnityVersionHandler.Wrap(val.Methods[i]);
				if (Marshal.PtrToStringAnsi(val2.Name) == "Interpolate")
				{
					INativeDetour.CreateAndApply<Interpolate>(val2.MethodPointer, Patch_Interpolate, ref Original_Interpolate);
					break;
				}
			}
		}

		[HarmonyPatch(typeof(ES_PathMove), "RecieveStateData")]
		[HarmonyPrefix]
		private static bool ES_PathMove_RecieveStateData(ES_PathMove __instance, pES_PathMoveData incomingData)
		{
			//IL_008e: Unknown result type (might be due to invalid IL or missing references)
			if (SNet.IsMaster)
			{
				return true;
			}
			if (Mathf.Min(LatencyTracker.Ping, 1f) / 2f <= 0f)
			{
				return true;
			}
			IntPtr pointer = ((Il2CppObjectBase)__instance.m_positionBuffer).Pointer;
			if (!map.ContainsKey(pointer))
			{
				return true;
			}
			EnemyData enemyData = map[pointer];
			if (Clock.Time < enemyData.triggeredTongue + 3f)
			{
				long now = LatencyTracker.Now;
				_ = (float)(now - enemyData.prevTimestamp) / 1000f;
				enemyData.prevTimestamp = now;
				lerpFactor = 1f;
				__instance.m_positionBuffer.Push(incomingData);
				return false;
			}
			return true;
		}

		[HarmonyPatch(typeof(ES_PathMove), "SyncEnter")]
		[HarmonyPostfix]
		private static void ES_PathMove_SyncEnter(ES_PathMove __instance)
		{
			//IL_0034: Unknown result type (might be due to invalid IL or missing references)
			//IL_0039: Unknown result type (might be due to invalid IL or missing references)
			//IL_004a: Unknown result type (might be due to invalid IL or missing references)
			//IL_004f: Unknown result type (might be due to invalid IL or missing references)
			if (!SNet.IsMaster)
			{
				IntPtr pointer = ((Il2CppObjectBase)__instance.m_positionBuffer).Pointer;
				if (map.ContainsKey(pointer))
				{
					EnemyData enemyData = map[pointer];
					enemyData.prevPos = ((ES_Base)__instance).m_enemyAgent.Position;
					enemyData.prevTimestamp = LatencyTracker.Now;
					enemyData.vel = Vector3.zero;
					((AgentAI)enemyData.ai).m_navMeshAgent.enabled = true;
				}
			}
		}

		[HarmonyPatch(typeof(ES_PathMove), "SyncUpdate")]
		[HarmonyPostfix]
		private static void ES_PathMove_SyncUpdate(ES_PathMove __instance)
		{
			//IL_005b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0066: Unknown result type (might be due to invalid IL or missing references)
			//IL_006b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0070: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ee: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fe: Unknown result type (might be due to invalid IL or missing references)
			//IL_0103: Unknown result type (might be due to invalid IL or missing references)
			//IL_0116: Unknown result type (might be due to invalid IL or missing references)
			//IL_0124: Unknown result type (might be due to invalid IL or missing references)
			//IL_0129: Unknown result type (might be due to invalid IL or missing references)
			//IL_0136: Unknown result type (might be due to invalid IL or missing references)
			//IL_013b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0173: Unknown result type (might be due to invalid IL or missing references)
			//IL_0188: Unknown result type (might be due to invalid IL or missing references)
			if (SNet.IsMaster)
			{
				return;
			}
			IntPtr pointer = ((Il2CppObjectBase)__instance.m_positionBuffer).Pointer;
			if (!map.ContainsKey(pointer))
			{
				return;
			}
			EnemyData enemyData = map[pointer];
			PlayerAgent localPlayerAgent = PlayerManager.GetLocalPlayerAgent();
			float num = enemyData.agent.EnemyBehaviorData.MeleeAttackDistance.Max * 1.05f;
			float num2 = num * num;
			Vector3 val = enemyData.agent.Position - ((Component)localPlayerAgent).transform.position;
			if (((Vector3)(ref val)).sqrMagnitude > num2 && enemyData.triggeredTongue != 0f)
			{
				enemyData.triggeredTongue = 0f;
			}
			if (enemyData.triggeredTongue == 0f && !((AgentAI)enemyData.ai).m_navMeshAgent.isOnOffMeshLink && ((AgentAI)enemyData.ai).m_target != null && (Object)(object)((AgentAI)enemyData.ai).m_target.m_agent == (Object)(object)localPlayerAgent)
			{
				val = ((Component)enemyData.agent).transform.position - ((Component)localPlayerAgent).transform.position;
				if (((Vector3)(ref val)).sqrMagnitude < num * num)
				{
					pES_EnemyAttackData val2 = default(pES_EnemyAttackData);
					val2.Position = enemyData.agent.Position;
					val2.TargetPosition = ((Agent)localPlayerAgent).AimTarget.position;
					((pAgent)(ref val2.TargetAgent)).Set((Agent)(object)localPlayerAgent);
					val2.AnimIndex = (byte)enemyData.agent.Locomotion.GetUniqueAnimIndex(EnemyLocomotion.s_hashAbilityFires, ref enemyData.lastAnimIndex);
					val2.AbilityType = (AgentAbility)1;
					((ES_EnemyAttackBase)enemyData.agent.Locomotion.StrikerAttack).RecieveAttackStart(val2);
					enemyData.triggeredTongue = Clock.Time;
				}
			}
		}

		[HarmonyPatch(typeof(ES_PathMove), "SyncExit")]
		[HarmonyPostfix]
		private static void ES_PathMove_SyncExit(ES_PathMove __instance)
		{
			if (!SNet.IsMaster)
			{
				IntPtr pointer = ((Il2CppObjectBase)__instance.m_positionBuffer).Pointer;
				if (map.ContainsKey(pointer))
				{
					((AgentAI)map[pointer].ai).m_navMeshAgent.enabled = false;
				}
			}
		}

		private unsafe static Vector3* Patch(Vector3* _Vector3Ptr, IntPtr _thisPtr, float t, Il2CppMethodInfo* _)
		{
			//IL_008d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0093: 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_009d: 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_00b8: 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_00c1: 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_00d8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00de: 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_00e9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ee: 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_0104: Unknown result type (might be due to invalid IL or missing references)
			//IL_0109: Unknown result type (might be due to invalid IL or missing references)
			Vector3* ptr = Original_Interpolate(_Vector3Ptr, _thisPtr, t, _);
			if (SNet.IsMaster)
			{
				return ptr;
			}
			if (!map.ContainsKey(_thisPtr))
			{
				return ptr;
			}
			float num = Mathf.Min(LatencyTracker.Ping, 1f) / 2f;
			if (num <= 0f)
			{
				return ptr;
			}
			EnemyData enemyData = map[_thisPtr];
			long now = LatencyTracker.Now;
			float num2 = (float)(now - enemyData.prevTimestamp) / 1000f;
			enemyData.prevTimestamp = now;
			lerpFactor = ExpDecay(lerpFactor, 5f, 0.5f, num2);
			Vector3 val = *ptr - enemyData.prevPos;
			enemyData.prevPos = *ptr;
			if (num2 <= 0f)
			{
				return ptr;
			}
			enemyData.vel = ExpDecay(enemyData.vel, val / num2, lerpFactor, num2);
			Vector3 destination = *ptr + enemyData.vel * num;
			enemyData.navMeshAgent.destination = destination;
			Unsafe.Write(ptr, enemyData.navMeshAgent.pathEndPosition);
			return ptr;
		}
	}
}
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;

		public static bool Debug
		{
			get
			{
				return debug.Value;
			}
			set
			{
				debug.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.");
		}
	}
	[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_006a: 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>();
			RundownManager.OnExpeditionGameplayStarted += Action.op_Implicit((Action)LatencyTracker.OnGameplayStarted);
			new GameObject().AddComponent<LatencyTracker>();
			new Prediction();
		}
	}
}