mirror of
https://github.com/All-Of-Us-Mods/MiraAPI.git
synced 2026-11-19 13:17:10 +00:00
Merge branch 'dev' into no-boxing
This commit is contained in:
15 files changed
+76
-66
No files matched your search
@@ -14,6 +14,7 @@ using Reactor.Utilities;
|
||||
|
||||
namespace MiraAPI.Example;
|
||||
|
||||
[RegisterEventContainer]
|
||||
public static class ExampleEventHandlers
|
||||
{
|
||||
public static void Initialize()
|
||||
|
||||
@@ -26,6 +26,9 @@ public partial class ExamplePlugin : BasePlugin, IMiraPlugin
|
||||
public string OptionsTitleText { get; } = "Mira API\nExample Mod";
|
||||
public string CustomOptionMenuNameTwo { get; } = "Example Options";
|
||||
|
||||
/// <inheritdoc />
|
||||
public bool ForceRegisterEvents => false;
|
||||
|
||||
public ConfigFile GetConfigFile()
|
||||
{
|
||||
return Config;
|
||||
|
||||
@@ -53,12 +53,6 @@ public static class PaletteManager
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!type.IsStatic())
|
||||
{
|
||||
Error($"Color class {type.Name} must be static.");
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach (var property in type.GetProperties(MiraPluginManager.DeclaredStaticMembers))
|
||||
{
|
||||
if (!property.PropertyType.IsAssignableTo(typeof(CustomColor)))
|
||||
|
||||
@@ -40,7 +40,7 @@ public sealed class VariableColor(
|
||||
/// <summary>
|
||||
/// Gets a delegate that takes in a time value and returns the dynamic shadow color.
|
||||
/// </summary>
|
||||
public Func<float, Color> ShadowEvaluator => shadowEvaluator ?? (t => mainEvaluator(t).DarkenAbsolute(0.24f));
|
||||
public Func<float, Color> ShadowEvaluator { get; } = shadowEvaluator ?? (t => mainEvaluator(t).DarkenAbsolute(0.24f));
|
||||
|
||||
/// <inheritdoc cref="VariableColor(StringNames?, string?, Func{float, Color}, Func{float, Color}?, Color32?, Color32?, float, CustomColorBrightness)"/>
|
||||
public VariableColor(
|
||||
@@ -91,6 +91,7 @@ public sealed class VariableColor(
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="VariableColor(StringNames?, string?, Func{float, Color}, Func{float, Color}?, Color32?, Color32?, float, CustomColorBrightness)"/>
|
||||
[Obsolete("Use the constructor that takes in a CustomColorBrightness parameter. This will be removed in a future version.")]
|
||||
public VariableColor(
|
||||
StringNames nameId,
|
||||
Func<float, Color> mainEvaluator,
|
||||
@@ -103,6 +104,7 @@ public sealed class VariableColor(
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="VariableColor(StringNames?, string?, Func{float, Color}, Func{float, Color}?, Color32?, Color32?, float, CustomColorBrightness)"/>
|
||||
[Obsolete("Use the constructor that takes in a CustomColorBrightness parameter. This will be removed in a future version.")]
|
||||
public VariableColor(
|
||||
StringNames nameId,
|
||||
Func<float, Color> mainEvaluator,
|
||||
@@ -113,6 +115,7 @@ public sealed class VariableColor(
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="VariableColor(StringNames?, string?, Func{float, Color}, Func{float, Color}?, Color32?, Color32?, float, CustomColorBrightness)"/>
|
||||
[Obsolete("Use the constructor that takes in a CustomColorBrightness parameter. This will be removed in a future version.")]
|
||||
public VariableColor(
|
||||
string rawId,
|
||||
Func<float, Color> mainEvaluator,
|
||||
@@ -125,6 +128,7 @@ public sealed class VariableColor(
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="VariableColor(StringNames?, string?, Func{float, Color}, Func{float, Color}?, Color32?, Color32?, float, CustomColorBrightness)"/>
|
||||
[Obsolete("Use the constructor that takes in a CustomColorBrightness parameter. This will be removed in a future version.")]
|
||||
public VariableColor(
|
||||
string rawId,
|
||||
Func<float, Color> mainEvaluator,
|
||||
|
||||
@@ -156,8 +156,13 @@ public static class MiraEventManager
|
||||
return true;
|
||||
}
|
||||
|
||||
internal static void RegisterEvents(Type type)
|
||||
internal static void RegisterEvents(Type type, IMiraPlugin miraPlugin)
|
||||
{
|
||||
if (!miraPlugin.ForceRegisterEvents && !type.IsDefined<RegisterEventContainerAttribute>())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (var method in type.GetMethods(MiraPluginManager.DeclaredStaticMembers))
|
||||
{
|
||||
if (!method.TryGetCustomAttribute<RegisterEventAttribute>(out var eventAttribute))
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
using System;
|
||||
|
||||
namespace MiraAPI.Events;
|
||||
|
||||
/// <summary>
|
||||
/// Register a container of <see cref="MiraEvent"/>s.
|
||||
/// </summary>
|
||||
[AttributeUsage(AttributeTargets.Class)]
|
||||
public class RegisterEventContainerAttribute : Attribute;
|
||||
@@ -14,13 +14,7 @@ public static class GameOverManager
|
||||
|
||||
private static int nextId = Enum.GetNames<GameOverReason>().Length;
|
||||
|
||||
/// <summary>
|
||||
/// Register a <see cref="CustomGameOver"/>.
|
||||
/// </summary>
|
||||
/// <param name="gameOverType">Type of the <see cref="CustomGameOver"/>.</param>
|
||||
/// <exception cref="ArgumentException">Thrown when the type is not a subclass of <see cref="CustomGameOver"/>, is abstract, or does not have a parameterless constructor.</exception>
|
||||
/// <returns><see langword="true"/> if the game over was registered successfully, <see langword="false"/> otherwise.</returns>
|
||||
public static bool RegisterGameOver(Type gameOverType)
|
||||
internal static bool RegisterGameOver(Type gameOverType)
|
||||
{
|
||||
if (!typeof(CustomGameOver).IsAssignableFrom(gameOverType))
|
||||
{
|
||||
|
||||
@@ -19,12 +19,6 @@ public static class CustomGameModeManager
|
||||
return ++lastId;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Register gamemode from type.
|
||||
/// </summary>
|
||||
/// <param name="gameModeType">Type of gamemode class, should inherit from <see cref="AbstractGameMode"/>.</param>
|
||||
/// <param name="pluginInfo">The custom plugin info of the mod.</param>
|
||||
/// <returns>Whether the gamemode was successfully registered.</returns>
|
||||
internal static bool RegisterGameMode(Type gameModeType, MiraPluginInfo pluginInfo)
|
||||
{
|
||||
if (!typeof(AbstractGameMode).IsAssignableFrom(gameModeType))
|
||||
|
||||
@@ -85,12 +85,6 @@ public static class KeybindManager
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!type.IsStatic())
|
||||
{
|
||||
Error($"Keybinds class {type.Name} must be static.");
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach (var property in type.GetProperties(MiraPluginManager.DeclaredStaticMembers))
|
||||
{
|
||||
if (property.PropertyType != typeof(MiraKeybind))
|
||||
|
||||
@@ -8,7 +8,6 @@ using MiraAPI.Patches;
|
||||
using MiraAPI.Patches.Roles;
|
||||
using MiraAPI.PluginLoading;
|
||||
using MiraAPI.Translation;
|
||||
using MiraAPI.VanillaEvents;
|
||||
using Reactor;
|
||||
using Reactor.Localization;
|
||||
using Reactor.Networking;
|
||||
@@ -44,6 +43,9 @@ public partial class MiraApiPlugin : BasePlugin, IMiraPlugin
|
||||
/// <inheritdoc />
|
||||
public bool DebugMode => IsDevBuild;
|
||||
|
||||
/// <inheritdoc />
|
||||
public bool ForceRegisterEvents => false;
|
||||
|
||||
/// <summary>
|
||||
/// Gets a value indicating whether the current device is running Starlight (on mobile).
|
||||
/// </summary>
|
||||
@@ -96,8 +98,6 @@ public partial class MiraApiPlugin : BasePlugin, IMiraPlugin
|
||||
"Dictates the unit of distance used in numeric option displays representing a distance.");
|
||||
Harmony.PatchAll();
|
||||
RoleOptionsCollectionPatch.PatchRoleMethods(Harmony);
|
||||
UiResetEvents.Initialize();
|
||||
JudgeEvents.Initialize();
|
||||
|
||||
ReactorCredits.Register("Mira API", Version, IsDevBuild, ReactorCredits.AlwaysShow);
|
||||
|
||||
|
||||
@@ -68,8 +68,9 @@ public static class ModifierManager
|
||||
return false;
|
||||
}
|
||||
|
||||
IdToTypeModifierMap.Add(GetNextTypeId(), modifierType);
|
||||
TypeToIdModifierMap.Add(modifierType, _nextTypeId);
|
||||
var id = GetNextTypeId();
|
||||
IdToTypeModifierMap.Add(id, modifierType);
|
||||
TypeToIdModifierMap.Add(modifierType, id);
|
||||
|
||||
BaseModifier modifier;
|
||||
var ctor = modifierType.GetConstructor(Type.EmptyTypes);
|
||||
@@ -105,7 +106,7 @@ public static class ModifierManager
|
||||
PrioritiesToIdsMap[priority] = list = [];
|
||||
}
|
||||
|
||||
list.Add(_nextTypeId);
|
||||
list.Add(id);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
using System.Globalization;
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using BepInEx.Configuration;
|
||||
using MiraAPI.Events;
|
||||
|
||||
namespace MiraAPI.PluginLoading;
|
||||
|
||||
@@ -63,6 +65,17 @@ public interface IMiraPlugin
|
||||
/// </summary>
|
||||
bool DebugMode => false;
|
||||
|
||||
/// <summary>
|
||||
/// Gets a value indicating whether the plugin relies on the legacy global event scanning system.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This property defaults to true to maintain backwards compatibility.
|
||||
/// To upgrade your mod, apply the <see cref="RegisterEventContainerAttribute"/> to any class
|
||||
/// containing events, and then override this property to return <see langword="false"/>.
|
||||
/// </remarks>
|
||||
[Obsolete("Global event scanning is deprecated. Add [RegisterEventContainer] to your event classes and override this property to false.")]
|
||||
bool ForceRegisterEvents => true;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the <see cref="ConfigFile"/> for the plugin.
|
||||
/// </summary>
|
||||
|
||||
@@ -49,6 +49,16 @@ public sealed class MiraPluginManager
|
||||
internal static ConfigFile MiraApiConfig { get; private set; } = null!;
|
||||
internal static MiraPluginInfo MiraApiPluginInfo { get; private set; } = null!;
|
||||
|
||||
/// <summary>
|
||||
/// Get a mira plugin by its GUID.
|
||||
/// </summary>
|
||||
/// <param name="pluginId">The plugin GUID.</param>
|
||||
/// <returns>A <see cref="MiraPluginInfo"/>.</returns>
|
||||
public static MiraPluginInfo? GetPluginByGuid(string pluginId)
|
||||
{
|
||||
return Instance._registeredPlugins.GetValueOrDefault(pluginId);
|
||||
}
|
||||
|
||||
internal void Initialize(BasePlugin miraApiPlugin, IMiraPlugin apiPlugin)
|
||||
{
|
||||
CustomGameModeManager.RegisterDefaultMode();
|
||||
@@ -130,12 +140,17 @@ public sealed class MiraPluginManager
|
||||
[SuppressMessage("Minor Code Smell", "S3626:Jump statements should not be redundant", Justification = "Code consistency.")]
|
||||
private static void ProcessAssemblyTypes(Assembly assembly, MiraPluginInfo info, BasePlugin basePlugin, IMiraPlugin plugin, List<Type> roles, bool isMira)
|
||||
{
|
||||
if (plugin.ForceRegisterEvents)
|
||||
{
|
||||
Warning("For optimisation reasons, MiraAPI no longer automatically searches every type for events. Please use RegisterEventsAttribute moving forward.");
|
||||
}
|
||||
|
||||
foreach (var type in assembly.GetTypes())
|
||||
{
|
||||
if ((isMira && type.IsAbstract) || type.IsDefined<MiraIgnoreAttribute>())
|
||||
continue;
|
||||
|
||||
RegisterEvents(type);
|
||||
RegisterEvents(type, plugin);
|
||||
|
||||
if (RegisterOptions(type, info))
|
||||
continue;
|
||||
@@ -172,11 +187,11 @@ public sealed class MiraPluginManager
|
||||
}
|
||||
}
|
||||
|
||||
private static void RegisterEvents(Type type)
|
||||
private static void RegisterEvents(Type type, IMiraPlugin miraPlugin)
|
||||
{
|
||||
try
|
||||
{
|
||||
MiraEventManager.RegisterEvents(type);
|
||||
MiraEventManager.RegisterEvents(type, miraPlugin);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
@@ -184,16 +199,6 @@ public sealed class MiraPluginManager
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get a mira plugin by its GUID.
|
||||
/// </summary>
|
||||
/// <param name="pluginId">The plugin GUID.</param>
|
||||
/// <returns>A <see cref="MiraPluginInfo"/>.</returns>
|
||||
public static MiraPluginInfo? GetPluginByGuid(string pluginId)
|
||||
{
|
||||
return Instance._registeredPlugins.GetValueOrDefault(pluginId);
|
||||
}
|
||||
|
||||
private static bool RegisterGameOver(Type type)
|
||||
{
|
||||
try
|
||||
|
||||
@@ -4,15 +4,11 @@ using MiraAPI.Roles;
|
||||
|
||||
namespace MiraAPI.VanillaEvents;
|
||||
|
||||
[RegisterEventContainer]
|
||||
internal static class JudgeEvents
|
||||
{
|
||||
public static void Initialize()
|
||||
{
|
||||
// This is required because MiraAPI isn't an IMiraPlugin (besides in the gamemodes branch)
|
||||
MiraEventManager.RegisterEventHandler<ProcessVotesEvent>(ProcessVotesEventHandler, -1000);
|
||||
}
|
||||
|
||||
private static void ProcessVotesEventHandler(ProcessVotesEvent @event)
|
||||
[RegisterEvent(-1000)]
|
||||
public static void ProcessVotesEventHandler(ProcessVotesEvent @event)
|
||||
{
|
||||
if (!MeetingHud.Instance.TryGetWinningOverrule(
|
||||
out var judgeOverrule,
|
||||
|
||||
@@ -5,17 +5,11 @@ using MiraAPI.LocalSettings;
|
||||
|
||||
namespace MiraAPI.VanillaEvents;
|
||||
|
||||
[RegisterEventContainer]
|
||||
internal static class UiResetEvents
|
||||
{
|
||||
public static void Initialize()
|
||||
{
|
||||
MiraEventManager.RegisterEventHandler<UiButtonResetEvent>(ResetButtonParents);
|
||||
MiraEventManager.RegisterEventHandler<UiButtonPostResetEvent>(PlaceWikiButton, -900);
|
||||
MiraEventManager.RegisterEventHandler<UiButtonPostResetEvent>(PlaceSubmergedButton, -800);
|
||||
MiraEventManager.RegisterEventHandler<UiButtonPostResetEvent>(PlaceModifierUi, -700);
|
||||
}
|
||||
|
||||
private static void ResetButtonParents(UiButtonResetEvent _)
|
||||
[RegisterEvent]
|
||||
public static void ResetButtonParents(UiButtonResetEvent _)
|
||||
{
|
||||
var wikiButton = MiraHudHelper.VanillaMatchInfoButton;
|
||||
var subButton = MiraHudHelper.SubmergedFloorButton;
|
||||
@@ -36,7 +30,8 @@ internal static class UiResetEvents
|
||||
}
|
||||
}
|
||||
|
||||
private static void PlaceWikiButton(UiButtonPostResetEvent @event)
|
||||
[RegisterEvent(-900)]
|
||||
public static void PlaceWikiButton(UiButtonPostResetEvent @event)
|
||||
{
|
||||
var wikiButton = MiraHudHelper.VanillaMatchInfoButton;
|
||||
if (!wikiButton)
|
||||
@@ -50,7 +45,8 @@ internal static class UiResetEvents
|
||||
wikiButton.transform.SetParent(opts.WikiOnBottomRow.Value ? secondRow.transform : firstRow.transform);
|
||||
}
|
||||
|
||||
private static void PlaceSubmergedButton(UiButtonPostResetEvent @event)
|
||||
[RegisterEvent(-800)]
|
||||
public static void PlaceSubmergedButton(UiButtonPostResetEvent @event)
|
||||
{
|
||||
var subButton = MiraHudHelper.SubmergedFloorButton;
|
||||
if (!subButton)
|
||||
@@ -62,7 +58,8 @@ internal static class UiResetEvents
|
||||
subButton.transform.SetParent(secondRow.transform);
|
||||
}
|
||||
|
||||
private static void PlaceModifierUi(UiButtonPostResetEvent @event)
|
||||
[RegisterEvent(-700)]
|
||||
public static void PlaceModifierUi(UiButtonPostResetEvent @event)
|
||||
{
|
||||
var modDisplay = MiraHudHelper.ModifierDisplayOnRight ? MiraHudHelper.ModifierDisplayObject : null!;
|
||||
if (!modDisplay)
|
||||
|
||||
Reference in new issue
Block a user