Files
sweetgum-server/src/main/java/scripting/AbstractScriptManager.java
2022-08-11 19:01:40 +02:00

93 lines
3.1 KiB
Java

/*
This file is part of the OdinMS Maple Story Server
Copyright (C) 2008 Patrick Huy <patrick.huy@frz.cc>
Matthias Butz <matze@odinms.de>
Jan Christian Meyer <vimes@odinms.de>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation version 3 as published by
the Free Software Foundation. You may not use, modify or distribute
this program under any other version of the GNU Affero General Public
License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package scripting;
import client.Client;
import com.oracle.truffle.js.scriptengine.GraalJSScriptEngine;
import constants.string.CharsetConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.script.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
/**
* @author Matze
*/
public abstract class AbstractScriptManager {
private static final Logger log = LoggerFactory.getLogger(AbstractScriptManager.class);
private final ScriptEngineFactory sef;
protected AbstractScriptManager() {
sef = new ScriptEngineManager().getEngineByName("graal.js").getFactory();
}
protected ScriptEngine getInvocableScriptEngine(String path) {
Path scriptFile = Path.of("scripts", path);
if (!Files.exists(scriptFile)) {
return null;
}
ScriptEngine engine = sef.getScriptEngine();
if (!(engine instanceof GraalJSScriptEngine graalScriptEngine)) {
throw new IllegalStateException("ScriptEngineFactory did not provide a GraalJSScriptEngine");
}
enableScriptHostAccess(graalScriptEngine);
try (BufferedReader br = Files.newBufferedReader(scriptFile, CharsetConstants.CHARSET)) {
engine.eval(br);
} catch (final ScriptException | IOException t) {
log.warn("Exception during script eval for file: {}", path, t);
return null;
}
return graalScriptEngine;
}
protected ScriptEngine getInvocableScriptEngine(String path, Client c) {
ScriptEngine engine = c.getScriptEngine("scripts/" + path);
if (engine == null) {
engine = getInvocableScriptEngine(path);
c.setScriptEngine(path, engine);
}
return engine;
}
/**
* Allow usage of "Java.type()" in script to look up host class
*/
private void enableScriptHostAccess(GraalJSScriptEngine engine) {
Bindings bindings = engine.getBindings(ScriptContext.ENGINE_SCOPE);
bindings.put("polyglot.js.allowHostAccess", true);
bindings.put("polyglot.js.allowHostClassLookup", true);
}
protected void resetContext(String path, Client c) {
c.removeScriptEngine("scripts/" + path);
}
}