optimize plugin loading

This commit is contained in:
XtraCube committed 2025-02-02 20:57:10 -05:00
1 parent a65de5a938
commit dcf557ca46
1 file changed
+82 -87
+82 -87
View File
@@ -38,17 +38,26 @@ public sealed class MiraPluginManager
}
var info = new MiraPluginInfo(miraPlugin, pluginInfo);
var roles = new List<Type>();
RegisterModifierAttribute(assembly);
RegisterAllOptions(assembly, info);
foreach (var type in assembly.GetTypes())
{
RegisterModifierAttribute(type);
RegisterOptions(type, info);
RegisterRoleAttribute(assembly, info);
RegisterButtonAttribute(assembly, info);
if (RegisterRoleAttribute(type, info, out var role))
{
roles.Add(role!);
}
RegisterColorClasses(assembly);
RegisterButtonAttribute(type, info);
RegisterColorClasses(type);
}
info.OptionGroups.Sort((x, y) => x.GroupPriority.CompareTo(y.GroupPriority));
CustomRoleManager.RegisterRoleTypes(roles, info);
_registeredPlugins.Add(assembly, info);
Logger<MiraApiPlugin>.Info($"Registering mod {pluginInfo.Metadata.GUID} with Mira API.");
};
IL2CPPChainloader.Instance.Finished += PaletteManager.RegisterAllColors;
@@ -65,121 +74,107 @@ public sealed class MiraPluginManager
return Instance._registeredPlugins.Values.First(plugin => plugin.PluginId == guid);
}
private static void RegisterAllOptions(Assembly assembly, MiraPluginInfo pluginInfo)
private static void RegisterOptions(Type type, MiraPluginInfo pluginInfo)
{
var filteredTypes = assembly.GetTypes().Where(type => type.IsAssignableTo(typeof(AbstractOptionGroup)));
foreach (var type in filteredTypes)
if (!type.IsAssignableTo(typeof(AbstractOptionGroup)))
{
if (!ModdedOptionsManager.RegisterGroup(type, pluginInfo))
return;
}
if (!ModdedOptionsManager.RegisterGroup(type, pluginInfo))
{
return;
}
foreach (var property in type.GetProperties())
{
if (property.PropertyType.IsAssignableTo(typeof(IModdedOption)))
{
ModdedOptionsManager.RegisterPropertyOption(type, property, pluginInfo);
continue;
}
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);
}
}
pluginInfo.OptionGroups.Sort((x, y) => x.GroupPriority.CompareTo(y.GroupPriority));
}
private static void RegisterRoleAttribute(Assembly assembly, MiraPluginInfo pluginInfo)
{
List<Type> roles = [];
foreach (var type in assembly.GetTypes())
{
var attribute = type.GetCustomAttribute<RegisterCustomRoleAttribute>();
var attribute = property.GetCustomAttribute<ModdedOptionAttribute>();
if (attribute == null)
{
continue;
}
if (!(typeof(RoleBehaviour).IsAssignableFrom(type) && typeof(ICustomRole).IsAssignableFrom(type)))
{
Logger<MiraApiPlugin>.Error($"{type.Name} does not inherit from RoleBehaviour or ICustomRole.");
continue;
}
if (!ModList.GetById(pluginInfo.PluginId).IsRequiredOnAllClients)
{
Logger<MiraApiPlugin>.Error("Custom roles are only supported on all clients.");
return;
}
roles.Add(type);
ModdedOptionsManager.RegisterAttributeOption(type, attribute, property, pluginInfo);
}
CustomRoleManager.RegisterRoleTypes(roles, pluginInfo);
}
private static void RegisterColorClasses(Assembly assembly)
private static bool RegisterRoleAttribute(Type type, MiraPluginInfo pluginInfo, out Type? role)
{
foreach (var type in assembly.GetTypes())
role = null;
var attribute = type.GetCustomAttribute<RegisterCustomRoleAttribute>();
if (attribute == null)
{
if (type.GetCustomAttribute<RegisterCustomColorsAttribute>() == null)
return false;
}
if (!(typeof(RoleBehaviour).IsAssignableFrom(type) && typeof(ICustomRole).IsAssignableFrom(type)))
{
Logger<MiraApiPlugin>.Error($"{type.Name} does not inherit from RoleBehaviour or ICustomRole.");
return false;
}
if (!ModList.GetById(pluginInfo.PluginId).IsRequiredOnAllClients)
{
Logger<MiraApiPlugin>.Error("Custom roles are only supported on all clients.");
return false;
}
role = type;
return true;
}
private static void RegisterColorClasses(Type type)
{
if (type.GetCustomAttribute<RegisterCustomColorsAttribute>() == null)
{
return;
}
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;
}
if (!type.IsStatic())
if (property.GetValue(null) is not CustomColor color)
{
Logger<MiraApiPlugin>.Error($"Color class {type.Name} must be static.");
Logger<MiraApiPlugin>.Error($"Color property {property.Name} in {type.Name} is not a CustomColor.");
continue;
}
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);
}
PaletteManager.CustomColors.Add(color);
}
}
private static void RegisterModifierAttribute(Assembly assembly)
private static void RegisterModifierAttribute(Type type)
{
foreach (var type in assembly.GetTypes())
var attribute = type.GetCustomAttribute<RegisterModifierAttribute>();
if (attribute != null)
{
var attribute = type.GetCustomAttribute<RegisterModifierAttribute>();
if (attribute != null)
{
ModifierManager.RegisterModifier(type);
}
ModifierManager.RegisterModifier(type);
}
}
private static void RegisterButtonAttribute(Assembly assembly, MiraPluginInfo pluginInfo)
private static void RegisterButtonAttribute(Type type, MiraPluginInfo pluginInfo)
{
foreach (var type in assembly.GetTypes())
var attribute = type.GetCustomAttribute<RegisterButtonAttribute>();
if (attribute != null)
{
var attribute = type.GetCustomAttribute<RegisterButtonAttribute>();
if (attribute != null)
{
CustomButtonManager.RegisterButton(type, pluginInfo);
}
CustomButtonManager.RegisterButton(type, pluginInfo);
}
}
}