diff --git a/src/resources/commands/callbacks/ServerCmdCallback.java b/src/resources/commands/callbacks/ServerCmdCallback.java index e65263d0..fb03e62f 100644 --- a/src/resources/commands/callbacks/ServerCmdCallback.java +++ b/src/resources/commands/callbacks/ServerCmdCallback.java @@ -44,7 +44,7 @@ import resources.sui.SuiListBox; import resources.sui.SuiListBox.ListBoxType; import resources.sui.SuiMessageBox; import resources.sui.SuiMessageBox.MessageBoxType; -import resources.utilities.Scripts; +import utilities.Scripts; import services.galaxy.GalacticManager; public class ServerCmdCallback implements ICmdCallback { diff --git a/src/resources/utilities/Scripts.java b/src/resources/utilities/Scripts.java deleted file mode 100644 index 204721a4..00000000 --- a/src/resources/utilities/Scripts.java +++ /dev/null @@ -1,54 +0,0 @@ -/*********************************************************************************** -* 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 . * -* * -***********************************************************************************/ -package resources.utilities; - -import java.io.File; - -import org.python.core.Py; -import org.python.util.PythonInterpreter; - -public final class Scripts { - private static final String SCRIPTS_PATH = "scripts/"; - - public static void execute(String script, String method, Object... args) { - PythonInterpreter interp; - - if (!scriptExists(script)) - return; // Ziggy: For now, this method gives no indication as to whether the script that was called actually was called or not. - - interp = new PythonInterpreter(); - - interp.execfile(SCRIPTS_PATH + script); - interp.get(method).__call__(Py.javas2pys(args)); - } - - private static boolean scriptExists(String file) { - return new File(SCRIPTS_PATH + file).exists(); - } - -} diff --git a/src/services/CoreManager.java b/src/services/CoreManager.java index ad04e1fd..ee1fb7fe 100644 --- a/src/services/CoreManager.java +++ b/src/services/CoreManager.java @@ -56,6 +56,7 @@ import resources.control.Manager; import resources.control.ServerStatus; import resources.server_info.Config; import services.galaxy.GalacticManager; +import utilities.Scripts; public class CoreManager extends Manager { @@ -103,12 +104,14 @@ public class CoreManager extends Manager { registerForIntent(ServerManagementIntent.TYPE); packetDebug = getConfig(ConfigFile.PRIMARY).getBoolean("PACKET-DEBUG", false); initializePacketOutput(); + Scripts.initialize(getConfig(ConfigFile.PRIMARY)); return galaxy != null && super.initialize(); } @Override public boolean stop() { galaxy.setStatus(GalaxyStatus.LOCKED); + Scripts.cleanup(); return super.stop(); } diff --git a/src/services/chat/ChatService.java b/src/services/chat/ChatService.java index 665fb168..ceb2f39e 100644 --- a/src/services/chat/ChatService.java +++ b/src/services/chat/ChatService.java @@ -62,6 +62,7 @@ import resources.encodables.player.Mail; import resources.objects.SWGObject; import resources.player.Player; import resources.server_info.CachedObjectDatabase; +import resources.server_info.Log; import resources.server_info.ObjectDatabase; import resources.server_info.ObjectDatabase.Traverser; import services.player.PlayerManager; diff --git a/src/services/commands/CommandService.java b/src/services/commands/CommandService.java index c6980cb8..4e11eb70 100644 --- a/src/services/commands/CommandService.java +++ b/src/services/commands/CommandService.java @@ -48,7 +48,7 @@ import resources.objects.SWGObject; import resources.player.AccessLevel; import resources.player.Player; import resources.server_info.Log; -import resources.utilities.Scripts; +import utilities.Scripts; import services.galaxy.GalacticManager; public class CommandService extends Service { @@ -74,8 +74,7 @@ public class CommandService extends Service { if (i instanceof GalacticPacketIntent) { GalacticPacketIntent gpi = (GalacticPacketIntent) i; Packet p = gpi.getPacket(); - long netId = gpi.getNetworkId(); - Player player = gpi.getPlayerManager().getPlayerFromNetworkId(netId); + Player player = gpi.getPlayerManager().getPlayerFromNetworkId(gpi.getNetworkId()); if (player != null) { if (p instanceof CommandQueueEnqueue) { CommandQueueEnqueue controller = (CommandQueueEnqueue) p; diff --git a/src/utilities/Scripts.java b/src/utilities/Scripts.java new file mode 100644 index 00000000..584ca770 --- /dev/null +++ b/src/utilities/Scripts.java @@ -0,0 +1,100 @@ +/******************************************************************************* + * 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 + ******************************************************************************/ + +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.File; +import java.io.IOException; +import java.nio.file.*; +import java.nio.file.attribute.BasicFileAttributes; + +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(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"); + 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; + + try { + Files.walkFileTree(scripts.toPath(), new FileVisitor() { + 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(); + } + } + + public static void cleanup() { + instance.interpreter.cleanup(); + } +}