= Updated plugins to each have their own global context

This commit is contained in:
Matthew Cech
2019-04-20 23:02:43 -07:00
parent 30ed670218
commit 25d8d51b1d
6 changed files with 71 additions and 71 deletions
+35 -3
View File
@@ -6,29 +6,61 @@ import java.nio.file.Files;
import java.nio.file.Path;
import java.util.stream.Stream;
//To promote flexibility, plugins are lua file with predefined callbacks.
//They are executed between the initial built-in preprocessing and the command parsing.
import org.luaj.vm2.Globals;
import org.luaj.vm2.LuaValue;
import org.luaj.vm2.lib.jse.JsePlatform;
// To promote flexibility, plugins are lua file with predefined callbacks.
// They are executed between the initial built-in preprocessing and the command parsing.
// This class wraps the lua layer - provides all the processing and function calls to the lua binding.
public class Plugin
{
public Path filepath;
public String contents;
private Globals globals;
private LuaValue func_plugin;
public Plugin(Path filepath)
{
this.filepath = filepath;
StringBuilder contentBuilder = new StringBuilder();
StringBuilder contentBuilder = new StringBuilder();
try
{
Stream<String> stream = Files.lines(filepath, StandardCharsets.UTF_8);
stream.forEach(str -> contentBuilder.append(str).append("\n"));
stream.close();
contents = contentBuilder.toString();
globals = JsePlatform.standardGlobals();
globals.load(contents).call();
func_plugin = globals.get("plugin");
String output = null;
}
catch (IOException e)
{
PluginLog.Error(e.getMessage());
}
}
public String Run(String args)
{
try
{
LuaValue res = func_plugin.call(LuaValue.valueOf(args));
if(!res.isnil())
return res.toString();
}
catch(Exception e)
{
PluginLog.Error("Failed to run plugin from file " + filepath);
}
return null;
}
}