Decompiled source of Lightweight v0.3.2

Lightweight.dll

Decompiled 2 weeks ago
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using BepInEx.Unity.IL2CPP;
using GTFO.API;
using HarmonyLib;
using Il2CppInterop.Runtime.Injection;
using Microsoft.CodeAnalysis;
using Player;
using SNetwork;
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(".NETCoreApp,Version=v6.0", FrameworkDisplayName = ".NET 6.0")]
[assembly: AssemblyCompany("Lightweight")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("Lightweight")]
[assembly: AssemblyTitle("Lightweight")]
[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 Lightweight
{
	internal static class ConfigMgr
	{
		public static ConfigFile conf;

		private static ConfigEntry<bool> _alterYVelocity;

		private static ConfigEntry<bool> _debug;

		public static bool AlterYVelocity => _alterYVelocity.Value;

		public static bool DEBUG => _debug.Value;

		static ConfigMgr()
		{
			//IL_0024: Unknown result type (might be due to invalid IL or missing references)
			//IL_002e: Expected O, but got Unknown
			string text = Path.Combine(Paths.ConfigPath, "Lightweight.cfg");
			Logger.Info("cfgPath = " + text);
			conf = new ConfigFile(text, true);
			int num = 1;
			string text2 = $"({num++}) Adjustments";
			_alterYVelocity = conf.Bind<bool>(text2, "Adjust Y Velocity", true, "If y-velocity (vertical movement) of the player will be scaled in movements according the ammunition carried.");
			text2 = "(Z) Dev";
			_debug = conf.Bind<bool>(text2, "Debug Logging", false, "Whether to enable debug logging");
		}
	}
	public class LightweightManager : MonoBehaviour
	{
		private float _velocityMultiplierTarget = 1f;

		public const float VELOCITY_MULT_MAX = 1.25f;

		public const float VELOCITY_MULT_MIN = 0.8f;

		private const float MAIN_WEIGHT = 0.4f;

		private const float SPECIAL_WEIGHT = 0.45f;

		private const float TOOL_WEIGHT = 0.15f;

		private const float CONSUMABLE_WEIGHT = 0f;

		private const float BREAKPOINT_DEAD_ZONE_MIN = 0.55f;

		private const float BREAKPOINT_DEAD_ZONE_MAX = 0.75f;

		private float _lastAutoUpdate = 0f;

		private const float AUTO_UPDATE_INTERVAL = 2f;

		public static LightweightManager? Instance { get; private set; }

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


		private void Awake()
		{
			if ((Object)(object)Instance == (Object)null)
			{
				Instance = this;
				Object.DontDestroyOnLoad((Object)(object)((Component)this).gameObject);
			}
			else
			{
				Object.Destroy((Object)(object)((Component)this).gameObject);
			}
		}

		private void Update()
		{
			VMult = Mathf.Lerp(_velocityMultiplierTarget, VMult, Time.deltaTime);
			if (Time.time - _lastAutoUpdate > 2f)
			{
				_lastAutoUpdate = Time.time;
				UpdateAmmo(PlayerBackpackManager.LocalBackpack.AmmoStorage);
			}
		}

		public void UpdateAmmo(PlayerAmmoStorage pas)
		{
			InventorySlotAmmo standardAmmo = pas.StandardAmmo;
			InventorySlotAmmo specialAmmo = pas.SpecialAmmo;
			InventorySlotAmmo classAmmo = pas.ClassAmmo;
			InventorySlotAmmo consumableAmmo = pas.ConsumableAmmo;
			float num = 0.4f * standardAmmo.RelInPack + 0.45f * specialAmmo.RelInPack + 0.15f * classAmmo.RelInPack + 0f * consumableAmmo.RelInPack;
			Logger.Debug($"P/M: {num:F3}/{Perc2Mult(num):F3} | M: {standardAmmo.RelInPack:F2} | S: {specialAmmo.RelInPack:F2} | T: {classAmmo.RelInPack:F2} | C: {consumableAmmo.RelInPack:F2}");
			_velocityMultiplierTarget = Perc2Mult(num);
		}

		private float Perc2Mult(float perc)
		{
			perc = Mathf.Clamp01(perc);
			if (perc > 0.75f)
			{
				return Mathf.Lerp(1f, 0.8f, (perc - 0.75f) / 0.25f);
			}
			if (perc < 0.55f)
			{
				return Mathf.Lerp(1f, 1.25f, (0.55f - perc) / 0.55f);
			}
			return 1f;
		}

		public void ResetMgr()
		{
			_velocityMultiplierTarget = 1f;
			VMult = 1f;
		}
	}
	internal static class Logger
	{
		private static ManualLogSource m_LogSource;

		public static void SetupFromInit(ManualLogSource logSource)
		{
			m_LogSource = logSource;
		}

		private static string Format(object data)
		{
			return data.ToString();
		}

		public static void Debug(object msg)
		{
			if (ConfigMgr.DEBUG)
			{
				m_LogSource.LogInfo((object)Format(msg));
			}
		}

		public static void Info(object msg)
		{
			m_LogSource.LogInfo((object)Format(msg));
		}

		public static void Warn(object msg)
		{
			m_LogSource.LogWarning((object)Format(msg));
		}

		public static void Error(object msg)
		{
			m_LogSource.LogError((object)Format(msg));
		}

		public static void Fatal(object msg)
		{
			m_LogSource.LogFatal((object)Format(msg));
		}

		public static void Chat(object msg)
		{
			PlayerChatManager.PostChatMessageLocaly(SNet.LocalPlayer, (string)msg, (SNet_Player)null);
		}
	}
	[HarmonyPatch]
	internal class Patch
	{
		[HarmonyPatch(typeof(PlayerAmmoStorage), "UpdateBulletsInPack")]
		[HarmonyPostfix]
		public static void UpdateBulletsInPack(PlayerAmmoStorage __instance)
		{
			LightweightManager.Instance.UpdateAmmo(__instance);
		}

		[HarmonyPatch(typeof(PlayerCharacterController), "Move")]
		[HarmonyPrefix]
		public static bool MovePrefix(PlayerCharacterController __instance, ref Vector3 delta)
		{
			delta.x *= LightweightManager.Instance.VMult;
			delta.z *= LightweightManager.Instance.VMult;
			if (ConfigMgr.AlterYVelocity)
			{
				delta.y *= LightweightManager.Instance.VMult;
			}
			return true;
		}
	}
	[BepInPlugin("io.takina.gtfo.Lightweight", "Lightweight", "0.3.2")]
	public class Plugin : BasePlugin
	{
		public const string NAME = "Lightweight";

		public const string GUID = "io.takina.gtfo.Lightweight";

		public const string VERSION = "0.3.2";

		public override void Load()
		{
			//IL_0068: Unknown result type (might be due to invalid IL or missing references)
			//IL_006e: Expected O, but got Unknown
			Logger.SetupFromInit(((BasePlugin)this).Log);
			Logger.Info("Lightweight io.takina.gtfo.Lightweight 0.3.2");
			Logger.Info("Patching...");
			Logger.Info("DEBUG = " + ConfigMgr.DEBUG);
			Logger.Info("ConfigMgr AlterYVelocity = " + ConfigMgr.AlterYVelocity);
			ClassInjector.RegisterTypeInIl2Cpp<LightweightManager>();
			GameObject val = new GameObject("Lightweight");
			((Object)val).hideFlags = (HideFlags)61;
			val.AddComponent<LightweightManager>();
			EventAPI.OnExpeditionStarted += delegate
			{
				LightweightManager.Instance.ResetMgr();
			};
			Harmony.CreateAndPatchAll(typeof(Patch), "io.takina.gtfo.Lightweight");
			Logger.Info("Finished Patching");
		}
	}
}