Decompiled source of Fast Climbing v1.3.0

BepInEx/plugins/inoyu.FastClimbing.dll

Decompiled 6 months ago
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using FastClimbingV2.Configs;
using GameNetcodeStuff;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Unity.Collections;
using Unity.Netcode;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("inoyu.FastClimbing")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.3.0.0")]
[assembly: AssemblyInformationalVersion("1.3.0+5170a696ad45ca1364f5135c03232477e8199cb9")]
[assembly: AssemblyProduct("FastClimbingV2")]
[assembly: AssemblyTitle("inoyu.FastClimbing")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.3.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.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;
		}
	}
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace FastClimbingV2
{
	[BepInPlugin("inoyu.FastClimbing", "FastClimbingV2", "1.3.0")]
	public class FastClimbingV2 : BaseUnityPlugin
	{
		internal static string TINY_LADDER_PREFIX = "tinyLadder";

		internal static bool isOnTinyLadder = false;

		public static FastClimbingV2 Instance { get; private set; } = null;


		internal static ManualLogSource Logger { get; private set; } = null;


		internal static Harmony? Harmony { get; set; }

		private void Awake()
		{
			Logger = ((BaseUnityPlugin)this).Logger;
			Instance = this;
			FastClimbingConfigs.BindConfigs(((BaseUnityPlugin)Instance).Config);
			Patch();
			Logger.LogInfo((object)"inoyu.FastClimbing v1.3.0 has loaded!");
		}

		internal static void Patch()
		{
			//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_0018: Expected O, but got Unknown
			if (Harmony == null)
			{
				Harmony = new Harmony("inoyu.FastClimbing");
			}
			Logger.LogDebug((object)"Patching...");
			Harmony.PatchAll();
			Logger.LogDebug((object)"Finished patching!");
		}
	}
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "inoyu.FastClimbing";

		public const string PLUGIN_NAME = "FastClimbingV2";

		public const string PLUGIN_VERSION = "1.3.0";
	}
}
namespace FastClimbingV2.Patches
{
	[HarmonyPatch(typeof(InteractTrigger))]
	internal class CheckForOtherModsPatch
	{
		[HarmonyPrefix]
		[HarmonyPriority(600)]
		[HarmonyPatch("ladderClimbAnimation")]
		private static void CheckForGiantExtLaddersPatch(InteractTrigger __instance, ref PlayerControllerB playerController)
		{
			if ((Object)(object)((Component)__instance).transform.parent != (Object)null && (Object)(object)((Component)((Component)__instance).transform.parent).transform.parent != (Object)null && (Object)(object)((Component)((Component)((Component)__instance).transform.parent).transform.parent).transform.parent != (Object)null && ((Object)((Component)((Component)((Component)__instance).transform.parent).transform.parent).transform.parent).name.StartsWith(FastClimbingV2.TINY_LADDER_PREFIX))
			{
				FastClimbingV2.isOnTinyLadder = true;
			}
			else
			{
				FastClimbingV2.isOnTinyLadder = false;
			}
		}
	}
	[HarmonyPatch(typeof(PlayerControllerB))]
	internal class SprintClimbPatch
	{
		private static float normalClimbSpeedValue;

		private static bool isNormalClimbSpeedValueSet;

		[HarmonyPatch("Update")]
		[HarmonyPriority(100)]
		[HarmonyPostfix]
		private static void crankThatClimbingSpeed(PlayerControllerB __instance)
		{
			if (FastClimbingV2.isOnTinyLadder)
			{
				return;
			}
			if (!isNormalClimbSpeedValueSet)
			{
				normalClimbSpeedValue = __instance.climbSpeed;
				isNormalClimbSpeedValueSet = true;
			}
			if (!__instance.isPlayerControlled || !__instance.isClimbingLadder)
			{
				return;
			}
			__instance.climbSpeed = normalClimbSpeedValue;
			if (__instance.isSprinting)
			{
				if (__instance.sprintMeter > 0.3f)
				{
					__instance.climbSpeed = normalClimbSpeedValue * ConfigSync.Instance.climbSpeedMultiplier;
				}
				else
				{
					__instance.isExhausted = true;
				}
			}
		}
	}
}
namespace FastClimbingV2.Configs
{
	[Serializable]
	[HarmonyPatch]
	public class ConfigSync
	{
		[CompilerGenerated]
		private static class <>O
		{
			public static HandleNamedMessageDelegate <0>__OnReceiveConfigSyncRequest;

