Compare commits

...
Author SHA1 Message Date
Luis C.M. ba2721d3df implemented class filters using reflection 2025-06-09 23:16:08 -05:00
Luis C.M. b1f5f90915 jar capable of scripting in Java 21 2025-06-09 23:03:46 -05:00
4 changed files with 50 additions and 36 deletions

No files matched your search

+1 -1
View File
@@ -7,7 +7,7 @@ plugins {
java { java {
toolchain { toolchain {
languageVersion.set(JavaLanguageVersion.of(21)) languageVersion.set(JavaLanguageVersion.of(8))
} }
} }
-2
View File
@@ -49,6 +49,4 @@ dependencies {
exclude module: 'log4j-core' exclude module: 'log4j-core'
} }
} }
implementation('org.openjdk.nashorn:nashorn-core:15.4')
} }
@@ -7,9 +7,11 @@ import noppes.npcs.config.ConfigScript;
import noppes.npcs.constants.EnumScriptType; import noppes.npcs.constants.EnumScriptType;
import noppes.npcs.controllers.data.IScriptHandler; import noppes.npcs.controllers.data.IScriptHandler;
import noppes.npcs.scripted.NpcAPI; import noppes.npcs.scripted.NpcAPI;
import org.openjdk.nashorn.api.scripting.ScriptObjectMirror;
import javax.script.*; import javax.script.Compilable;
import javax.script.CompiledScript;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.io.StringWriter; import java.io.StringWriter;
import java.lang.reflect.Method; import java.lang.reflect.Method;
@@ -33,7 +35,6 @@ public class ScriptContainer {
private static Method luaCoerce; private static Method luaCoerce;
private static Method luaCall; private static Method luaCall;
private CompiledScript compScript = null; private CompiledScript compScript = null;
private final HashMap<String, ScriptObjectMirror> cachedFunctions = new HashMap<>();
public ScriptContainer(IScriptHandler handler) { public ScriptContainer(IScriptHandler handler) {
this.handler = handler; this.handler = handler;
@@ -172,7 +173,6 @@ public class ScriptContainer {
try { try {
if (!evaluated) { if (!evaluated) {
this.cachedFunctions.clear();
engine.eval(getFullCode()); engine.eval(getFullCode());
evaluated = true; evaluated = true;
} }
@@ -188,15 +188,7 @@ public class ScriptContainer {
unknownFunctions.add(type); unknownFunctions.add(type);
} }
} else { } else {
if (!this.cachedFunctions.containsKey(type)) { ((Invocable) engine).invokeFunction(type, event);
ScriptObjectMirror global = (ScriptObjectMirror) engine.getBindings(ScriptContext.ENGINE_SCOPE);
ScriptObjectMirror func = (ScriptObjectMirror) global.get(type);
this.cachedFunctions.put(type, func);
}
ScriptObjectMirror func = this.cachedFunctions.get(type);
if (func != null) {
func.call(null, event);
}
} }
} catch (NoSuchMethodException e) { } catch (NoSuchMethodException e) {
unknownFunctions.add(type); unknownFunctions.add(type);
@@ -1,6 +1,8 @@
package noppes.npcs.controllers; package noppes.npcs.controllers;
import cpw.mods.fml.common.eventhandler.SubscribeEvent; import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import jdk.nashorn.api.scripting.ClassFilter;
import jdk.nashorn.api.scripting.NashornScriptEngineFactory;
import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList; import net.minecraft.nbt.NBTTagList;
@@ -19,8 +21,6 @@ import noppes.npcs.entity.EntityNPCInterface;
import noppes.npcs.scripted.ScriptWorld; import noppes.npcs.scripted.ScriptWorld;
import noppes.npcs.util.JsonException; import noppes.npcs.util.JsonException;
import noppes.npcs.util.NBTJsonUtil; import noppes.npcs.util.NBTJsonUtil;
import org.openjdk.nashorn.api.scripting.ClassFilter;
import org.openjdk.nashorn.api.scripting.NashornScriptEngineFactory;
import javax.script.ScriptContext; import javax.script.ScriptContext;
import javax.script.ScriptEngine; import javax.script.ScriptEngine;
@@ -32,6 +32,8 @@ import java.io.FileReader;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.lang.reflect.Proxy; import java.lang.reflect.Proxy;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.*; import java.util.*;
import static noppes.npcs.util.CustomNPCsThreader.customNPCThread; import static noppes.npcs.util.CustomNPCsThreader.customNPCThread;
@@ -57,6 +59,7 @@ public class ScriptController {
public GlobalNPCDataScript globalNpcScripts = new GlobalNPCDataScript((EntityNPCInterface) null); public GlobalNPCDataScript globalNpcScripts = new GlobalNPCDataScript((EntityNPCInterface) null);
public long lastGlobalNpcUpdate = 0L; public long lastGlobalNpcUpdate = 0L;
private URLClassLoader loader;
public ScriptController() { public ScriptController() {
Instance = this; Instance = this;
@@ -66,10 +69,16 @@ public class ScriptController {
LogWriter.info("Script Engines Available:"); LogWriter.info("Script Engines Available:");
try { try {
// this.nashornFactory = new NashornScriptEngineFactory(); File nashornJar = new File("mods/nashorn.jar"); // Adjust path if needed
// LogWriter.info("→ standalone Nashorn loaded"); this.loader = new URLClassLoader(new URL[]{nashornJar.toURI().toURL()}, CustomNpcs.class.getClassLoader());
Class<?> clazz = Class.forName("org.openjdk.nashorn.api.scripting.NashornScriptEngineFactory");
this.nashornFactory = (ScriptEngineFactory) clazz.newInstance(); ClassLoader cl = CustomNpcs.class.getClassLoader();
Thread.currentThread().setContextClassLoader(cl);
LogWriter.info("ClassLoader: " + cl);
LogWriter.info("Parent: " + cl.getParent());
Class<?> factoryClass = Class.forName("jdk.nashorn.api.scripting.NashornScriptEngineFactory", true, loader);
this.nashornFactory = (ScriptEngineFactory) factoryClass.getDeclaredConstructor().newInstance();
this.languages.put("ECMAScript", ".js"); this.languages.put("ECMAScript", ".js");
LogWriter.info("→ standalone Nashorn loaded: " + this.nashornFactory.getEngineName()); LogWriter.info("→ standalone Nashorn loaded: " + this.nashornFactory.getEngineName());
} catch (Exception e) { } catch (Exception e) {
@@ -292,29 +301,44 @@ public class ScriptController {
public ScriptEngine getEngineByName(String language) { public ScriptEngine getEngineByName(String language) {
if (nashornNames.contains(language) && this.nashornFactory != null) { if (nashornNames.contains(language) && this.nashornFactory != null) {
ScriptEngine scriptEngine = getOpenJDKNashornEngine(); try {
if (scriptEngine != null) return scriptEngine; ScriptEngine scriptEngine;
Class<?> factoryClass = Class.forName("jdk.nashorn.api.scripting.NashornScriptEngineFactory",
true, this.loader);
if (ConfigScript.EnableBannedClasses) { if (ConfigScript.EnableBannedClasses) {
try { Class<?> classFilterClass = Class.forName("jdk.nashorn.api.scripting.ClassFilter",
ClassFilter filter = s -> { true, this.loader);
for (String className : ConfigScript.BannedClasses) {
if (s.compareTo(className) == 0) { Object classFilter = Proxy.newProxyInstance(
return false; classFilterClass.getClassLoader(),
} new Class[]{classFilterClass},
(proxy, method, args) -> {
if ("exposeToScripts".equals(method.getName()) && args.length == 1) {
String className = (String) args[0];
for (String banned : ConfigScript.BannedClasses) {
if (className.equals(banned)) return false;
} }
return true; return true;
};
NashornScriptEngineFactory nashornScriptEngineFactory = (NashornScriptEngineFactory) this.nashornFactory;
scriptEngine = nashornScriptEngineFactory.getScriptEngine(filter);
} catch (Exception e) {
scriptEngine = this.nashornFactory.getScriptEngine();
} }
throw new UnsupportedOperationException("Unsupported method: " + method);
});
Method getScriptEngine = factoryClass.getMethod(
"getScriptEngine",
classFilterClass);
scriptEngine = (ScriptEngine) getScriptEngine.invoke(this.nashornFactory, classFilter);
} else { } else {
scriptEngine = this.nashornFactory.getScriptEngine(); Method getScriptEngine = factoryClass.getMethod("getScriptEngine");
scriptEngine = (ScriptEngine) getScriptEngine.invoke(this.nashornFactory);
} }
scriptEngine.setBindings(this.manager.getBindings(), ScriptContext.GLOBAL_SCOPE); scriptEngine.setBindings(this.manager.getBindings(), ScriptContext.GLOBAL_SCOPE);
return scriptEngine; return scriptEngine;
} catch (Exception exception) {
LogWriter.except(exception);
return null;
}
} }
return manager.getEngineByName(language); return manager.getEngineByName(language);
} }