Decompiled source of MovementSpeedAPI v1.1.0

MovementSpeedAPI.dll

Decompiled 2 weeks ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
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 Microsoft.CodeAnalysis;

[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+fdb53e5ced194fff91e4689d184c66d38c5c9b8c")]
[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.0")]
	internal sealed class EntryPoint : BasePlugin
	{
		public const string MODNAME = "MovementSpeedAPI";

		public override void Load()
		{
			LevelAPI.OnLevelCleanup += OnLevelCleanup;
			((BasePlugin)this).Log.LogMessage((object)"Loaded MovementSpeedAPI");
		}

		private static void OnLevelCleanup()
		{
			MoveSpeedAPI.Reset();
		}
	}
	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 = true;
				_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<ISpeedModifier>[] _layers = new HashSet<ISpeedModifier>[NumLayers];

		private bool _useOverride;

		private float _overrideMod = 1f;

		private float _maxMod = 1f;

		private float _minMod = 1f;

		private float _multMod = 1f;

		private float _addMod = 1f;

		public float Mod { get; private set; } = 1f;


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

		public void Reset()
		{
			HashSet<ISpeedModifier>[] layers = _layers;
			foreach (HashSet<ISpeedModifier> hashSet in layers)
			{
				if (hashSet == null)
				{
					continue;
				}
				foreach (SpeedModifier item in hashSet)
				{
					item.Active = false;
				}
				hashSet.Clear();
			}
			_useOverride = false;
			_overrideMod = 1f;
			_maxMod = 1f;
			_minMod = 1f;
			_multMod = 1f;
			_addMod = 1f;
			Mod = 1f;
		}

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

		private void Refresh(StackLayer layer)
		{
			switch (layer)
			{
			case StackLayer.Override:
			{
				HashSet<ISpeedModifier> layer2 = GetLayer(StackLayer.Override);
				if (layer2.Count > 0)
				{
					_overrideMod = layer2.First().Mod;
					_useOverride = true;
				}
				else
				{
					_overrideMod = 1f;
					_useOverride = false;
				}
				break;
			}
			case StackLayer.Max:
				_maxMod = GetLayer(StackLayer.Max).Max((ISpeedModifier modifier) => modifier.Mod);
				break;
			case StackLayer.Min:
				_minMod = GetLayer(StackLayer.Min).Min((ISpeedModifier modifier) => modifier.Mod);
				break;
			case StackLayer.Multiply:
				_multMod = 1f;
				foreach (ISpeedModifier item in GetLayer(StackLayer.Multiply))
				{
					_multMod *= item.Mod;
				}
				break;
			case StackLayer.Add:
				_addMod = 1f;
				foreach (ISpeedModifier item2 in GetLayer(StackLayer.Add))
				{
					_addMod += item2.Mod - 1f;
				}
				break;
			}
			float mod = Mod;
			Mod = (_useOverride ? _overrideMod : (_maxMod * _minMod * _multMod * _addMod));
			if (mod != Mod)
			{
				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 = 0f;

		private static float _baseRunSpeed;

		private static float _baseCrouchSpeed;

		private static float _baseAirSpeed;

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

		internal static void Reset()
		{
			foreach (ModifierGroup value in _groups.Values)
			{
				value.Reset();
			}
		}

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

		private static void CacheBaseSpeed()
		{
			if (_baseWalkSpeed == 0f)
			{
				_playerData = GameDataBlockBase<PlayerDataBlock>.GetBlock(1u);
				_baseWalkSpeed = _playerData.walkMoveSpeed;
				_baseRunSpeed = _playerData.runMoveSpeed;
				_baseCrouchSpeed = _playerData.crouchMoveSpeed;
				_baseAirSpeed = _playerData.airMoveSpeed;
			}
		}
	}
	public enum StackLayer
	{
		Multiply,
		Add,
		Max,
		Min,
		Override
	}
}