Decompiled source of StatReformat v1.0.1

BepInEx/plugins/StatReformat.dll

Decompiled 2 months ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using System.Text;
using System.Text.RegularExpressions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Pigeon;
using TMPro;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyCompany("StatReformat")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("0.0.0.0")]
[assembly: AssemblyInformationalVersion("0.0.0-alpha.0")]
[assembly: AssemblyProduct("StatReformat")]
[assembly: AssemblyTitle("StatReformat")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.0.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace StatReformat
{
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "StatReformat";

		public const string PLUGIN_NAME = "StatReformat";

		public const string PLUGIN_VERSION = "0.0.0";
	}
}
namespace StatReformatMod
{
	[BepInPlugin("com.yourname.statreformat", "StatReformat", "1.0.0")]
	[MycoMod(/*Could not decode attribute arguments.*/)]
	public class StatReformatPlugin : BaseUnityPlugin
	{
		internal static ConfigEntry<bool> EnableStatReformat;

		internal static ConfigEntry<string> StatFormat;

		internal static ConfigEntry<bool> DebugStatLogging;

		internal static ConfigEntry<bool> EnableSafetyPatch;

		internal static ManualLogSource Logger;

		private Harmony harmony;

		private void Awake()
		{
			//IL_0094: Unknown result type (might be due to invalid IL or missing references)
			//IL_009a: Expected O, but got Unknown
			Logger = ((BaseUnityPlugin)this).Logger;
			EnableStatReformat = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "EnableStatReformat", true, "If true, forces all stats to 'Key: Value' format.");
			StatFormat = ((BaseUnityPlugin)this).Config.Bind<string>("General", "StatFormat", "{0}: {1}", "Format string for stats (e.g., '{0}: {1}' for 'Key: Value').");
			DebugStatLogging = ((BaseUnityPlugin)this).Config.Bind<bool>("Debug", "EnableStatLogging", true, "Logs raw and reformatted stats for debugging.");
			EnableSafetyPatch = ((BaseUnityPlugin)this).Config.Bind<bool>("Debug", "EnableSafetyPatch", false, "Enables ScoutLaserRifle safety (logs/skips nulls; disable if crashing).");
			Harmony val = new Harmony("com.yourname.statreformat");
			val.PatchAll();
			Logger.LogInfo((object)(val.Id + " loaded!"));
		}

		private void OnDestroy()
		{
			Harmony obj = harmony;
			if (obj != null)
			{
				obj.UnpatchSelf();
			}
		}

		public static void ReformatStats(Upgrade __instance, ref string text)
		{
			if (!EnableStatReformat.Value || string.IsNullOrEmpty(text))
			{
				return;
			}
			if (DebugStatLogging.Value)
			{
				Logger.LogDebug((object)("ReformatStats raw for " + __instance.APIName + ": " + text));
			}
			try
			{
				string[] array = text.Split(new string[2] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries);
				StringBuilder stringBuilder = new StringBuilder();
				string[] array2 = array;
				foreach (string text2 in array2)
				{
					string text3 = text2.Trim();
					if (string.IsNullOrWhiteSpace(text3))
					{
						stringBuilder.AppendLine();
						continue;
					}
					string input = Regex.Replace(text3, "<[^>]*>", "");
					if (Regex.IsMatch(input, "^[-+]?\\d"))
					{
						Match match = Regex.Match(input, "^([-+]?\\d+(?:\\.\\d+)?[%s]?)\\s*(.+)$");
						if (match.Success)
						{
							string value = match.Groups[1].Value;
							string text4 = match.Groups[2].Value.Trim();
							if (!string.IsNullOrEmpty(text4))
							{
								string value2 = text4 + ": <b>" + value + "</b>";
								stringBuilder.AppendLine(value2);
								continue;
							}
						}
					}
					stringBuilder.AppendLine(text2);
				}
				text = stringBuilder.ToString().TrimEnd(Array.Empty<char>());
				if (DebugStatLogging.Value)
				{
					Logger.LogDebug((object)("ReformatStats after for " + __instance.APIName + ": " + text));
				}
			}
			catch (Exception ex)
			{
				Logger.LogWarning((object)("ReformatStats: Failed for " + __instance.APIName + ": " + ex.Message));
			}
		}

