Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
js6pak committed Aug 22, 2024
1 parent 09cea54 commit 03c6315
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 16 deletions.
8 changes: 6 additions & 2 deletions Reactor/Patches/Miscellaneous/PingTrackerPatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
40 changes: 40 additions & 0 deletions Reactor/Utilities/Extensions/RichTextExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
namespace Reactor.Utilities.Extensions;

/// <summary>
/// Provides extension methods for TestMeshPro's Rich Text.
/// </summary>
internal static class RichTextExtensions
{
private static string Wrap(this string text, string tag)
{
return $"<{tag}>{text}</{tag}>";
}

private static string Wrap(this string text, string tag, string value)
{
return $"<{tag}={value}>{text}</{tag}>";
}

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("<noparse>", string.Empty)
.Replace("</noparse>", string.Empty)
.Wrap("noparse");
}
}
29 changes: 15 additions & 14 deletions Reactor/Utilities/ReactorPingTracker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using BepInEx.Unity.IL2CPP;
using Reactor.Utilities.Extensions;

namespace Reactor.Utilities;

Expand All @@ -12,13 +13,13 @@ public static class ReactorPingTracker
{
private readonly struct ModIdentifier(string name, string version, Func<bool>? 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 => $"</noparse><color={(isPreRelease ? DevColor : NormalColor)}><noparse>{Name} {version}</noparse></color><noparse>";
public string Text { get; } = $"{name} {version}".EscapeRichText().Color(isPreRelease ? PreReleaseColor : NormalColor);
}

private static readonly List<ModIdentifier> _modIdentifiers = [];
Expand All @@ -33,13 +34,13 @@ private readonly struct ModIdentifier(string name, string version, Func<bool>? s
/// </summary>
/// <param name="name">The user-friendly name of the mod. Can contain spaces or special characters.</param>
/// <param name="version">The version of the mod.</param>
/// <param name="isPreRelease">If this version is a development or beta version. If true, it will display the mod in red in the PingTracker.</param>
/// <param name="shouldShow">
/// 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 <see cref="ReactorPingTracker.AlwaysShow"/>.
/// </param>
/// <param name="isPreRelease">If this version is a development or beta version. If true, it will display the mod in red in the PingTracker.</param>
public static void Register(string name, string version, Func<bool>? shouldShow, bool isPreRelease = false)
public static void Register(string name, string version, bool isPreRelease, Func<bool>? shouldShow)
{
const int MaxLength = 60;

Expand All @@ -49,12 +50,6 @@ public static void Register(string name, string version, Func<bool>? shouldShow,
return;
}

if (name.Contains("</noparse>", StringComparison.OrdinalIgnoreCase) || version.Contains("</noparse>", StringComparison.OrdinalIgnoreCase))
{
Error($"Not registering mod \"{name}\" with version \"{version}\" in {nameof(ReactorPingTracker)} because it contains the string \"</noparse>\" which is disallowed.");
return;
}

if (_modIdentifiers.Any(m => m.Name == name))
{
Error($"Mod \"{name}\" is already registered in {nameof(ReactorPingTracker)}.");
Expand Down Expand Up @@ -87,11 +82,17 @@ public static void Register<T>(Func<bool>? 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 "<align=center><size=50%><space=3em><noparse>" + string.Join(", ", _modIdentifiers.Where(m => m.ShouldShow).Select(m => m.Text)) + "</noparse></size></align>";
var mods = _modIdentifiers.Where(m => m.ShouldShow).Select(m => m.Text).ToArray();
if (mods.Length == 0)
{
return null;
}

return ("<space=3em>" + string.Join(", ", mods)).Size("50%").Align("center");
}
}

0 comments on commit 03c6315

Please sign in to comment.