			public static HandleNamedMessageDelegate <1>__OnReceiveConfigSync;
		}

		public static ConfigSync defaultConfig;

		public static ConfigSync Instance;

		public static PlayerControllerB localPlayerController;

		public static bool isSynced;

		public float climbSpeedMultiplier = 1.8f;

		public static void BuildDefaultConfigSync()
		{
			Instance = new ConfigSync();
		}

		public static void BuildServerConfigSync()
		{
			if (Instance == null)
			{
				defaultConfig = new ConfigSync();
				defaultConfig.climbSpeedMultiplier = FastClimbingConfigs.CLIMB_SPEED_MULTIPLIER.Value;
				Instance = defaultConfig;
			}
		}

		[HarmonyPatch(typeof(PlayerControllerB), "ConnectClientToPlayerObject")]
		[HarmonyPostfix]
		public static void InitializeLocalPlayer(PlayerControllerB __instance)
		{
			//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_008c: Expected O, but got Unknown
			//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: Expected O, but got Unknown
			localPlayerController = __instance;
			if (NetworkManager.Singleton.IsServer)
			{
				BuildServerConfigSync();
				CustomMessagingManager customMessagingManager = NetworkManager.Singleton.CustomMessagingManager;
				object obj = <>O.<0>__OnReceiveConfigSyncRequest;
				if (obj == null)
				{
					HandleNamedMessageDelegate val = OnReceiveConfigSyncRequest;
					<>O.<0>__OnReceiveConfigSyncRequest = val;
					obj = (object)val;
				}
				customMessagingManager.RegisterNamedMessageHandler("BetterStamina-OnRequestConfigSync", (HandleNamedMessageDelegate)obj);
				OnLocalClientConfigSync();
				return;
			}
			isSynced = false;
			BuildDefaultConfigSync();
			CustomMessagingManager customMessagingManager2 = NetworkManager.Singleton.CustomMessagingManager;
			object obj2 = <>O.<1>__OnReceiveConfigSync;
			if (obj2 == null)
			{
				HandleNamedMessageDelegate val2 = OnReceiveConfigSync;
				<>O.<1>__OnReceiveConfigSync = val2;
				obj2 = (object)val2;
			}
			customMessagingManager2.RegisterNamedMessageHandler("BetterStamina-OnReceiveConfigSync", (HandleNamedMessageDelegate)obj2);
			RequestConfigSync();
		}

		public static void RequestConfigSync()
		{
			//IL_003b: Unknown result type (might be due to invalid IL or missing references)
			if (NetworkManager.Singleton.IsClient)
			{
				FastClimbingV2.Logger.LogDebug((object)"Sending config sync request to server.");
				FastBufferWriter val = default(FastBufferWriter);
				((FastBufferWriter)(ref val))..ctor(4, (Allocator)2, -1);
				NetworkManager.Singleton.CustomMessagingManager.SendNamedMessage("BetterStamina-OnRequestConfigSync", 0uL, val, (NetworkDelivery)3);
			}
			else
			{
				FastClimbingV2.Logger.LogError((object)"Failed to send config sync request.");
			}
		}

