+ Added plugin
This commit is contained in:
@@ -1,8 +1,34 @@
|
||||
package core.lua;
|
||||
|
||||
// To promote flexibility, plugins are lua file with predefined callbacks.
|
||||
// They are executed between the initial built-in preprocessing and the command parsing.
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
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.
|
||||
public class Plugin
|
||||
{
|
||||
public Path filepath;
|
||||
public String contents;
|
||||
|
||||
public Plugin(Path filepath)
|
||||
{
|
||||
this.filepath = filepath;
|
||||
|
||||
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();
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
PluginLog.Error(e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
package core.lua;
|
||||
|
||||
import org.luaj.vm2.Globals;
|
||||
import org.luaj.vm2.LuaValue;
|
||||
import org.luaj.vm2.lib.jse.JsePlatform;
|
||||
|
||||
// This class wraps the lua layer - provides all the processing and function
|
||||
// calls to the lua binding.
|
||||
public class PluginLoader
|
||||
{
|
||||
private Globals globals;
|
||||
|
||||
public PluginLoader()
|
||||
{
|
||||
globals = JsePlatform.standardGlobals();
|
||||
PluginLog.Log("Created new PluginLoader lua environment");
|
||||
}
|
||||
|
||||
public String Process(Plugin toProcess, String args)
|
||||
{
|
||||
globals.load(toProcess.contents).call();
|
||||
LuaValue pluginFunc = globals.get("plugin");
|
||||
|
||||
String output = null;
|
||||
|
||||
try
|
||||
{
|
||||
LuaValue res = pluginFunc.call(LuaValue.valueOf(args));
|
||||
|
||||
if(res.isnil())
|
||||
output = null;
|
||||
else
|
||||
output = res.toString();
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
output = null;
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package core.lua;
|
||||
|
||||
import utils.GlobalLog;
|
||||
import utils.LogFilter;
|
||||
|
||||
public class PluginLog
|
||||
{
|
||||
public static void Log(String s) { GlobalLog.Log(LogFilter.Plugin, s); }
|
||||
public static void Warn(String s) { GlobalLog.Warn(LogFilter.Plugin, s); }
|
||||
public static void Error(String s) { GlobalLog.Error(LogFilter.Plugin, s); }
|
||||
}
|
||||
@@ -1,8 +1,64 @@
|
||||
package core.lua;
|
||||
|
||||
// Reads in, handles, and manipulates plugins. Plugins are
|
||||
// loaded in the order they appear in the folder.
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.ArrayList;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
//Reads in, handles, and manipulates plugins. Plugins are
|
||||
//loaded in the order they appear in the folder.
|
||||
public class PluginManager
|
||||
{
|
||||
public PluginLoader pluginLoader;
|
||||
public final String pluginFolder;
|
||||
public ArrayList<Plugin> plugins;
|
||||
|
||||
public void AddPlugin(Path path)
|
||||
{
|
||||
plugins.add(new Plugin(path));
|
||||
}
|
||||
|
||||
public PluginManager(String folder)
|
||||
{
|
||||
this.pluginFolder = folder;
|
||||
plugins = new ArrayList<Plugin>();
|
||||
this.pluginLoader = new PluginLoader();
|
||||
|
||||
try
|
||||
{
|
||||
try (Stream<Path> paths = Files.walk(Paths.get(this.pluginFolder)))
|
||||
{
|
||||
paths.filter(Files::isRegularFile).forEach((path)->{ AddPlugin(path); });
|
||||
}
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
PluginLog.Error(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public String CallAll(String input)
|
||||
{
|
||||
for(int i = 0; i < plugins.size(); ++i)
|
||||
{
|
||||
Plugin script = plugins.get(i);
|
||||
String out = pluginLoader.Process(script, input);
|
||||
|
||||
if(out != null)
|
||||
{
|
||||
PluginLog.Log("Executed plugin at " + script.filepath);
|
||||
return out;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public void PrintAll()
|
||||
{
|
||||
for(int i = 0; i < plugins.size(); ++i)
|
||||
PluginLog.Log(plugins.get(i).contents.toString());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user