Addresses code quality review of 9161868:
- PluginDiscoveryResult.Error is now Exception? rather than string?,
preserving stack traces across the plugin boundary for logging
- PluginDiscovery.Scan orders subdirectories by ordinal string comparison
so plugin load order is reproducible across platforms
38 lines
1.2 KiB
C#
38 lines
1.2 KiB
C#
namespace AcDream.Core.Plugins;
|
|
|
|
public sealed record PluginDiscoveryResult(
|
|
string PluginDirectory,
|
|
PluginManifest? Manifest,
|
|
Exception? Error)
|
|
{
|
|
public bool Success => Manifest is not null && Error is null;
|
|
}
|
|
|
|
public static class PluginDiscovery
|
|
{
|
|
public static IReadOnlyList<PluginDiscoveryResult> Scan(string pluginsRootDirectory)
|
|
{
|
|
if (!Directory.Exists(pluginsRootDirectory))
|
|
return Array.Empty<PluginDiscoveryResult>();
|
|
|
|
var results = new List<PluginDiscoveryResult>();
|
|
foreach (var subdir in Directory.EnumerateDirectories(pluginsRootDirectory).OrderBy(p => p, StringComparer.Ordinal))
|
|
{
|
|
var manifestPath = Path.Combine(subdir, "plugin.json");
|
|
if (!File.Exists(manifestPath))
|
|
continue;
|
|
|
|
try
|
|
{
|
|
var json = File.ReadAllText(manifestPath);
|
|
var manifest = PluginManifest.Parse(json);
|
|
results.Add(new PluginDiscoveryResult(subdir, manifest, null));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
results.Add(new PluginDiscoveryResult(subdir, null, ex));
|
|
}
|
|
}
|
|
return results;
|
|
}
|
|
}
|