Refactored the Lua module loading & standard libraries from the plugin base to the factory

This commit is contained in:
Thorsten Sommer 2025-03-23 14:08:15 +01:00
parent f885db8f1b
commit ff17b6749d
Signed by: tsommer
GPG Key ID: 371BBA77A02C0108
2 changed files with 12 additions and 14 deletions

View File

@ -69,20 +69,6 @@ public abstract class PluginBase : IPluginMetadata
this.state = state; this.state = state;
this.Type = type; this.Type = type;
// For security reasons, we don't want to allow the plugin to load modules:
this.state.ModuleLoader = new NoModuleLoader();
// Add some useful libraries:
this.state.OpenModuleLibrary();
this.state.OpenStringLibrary();
this.state.OpenTableLibrary();
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>(); var issues = new List<string>();
if(!string.IsNullOrWhiteSpace(parseError)) if(!string.IsNullOrWhiteSpace(parseError))
issues.Add(parseError); issues.Add(parseError);

View File

@ -3,6 +3,7 @@ using System.Text;
using AIStudio.Settings; using AIStudio.Settings;
using Lua; using Lua;
using Lua.Standard;
namespace AIStudio.Tools.PluginSystem; namespace AIStudio.Tools.PluginSystem;
@ -95,6 +96,17 @@ public static partial class PluginFactory
var state = LuaState.Create(); var state = LuaState.Create();
// Add the module loader so that the plugin can load other Lua modules:
state.ModuleLoader = new PluginLoader(pluginPath);
// Add some useful libraries:
state.OpenModuleLibrary();
state.OpenStringLibrary();
state.OpenTableLibrary();
state.OpenMathLibrary();
state.OpenBitwiseLibrary();
state.OpenCoroutineLibrary();
try try
{ {
await state.DoStringAsync(code, cancellationToken: cancellationToken); await state.DoStringAsync(code, cancellationToken: cancellationToken);