		public static void ReformatUIText(TextMeshProUGUI textComponent, string fieldName = "text")
		{
			if (!EnableStatReformat.Value || (Object)(object)textComponent == (Object)null)
			{
				return;
			}
			string text = ((TMP_Text)textComponent).text;
			if (string.IsNullOrEmpty(text))
			{
				return;
			}
			if (DebugStatLogging.Value)
			{
				Logger.LogDebug((object)("ReformatUIText raw " + fieldName + ": " + text));
			}
			try
			{
				string[] array = text.Split(new string[2] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries);
				StringBuilder stringBuilder = new StringBuilder();
				string[] array2 = array;
				foreach (string text2 in array2)
				{
					string text3 = text2.Trim();
					if (string.IsNullOrWhiteSpace(text3))
					{
						stringBuilder.AppendLine();
						continue;
					}
					string input = Regex.Replace(text3, "<[^>]*>", "");
					if (Regex.IsMatch(input, "^[-+]?\\d"))
					{
						Match match = Regex.Match(input, "^([-+]?\\d+(?:\\.\\d+)?[%s]?)\\s*(.+)$");
						if (match.Success)
						{
							string value = match.Groups[1].Value;
							string text4 = match.Groups[2].Value.Trim();
							if (!string.IsNullOrEmpty(text4))
							{
								string value2 = text4 + ": <b>" + value + "</b>";
								stringBuilder.AppendLine(value2);
								continue;
							}
						}
					}
					stringBuilder.AppendLine(text2);
				}
				string text6 = (((TMP_Text)textComponent).text = stringBuilder.ToString().TrimEnd(Array.Empty<char>()));
				((TMP_Text)textComponent).ForceMeshUpdate(false, false);
				if (DebugStatLogging.Value)
				{
					Logger.LogDebug((object)("ReformatUIText after " + fieldName + ": " + text6));
				}
			}
			catch (Exception ex)
			{
				Logger.LogWarning((object)("ReformatUIText: Failed for " + fieldName + ": " + ex.Message));
			}
		}
	}
	[HarmonyPatch(typeof(Upgrade), "GetStatList")]
	public static class StatListReformatPatch
	{
		private static void Postfix(Upgrade __instance, int seed, ref string __result)
		{
			StatReformatPlugin.Logger.LogInfo((object)$"GetStatList called for {__instance.APIName}, raw result length: {__result?.Length ?? 0}");
			StatReformatPlugin.ReformatStats(__instance, ref __result);
		}
	}
	[HarmonyPatch(typeof(Upgrade), "ModifyDisplayedProperties")]
	public static class ModifyPropertiesReformatPatch
	{
		private static void Postfix(Upgrade __instance, ref string properties, UpgradeInstance instance)
		{
			StatReformatPlugin.Logger.LogInfo((object)$"ModifyDisplayedProperties called, raw properties length: {properties?.Length ?? 0}");
		}
	}
	[HarmonyPatch(typeof(HoverInfoDisplay), "Activate")]
	public static class HoverInfoDisplayReformatPatch
	{
		private static void Postfix(HoverInfoDisplay __instance, HoverInfo info, bool resetPosition)
		{
			//IL_004a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0059: Expected O, but got Unknown
			//IL_007e: Unknown result type (might be due to invalid IL or missing references)
			//IL_008d: Expected O, but got Unknown
			if (!StatReformatPlugin.EnableStatReformat.Value || (Object)(object)info == (Object)null)
			{
				return;
			}
			StatReformatPlugin.Logger.LogInfo((object)"Activate postfix fired!");
			try
			{
				FieldInfo fieldInfo = AccessTools.Field(typeof(HoverInfoDisplay), "text");
				if (fieldInfo != null)
				{
					StatReformatPlugin.ReformatUIText((TextMeshProUGUI)fieldInfo.GetValue(__instance), "main text");
				}
				FieldInfo fieldInfo2 = AccessTools.Field(typeof(HoverInfoDisplay), "statsText");
				if (fieldInfo2 != null)
				{
					StatReformatPlugin.ReformatUIText((TextMeshProUGUI)fieldInfo2.GetValue(__instance), "statsText");
				}
			}
			catch (Exception ex)
			{
				StatReformatPlugin.Logger.LogWarning((object)("HoverInfoDisplayReformatPatch: Failed: " + ex.Message));
			}
		}
	}
	[HarmonyPatch(typeof(HoverInfoDisplay), "Refresh")]
	public static class HoverInfoDisplayRefreshReformatPatch
	{
		private static void Postfix(HoverInfoDisplay __instance)
		{
			//IL_0077: Unknown result type (might be due to invalid IL or missing references)
			//IL_0086: Expected O, but got Unknown
			//IL_00ab: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ba: Expected O, but got Unknown
			StatReformatPlugin.Logger.LogInfo((object)"Refresh postfix fired!");
			FieldInfo fieldInfo = AccessTools.Field(typeof(HoverInfoDisplay), "selectedInfo");
			if (fieldInfo == null)
			{
				return;
			}
			object? value = fieldInfo.GetValue(__instance);
			HoverInfo val = (HoverInfo)((value is HoverInfo) ? value : null);
			if (!StatReformatPlugin.EnableStatReformat.Value || (Object)(object)val == (Object)null)
			{
				return;
			}
			try
			{
				FieldInfo fieldInfo2 = AccessTools.Field(typeof(HoverInfoDisplay), "text");
				if (fieldInfo2 != null)
				{
					StatReformatPlugin.ReformatUIText((TextMeshProUGUI)fieldInfo2.GetValue(__instance), "main text (refresh)");
				}
				FieldInfo fieldInfo3 = AccessTools.Field(typeof(HoverInfoDisplay), "statsText");
				if (fieldInfo3 != null)
				{
					StatReformatPlugin.ReformatUIText((TextMeshProUGUI)fieldInfo3.GetValue(__instance), "statsText (refresh)");
				}
			}
			catch (Exception ex)
			{
				StatReformatPlugin.Logger.LogWarning((object)("HoverInfoDisplayRefreshReformatPatch: Failed: " + ex.Message));
			}
		}
	}
}
namespace System.Runtime.CompilerServices
{
	[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
	internal sealed class IgnoresAccessChecksToAttribute : Attribute
	{
		public IgnoresAccessChecksToAttribute(string assemblyName)
		{
		}
	}
}