ScriptStack 1.0.5
Loading...
Searching...
No Matches
PluginLoader.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.IO;
4using System.Linq;
5using System.Reflection;
6
8{
9 public static class PluginLoader
10 {
11 public static List<Plugin> LoadPlugins(string pluginsRoot, ScriptStack.Manager manager, string[]? sharedAssemblyNames = null)
12 {
13 var plugins = new List<Plugin>();
14 if (!Directory.Exists(pluginsRoot)) return plugins;
15 var shared = sharedAssemblyNames ?? new[] { Assembly.GetExecutingAssembly().GetName().Name! };
16
17 foreach (var pluginDir in Directory.GetDirectories(pluginsRoot))
18 {
19 try
20 {
21 var dlls = Directory.GetFiles(pluginDir, "*.dll");
22 if (dlls.Length == 0) continue;
23
24 string pluginDll = dlls.FirstOrDefault(f =>
25 string.Equals(Path.GetFileNameWithoutExtension(f), Path.GetFileName(pluginDir), StringComparison.OrdinalIgnoreCase))
26 ?? dlls[0];
27
28 var loadContext = new PluginLoadContext(pluginDll, shared);
29 var assembly = loadContext.LoadFromAssemblyPath(pluginDll);
30
31 foreach (var type in assembly.GetExportedTypes())
32 {
33 if (!typeof(ScriptStack.Runtime.Model).IsAssignableFrom(type)) continue;
34 var ctor = type.GetConstructor(Type.EmptyTypes);
35 if (ctor == null) continue;
36 var obj = ctor.Invoke(Array.Empty<object>());
37 var model = (ScriptStack.Runtime.Model)obj;
38 manager.Register(model);
39
40 plugins.Add(new Plugin
41 {
42 Directory = pluginDir,
43 LoadContext = loadContext,
44 Assembly = assembly,
45 Instance = model
46 });
47 }
48 }
49 catch (Exception ex)
50 {
51 Console.WriteLine($"[PluginLoader] Fehler beim Laden von '{pluginDir}': {ex.Message}");
52 }
53 }
54
55 return plugins;
56 }
57 }
58}
API entry point.
Definition Manager.cs:21
static List< Plugin > LoadPlugins(string pluginsRoot, ScriptStack.Manager manager, string[]? sharedAssemblyNames=null)