Files
WCS/Plugins/Driver/Cowain.Driver/DriverServiceExtensions.cs
2026-03-02 09:08:20 +08:00

123 lines
4.0 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using Cowain.Base.Abstractions.Plugin;
using Microsoft.Extensions.DependencyInjection;
using Plugin.Cowain.Driver.Abstractions;
using Plugin.Cowain.Driver.Attributes;
using Plugin.Cowain.Driver.Models;
using System.Reflection;
namespace Plugin.Cowain.Driver;
public static class DriverServiceExtensions
{
public static List<Type>? DriverTypes { get; private set; }
/// <summary>
/// 注册插件服务
/// </summary>
/// <param name="services"></param>
/// <returns></returns>
public static IServiceCollection AddDrivers(this IServiceCollection services, List<Assembly>? assemblies)
{
if (assemblies == null)
{
return services;
}
var driverTypes = assemblies.SelectMany(a => a.GetTypes())
.Where(t => typeof(DriverBase).IsAssignableFrom(t) && t.IsClass && !t.IsAbstract);
if (DriverTypes == null)
{
DriverTypes = driverTypes.ToList();
}
// 注册时只注册类型不注册IDriver
foreach (var driver in driverTypes)
{
services.AddTransient(driver); // 只注册类型
}
return services;
}
/// <summary>
/// 注册变量动作
/// </summary>
/// <param name="services"></param>
/// <returns></returns>
public static IServiceCollection AddVariables(this IServiceCollection services, List<Assembly>? assemblies)
{
if (assemblies == null)
{
return services;
}
var driverTypes = assemblies.SelectMany(a => a.GetTypes())
.Where(t => typeof(IVariableAction).IsAssignableFrom(t) && t.IsClass && !t.IsAbstract);
Dictionary<string, Type> drivers = new Dictionary<string, Type>();
foreach (var driver in driverTypes)
{
//检查是否已经注册了同名的动作
var attribute = driver.GetCustomAttributes(typeof(ActionAttribute), false).FirstOrDefault() as ActionAttribute;
if (attribute != null)
{
var driverInfo = new ActionInfo
{
Name = attribute.Name,
Desc = attribute.Desc,
};
if (!drivers.ContainsKey(driverInfo.Name))
{
services.AddTransient(typeof(IVariableAction), driver);
}
else
{
throw new Exception($"Variable action with name '{driverInfo.Name}' is already registered.");
}
}
}
return services;
}
/// <summary>
/// 注册动作条件
/// </summary>
/// <param name="services"></param>
/// <returns></returns>
public static IServiceCollection AddActionConditions(this IServiceCollection services, List<Assembly>? assemblies)
{
if (assemblies == null)
{
return services;
}
var conditionTypes = assemblies.SelectMany(a => a.GetTypes())
.Where(t => typeof(IActionCondition).IsAssignableFrom(t) && t.IsClass && !t.IsAbstract);
Dictionary<string, Type> conditions = new Dictionary<string, Type>();
foreach (var cond in conditionTypes)
{
//检查是否已经注册了同名的条件
var attribute = cond.GetCustomAttributes(typeof(ConditionAttribute), false).FirstOrDefault() as ConditionAttribute;
if (attribute != null)
{
var driverInfo = new ConditionInfo
{
Name = attribute.Name,
Desc = attribute.Desc,
};
if (!conditions.ContainsKey(driverInfo.Name))
{
services.AddTransient(typeof(IActionCondition), cond);
}
else
{
throw new Exception($"condition with name '{driverInfo.Name}' is already registered.");
}
}
}
return services;
}
}