Decompiled source of ClassicHUD v1.0.0

ClassicHUD.dll

Decompiled a week 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 ComputerysModdingUtilities;
using HarmonyLib;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: StraftatMod(true)]
[assembly: AssemblyCompany("1ntrospective")]
[assembly: AssemblyCopyright("Copyright (c) 1ntrospective")]
[assembly: AssemblyTitle("ClassicHUD")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("0.0.0.0")]
namespace ClassicHUD;

[BepInPlugin("1ntrospective.straftat.classichud", "ClassicHUD", "2.0.0")]
public class Plugin : BaseUnityPlugin
{
	private static ManualLogSource Log;

	private const float HealthX = 25f;

	private const float HealthY = 10f;

	private const float HealthScale = 0.8f;

	private const float AmmoScale = 0.8f;

	private const float AmmoLeftX = 200f;

	private const float AmmoLeftY = 880f;

	private const float AmmoRightX = 1735f;

	private const float AmmoRightY = 880f;

	private static readonly Color AmmoColor = new Color(22f / 51f, 0.41568628f, 0.4117647f, 1f);

	private const float InfoFontPx = 9f;

	private const float InfoMarginX = 8f;

	private const float InfoMarginY = 4f;

	private const float InfoLineGap = 1f;

	private static readonly BindingFlags BF = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;

	private TMP_FontAsset arial;

	private bool fontTried;

	private TMP_Text fps;

	private TMP_Text ping;

	private TMP_Text health;

	private readonly List<TMP_Text> ammoL = new List<TMP_Text>();

	private readonly List<TMP_Text> ammoR = new List<TMP_Text>();

	private readonly List<Transform> ammoParents = new List<Transform>();

	private readonly Dictionary<TMP_Text, float> baseSize = new Dictionary<TMP_Text, float>();

	private float scanTimer;

