-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0bc5882
commit 8761ce8
Showing
8 changed files
with
247 additions
and
268 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
src/main/java/xyz/wagyourtail/jsmacros/jython/client/JsMacrosJython.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package xyz.wagyourtail.jsmacros.jython.client; | ||
|
||
import net.fabricmc.api.ClientModInitializer; | ||
import net.fabricmc.loader.api.FabricLoader; | ||
import org.python.util.PythonInterpreter; | ||
import xyz.wagyourtail.jsmacros.client.JsMacros; | ||
import xyz.wagyourtail.jsmacros.jython.language.impl.JythonLanguageDescription; | ||
import xyz.wagyourtail.jsmacros.jython.library.impl.FConsumerJython; | ||
|
||
public class JsMacrosJython implements ClientModInitializer { | ||
public static boolean hasJEP = false; | ||
|
||
@Override | ||
public void onInitializeClient() { | ||
hasJEP = FabricLoader.getInstance().isModLoaded("jsmacros-jep"); | ||
JsMacros.core.addLanguage(new JythonLanguageDescription(hasJEP ? "jython.py" : ".py", JsMacros.core)); | ||
JsMacros.core.sortLanguages(); | ||
JsMacros.core.libraryRegistry.addLibrary(FConsumerJython.class); | ||
|
||
|
||
// pre-init | ||
Thread t = new Thread(() -> { | ||
try (PythonInterpreter interp = new PythonInterpreter()) { | ||
interp.exec("print(\"Jython Loaded.\")"); | ||
} catch(Exception e) { | ||
e.printStackTrace(); | ||
} | ||
}); | ||
|
||
t.start(); | ||
} | ||
|
||
} |
51 changes: 51 additions & 0 deletions
51
src/main/java/xyz/wagyourtail/jsmacros/jython/language/impl/JythonLanguageDescription.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package xyz.wagyourtail.jsmacros.jython.language.impl; | ||
|
||
import org.python.util.PythonInterpreter; | ||
import xyz.wagyourtail.jsmacros.core.Core; | ||
import xyz.wagyourtail.jsmacros.core.config.ScriptTrigger; | ||
import xyz.wagyourtail.jsmacros.core.event.BaseEvent; | ||
import xyz.wagyourtail.jsmacros.core.language.BaseLanguage; | ||
|
||
import java.io.File; | ||
import java.nio.file.Path; | ||
import java.util.Map; | ||
|
||
public class JythonLanguageDescription extends BaseLanguage { | ||
|
||
public JythonLanguageDescription(String extension, Core runner) { | ||
super(extension, runner); | ||
} | ||
|
||
protected void execContext(Executor exec) throws Exception { | ||
try (PythonInterpreter interp = new PythonInterpreter()) { | ||
retrieveLibs(interp).forEach(interp::set); | ||
|
||
exec.accept(interp); | ||
} | ||
} | ||
|
||
@Override | ||
public void exec(ScriptTrigger scriptTrigger, File file, BaseEvent baseEvent) throws Exception { | ||
execContext((interp) -> { | ||
interp.set("event", baseEvent); | ||
interp.set("file", file); | ||
|
||
interp.exec("import os\nos.chdir('" | ||
+ file.getParentFile().getCanonicalPath().replaceAll("\\\\", "/") + "')"); | ||
interp.execfile(file.getCanonicalPath()); | ||
}); | ||
} | ||
|
||
@Override | ||
public void exec(String script, Map<String, Object> globals, Path path) throws Exception { | ||
execContext((interp) -> { | ||
globals.forEach(interp::set); | ||
|
||
interp.exec(script); | ||
}); | ||
} | ||
|
||
protected interface Executor { | ||
void accept(PythonInterpreter interpreter) throws Exception; | ||
} | ||
} |
152 changes: 152 additions & 0 deletions
152
src/main/java/xyz/wagyourtail/jsmacros/jython/library/impl/FConsumerJython.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
package xyz.wagyourtail.jsmacros.jython.library.impl; | ||
|
||
import org.python.core.PyFunction; | ||
import org.python.core.PyObject; | ||
import xyz.wagyourtail.jsmacros.core.MethodWrapper; | ||
import xyz.wagyourtail.jsmacros.core.language.BaseLanguage; | ||
import xyz.wagyourtail.jsmacros.core.library.IFConsumer; | ||
import xyz.wagyourtail.jsmacros.core.library.Library; | ||
import xyz.wagyourtail.jsmacros.core.library.PerExecLanguageLibrary; | ||
import xyz.wagyourtail.jsmacros.jython.language.impl.JythonLanguageDescription; | ||
|
||
@Library(value = "consumer", languages = JythonLanguageDescription.class) | ||
public class FConsumerJython extends PerExecLanguageLibrary<IFConsumer> implements IFConsumer<PyFunction, PyFunction, PyFunction> { | ||
|
||
public FConsumerJython(Class<? extends BaseLanguage> language, Object context, Thread thread) { | ||
super(language, context, thread); | ||
} | ||
|
||
@Override | ||
public <A, B, R> MethodWrapper<A, B, R> toConsumer(PyFunction pyFunction) { | ||
return autoWrap(pyFunction); | ||
} | ||
|
||
@Override | ||
public <A, B, R> MethodWrapper<A, B, R> toBiConsumer(PyFunction pyFunction) { | ||
return autoWrap(pyFunction); | ||
} | ||
|
||
@Override | ||
public <A, B, R> MethodWrapper<A, B, R> toAsyncConsumer(PyFunction pyFunction) { | ||
return autoWrapAsync(pyFunction); | ||
} | ||
|
||
@Override | ||
public <A, B, R> MethodWrapper<A, B, R> toAsyncBiConsumer(PyFunction pyFunction) { | ||
return autoWrapAsync(pyFunction); | ||
} | ||
|
||
@Override | ||
public <A, B, R> MethodWrapper<A, B, R> autoWrap(PyFunction pyFunction) { | ||
return new MethodWrapper<A, B, R>() { | ||
@Override | ||
public void accept(A a) { | ||
runInt(a); | ||
} | ||
|
||
@Override | ||
public void accept(A a, B b) { | ||
runInt(a, b); | ||
} | ||
|
||
@Override | ||
public R apply(A a) { | ||
return (R) runInt(a).__tojava__(Object.class); | ||
} | ||
|
||
@Override | ||
public R apply(A a, B b) { | ||
return (R) runInt(a, b).__tojava__(Object.class); | ||
} | ||
|
||
@Override | ||
public boolean test(A a) { | ||
return (boolean) runInt(a).__tojava__(boolean.class); | ||
} | ||
|
||
@Override | ||
public boolean test(A a, B b) { | ||
return (boolean) runInt(a, b).__tojava__(boolean.class); | ||
} | ||
|
||
@Override | ||
public void run() { | ||
runInt(); | ||
} | ||
|
||
@Override | ||
public int compare(A o1, A o2) { | ||
return (int) runInt(o1, o2).__tojava__(int.class); | ||
} | ||
|
||
@Override | ||
public R get() { | ||
return (R) runInt().__tojava__(Object.class); | ||
} | ||
|
||
private PyObject runInt(Object...args) { | ||
return pyFunction._jcall(args); | ||
} | ||
}; | ||
} | ||
|
||
@Override | ||
public <A, B, R> MethodWrapper<A, B, R> autoWrapAsync(PyFunction pyFunction) { | ||
return new MethodWrapper<A, B, R>() { | ||
@Override | ||
public void accept(A a) { | ||
acceptInt(a); | ||
} | ||
|
||
@Override | ||
public void accept(A a, B b) { | ||
acceptInt(a, b); | ||
} | ||
|
||
@Override | ||
public R apply(A a) { | ||
return (R) runInt(a).__tojava__(Object.class); | ||
} | ||
|
||
@Override | ||
public R apply(A a, B b) { | ||
return (R) runInt(a, b).__tojava__(Object.class); | ||
} | ||
|
||
@Override | ||
public boolean test(A a) { | ||
return (boolean) runInt(a).__tojava__(boolean.class); | ||
} | ||
|
||
@Override | ||
public boolean test(A a, B b) { | ||
return (boolean) runInt(a, b).__tojava__(boolean.class); | ||
} | ||
|
||
@Override | ||
public void run() { | ||
acceptInt(); | ||
} | ||
|
||
@Override | ||
public int compare(A o1, A o2) { | ||
return (int) runInt(o1, o2).__tojava__(int.class); | ||
} | ||
|
||
@Override | ||
public R get() { | ||
return (R) runInt().__tojava__(Object.class); | ||
} | ||
|
||
private PyObject runInt(Object...args) { | ||
return pyFunction._jcall(args); | ||
} | ||
|
||
private void acceptInt(Object...args) { | ||
Thread t = new Thread(() -> pyFunction._jcall(args)); | ||
t.start(); | ||
} | ||
}; | ||
} | ||
|
||
} |
96 changes: 0 additions & 96 deletions
96
src/main/java/xyz/wagyourtail/jsmacrosjython/JsMacrosJython.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.