111 lines
3.8 KiB
C#
111 lines
3.8 KiB
C#
using System;
|
||
using System.IO;
|
||
using System.Reflection;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Text.Json;
|
||
using VisionAsist.SDK;
|
||
|
||
namespace VisionAsist.Models;
|
||
|
||
public class Core
|
||
{
|
||
public class LoadedModule
|
||
{
|
||
public string Name { get; set; } = string.Empty;
|
||
public IModule Module { get; set; } = null!;
|
||
public List<ToolDefinition> Tools { get; set; } = new();
|
||
}
|
||
|
||
public static List<LoadedModule> ModuleList = new();
|
||
static readonly string PluginPath = Path.Combine(AppContext.BaseDirectory, "Modules");
|
||
|
||
static Core()
|
||
{
|
||
Console.OutputEncoding = System.Text.Encoding.UTF8;
|
||
Console.InputEncoding = System.Text.Encoding.UTF8;
|
||
|
||
if (!Directory.Exists(PluginPath)) Directory.CreateDirectory(PluginPath);
|
||
|
||
AppDomain.CurrentDomain.AssemblyResolve += ResolveAssembly;
|
||
LoadModules();
|
||
}
|
||
|
||
private static Assembly? ResolveAssembly(object? sender, ResolveEventArgs args)
|
||
{
|
||
string assemblyName = new AssemblyName(args.Name).Name + ".dll";
|
||
foreach (var dir in Directory.GetDirectories(PluginPath))
|
||
{
|
||
string assemblyPath = Path.Combine(dir, assemblyName);
|
||
if (File.Exists(assemblyPath)) return Assembly.LoadFrom(assemblyPath);
|
||
}
|
||
return null;
|
||
}
|
||
|
||
private static void LoadModules()
|
||
{
|
||
if (!Directory.Exists(PluginPath)) return;
|
||
|
||
foreach (var dir in Directory.GetDirectories(PluginPath))
|
||
{
|
||
string folderName = Path.GetFileName(dir);
|
||
// DLL называется так же, как и папка
|
||
string dllPath = Path.Combine(dir, folderName + ".dll");
|
||
|
||
if (File.Exists(dllPath))
|
||
{
|
||
try
|
||
{
|
||
Assembly assembly = Assembly.LoadFrom(dllPath);
|
||
var type = assembly.GetTypes().FirstOrDefault(t =>
|
||
typeof(IModule).IsAssignableFrom(t) && !t.IsInterface && !t.IsAbstract);
|
||
|
||
if (type != null)
|
||
{
|
||
var module = (IModule)Activator.CreateInstance(type)!;
|
||
if (!ModuleList.Any(m => m.Name == module.Name))
|
||
{
|
||
ModuleList.Add(new LoadedModule
|
||
{
|
||
Name = module.Name,
|
||
Module = module,
|
||
Tools = module.GetTools()
|
||
});
|
||
Console.WriteLine($"[CORE]: Загружен модуль '{module.Name}' из {dllPath}");
|
||
}
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
Console.WriteLine($"[CORE ERROR]: Не удалось загрузить {dllPath}: {ex.Message}");
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
public static string GetToolsManifestJson()
|
||
{
|
||
var allTools = ModuleList.SelectMany(m => m.Tools.Select(t => new
|
||
{
|
||
type = "function",
|
||
function = new
|
||
{
|
||
name = t.Name,
|
||
description = t.Description,
|
||
parameters = JsonDocument.Parse(t.ParametersSchema).RootElement
|
||
}
|
||
})).ToList();
|
||
|
||
return JsonSerializer.Serialize(allTools, new JsonSerializerOptions { WriteIndented = true });
|
||
}
|
||
|
||
public static string CallTool(string toolName, string argsJson)
|
||
{
|
||
foreach (var module in ModuleList)
|
||
{
|
||
var tool = module.Tools.FirstOrDefault(t => t.Name == toolName);
|
||
if (tool != null) return module.Module.Execute(toolName, argsJson);
|
||
}
|
||
return "Ошибка: Инструмент не найден";
|
||
}
|
||
} |