From a4008947568489a70ddf85e2655ae3a8c1b61f5b Mon Sep 17 00:00:00 2001 From: wagyourtail Date: Wed, 12 Aug 2020 02:58:58 -0600 Subject: [PATCH] add support for runScript --- build.gradle | 2 +- .../jsmacrosjython/JsMacrosJython.java | 27 ++++++++++++++++--- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/build.gradle b/build.gradle index 22a2dee..c54c590 100644 --- a/build.gradle +++ b/build.gradle @@ -39,7 +39,7 @@ dependencies { mappings "net.fabricmc:yarn:${project.yarn_mappings}:v2" modImplementation "net.fabricmc:fabric-loader:${project.loader_version}" - modImplementation name: "jsmacros-1.16.1-1.2.2-beta-b071ddf-dev" + modImplementation name: "jsmacros-1.16.2-1.2.4-beta-921af6a-dev" //partial fabric api modImplementation "net.fabricmc.fabric-api:fabric-api-base:${project.api_base_version}" diff --git a/src/main/java/xyz/wagyourtail/jsmacrosjython/JsMacrosJython.java b/src/main/java/xyz/wagyourtail/jsmacrosjython/JsMacrosJython.java index 6fcbefe..1188d10 100644 --- a/src/main/java/xyz/wagyourtail/jsmacrosjython/JsMacrosJython.java +++ b/src/main/java/xyz/wagyourtail/jsmacrosjython/JsMacrosJython.java @@ -1,6 +1,7 @@ package xyz.wagyourtail.jsmacrosjython; import java.io.File; +import java.nio.file.Path; import java.util.Map; import org.python.util.PythonInterpreter; @@ -13,7 +14,6 @@ public class JsMacrosJython implements ClientModInitializer { public static boolean hasJEP = false; - private static Functions consumerFix = new consumerFunctions("consumer"); @Override public void onInitializeClient() { @@ -26,6 +26,7 @@ public void onInitializeClient() { // register language RunScript.addLanguage(new RunScript.Language() { + private Functions consumerFix = new consumerFunctions("consumer"); @Override public void exec(RawMacro macro, File file, String event, Map args) throws Exception { @@ -49,12 +50,32 @@ public void exec(RawMacro macro, File file, String event, Map ar } } - + @Override + public void exec(String script, Map globals, Path path) throws Exception { + try (PythonInterpreter interp = new PythonInterpreter()) { + + for (Functions f : RunScript.standardLib) { + if (!f.excludeLanguages.contains("jython.py")) { + interp.set(f.libName, f); + } + } + interp.set(consumerFix.libName, consumerFix); + + if (globals != null) for (Map.Entry e : globals.entrySet()) { + interp.set(e.getKey(), e.getValue()); + } + + interp.exec(script); + } catch (Exception e) { + throw e; + } + + } + @Override public String extension() { return hasJEP ? "jython.py" : ".py"; } - }); RunScript.sortLanguages();