From cbc83a59f366c0573eb577c552773e7a1c514c00 Mon Sep 17 00:00:00 2001 From: Alexejhero <32238504+Alexejhero@users.noreply.github.com> Date: Fri, 16 Aug 2024 17:52:34 +0300 Subject: [PATCH] Improve ModIdentifier struct and address reviews --- Reactor/Patches/ReactorPingTracker.cs | 51 ++++++++++----------------- 1 file changed, 18 insertions(+), 33 deletions(-) diff --git a/Reactor/Patches/ReactorPingTracker.cs b/Reactor/Patches/ReactorPingTracker.cs index 0e2fc5e..59b6d03 100644 --- a/Reactor/Patches/ReactorPingTracker.cs +++ b/Reactor/Patches/ReactorPingTracker.cs @@ -9,36 +9,21 @@ namespace Reactor.Patches; /// public static class ReactorPingTracker { - private struct ModIdentifier + private readonly struct ModIdentifier(string modName, string version, bool isDevBuild, Func? shouldShow) { private static string NormalColor => !AmongUsClient.Instance.IsGameStarted ? "#fff" : "#fff8"; private static string DevColor => !AmongUsClient.Instance.IsGameStarted ? "#f00" : "#f008"; - public string ModName; - public string Version; - public bool IsDevBuild; - public Func? ShouldShow; + public string ModName => modName; + public string Text => $"{ModName} {version}"; - public readonly string Text => $"{ModName} {Version}"; + public bool ShouldShow() => shouldShow?.Invoke() ?? true; } - private static readonly List _modIdentifiers = - [ - new ModIdentifier - { - ModName = ReactorPlugin.Name, - Version = ReactorPlugin.Version, -#if DEBUG - IsDevBuild = true, -#else - IsDevBuild = false, -#endif - ShouldShow = () => AmongUsClient.Instance.IsGameStarted, - } - ]; + private static readonly List _modIdentifiers = []; /// - /// Registers a mod with the PingTrackerManager, adding it to the list of mods that will be displayed in the PingTracker. + /// Registers a mod with the , adding it to the list of mods that will be displayed in the PingTracker. /// /// The user-friendly name of the mod. Can contain spaces or special characters. /// The version of the mod. @@ -46,40 +31,40 @@ private struct ModIdentifier /// 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 null to avoid delegate calls. public static void RegisterMod(string modName, string version, bool isDevOrBetaBuild, Func? shouldShow) { + if (modName.Length + version.Length > 60) + { + Error($"Not registering mod \"{modName}\" with version \"{version}\" in {nameof(ReactorPingTracker)} because the combined length of the mod name and version is greater than 60 characters."); + return; + } + if (modName.Contains("", StringComparison.OrdinalIgnoreCase) || version.Contains("", StringComparison.OrdinalIgnoreCase)) { - Error($"Not registering mod \"{modName}\" with version \"{version}\" in PingTrackerManager because it contains the string \"\" which is disallowed."); + Error($"Not registering mod \"{modName}\" with version \"{version}\" in {nameof(ReactorPingTracker)} because it contains the string \"\" which is disallowed."); return; } if (_modIdentifiers.Any(m => m.ModName == modName)) { - Error($"Mod \"{modName}\" is already registered in PingTrackerManager."); + Error($"Mod \"{modName}\" is already registered in {nameof(ReactorPingTracker)}."); return; } - _modIdentifiers.Add(new ModIdentifier - { - ModName = modName, - Version = version, - IsDevBuild = isDevOrBetaBuild, - ShouldShow = shouldShow, - }); + _modIdentifiers.Add(new ModIdentifier(modName, version, isDevOrBetaBuild, shouldShow)); _modIdentifiers.Sort((a, b) => string.Compare(a.ModName, b.ModName, StringComparison.Ordinal)); if (!isDevOrBetaBuild) { - Info($"Mod \"{modName}\" registered in PingTrackerManager with version {version}."); + Info($"Mod \"{modName}\" registered in {nameof(ReactorPingTracker)} with version {version}."); } else { - Warning($"Mod \"{modName}\" registered in PingTrackerManager with DEVELOPMENT/BETA version {version}."); + Warning($"Mod \"{modName}\" registered in {nameof(ReactorPingTracker)} with DEVELOPMENT/BETA version {version}."); } } internal static string GetPingTrackerText() { - return " " + string.Join(", ", _modIdentifiers.Where(m => m.ShouldShow?.Invoke() ?? true).Select(m => m.Text)) + ""; + return " " + string.Join(", ", _modIdentifiers.Where(m => m.ShouldShow()).Select(m => m.Text)) + ""; } }