Changed the scripting language of Holocore to JavaScript. All scripts have been rewritten.

This commit is contained in:
Ziggeh
2015-07-28 04:46:53 +02:00
parent 02f156c9b7
commit 019e73ead6
54 changed files with 237 additions and 332 deletions
+27 -90
View File
@@ -1,100 +1,37 @@
/*******************************************************************************
* Copyright (c) 2015 /// Project SWG /// www.projectswg.com
*
* ProjectSWG is the first NGE emulator for Star Wars Galaxies founded on
* July 7th, 2011 after SOE announced the official shutdown of Star Wars Galaxies.
* Our goal is to create an emulator which will provide a server for players to
* continue playing a game similar to the one they used to play. We are basing
* it on the final publish of the game prior to end-game events.
*
* This file is part of Holocore.
*
* --------------------------------------------------------------------------------
*
* Holocore 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, either version 3 of the
* License, or (at your option) any later version.
*
* Holocore 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 Holocore. If not, see <http://www.gnu.org/licenses/>
******************************************************************************/
package utilities;
import org.python.core.Py;
import org.python.core.PyObject;
import org.python.core.PySystemState;
import org.python.util.PythonInterpreter;
import resources.server_info.Config;
import java.io.FileReader;
import java.io.File;
import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
public class Scripts {
private static Scripts instance = new Scripts();
private static final String SCRIPTS = "scripts/";
private final PythonInterpreter interpreter;
private Scripts() {
interpreter = new PythonInterpreter();
}
public static PyObject execute(String script, String method, Object ... args) {
if (!exists(script))
return null;
instance.interpreter.execfile(SCRIPTS + script);
return instance.interpreter.get(method).__call__(Py.javas2pys(args));
}
public static boolean exists(String script) {
return (new File(SCRIPTS + script).exists());
}
public static void initialize(Config config) {
PySystemState systemState = instance.interpreter.getSystemState();
systemState.setCurrentWorkingDir(systemState.getCurrentWorkingDir() + "/");
Scripts.initScripts(systemState);
}
private static void initScripts(PySystemState systemState) {
File scripts = new File(systemState.getCurrentWorkingDir());
final int length = scripts.getAbsolutePath().length();
final PythonInterpreter interpreter = instance.interpreter;
private static final String EXTENSION = ".js";
private static final ScriptEngine ENGINE = new ScriptEngineManager().getEngineByName("nashorn");
private static final Invocable INVOCABLE = (Invocable) ENGINE;
// Prevents instantiation.
private Scripts() {}
/**
* @param script name of the script, relative to the scripts folder.
* @param function name of the specific function within the script.
* @param args to pass to the function.
* @return whatever the function returns. If the function doesn't have a return statement, this method returns {@code null}.
* If an exception occurs, {@code null} is returned.
*/
@SuppressWarnings("unchecked")
public static <T> T invoke(String script, String function, Object... args) {
try {
Files.walkFileTree(scripts.toPath(), new FileVisitor<Path>() {
public FileVisitResult preVisitDirectory(Path path, BasicFileAttributes basicFileAttributes) throws IOException {
return FileVisitResult.CONTINUE;
}
public FileVisitResult visitFile(Path path, BasicFileAttributes basicFileAttributes) throws IOException {
String name = path.toFile().getAbsolutePath();
if (name.endsWith(".py"))
interpreter.execfile(name.substring(length + 1));
return FileVisitResult.CONTINUE;
}
public FileVisitResult visitFileFailed(Path path, IOException e) throws IOException {
return FileVisitResult.CONTINUE;
}
public FileVisitResult postVisitDirectory(Path path, IOException e) throws IOException {
return FileVisitResult.CONTINUE;
}
});
} catch (IOException e) {
e.printStackTrace();
ENGINE.eval(new FileReader(SCRIPTS + script + EXTENSION));
return (T) INVOCABLE.invokeFunction(function, args);
} catch(Throwable t) {
t.printStackTrace();
return null;
}
}
public static void cleanup() {
instance.interpreter.cleanup();
}
}