This commit is contained in:
@@ -2,56 +2,110 @@ using System;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
using System.Collections.Generic;
|
||||
using VisionAsist.SDK;
|
||||
using System.Linq;
|
||||
using System.Text.Json;
|
||||
using VisionAsist.SDK;
|
||||
|
||||
namespace VisionAsist.Models;
|
||||
|
||||
public class Core
|
||||
{
|
||||
public class Modules
|
||||
public class LoadedModule
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public IModule Module { get; set; }
|
||||
public string[] commands { get; set; }
|
||||
public string Name { get; set; } = string.Empty;
|
||||
public IModule Module { get; set; } = null!;
|
||||
public List<ToolDefinition> Tools { get; set; } = new();
|
||||
}
|
||||
|
||||
public static List<Modules> modulelist = new();
|
||||
static string Plugin = Path.Combine(AppContext.BaseDirectory, "Modules");
|
||||
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(Plugin))
|
||||
{
|
||||
Directory.CreateDirectory(Plugin);
|
||||
}
|
||||
if (!Directory.Exists(PluginPath)) Directory.CreateDirectory(PluginPath);
|
||||
|
||||
string[] folderNames = new DirectoryInfo(Plugin)
|
||||
.GetDirectories()
|
||||
.Select(d => d.Name)
|
||||
.ToArray();
|
||||
|
||||
foreach (string folderName in folderNames)
|
||||
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 mpn = Path.Combine(Plugin, folderName, "Module.dll");
|
||||
if (File.Exists(mpn))
|
||||
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))
|
||||
{
|
||||
Assembly assembly = Assembly.LoadFrom(mpn);
|
||||
var type = assembly.GetTypes().FirstOrDefault(t =>
|
||||
typeof(IModule).IsAssignableFrom(t) && !t.IsInterface && !t.IsAbstract);
|
||||
if (type != null)
|
||||
try
|
||||
{
|
||||
var module = (IModule)Activator.CreateInstance(type)!;
|
||||
modulelist.Add(new Modules { Name = module.Name, Module = module, commands = module.GetCommands() });
|
||||
foreach (var cmd in module.GetCommands())
|
||||
Assembly assembly = Assembly.LoadFrom(dllPath);
|
||||
var type = assembly.GetTypes().FirstOrDefault(t =>
|
||||
typeof(IModule).IsAssignableFrom(t) && !t.IsInterface && !t.IsAbstract);
|
||||
|
||||
if (type != null)
|
||||
{
|
||||
Console.WriteLine($"- {cmd}");
|
||||
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 "Ошибка: Инструмент не найден";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user