mirror of
https://github.com/MindWorkAI/AI-Studio.git
synced 2025-08-21 12:32:56 +00:00
Added a loader which allows Lua scripts to load other Lua scripts
This commit is contained in:
parent
c679e17784
commit
51f34392ed
@ -7,4 +7,4 @@ namespace AIStudio.Tools.PluginSystem;
|
||||
/// </summary>
|
||||
/// <param name="state">The Lua state that the plugin was loaded into.</param>
|
||||
/// <param name="parsingError">The error message that occurred while parsing the plugin.</param>
|
||||
public sealed class NoPlugin(LuaState state, string parsingError) : PluginBase(state, PluginType.NONE, parsingError);
|
||||
public sealed class NoPlugin(LuaState state, string parsingError) : PluginBase(string.Empty, state, PluginType.NONE, parsingError);
|
@ -78,7 +78,7 @@ public abstract class PluginBase
|
||||
/// </remarks>
|
||||
public bool IsValid => this is not NoPlugin && this.baseIssues.Count == 0 && this.pluginIssues.Count == 0;
|
||||
|
||||
protected PluginBase(LuaState state, PluginType type, string parseError = "")
|
||||
protected PluginBase(string path, LuaState state, PluginType type, string parseError = "")
|
||||
{
|
||||
this.state = state;
|
||||
this.Type = type;
|
||||
@ -93,6 +93,9 @@ public abstract class PluginBase
|
||||
this.state.OpenMathLibrary();
|
||||
this.state.OpenBitwiseLibrary();
|
||||
this.state.OpenCoroutineLibrary();
|
||||
|
||||
// Add the module loader so that the plugin can load other Lua modules:
|
||||
this.state.ModuleLoader = new PluginLoader(path);
|
||||
|
||||
var issues = new List<string>();
|
||||
if(!string.IsNullOrWhiteSpace(parseError))
|
||||
|
@ -90,7 +90,7 @@ public static class PluginFactory
|
||||
|
||||
}
|
||||
|
||||
public static async Task<PluginBase> Load(string code, CancellationToken cancellationToken = default)
|
||||
public static async Task<PluginBase> Load(string path, string code, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var state = LuaState.Create();
|
||||
|
||||
@ -114,7 +114,7 @@ public static class PluginFactory
|
||||
|
||||
return type switch
|
||||
{
|
||||
PluginType.LANGUAGE => new PluginLanguage(state, type),
|
||||
PluginType.LANGUAGE => new PluginLanguage(path, state, type),
|
||||
|
||||
_ => new NoPlugin(state, "This plugin type is not supported yet. Please try again with a future version of AI Studio.")
|
||||
};
|
||||
|
48
app/MindWork AI Studio/Tools/PluginSystem/PluginLoader.cs
Normal file
48
app/MindWork AI Studio/Tools/PluginSystem/PluginLoader.cs
Normal file
@ -0,0 +1,48 @@
|
||||
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>
|
||||
public sealed class PluginLoader(string pluginDirectory) : ILuaModuleLoader
|
||||
{
|
||||
private static readonly string PLUGIN_BASE_PATH = Path.Join(SettingsManager.DataDirectory, "plugins");
|
||||
|
||||
#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 plugin base path:
|
||||
if (!pluginDirectory.StartsWith(PLUGIN_BASE_PATH, 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
|
||||
}
|
Loading…
Reference in New Issue
Block a user