From 0dd13c5a8bbb0d12f4ea140897e5ca50c58a6ef4 Mon Sep 17 00:00:00 2001 From: Egor Date: Thu, 19 Mar 2026 13:58:29 +0200 Subject: [PATCH] Work Modules --- ModuleWeather/Module.cs | 4 +- ModuleWeather/ModuleWeather.csproj | 30 +++++++++----- VisionAsist/Models/Core.cs | 32 ++++++++++++++- VisionAsist/ViewModels/SettingsViewModel.cs | 45 +++++++-------------- 4 files changed, 67 insertions(+), 44 deletions(-) diff --git a/ModuleWeather/Module.cs b/ModuleWeather/Module.cs index 775153c..00c6533 100644 --- a/ModuleWeather/Module.cs +++ b/ModuleWeather/Module.cs @@ -1,8 +1,8 @@ using Avalonia.Controls; - +using VisionAsist.SDK; namespace ModuleWeather; -public class WeatherModule +public class WeatherModule : IModule { public string Name => "Прогноз Погоды"; diff --git a/ModuleWeather/ModuleWeather.csproj b/ModuleWeather/ModuleWeather.csproj index 3a4f7bf..d404b85 100644 --- a/ModuleWeather/ModuleWeather.csproj +++ b/ModuleWeather/ModuleWeather.csproj @@ -4,21 +4,31 @@ net10.0 enable enable + + Module - + + + + + + + + + + + + + - - MainWindow.axaml - + + - - - - - - + \ No newline at end of file diff --git a/VisionAsist/Models/Core.cs b/VisionAsist/Models/Core.cs index cbde75a..abc5c35 100644 --- a/VisionAsist/Models/Core.cs +++ b/VisionAsist/Models/Core.cs @@ -1,19 +1,47 @@ using System; -using Avalonia.Controls; +using System.IO; +using System.Reflection; using VisionAsist.SDK; -using System.Threading.Tasks; +using System.Collections.Generic; +using VisionAsist.SDK; +using System.Linq; namespace VisionAsist.Models; + public class Core { + public static Dictionary _loadedModules = new(); public static TrigerCore triger = new(); public static string TextAsist; + static string Plugin = Path.Combine(AppContext.BaseDirectory, "Modules"); static Core() { Console.OutputEncoding = System.Text.Encoding.UTF8; Console.InputEncoding = System.Text.Encoding.UTF8; + string[] folderNames = new DirectoryInfo(Plugin) + .GetDirectories() + .Select(d => d.Name) + .ToArray(); + foreach (string folderName in folderNames) + { + string mpn = Path.Combine(Plugin, folderName, "Module.dll"); + if (File.Exists(mpn)) + { + Assembly assembly = Assembly.LoadFrom(mpn); + var type = assembly.GetTypes().FirstOrDefault(t => + typeof(IModule).IsAssignableFrom(t) && !t.IsInterface && !t.IsAbstract); + if (type != null) + { + var module = (IModule)Activator.CreateInstance(type)!; + Core._loadedModules.Add(module.Name, module); + + } + + + } + } } public static void StartListing() diff --git a/VisionAsist/ViewModels/SettingsViewModel.cs b/VisionAsist/ViewModels/SettingsViewModel.cs index 967822d..cf18439 100644 --- a/VisionAsist/ViewModels/SettingsViewModel.cs +++ b/VisionAsist/ViewModels/SettingsViewModel.cs @@ -5,10 +5,10 @@ using CommunityToolkit.Mvvm.Input; using System.Collections.ObjectModel; using VisionAsist.Models; using VisionAsist.SDK; - -namespace VisionAsist.ViewModels; using System.IO; using System.Linq; +namespace VisionAsist.ViewModels; + public class ModuleItem { public string Name { get; set; } @@ -17,18 +17,18 @@ public class ModuleItem public class SettingsViewModel : ViewModelBase { - string Plugin = Path.Combine(AppContext.BaseDirectory, "Modules"); + public ObservableCollection Modules { get; } = new(); public SettingsViewModel() { - string[] folderNames = new DirectoryInfo(Plugin) - .GetDirectories() - .Select(d => d.Name) - .ToArray(); - foreach (string folderName in folderNames) + + foreach (string Name in Core._loadedModules.Keys) { - AddModule(folderName); + + + AddModule(Name); + } } @@ -43,29 +43,14 @@ public class SettingsViewModel : ViewModelBase private void OpenSettings(string moduleName) { - string dllPath = Path.Combine(AppContext.BaseDirectory, "Modules", moduleName, "Module.dll"); - if (File.Exists(dllPath)) + + var module = Core._loadedModules.Values.FirstOrDefault(m => m.Name == moduleName); + if (module != null) { - Console.OutputEncoding = System.Text.Encoding.UTF8; - Console.InputEncoding = System.Text.Encoding.UTF8; - Assembly assembly = Assembly.LoadFrom(dllPath); - var type = assembly.GetTypes().FirstOrDefault(t => - typeof(IModule).IsAssignableFrom(t) && !t.IsInterface && !t.IsAbstract); - if (type != null) - { - // 4. Создаем экземпляр класса (WeatherModule) - var module = (IModule)Activator.CreateInstance(type)!; - Console.WriteLine($"Успешно загружен модуль: {module.Name}"); - - // 5. Запускаем команду и передаем текущее окно (this) в качестве родителя - var result = module.Execute("ShowWeather", new object[] { this }); - Console.WriteLine($"Ответ модуля: {result}"); - } - else - { - Console.WriteLine("В DLL не найден класс, реализующий VisionAsist.SDK.IModule!"); - } + module.Execute("ShowWeather", new object[] { this }); } + + } } \ No newline at end of file