Decompiled source of GB Gravity v1.1.17

GBGravity.dll

Decompiled 3 weeks ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using GBGravity;
using MelonLoader;
using MelonLoader.Preferences;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: MelonInfo(typeof(GravityMod), "GB Gravity", "1.0.0", "Clipover", null)]
[assembly: MelonGame(null, null)]
[assembly: TargetFramework(".NETCoreApp,Version=v6.0", FrameworkDisplayName = ".NET 6.0")]
[assembly: AssemblyCompany("GBGravity")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("GBGravity")]
[assembly: AssemblyTitle("GBGravity")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace GBGravity;

public class GravityMod : MelonMod
{
	private MelonPreferences_Entry<string> nextKey;

	private MelonPreferences_Entry<string> resetKey;

	private MelonPreferences_Entry<string> upKey;

	private MelonPreferences_Entry<string> downKey;

	private MelonPreferences_Entry<string> presetsPref;

	private MelonPreferences_Entry<string> namesPref;

	private MelonPreferences_Entry<bool> showLabel;

	private MelonPreferences_Entry<float> stepPref;

	private readonly List<float> multipliers = new List<float>();

	private readonly List<string> labels = new List<string>();

	private Vector3 baseGravity = new Vector3(0f, -9.81f, 0f);

	private int index;

	private float multiplier = 1f;

	private bool modified;

	private float nextCheck;

	private float labelUntil;

	private string labelText = "";

	private bool guiBroken;

	public override void OnInitializeMelon()
	{
		//IL_00ff: 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)
		MelonPreferences_Category val = MelonPreferences.CreateCategory("GBGravity");
		nextKey = val.CreateEntry<string>("NextPresetKey", "g", (string)null, (string)null, false, false, (ValueValidator)null, (string)null);
		resetKey = val.CreateEntry<string>("ResetKey", "r", (string)null, (string)null, false, false, (ValueValidator)null, (string)null);
		upKey = val.CreateEntry<string>("StrongerKey", "equals", (string)null, (string)null, false, false, (ValueValidator)null, (string)null);
		downKey = val.CreateEntry<string>("WeakerKey", "minus", (string)null, (string)null, false, false, (ValueValidator)null, (string)null);
		presetsPref = val.CreateEntry<string>("Presets", "1,0.35,0.12,0,2.5", (string)null, (string)null, false, false, (ValueValidator)null, (string)null);
		namesPref = val.CreateEntry<string>("PresetNames", "Normal,Moon,Float,Zero,Heavy", (string)null, (string)null, false, false, (ValueValidator)null, (string)null);
		stepPref = val.CreateEntry<float>("FineStep", 0.1f, (string)null, (string)null, false, false, (ValueValidator)null, (string)null);
		showLabel = val.CreateEntry<bool>("ShowLabel", true, (string)null, (string)null, false, false, (ValueValidator)null, (string)null);
		Keys.Log = delegate(string s)
		{
			((MelonBase)this).LoggerInstance.Msg(s);
		};
		ParsePresets();
		try
		{
			baseGravity = Physics.gravity;
		}
		catch (Exception)
		{
		}
		((MelonBase)this).LoggerInstance.Msg("GB Gravity ready. " + nextKey.Value.ToUpper() + " = next preset, " + resetKey.Value.ToUpper() + " = reset, " + upKey.Value + " / " + downKey.Value + " = fine tune. Normal gravity is " + baseGravity.y.ToString("0.##", CultureInfo.InvariantCulture));
	}

	private void ParsePresets()
	{
		multipliers.Clear();
		labels.Clear();
		string[] array = presetsPref.Value.Split(',');
		string[] array2 = namesPref.Value.Split(',');
		for (int i = 0; i < array.Length; i++)
		{
			if (float.TryParse(array[i].Trim(), NumberStyles.Float, CultureInfo.InvariantCulture, out var result))
			{
				multipliers.Add(result);
				labels.Add((i < array2.Length && array2[i].Trim().Length > 0) ? array2[i].Trim() : ("Preset " + multipliers.Count));
			}
		}
		if (multipliers.Count == 0)
		{
			multipliers.Add(1f);
			labels.Add("Normal");
		}
	}

	public override void OnSceneWasLoaded(int buildIndex, string sceneName)
	{
		nextCheck = 0f;
	}

	public override void OnUpdate()
	{
		if (Keys.Pressed(nextKey.Value))
		{
			NextPreset();
		}
		if (Keys.Pressed(resetKey.Value))
		{
			Reset();
		}
		if (Keys.Pressed(upKey.Value))
		{
			Nudge(stepPref.Value);
		}
		if (Keys.Pressed(downKey.Value))
		{
			Nudge(0f - stepPref.Value);
		}
		if (Time.unscaledTime >= nextCheck)
		{
			nextCheck = Time.unscaledTime + 1f;
			Maintain();
		}
	}

	private void NextPreset()
	{
		index = (index + 1) % multipliers.Count;
		multiplier = multipliers[index];
		Apply(labels[index]);
	}

	private void Reset()
	{
		index = 0;
		multiplier = 1f;
		Apply("Normal");
	}

	private void Nudge(float amount)
	{
		multiplier = Mathf.Clamp(multiplier + amount, -5f, 8f);
		Apply("Custom");
	}

	private void Apply(string name)
	{
		//IL_0056: Unknown result type (might be due to invalid IL or missing references)
		//IL_0027: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b2: 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)
		modified = Math.Abs(multiplier - 1f) > 0.0001f;
		Physics.gravity = (Vector3)(modified ? new Vector3(baseGravity.x, baseGravity.y * multiplier, baseGravity.z) : baseGravity);
		labelText = name + " (" + multiplier.ToString("0.##", CultureInfo.InvariantCulture) + "x)";
		labelUntil = Time.unscaledTime + 2.5f;
		((MelonBase)this).LoggerInstance.Msg("Gravity: " + labelText + ", y = " + Physics.gravity.y.ToString("0.##", CultureInfo.InvariantCulture));
	}

	private void Maintain()
	{
		//IL_0027: Unknown result type (might be due to invalid IL or missing references)
		//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_0056: Unknown result type (might be due to invalid IL or missing references)
		if (!modified)
		{
			baseGravity = Physics.gravity;
			return;
		}
		float num = baseGravity.y * multiplier;
		if (Mathf.Abs(Physics.gravity.y - num) > 0.001f)
		{
			Physics.gravity = new Vector3(baseGravity.x, num, baseGravity.z);
		}
	}

	public override void OnGUI()
	{
		//IL_0040: Unknown result type (might be due to invalid IL or missing references)
		//IL_0045: Unknown result type (might be due to invalid IL or missing references)
		//IL_0063: 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 (guiBroken || !showLabel.Value || Time.unscaledTime > labelUntil)
		{
			return;
		}
		try
		{
			float num = Mathf.Max(1f, (float)Screen.height / 1080f);
			GUI.matrix = Matrix4x4.Scale(new Vector3(num, num, 1f));
			GUI.Label(new Rect(20f, 20f, 500f, 40f), "Gravity: " + labelText);
			GUI.matrix = Matrix4x4.identity;
		}
		catch (Exception ex)
		{
			guiBroken = true;
			((MelonBase)this).LoggerInstance.Warning("On-screen label disabled: " + ex.Message);
		}
	}
}
public static class Keys
{
	public static Action<string> Log = delegate
	{
	};

	private static bool resolved;

	private static bool legacyBroken;

	private static Type kbType;

	private static PropertyInfo current;

	private static readonly Dictionary<string, PropertyInfo> keyProps = new Dictionary<string, PropertyInfo>();

	private static PropertyInfo pressedProp;

	private static void Resolve()
	{
		resolved = true;
		Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
		foreach (Assembly assembly in assemblies)
		{
			Type type = assembly.GetType("Il2CppUnityEngine.InputSystem.Keyboard") ?? assembly.GetType("UnityEngine.InputSystem.Keyboard");
			if (type != null)
			{
				kbType = type;
				break;
			}
		}
		if (kbType != null)
		{
			current = kbType.GetProperty("current", BindingFlags.Static | BindingFlags.Public);
		}
		Log("input backend: " + ((current != null) ? "InputSystem" : "legacy Input"));
	}

	public static bool Pressed(string key)
	{
		//IL_00e6: Unknown result type (might be due to invalid IL or missing references)
		if (string.IsNullOrEmpty(key))
		{
			return false;
		}
		if (!resolved)
		{
			Resolve();
		}
		try
		{
			if (current != null)
			{
				object value = current.GetValue(null, null);
				if (value != null)
				{
					if (!keyProps.TryGetValue(key, out var value2))
					{
						value2 = kbType.GetProperty(key + "Key");
						keyProps[key] = value2;
					}
					object obj = ((value2 != null) ? value2.GetValue(value, null) : null);
					if (obj != null)
					{
						if (pressedProp == null)
						{
							pressedProp = obj.GetType().GetProperty("wasPressedThisFrame");
						}
						if (pressedProp != null && (bool)pressedProp.GetValue(obj, null))
						{
							return true;
						}
					}
				}
			}
		}
		catch (Exception)
		{
		}
		if (!legacyBroken)
		{
			try
			{
				return Input.GetKeyDown((KeyCode)Enum.Parse(typeof(KeyCode), key, ignoreCase: true));
			}
			catch (Exception)
			{
				legacyBroken = true;
			}
		}
		return false;
	}
}