Skip to content

Commit

Permalink
Improve ModIdentifier struct and address reviews
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexejhero committed Aug 16, 2024
1 parent e83f1a3 commit cbc83a5
Showing 1 changed file with 18 additions and 33 deletions.
51 changes: 18 additions & 33 deletions Reactor/Patches/ReactorPingTracker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,77 +9,62 @@ namespace Reactor.Patches;
/// </summary>
public static class ReactorPingTracker
{
private struct ModIdentifier
private readonly struct ModIdentifier(string modName, string version, bool isDevBuild, Func<bool>? 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<bool>? ShouldShow;
public string ModName => modName;
public string Text => $"</noparse><color={(isDevBuild ? DevColor : NormalColor)}><noparse>{ModName} {version}</noparse></color><noparse>";

public readonly string Text => $"</noparse><color={(IsDevBuild ? DevColor : NormalColor)}><noparse>{ModName} {Version}</noparse></color><noparse>";
public bool ShouldShow() => shouldShow?.Invoke() ?? true;
}

private static readonly List<ModIdentifier> _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<ModIdentifier> _modIdentifiers = [];

/// <summary>
/// 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 <see cref="ReactorPingTracker"/>, adding it to the list of mods that will be displayed in the PingTracker.
/// </summary>
/// <param name="modName">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="isDevOrBetaBuild">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 null to avoid delegate calls.</param>
public static void RegisterMod(string modName, string version, bool isDevOrBetaBuild, Func<bool>? 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("</noparse>", StringComparison.OrdinalIgnoreCase) || version.Contains("</noparse>", StringComparison.OrdinalIgnoreCase))
{
Error($"Not registering mod \"{modName}\" with version \"{version}\" in PingTrackerManager because it contains the string \"</noparse>\" which is disallowed.");
Error($"Not registering mod \"{modName}\" with version \"{version}\" in {nameof(ReactorPingTracker)} because it contains the string \"</noparse>\" 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 "<align=center><size=50%><noparse> " + string.Join(", ", _modIdentifiers.Where(m => m.ShouldShow?.Invoke() ?? true).Select(m => m.Text)) + "</noparse></size></align>";
return "<align=center><size=50%><noparse> " + string.Join(", ", _modIdentifiers.Where(m => m.ShouldShow()).Select(m => m.Text)) + "</noparse></size></align>";
}
}

0 comments on commit cbc83a5

Please sign in to comment.