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

@@ -24,7 +24,6 @@ package client;
import client.inventory.MapleInventoryType;
import config.YamlConfig;
import constants.game.GameConstants;
import jdk.nashorn.api.scripting.NashornScriptEngine;
import net.server.Server;
import net.server.audit.locks.MonitoredLockType;
import net.server.audit.locks.factory.MonitoredReentrantLockFactory;
@@ -50,6 +49,7 @@ import server.maps.MapleMap;
import server.maps.MapleMiniDungeonInfo;
import tools.*;
import javax.script.ScriptEngine;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.InetAddress;
@@ -85,7 +85,7 @@ public class MapleClient {
private long lastPong;
private int gmlevel;
private Set<String> macs = new HashSet<>();
private Map<String, NashornScriptEngine> engines = new HashMap<>();
private Map<String, ScriptEngine> engines = new HashMap<>();
private byte characterSlots = 3;
private byte loginattempt = 0;
private String pin = "";
@@ -1041,11 +1041,11 @@ public class MapleClient {
gmlevel = level;
}
public void setScriptEngine(String name, NashornScriptEngine e) {
engines.put(name, e);
public void setScriptEngine(String name, ScriptEngine e) {
engines.put(name, e);
}
public NashornScriptEngine getScriptEngine(String name) {
public ScriptEngine getScriptEngine(String name) {
return engines.get(name);
}

View File

@@ -5,32 +5,6 @@ public class ServerConstants {
//Server Version
public static short VERSION = 83;
//Java Configuration
public static final boolean JAVA_8 = getJavaVersion() >= 8; //Max amount of times a party leader is allowed to persist on the Party Search before entry expiration (thus needing to manually restart the Party Search to be able to search for members).
//Debug Variables
public static int[] DEBUG_VALUES = new int[10]; // Field designed for packet testing purposes
// https://github.com/openstreetmap/josm/blob/a3a6e8a6b657cf4c5b4c64ea14d6e87be6280d65/src/org/openstreetmap/josm/tools/Utils.java#L1566-L1585
// Added by kolakcc (Familiar)
/**
* Returns the Java version as an int value.
* @return the Java version as an int value (8, 9, etc.)
* @since 12130
*/
public static int getJavaVersion() {
String version = System.getProperty("java.version");
if (version.startsWith("1.")) {
version = version.substring(2);
}
// Allow these formats:
// 1.8.0_72-ea
// 9-ea
// 9
// 9.0.1
int dotPos = version.indexOf('.');
int dashPos = version.indexOf('-');
return Integer.parseInt(version.substring(0,
dotPos > -1 ? dotPos : dashPos > -1 ? dashPos : 1));
}
}

View File

@@ -28,7 +28,6 @@ import client.inventory.manipulator.MapleInventoryManipulator;
import config.YamlConfig;
import constants.game.GameConstants;
import constants.inventory.ItemConstants;
import constants.net.ServerConstants;
import net.server.Server;
import net.server.guild.MapleGuild;
import net.server.world.MapleParty;
@@ -217,45 +216,30 @@ public class AbstractPlayerInteraction {
public boolean canHold(int itemid, int quantity, int removeItemid, int removeQuantity) {
return canHoldAllAfterRemoving(Collections.singletonList(itemid), Collections.singletonList(quantity), Collections.singletonList(removeItemid), Collections.singletonList(removeQuantity));
}
private List<Integer> convertToIntegerArray(List<Object> list) {
List<Integer> intList = new ArrayList<>(); // JAVA 7 Rhino script engine. Thanks Bruno, felipepm10 for noticing a typecast issue here.
if (ServerConstants.JAVA_8) {
for (Object d: list) {
intList.add((Integer) d);
}
} else {
for (Object d: list) {
intList.add(((Double) d).intValue());
}
}
return intList;
private List<Integer> convertToIntegerList(List<Object> objects) {
List<Integer> intList = new ArrayList<>();
for (Object object : objects) {
intList.add((Integer) object);
}
public boolean canHoldAll(List<Object> itemids) {
List<Object> quantity = new LinkedList<>();
if (ServerConstants.JAVA_8) {
Integer intOne = 1;
for (int i = 0; i < itemids.size(); i++) {
quantity.add(intOne);
}
} else {
Double doubleOne = 1.0;
for (int i = 0; i < itemids.size(); i++) {
quantity.add(doubleOne);
}
}
return canHoldAll(itemids, quantity);
return intList;
}
public boolean canHoldAll(List<Object> itemids) {
List<Object> quantity = new LinkedList<>();
final int intOne = 1;
for (int i = 0; i < itemids.size(); i++) {
quantity.add(intOne);
}
return canHoldAll(itemids, quantity);
}
public boolean canHoldAll(List<Object> itemids, List<Object> quantity) {
return canHoldAll(convertToIntegerArray(itemids), convertToIntegerArray(quantity), true);
return canHoldAll(convertToIntegerList(itemids), convertToIntegerList(quantity), true);
}
private boolean canHoldAll(List<Integer> itemids, List<Integer> quantity, boolean isInteger) {

View File

@@ -22,39 +22,35 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
package scripting;
import client.MapleClient;
import tools.FilePrinter;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineFactory;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import javax.script.*;
import constants.net.ServerConstants;
import jdk.nashorn.api.scripting.NashornScriptEngine;
import tools.FilePrinter;
/**
*
* @author Matze
*/
public abstract class AbstractScriptManager {
private ScriptEngineFactory sef;
private final ScriptEngineFactory sef;
protected AbstractScriptManager() {
sef = new ScriptEngineManager().getEngineByName("javascript").getFactory();
sef = new ScriptEngineManager().getEngineByName("graal.js").getFactory();
}
protected NashornScriptEngine getScriptEngine(String path) {
protected ScriptEngine getScriptEngine(String path) {
path = "scripts/" + path;
File scriptFile = new File(path);
if (!scriptFile.exists()) {
return null;
}
NashornScriptEngine engine = (NashornScriptEngine) sef.getScriptEngine();
ScriptEngine engine = sef.getScriptEngine();
try (FileReader fr = new FileReader(scriptFile)) {
if (ServerConstants.JAVA_8){
engine.eval("load('nashorn:mozilla_compat.js');" + System.lineSeparator());
}
engine.eval(fr);
} catch (final ScriptException | IOException t) {
FilePrinter.printError(FilePrinter.INVOCABLE + path.substring(12), t, path);
@@ -64,8 +60,8 @@ public abstract class AbstractScriptManager {
return engine;
}
protected NashornScriptEngine getScriptEngine(String path, MapleClient c) {
NashornScriptEngine engine = c.getScriptEngine("scripts/" + path);
protected ScriptEngine getScriptEngine(String path, MapleClient c) {
ScriptEngine engine = c.getScriptEngine("scripts/" + path);
if (engine == null) {
engine = getScriptEngine(path);
c.setScriptEngine(path, engine);

View File

@@ -26,7 +26,6 @@ import client.Skill;
import client.SkillFactory;
import config.YamlConfig;
import constants.inventory.ItemConstants;
import constants.net.ServerConstants;
import net.server.audit.LockCollector;
import net.server.audit.locks.*;
import net.server.audit.locks.factory.MonitoredReadLockFactory;
@@ -52,6 +51,7 @@ import server.maps.MapleReactor;
import tools.MaplePacketCreator;
import tools.Pair;
import javax.script.Invocable;
import javax.script.ScriptException;
import java.awt.*;
import java.util.List;
@@ -217,7 +217,7 @@ public class EventInstanceManager {
public Object invokeScriptFunction(String name, Object... args) throws ScriptException, NoSuchMethodException {
if (!disposed) {
return em.getIv().invokeFunction(name, args);
return ((Invocable) em.getIv()).invokeFunction(name, args);
} else {
return null;
}
@@ -557,31 +557,25 @@ public class EventInstanceManager {
}
public void monsterKilled(MapleCharacter chr, final MapleMonster mob) {
try {
int inc;
if (ServerConstants.JAVA_8) {
inc = (int)invokeScriptFunction("monsterValue", EventInstanceManager.this, mob.getId());
} else {
inc = ((Double) invokeScriptFunction("monsterValue", EventInstanceManager.this, mob.getId())).intValue();
}
if (inc != 0) {
Integer kc = killCount.get(chr);
if (kc == null) {
kc = inc;
} else {
kc += inc;
}
killCount.put(chr, kc);
if (expedition != null){
expedition.monsterKilled(chr, mob);
}
}
} catch (ScriptException | NoSuchMethodException ex) {
ex.printStackTrace();
}
}
try {
final int inc = (int) invokeScriptFunction("monsterValue", EventInstanceManager.this, mob.getId());
if (inc != 0) {
Integer kc = killCount.get(chr);
if (kc == null) {
kc = inc;
} else {
kc += inc;
}
killCount.put(chr, kc);
if (expedition != null) {
expedition.monsterKilled(chr, mob);
}
}
} catch (ScriptException | NoSuchMethodException ex) {
ex.printStackTrace();
}
}
public int getKillCount(MapleCharacter chr) {
Integer kc = killCount.get(chr);
@@ -886,31 +880,25 @@ public class EventInstanceManager {
public MapleMonster getMonster(int mid) {
return(MapleLifeFactory.getMonster(mid));
}
private List<Integer> convertToIntegerArray(List<Object> list) {
List<Integer> intList = new ArrayList<>();
if (ServerConstants.JAVA_8) {
for (Object d: list) {
intList.add((Integer) d);
private List<Integer> convertToIntegerList(List<Object> objects) {
List<Integer> intList = new ArrayList<>();
for (Object object : objects) {
intList.add((Integer) object);
}
} else {
for (Object d: list) {
intList.add(((Double) d).intValue());
}
}
return intList;
return intList;
}
public void setEventClearStageExp(List<Object> gain) {
onMapClearExp.clear();
onMapClearExp.addAll(convertToIntegerArray(gain));
onMapClearExp.addAll(convertToIntegerList(gain));
}
public void setEventClearStageMeso(List<Object> gain) {
onMapClearMeso.clear();
onMapClearMeso.addAll(convertToIntegerArray(gain));
onMapClearMeso.addAll(convertToIntegerList(gain));
}
public Integer getClearStageExp(int stage) { //stage counts from ONE.
@@ -940,7 +928,7 @@ public class EventInstanceManager {
}
public final void setExclusiveItems(List<Object> items) {
List<Integer> exclusive = convertToIntegerArray(items);
List<Integer> exclusive = convertToIntegerList(items);
wL.lock();
try {
@@ -968,8 +956,8 @@ public class EventInstanceManager {
if(eventLevel <= 0 || eventLevel > YamlConfig.config.server.MAX_EVENT_LEVELS) return;
eventLevel--; //event level starts from 1
List<Integer> rewardIds = convertToIntegerArray(rwds);
List<Integer> rewardQtys = convertToIntegerArray(qtys);
List<Integer> rewardIds = convertToIntegerList(rwds);
List<Integer> rewardQtys = convertToIntegerList(qtys);
//rewardsSet and rewardsQty hold temporary values
wL.lock();

View File

@@ -24,10 +24,6 @@ package scripting.event;
import client.MapleCharacter;
import config.YamlConfig;
import constants.game.GameConstants;
import constants.net.ServerConstants;
import jdk.nashorn.api.scripting.NashornScriptEngine;
import jdk.nashorn.api.scripting.ScriptObjectMirror;
import jdk.nashorn.api.scripting.ScriptUtils;
import net.server.Server;
import net.server.audit.LockCollector;
import net.server.audit.locks.MonitoredLockType;
@@ -48,6 +44,8 @@ import server.maps.MapleMap;
import server.quest.MapleQuest;
import tools.exceptions.EventInstanceInProgressException;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptException;
import java.util.*;
import java.util.concurrent.Semaphore;
@@ -62,7 +60,7 @@ import java.util.logging.Logger;
* @author Ronan
*/
public class EventManager {
private NashornScriptEngine iv;
private ScriptEngine iv;
private Channel cserv;
private World wserv;
private Server server;
@@ -85,7 +83,7 @@ public class EventManager {
private static final int maxLobbys = 8; // an event manager holds up to this amount of concurrent lobbys
public EventManager(Channel cserv, NashornScriptEngine iv, String name) {
public EventManager(Channel cserv, ScriptEngine iv, String name) {
this.server = Server.getInstance();
this.iv = iv;
this.cserv = cserv;
@@ -104,7 +102,7 @@ public class EventManager {
ess.dispose();
try {
iv.invokeFunction("cancelSchedule", (Object) null);
((Invocable) iv).invokeFunction("cancelSchedule", (Object) null);
} catch (ScriptException | NoSuchMethodException ex) {
ex.printStackTrace();
}
@@ -151,20 +149,14 @@ public class EventManager {
queueLock = queueLock.dispose();
startLock = startLock.dispose();
}
private List<Integer> convertToIntegerArray(List<Object> list) {
private List<Integer> convertToIntegerList(List<Object> objects) {
List<Integer> intList = new ArrayList<>();
if (ServerConstants.JAVA_8) {
for (Object d: list) {
intList.add((Integer) d);
}
} else {
for (Object d: list) {
intList.add(((Double) d).intValue());
}
for (Object object : objects) {
intList.add((Integer) object);
}
return intList;
}
@@ -174,18 +166,8 @@ public class EventManager {
private List<Integer> getLobbyRange() {
try {
if (!ServerConstants.JAVA_8) {
return convertToIntegerArray((List<Object>)iv.invokeFunction("setLobbyRange", (Object) null));
} else { // java 8 support here thanks to MedicOP
ScriptObjectMirror object = (ScriptObjectMirror) iv.invokeFunction("setLobbyRange", (Object) null);
int[] to = object.to(int[].class);
List<Integer> list = new ArrayList<>();
for (int i : to) {
list.add(i);
}
return list;
}
List<Object> objects = (List<Object>) ((Invocable) iv).invokeFunction("setLobbyRange", (Object) null);
return convertToIntegerList(objects);
} catch (ScriptException | NoSuchMethodException ex) { // they didn't define a lobby range
List<Integer> defaultRange = new ArrayList<>();
defaultRange.add(0);
@@ -202,7 +184,7 @@ public class EventManager {
public EventScheduledFuture schedule(final String methodName, final EventInstanceManager eim, long delay) {
Runnable r = () -> {
try {
iv.invokeFunction(methodName, eim);
((Invocable) iv).invokeFunction(methodName, eim);
} catch (ScriptException | NoSuchMethodException ex) {
Logger.getLogger(EventManager.class.getName()).log(Level.SEVERE, null, ex);
}
@@ -217,7 +199,7 @@ public class EventManager {
public EventScheduledFuture scheduleAtTimestamp(final String methodName, long timestamp) {
Runnable r = () -> {
try {
iv.invokeFunction(methodName, (Object) null);
((Invocable) iv).invokeFunction(methodName, (Object) null);
} catch (ScriptException | NoSuchMethodException ex) {
Logger.getLogger(EventManager.class.getName()).log(Level.SEVERE, null, ex);
}
@@ -235,7 +217,7 @@ public class EventManager {
return cserv;
}
public NashornScriptEngine getIv() {
public ScriptEngine getIv() {
return iv;
}
@@ -386,7 +368,7 @@ public class EventManager {
}
private EventInstanceManager createInstance(String name, Object... args) throws ScriptException, NoSuchMethodException {
return (EventInstanceManager) iv.invokeFunction(name, args);
return (EventInstanceManager) ((Invocable) iv).invokeFunction(name, args);
}
private void registerEventInstance(String eventName, int lobbyId) {
@@ -707,8 +689,8 @@ public class EventManager {
}
registerEventInstance(eim.getName(), lobbyId);
eim.setLeader(leader);
iv.invokeFunction("setup", eim);
((Invocable) iv).invokeFunction("setup", eim);
eim.setProperty("leader", ldr);
eim.startEvent();
@@ -735,17 +717,10 @@ public class EventManager {
return(new ArrayList<>());
}
try {
Object p = iv.invokeFunction("getEligibleParty", party.getPartyMembersOnline());
Object p = ((Invocable) iv).invokeFunction("getEligibleParty", party.getPartyMembersOnline());
if(p != null) {
List<MaplePartyCharacter> lmpc;
if(ServerConstants.JAVA_8) {
lmpc = new ArrayList<>(((Map<String, MaplePartyCharacter>)(ScriptUtils.convert(p, Map.class))).values());
} else {
lmpc = new ArrayList<>((List<MaplePartyCharacter>) p);
}
final List<MaplePartyCharacter> lmpc = new ArrayList<>((List<MaplePartyCharacter>) p);
party.setEligibleMembers(lmpc);
return lmpc;
}
@@ -758,7 +733,7 @@ public class EventManager {
public void clearPQ(EventInstanceManager eim) {
try {
iv.invokeFunction("clearPQ", eim);
((Invocable) iv).invokeFunction("clearPQ", eim);
} catch (ScriptException | NoSuchMethodException ex) {
Logger.getLogger(EventManager.class.getName()).log(Level.SEVERE, null, ex);
}
@@ -766,7 +741,7 @@ public class EventManager {
public void clearPQ(EventInstanceManager eim, MapleMap toMap) {
try {
iv.invokeFunction("clearPQ", eim, toMap);
((Invocable) iv).invokeFunction("clearPQ", eim, toMap);
} catch (ScriptException | NoSuchMethodException ex) {
Logger.getLogger(EventManager.class.getName()).log(Level.SEVERE, null, ex);
}

View File

@@ -21,19 +21,19 @@
*/
package scripting.event;
import java.util.concurrent.ConcurrentHashMap;
import net.server.channel.Channel;
import scripting.AbstractScriptManager;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import jdk.nashorn.api.scripting.NashornScriptEngine;
import net.server.channel.Channel;
import scripting.AbstractScriptManager;
/**
*
* @author Matze
@@ -42,11 +42,11 @@ public class EventScriptManager extends AbstractScriptManager {
private class EventEntry {
public EventEntry(NashornScriptEngine iv, EventManager em) {
public EventEntry(ScriptEngine iv, EventManager em) {
this.iv = iv;
this.em = em;
}
public NashornScriptEngine iv;
public ScriptEngine iv;
public EventManager em;
}
@@ -58,7 +58,7 @@ public class EventScriptManager extends AbstractScriptManager {
super();
for (String script : scripts) {
if (!script.equals("")) {
NashornScriptEngine iv = getScriptEngine("event/" + script + ".js");
ScriptEngine iv = getScriptEngine("event/" + script + ".js");
events.put(script, new EventEntry(iv, new EventManager(cserv, iv, script)));
}
}
@@ -83,7 +83,7 @@ public class EventScriptManager extends AbstractScriptManager {
for (EventEntry entry : events.values()) {
try {
entry.iv.put("em", entry.em);
entry.iv.invokeFunction("init", (Object) null);
((Invocable) entry.iv).invokeFunction("init", (Object) null);
} catch (Exception ex) {
Logger.getLogger(EventScriptManager.class.getName()).log(Level.SEVERE, null, ex);
System.out.println("Error on script: " + entry.em.getName());
@@ -102,7 +102,7 @@ public class EventScriptManager extends AbstractScriptManager {
Channel cserv = eventEntries.iterator().next().getValue().em.getChannelServer();
for (Entry<String, EventEntry> entry : eventEntries) {
String script = entry.getKey();
NashornScriptEngine iv = getScriptEngine("event/" + script + ".js");
ScriptEngine iv = getScriptEngine("event/" + script + ".js");
events.put(script, new EventEntry(iv, new EventManager(cserv, iv, script)));
}
}

View File

@@ -23,15 +23,16 @@ package scripting.map;
import client.MapleCharacter;
import client.MapleClient;
import java.lang.reflect.UndeclaredThrowableException;
import java.util.HashMap;
import java.util.Map;
import scripting.AbstractScriptManager;
import tools.FilePrinter;
import javax.script.Invocable;
import javax.script.ScriptEngineFactory;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import jdk.nashorn.api.scripting.NashornScriptEngine;
import scripting.AbstractScriptManager;
import tools.FilePrinter;
import java.lang.reflect.UndeclaredThrowableException;
import java.util.HashMap;
import java.util.Map;
public class MapScriptManager extends AbstractScriptManager {
@@ -41,12 +42,12 @@ public class MapScriptManager extends AbstractScriptManager {
return instance;
}
private Map<String, NashornScriptEngine> scripts = new HashMap<>();
private ScriptEngineFactory sef;
private Map<String, Invocable> scripts = new HashMap<>();
private final ScriptEngineFactory sef;
private MapScriptManager() {
ScriptEngineManager sem = new ScriptEngineManager();
sef = sem.getEngineByName("javascript").getFactory();
sef = sem.getEngineByName("graal.js").getFactory();
}
public void reloadScripts() {
@@ -64,7 +65,7 @@ public class MapScriptManager extends AbstractScriptManager {
}
}
NashornScriptEngine iv = scripts.get(mapScriptPath);
Invocable iv = scripts.get(mapScriptPath);
if (iv != null) {
try {
iv.invokeFunction("start", new MapScriptMethods(c));
@@ -75,13 +76,13 @@ public class MapScriptManager extends AbstractScriptManager {
}
try {
iv = getScriptEngine("map/" + mapScriptPath + ".js");
iv = (Invocable) getScriptEngine("map/" + mapScriptPath + ".js");
if (iv == null) {
return false;
}
scripts.put(mapScriptPath, iv);
iv.invokeFunction("start", new MapScriptMethods(c));
((Invocable) iv).invokeFunction("start", new MapScriptMethods(c));
return true;
} catch (final UndeclaredThrowableException | ScriptException ute) {
FilePrinter.printError(FilePrinter.MAP_SCRIPT + mapScriptPath + ".txt", ute);

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);

View File

@@ -22,35 +22,37 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
package scripting.portal;
import client.MapleClient;
import java.lang.reflect.UndeclaredThrowableException;
import java.util.HashMap;
import java.util.Map;
import javax.script.ScriptEngineFactory;
import javax.script.ScriptEngineManager;
import jdk.nashorn.api.scripting.NashornScriptEngine;
import scripting.AbstractScriptManager;
import server.maps.MaplePortal;
import tools.FilePrinter;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineFactory;
import javax.script.ScriptEngineManager;
import java.lang.reflect.UndeclaredThrowableException;
import java.util.HashMap;
import java.util.Map;
public class PortalScriptManager extends AbstractScriptManager {
private static PortalScriptManager instance = new PortalScriptManager();
private static final PortalScriptManager instance = new PortalScriptManager();
public static PortalScriptManager getInstance() {
return instance;
}
private Map<String, NashornScriptEngine> scripts = new HashMap<>();
private ScriptEngineFactory sef;
private Map<String, ScriptEngine> scripts = new HashMap<>();
private final ScriptEngineFactory sef;
private PortalScriptManager() {
ScriptEngineManager sem = new ScriptEngineManager();
sef = sem.getEngineByName("javascript").getFactory();
sef = sem.getEngineByName("graal.js").getFactory();
}
private NashornScriptEngine getPortalScript(String scriptName) {
private ScriptEngine getPortalScript(String scriptName) {
String scriptPath = "portal/" + scriptName + ".js";
NashornScriptEngine iv = scripts.get(scriptPath);
ScriptEngine iv = scripts.get(scriptPath);
if (iv != null) {
return iv;
}
@@ -66,9 +68,9 @@ public class PortalScriptManager extends AbstractScriptManager {
public boolean executePortalScript(MaplePortal portal, MapleClient c) {
try {
NashornScriptEngine iv = getPortalScript(portal.getScriptName());
ScriptEngine iv = getPortalScript(portal.getScriptName());
if (iv != null) {
boolean couldWarp = (boolean) iv.invokeFunction("enter", new PortalPlayerInteraction(c, portal));
boolean couldWarp = (boolean) ((Invocable) iv).invokeFunction("enter", new PortalPlayerInteraction(c, portal));
return couldWarp;
}
} catch (UndeclaredThrowableException ute) {

View File

@@ -21,84 +21,84 @@
*/
package scripting.quest;
import java.lang.reflect.UndeclaredThrowableException;
import java.util.HashMap;
import java.util.Map;
import jdk.nashorn.api.scripting.NashornScriptEngine;
import client.MapleClient;
import client.MapleQuestStatus;
import constants.game.GameConstants;
import scripting.AbstractScriptManager;
import server.quest.MapleQuest;
import tools.FilePrinter;
import constants.game.GameConstants;
import client.MapleClient;
import client.MapleQuestStatus;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import java.lang.reflect.UndeclaredThrowableException;
import java.util.HashMap;
import java.util.Map;
/**
*
* @author RMZero213
*/
public class QuestScriptManager extends AbstractScriptManager {
private static QuestScriptManager instance = new QuestScriptManager();
private static QuestScriptManager instance = new QuestScriptManager();
public static QuestScriptManager getInstance() {
return instance;
}
private Map<MapleClient, QuestActionManager> qms = new HashMap<>();
private Map<MapleClient, NashornScriptEngine> scripts = new HashMap<>();
private NashornScriptEngine getQuestScriptEngine(MapleClient c, short questid) {
NashornScriptEngine iv = getScriptEngine("quest/" + questid + ".js", c);
if (iv == null && GameConstants.isMedalQuest(questid)) {
iv = getScriptEngine("quest/medalQuest.js", c); // start generic medal quest
}
private Map<MapleClient, ScriptEngine> scripts = new HashMap<>();
return iv;
private ScriptEngine getQuestScriptEngine(MapleClient c, short questid) {
ScriptEngine iv = getScriptEngine("quest/" + questid + ".js", c);
if (iv == null && GameConstants.isMedalQuest(questid)) {
iv = getScriptEngine("quest/medalQuest.js", c); // start generic medal quest
}
return iv;
}
public void start(MapleClient c, short questid, int npc) {
MapleQuest quest = MapleQuest.getInstance(questid);
try {
QuestActionManager qm = new QuestActionManager(c, questid, npc, true);
if (qms.containsKey(c)) {
return;
}
if(c.canClickNPC()) {
qms.put(c, qm);
if (!quest.hasScriptRequirement(false)) { // lack of scripted quest checks found thanks to Mali, Resinate
qm.dispose();
return;
}
NashornScriptEngine iv = getQuestScriptEngine(c, questid);
if (iv == null) {
FilePrinter.printError(FilePrinter.QUEST_UNCODED, "START Quest " + questid + " is uncoded.");
qm.dispose();
return;
}
iv.put("qm", qm);
scripts.put(c, iv);
c.setClickedNPC();
iv.invokeFunction("start", (byte) 1, (byte) 0, 0);
}
} catch (final UndeclaredThrowableException ute) {
FilePrinter.printError(FilePrinter.QUEST + questid + ".txt", ute);
dispose(c);
} catch (final Throwable t) {
FilePrinter.printError(FilePrinter.QUEST + getQM(c).getQuest() + ".txt", t);
dispose(c);
MapleQuest quest = MapleQuest.getInstance(questid);
try {
QuestActionManager qm = new QuestActionManager(c, questid, npc, true);
if (qms.containsKey(c)) {
return;
}
if (c.canClickNPC()) {
qms.put(c, qm);
if (!quest.hasScriptRequirement(false)) { // lack of scripted quest checks found thanks to Mali, Resinate
qm.dispose();
return;
}
}
ScriptEngine iv = getQuestScriptEngine(c, questid);
if (iv == null) {
FilePrinter.printError(FilePrinter.QUEST_UNCODED, "START Quest " + questid + " is uncoded.");
qm.dispose();
return;
}
iv.put("qm", qm);
scripts.put(c, iv);
c.setClickedNPC();
((Invocable) iv).invokeFunction("start", (byte) 1, (byte) 0, 0);
}
} catch (final UndeclaredThrowableException ute) {
FilePrinter.printError(FilePrinter.QUEST + questid + ".txt", ute);
dispose(c);
} catch (final Throwable t) {
FilePrinter.printError(FilePrinter.QUEST + getQM(c).getQuest() + ".txt", t);
dispose(c);
}
}
public void start(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("start", mode, type, selection);
((Invocable) iv).invokeFunction("start", mode, type, selection);
} catch (final UndeclaredThrowableException ute) {
FilePrinter.printError(FilePrinter.QUEST + getQM(c).getQuest() + ".txt", ute);
dispose(c);
@@ -110,51 +110,51 @@ public class QuestScriptManager extends AbstractScriptManager {
}
public void end(MapleClient c, short questid, int npc) {
MapleQuest quest = MapleQuest.getInstance(questid);
if (!c.getPlayer().getQuest(quest).getStatus().equals(MapleQuestStatus.Status.STARTED) || !c.getPlayer().getMap().containsNPC(npc)) {
dispose(c);
return;
}
try {
QuestActionManager qm = new QuestActionManager(c, questid, npc, false);
if (qms.containsKey(c)) {
return;
}
if(c.canClickNPC()){
qms.put(c, qm);
if (!quest.hasScriptRequirement(true)) {
qm.dispose();
return;
}
NashornScriptEngine iv = getQuestScriptEngine(c, questid);
if (iv == null) {
FilePrinter.printError(FilePrinter.QUEST_UNCODED, "END Quest " + questid + " is uncoded.");
qm.dispose();
return;
}
iv.put("qm", qm);
scripts.put(c, iv);
c.setClickedNPC();
iv.invokeFunction("end", (byte) 1, (byte) 0, 0);
}
} catch (final UndeclaredThrowableException ute) {
FilePrinter.printError(FilePrinter.QUEST + questid + ".txt", ute);
dispose(c);
} catch (final Throwable t) {
FilePrinter.printError(FilePrinter.QUEST + getQM(c).getQuest() + ".txt", t);
dispose(c);
}
}
MapleQuest quest = MapleQuest.getInstance(questid);
if (!c.getPlayer().getQuest(quest).getStatus().equals(MapleQuestStatus.Status.STARTED) || !c.getPlayer().getMap().containsNPC(npc)) {
dispose(c);
return;
}
try {
QuestActionManager qm = new QuestActionManager(c, questid, npc, false);
if (qms.containsKey(c)) {
return;
}
if (c.canClickNPC()) {
qms.put(c, qm);
if (!quest.hasScriptRequirement(true)) {
qm.dispose();
return;
}
ScriptEngine iv = getQuestScriptEngine(c, questid);
if (iv == null) {
FilePrinter.printError(FilePrinter.QUEST_UNCODED, "END Quest " + questid + " is uncoded.");
qm.dispose();
return;
}
iv.put("qm", qm);
scripts.put(c, iv);
c.setClickedNPC();
((Invocable) iv).invokeFunction("end", (byte) 1, (byte) 0, 0);
}
} catch (final UndeclaredThrowableException ute) {
FilePrinter.printError(FilePrinter.QUEST + questid + ".txt", ute);
dispose(c);
} catch (final Throwable t) {
FilePrinter.printError(FilePrinter.QUEST + getQM(c).getQuest() + ".txt", t);
dispose(c);
}
}
public void end(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("end", mode, type, selection);
((Invocable) iv).invokeFunction("end", mode, type, selection);
} catch (final UndeclaredThrowableException ute) {
FilePrinter.printError(FilePrinter.QUEST + getQM(c).getQuest() + ".txt", ute);
dispose(c);
@@ -166,34 +166,34 @@ public class QuestScriptManager extends AbstractScriptManager {
}
public void raiseOpen(MapleClient c, short questid, int npc) {
try {
QuestActionManager qm = new QuestActionManager(c, questid, npc, true);
if (qms.containsKey(c)) {
return;
}
if(c.canClickNPC()) {
qms.put(c, qm);
NashornScriptEngine iv = getQuestScriptEngine(c, questid);
if (iv == null) {
//FilePrinter.printError(FilePrinter.QUEST_UNCODED, "RAISE Quest " + questid + " is uncoded.");
qm.dispose();
return;
}
iv.put("qm", qm);
scripts.put(c, iv);
c.setClickedNPC();
iv.invokeFunction("raiseOpen");
}
} catch (final UndeclaredThrowableException ute) {
FilePrinter.printError(FilePrinter.QUEST + questid + ".txt", ute);
dispose(c);
} catch (final Throwable t) {
FilePrinter.printError(FilePrinter.QUEST + getQM(c).getQuest() + ".txt", t);
dispose(c);
try {
QuestActionManager qm = new QuestActionManager(c, questid, npc, true);
if (qms.containsKey(c)) {
return;
}
}
if (c.canClickNPC()) {
qms.put(c, qm);
ScriptEngine iv = getQuestScriptEngine(c, questid);
if (iv == null) {
//FilePrinter.printError(FilePrinter.QUEST_UNCODED, "RAISE Quest " + questid + " is uncoded.");
qm.dispose();
return;
}
iv.put("qm", qm);
scripts.put(c, iv);
c.setClickedNPC();
((Invocable) iv).invokeFunction("raiseOpen");
}
} catch (final UndeclaredThrowableException ute) {
FilePrinter.printError(FilePrinter.QUEST + questid + ".txt", ute);
dispose(c);
} catch (final Throwable t) {
FilePrinter.printError(FilePrinter.QUEST + getQM(c).getQuest() + ".txt", t);
dispose(c);
}
}
public void dispose(QuestActionManager qm, MapleClient c) {
qms.remove(c);

View File

@@ -28,7 +28,6 @@ import client.inventory.Item;
import client.inventory.MapleInventoryType;
import config.YamlConfig;
import constants.inventory.ItemConstants;
import jdk.nashorn.api.scripting.NashornScriptEngine;
import scripting.AbstractPlayerInteraction;
import scripting.event.EventInstanceManager;
import scripting.event.EventManager;
@@ -44,6 +43,8 @@ import server.partyquest.MapleCarnivalFactory;
import server.partyquest.MapleCarnivalFactory.MCSkill;
import tools.MaplePacketCreator;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptException;
import java.awt.*;
import java.util.ArrayList;
@@ -59,10 +60,10 @@ import java.util.logging.Logger;
*/
public class ReactorActionManager extends AbstractPlayerInteraction {
private MapleReactor reactor;
private NashornScriptEngine iv;
private ScriptEngine iv;
private ScheduledFuture<?> sprayTask = null;
public ReactorActionManager(MapleClient c, MapleReactor reactor, NashornScriptEngine iv) {
public ReactorActionManager(MapleClient c, MapleReactor reactor, ScriptEngine iv) {
super(c);
this.reactor = reactor;
this.iv = iv;
@@ -319,7 +320,7 @@ public class ReactorActionManager extends AbstractPlayerInteraction {
public ScheduledFuture<?> schedule(final String methodName, final EventInstanceManager eim, long delay) {
return TimerManager.getInstance().schedule(() -> {
try {
iv.invokeFunction(methodName, eim);
((Invocable) iv).invokeFunction(methodName, eim);
} catch (ScriptException | NoSuchMethodException ex) {
Logger.getLogger(EventManager.class.getName()).log(Level.SEVERE, null, ex);
}
@@ -329,7 +330,7 @@ public class ReactorActionManager extends AbstractPlayerInteraction {
public ScheduledFuture<?> scheduleAtTimestamp(final String methodName, long timestamp) {
return TimerManager.getInstance().scheduleAtTimestamp(() -> {
try {
iv.invokeFunction(methodName, (Object) null);
((Invocable) iv).invokeFunction(methodName, (Object) null);
} catch (ScriptException | NoSuchMethodException ex) {
Logger.getLogger(EventManager.class.getName()).log(Level.SEVERE, null, ex);
}

View File

@@ -22,13 +22,14 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
package scripting.reactor;
import client.MapleClient;
import jdk.nashorn.api.scripting.NashornScriptEngine;
import scripting.AbstractScriptManager;
import server.maps.MapleReactor;
import server.maps.ReactorDropEntry;
import tools.DatabaseConnection;
import tools.FilePrinter;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptException;
import java.sql.Connection;
import java.sql.PreparedStatement;
@@ -53,12 +54,12 @@ public class ReactorScriptManager extends AbstractScriptManager {
public void onHit(MapleClient c, MapleReactor reactor) {
try {
NashornScriptEngine iv = getScriptEngine("reactor/" + reactor.getId() + ".js", c);
ScriptEngine iv = getScriptEngine("reactor/" + reactor.getId() + ".js", c);
if (iv == null) return;
ReactorActionManager rm = new ReactorActionManager(c, reactor, iv);
iv.put("rm", rm);
iv.invokeFunction("hit");
((Invocable) iv).invokeFunction("hit");
} catch (final NoSuchMethodException e) {} //do nothing, hit is OPTIONAL
catch (final ScriptException | NullPointerException e) {
@@ -68,12 +69,12 @@ public class ReactorScriptManager extends AbstractScriptManager {
public void act(MapleClient c, MapleReactor reactor) {
try {
NashornScriptEngine iv = getScriptEngine("reactor/" + reactor.getId() + ".js", c);
ScriptEngine iv = getScriptEngine("reactor/" + reactor.getId() + ".js", c);
if (iv == null) return;
ReactorActionManager rm = new ReactorActionManager(c, reactor, iv);
iv.put("rm", rm);
iv.invokeFunction("act");
((Invocable) iv).invokeFunction("act");
} catch (final ScriptException | NoSuchMethodException | NullPointerException e) {
FilePrinter.printError(FilePrinter.REACTOR + reactor.getId() + ".txt", e);
}
@@ -114,15 +115,15 @@ public class ReactorScriptManager extends AbstractScriptManager {
private void touching(MapleClient c, MapleReactor reactor, boolean touching) {
try {
NashornScriptEngine iv = getScriptEngine("reactor/" + reactor.getId() + ".js", c);
ScriptEngine iv = getScriptEngine("reactor/" + reactor.getId() + ".js", c);
if (iv == null) return;
ReactorActionManager rm = new ReactorActionManager(c, reactor, iv);
iv.put("rm", rm);
if (touching) {
iv.invokeFunction("touch");
((Invocable) iv).invokeFunction("touch");
} else {
iv.invokeFunction("untouch");
((Invocable) iv).invokeFunction("untouch");
}
} catch (final ScriptException | NoSuchMethodException | NullPointerException ute) {
FilePrinter.printError(FilePrinter.REACTOR + reactor.getId() + ".txt", ute);