Replace NashornScriptEngine with implicit GraalJSScriptEngine

GraalJSScriptEngine implements Invocable, which is why casting to it works.
However, this is just a quick and ugly fix to make it compile.
A better solution would be to cast it once, immediately after `eval`,
and from then on only handle it as Invocable.

Scripts still need to be fixed. They are still using Rhino and Nashorn-specific
ways of importing packages.
Usages of "importPackage" and "Packages" need to be replaced with
the Graal specific "Java.type".
This commit is contained in:
P0nk
2021-04-17 15:08:45 +02:00
parent e42fb27459
commit a18a1cb8ce
13 changed files with 284 additions and 364 deletions

View File

@@ -23,22 +23,20 @@ package scripting.npc;
import client.MapleCharacter;
import client.MapleClient;
import java.lang.reflect.UndeclaredThrowableException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.script.ScriptException;
import jdk.nashorn.api.scripting.NashornScriptEngine;
import net.server.world.MaplePartyCharacter;
import scripting.AbstractScriptManager;
import server.MapleItemInformationProvider.ScriptedItem;
import tools.FilePrinter;
import tools.MaplePacketCreator;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptException;
import java.lang.reflect.UndeclaredThrowableException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
*
* @author Matze
@@ -52,10 +50,10 @@ public class NPCScriptManager extends AbstractScriptManager {
}
private Map<MapleClient, NPCConversationManager> cms = new HashMap<>();
private Map<MapleClient, NashornScriptEngine> scripts = new HashMap<>();
private Map<MapleClient, ScriptEngine> scripts = new HashMap<>();
public boolean isNpcScriptAvailable(MapleClient c, String fileName) {
NashornScriptEngine iv = null;
ScriptEngine iv = null;
if (fileName != null) {
iv = getScriptEngine("npc/" + fileName + ".js", c);
}
@@ -91,7 +89,7 @@ public class NPCScriptManager extends AbstractScriptManager {
return;
}
cms.put(c, cm);
NashornScriptEngine iv = getScriptEngine("npc/" + filename + ".js", c);
ScriptEngine iv = getScriptEngine("npc/" + filename + ".js", c);
if (iv == null) {
c.getPlayer().dropMessage(1, "NPC " + npc + " is uncoded.");
@@ -101,7 +99,7 @@ public class NPCScriptManager extends AbstractScriptManager {
iv.put("cm", cm);
scripts.put(c, iv);
try {
iv.invokeFunction("start", chrs);
((Invocable) iv).invokeFunction("start", chrs);
} catch (final NoSuchMethodException nsme) {
nsme.printStackTrace();
}
@@ -123,7 +121,7 @@ public class NPCScriptManager extends AbstractScriptManager {
}
if (c.canClickNPC()) {
cms.put(c, cm);
NashornScriptEngine iv = null;
ScriptEngine iv = null;
if (!itemScript) {
if (fileName != null) {
iv = getScriptEngine("npc/" + fileName + ".js", c);
@@ -145,10 +143,10 @@ public class NPCScriptManager extends AbstractScriptManager {
scripts.put(c, iv);
c.setClickedNPC();
try {
iv.invokeFunction("start");
((Invocable) iv).invokeFunction("start");
} catch (final NoSuchMethodException nsme) {
try {
iv.invokeFunction("start", chr);
((Invocable) iv).invokeFunction("start", chr);
} catch (final NoSuchMethodException nsma) {
nsma.printStackTrace();
}
@@ -171,11 +169,11 @@ public class NPCScriptManager extends AbstractScriptManager {
}
public void action(MapleClient c, byte mode, byte type, int selection) {
NashornScriptEngine iv = scripts.get(c);
ScriptEngine iv = scripts.get(c);
if (iv != null) {
try {
c.setClickedNPC();
iv.invokeFunction("action", mode, type, selection);
((Invocable) iv).invokeFunction("action", mode, type, selection);
} catch (ScriptException | NoSuchMethodException t) {
if (getCM(c) != null) {
FilePrinter.printError(FilePrinter.NPC + getCM(c).getNpc() + ".txt", t);