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
+115
-73
No files matched your search
@@ -31,7 +31,6 @@ public static class ModdedOptionsManager
|
|||||||
{
|
{
|
||||||
if (Activator.CreateInstance(type) is not AbstractOptionGroup group)
|
if (Activator.CreateInstance(type) is not AbstractOptionGroup group)
|
||||||
{
|
{
|
||||||
Logger<MiraApiPlugin>.Error($"Skipping option group {type.Name}. Does not inherit AbstractOptionGroup.");
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -16,18 +16,11 @@ public static class CustomButtonManager
|
|||||||
internal static readonly Dictionary<Type, Type> ButtonEventTypes = [];
|
internal static readonly Dictionary<Type, Type> ButtonEventTypes = [];
|
||||||
internal static readonly Dictionary<Type, Type> ButtonCancelledEventTypes = [];
|
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 false;
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (Activator.CreateInstance(buttonType) is not CustomActionButton button)
|
|
||||||
{
|
|
||||||
Logger<MiraApiPlugin>.Error($"Failed to create button from {buttonType.Name}");
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
CustomButtons.Add(button);
|
CustomButtons.Add(button);
|
||||||
@@ -38,5 +31,7 @@ public static class CustomButtonManager
|
|||||||
|
|
||||||
ButtonEventTypes.Add(buttonType, typeof(MiraButtonClickEvent<>).MakeGenericType(buttonType));
|
ButtonEventTypes.Add(buttonType, typeof(MiraButtonClickEvent<>).MakeGenericType(buttonType));
|
||||||
ButtonCancelledEventTypes.Add(buttonType, typeof(MiraButtonCancelledEvent<>).MakeGenericType(buttonType));
|
ButtonCancelledEventTypes.Add(buttonType, typeof(MiraButtonCancelledEvent<>).MakeGenericType(buttonType));
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -49,11 +49,11 @@ public static class ModifierManager
|
|||||||
return TypeToIdModifierMap.GetValueOrDefault(type);
|
return TypeToIdModifierMap.GetValueOrDefault(type);
|
||||||
}
|
}
|
||||||
|
|
||||||
internal static void RegisterModifier(Type modifierType)
|
internal static bool RegisterModifier(Type modifierType)
|
||||||
{
|
{
|
||||||
if (!typeof(BaseModifier).IsAssignableFrom(modifierType))
|
if (!typeof(BaseModifier).IsAssignableFrom(modifierType))
|
||||||
{
|
{
|
||||||
return;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
IdToTypeModifierMap.Add(GetNextId(), modifierType);
|
IdToTypeModifierMap.Add(GetNextId(), modifierType);
|
||||||
@@ -61,7 +61,7 @@ public static class ModifierManager
|
|||||||
|
|
||||||
if (!typeof(GameModifier).IsAssignableFrom(modifierType))
|
if (!typeof(GameModifier).IsAssignableFrom(modifierType))
|
||||||
{
|
{
|
||||||
return;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
var mod = Activator.CreateInstance(modifierType) as GameModifier;
|
var mod = Activator.CreateInstance(modifierType) as GameModifier;
|
||||||
@@ -73,6 +73,7 @@ public static class ModifierManager
|
|||||||
}
|
}
|
||||||
|
|
||||||
list.Add(_nextId);
|
list.Add(_nextId);
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
internal static void AssignModifiers(List<PlayerControl> plrs)
|
internal static void AssignModifiers(List<PlayerControl> plrs)
|
||||||
|
|||||||
@@ -47,15 +47,27 @@ public sealed class MiraPluginManager
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
RegisterModifier(type);
|
if (RegisterModifier(type))
|
||||||
RegisterOptions(type, info);
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (RegisterOptions(type, info))
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
if (RegisterRoleAttribute(type, info, out var role))
|
if (RegisterRoleAttribute(type, info, out var role))
|
||||||
{
|
{
|
||||||
roles.Add(role!);
|
roles.Add(role!);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (RegisterButtonAttribute(type, info))
|
||||||
|
{
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
RegisterButtonAttribute(type, info);
|
|
||||||
RegisterColorClasses(type);
|
RegisterColorClasses(type);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -72,105 +84,140 @@ public sealed class MiraPluginManager
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Get a mira plugin by its GUID.
|
/// Get a mira plugin by its GUID.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="guid">The plugin GUID.</param>
|
/// <param name="pluginId">The plugin GUID.</param>
|
||||||
/// <returns>A MiraPluginInfo.</returns>
|
/// <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)
|
||||||
{
|
{
|
||||||
if (!type.IsAssignableTo(typeof(AbstractOptionGroup)))
|
try
|
||||||
{
|
{
|
||||||
return;
|
if (!type.IsAssignableTo(typeof(AbstractOptionGroup)))
|
||||||
}
|
|
||||||
|
|
||||||
if (!ModdedOptionsManager.RegisterGroup(type, pluginInfo))
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (var property in type.GetProperties())
|
|
||||||
{
|
|
||||||
if (property.PropertyType.IsAssignableTo(typeof(IModdedOption)))
|
|
||||||
{
|
{
|
||||||
ModdedOptionsManager.RegisterPropertyOption(type, property, pluginInfo);
|
return false;
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var attribute = property.GetCustomAttribute<ModdedOptionAttribute>();
|
if (!ModdedOptionsManager.RegisterGroup(type, pluginInfo))
|
||||||
if (attribute == null)
|
|
||||||
{
|
{
|
||||||
continue;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
ModdedOptionsManager.RegisterAttributeOption(type, attribute, property, pluginInfo);
|
foreach (var property in type.GetProperties())
|
||||||
|
{
|
||||||
|
if (property.PropertyType.IsAssignableTo(typeof(IModdedOption)))
|
||||||
|
{
|
||||||
|
ModdedOptionsManager.RegisterPropertyOption(type, property, pluginInfo);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
var attribute = property.GetCustomAttribute<ModdedOptionAttribute>();
|
||||||
|
if (attribute == null)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
private static bool RegisterRoleAttribute(Type type, MiraPluginInfo pluginInfo, out Type? role)
|
||||||
{
|
{
|
||||||
role = null;
|
role = null;
|
||||||
|
try
|
||||||
if (!(typeof(RoleBehaviour).IsAssignableFrom(type) && typeof(ICustomRole).IsAssignableFrom(type)))
|
|
||||||
{
|
{
|
||||||
return false;
|
if (!(typeof(RoleBehaviour).IsAssignableFrom(type) && typeof(ICustomRole).IsAssignableFrom(type)))
|
||||||
}
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
if (!ModList.GetById(pluginInfo.PluginId).IsRequiredOnAllClients)
|
if (!ModList.GetById(pluginInfo.PluginId).IsRequiredOnAllClients)
|
||||||
|
{
|
||||||
|
Logger<MiraApiPlugin>.Error("Custom roles are only supported on all clients.");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
role = type;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
Logger<MiraApiPlugin>.Error("Custom roles are only supported on all clients.");
|
Logger<MiraApiPlugin>.Error($"Failed to register role for {type.Name}: {e}");
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
return false;
|
||||||
role = type;
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void RegisterColorClasses(Type type)
|
private static void RegisterColorClasses(Type type)
|
||||||
{
|
{
|
||||||
if (type.GetCustomAttribute<RegisterCustomColorsAttribute>() == null)
|
try
|
||||||
{
|
{
|
||||||
return;
|
if (type.GetCustomAttribute<RegisterCustomColorsAttribute>() == null)
|
||||||
}
|
|
||||||
|
|
||||||
if (!type.IsStatic())
|
|
||||||
{
|
|
||||||
Logger<MiraApiPlugin>.Error($"Color class {type.Name} must be static.");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (var property in type.GetProperties())
|
|
||||||
{
|
|
||||||
if (property.PropertyType != typeof(CustomColor))
|
|
||||||
{
|
{
|
||||||
continue;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (property.GetValue(null) is not CustomColor color)
|
if (!type.IsStatic())
|
||||||
{
|
{
|
||||||
Logger<MiraApiPlugin>.Error($"Color property {property.Name} in {type.Name} is not a CustomColor.");
|
Logger<MiraApiPlugin>.Error($"Color class {type.Name} must be static.");
|
||||||
continue;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
PaletteManager.CustomColors.Add(color);
|
foreach (var property in type.GetProperties())
|
||||||
|
{
|
||||||
|
if (property.PropertyType != typeof(CustomColor))
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (property.GetValue(null) is not CustomColor color)
|
||||||
|
{
|
||||||
|
Logger<MiraApiPlugin>.Error($"Color property {property.Name} in {type.Name} is not a CustomColor.");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
PaletteManager.CustomColors.Add(color);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
Logger<MiraApiPlugin>.Error($"Failed to register color class {type.Name}: {e}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void RegisterModifier(Type type)
|
private static bool RegisterModifier(Type type)
|
||||||
{
|
{
|
||||||
if (type.IsAssignableTo(typeof(BaseModifier)))
|
try
|
||||||
{
|
{
|
||||||
ModifierManager.RegisterModifier(type);
|
return ModifierManager.RegisterModifier(type);
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
Logger<MiraApiPlugin>.Error($"Failed to register modifier {type.Name}: {e}");
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void RegisterButtonAttribute(Type type, MiraPluginInfo pluginInfo)
|
private static bool RegisterButtonAttribute(Type type, MiraPluginInfo pluginInfo)
|
||||||
{
|
{
|
||||||
if (type.IsAssignableTo(typeof(CustomActionButton)) || type.IsAssignableTo(typeof(CustomActionButton<>)))
|
try
|
||||||
{
|
{
|
||||||
CustomButtonManager.RegisterButton(type, pluginInfo);
|
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