Decompiled source of PowerMeterDecimals v1.0.0

PowerMeterDecimals.dll

Decompiled 13 hours ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using HarmonyLib;
using PowerMeterDecimals;
using UnityEngine;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("0.0.0.0")]
[BepInPlugin("com.fragm.sbg.powermeterdecimals", "PowerMeterDecimals", "1.0.0")]
public class PowerMeterDecimalsPlugin : BaseUnityPlugin
{
	private void Awake()
	{
		//IL_0005: Unknown result type (might be due to invalid IL or missing references)
		new Harmony("com.fragm.sbg.powermeterdecimals").PatchAll();
		((BaseUnityPlugin)this).Logger.LogInfo((object)"PowerMeterDecimals loaded — power bar shows 2 decimal places.");
	}
}
[HarmonyPatch(typeof(SwingPowerBarUi))]
internal static class SwingPowerBarUiPatch
{
	private static FieldInfo _powerLabelField;

	static SwingPowerBarUiPatch()
	{
		_powerLabelField = typeof(SwingPowerBarUi).GetField("powerLabel", BindingFlags.Instance | BindingFlags.NonPublic);
	}

	[HarmonyTargetMethod]
	private static MethodBase TargetMethod()
	{
		return typeof(SwingPowerBarUi).GetMethod("SetNormalizedPowerInternal", BindingFlags.Instance | BindingFlags.NonPublic);
	}

	[HarmonyPostfix]
	private static void Postfix(object __instance, float normalizedPower, float labelNormalizedPower)
	{
		try
		{
			if (_powerLabelField == null)
			{
				return;
			}
			object value = _powerLabelField.GetValue(__instance);
			if (value != null)
			{
				PropertyInfo property = value.GetType().GetProperty("text", BindingFlags.Instance | BindingFlags.Public);
				if (!(property == null))
				{
					string remapTag = RemappedSwingDetector.GetRemapTag(normalizedPower, labelNormalizedPower);
					string value2 = PowerLabelFormatter.Format(normalizedPower, labelNormalizedPower, remapTag);
					property.SetValue(value, value2, null);
					DisableWordWrapping(value);
				}
			}
		}
		catch
		{
		}
	}

	private static void DisableWordWrapping(object label)
	{
		PropertyInfo property = label.GetType().GetProperty("enableWordWrapping", BindingFlags.Instance | BindingFlags.Public);
		if (!(property == null) && !(property.PropertyType != typeof(bool)) && (bool)property.GetValue(label, null))
		{
			property.SetValue(label, false, null);
		}
	}
}
[HarmonyPatch]
internal static class TickMarksPatch
{
	private static readonly FieldInfo _backgroundField = typeof(SwingPowerBarUi).GetField("background", BindingFlags.Instance | BindingFlags.NonPublic);

	[HarmonyTargetMethod]
	private static MethodBase TargetMethod()
	{
		return typeof(SwingPowerBarUi).GetMethod("Awake", BindingFlags.Instance | BindingFlags.NonPublic);
	}

