Skip to content

Commit 36ca4f4

Browse files
author
LoneWandererProductions
committed
Prepare to remove my old Plugin Loader
1 parent 00c708d commit 36ca4f4

3 files changed

Lines changed: 67 additions & 5 deletions

File tree

CoreLibrary.sln

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CommonExtendedObjectsTests"
7777
EndProject
7878
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Projektmappenelemente", "Projektmappenelemente", "{84D27F74-D2BA-6C25-2661-968F101900D9}"
7979
ProjectSection(SolutionItems) = preProject
80-
.gitattributes = .gitattributes
81-
.gitignore = .gitignore
8280
.github\workflows\dotnet.yml = .github\workflows\dotnet.yml
83-
License.txt = License.txt
84-
README.md = README.md
85-
Todo.txt = Todo.txt
8681
EndProjectSection
8782
EndProject
8883
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CoreViewer", "CoreViewer\CoreViewer.csproj", "{B17F7012-EB24-4A3A-9A97-9779E5116D66}"
@@ -97,6 +92,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CommonControls.Images", "Co
9792
EndProject
9893
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CommonControls.Converter", "CommonControls.Converter\CommonControls.Converter.csproj", "{A2C8D860-41F8-40B8-B939-BCA253AC2729}"
9994
EndProject
95+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Loader", "..\PluginArchitecture\Loader\Loader.csproj", "{9C0DCDDA-04F4-1201-B0C6-AF41EE961062}"
96+
EndProject
10097
Global
10198
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10299
Debug|Any CPU = Debug|Any CPU
@@ -255,6 +252,10 @@ Global
255252
{A2C8D860-41F8-40B8-B939-BCA253AC2729}.Debug|Any CPU.Build.0 = Debug|Any CPU
256253
{A2C8D860-41F8-40B8-B939-BCA253AC2729}.Release|Any CPU.ActiveCfg = Release|Any CPU
257254
{A2C8D860-41F8-40B8-B939-BCA253AC2729}.Release|Any CPU.Build.0 = Release|Any CPU
255+
{9C0DCDDA-04F4-1201-B0C6-AF41EE961062}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
256+
{9C0DCDDA-04F4-1201-B0C6-AF41EE961062}.Debug|Any CPU.Build.0 = Debug|Any CPU
257+
{9C0DCDDA-04F4-1201-B0C6-AF41EE961062}.Release|Any CPU.ActiveCfg = Release|Any CPU
258+
{9C0DCDDA-04F4-1201-B0C6-AF41EE961062}.Release|Any CPU.Build.0 = Release|Any CPU
258259
EndGlobalSection
259260
GlobalSection(SolutionProperties) = preSolution
260261
HideSolutionNode = FALSE

Loader/Loader.csproj

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net9.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
</PropertyGroup>
8+
9+
</Project>

Loader/PluginLoading.cs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* COPYRIGHT: See COPYING in the top level directory
3+
* PROJECT: Loader
4+
* FILE: PluginLoading.cs
5+
* PURPOSE: Generic plugin loader that loads implementations of a given contract.
6+
* PROGRAMMER: Peter Geinitz (Wayfarer)
7+
*/
8+
9+
using System.Reflection;
10+
11+
namespace Loader
12+
{
13+
/// <summary>
14+
/// Generic plugin loader that loads implementations of a given contract.
15+
/// </summary>
16+
public sealed class PluginLoading
17+
{
18+
/// <summary>
19+
/// Loads all implementations of <typeparamref name="TContract"/> from the specified directory.
20+
/// </summary>
21+
/// <typeparam name="TContract">The contract interface or base class.</typeparam>
22+
/// <param name="directory">Directory containing plugin assemblies.</param>
23+
/// <returns>Loaded plugin instances.</returns>
24+
public IReadOnlyList<TContract> Load<TContract>(string directory)
25+
where TContract : class
26+
{
27+
if (directory == null)
28+
throw new ArgumentNullException(nameof(directory));
29+
30+
var result = new List<TContract>();
31+
32+
foreach (var file in Directory.EnumerateFiles(directory, "*.dll"))
33+
{
34+
var asm = Assembly.LoadFrom(file);
35+
36+
foreach (var type in asm.GetTypes())
37+
{
38+
if (type.IsAbstract || type.IsInterface)
39+
continue;
40+
41+
if (!typeof(TContract).IsAssignableFrom(type))
42+
continue;
43+
44+
if (Activator.CreateInstance(type) is TContract instance)
45+
result.Add(instance);
46+
}
47+
}
48+
49+
return result;
50+
}
51+
}
52+
}

0 commit comments

Comments
 (0)