Decompiled source of MovementSpeedAPI v1.1.1

MovementSpeedAPI.dll

Decompiled a day ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Logging;
using BepInEx.Unity.IL2CPP;
using GTFO.API;
using GameData;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Player;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETCoreApp,Version=v6.0", FrameworkDisplayName = ".NET 6.0")]
[assembly: AssemblyCompany("MovementSpeedAPI")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+77d1a3bea4b32897296713d49e643d3164e5a8b8")]
[assembly: AssemblyProduct("MovementSpeedAPI")]
[assembly: AssemblyTitle("MovementSpeedAPI")]
[assembly: AssemblyVersion("1.0.0.0")]
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 MovementSpeedAPI
{
	internal static class DinoLogger
	{
		private static ManualLogSource logger = Logger.CreateLogSource("MovementSpeedAPI");

		public static void Log(string format, params object[] args)
		{
			Log(string.Format(format, args));
		}

		public static void Log(string str)
		{
			if (logger != null)
			{
				logger.Log((LogLevel)8, (object)str);
			}
		}

		public static void Warning(string format, params object[] args)
		{
			Warning(string.Format(format, args));
		}

		public static void Warning(string str)
		{
			if (logger != null)
			{
				logger.Log((LogLevel)4, (object)str);
			}
		}

		public static void Error(string format, params object[] args)
		{
			Error(string.Format(format, args));
		}

		public static void Error(string str)
		{
			if (logger != null)
			{
				logger.Log((LogLevel)2, (object)str);
			}
		}

		public static void Debug(string format, params object[] args)
		{
			Debug(string.Format(format, args));
		}

		public static void Debug(string str)
		{
			if (logger != null)
			{
				logger.Log((LogLevel)32, (object)str);
			}
		}
	}
	[BepInPlugin("Dinorush.MovementSpeedAPI", "MovementSpeedAPI", "1.1.1")]
	internal sealed class EntryPoint : BasePlugin
	{
		public const string MODNAME = "MovementSpeedAPI";

		public override void Load()
		{
			//IL_0005: Unknown result type (might be due to invalid IL or missing references)
			new Harmony("MovementSpeedAPI").PatchAll();
			LevelAPI.OnLevelCleanup += MoveSpeedAPI.Reset;
			((BasePlugin)this).Log.LogMessage((object)"Loaded MovementSpeedAPI");
		}
	}
	public interface ISpeedModifier
	{
		float Mod { get; set; }

		StackLayer Layer { get; }

		bool Active { get; }

		void Enable();

		void Enable(float mod);

		void Disable();
	}
	internal class ModifierGroup
	{
		public class SpeedModifier : ISpeedModifier
		{
			private float _mod;

			private readonly ModifierGroup _parent;

			public float Mod
			{
				get
				{
					return _mod;
				}
				set
				{
					float mod = _mod;
					_mod = value;
					if (Active && mod != _mod)
					{
						_parent.Refresh(Layer);
					}
				}
			}

			public StackLayer Layer { get; }

			public bool Active { get; internal set; }

			public SpeedModifier(float mod, StackLayer layer, ModifierGroup parent)
			{
				Active = false;
				_mod = mod;
				Layer = layer;
				_parent = parent;
			}

			public void Enable()
			{
				if (!Active)
				{
					Active = true;
					_parent.GetLayer(Layer).Add(this);
					_parent.Refresh(Layer);
				}
			}

			public void Enable(float mod)
			{
				Enable();
				Mod = mod;
			}

			public void Disable()
			{
				if (Active)
				{
					Active = false;
					_parent.GetLayer(Layer).Remove(this);
					_parent.Refresh(Layer);
				}
			}
		}

		private static readonly int NumLayers = (int)(Enum.GetValues<StackLayer>()[^1] + 1);

		private readonly HashSet<SpeedModifier>[] _layers = new HashSet<SpeedModifier>[NumLayers];

		private StackValue _mod = new StackValue();

		public StackValue Mod => _mod;

		public ISpeedModifier Add(float mod, StackLayer layer = StackLayer.Multiply)
		{
			SpeedModifier speedModifier = new SpeedModifier(mod, layer, this);
			speedModifier.Enable();
			return speedModifier;
		}

		public void Reset()
		{
			HashSet<SpeedModifier>[] layers = _layers;
			foreach (HashSet<SpeedModifier> hashSet in layers)
			{
				if (hashSet == null)
				{
					continue;
				}
				foreach (SpeedModifier item in hashSet)
				{
					item.Active = false;
				}
				hashSet.Clear();
			}
			_mod.Reset();
		}

		private HashSet<SpeedModifier> GetLayer(StackLayer layer)
		{
			return _layers[(int)layer] ?? (_layers[(int)layer] = new HashSet<SpeedModifier>());
		}

		private void Refresh(StackLayer layer)
		{
			if (_layers[(int)layer] == null)
			{
				return;
			}
			_mod.Reset(layer);
			foreach (SpeedModifier item in GetLayer(layer))
			{
				_mod.Add(item.Mod, layer);
			}
			MoveSpeedAPI.Refresh();
		}
	}
	public static class MoveSpeedAPI
	{
		public const string DefaultGroup = "Default";

		private static readonly int NumLayers = (int)(Enum.GetValues<StackLayer>()[^1] + 1);

		private static readonly Dictionary<string, ModifierGroup> _groups = new Dictionary<string, ModifierGroup>();

		private static PlayerDataBlock _playerData = null;

		private static float _baseWalkSpeed;

		private static float _baseRunSpeed;

		private static float _baseCrouchSpeed;

		private static float _baseAirSpeed;

		private static float _lastScale;

		public static ISpeedModifier AddModifier(float mod, StackLayer layer = StackLayer.Multiply, string group = "Default")
		{
			if (layer < StackLayer.Multiply || (int)layer >= NumLayers)
			{
				throw new ArgumentException($"Invalid layer {layer} provided.");
			}
			if (!_groups.TryGetValue(group, out ModifierGroup value))
			{
				_groups.Add(group, value = new ModifierGroup());
			}
			return value.Add(mod, layer);
		}

		internal static void Reset()
		{
			_lastScale = 1f;
			foreach (ModifierGroup value in _groups.Values)
			{
				value.Reset();
			}
			if (_playerData != null)
			{
				_playerData.walkMoveSpeed = _baseWalkSpeed;
				_playerData.runMoveSpeed = _baseRunSpeed;
				_playerData.crouchMoveSpeed = _baseCrouchSpeed;
				_playerData.airMoveSpeed = _baseAirSpeed;
			}
		}

		internal static void Refresh()
		{
			float num = 1f;
			foreach (ModifierGroup value in _groups.Values)
			{
				num *= value.Mod.Value;
			}
			if (_lastScale != num && _playerData != null)
			{
				_lastScale = num;
				_playerData.walkMoveSpeed = _baseWalkSpeed * num;
				_playerData.runMoveSpeed = _baseRunSpeed * num;
				_playerData.crouchMoveSpeed = _baseCrouchSpeed * num;
				_playerData.airMoveSpeed = _baseAirSpeed * num;
			}
		}

		internal static void CachePlayerData(PlayerDataBlock data)
		{
			_playerData = data;
			_baseWalkSpeed = _playerData.walkMoveSpeed;
			_baseRunSpeed = _playerData.runMoveSpeed;
			_baseCrouchSpeed = _playerData.crouchMoveSpeed;
			_baseAirSpeed = _playerData.airMoveSpeed;
		}
	}
	[HarmonyPatch(typeof(LocalPlayerAgent))]
	internal static class PlayerSetupPatch
	{
		[HarmonyPatch("Setup")]
		[HarmonyPrefix]
		private static void OnAgentSetup(LocalPlayerAgent __instance)
		{
			if (!((PlayerAgent)__instance).m_isSetup)
			{
				MoveSpeedAPI.CachePlayerData(GameDataBlockBase<PlayerDataBlock>.GetBlock(1u));
			}
		}
	}
	public enum StackLayer
	{
		Multiply,
		Add,
		Max,
		Min,
		Override
	}
	internal struct StackValue
	{
		private float _value;

		private bool _isDirty;

		public bool UseOverride;

		public float OverrideMod;

		public float AddMod;

		public float MultMod;

		public float MaxMod;

		private bool _hasMax;

		public float MinMod;

		private bool _hasMin;

		public float Value
		{
			get
			{
				if (!_isDirty)
				{
					return _value;
				}
				_value = (UseOverride ? OverrideMod : (AddMod * MultMod * MaxMod * MinMod));
				_isDirty = false;
				return _value;
			}
		}

		public StackValue()
		{
			UseOverride = false;
			OverrideMod = 1f;
			AddMod = 1f;
			MultMod = 1f;
			MaxMod = 1f;
			_hasMax = false;
			MinMod = 1f;
			_hasMin = false;
			_value = 1f;
			_isDirty = false;
		}

		public StackValue(StackValue other)
		{
			UseOverride = other.UseOverride;
			OverrideMod = other.OverrideMod;
			AddMod = other.AddMod;
			MultMod = other.MultMod;
			MaxMod = other.MaxMod;
			_hasMax = other._hasMax;
			MinMod = other.MinMod;
			_hasMin = other._hasMin;
			_value = other._value;
			_isDirty = other._isDirty;
		}

		public void Add(float mod, StackLayer type)
		{
			switch (type)
			{
			case StackLayer.Override:
				OverrideMod = mod;
				UseOverride = true;
				break;
			case StackLayer.Add:
				AddMod += mod - 1f;
				break;
			case StackLayer.Multiply:
				MultMod *= mod;
				break;
			case StackLayer.Max:
				MaxMod = Math.Max(GetMax(), mod);
				_hasMax = true;
				break;
			case StackLayer.Min:
				MinMod = Math.Min(GetMin(), mod);
				_hasMin = true;
				break;
			}
			_isDirty = true;
		}

		public void Reset(StackLayer type)
		{
			switch (type)
			{
			case StackLayer.Override:
				UseOverride = false;
				OverrideMod = 1f;
				break;
			case StackLayer.Add:
				AddMod = 1f;
				break;
			case StackLayer.Multiply:
				MultMod = 1f;
				break;
			case StackLayer.Max:
				MaxMod = 1f;
				_hasMax = false;
				break;
			case StackLayer.Min:
				MinMod = 1f;
				_hasMin = false;
				break;
			}
			_isDirty = true;
		}

		public void Reset()
		{
			UseOverride = false;
			OverrideMod = 1f;
			AddMod = 1f;
			MultMod = 1f;
			MaxMod = 1f;
			_hasMax = false;
			MinMod = 1f;
			_hasMin = false;
			_value = 1f;
			_isDirty = false;
		}

		public void Combine(StackValue other)
		{
			AddMod += other.AddMod - 1f;
			MultMod *= other.MultMod;
			MaxMod = ((_hasMax || other._hasMax) ? Math.Max(GetMax(), other.GetMax()) : 1f);
			_hasMax |= other._hasMax;
			MinMod = ((_hasMin || other._hasMin) ? Math.Min(GetMin(), other.GetMin()) : 1f);
			_hasMin |= other._hasMin;
			OverrideMod = (UseOverride ? OverrideMod : other.OverrideMod);
			UseOverride |= other.UseOverride;
			_isDirty = true;
		}

		private readonly float GetMin()
		{
			if (!_hasMin)
			{
				return float.MaxValue;
			}
			return MinMod;
		}

		private readonly float GetMax()
		{
			if (!_hasMax)
			{
				return float.MinValue;
			}
			return MaxMod;
		}
	}
}