	[HarmonyPostfix]
	private static void Postfix(MonoBehaviour __instance)
	{
		//IL_0091: Unknown result type (might be due to invalid IL or missing references)
		//IL_0096: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a3: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ba: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d1: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e3: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f8: Unknown result type (might be due to invalid IL or missing references)
		//IL_010c: Unknown result type (might be due to invalid IL or missing references)
		//IL_013a: Unknown result type (might be due to invalid IL or missing references)
		//IL_013f: Unknown result type (might be due to invalid IL or missing references)
		//IL_014c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0183: Unknown result type (might be due to invalid IL or missing references)
		//IL_01a0: Unknown result type (might be due to invalid IL or missing references)
		//IL_01b2: Unknown result type (might be due to invalid IL or missing references)
		//IL_01c7: Unknown result type (might be due to invalid IL or missing references)
		//IL_01dc: Unknown result type (might be due to invalid IL or missing references)
		//IL_01f0: Unknown result type (might be due to invalid IL or missing references)
		try
		{
			if (_backgroundField == null)
			{
				return;
			}
			object? value = _backgroundField.GetValue(__instance);
			Image val = (Image)((value is Image) ? value : null);
			if (!((Object)(object)val == (Object)null))
			{
				RectTransform rectTransform = ((Graphic)val).rectTransform;
				Font builtinResource = Resources.GetBuiltinResource<Font>("Arial.ttf");
				for (int i = 10; i <= 100; i += 10)
				{
					float num = (float)i / 100f;
					float num2 = ((i == 50 || i == 100) ? 0.55f : 0.3f);
					GameObject val2 = new GameObject("PowerTick_" + i, new Type[1] { typeof(Image) });
					val2.transform.SetParent((Transform)(object)rectTransform, false);
					((Graphic)val2.GetComponent<Image>()).color = new Color(1f, 1f, 1f, num2);
					RectTransform component = val2.GetComponent<RectTransform>();
					component.anchorMin = new Vector2(0f, num);
					component.anchorMax = new Vector2(1f, num);
					component.offsetMin = new Vector2(0f, 0f);
					component.offsetMax = new Vector2(0f, 1f);
					GameObject val3 = new GameObject("PowerTickLabel_" + i, new Type[1] { typeof(Text) });
					val3.transform.SetParent((Transform)(object)rectTransform, false);
					Text component2 = val3.GetComponent<Text>();
					component2.text = i.ToString();
					component2.font = builtinResource;
					component2.fontSize = 9;
					((Graphic)component2).color = new Color(1f, 1f, 1f, 0.75f);
					component2.alignment = (TextAnchor)5;
					RectTransform component3 = val3.GetComponent<RectTransform>();
					component3.anchorMin = new Vector2(1f, num);
					component3.anchorMax = new Vector2(1f, num);
					component3.pivot = new Vector2(1f, 0.5f);
					component3.offsetMin = new Vector2(-22f, -6f);
					component3.offsetMax = new Vector2(0f, 6f);
				}
			}
		}
		catch
		{
		}
	}
}
namespace PowerMeterDecimals;

public static class PowerLabelFormatter
{
	public const string ROCKET_DRIVER_TAG = "RD";

	public const string JUMBO_BURGER_TAG = "JB";

	public const string UNKNOWN_REMAP_TAG = "vanilla";

	private const float PERCENT_SCALE = 100f;

	private const float CEIL_GUARD = 0.0001f;

	private const string TRUE_PERCENT_FORMAT = "F2";

	private const string REMAPPED_FORMAT = "{0}  ({1} {2})";

	public static string Format(float trueCharge, float labelPower, string remapTag)
	{
		string text = (trueCharge * 100f).ToString("F2");
		if (string.IsNullOrEmpty(remapTag))
		{
			return text;
		}
		return $"{text}  ({remapTag} {GetVanillaPercent(labelPower)})";
	}

	public static int GetVanillaPercent(float labelPower)
	{
		return (int)Math.Ceiling(labelPower * 100f - 0.0001f);
	}
}
internal static class RemappedSwingDetector
{
	private const float REMAP_EPSILON = 0.0005f;

	private static readonly MethodInfo GetSwingTypeMethod = typeof(PlayerGolfer).GetMethod("GetSwingType", BindingFlags.Instance | BindingFlags.NonPublic);

	internal static string GetRemapTag(float trueCharge, float labelPower)
	{
		//IL_0028: Unknown result type (might be due to invalid IL or missing references)
		try
		{
			PlayerGolfer localPlayerAsGolfer = GameManager.LocalPlayerAsGolfer;
			if ((Object)(object)localPlayerAsGolfer != (Object)null && GetSwingTypeMethod != null)
			{
				return TagFor((SwingType)GetSwingTypeMethod.Invoke(localPlayerAsGolfer, null));
			}
		}
		catch
		{
		}
		if (!(Mathf.Abs(labelPower - trueCharge) > 0.0005f))
		{
			return null;
		}
		return "vanilla";
	}

	private static string TagFor(SwingType swingType)
	{
		//IL_0000: Unknown result type (might be due to invalid IL or missing references)
		//IL_0002: Invalid comparison between Unknown and I4
		//IL_0004: Unknown result type (might be due to invalid IL or missing references)
		//IL_0006: Invalid comparison between Unknown and I4
		if ((int)swingType != 2)
		{
			if ((int)swingType == 3)
			{
				return "JB";
			}
			return null;
		}
		return "RD";
	}
}