Enhance plugin loading by making the plugin path optional

This commit is contained in:
Thorsten Sommer 2025-04-25 15:26:25 +02:00
parent 85c93cea43
commit 6df07dd56b
Signed by: tsommer
GPG Key ID: 371BBA77A02C0108

View File

@ -101,15 +101,17 @@ public static partial class PluginFactory
} }
} }
private static async Task<PluginBase> Load(string pluginPath, string code, CancellationToken cancellationToken = default) public static async Task<PluginBase> Load(string? pluginPath, string code, CancellationToken cancellationToken = default)
{ {
if(ForbiddenPlugins.Check(code) is { IsForbidden: true } forbiddenState) if(ForbiddenPlugins.Check(code) is { IsForbidden: true } forbiddenState)
return new NoPlugin($"This plugin is forbidden: {forbiddenState.Message}"); return new NoPlugin($"This plugin is forbidden: {forbiddenState.Message}");
var state = LuaState.Create(); var state = LuaState.Create();
if (!string.IsNullOrWhiteSpace(pluginPath))
// Add the module loader so that the plugin can load other Lua modules: {
state.ModuleLoader = new PluginLoader(pluginPath); // Add the module loader so that the plugin can load other Lua modules:
state.ModuleLoader = new PluginLoader(pluginPath);
}
// Add some useful libraries: // Add some useful libraries:
state.OpenModuleLibrary(); state.OpenModuleLibrary();
@ -141,7 +143,7 @@ public static partial class PluginFactory
if(type is PluginType.NONE) if(type is PluginType.NONE)
return new NoPlugin($"TYPE is not a valid plugin type. Valid types are: {CommonTools.GetAllEnumValues<PluginType>()}"); return new NoPlugin($"TYPE is not a valid plugin type. Valid types are: {CommonTools.GetAllEnumValues<PluginType>()}");
var isInternal = pluginPath.StartsWith(INTERNAL_PLUGINS_ROOT, StringComparison.OrdinalIgnoreCase); var isInternal = !string.IsNullOrWhiteSpace(pluginPath) && pluginPath.StartsWith(INTERNAL_PLUGINS_ROOT, StringComparison.OrdinalIgnoreCase);
return type switch return type switch
{ {
PluginType.LANGUAGE => new PluginLanguage(isInternal, state, type), PluginType.LANGUAGE => new PluginLanguage(isInternal, state, type),