voile/Voile.Patchouli/Reflection/VoilePluginManager.cs
2026-01-21 21:38:48 +01:00

83 lines
2.1 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Voile.Common.Reflection;
using Voile.Patchouli.Data;
namespace Voile.Patchouli.Reflection
{
public class VoilePluginManager
{
private VoilePluginManager() { }
public static VoilePluginManager _instance;
private static Type _dataStorageFactoryType = typeof(IVoileDataStorageFactory);
private static Type _objectStorageFactoryType = typeof(IVoileObjectStorageFactory);
public static VoilePluginManager GetInstance()
{
if (_instance == null)
{
_instance = new VoilePluginManager();
}
return _instance;
}
private List<Assembly> _loadedAssemblies;
private List<VoilePluginInfo> _knownDataStorages;
private List<VoilePluginInfo> _knownObjectStorages;
public bool ScanAssembly(Assembly assembly)
{
if (_loadedAssemblies == null)
_loadedAssemblies = new List<Assembly>();
if (_loadedAssemblies.Contains(assembly))
return false;
bool result = false;
Type[] types = assembly.GetTypes();
foreach (Type t in types)
{
VoilePluginAttribute? voilePluginAttribute = t.GetCustomAttribute<VoilePluginAttribute>();
if (voilePluginAttribute == null)
continue;
VoilePluginIdAttribute? voilePluginIdAttribute = t.GetCustomAttribute<VoilePluginIdAttribute>();
if (voilePluginIdAttribute == null)
continue;
VoilePluginInfo child = new VoilePluginInfo(t);
child.Id = voilePluginIdAttribute.Id;
if (t.IsAssignableTo(_dataStorageFactoryType))
{
if (_knownDataStorages == null)
_knownDataStorages = new List<VoilePluginInfo>();
child.Subsystem = VoileSubsystemType.DataStorage;
_knownDataStorages.Add(child);
result = true;
continue;
}
else if (t.IsAssignableTo(_objectStorageFactoryType))
{
if (_knownObjectStorages == null)
_knownObjectStorages = new List<VoilePluginInfo>();
child.Subsystem = VoileSubsystemType.ObjectStorage;
_knownObjectStorages.Add(child);
result = true;
continue;
}
else
{
}
}
return result;
}
}
}