		public static void OnReceiveConfigSyncRequest(ulong clientId, FastBufferReader reader)
		{
			//IL_0054: Unknown result type (might be due to invalid IL or missing references)
			//IL_005a: Unknown result type (might be due to invalid IL or missing references)
			//IL_007d: Unknown result type (might be due to invalid IL or missing references)
			if (NetworkManager.Singleton.IsServer)
			{
				FastClimbingV2.Logger.LogDebug((object)("Receiving config sync request from client with id: " + clientId + ". Sending config sync to client."));
				byte[] array = SerializeConfigToByteArray(Instance);
				FastBufferWriter val = default(FastBufferWriter);
				((FastBufferWriter)(ref val))..ctor(array.Length + 4, (Allocator)2, -1);
				int num = array.Length;
				((FastBufferWriter)(ref val)).WriteValueSafe<int>(ref num, default(ForPrimitives));
				((FastBufferWriter)(ref val)).WriteBytesSafe(array, -1, 0);
				NetworkManager.Singleton.CustomMessagingManager.SendNamedMessage("BetterStamina-OnReceiveConfigSync", clientId, val, (NetworkDelivery)3);
			}
		}

		public static void OnReceiveConfigSync(ulong clientId, FastBufferReader reader)
		{
			//IL_0014: 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)
			if (((FastBufferReader)(ref reader)).TryBeginRead(4))
			{
				int num = default(int);
				((FastBufferReader)(ref reader)).ReadValueSafe<int>(ref num, default(ForPrimitives));
				if (((FastBufferReader)(ref reader)).TryBeginRead(num))
				{
					FastClimbingV2.Logger.LogDebug((object)"Receiving config sync from server.");
					byte[] data = new byte[num];
					((FastBufferReader)(ref reader)).ReadBytesSafe(ref data, num, 0);
					Instance = DeserializeFromByteArray(data);
					OnLocalClientConfigSync();
				}
				else
				{
					FastClimbingV2.Logger.LogError((object)"Error receiving sync from server.");
				}
			}
			else
			{
				FastClimbingV2.Logger.LogError((object)"Error receiving bytes length.");
			}
		}

		public static void OnLocalClientConfigSync()
		{
			isSynced = true;
		}

		public static byte[] SerializeConfigToByteArray(ConfigSync config)
		{
			BinaryFormatter binaryFormatter = new BinaryFormatter();
			MemoryStream memoryStream = new MemoryStream();
			binaryFormatter.Serialize(memoryStream, config);
			return memoryStream.ToArray();
		}

		public static ConfigSync DeserializeFromByteArray(byte[] data)
		{
			MemoryStream serializationStream = new MemoryStream(data);
			BinaryFormatter binaryFormatter = new BinaryFormatter();
			return (ConfigSync)binaryFormatter.Deserialize(serializationStream);
		}
	}
	[Serializable]
	public static class FastClimbingConfigs
	{
		public static ConfigEntry<float> CLIMB_SPEED_MULTIPLIER;

		private const float BASE_SPRINT_SPEED_MULTIPLIER = 1.85f;

		private const float MAX_SPRINT_SPEED_MULTIPLIER = 80f;

		private const float MIN_SPRINT_SPEED_MULTIPLIER = 1f;

		public static void BindConfigs(ConfigFile cfg)
		{
			CLIMB_SPEED_MULTIPLIER = cfg.Bind<float>("SprintClimbing", "sprintClimbingSpeed", 1.85f, "A multiplier for the climbing speed while sprinting!");
			fixClimbspeed();
		}

		private static void fixClimbspeed()
		{
			if (CLIMB_SPEED_MULTIPLIER.Value > 80f)
			{
				CLIMB_SPEED_MULTIPLIER.Value = 80f;
				FastClimbingV2.Logger.LogInfo((object)("climbing speed value too high, was set to " + 80f + "."));
			}
			if (CLIMB_SPEED_MULTIPLIER.Value < 1f)
			{
				CLIMB_SPEED_MULTIPLIER.Value = 1f;
				FastClimbingV2.Logger.LogInfo((object)("climbing speed value too low, was set to " + 1f + "."));
			}
		}
	}
}