AI-Studio/app/MindWork AI Studio/Tools/PluginSystem/PluginLoader.cs
nilskruthoff 8a9e6aeb87
Some checks are pending
Build and Release / Determine run mode (push) Waiting to run
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-aarch64-apple-darwin, osx-arm64, macos-latest, aarch64-apple-darwin, dmg,app,updater, dmg) (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-aarch64-unknown-linux-gnu, linux-arm64, ubuntu-22.04-arm, aarch64-unknown-linux-gnu, appimage,updater, appimage) (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-x86_64-apple-darwin, osx-x64, macos-latest, x86_64-apple-darwin, dmg,app,updater, dmg) (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-x86_64-pc-windows-msvc.exe, win-x64, windows-latest, x86_64-pc-windows-msvc, nsis,updater, nsis) (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-x86_64-unknown-linux-gnu, linux-x64, ubuntu-22.04, x86_64-unknown-linux-gnu, appimage,updater, appimage) (push) Blocked by required conditions
Build and Release / Prepare & create release (push) Blocked by required conditions
Build and Release / Publish release (push) Blocked by required conditions
Added export and import of plugin archives (#885)
Co-authored-by: Thorsten Sommer <SommerEngineering@users.noreply.github.com>
2026-08-06 10:42:20 +02:00

55 lines
2.1 KiB
C#

using System.Text;
using AIStudio.Settings;
using Lua;
namespace AIStudio.Tools.PluginSystem;
/// <summary>
/// Loads Lua modules from a plugin directory.
/// </summary>
/// <remarks>
/// Any plugin can load Lua modules from its own directory. This class is used to load these modules.
/// Loading other modules outside the plugin directory is not allowed.
/// </remarks>
/// <param name="pluginDirectory">The directory where the plugin is located.</param>
/// <param name="allowedBaseDirectory">
/// The directory the plugin directory must be nested in. Without it, the installed plugins directory
/// is used. Validating a plugin before its installation needs this, because the plugin is not
/// installed yet and lives in a staging directory outside the installed plugins directory.
/// </param>
public sealed class PluginLoader(string pluginDirectory, string? allowedBaseDirectory = null) : ILuaModuleLoader
{
private static readonly string PLUGIN_BASE_PATH = Path.Join(SettingsManager.DataDirectory, "plugins");
private readonly string baseDirectory = string.IsNullOrWhiteSpace(allowedBaseDirectory) ? PLUGIN_BASE_PATH : allowedBaseDirectory;
#region Implementation of ILuaModuleLoader
/// <inheritdoc />
public bool Exists(string moduleName)
{
// Ensure that the user doesn't try to escape the plugin directory:
if (moduleName.Contains("..") || pluginDirectory.Contains(".."))
return false;
// Ensure that the plugin directory is nested in the allowed base directory:
if (!pluginDirectory.StartsWith(this.baseDirectory, StringComparison.OrdinalIgnoreCase))
return false;
var path = Path.Join(pluginDirectory, $"{moduleName}.lua");
return File.Exists(path);
}
/// <inheritdoc />
public async ValueTask<LuaModule> LoadAsync(string moduleName, CancellationToken cancellationToken = default)
{
var path = Path.Join(pluginDirectory, $"{moduleName}.lua");
var code = await File.ReadAllTextAsync(path, Encoding.UTF8, cancellationToken);
return new(moduleName, code);
}
#endregion
}