mirror of
https://github.com/wwiinnddyy/LanMountainDesktop.git
synced 2026-06-20 23:54:26 +08:00
0.5.6
插件系统再进化
This commit is contained in:
@@ -8,6 +8,7 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Compile Remove="_build_verify_*\**\*.cs" />
|
||||
<PackageReference Include="Avalonia" Version="11.3.12" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
@@ -1,83 +0,0 @@
|
||||
using System.Reflection;
|
||||
using System.Threading;
|
||||
|
||||
namespace LanMountainDesktop.PluginSdk;
|
||||
|
||||
public sealed class LoadedPlugin : IDisposable, IAsyncDisposable
|
||||
{
|
||||
private int _disposed;
|
||||
|
||||
internal LoadedPlugin(
|
||||
PluginManifest manifest,
|
||||
string sourcePath,
|
||||
string assemblyPath,
|
||||
Assembly assembly,
|
||||
IPlugin plugin,
|
||||
IPluginContext context,
|
||||
IReadOnlyList<PluginSettingsPageRegistration> settingsPages,
|
||||
IReadOnlyList<PluginDesktopComponentRegistration> desktopComponents,
|
||||
PluginLoadContext loadContext)
|
||||
{
|
||||
Manifest = manifest;
|
||||
SourcePath = sourcePath;
|
||||
AssemblyPath = assemblyPath;
|
||||
Assembly = assembly;
|
||||
Plugin = plugin;
|
||||
Context = context;
|
||||
SettingsPages = settingsPages;
|
||||
DesktopComponents = desktopComponents;
|
||||
LoadContext = loadContext;
|
||||
}
|
||||
|
||||
public PluginManifest Manifest { get; }
|
||||
|
||||
public string SourcePath { get; }
|
||||
|
||||
public string AssemblyPath { get; }
|
||||
|
||||
public Assembly Assembly { get; }
|
||||
|
||||
public IPlugin Plugin { get; }
|
||||
|
||||
public IPluginContext Context { get; }
|
||||
|
||||
public IReadOnlyList<PluginSettingsPageRegistration> SettingsPages { get; }
|
||||
|
||||
public IReadOnlyList<PluginDesktopComponentRegistration> DesktopComponents { get; }
|
||||
|
||||
public PluginLoadContext LoadContext { get; }
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
DisposeAsync().AsTask().GetAwaiter().GetResult();
|
||||
}
|
||||
|
||||
public async ValueTask DisposeAsync()
|
||||
{
|
||||
if (Interlocked.Exchange(ref _disposed, 1) != 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (Plugin is IAsyncDisposable asyncDisposable)
|
||||
{
|
||||
await asyncDisposable.DisposeAsync();
|
||||
}
|
||||
else if (Plugin is IDisposable disposable)
|
||||
{
|
||||
disposable.Dispose();
|
||||
}
|
||||
|
||||
if (Context is IAsyncDisposable asyncContext)
|
||||
{
|
||||
await asyncContext.DisposeAsync();
|
||||
}
|
||||
else if (Context is IDisposable disposableContext)
|
||||
{
|
||||
disposableContext.Dispose();
|
||||
}
|
||||
|
||||
LoadContext.Unload();
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
}
|
||||
9
LanMountainDesktop.PluginSdk/PluginHostPropertyKeys.cs
Normal file
9
LanMountainDesktop.PluginSdk/PluginHostPropertyKeys.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
namespace LanMountainDesktop.PluginSdk;
|
||||
|
||||
public static class PluginHostPropertyKeys
|
||||
{
|
||||
public const string HostApplicationName = "HostApplicationName";
|
||||
public const string HostVersion = "HostVersion";
|
||||
public const string PluginSdkApiVersion = "PluginSdkApiVersion";
|
||||
public const string HostLanguageCode = "HostLanguageCode";
|
||||
}
|
||||
@@ -1,66 +0,0 @@
|
||||
using System.Reflection;
|
||||
using System.Runtime.Loader;
|
||||
|
||||
namespace LanMountainDesktop.PluginSdk;
|
||||
|
||||
public sealed class PluginLoadContext : AssemblyLoadContext
|
||||
{
|
||||
private readonly AssemblyDependencyResolver _resolver;
|
||||
private readonly HashSet<string> _sharedAssemblyNames;
|
||||
|
||||
public PluginLoadContext(string mainAssemblyPath, IEnumerable<string>? sharedAssemblyNames = null)
|
||||
: base($"{Path.GetFileNameWithoutExtension(mainAssemblyPath)}_{Guid.NewGuid():N}", isCollectible: true)
|
||||
{
|
||||
ArgumentException.ThrowIfNullOrWhiteSpace(mainAssemblyPath);
|
||||
|
||||
MainAssemblyPath = Path.GetFullPath(mainAssemblyPath);
|
||||
_resolver = new AssemblyDependencyResolver(MainAssemblyPath);
|
||||
_sharedAssemblyNames = new HashSet<string>(StringComparer.OrdinalIgnoreCase)
|
||||
{
|
||||
typeof(IPlugin).Assembly.GetName().Name!
|
||||
};
|
||||
|
||||
if (sharedAssemblyNames is null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (var assemblyName in sharedAssemblyNames)
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(assemblyName))
|
||||
{
|
||||
_sharedAssemblyNames.Add(assemblyName.Trim());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public string MainAssemblyPath { get; }
|
||||
|
||||
protected override Assembly? Load(AssemblyName assemblyName)
|
||||
{
|
||||
var simpleName = assemblyName.Name;
|
||||
if (string.IsNullOrWhiteSpace(simpleName))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
if (_sharedAssemblyNames.Contains(simpleName))
|
||||
{
|
||||
return Default.Assemblies.FirstOrDefault(
|
||||
assembly => string.Equals(
|
||||
assembly.GetName().Name,
|
||||
simpleName,
|
||||
StringComparison.OrdinalIgnoreCase))
|
||||
?? null;
|
||||
}
|
||||
|
||||
var assemblyPath = _resolver.ResolveAssemblyToPath(assemblyName);
|
||||
return assemblyPath is null ? null : LoadFromAssemblyPath(assemblyPath);
|
||||
}
|
||||
|
||||
protected override nint LoadUnmanagedDll(string unmanagedDllName)
|
||||
{
|
||||
var libraryPath = _resolver.ResolveUnmanagedDllToPath(unmanagedDllName);
|
||||
return libraryPath is null ? nint.Zero : LoadUnmanagedDllFromPath(libraryPath);
|
||||
}
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
namespace LanMountainDesktop.PluginSdk;
|
||||
|
||||
public sealed record PluginLoadResult(
|
||||
string SourcePath,
|
||||
PluginManifest? Manifest,
|
||||
LoadedPlugin? LoadedPlugin,
|
||||
Exception? Error)
|
||||
{
|
||||
public bool IsSuccess => LoadedPlugin is not null && Error is null;
|
||||
|
||||
public static PluginLoadResult Success(string sourcePath, PluginManifest manifest, LoadedPlugin loadedPlugin)
|
||||
{
|
||||
return new PluginLoadResult(sourcePath, manifest, loadedPlugin, null);
|
||||
}
|
||||
|
||||
public static PluginLoadResult Failure(string sourcePath, PluginManifest? manifest, Exception error)
|
||||
{
|
||||
return new PluginLoadResult(sourcePath, manifest, null, error);
|
||||
}
|
||||
}
|
||||
@@ -1,924 +0,0 @@
|
||||
using System.Collections.ObjectModel;
|
||||
using System.IO.Compression;
|
||||
using System.Reflection;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
|
||||
namespace LanMountainDesktop.PluginSdk;
|
||||
|
||||
public sealed class PluginLoader
|
||||
{
|
||||
private readonly PluginLoaderOptions _options;
|
||||
|
||||
public PluginLoader(PluginLoaderOptions? options = null)
|
||||
{
|
||||
_options = options ?? new PluginLoaderOptions();
|
||||
}
|
||||
|
||||
public IReadOnlyList<PluginLoadResult> LoadAll(
|
||||
string pluginsRootDirectory,
|
||||
IServiceProvider? services = null,
|
||||
IReadOnlyDictionary<string, object?>? properties = null)
|
||||
{
|
||||
ArgumentException.ThrowIfNullOrWhiteSpace(pluginsRootDirectory);
|
||||
|
||||
if (!Directory.Exists(pluginsRootDirectory))
|
||||
{
|
||||
return Array.Empty<PluginLoadResult>();
|
||||
}
|
||||
|
||||
var results = new List<PluginLoadResult>();
|
||||
var candidates = DiscoverCandidates(pluginsRootDirectory, results);
|
||||
var selectedPluginIds = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
foreach (var candidate in candidates)
|
||||
{
|
||||
if (!selectedPluginIds.Add(candidate.Manifest.Id))
|
||||
{
|
||||
results.Add(PluginLoadResult.Failure(
|
||||
candidate.SourcePath,
|
||||
candidate.Manifest,
|
||||
new InvalidOperationException(
|
||||
$"Duplicate plugin id '{candidate.Manifest.Id}' was found. Source '{candidate.SourcePath}' was ignored because a higher-priority source was already selected.")));
|
||||
continue;
|
||||
}
|
||||
|
||||
results.Add(candidate.SourceKind switch
|
||||
{
|
||||
PluginSourceKind.Package => LoadFromPackage(
|
||||
candidate.SourcePath,
|
||||
pluginsRootDirectory,
|
||||
candidate.Manifest,
|
||||
services,
|
||||
properties),
|
||||
_ => LoadFromManifest(
|
||||
candidate.SourcePath,
|
||||
candidate.Manifest,
|
||||
services,
|
||||
properties)
|
||||
});
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
public PluginLoadResult LoadFromManifest(
|
||||
string manifestPath,
|
||||
IServiceProvider? services = null,
|
||||
IReadOnlyDictionary<string, object?>? properties = null)
|
||||
{
|
||||
PluginManifest? manifest = null;
|
||||
|
||||
try
|
||||
{
|
||||
manifest = PluginManifest.Load(manifestPath);
|
||||
return LoadFromManifest(manifestPath, manifest, services, properties);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return PluginLoadResult.Failure(Path.GetFullPath(manifestPath), manifest, ex);
|
||||
}
|
||||
}
|
||||
|
||||
public PluginLoadResult LoadFromPackage(
|
||||
string packagePath,
|
||||
string pluginsRootDirectory,
|
||||
IServiceProvider? services = null,
|
||||
IReadOnlyDictionary<string, object?>? properties = null)
|
||||
{
|
||||
PluginManifest? manifest = null;
|
||||
|
||||
try
|
||||
{
|
||||
manifest = ReadManifestFromPackage(packagePath);
|
||||
return LoadFromPackage(packagePath, pluginsRootDirectory, manifest, services, properties);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return PluginLoadResult.Failure(Path.GetFullPath(packagePath), manifest, ex);
|
||||
}
|
||||
}
|
||||
|
||||
public PluginLoadResult LoadFromAssembly(
|
||||
string assemblyPath,
|
||||
PluginManifest manifest,
|
||||
IServiceProvider? services = null,
|
||||
IReadOnlyDictionary<string, object?>? properties = null)
|
||||
{
|
||||
ArgumentException.ThrowIfNullOrWhiteSpace(assemblyPath);
|
||||
ArgumentNullException.ThrowIfNull(manifest);
|
||||
|
||||
var fullAssemblyPath = Path.GetFullPath(assemblyPath);
|
||||
var pluginDirectory = Path.GetDirectoryName(fullAssemblyPath)
|
||||
?? throw new InvalidOperationException($"Failed to determine the plugin directory of '{fullAssemblyPath}'.");
|
||||
var dataDirectory = Path.Combine(pluginDirectory, _options.DataDirectoryName);
|
||||
return LoadCore(fullAssemblyPath, fullAssemblyPath, pluginDirectory, dataDirectory, manifest, services, properties);
|
||||
}
|
||||
|
||||
private PluginLoadResult LoadCore(
|
||||
string sourcePath,
|
||||
string assemblyPath,
|
||||
string pluginDirectory,
|
||||
string dataDirectory,
|
||||
PluginManifest manifest,
|
||||
IServiceProvider? services,
|
||||
IReadOnlyDictionary<string, object?>? properties)
|
||||
{
|
||||
PluginLoadContext? loadContext = null;
|
||||
IPlugin? plugin = null;
|
||||
PluginContext? context = null;
|
||||
|
||||
try
|
||||
{
|
||||
loadContext = new PluginLoadContext(assemblyPath, _options.SharedAssemblyNames);
|
||||
var assembly = loadContext.LoadFromAssemblyPath(assemblyPath);
|
||||
var pluginType = ResolvePluginType(assembly);
|
||||
plugin = CreatePluginInstance(pluginType);
|
||||
context = CreateContext(manifest, pluginDirectory, dataDirectory, services, properties);
|
||||
|
||||
plugin.Initialize(context);
|
||||
var settingsPages = context.GetSettingsPagesSnapshot();
|
||||
var desktopComponents = context.GetDesktopComponentsSnapshot();
|
||||
|
||||
var loadedPlugin = new LoadedPlugin(
|
||||
manifest,
|
||||
sourcePath,
|
||||
assemblyPath,
|
||||
assembly,
|
||||
plugin,
|
||||
context,
|
||||
settingsPages,
|
||||
desktopComponents,
|
||||
loadContext);
|
||||
|
||||
return PluginLoadResult.Success(sourcePath, manifest, loadedPlugin);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
DisposeInstance(plugin);
|
||||
DisposeInstance(context);
|
||||
loadContext?.Unload();
|
||||
return PluginLoadResult.Failure(sourcePath, manifest, ex);
|
||||
}
|
||||
}
|
||||
|
||||
private PluginLoadResult LoadFromManifest(
|
||||
string manifestPath,
|
||||
PluginManifest manifest,
|
||||
IServiceProvider? services,
|
||||
IReadOnlyDictionary<string, object?>? properties)
|
||||
{
|
||||
try
|
||||
{
|
||||
var fullManifestPath = Path.GetFullPath(manifestPath);
|
||||
var assemblyPath = manifest.ResolveEntranceAssemblyPath(fullManifestPath);
|
||||
if (!File.Exists(assemblyPath))
|
||||
{
|
||||
throw new FileNotFoundException(
|
||||
$"Plugin '{manifest.Id}' entrance assembly '{assemblyPath}' was not found.",
|
||||
assemblyPath);
|
||||
}
|
||||
|
||||
var pluginDirectory = Path.GetDirectoryName(assemblyPath)
|
||||
?? throw new InvalidOperationException($"Failed to determine the plugin directory of '{assemblyPath}'.");
|
||||
var dataDirectory = Path.Combine(pluginDirectory, _options.DataDirectoryName);
|
||||
return LoadCore(fullManifestPath, assemblyPath, pluginDirectory, dataDirectory, manifest, services, properties);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return PluginLoadResult.Failure(Path.GetFullPath(manifestPath), manifest, ex);
|
||||
}
|
||||
}
|
||||
|
||||
private PluginLoadResult LoadFromPackage(
|
||||
string packagePath,
|
||||
string pluginsRootDirectory,
|
||||
PluginManifest manifest,
|
||||
IServiceProvider? services,
|
||||
IReadOnlyDictionary<string, object?>? properties)
|
||||
{
|
||||
try
|
||||
{
|
||||
var fullPackagePath = Path.GetFullPath(packagePath);
|
||||
var extractionDirectory = ExtractPackage(fullPackagePath, pluginsRootDirectory);
|
||||
var extractedManifestPath = Path.Combine(extractionDirectory, _options.ManifestFileName);
|
||||
|
||||
if (!File.Exists(extractedManifestPath))
|
||||
{
|
||||
throw new FileNotFoundException(
|
||||
$"Plugin package '{fullPackagePath}' does not contain '{_options.ManifestFileName}'.",
|
||||
extractedManifestPath);
|
||||
}
|
||||
|
||||
var extractedManifest = PluginManifest.Load(extractedManifestPath);
|
||||
if (!string.Equals(extractedManifest.Id, manifest.Id, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
$"Plugin package '{fullPackagePath}' manifest id changed after extraction. Expected '{manifest.Id}', actual '{extractedManifest.Id}'.");
|
||||
}
|
||||
|
||||
var assemblyPath = extractedManifest.ResolveEntranceAssemblyPath(extractedManifestPath);
|
||||
if (!File.Exists(assemblyPath))
|
||||
{
|
||||
throw new FileNotFoundException(
|
||||
$"Plugin '{extractedManifest.Id}' entrance assembly '{assemblyPath}' was not found after package extraction.",
|
||||
assemblyPath);
|
||||
}
|
||||
|
||||
var dataDirectory = GetPackagedDataDirectory(pluginsRootDirectory, extractedManifest);
|
||||
return LoadCore(fullPackagePath, assemblyPath, extractionDirectory, dataDirectory, extractedManifest, services, properties);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return PluginLoadResult.Failure(Path.GetFullPath(packagePath), manifest, ex);
|
||||
}
|
||||
}
|
||||
|
||||
private PluginContext CreateContext(
|
||||
PluginManifest manifest,
|
||||
string pluginDirectory,
|
||||
string dataDirectory,
|
||||
IServiceProvider? services,
|
||||
IReadOnlyDictionary<string, object?>? properties)
|
||||
{
|
||||
Directory.CreateDirectory(dataDirectory);
|
||||
|
||||
return new PluginContext(
|
||||
manifest,
|
||||
pluginDirectory,
|
||||
dataDirectory,
|
||||
services ?? NullServiceProvider.Instance,
|
||||
CreateReadOnlyProperties(properties));
|
||||
}
|
||||
|
||||
private IReadOnlyList<PluginCandidate> DiscoverCandidates(
|
||||
string pluginsRootDirectory,
|
||||
List<PluginLoadResult> preparationFailures)
|
||||
{
|
||||
var candidates = new List<PluginCandidate>();
|
||||
|
||||
foreach (var packagePath in EnumerateCandidatePaths(
|
||||
pluginsRootDirectory,
|
||||
"*" + NormalizePackageExtension(_options.PackageFileExtension)))
|
||||
{
|
||||
try
|
||||
{
|
||||
var manifest = ReadManifestFromPackage(packagePath);
|
||||
candidates.Add(new PluginCandidate(Path.GetFullPath(packagePath), manifest, PluginSourceKind.Package));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
preparationFailures.Add(PluginLoadResult.Failure(Path.GetFullPath(packagePath), null, ex));
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var manifestPath in EnumerateCandidatePaths(pluginsRootDirectory, _options.ManifestFileName))
|
||||
{
|
||||
try
|
||||
{
|
||||
var manifest = PluginManifest.Load(manifestPath);
|
||||
candidates.Add(new PluginCandidate(Path.GetFullPath(manifestPath), manifest, PluginSourceKind.Manifest));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
preparationFailures.Add(PluginLoadResult.Failure(Path.GetFullPath(manifestPath), null, ex));
|
||||
}
|
||||
}
|
||||
|
||||
return candidates
|
||||
.OrderBy(candidate => candidate.SourceKind)
|
||||
.ThenBy(candidate => candidate.SourcePath, StringComparer.OrdinalIgnoreCase)
|
||||
.ToArray();
|
||||
}
|
||||
|
||||
private IEnumerable<string> EnumerateCandidatePaths(string pluginsRootDirectory, string searchPattern)
|
||||
{
|
||||
var runtimeRootDirectory = EnsureTrailingSeparator(GetRuntimeRootDirectory(pluginsRootDirectory));
|
||||
|
||||
return Directory
|
||||
.EnumerateFiles(pluginsRootDirectory, searchPattern, SearchOption.AllDirectories)
|
||||
.Select(Path.GetFullPath)
|
||||
.Where(path => !path.StartsWith(runtimeRootDirectory, StringComparison.OrdinalIgnoreCase))
|
||||
.OrderBy(path => path, StringComparer.OrdinalIgnoreCase);
|
||||
}
|
||||
|
||||
private PluginManifest ReadManifestFromPackage(string packagePath)
|
||||
{
|
||||
ArgumentException.ThrowIfNullOrWhiteSpace(packagePath);
|
||||
|
||||
var fullPackagePath = Path.GetFullPath(packagePath);
|
||||
if (!File.Exists(fullPackagePath))
|
||||
{
|
||||
throw new FileNotFoundException($"Plugin package '{fullPackagePath}' was not found.", fullPackagePath);
|
||||
}
|
||||
|
||||
using var archive = ZipFile.OpenRead(fullPackagePath);
|
||||
var manifestEntries = archive.Entries
|
||||
.Where(entry =>
|
||||
!string.IsNullOrWhiteSpace(entry.Name) &&
|
||||
string.Equals(entry.Name, _options.ManifestFileName, StringComparison.OrdinalIgnoreCase))
|
||||
.ToArray();
|
||||
|
||||
if (manifestEntries.Length == 0)
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
$"Plugin package '{fullPackagePath}' does not contain '{_options.ManifestFileName}'.");
|
||||
}
|
||||
|
||||
if (manifestEntries.Length > 1)
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
$"Plugin package '{fullPackagePath}' contains multiple '{_options.ManifestFileName}' files.");
|
||||
}
|
||||
|
||||
using var stream = manifestEntries[0].Open();
|
||||
return PluginManifest.Load(stream, $"{fullPackagePath}!/{manifestEntries[0].FullName}");
|
||||
}
|
||||
|
||||
private string ExtractPackage(string packagePath, string pluginsRootDirectory)
|
||||
{
|
||||
var extractionDirectory = GetPackageExtractionDirectory(pluginsRootDirectory, packagePath);
|
||||
RecreateDirectory(extractionDirectory);
|
||||
ZipFile.ExtractToDirectory(packagePath, extractionDirectory, overwriteFiles: true);
|
||||
return extractionDirectory;
|
||||
}
|
||||
|
||||
private string GetPackageExtractionDirectory(string pluginsRootDirectory, string packagePath)
|
||||
{
|
||||
var packageName = SanitizeDirectoryName(Path.GetFileNameWithoutExtension(packagePath));
|
||||
var packageHash = Convert.ToHexString(
|
||||
SHA256.HashData(Encoding.UTF8.GetBytes(Path.GetFullPath(packagePath))))
|
||||
.Substring(0, 12);
|
||||
|
||||
return Path.Combine(
|
||||
GetRuntimeRootDirectory(pluginsRootDirectory),
|
||||
_options.ExtractedPackagesDirectoryName,
|
||||
$"{packageName}_{packageHash}");
|
||||
}
|
||||
|
||||
private string GetPackagedDataDirectory(string pluginsRootDirectory, PluginManifest manifest)
|
||||
{
|
||||
return Path.Combine(
|
||||
GetRuntimeRootDirectory(pluginsRootDirectory),
|
||||
_options.PackagedDataDirectoryName,
|
||||
SanitizeDirectoryName(manifest.Id));
|
||||
}
|
||||
|
||||
private string GetRuntimeRootDirectory(string pluginsRootDirectory)
|
||||
{
|
||||
return Path.Combine(Path.GetFullPath(pluginsRootDirectory), _options.RuntimeDirectoryName);
|
||||
}
|
||||
|
||||
private static void RecreateDirectory(string directoryPath)
|
||||
{
|
||||
if (Directory.Exists(directoryPath))
|
||||
{
|
||||
Directory.Delete(directoryPath, recursive: true);
|
||||
}
|
||||
|
||||
Directory.CreateDirectory(directoryPath);
|
||||
}
|
||||
|
||||
private static string NormalizePackageExtension(string extension)
|
||||
{
|
||||
ArgumentException.ThrowIfNullOrWhiteSpace(extension);
|
||||
return extension.StartsWith(".", StringComparison.Ordinal) ? extension : "." + extension;
|
||||
}
|
||||
|
||||
private static string EnsureTrailingSeparator(string path)
|
||||
{
|
||||
var fullPath = Path.GetFullPath(path);
|
||||
return fullPath.EndsWith(Path.DirectorySeparatorChar.ToString(), StringComparison.Ordinal)
|
||||
? fullPath
|
||||
: fullPath + Path.DirectorySeparatorChar;
|
||||
}
|
||||
|
||||
private static string SanitizeDirectoryName(string value)
|
||||
{
|
||||
var invalidCharacters = Path.GetInvalidFileNameChars();
|
||||
var builder = new StringBuilder(value.Length);
|
||||
|
||||
foreach (var ch in value)
|
||||
{
|
||||
builder.Append(invalidCharacters.Contains(ch) ? '_' : ch);
|
||||
}
|
||||
|
||||
return string.IsNullOrWhiteSpace(builder.ToString()) ? "_plugin" : builder.ToString().Trim();
|
||||
}
|
||||
|
||||
private static ReadOnlyDictionary<string, object?> CreateReadOnlyProperties(
|
||||
IReadOnlyDictionary<string, object?>? properties)
|
||||
{
|
||||
if (properties is null || properties.Count == 0)
|
||||
{
|
||||
return new ReadOnlyDictionary<string, object?>(
|
||||
new Dictionary<string, object?>(StringComparer.OrdinalIgnoreCase));
|
||||
}
|
||||
|
||||
var map = new Dictionary<string, object?>(StringComparer.OrdinalIgnoreCase);
|
||||
foreach (var pair in properties)
|
||||
{
|
||||
map[pair.Key] = pair.Value;
|
||||
}
|
||||
|
||||
return new ReadOnlyDictionary<string, object?>(map);
|
||||
}
|
||||
|
||||
private static Type ResolvePluginType(Assembly assembly)
|
||||
{
|
||||
var candidateTypes = GetLoadableTypes(assembly)
|
||||
.Where(type =>
|
||||
typeof(IPlugin).IsAssignableFrom(type) &&
|
||||
!type.IsAbstract &&
|
||||
!type.IsInterface &&
|
||||
!type.ContainsGenericParameters)
|
||||
.ToArray();
|
||||
|
||||
if (candidateTypes.Length == 0)
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
$"Assembly '{assembly.Location}' does not contain a concrete type implementing '{nameof(IPlugin)}'.");
|
||||
}
|
||||
|
||||
var attributedTypes = candidateTypes
|
||||
.Where(type => type.IsDefined(typeof(PluginEntranceAttribute), inherit: false))
|
||||
.ToArray();
|
||||
|
||||
if (attributedTypes.Length == 1)
|
||||
{
|
||||
return attributedTypes[0];
|
||||
}
|
||||
|
||||
if (attributedTypes.Length > 1)
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
$"Assembly '{assembly.Location}' contains multiple plugin entrance types. Mark only one type with '{nameof(PluginEntranceAttribute)}'.");
|
||||
}
|
||||
|
||||
if (candidateTypes.Length == 1)
|
||||
{
|
||||
return candidateTypes[0];
|
||||
}
|
||||
|
||||
throw new InvalidOperationException(
|
||||
$"Assembly '{assembly.Location}' contains multiple '{nameof(IPlugin)}' implementations. Mark the intended entrance type with '{nameof(PluginEntranceAttribute)}'.");
|
||||
}
|
||||
|
||||
private static IPlugin CreatePluginInstance(Type pluginType)
|
||||
{
|
||||
if (pluginType.GetConstructor(Type.EmptyTypes) is null)
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
$"Plugin type '{pluginType.FullName}' must expose a public parameterless constructor.");
|
||||
}
|
||||
|
||||
if (Activator.CreateInstance(pluginType) is not IPlugin plugin)
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
$"Failed to create plugin instance of type '{pluginType.FullName}'.");
|
||||
}
|
||||
|
||||
return plugin;
|
||||
}
|
||||
|
||||
private static void DisposeInstance(object? instance)
|
||||
{
|
||||
if (instance is null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
if (instance is IAsyncDisposable asyncDisposable)
|
||||
{
|
||||
asyncDisposable.DisposeAsync().AsTask().GetAwaiter().GetResult();
|
||||
return;
|
||||
}
|
||||
|
||||
if (instance is IDisposable disposable)
|
||||
{
|
||||
disposable.Dispose();
|
||||
}
|
||||
}
|
||||
catch (Exception disposeError)
|
||||
{
|
||||
System.Diagnostics.Debug.WriteLine(
|
||||
$"[PluginLoader] Disposal of '{instance.GetType().FullName}' failed: {disposeError}");
|
||||
}
|
||||
}
|
||||
|
||||
private static Type[] GetLoadableTypes(Assembly assembly)
|
||||
{
|
||||
try
|
||||
{
|
||||
return assembly.GetTypes();
|
||||
}
|
||||
catch (ReflectionTypeLoadException ex)
|
||||
{
|
||||
var loaderMessages = ex.LoaderExceptions
|
||||
.Where(exception => exception is not null)
|
||||
.Select(exception => exception!.Message)
|
||||
.ToArray();
|
||||
|
||||
var detail = loaderMessages.Length == 0
|
||||
? "No additional loader diagnostics were provided."
|
||||
: string.Join(Environment.NewLine, loaderMessages);
|
||||
|
||||
throw new InvalidOperationException(
|
||||
$"Failed to inspect plugin assembly '{assembly.Location}'.{Environment.NewLine}{detail}",
|
||||
ex);
|
||||
}
|
||||
}
|
||||
|
||||
private sealed class PluginContext : IPluginContext, IDisposable, IAsyncDisposable
|
||||
{
|
||||
private readonly Dictionary<string, PluginSettingsPageRegistration> _settingsPages =
|
||||
new(StringComparer.OrdinalIgnoreCase);
|
||||
private readonly Dictionary<string, PluginDesktopComponentRegistration> _desktopComponents =
|
||||
new(StringComparer.OrdinalIgnoreCase);
|
||||
private readonly Dictionary<Type, object> _registeredServices = [];
|
||||
private readonly List<object> _serviceRegistrationOrder = [];
|
||||
private readonly object _serviceGate = new();
|
||||
private readonly IServiceProvider _hostServices;
|
||||
private int _disposed;
|
||||
|
||||
public PluginContext(
|
||||
PluginManifest manifest,
|
||||
string pluginDirectory,
|
||||
string dataDirectory,
|
||||
IServiceProvider services,
|
||||
IReadOnlyDictionary<string, object?> properties)
|
||||
{
|
||||
Manifest = manifest;
|
||||
PluginDirectory = pluginDirectory;
|
||||
DataDirectory = dataDirectory;
|
||||
_hostServices = services;
|
||||
Services = new PluginCompositeServiceProvider(this);
|
||||
Properties = properties;
|
||||
|
||||
RegisterBuiltInService<IPluginContext>(this);
|
||||
RegisterBuiltInService<IPluginMessageBus>(new PluginMessageBus());
|
||||
}
|
||||
|
||||
public PluginManifest Manifest { get; }
|
||||
|
||||
public string PluginDirectory { get; }
|
||||
|
||||
public string DataDirectory { get; }
|
||||
|
||||
public IServiceProvider Services { get; }
|
||||
|
||||
public IReadOnlyDictionary<string, object?> Properties { get; }
|
||||
|
||||
public T? GetService<T>()
|
||||
{
|
||||
return (T?)Services.GetService(typeof(T));
|
||||
}
|
||||
|
||||
public bool TryGetProperty<T>(string key, out T? value)
|
||||
{
|
||||
ArgumentException.ThrowIfNullOrWhiteSpace(key);
|
||||
|
||||
if (Properties.TryGetValue(key, out var rawValue) && rawValue is T typedValue)
|
||||
{
|
||||
value = typedValue;
|
||||
return true;
|
||||
}
|
||||
|
||||
value = default;
|
||||
return false;
|
||||
}
|
||||
|
||||
public void RegisterService<TService>(TService service)
|
||||
where TService : class
|
||||
{
|
||||
RegisterServiceCore(typeof(TService), service, allowOverride: false);
|
||||
}
|
||||
|
||||
public void RegisterSettingsPage(PluginSettingsPageRegistration registration)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(registration);
|
||||
ThrowIfDisposed();
|
||||
|
||||
if (!_settingsPages.TryAdd(registration.Id, registration))
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
$"Plugin '{Manifest.Id}' already registered a settings page with id '{registration.Id}'.");
|
||||
}
|
||||
}
|
||||
|
||||
public void RegisterDesktopComponent(PluginDesktopComponentRegistration registration)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(registration);
|
||||
ThrowIfDisposed();
|
||||
|
||||
if (!_desktopComponents.TryAdd(registration.ComponentId, registration))
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
$"Plugin '{Manifest.Id}' already registered a desktop component with id '{registration.ComponentId}'.");
|
||||
}
|
||||
}
|
||||
|
||||
public IReadOnlyList<PluginSettingsPageRegistration> GetSettingsPagesSnapshot()
|
||||
{
|
||||
ThrowIfDisposed();
|
||||
return _settingsPages.Values
|
||||
.OrderBy(page => page.SortOrder)
|
||||
.ThenBy(page => page.Title, StringComparer.OrdinalIgnoreCase)
|
||||
.ToArray();
|
||||
}
|
||||
|
||||
public IReadOnlyList<PluginDesktopComponentRegistration> GetDesktopComponentsSnapshot()
|
||||
{
|
||||
ThrowIfDisposed();
|
||||
return _desktopComponents.Values
|
||||
.OrderBy(component => component.Category, StringComparer.OrdinalIgnoreCase)
|
||||
.ThenBy(component => component.DisplayName, StringComparer.OrdinalIgnoreCase)
|
||||
.ToArray();
|
||||
}
|
||||
|
||||
internal object? ResolveService(Type serviceType)
|
||||
{
|
||||
if (Volatile.Read(ref _disposed) != 0)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
if (serviceType == typeof(IServiceProvider))
|
||||
{
|
||||
return Services;
|
||||
}
|
||||
|
||||
lock (_serviceGate)
|
||||
{
|
||||
if (_registeredServices.TryGetValue(serviceType, out var service))
|
||||
{
|
||||
return service;
|
||||
}
|
||||
|
||||
foreach (var registeredService in _registeredServices.Values)
|
||||
{
|
||||
if (serviceType.IsInstanceOfType(registeredService))
|
||||
{
|
||||
return registeredService;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return _hostServices.GetService(serviceType);
|
||||
}
|
||||
|
||||
private void RegisterBuiltInService<TService>(TService service)
|
||||
where TService : class
|
||||
{
|
||||
RegisterServiceCore(typeof(TService), service, allowOverride: true);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
DisposeAsync().AsTask().GetAwaiter().GetResult();
|
||||
}
|
||||
|
||||
public async ValueTask DisposeAsync()
|
||||
{
|
||||
if (Interlocked.Exchange(ref _disposed, 1) != 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
object[] services;
|
||||
lock (_serviceGate)
|
||||
{
|
||||
services = _serviceRegistrationOrder.ToArray();
|
||||
_registeredServices.Clear();
|
||||
_serviceRegistrationOrder.Clear();
|
||||
}
|
||||
|
||||
_settingsPages.Clear();
|
||||
_desktopComponents.Clear();
|
||||
|
||||
var disposedServices = new HashSet<object>(ReferenceEqualityComparer.Instance);
|
||||
for (var i = services.Length - 1; i >= 0; i--)
|
||||
{
|
||||
var service = services[i];
|
||||
if (ReferenceEquals(service, this) || !disposedServices.Add(service))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (service is IAsyncDisposable asyncDisposable)
|
||||
{
|
||||
await asyncDisposable.DisposeAsync();
|
||||
}
|
||||
else if (service is IDisposable disposable)
|
||||
{
|
||||
disposable.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void RegisterServiceCore(Type serviceType, object service, bool allowOverride)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(serviceType);
|
||||
ArgumentNullException.ThrowIfNull(service);
|
||||
ThrowIfDisposed();
|
||||
|
||||
if (!serviceType.IsInstanceOfType(service))
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
$"Service instance '{service.GetType().FullName}' is not assignable to '{serviceType.FullName}'.");
|
||||
}
|
||||
|
||||
lock (_serviceGate)
|
||||
{
|
||||
if (!allowOverride && _registeredServices.ContainsKey(serviceType))
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
$"Plugin '{Manifest.Id}' already registered a service for '{serviceType.FullName}'.");
|
||||
}
|
||||
|
||||
_registeredServices[serviceType] = service;
|
||||
_serviceRegistrationOrder.Add(service);
|
||||
}
|
||||
}
|
||||
|
||||
private void ThrowIfDisposed()
|
||||
{
|
||||
if (Volatile.Read(ref _disposed) != 0)
|
||||
{
|
||||
throw new ObjectDisposedException(nameof(PluginContext));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private sealed class PluginCompositeServiceProvider : IServiceProvider
|
||||
{
|
||||
private readonly PluginContext _context;
|
||||
|
||||
public PluginCompositeServiceProvider(PluginContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public object? GetService(Type serviceType)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(serviceType);
|
||||
return _context.ResolveService(serviceType);
|
||||
}
|
||||
}
|
||||
|
||||
private sealed class PluginMessageBus : IPluginMessageBus, IDisposable
|
||||
{
|
||||
private readonly Dictionary<Type, List<Subscription>> _subscriptions = [];
|
||||
private readonly object _gate = new();
|
||||
private int _disposed;
|
||||
|
||||
public IDisposable Subscribe<TMessage>(Action<TMessage> handler)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(handler);
|
||||
if (Volatile.Read(ref _disposed) != 0)
|
||||
{
|
||||
throw new ObjectDisposedException(nameof(PluginMessageBus));
|
||||
}
|
||||
|
||||
var subscription = new Subscription(this, typeof(TMessage), message => handler((TMessage)message!));
|
||||
lock (_gate)
|
||||
{
|
||||
if (!_subscriptions.TryGetValue(subscription.MessageType, out var handlers))
|
||||
{
|
||||
handlers = [];
|
||||
_subscriptions[subscription.MessageType] = handlers;
|
||||
}
|
||||
|
||||
handlers.Add(subscription);
|
||||
}
|
||||
|
||||
return subscription;
|
||||
}
|
||||
|
||||
public void Publish<TMessage>(TMessage message)
|
||||
{
|
||||
if (Volatile.Read(ref _disposed) != 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Subscription[] handlers;
|
||||
lock (_gate)
|
||||
{
|
||||
if (!_subscriptions.TryGetValue(typeof(TMessage), out var subscriptions) || subscriptions.Count == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
handlers = subscriptions.ToArray();
|
||||
}
|
||||
|
||||
foreach (var handler in handlers)
|
||||
{
|
||||
try
|
||||
{
|
||||
handler.Invoke(message);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
System.Diagnostics.Debug.WriteLine(
|
||||
$"[PluginMessageBus] Handler for '{typeof(TMessage).FullName}' failed: {ex}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (Interlocked.Exchange(ref _disposed, 1) != 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
lock (_gate)
|
||||
{
|
||||
_subscriptions.Clear();
|
||||
}
|
||||
}
|
||||
|
||||
private void Unsubscribe(Subscription subscription)
|
||||
{
|
||||
lock (_gate)
|
||||
{
|
||||
if (!_subscriptions.TryGetValue(subscription.MessageType, out var handlers))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
handlers.Remove(subscription);
|
||||
if (handlers.Count == 0)
|
||||
{
|
||||
_subscriptions.Remove(subscription.MessageType);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private sealed class Subscription : IDisposable
|
||||
{
|
||||
private readonly PluginMessageBus _owner;
|
||||
private int _disposed;
|
||||
|
||||
public Subscription(PluginMessageBus owner, Type messageType, Action<object?> handler)
|
||||
{
|
||||
_owner = owner;
|
||||
MessageType = messageType;
|
||||
Handler = handler;
|
||||
}
|
||||
|
||||
public Type MessageType { get; }
|
||||
|
||||
public Action<object?> Handler { get; }
|
||||
|
||||
public void Invoke(object? message)
|
||||
{
|
||||
if (_disposed != 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Handler(message);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (Interlocked.Exchange(ref _disposed, 1) != 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_owner.Unsubscribe(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private sealed class NullServiceProvider : IServiceProvider
|
||||
{
|
||||
public static NullServiceProvider Instance { get; } = new();
|
||||
|
||||
private NullServiceProvider()
|
||||
{
|
||||
}
|
||||
|
||||
public object? GetService(Type serviceType)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private enum PluginSourceKind
|
||||
{
|
||||
Package = 0,
|
||||
Manifest = 1
|
||||
}
|
||||
|
||||
private sealed record PluginCandidate(
|
||||
string SourcePath,
|
||||
PluginManifest Manifest,
|
||||
PluginSourceKind SourceKind);
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
namespace LanMountainDesktop.PluginSdk;
|
||||
|
||||
public sealed class PluginLoaderOptions
|
||||
{
|
||||
public string ManifestFileName { get; init; } = PluginSdkInfo.ManifestFileName;
|
||||
|
||||
public string PackageFileExtension { get; init; } = PluginSdkInfo.PackageFileExtension;
|
||||
|
||||
public string DataDirectoryName { get; init; } = PluginSdkInfo.DataDirectoryName;
|
||||
|
||||
public string RuntimeDirectoryName { get; init; } = PluginSdkInfo.RuntimeDirectoryName;
|
||||
|
||||
public string ExtractedPackagesDirectoryName { get; init; } = PluginSdkInfo.ExtractedPackagesDirectoryName;
|
||||
|
||||
public string PackagedDataDirectoryName { get; init; } = PluginSdkInfo.PackagedDataDirectoryName;
|
||||
|
||||
public ISet<string> SharedAssemblyNames { get; } = new HashSet<string>(StringComparer.OrdinalIgnoreCase)
|
||||
{
|
||||
typeof(IPlugin).Assembly.GetName().Name!
|
||||
};
|
||||
}
|
||||
114
LanMountainDesktop.PluginSdk/PluginLocalizer.cs
Normal file
114
LanMountainDesktop.PluginSdk/PluginLocalizer.cs
Normal file
@@ -0,0 +1,114 @@
|
||||
using System.Globalization;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace LanMountainDesktop.PluginSdk;
|
||||
|
||||
public sealed class PluginLocalizer
|
||||
{
|
||||
private static readonly JsonSerializerOptions JsonOptions = new()
|
||||
{
|
||||
ReadCommentHandling = JsonCommentHandling.Skip,
|
||||
AllowTrailingCommas = true
|
||||
};
|
||||
|
||||
private readonly Dictionary<string, Dictionary<string, string>> _cache =
|
||||
new(StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
public PluginLocalizer(string pluginDirectory, string? languageCode)
|
||||
{
|
||||
ArgumentException.ThrowIfNullOrWhiteSpace(pluginDirectory);
|
||||
|
||||
PluginDirectory = pluginDirectory;
|
||||
LanguageCode = NormalizeLanguageCode(languageCode);
|
||||
}
|
||||
|
||||
public string PluginDirectory { get; }
|
||||
|
||||
public string LanguageCode { get; }
|
||||
|
||||
public static PluginLocalizer Create(IPluginContext context)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(context);
|
||||
return new PluginLocalizer(context.PluginDirectory, ResolveLanguageCode(context.Properties));
|
||||
}
|
||||
|
||||
public static PluginLocalizer Create(PluginDesktopComponentContext context)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(context);
|
||||
return new PluginLocalizer(context.PluginDirectory, ResolveLanguageCode(context.Properties));
|
||||
}
|
||||
|
||||
public string GetString(string key, string fallback)
|
||||
{
|
||||
ArgumentException.ThrowIfNullOrWhiteSpace(key);
|
||||
|
||||
var primaryTable = LoadLanguageTable(LanguageCode);
|
||||
if (primaryTable.TryGetValue(key, out var value) && !string.IsNullOrWhiteSpace(value))
|
||||
{
|
||||
return value;
|
||||
}
|
||||
|
||||
if (!string.Equals(LanguageCode, "en-US", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
var fallbackTable = LoadLanguageTable("en-US");
|
||||
if (fallbackTable.TryGetValue(key, out value) && !string.IsNullOrWhiteSpace(value))
|
||||
{
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
return fallback;
|
||||
}
|
||||
|
||||
public string Format(string key, string fallback, params object[] args)
|
||||
{
|
||||
return string.Format(CultureInfo.CurrentCulture, GetString(key, fallback), args);
|
||||
}
|
||||
|
||||
public static string NormalizeLanguageCode(string? languageCode)
|
||||
{
|
||||
return string.Equals(languageCode, "en-US", StringComparison.OrdinalIgnoreCase)
|
||||
? "en-US"
|
||||
: "zh-CN";
|
||||
}
|
||||
|
||||
public static string ResolveLanguageCode(IReadOnlyDictionary<string, object?> properties)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(properties);
|
||||
|
||||
return properties.TryGetValue(PluginHostPropertyKeys.HostLanguageCode, out var rawValue) &&
|
||||
rawValue is string languageCode
|
||||
? NormalizeLanguageCode(languageCode)
|
||||
: NormalizeLanguageCode(CultureInfo.CurrentUICulture.Name);
|
||||
}
|
||||
|
||||
private Dictionary<string, string> LoadLanguageTable(string languageCode)
|
||||
{
|
||||
if (_cache.TryGetValue(languageCode, out var table))
|
||||
{
|
||||
return table;
|
||||
}
|
||||
|
||||
var result = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
|
||||
try
|
||||
{
|
||||
var filePath = Path.Combine(PluginDirectory, "Localization", $"{languageCode}.json");
|
||||
if (File.Exists(filePath))
|
||||
{
|
||||
var json = File.ReadAllText(filePath).TrimStart('\uFEFF');
|
||||
var data = JsonSerializer.Deserialize<Dictionary<string, string>>(json, JsonOptions);
|
||||
if (data is not null)
|
||||
{
|
||||
result = new Dictionary<string, string>(data, StringComparer.OrdinalIgnoreCase);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
// Keep empty localization table for plugin resilience.
|
||||
}
|
||||
|
||||
_cache[languageCode] = result;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user