diff options
Diffstat (limited to 'src/main/java/ftbsc/bscv/system/Macros.java')
-rw-r--r-- | src/main/java/ftbsc/bscv/system/Macros.java | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/src/main/java/ftbsc/bscv/system/Macros.java b/src/main/java/ftbsc/bscv/system/Macros.java new file mode 100644 index 0000000..ca8d56f --- /dev/null +++ b/src/main/java/ftbsc/bscv/system/Macros.java @@ -0,0 +1,50 @@ +package ftbsc.bscv.system; + +import ftbsc.bscv.Boscovicino; +import party.iroiro.luajava.Lua; +import party.iroiro.luajava.luajit.LuaJit; + +import java.io.IOException; +import java.nio.file.DirectoryStream; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.HashMap; + +public class Macros { + private final Lua lua; + private final HashMap<String, String> macroCache; + + public Macros() throws IOException { + this.lua = new LuaJit(); + this.lua.pushJavaClass(Boscovicino.class); //TODO use instance + this.lua.setGlobal("BSCV"); + this.macroCache = new HashMap<>(); + + // load macros + Path path = Paths.get("macros/"); + if(!Files.isDirectory(path)) { + Files.createDirectories(path); + } + + //TODO lazy load setting + try(DirectoryStream<Path> stream = Files.newDirectoryStream(path, (p) -> p.toFile().isFile())) { + for(Path macro : stream) { + String name = macro.getFileName().toString(); + if(name.endsWith(".lua")) { + String code = String.join("\n", Files.readAllLines(macro)); + this.macroCache.put(name, code); + Boscovicino.bindings.registerBinding(name, Bindings.UNBOUND, name); + } + } + } + } + + public boolean execute(String macroFilePath) { + String code = this.macroCache.get(macroFilePath); + if(code == null) return false; + + this.lua.execute(code); + return true; + } +} |