From 181013efecc3edffa6bcc6bf1cd8e9b7a77da98f Mon Sep 17 00:00:00 2001 From: Az <56699741+AlchlcDvl@users.noreply.github.com> Date: Mon, 26 Oct 2026 00:52:19 -0400 Subject: [PATCH] minor code fixes and nitpicks --- MiraAPI.Example/ExamplePlugin.cs | 2 +- MiraAPI/Patches/RoleGuidePatches.cs | 1 + .../Utilities/{ => Components}/LoadingRing.cs | 2 +- MiraAPI/Utilities/Extensions.cs | 51 ++++++++++--- MiraAPI/Utilities/GuideOptionsPatches.cs | 73 ++++++++++--------- 5 files changed, 80 insertions(+), 49 deletions(-) rename MiraAPI/Utilities/{ => Components}/LoadingRing.cs (98%) diff --git a/MiraAPI.Example/ExamplePlugin.cs b/MiraAPI.Example/ExamplePlugin.cs index 989e14c2..eadc271d 100644 --- a/MiraAPI.Example/ExamplePlugin.cs +++ b/MiraAPI.Example/ExamplePlugin.cs @@ -19,7 +19,7 @@ public partial class ExamplePlugin : BasePlugin, IMiraPlugin { public ExamplePlugin() { - MiraLocaleManager.Register("mira.example"); + MiraLocaleManager.Register("mira.example", typeof(ExamplePlugin).Assembly); } public Harmony Harmony { get; } = new(Id); diff --git a/MiraAPI/Patches/RoleGuidePatches.cs b/MiraAPI/Patches/RoleGuidePatches.cs index 1310bbd4..f658d6fa 100644 --- a/MiraAPI/Patches/RoleGuidePatches.cs +++ b/MiraAPI/Patches/RoleGuidePatches.cs @@ -16,6 +16,7 @@ using MiraAPI.Roles; using MiraAPI.Translation; using MiraAPI.Utilities; using MiraAPI.Utilities.Assets; +using MiraAPI.Utilities.Components; using Reactor.Utilities; using Reactor.Utilities.Extensions; using TMPro; diff --git a/MiraAPI/Utilities/LoadingRing.cs b/MiraAPI/Utilities/Components/LoadingRing.cs similarity index 98% rename from MiraAPI/Utilities/LoadingRing.cs rename to MiraAPI/Utilities/Components/LoadingRing.cs index 3f8af876..f871f59b 100644 --- a/MiraAPI/Utilities/LoadingRing.cs +++ b/MiraAPI/Utilities/Components/LoadingRing.cs @@ -2,7 +2,7 @@ using System; using Reactor.Utilities.Attributes; using UnityEngine; -namespace MiraAPI.Utilities; +namespace MiraAPI.Utilities.Components; /// /// A spinning ring used to indicate that content is loading. diff --git a/MiraAPI/Utilities/Extensions.cs b/MiraAPI/Utilities/Extensions.cs index 57fcd1a3..ae5c6af6 100644 --- a/MiraAPI/Utilities/Extensions.cs +++ b/MiraAPI/Utilities/Extensions.cs @@ -26,6 +26,8 @@ namespace MiraAPI.Utilities; /// public static class Extensions { + private static readonly Regex DisplayRegex = new(@"([^\^])([A-Z][a-z$])", default, Regex.InfiniteMatchTimeout); + internal static NetData GetNetData(this ICustomRole role) { var count = role.GetCount(); @@ -498,8 +500,7 @@ public static class Extensions /// A proper string for the provided . public static string ToDisplayString(this Enum @enum) { - var regex = new Regex(@"([^\^])([A-Z][a-z$])", default, Regex.InfiniteMatchTimeout); - return regex.Replace(@enum.ToString(), m => $"{m.Groups[1].Value} {m.Groups[2].Value}"); + return DisplayRegex.Replace(@enum.ToString(), m => $"{m.Groups[1].Value} {m.Groups[2].Value}"); } /// @@ -511,8 +512,7 @@ public static class Extensions public static string ToDisplayString(this T @enum) where T : struct, Enum { - var regex = new Regex(@"([^\^])([A-Z][a-z$])", default, Regex.InfiniteMatchTimeout); - return regex.Replace(@enum.ToString(), m => $"{m.Groups[1].Value} {m.Groups[2].Value}"); + return DisplayRegex.Replace(@enum.ToString(), m => $"{m.Groups[1].Value} {m.Groups[2].Value}"); } /// @@ -539,17 +539,46 @@ public static class Extensions /// The distance between the types. public static int GetInheritanceDistance(Type from, Type to) { - if (!from.IsAssignableFrom(to)) return int.MaxValue; + if (from == null || to == null) + throw new ArgumentNullException(from == null ? nameof(from) : nameof(to)); - var type = from; - var distance = 0; - while (type != null && type != to) + if (from == to) + return 0; + + if (!to.IsGenericTypeDefinition && !to.IsAssignableFrom(from)) + return int.MaxValue; + + var queue = new Queue<(Type Type, int Distance)>(); + var visited = new HashSet(); + + queue.Enqueue((from, 0)); + visited.Add(from); + + while (queue.Count > 0) { - type = type.BaseType; - distance++; + var (current, dist) = queue.Dequeue(); + + if (current == to || (current.IsGenericType && current.GetGenericTypeDefinition() == to)) + return dist; + + if (current.BaseType != null && visited.Add(current.BaseType)) + queue.Enqueue((current.BaseType, dist + 1)); + + var allInterfaces = current.GetInterfaces(); + var baseInterfaces = current.BaseType?.GetInterfaces() ?? Type.EmptyTypes; + + var directInterfaces = allInterfaces.Except(baseInterfaces).ToList(); + var inheritedInterfaces = directInterfaces.SelectMany(i => i.GetInterfaces()); + var immediateInterfaces = directInterfaces.Except(inheritedInterfaces); + + foreach (var iFace in immediateInterfaces) + { + if (visited.Add(iFace)) + queue.Enqueue((iFace, dist + 1)); + } } - return type == to ? distance : int.MaxValue; + return to.IsAssignableFrom(from) ? int.MaxValue - 1 : int.MaxValue; } /// diff --git a/MiraAPI/Utilities/GuideOptionsPatches.cs b/MiraAPI/Utilities/GuideOptionsPatches.cs index e33f6017..a86472ac 100644 --- a/MiraAPI/Utilities/GuideOptionsPatches.cs +++ b/MiraAPI/Utilities/GuideOptionsPatches.cs @@ -8,7 +8,6 @@ using MiraAPI.GameOptions.OptionTypes; using MiraAPI.PluginLoading; using MiraAPI.Translation; using MiraAPI.Utilities.Assets; -using Reactor.Utilities; using UnityEngine; namespace MiraAPI.Utilities; @@ -103,40 +102,42 @@ internal static class GuideOptionPages } } - if (entries.Count > 0) + if (entries.Count <= 0) { - var pageKey = item.Group.MatchInfoGuidePageKey?.Trim(); - var combinedKey = string.IsNullOrEmpty(pageKey) ? null : $"{item.Plugin.PluginId}\0{pageKey}"; - if (combinedKey != null && combinedPages.TryGetValue(combinedKey, out var combinedPage)) + continue; + } + + var pageKey = item.Group.MatchInfoGuidePageKey?.Trim(); + var combinedKey = string.IsNullOrEmpty(pageKey) ? null : $"{item.Plugin.PluginId}\0{pageKey}"; + if (combinedKey != null && combinedPages.TryGetValue(combinedKey, out var combinedPage)) + { + combinedPage.Groups.Add(item.Group); + combinedPage.Entries.AddRange(entries); + combinedPage.ModSearchTerms.UnionWith(modSearchTerms); + combinedPage.Order = Math.Min(combinedPage.Order, item.Group.MatchInfoGuidePageOrder); + combinedPage.MenuOrder = Math.Min( + combinedPage.MenuOrder, + GetMenuOrder(item.Group.ParentMenu)); + combinedPage.GroupPriority = Math.Min( + combinedPage.GroupPriority, + item.Group.GroupPriority); + } + else + { + var page = new GuideOptionPage( + item.Group, + item.Group.MatchInfoGuidePageTitle, + item.Group.MatchInfoGuidePageOrder, + GetMenuOrder(item.Group.ParentMenu), + item.Group.GroupPriority, + Mathf.Clamp(item.Group.MatchInfoGuidePageColumns, 1, 6), + item.ModAbbreviation, + modSearchTerms, + entries); + pages.Add(page); + if (combinedKey != null) { - combinedPage.Groups.Add(item.Group); - combinedPage.Entries.AddRange(entries); - combinedPage.ModSearchTerms.UnionWith(modSearchTerms); - combinedPage.Order = Math.Min(combinedPage.Order, item.Group.MatchInfoGuidePageOrder); - combinedPage.MenuOrder = Math.Min( - combinedPage.MenuOrder, - GetMenuOrder(item.Group.ParentMenu)); - combinedPage.GroupPriority = Math.Min( - combinedPage.GroupPriority, - item.Group.GroupPriority); - } - else - { - var page = new GuideOptionPage( - item.Group, - item.Group.MatchInfoGuidePageTitle, - item.Group.MatchInfoGuidePageOrder, - GetMenuOrder(item.Group.ParentMenu), - item.Group.GroupPriority, - Mathf.Clamp(item.Group.MatchInfoGuidePageColumns, 1, 6), - item.ModAbbreviation, - modSearchTerms, - entries); - pages.Add(page); - if (combinedKey != null) - { - combinedPages.Add(combinedKey, page); - } + combinedPages.Add(combinedKey, page); } } } @@ -269,7 +270,7 @@ internal static class GuideOptionPages } catch (Exception e) { - Logger.Warning($"GroupVisible threw for {group.GetType().Name}: {e.Message}"); + Warning($"GroupVisible threw for {group.GetType().Name}: {e.Message}"); return false; } } @@ -282,7 +283,7 @@ internal static class GuideOptionPages } catch (Exception e) { - Logger.Warning($"Visible threw for option '{option.Title}': {e.Message}"); + Warning($"Visible threw for option '{option.Title}': {e.Message}"); return false; } } @@ -303,7 +304,7 @@ internal static class GuideOptionPages } catch (Exception e) { - Logger.Warning($"Could not format option '{option.Title}' for the guide: {e.Message}"); + Warning($"Could not format option '{option.Title}' for the guide: {e.Message}"); return null; } }