mirror of
https://github.com/All-Of-Us-Mods/MiraAPI.git
synced 2026-10-20 01:46:15 +00:00
further optimization?
This commit is contained in:
1 parent
bb27f83e8b
commit
59ab2736ab
4 files changed
+74
-32
No files matched your search
@@ -31,7 +31,6 @@ public static class ModdedOptionsManager
|
||||
{
|
||||
if (Activator.CreateInstance(type) is not AbstractOptionGroup group)
|
||||
{
|
||||
Logger<MiraApiPlugin>.Error($"Skipping option group {type.Name}. Does not inherit AbstractOptionGroup.");
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@@ -16,18 +16,11 @@ public static class CustomButtonManager
|
||||
internal static readonly Dictionary<Type, Type> ButtonEventTypes = [];
|
||||
internal static readonly Dictionary<Type, Type> ButtonCancelledEventTypes = [];
|
||||
|
||||
internal static void RegisterButton(Type buttonType, MiraPluginInfo pluginInfo)
|
||||
internal static bool RegisterButton(Type buttonType, MiraPluginInfo pluginInfo)
|
||||
{
|
||||
if (!typeof(CustomActionButton).IsAssignableFrom(buttonType))
|
||||
if (!buttonType.IsAssignableTo(typeof(CustomActionButton)) || Activator.CreateInstance(buttonType) is not CustomActionButton button)
|
||||
{
|
||||
Logger<MiraApiPlugin>.Error($"Skipping button {buttonType.Name}. Does not inherit CustomActionButton!");
|
||||
return;
|
||||
}
|
||||
|
||||
if (Activator.CreateInstance(buttonType) is not CustomActionButton button)
|
||||
{
|
||||
Logger<MiraApiPlugin>.Error($"Failed to create button from {buttonType.Name}");
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
CustomButtons.Add(button);
|
||||
@@ -38,5 +31,7 @@ public static class CustomButtonManager
|
||||
|
||||
ButtonEventTypes.Add(buttonType, typeof(MiraButtonClickEvent<>).MakeGenericType(buttonType));
|
||||
ButtonCancelledEventTypes.Add(buttonType, typeof(MiraButtonCancelledEvent<>).MakeGenericType(buttonType));
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -49,11 +49,11 @@ public static class ModifierManager
|
||||
return TypeToIdModifierMap.GetValueOrDefault(type);
|
||||
}
|
||||
|
||||
internal static void RegisterModifier(Type modifierType)
|
||||
internal static bool RegisterModifier(Type modifierType)
|
||||
{
|
||||
if (!typeof(BaseModifier).IsAssignableFrom(modifierType))
|
||||
{
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
IdToTypeModifierMap.Add(GetNextId(), modifierType);
|
||||
@@ -61,7 +61,7 @@ public static class ModifierManager
|
||||
|
||||
if (!typeof(GameModifier).IsAssignableFrom(modifierType))
|
||||
{
|
||||
return;
|
||||
return true;
|
||||
}
|
||||
|
||||
var mod = Activator.CreateInstance(modifierType) as GameModifier;
|
||||
@@ -73,6 +73,7 @@ public static class ModifierManager
|
||||
}
|
||||
|
||||
list.Add(_nextId);
|
||||
return true;
|
||||
}
|
||||
|
||||
internal static void AssignModifiers(List<PlayerControl> plrs)
|
||||
|
||||
@@ -47,15 +47,27 @@ public sealed class MiraPluginManager
|
||||
continue;
|
||||
}
|
||||
|
||||
RegisterModifier(type);
|
||||
RegisterOptions(type, info);
|
||||
if (RegisterModifier(type))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (RegisterOptions(type, info))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (RegisterRoleAttribute(type, info, out var role))
|
||||
{
|
||||
roles.Add(role!);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (RegisterButtonAttribute(type, info))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
RegisterButtonAttribute(type, info);
|
||||
RegisterColorClasses(type);
|
||||
}
|
||||
|
||||
@@ -72,23 +84,25 @@ public sealed class MiraPluginManager
|
||||
/// <summary>
|
||||
/// Get a mira plugin by its GUID.
|
||||
/// </summary>
|
||||
/// <param name="guid">The plugin GUID.</param>
|
||||
/// <param name="pluginId">The plugin GUID.</param>
|
||||
/// <returns>A MiraPluginInfo.</returns>
|
||||
public static MiraPluginInfo GetPluginByGuid(string guid)
|
||||
public static MiraPluginInfo GetPluginByGuid(string pluginId)
|
||||
{
|
||||
return Instance._registeredPlugins.Values.First(plugin => plugin.PluginId == guid);
|
||||
return Instance._registeredPlugins.Values.First(plugin => plugin.PluginId == pluginId);
|
||||
}
|
||||
|
||||
private static void RegisterOptions(Type type, MiraPluginInfo pluginInfo)
|
||||
private static bool RegisterOptions(Type type, MiraPluginInfo pluginInfo)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!type.IsAssignableTo(typeof(AbstractOptionGroup)))
|
||||
{
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!ModdedOptionsManager.RegisterGroup(type, pluginInfo))
|
||||
{
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach (var property in type.GetProperties())
|
||||
@@ -107,12 +121,21 @@ public sealed class MiraPluginManager
|
||||
|
||||
ModdedOptionsManager.RegisterAttributeOption(type, attribute, property, pluginInfo);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Logger<MiraApiPlugin>.Error($"Failed to register options for {type.Name}: {e}");
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private static bool RegisterRoleAttribute(Type type, MiraPluginInfo pluginInfo, out Type? role)
|
||||
{
|
||||
role = null;
|
||||
|
||||
try
|
||||
{
|
||||
if (!(typeof(RoleBehaviour).IsAssignableFrom(type) && typeof(ICustomRole).IsAssignableFrom(type)))
|
||||
{
|
||||
return false;
|
||||
@@ -127,8 +150,16 @@ public sealed class MiraPluginManager
|
||||
role = type;
|
||||
return true;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Logger<MiraApiPlugin>.Error($"Failed to register role for {type.Name}: {e}");
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private static void RegisterColorClasses(Type type)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (type.GetCustomAttribute<RegisterCustomColorsAttribute>() == null)
|
||||
{
|
||||
@@ -157,20 +188,36 @@ public sealed class MiraPluginManager
|
||||
PaletteManager.CustomColors.Add(color);
|
||||
}
|
||||
}
|
||||
|
||||
private static void RegisterModifier(Type type)
|
||||
catch (Exception e)
|
||||
{
|
||||
if (type.IsAssignableTo(typeof(BaseModifier)))
|
||||
{
|
||||
ModifierManager.RegisterModifier(type);
|
||||
Logger<MiraApiPlugin>.Error($"Failed to register color class {type.Name}: {e}");
|
||||
}
|
||||
}
|
||||
|
||||
private static void RegisterButtonAttribute(Type type, MiraPluginInfo pluginInfo)
|
||||
private static bool RegisterModifier(Type type)
|
||||
{
|
||||
if (type.IsAssignableTo(typeof(CustomActionButton)) || type.IsAssignableTo(typeof(CustomActionButton<>)))
|
||||
try
|
||||
{
|
||||
CustomButtonManager.RegisterButton(type, pluginInfo);
|
||||
return ModifierManager.RegisterModifier(type);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Logger<MiraApiPlugin>.Error($"Failed to register modifier {type.Name}: {e}");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private static bool RegisterButtonAttribute(Type type, MiraPluginInfo pluginInfo)
|
||||
{
|
||||
try
|
||||
{
|
||||
return CustomButtonManager.RegisterButton(type, pluginInfo);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Logger<MiraApiPlugin>.Error($"Failed to register button {type.Name}: {e}");
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user