From 01ebe48b093dba29321f0b89e4018e61db9ff3b0 Mon Sep 17 00:00:00 2001 From: js6pak Date: Tue, 20 Aug 2024 19:57:40 +0200 Subject: [PATCH] Refactor --- .../Patches/Miscellaneous/PingTrackerPatch.cs | 8 +++- .../Extensions/RichTextExtensions.cs | 40 +++++++++++++++++++ Reactor/Utilities/ReactorPingTracker.cs | 29 +++++++------- 3 files changed, 61 insertions(+), 16 deletions(-) create mode 100644 Reactor/Utilities/Extensions/RichTextExtensions.cs diff --git a/Reactor/Patches/Miscellaneous/PingTrackerPatch.cs b/Reactor/Patches/Miscellaneous/PingTrackerPatch.cs index 945aff7..071dc3f 100644 --- a/Reactor/Patches/Miscellaneous/PingTrackerPatch.cs +++ b/Reactor/Patches/Miscellaneous/PingTrackerPatch.cs @@ -11,7 +11,11 @@ internal static class PingTrackerPatch [HarmonyPriority(Priority.Last)] public static void Postfix(PingTracker __instance) { - if (!__instance.text.text.EndsWith("\n", StringComparison.InvariantCulture)) __instance.text.text += "\n"; - __instance.text.text += ReactorPingTracker.GetPingTrackerText(); + var extraText = ReactorPingTracker.GetText(); + if (extraText != null) + { + if (!__instance.text.text.EndsWith("\n", StringComparison.InvariantCulture)) __instance.text.text += "\n"; + __instance.text.text += extraText; + } } } diff --git a/Reactor/Utilities/Extensions/RichTextExtensions.cs b/Reactor/Utilities/Extensions/RichTextExtensions.cs new file mode 100644 index 0000000..156a8c0 --- /dev/null +++ b/Reactor/Utilities/Extensions/RichTextExtensions.cs @@ -0,0 +1,40 @@ +namespace Reactor.Utilities.Extensions; + +/// +/// Provides extension methods for TestMeshPro's Rich Text. +/// +internal static class RichTextExtensions +{ + private static string Wrap(this string text, string tag) + { + return $"<{tag}>{text}"; + } + + private static string Wrap(this string text, string tag, string value) + { + return $"<{tag}={value}>{text}"; + } + + public static string Align(this string text, string value) + { + return text.Wrap("align", value); + } + + public static string Color(this string text, string value) + { + return text.Wrap("color", value); + } + + public static string Size(this string text, string value) + { + return text.Wrap("size", value); + } + + public static string EscapeRichText(this string text) + { + return text + .Replace("", string.Empty) + .Replace("", string.Empty) + .Wrap("noparse"); + } +} diff --git a/Reactor/Utilities/ReactorPingTracker.cs b/Reactor/Utilities/ReactorPingTracker.cs index 3468303..beb8325 100644 --- a/Reactor/Utilities/ReactorPingTracker.cs +++ b/Reactor/Utilities/ReactorPingTracker.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using BepInEx.Unity.IL2CPP; +using Reactor.Utilities.Extensions; namespace Reactor.Utilities; @@ -12,13 +13,13 @@ public static class ReactorPingTracker { private readonly struct ModIdentifier(string name, string version, Func? shouldShow, bool isPreRelease) { - private static string NormalColor => !AmongUsClient.Instance.IsGameStarted ? "#fff" : "#fff7"; - private static string DevColor => !AmongUsClient.Instance.IsGameStarted ? "#f00" : "#f447"; + private const string NormalColor = "#fff"; + private const string PreReleaseColor = "#f00"; public string Name => name; public bool ShouldShow => shouldShow == AlwaysShow || shouldShow(); - public string Text => $"{Name} {version}"; + public string Text { get; } = $"{name} {version}".EscapeRichText().Color(isPreRelease ? PreReleaseColor : NormalColor); } private static readonly List _modIdentifiers = []; @@ -33,13 +34,13 @@ private readonly struct ModIdentifier(string name, string version, Func? s /// /// The user-friendly name of the mod. Can contain spaces or special characters. /// The version of the mod. + /// If this version is a development or beta version. If true, it will display the mod in red in the PingTracker. /// /// This function will be called every frame to determine if the mod should be displayed or not. /// This function should return false if your mod is currently disabled or has no effect on gameplay at the time. /// If you want the mod to be displayed at all times, you can set this parameter to . /// - /// If this version is a development or beta version. If true, it will display the mod in red in the PingTracker. - public static void Register(string name, string version, Func? shouldShow, bool isPreRelease = false) + public static void Register(string name, string version, bool isPreRelease, Func? shouldShow) { const int MaxLength = 60; @@ -49,12 +50,6 @@ public static void Register(string name, string version, Func? shouldShow, return; } - if (name.Contains("", StringComparison.OrdinalIgnoreCase) || version.Contains("", StringComparison.OrdinalIgnoreCase)) - { - Error($"Not registering mod \"{name}\" with version \"{version}\" in {nameof(ReactorPingTracker)} because it contains the string \"\" which is disallowed."); - return; - } - if (_modIdentifiers.Any(m => m.Name == name)) { Error($"Mod \"{name}\" is already registered in {nameof(ReactorPingTracker)}."); @@ -87,11 +82,17 @@ public static void Register(Func? shouldShow) where T : BasePlugin var metadata = pluginInfo.Metadata; - Register(metadata.Name, metadata.Version.ToString(), shouldShow, metadata.Version.IsPreRelease); + Register(metadata.Name, metadata.Version.ToString(), metadata.Version.IsPreRelease, shouldShow); } - internal static string GetPingTrackerText() + internal static string? GetText() { - return "" + string.Join(", ", _modIdentifiers.Where(m => m.ShouldShow).Select(m => m.Text)) + ""; + var mods = _modIdentifiers.Where(m => m.ShouldShow).Select(m => m.Text).ToArray(); + if (mods.Length == 0) + { + return null; + } + + return ("" + string.Join(", ", mods)).Size("50%").Align("center"); } }