Compare commits

...
1 Commits
Author SHA1 Message Date
Az 181013efec minor code fixes and nitpicks 2026-10-26 00:52:19 -04:00
5 changed files with 80 additions and 49 deletions

No files matched your search

+1 -1
View File
@@ -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);
+1
View File
@@ -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;
@@ -2,7 +2,7 @@ using System;
using Reactor.Utilities.Attributes;
using UnityEngine;
namespace MiraAPI.Utilities;
namespace MiraAPI.Utilities.Components;
/// <summary>
/// A spinning ring used to indicate that content is loading.
+40 -11
View File
@@ -26,6 +26,8 @@ namespace MiraAPI.Utilities;
/// </summary>
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
/// <returns>A proper string for the provided <see cref="Enum"/>.</returns>
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}");
}
/// <summary>
@@ -511,8 +512,7 @@ public static class Extensions
public static string ToDisplayString<T>(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}");
}
/// <summary>
@@ -539,17 +539,46 @@ public static class Extensions
/// <returns>The distance between the types.</returns>
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<Type>();
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;
}
/// <summary>
+37 -36
View File
@@ -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<MiraApiPlugin>.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<MiraApiPlugin>.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<MiraApiPlugin>.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;
}
}