	private void Awake()
	{
		//IL_0010: Unknown result type (might be due to invalid IL or missing references)
		//IL_003b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0049: Expected O, but got Unknown
		Log = ((BaseUnityPlugin)this).Logger;
		try
		{
			new Harmony("1ntrospective.straftat.classichud").Patch((MethodBase)AccessTools.Method(typeof(PlayerSetup), "HideHUD", (Type[])null, (Type[])null), new HarmonyMethod(typeof(Patches), "HideHudPrefix", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
		}
		catch (Exception ex)
		{
			((BaseUnityPlugin)this).Logger.LogError((object)("Could not patch PlayerSetup.HideHUD: " + ex.Message));
		}
		((BaseUnityPlugin)this).Logger.LogInfo((object)"ClassicHUD by 1ntrospective ready.");
	}

	private static TMP_Text Field(object o, string name)
	{
		FieldInfo field = o.GetType().GetField(name, BF);
		if (!(field == null))
		{
			object? value = field.GetValue(o);
			return (TMP_Text)((value is TMP_Text) ? value : null);
		}
		return null;
	}

	private static bool Flag(object o, string name)
	{
		FieldInfo field = o.GetType().GetField(name, BF);
		if (field != null && field.FieldType == typeof(bool))
		{
			return (bool)field.GetValue(o);
		}
		return false;
	}

	private void Scan()
	{
		PauseManager instance = PauseManager.Instance;
		if ((Object)(object)instance == (Object)null)
		{
			return;
		}
		fps = Field(instance, "minimalFpsText");
		ping = Field(instance, "minimalPingText");
		health = Field(instance, "minimalistHealthText");
		ammoL.Clear();
		ammoR.Clear();
		ammoParents.Clear();
		HUD[] array = Object.FindObjectsOfType<HUD>();
		foreach (HUD val in array)
		{
			TMP_Text val2 = Field(val, "text");
			if (!((Object)(object)val2 == (Object)null))
			{
				bool num = Flag(val, "ammoRight") || Flag(val, "ammoReloadRight");
				bool flag = Flag(val, "ammoLeft") || Flag(val, "ammoReloadLeft");
				if (num)
				{
					ammoR.Add(val2);
				}
				if (flag)
				{
					ammoL.Add(val2);
				}
				if ((num || flag) && (Object)(object)((Component)val).transform.parent != (Object)null)
				{
					ammoParents.Add(((Component)val).transform.parent);
				}
			}
		}
		TMP_Text val3 = Field(instance, "rightGunAmmo");
		TMP_Text val4 = Field(instance, "leftGunAmmo");
		if ((Object)(object)val3 != (Object)null && ((Component)val3).gameObject.activeInHierarchy && !ammoR.Contains(val3))
		{
			ammoR.Add(val3);
		}
		if ((Object)(object)val4 != (Object)null && ((Component)val4).gameObject.activeInHierarchy && !ammoL.Contains(val4))
		{
			ammoL.Add(val4);
		}
	}

	private void EnsureFont()
	{
		if ((Object)(object)arial != (Object)null || fontTried)
		{
			return;
		}
		fontTried = true;
		try
		{
			Font val = Font.CreateDynamicFontFromOSFont("Arial", 32);
			if ((Object)(object)val != (Object)null)
			{
				arial = TMP_FontAsset.CreateFontAsset(val);
			}
		}
		catch (Exception ex)
		{
			Log.LogWarning((object)("Could not create the Arial font: " + ex.Message));
		}
	}

	private void LateUpdate()
	{
		//IL_0063: Unknown result type (might be due to invalid IL or missing references)
		//IL_0068: Unknown result type (might be due to invalid IL or missing references)
		//IL_0075: Unknown result type (might be due to invalid IL or missing references)
		//IL_01ed: Unknown result type (might be due to invalid IL or missing references)
		//IL_0256: Unknown result type (might be due to invalid IL or missing references)
		scanTimer -= Time.unscaledDeltaTime;
		if (scanTimer <= 0f)
		{
			scanTimer = 1f;
			Scan();
		}
		EnsureFont();
		float s = (float)Screen.height / 1080f;
		foreach (Transform ammoParent in ammoParents)
		{
			if ((Object)(object)ammoParent != (Object)null && ammoParent.localScale != Vector3.one)
			{
				ammoParent.localScale = Vector3.one;
			}
		}
		int line = 0;
		if ((Object)(object)fps != (Object)null)
		{
			Info(fps, line++, s);
		}
		if ((Object)(object)ping != (Object)null)
		{
			Info(ping, line, s);
		}
		if ((Object)(object)health != (Object)null)
		{
			ScaleFont(health, 0.8f);
			Place(health, 25f, 10f, fromRight: false, 0f, 1f, (TextAlignmentOptions)257, 500f, 70f, s, null);
		}
		foreach (TMP_Text item in ammoL)
		{
			ScaleFont(item, 0.8f);
		}
		foreach (TMP_Text item2 in ammoR)
		{
			ScaleFont(item2, 0.8f);
		}
		foreach (TMP_Text item3 in ammoL)
		{
			Place(item3, 200f, 880f, fromRight: false, 0.5f, 0.5f, (TextAlignmentOptions)514, 400f, 200f, s, AmmoColor);
		}
		foreach (TMP_Text item4 in ammoR)
		{
			Place(item4, 1735f, 880f, fromRight: true, 0.5f, 0.5f, (TextAlignmentOptions)514, 400f, 200f, s, AmmoColor);
		}
	}

	private void ScaleFont(TMP_Text t, float mul)
	{
		if (!baseSize.TryGetValue(t, out var value))
		{
			value = t.fontSize;
			baseSize[t] = value;
		}
		if (Mathf.Abs(t.fontSize - value * mul) > 0.01f)
		{
			t.fontSize = value * mul;
		}
	}

	private static bool IsEmptyAmmo(string str)
	{
		if (string.IsNullOrEmpty(str))
		{
			return true;
		}
		foreach (char c in str)
		{
			if (c != '-' && c != '_' && c != '–' && c != '—' && c != '−' && c != '.' && !char.IsWhiteSpace(c))
			{
				return false;
			}
		}
		return true;
	}

	private void Info(TMP_Text t, int line, float s)
	{
		//IL_008d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0098: Unknown result type (might be due to invalid IL or missing references)
		if (!((Component)t).gameObject.activeSelf)
		{
			((Component)t).gameObject.SetActive(true);
		}
		Canvas val = (((Object)(object)((Graphic)t).canvas != (Object)null) ? ((Graphic)t).canvas.rootCanvas : null);
		if (!((Object)(object)val == (Object)null))
		{
			float num = Mathf.Max(0.0001f, val.scaleFactor);
			if ((Object)(object)arial != (Object)null && (Object)(object)t.font != (Object)(object)arial)
			{
				t.font = arial;
			}
			t.fontStyle = (FontStyles)0;
			t.enableAutoSizing = false;
			t.margin = Vector4.zero;
			((Graphic)t).color = Color.black;
			float num2 = 9f * s / num;
			if (Mathf.Abs(t.fontSize - num2) > 0.01f)
			{
				t.fontSize = num2;
			}
			float ry = 4f + (float)line * 12.7f;
			Place(t, 8f, ry, fromRight: true, 1f, 1f, (TextAlignmentOptions)260, 300f, 13.5f, s, null, marginMode: true);
		}
	}

	private void Place(TMP_Text t, float rx, float ry, bool fromRight, float pivX, float pivY, TextAlignmentOptions al, float w, float h, float s, Color? col, bool marginMode = false)
	{
		//IL_0050: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c1: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c8: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d4: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ed: Unknown result type (might be due to invalid IL or missing references)
		//IL_0070: Unknown result type (might be due to invalid IL or missing references)
		//IL_0075: Unknown result type (might be due to invalid IL or missing references)
		//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_0133: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a0: Unknown result type (might be due to invalid IL or missing references)
		//IL_0153: Unknown result type (might be due to invalid IL or missing references)
		//IL_0164: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)t == (Object)null || !((Component)t).gameObject.activeInHierarchy)
		{
			return;
		}
		Canvas val = (((Object)(object)((Graphic)t).canvas != (Object)null) ? ((Graphic)t).canvas.rootCanvas : null);
		if ((Object)(object)val == (Object)null)
		{
			return;
		}
		float num = Mathf.Max(0.0001f, val.scaleFactor);
		t.alignment = al;
		t.enableWordWrapping = false;
		t.overflowMode = (TextOverflowModes)0;
		if (col.HasValue)
		{
			Color value = col.Value;
			if (IsEmptyAmmo(t.text))
			{
				value.a = 0f;
			}
			if (((Graphic)t).color != value)
			{
				((Graphic)t).color = value;
			}
		}
		RectTransform rectTransform = t.rectTransform;
		Vector2 val2 = default(Vector2);
		((Vector2)(ref val2))..ctor(0.5f, 0.5f);
		rectTransform.anchorMax = val2;
		rectTransform.anchorMin = val2;
		rectTransform.pivot = new Vector2(pivX, pivY);
		rectTransform.sizeDelta = new Vector2(w * s / num, h * s / num);
		float num2 = ((!marginMode) ? (fromRight ? ((float)Screen.width - (1920f - rx) * s) : (rx * s)) : ((float)Screen.width - rx * s));
		float num3 = (float)Screen.height - ry * s;
		Camera val3 = (((int)val.renderMode == 0) ? null : val.worldCamera);
		Transform transform = ((Component)val).transform;
		Vector3 position = default(Vector3);
		if (RectTransformUtility.ScreenPointToWorldPointInRectangle((RectTransform)(object)((transform is RectTransform) ? transform : null), new Vector2(num2, num3), val3, ref position))
		{
			((Transform)rectTransform).position = position;
		}
	}
}
internal static class Patches
{
	public static void HideHudPrefix(ref bool __0)
	{
		__0 = true;
	}
}