diff --git a/L2J_Mobius_1.0_Ertheia/dist/game/config/ScriptEngine.ini b/L2J_Mobius_1.0_Ertheia/dist/game/config/ScriptEngine.ini index 2d1e972e72..5004dca9fd 100644 --- a/L2J_Mobius_1.0_Ertheia/dist/game/config/ScriptEngine.ini +++ b/L2J_Mobius_1.0_Ertheia/dist/game/config/ScriptEngine.ini @@ -1,23 +1,23 @@ -############################################################################### -# Properties retrived with System.getProperty(String) can be used as values +# --------------------------------------------------------------------------- +# Script Engine Settings +# --------------------------------------------------------------------------- +# Properties retrieved with System.getProperty(String) can be used as values # by enclosing the property name with %. Eg.: %java.class.path% -############################################################################### - # The parent class loader for isolated script class loaders. # When this property is not specified, has an invalid value or is a class name which could not be found, the System classloader is used. # Values: System, ThreadContext or a fully qualified java class name -language.Java.classloader=System -#language.Java.classloader=ThreadContext +classloader=System +#classloader=ThreadContext -# Source compatibility -language.Java.source=1.8 +# Source compatibility. +source=1.8 # The java sourcepath, when you have a different datapack root, you must change this too. -language.Java.sourcepath=data/scripts +sourcepath=data/scripts -# The java classpath -language.Java.cp=%java.class.path% +# The java classpath. +cp=%java.class.path% -# The debug informations to generate for compiled class files -language.Java.g=source,lines,vars \ No newline at end of file +# The debug informations to generate for compiled class files. +g=source,lines,vars \ No newline at end of file diff --git a/L2J_Mobius_1.0_Ertheia/java/com/l2jmobius/gameserver/scripting/AbstractExecutionContext.java b/L2J_Mobius_1.0_Ertheia/java/com/l2jmobius/gameserver/scripting/AbstractExecutionContext.java deleted file mode 100644 index 296b54f06b..0000000000 --- a/L2J_Mobius_1.0_Ertheia/java/com/l2jmobius/gameserver/scripting/AbstractExecutionContext.java +++ /dev/null @@ -1,75 +0,0 @@ -/* - * This file is part of the L2J Mobius project. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * 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 - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package com.l2jmobius.gameserver.scripting; - -import java.nio.file.Path; -import java.util.HashMap; -import java.util.Map; - -/** - * @author HorridoJoho - * @param - */ -public abstract class AbstractExecutionContext implements IExecutionContext -{ - private final T _engine; - private final Map _properties; - private volatile Path _currentExecutingScipt; - - protected AbstractExecutionContext(T engine) - { - if (engine == null) - { - throw new IllegalArgumentException(); - } - _engine = engine; - _properties = new HashMap<>(); - } - - protected final void setCurrentExecutingScript(Path currentExecutingScript) - { - _currentExecutingScipt = currentExecutingScript; - } - - @Override - public final String setProperty(String key, String value) - { - return _properties.put(key, value); - } - - @Override - public final String getProperty(String key) - { - if (!_properties.containsKey(key)) - { - return _engine.getProperty(key); - } - return _properties.get(key); - } - - @Override - public final Path getCurrentExecutingScript() - { - return _currentExecutingScipt; - } - - @Override - public final T getScriptingEngine() - { - return _engine; - } -} diff --git a/L2J_Mobius_1.0_Ertheia/java/com/l2jmobius/gameserver/scripting/AbstractScriptingEngine.java b/L2J_Mobius_1.0_Ertheia/java/com/l2jmobius/gameserver/scripting/AbstractScriptingEngine.java deleted file mode 100644 index 90900438ec..0000000000 --- a/L2J_Mobius_1.0_Ertheia/java/com/l2jmobius/gameserver/scripting/AbstractScriptingEngine.java +++ /dev/null @@ -1,74 +0,0 @@ -/* - * This file is part of the L2J Mobius project. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * 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 - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package com.l2jmobius.gameserver.scripting; - -import java.util.Arrays; -import java.util.HashMap; -import java.util.Map; - -/** - * @author HorridoJoho - */ -public abstract class AbstractScriptingEngine implements IScriptingEngine -{ - private final String _engineName; - private final String _engineVersion; - private final String[] _commonFileExtensions; - private final Map _properties; - - protected AbstractScriptingEngine(String engineName, String engineVersion, String... commonFileExtensions) - { - if ((engineName == null) || engineName.isEmpty() || (engineVersion == null) || engineVersion.isEmpty() || (commonFileExtensions == null) || (commonFileExtensions.length == 0)) - { - throw new IllegalArgumentException(); - } - _engineName = engineName; - _engineVersion = engineVersion; - _commonFileExtensions = commonFileExtensions; - _properties = new HashMap<>(); - } - - @Override - public final String setProperty(String key, String value) - { - return _properties.put(key, value); - } - - @Override - public final String getProperty(String key) - { - return _properties.get(key); - } - - @Override - public final String getEngineName() - { - return _engineName; - } - - @Override - public final String getEngineVersion() - { - return _engineVersion; - } - - @Override - public final String[] getCommonFileExtensions() - { - return Arrays.copyOf(_commonFileExtensions, _commonFileExtensions.length); - } -} diff --git a/L2J_Mobius_1.0_Ertheia/java/com/l2jmobius/gameserver/scripting/IExecutionContext.java b/L2J_Mobius_1.0_Ertheia/java/com/l2jmobius/gameserver/scripting/IExecutionContext.java deleted file mode 100644 index eaf4860b8a..0000000000 --- a/L2J_Mobius_1.0_Ertheia/java/com/l2jmobius/gameserver/scripting/IExecutionContext.java +++ /dev/null @@ -1,71 +0,0 @@ -/* - * This file is part of the L2J Mobius project. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * 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 - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package com.l2jmobius.gameserver.scripting; - -import java.nio.file.Path; -import java.util.Map; -import java.util.Map.Entry; - -/** - * @author HorridoJoho - */ -public interface IExecutionContext -{ - /** - * Properties set here override the settings from the IScriptEngine
- * this class was created from. - * @param key the key - * @param value the value - * @return the previous value, or null when this key was not present before - */ - String setProperty(String key, String value); - - /** - * Executes all script in the iterable. - * @param sourcePaths the scripts to execute - * @return map of failed executions, Path=source file Throwable=thrown exception - * @throws Exception preparation for script execution failed - */ - Map executeScripts(Iterable sourcePaths) throws Exception; - - /** - * Executes a single file. - * @param sourcePath the script to execute - * @return entry of failed execution, Path=source file Throwable=thrown exception - * @throws Exception preparation for script execution failed - */ - Entry executeScript(Path sourcePath) throws Exception; - - /** - * Method to get the specified property value. - * @param key the key - * @return the value, or null if the key is not present - */ - String getProperty(String key); - - /** - * Method to get the current executing script file. - * @return the currently executing script file, null if non - */ - Path getCurrentExecutingScript(); - - /** - * Method to get the script engine this execution context belongs to. - * @return the script engine this execution context belongs to - */ - IScriptingEngine getScriptingEngine(); -} diff --git a/L2J_Mobius_1.0_Ertheia/java/com/l2jmobius/gameserver/scripting/IScriptingEngine.java b/L2J_Mobius_1.0_Ertheia/java/com/l2jmobius/gameserver/scripting/IScriptingEngine.java deleted file mode 100644 index 883172cce1..0000000000 --- a/L2J_Mobius_1.0_Ertheia/java/com/l2jmobius/gameserver/scripting/IScriptingEngine.java +++ /dev/null @@ -1,75 +0,0 @@ -/* - * This file is part of the L2J Mobius project. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * 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 - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package com.l2jmobius.gameserver.scripting; - -/** - * @author HorridoJoho - */ -public interface IScriptingEngine -{ - /** - * Sets script engine properties. The script values will be available
- * to the the insatnces created {@link IExecutionContext} implementation. - * @param key the key - * @param value the value - * @return the previous value, or null when this key was not present before - */ - String setProperty(String key, String value); - - /** - * Creates an execution context. - * @return the created execution context. - */ - IExecutionContext createExecutionContext(); - - /** - * Method to get the specified property value. - * @param key the key - * @return the value,or null if the key is not present - */ - String getProperty(String key); - - /** - * Method to get the engine name. - * @return the engine name - */ - String getEngineName(); - - /** - * Method to get the engine version. - * @return the engine version - */ - String getEngineVersion(); - - /** - * Method to get the scripting language name. - * @return the scripting engine name - */ - String getLanguageName(); - - /** - * Method to get the the language version. - * @return the language version - */ - String getLanguageVersion(); - - /** - * Method to retrive the commonly used file extensions for the language. - * @return the commonly used file extensions for the language - */ - String[] getCommonFileExtensions(); -} diff --git a/L2J_Mobius_1.0_Ertheia/java/com/l2jmobius/gameserver/scripting/ScriptEngineManager.java b/L2J_Mobius_1.0_Ertheia/java/com/l2jmobius/gameserver/scripting/ScriptEngineManager.java index f3d0215f94..3bbe843258 100644 --- a/L2J_Mobius_1.0_Ertheia/java/com/l2jmobius/gameserver/scripting/ScriptEngineManager.java +++ b/L2J_Mobius_1.0_Ertheia/java/com/l2jmobius/gameserver/scripting/ScriptEngineManager.java @@ -17,7 +17,6 @@ package com.l2jmobius.gameserver.scripting; import java.io.File; -import java.io.FileInputStream; import java.io.IOException; import java.nio.file.FileVisitResult; import java.nio.file.Files; @@ -31,9 +30,6 @@ import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Map.Entry; -import java.util.Objects; -import java.util.Properties; -import java.util.ServiceLoader; import java.util.logging.Level; import java.util.logging.Logger; @@ -41,34 +37,27 @@ import org.w3c.dom.Document; import com.l2jmobius.Config; import com.l2jmobius.commons.util.IXmlReader; +import com.l2jmobius.gameserver.scripting.java.JavaExecutionContext; import com.l2jmobius.gameserver.scripting.java.JavaScriptingEngine; /** - * Caches script engines and provides functionality for executing and managing scripts. - * @author KenM, HorridoJoho + * @author Mobius */ public final class ScriptEngineManager implements IXmlReader { private static final Logger LOGGER = Logger.getLogger(ScriptEngineManager.class.getName()); + public static final Path SCRIPT_FOLDER = Config.SCRIPT_ROOT.toPath(); public static final Path MASTER_HANDLER_FILE = Paths.get(SCRIPT_FOLDER.toString(), "handlers", "MasterHandler.java"); public static final Path EFFECT_MASTER_HANDLER_FILE = Paths.get(SCRIPT_FOLDER.toString(), "handlers", "EffectMasterHandler.java"); public static final Path SKILL_CONDITION_HANDLER_FILE = Paths.get(SCRIPT_FOLDER.toString(), "handlers", "SkillConditionMasterHandler.java"); public static final Path CONDITION_HANDLER_FILE = Paths.get(SCRIPT_FOLDER.toString(), "handlers", "ConditionMasterHandler.java"); - private IExecutionContext _javaExecutionContext = null; - static final List _exclusions = new ArrayList<>(); + private static final JavaExecutionContext _javaExecutionContext = new JavaScriptingEngine().createExecutionContext(); + protected static final List _exclusions = new ArrayList<>(); protected ScriptEngineManager() { - final Properties props = loadProperties(); - - // Default java engine implementation - registerEngine(new JavaScriptingEngine(), props); - - // Load external script engines - ServiceLoader.load(IScriptingEngine.class).forEach(engine -> registerEngine(engine, props)); - // Load Scripts.xml load(); } @@ -143,72 +132,6 @@ public final class ScriptEngineManager implements IXmlReader } } - private Properties loadProperties() - { - Properties props = null; - try (FileInputStream fis = new FileInputStream("config/ScriptEngine.ini")) - { - props = new Properties(); - props.load(fis); - } - catch (Exception e) - { - props = null; - LOGGER.warning("Couldn't load ScriptEngine.ini: " + e.getMessage()); - } - return props; - } - - private void registerEngine(IScriptingEngine engine, Properties props) - { - maybeSetProperties("language." + engine.getLanguageName() + ".", props, engine); - _javaExecutionContext = engine.createExecutionContext(); - - LOGGER.info("ScriptEngine: " + engine.getEngineName() + " " + engine.getEngineVersion() + " (" + engine.getLanguageName() + " " + engine.getLanguageVersion() + ")"); - } - - private void maybeSetProperties(String propPrefix, Properties props, IScriptingEngine engine) - { - if (props == null) - { - return; - } - - for (Entry prop : props.entrySet()) - { - String key = (String) prop.getKey(); - String value = (String) prop.getValue(); - - if (key.startsWith(propPrefix)) - { - key = key.substring(propPrefix.length()); - if (value.startsWith("%") && value.endsWith("%")) - { - value = System.getProperty(value.substring(1, value.length() - 1)); - } - - engine.setProperty(key, value); - } - } - } - - public void executeScriptList() throws Exception - { - if (Config.ALT_DEV_NO_QUESTS) - { - return; - } - - final List files = new ArrayList<>(); - processDirectory(SCRIPT_FOLDER.toFile(), files); - - final Map invokationErrors = _javaExecutionContext.executeScripts(files); - for (Entry entry : invokationErrors.entrySet()) - { - LOGGER.log(Level.WARNING, "ScriptEngine: " + entry.getKey() + " failed execution!", entry.getValue()); - } - } - private void processDirectory(File dir, List files) { for (File file : dir.listFiles()) @@ -230,15 +153,12 @@ public final class ScriptEngineManager implements IXmlReader public void executeScript(Path sourceFile) throws Exception { - Objects.requireNonNull(sourceFile); - if (!sourceFile.isAbsolute()) { sourceFile = SCRIPT_FOLDER.resolve(sourceFile); } sourceFile = sourceFile.toAbsolutePath(); - Objects.requireNonNull(sourceFile, "ScriptFile: " + sourceFile + " does not have an extension to determine the script engine!"); final Entry error = _javaExecutionContext.executeScript(sourceFile); if (error != null) @@ -247,6 +167,23 @@ public final class ScriptEngineManager implements IXmlReader } } + public void executeScriptList() throws Exception + { + if (Config.ALT_DEV_NO_QUESTS) + { + return; + } + + final List files = new ArrayList<>(); + processDirectory(SCRIPT_FOLDER.toFile(), files); + + final Map invokationErrors = _javaExecutionContext.executeScripts(files); + for (Entry entry : invokationErrors.entrySet()) + { + LOGGER.log(Level.WARNING, "ScriptEngine: " + entry.getKey() + " failed execution!", entry.getValue()); + } + } + public Path getCurrentLoadingScript() { return _javaExecutionContext.getCurrentExecutingScript(); @@ -254,11 +191,11 @@ public final class ScriptEngineManager implements IXmlReader public static ScriptEngineManager getInstance() { - return SingletonHolder._instance; + return SingletonHolder.INSTANCE; } private static class SingletonHolder { - protected static final ScriptEngineManager _instance = new ScriptEngineManager(); + protected static final ScriptEngineManager INSTANCE = new ScriptEngineManager(); } } \ No newline at end of file diff --git a/L2J_Mobius_1.0_Ertheia/java/com/l2jmobius/gameserver/scripting/java/JavaCompilerException.java b/L2J_Mobius_1.0_Ertheia/java/com/l2jmobius/gameserver/scripting/java/JavaCompilerException.java deleted file mode 100644 index 241ffe91bd..0000000000 --- a/L2J_Mobius_1.0_Ertheia/java/com/l2jmobius/gameserver/scripting/java/JavaCompilerException.java +++ /dev/null @@ -1,28 +0,0 @@ -/* - * This file is part of the L2J Mobius project. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * 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 - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package com.l2jmobius.gameserver.scripting.java; - -/** - * @author HorridoJoho - */ -public final class JavaCompilerException extends RuntimeException -{ - public JavaCompilerException(String diagnostics) - { - super(diagnostics); - } -} diff --git a/L2J_Mobius_1.0_Ertheia/java/com/l2jmobius/gameserver/scripting/java/JavaExecutionContext.java b/L2J_Mobius_1.0_Ertheia/java/com/l2jmobius/gameserver/scripting/java/JavaExecutionContext.java index 70d4da0893..1eddebd693 100644 --- a/L2J_Mobius_1.0_Ertheia/java/com/l2jmobius/gameserver/scripting/java/JavaExecutionContext.java +++ b/L2J_Mobius_1.0_Ertheia/java/com/l2jmobius/gameserver/scripting/java/JavaExecutionContext.java @@ -22,9 +22,9 @@ import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.nio.charset.StandardCharsets; import java.nio.file.Path; +import java.util.ArrayList; import java.util.Arrays; -import java.util.LinkedHashMap; -import java.util.LinkedList; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; @@ -34,22 +34,20 @@ import org.openjavac.tools.Diagnostic; import org.openjavac.tools.DiagnosticCollector; import org.openjavac.tools.JavaFileObject; -import com.l2jmobius.gameserver.scripting.AbstractExecutionContext; import com.l2jmobius.gameserver.scripting.annotations.Disabled; /** * @author HorridoJoho */ -public final class JavaExecutionContext extends AbstractExecutionContext +public final class JavaExecutionContext extends JavaScriptingEngine { private static final Logger LOGGER = Logger.getLogger(JavaExecutionContext.class.getName()); - private static final List _options = new LinkedList<>(); + private static final List _options = new ArrayList<>(); + private static Path _currentExecutingScript; JavaExecutionContext(JavaScriptingEngine engine) { - super(engine); - // Set options. addOptionIfNotNull(_options, getProperty("source"), "-source"); addOptionIfNotNull(_options, getProperty("sourcepath"), "-sourcepath"); @@ -76,7 +74,7 @@ public final class JavaExecutionContext extends AbstractExecutionContext executeScripts(Iterable sourcePaths) throws Exception { final DiagnosticCollector fileManagerDiagnostics = new DiagnosticCollector<>(); final DiagnosticCollector compilationDiagnostics = new DiagnosticCollector<>(); - try (ScriptingFileManager fileManager = new ScriptingFileManager(getScriptingEngine().getCompiler().getStandardFileManager(fileManagerDiagnostics, null, StandardCharsets.UTF_8))) + try (ScriptingFileManager fileManager = new ScriptingFileManager(getCompiler().getStandardFileManager(fileManagerDiagnostics, null, StandardCharsets.UTF_8))) { // We really need an iterable of files or strings. - final List sourcePathStrings = new LinkedList<>(); + final List sourcePathStrings = new ArrayList<>(); for (Path sourcePath : sourcePaths) { sourcePathStrings.add(sourcePath.toString()); @@ -150,7 +147,7 @@ public final class JavaExecutionContext extends AbstractExecutionContext executionFailures = new LinkedHashMap<>(); + final Map executionFailures = new HashMap<>(); final Iterable compiledClasses = fileManager.getCompiledClasses(); for (Path sourcePath : sourcePaths) { @@ -199,7 +196,7 @@ public final class JavaExecutionContext extends AbstractExecutionContext executeScript(Path sourcePath) throws Exception { final Map executionFailures = executeScripts(Arrays.asList(sourcePath)); @@ -254,4 +250,9 @@ public final class JavaExecutionContext extends AbstractExecutionContext _properties = new HashMap<>(); + private final static JavaCompiler _compiler = JavacTool.create(); public JavaScriptingEngine() { - super("Java Engine", "10", "java"); - } - - private void determineCompilerOrThrow() - { - if (_compiler == null) + // Load config. + Properties props = new Properties(); + try (FileInputStream fis = new FileInputStream("config/ScriptEngine.ini")) { - _compiler = JavacTool.create(); + props.load(fis); + } + catch (Exception e) + { + LOGGER.warning("Could not load ScriptEngine.ini: " + e.getMessage()); } - if (_compiler == null) + // Set properties. + for (Entry prop : props.entrySet()) { - throw new IllegalStateException("No JavaCompiler service installed!"); - } - } - - private void ensureCompilerOrThrow() - { - if (_compiler == null) - { - synchronized (this) + String key = (String) prop.getKey(); + String value = (String) prop.getValue(); + + if (value.startsWith("%") && value.endsWith("%")) { - if (_compiler == null) - { - determineCompilerOrThrow(); - } + value = System.getProperty(value.substring(1, value.length() - 1)); } + + _properties.put(key, value); } } - JavaCompiler getCompiler() + public JavaExecutionContext createExecutionContext() { - return _compiler; - } - - @Override - public IExecutionContext createExecutionContext() - { - ensureCompilerOrThrow(); return new JavaExecutionContext(this); } - @Override - public String getLanguageName() + public final String getProperty(String key) { - return "Java"; + return _properties.get(key); } - @Override - public String getLanguageVersion() + public JavaCompiler getCompiler() { - ensureCompilerOrThrow(); - return Arrays.deepToString(_compiler.getSourceVersions().toArray(new SourceVersion[0])).replace("RELEASE_", ""); + return _compiler; } } \ No newline at end of file diff --git a/L2J_Mobius_1.0_Ertheia/java/com/l2jmobius/gameserver/scripting/java/ScriptingOutputFileObject.java b/L2J_Mobius_1.0_Ertheia/java/com/l2jmobius/gameserver/scripting/java/ScriptingOutputFileObject.java index 439cf5d260..9ab668887e 100644 --- a/L2J_Mobius_1.0_Ertheia/java/com/l2jmobius/gameserver/scripting/java/ScriptingOutputFileObject.java +++ b/L2J_Mobius_1.0_Ertheia/java/com/l2jmobius/gameserver/scripting/java/ScriptingOutputFileObject.java @@ -144,5 +144,4 @@ final class ScriptingOutputFileObject implements JavaFileObject { return null; } - } diff --git a/L2J_Mobius_2.5_Underground/dist/game/config/ScriptEngine.ini b/L2J_Mobius_2.5_Underground/dist/game/config/ScriptEngine.ini index 2d1e972e72..5004dca9fd 100644 --- a/L2J_Mobius_2.5_Underground/dist/game/config/ScriptEngine.ini +++ b/L2J_Mobius_2.5_Underground/dist/game/config/ScriptEngine.ini @@ -1,23 +1,23 @@ -############################################################################### -# Properties retrived with System.getProperty(String) can be used as values +# --------------------------------------------------------------------------- +# Script Engine Settings +# --------------------------------------------------------------------------- +# Properties retrieved with System.getProperty(String) can be used as values # by enclosing the property name with %. Eg.: %java.class.path% -############################################################################### - # The parent class loader for isolated script class loaders. # When this property is not specified, has an invalid value or is a class name which could not be found, the System classloader is used. # Values: System, ThreadContext or a fully qualified java class name -language.Java.classloader=System -#language.Java.classloader=ThreadContext +classloader=System +#classloader=ThreadContext -# Source compatibility -language.Java.source=1.8 +# Source compatibility. +source=1.8 # The java sourcepath, when you have a different datapack root, you must change this too. -language.Java.sourcepath=data/scripts +sourcepath=data/scripts -# The java classpath -language.Java.cp=%java.class.path% +# The java classpath. +cp=%java.class.path% -# The debug informations to generate for compiled class files -language.Java.g=source,lines,vars \ No newline at end of file +# The debug informations to generate for compiled class files. +g=source,lines,vars \ No newline at end of file diff --git a/L2J_Mobius_2.5_Underground/java/com/l2jmobius/gameserver/scripting/AbstractExecutionContext.java b/L2J_Mobius_2.5_Underground/java/com/l2jmobius/gameserver/scripting/AbstractExecutionContext.java deleted file mode 100644 index 296b54f06b..0000000000 --- a/L2J_Mobius_2.5_Underground/java/com/l2jmobius/gameserver/scripting/AbstractExecutionContext.java +++ /dev/null @@ -1,75 +0,0 @@ -/* - * This file is part of the L2J Mobius project. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * 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 - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package com.l2jmobius.gameserver.scripting; - -import java.nio.file.Path; -import java.util.HashMap; -import java.util.Map; - -/** - * @author HorridoJoho - * @param - */ -public abstract class AbstractExecutionContext implements IExecutionContext -{ - private final T _engine; - private final Map _properties; - private volatile Path _currentExecutingScipt; - - protected AbstractExecutionContext(T engine) - { - if (engine == null) - { - throw new IllegalArgumentException(); - } - _engine = engine; - _properties = new HashMap<>(); - } - - protected final void setCurrentExecutingScript(Path currentExecutingScript) - { - _currentExecutingScipt = currentExecutingScript; - } - - @Override - public final String setProperty(String key, String value) - { - return _properties.put(key, value); - } - - @Override - public final String getProperty(String key) - { - if (!_properties.containsKey(key)) - { - return _engine.getProperty(key); - } - return _properties.get(key); - } - - @Override - public final Path getCurrentExecutingScript() - { - return _currentExecutingScipt; - } - - @Override - public final T getScriptingEngine() - { - return _engine; - } -} diff --git a/L2J_Mobius_2.5_Underground/java/com/l2jmobius/gameserver/scripting/AbstractScriptingEngine.java b/L2J_Mobius_2.5_Underground/java/com/l2jmobius/gameserver/scripting/AbstractScriptingEngine.java deleted file mode 100644 index 90900438ec..0000000000 --- a/L2J_Mobius_2.5_Underground/java/com/l2jmobius/gameserver/scripting/AbstractScriptingEngine.java +++ /dev/null @@ -1,74 +0,0 @@ -/* - * This file is part of the L2J Mobius project. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * 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 - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package com.l2jmobius.gameserver.scripting; - -import java.util.Arrays; -import java.util.HashMap; -import java.util.Map; - -/** - * @author HorridoJoho - */ -public abstract class AbstractScriptingEngine implements IScriptingEngine -{ - private final String _engineName; - private final String _engineVersion; - private final String[] _commonFileExtensions; - private final Map _properties; - - protected AbstractScriptingEngine(String engineName, String engineVersion, String... commonFileExtensions) - { - if ((engineName == null) || engineName.isEmpty() || (engineVersion == null) || engineVersion.isEmpty() || (commonFileExtensions == null) || (commonFileExtensions.length == 0)) - { - throw new IllegalArgumentException(); - } - _engineName = engineName; - _engineVersion = engineVersion; - _commonFileExtensions = commonFileExtensions; - _properties = new HashMap<>(); - } - - @Override - public final String setProperty(String key, String value) - { - return _properties.put(key, value); - } - - @Override - public final String getProperty(String key) - { - return _properties.get(key); - } - - @Override - public final String getEngineName() - { - return _engineName; - } - - @Override - public final String getEngineVersion() - { - return _engineVersion; - } - - @Override - public final String[] getCommonFileExtensions() - { - return Arrays.copyOf(_commonFileExtensions, _commonFileExtensions.length); - } -} diff --git a/L2J_Mobius_2.5_Underground/java/com/l2jmobius/gameserver/scripting/IExecutionContext.java b/L2J_Mobius_2.5_Underground/java/com/l2jmobius/gameserver/scripting/IExecutionContext.java deleted file mode 100644 index eaf4860b8a..0000000000 --- a/L2J_Mobius_2.5_Underground/java/com/l2jmobius/gameserver/scripting/IExecutionContext.java +++ /dev/null @@ -1,71 +0,0 @@ -/* - * This file is part of the L2J Mobius project. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * 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 - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package com.l2jmobius.gameserver.scripting; - -import java.nio.file.Path; -import java.util.Map; -import java.util.Map.Entry; - -/** - * @author HorridoJoho - */ -public interface IExecutionContext -{ - /** - * Properties set here override the settings from the IScriptEngine
- * this class was created from. - * @param key the key - * @param value the value - * @return the previous value, or null when this key was not present before - */ - String setProperty(String key, String value); - - /** - * Executes all script in the iterable. - * @param sourcePaths the scripts to execute - * @return map of failed executions, Path=source file Throwable=thrown exception - * @throws Exception preparation for script execution failed - */ - Map executeScripts(Iterable sourcePaths) throws Exception; - - /** - * Executes a single file. - * @param sourcePath the script to execute - * @return entry of failed execution, Path=source file Throwable=thrown exception - * @throws Exception preparation for script execution failed - */ - Entry executeScript(Path sourcePath) throws Exception; - - /** - * Method to get the specified property value. - * @param key the key - * @return the value, or null if the key is not present - */ - String getProperty(String key); - - /** - * Method to get the current executing script file. - * @return the currently executing script file, null if non - */ - Path getCurrentExecutingScript(); - - /** - * Method to get the script engine this execution context belongs to. - * @return the script engine this execution context belongs to - */ - IScriptingEngine getScriptingEngine(); -} diff --git a/L2J_Mobius_2.5_Underground/java/com/l2jmobius/gameserver/scripting/IScriptingEngine.java b/L2J_Mobius_2.5_Underground/java/com/l2jmobius/gameserver/scripting/IScriptingEngine.java deleted file mode 100644 index 883172cce1..0000000000 --- a/L2J_Mobius_2.5_Underground/java/com/l2jmobius/gameserver/scripting/IScriptingEngine.java +++ /dev/null @@ -1,75 +0,0 @@ -/* - * This file is part of the L2J Mobius project. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * 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 - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package com.l2jmobius.gameserver.scripting; - -/** - * @author HorridoJoho - */ -public interface IScriptingEngine -{ - /** - * Sets script engine properties. The script values will be available
- * to the the insatnces created {@link IExecutionContext} implementation. - * @param key the key - * @param value the value - * @return the previous value, or null when this key was not present before - */ - String setProperty(String key, String value); - - /** - * Creates an execution context. - * @return the created execution context. - */ - IExecutionContext createExecutionContext(); - - /** - * Method to get the specified property value. - * @param key the key - * @return the value,or null if the key is not present - */ - String getProperty(String key); - - /** - * Method to get the engine name. - * @return the engine name - */ - String getEngineName(); - - /** - * Method to get the engine version. - * @return the engine version - */ - String getEngineVersion(); - - /** - * Method to get the scripting language name. - * @return the scripting engine name - */ - String getLanguageName(); - - /** - * Method to get the the language version. - * @return the language version - */ - String getLanguageVersion(); - - /** - * Method to retrive the commonly used file extensions for the language. - * @return the commonly used file extensions for the language - */ - String[] getCommonFileExtensions(); -} diff --git a/L2J_Mobius_2.5_Underground/java/com/l2jmobius/gameserver/scripting/ScriptEngineManager.java b/L2J_Mobius_2.5_Underground/java/com/l2jmobius/gameserver/scripting/ScriptEngineManager.java index 25a46c93b2..7f8d5fbd5b 100644 --- a/L2J_Mobius_2.5_Underground/java/com/l2jmobius/gameserver/scripting/ScriptEngineManager.java +++ b/L2J_Mobius_2.5_Underground/java/com/l2jmobius/gameserver/scripting/ScriptEngineManager.java @@ -17,7 +17,6 @@ package com.l2jmobius.gameserver.scripting; import java.io.File; -import java.io.FileInputStream; import java.io.IOException; import java.nio.file.FileVisitResult; import java.nio.file.Files; @@ -31,9 +30,6 @@ import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Map.Entry; -import java.util.Objects; -import java.util.Properties; -import java.util.ServiceLoader; import java.util.logging.Level; import java.util.logging.Logger; @@ -41,15 +37,16 @@ import org.w3c.dom.Document; import com.l2jmobius.Config; import com.l2jmobius.commons.util.IXmlReader; +import com.l2jmobius.gameserver.scripting.java.JavaExecutionContext; import com.l2jmobius.gameserver.scripting.java.JavaScriptingEngine; /** - * Caches script engines and provides functionality for executing and managing scripts. - * @author KenM, HorridoJoho + * @author Mobius */ public final class ScriptEngineManager implements IXmlReader { private static final Logger LOGGER = Logger.getLogger(ScriptEngineManager.class.getName()); + public static final Path SCRIPT_FOLDER = Config.SCRIPT_ROOT.toPath(); public static final Path MASTER_HANDLER_FILE = Paths.get(SCRIPT_FOLDER.toString(), "handlers", "MasterHandler.java"); public static final Path EFFECT_MASTER_HANDLER_FILE = Paths.get(SCRIPT_FOLDER.toString(), "handlers", "EffectMasterHandler.java"); @@ -57,19 +54,11 @@ public final class ScriptEngineManager implements IXmlReader public static final Path CONDITION_HANDLER_FILE = Paths.get(SCRIPT_FOLDER.toString(), "handlers", "ConditionMasterHandler.java"); public static final Path ONE_DAY_REWARD_MASTER_HANDLER = Paths.get(SCRIPT_FOLDER.toString(), "handlers", "DailyMissionMasterHandler.java"); - private IExecutionContext _javaExecutionContext = null; - static final List _exclusions = new ArrayList<>(); + private static final JavaExecutionContext _javaExecutionContext = new JavaScriptingEngine().createExecutionContext(); + protected static final List _exclusions = new ArrayList<>(); protected ScriptEngineManager() { - final Properties props = loadProperties(); - - // Default java engine implementation - registerEngine(new JavaScriptingEngine(), props); - - // Load external script engines - ServiceLoader.load(IScriptingEngine.class).forEach(engine -> registerEngine(engine, props)); - // Load Scripts.xml load(); } @@ -144,71 +133,6 @@ public final class ScriptEngineManager implements IXmlReader } } - private Properties loadProperties() - { - Properties props = null; - try (FileInputStream fis = new FileInputStream("config/ScriptEngine.ini")) - { - props = new Properties(); - props.load(fis); - } - catch (Exception e) - { - props = null; - LOGGER.warning("Couldn't load ScriptEngine.ini: " + e.getMessage()); - } - return props; - } - - private void registerEngine(IScriptingEngine engine, Properties props) - { - maybeSetProperties("language." + engine.getLanguageName() + ".", props, engine); - _javaExecutionContext = engine.createExecutionContext(); - LOGGER.info("ScriptEngine: " + engine.getEngineName() + " " + engine.getEngineVersion() + " (" + engine.getLanguageName() + " " + engine.getLanguageVersion() + ")"); - } - - private void maybeSetProperties(String propPrefix, Properties props, IScriptingEngine engine) - { - if (props == null) - { - return; - } - - for (Entry prop : props.entrySet()) - { - String key = (String) prop.getKey(); - String value = (String) prop.getValue(); - - if (key.startsWith(propPrefix)) - { - key = key.substring(propPrefix.length()); - if (value.startsWith("%") && value.endsWith("%")) - { - value = System.getProperty(value.substring(1, value.length() - 1)); - } - - engine.setProperty(key, value); - } - } - } - - public void executeScriptList() throws Exception - { - if (Config.ALT_DEV_NO_QUESTS) - { - return; - } - - final List files = new ArrayList<>(); - processDirectory(SCRIPT_FOLDER.toFile(), files); - - final Map invokationErrors = _javaExecutionContext.executeScripts(files); - for (Entry entry : invokationErrors.entrySet()) - { - LOGGER.log(Level.WARNING, "ScriptEngine: " + entry.getKey() + " failed execution!", entry.getValue()); - } - } - private void processDirectory(File dir, List files) { for (File file : dir.listFiles()) @@ -230,15 +154,12 @@ public final class ScriptEngineManager implements IXmlReader public void executeScript(Path sourceFile) throws Exception { - Objects.requireNonNull(sourceFile); - if (!sourceFile.isAbsolute()) { sourceFile = SCRIPT_FOLDER.resolve(sourceFile); } sourceFile = sourceFile.toAbsolutePath(); - Objects.requireNonNull(sourceFile, "ScriptFile: " + sourceFile + " does not have an extension to determine the script engine!"); final Entry error = _javaExecutionContext.executeScript(sourceFile); if (error != null) @@ -247,6 +168,23 @@ public final class ScriptEngineManager implements IXmlReader } } + public void executeScriptList() throws Exception + { + if (Config.ALT_DEV_NO_QUESTS) + { + return; + } + + final List files = new ArrayList<>(); + processDirectory(SCRIPT_FOLDER.toFile(), files); + + final Map invokationErrors = _javaExecutionContext.executeScripts(files); + for (Entry entry : invokationErrors.entrySet()) + { + LOGGER.log(Level.WARNING, "ScriptEngine: " + entry.getKey() + " failed execution!", entry.getValue()); + } + } + public Path getCurrentLoadingScript() { return _javaExecutionContext.getCurrentExecutingScript(); @@ -254,11 +192,11 @@ public final class ScriptEngineManager implements IXmlReader public static ScriptEngineManager getInstance() { - return SingletonHolder._instance; + return SingletonHolder.INSTANCE; } private static class SingletonHolder { - protected static final ScriptEngineManager _instance = new ScriptEngineManager(); + protected static final ScriptEngineManager INSTANCE = new ScriptEngineManager(); } } \ No newline at end of file diff --git a/L2J_Mobius_2.5_Underground/java/com/l2jmobius/gameserver/scripting/java/JavaCompilerException.java b/L2J_Mobius_2.5_Underground/java/com/l2jmobius/gameserver/scripting/java/JavaCompilerException.java deleted file mode 100644 index 241ffe91bd..0000000000 --- a/L2J_Mobius_2.5_Underground/java/com/l2jmobius/gameserver/scripting/java/JavaCompilerException.java +++ /dev/null @@ -1,28 +0,0 @@ -/* - * This file is part of the L2J Mobius project. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * 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 - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package com.l2jmobius.gameserver.scripting.java; - -/** - * @author HorridoJoho - */ -public final class JavaCompilerException extends RuntimeException -{ - public JavaCompilerException(String diagnostics) - { - super(diagnostics); - } -} diff --git a/L2J_Mobius_2.5_Underground/java/com/l2jmobius/gameserver/scripting/java/JavaExecutionContext.java b/L2J_Mobius_2.5_Underground/java/com/l2jmobius/gameserver/scripting/java/JavaExecutionContext.java index 70d4da0893..1eddebd693 100644 --- a/L2J_Mobius_2.5_Underground/java/com/l2jmobius/gameserver/scripting/java/JavaExecutionContext.java +++ b/L2J_Mobius_2.5_Underground/java/com/l2jmobius/gameserver/scripting/java/JavaExecutionContext.java @@ -22,9 +22,9 @@ import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.nio.charset.StandardCharsets; import java.nio.file.Path; +import java.util.ArrayList; import java.util.Arrays; -import java.util.LinkedHashMap; -import java.util.LinkedList; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; @@ -34,22 +34,20 @@ import org.openjavac.tools.Diagnostic; import org.openjavac.tools.DiagnosticCollector; import org.openjavac.tools.JavaFileObject; -import com.l2jmobius.gameserver.scripting.AbstractExecutionContext; import com.l2jmobius.gameserver.scripting.annotations.Disabled; /** * @author HorridoJoho */ -public final class JavaExecutionContext extends AbstractExecutionContext +public final class JavaExecutionContext extends JavaScriptingEngine { private static final Logger LOGGER = Logger.getLogger(JavaExecutionContext.class.getName()); - private static final List _options = new LinkedList<>(); + private static final List _options = new ArrayList<>(); + private static Path _currentExecutingScript; JavaExecutionContext(JavaScriptingEngine engine) { - super(engine); - // Set options. addOptionIfNotNull(_options, getProperty("source"), "-source"); addOptionIfNotNull(_options, getProperty("sourcepath"), "-sourcepath"); @@ -76,7 +74,7 @@ public final class JavaExecutionContext extends AbstractExecutionContext executeScripts(Iterable sourcePaths) throws Exception { final DiagnosticCollector fileManagerDiagnostics = new DiagnosticCollector<>(); final DiagnosticCollector compilationDiagnostics = new DiagnosticCollector<>(); - try (ScriptingFileManager fileManager = new ScriptingFileManager(getScriptingEngine().getCompiler().getStandardFileManager(fileManagerDiagnostics, null, StandardCharsets.UTF_8))) + try (ScriptingFileManager fileManager = new ScriptingFileManager(getCompiler().getStandardFileManager(fileManagerDiagnostics, null, StandardCharsets.UTF_8))) { // We really need an iterable of files or strings. - final List sourcePathStrings = new LinkedList<>(); + final List sourcePathStrings = new ArrayList<>(); for (Path sourcePath : sourcePaths) { sourcePathStrings.add(sourcePath.toString()); @@ -150,7 +147,7 @@ public final class JavaExecutionContext extends AbstractExecutionContext executionFailures = new LinkedHashMap<>(); + final Map executionFailures = new HashMap<>(); final Iterable compiledClasses = fileManager.getCompiledClasses(); for (Path sourcePath : sourcePaths) { @@ -199,7 +196,7 @@ public final class JavaExecutionContext extends AbstractExecutionContext executeScript(Path sourcePath) throws Exception { final Map executionFailures = executeScripts(Arrays.asList(sourcePath)); @@ -254,4 +250,9 @@ public final class JavaExecutionContext extends AbstractExecutionContext _properties = new HashMap<>(); + private final static JavaCompiler _compiler = JavacTool.create(); public JavaScriptingEngine() { - super("Java Engine", "10", "java"); - } - - private void determineCompilerOrThrow() - { - if (_compiler == null) + // Load config. + Properties props = new Properties(); + try (FileInputStream fis = new FileInputStream("config/ScriptEngine.ini")) { - _compiler = JavacTool.create(); + props.load(fis); + } + catch (Exception e) + { + LOGGER.warning("Could not load ScriptEngine.ini: " + e.getMessage()); } - if (_compiler == null) + // Set properties. + for (Entry prop : props.entrySet()) { - throw new IllegalStateException("No JavaCompiler service installed!"); - } - } - - private void ensureCompilerOrThrow() - { - if (_compiler == null) - { - synchronized (this) + String key = (String) prop.getKey(); + String value = (String) prop.getValue(); + + if (value.startsWith("%") && value.endsWith("%")) { - if (_compiler == null) - { - determineCompilerOrThrow(); - } + value = System.getProperty(value.substring(1, value.length() - 1)); } + + _properties.put(key, value); } } - JavaCompiler getCompiler() + public JavaExecutionContext createExecutionContext() { - return _compiler; - } - - @Override - public IExecutionContext createExecutionContext() - { - ensureCompilerOrThrow(); return new JavaExecutionContext(this); } - @Override - public String getLanguageName() + public final String getProperty(String key) { - return "Java"; + return _properties.get(key); } - @Override - public String getLanguageVersion() + public JavaCompiler getCompiler() { - ensureCompilerOrThrow(); - return Arrays.deepToString(_compiler.getSourceVersions().toArray(new SourceVersion[0])).replace("RELEASE_", ""); + return _compiler; } } \ No newline at end of file diff --git a/L2J_Mobius_2.5_Underground/java/com/l2jmobius/gameserver/scripting/java/ScriptingOutputFileObject.java b/L2J_Mobius_2.5_Underground/java/com/l2jmobius/gameserver/scripting/java/ScriptingOutputFileObject.java index 439cf5d260..9ab668887e 100644 --- a/L2J_Mobius_2.5_Underground/java/com/l2jmobius/gameserver/scripting/java/ScriptingOutputFileObject.java +++ b/L2J_Mobius_2.5_Underground/java/com/l2jmobius/gameserver/scripting/java/ScriptingOutputFileObject.java @@ -144,5 +144,4 @@ final class ScriptingOutputFileObject implements JavaFileObject { return null; } - } diff --git a/L2J_Mobius_3.0_Helios/dist/game/config/ScriptEngine.ini b/L2J_Mobius_3.0_Helios/dist/game/config/ScriptEngine.ini index 2d1e972e72..5004dca9fd 100644 --- a/L2J_Mobius_3.0_Helios/dist/game/config/ScriptEngine.ini +++ b/L2J_Mobius_3.0_Helios/dist/game/config/ScriptEngine.ini @@ -1,23 +1,23 @@ -############################################################################### -# Properties retrived with System.getProperty(String) can be used as values +# --------------------------------------------------------------------------- +# Script Engine Settings +# --------------------------------------------------------------------------- +# Properties retrieved with System.getProperty(String) can be used as values # by enclosing the property name with %. Eg.: %java.class.path% -############################################################################### - # The parent class loader for isolated script class loaders. # When this property is not specified, has an invalid value or is a class name which could not be found, the System classloader is used. # Values: System, ThreadContext or a fully qualified java class name -language.Java.classloader=System -#language.Java.classloader=ThreadContext +classloader=System +#classloader=ThreadContext -# Source compatibility -language.Java.source=1.8 +# Source compatibility. +source=1.8 # The java sourcepath, when you have a different datapack root, you must change this too. -language.Java.sourcepath=data/scripts +sourcepath=data/scripts -# The java classpath -language.Java.cp=%java.class.path% +# The java classpath. +cp=%java.class.path% -# The debug informations to generate for compiled class files -language.Java.g=source,lines,vars \ No newline at end of file +# The debug informations to generate for compiled class files. +g=source,lines,vars \ No newline at end of file diff --git a/L2J_Mobius_3.0_Helios/java/com/l2jmobius/gameserver/scripting/AbstractExecutionContext.java b/L2J_Mobius_3.0_Helios/java/com/l2jmobius/gameserver/scripting/AbstractExecutionContext.java deleted file mode 100644 index 296b54f06b..0000000000 --- a/L2J_Mobius_3.0_Helios/java/com/l2jmobius/gameserver/scripting/AbstractExecutionContext.java +++ /dev/null @@ -1,75 +0,0 @@ -/* - * This file is part of the L2J Mobius project. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * 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 - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package com.l2jmobius.gameserver.scripting; - -import java.nio.file.Path; -import java.util.HashMap; -import java.util.Map; - -/** - * @author HorridoJoho - * @param - */ -public abstract class AbstractExecutionContext implements IExecutionContext -{ - private final T _engine; - private final Map _properties; - private volatile Path _currentExecutingScipt; - - protected AbstractExecutionContext(T engine) - { - if (engine == null) - { - throw new IllegalArgumentException(); - } - _engine = engine; - _properties = new HashMap<>(); - } - - protected final void setCurrentExecutingScript(Path currentExecutingScript) - { - _currentExecutingScipt = currentExecutingScript; - } - - @Override - public final String setProperty(String key, String value) - { - return _properties.put(key, value); - } - - @Override - public final String getProperty(String key) - { - if (!_properties.containsKey(key)) - { - return _engine.getProperty(key); - } - return _properties.get(key); - } - - @Override - public final Path getCurrentExecutingScript() - { - return _currentExecutingScipt; - } - - @Override - public final T getScriptingEngine() - { - return _engine; - } -} diff --git a/L2J_Mobius_3.0_Helios/java/com/l2jmobius/gameserver/scripting/AbstractScriptingEngine.java b/L2J_Mobius_3.0_Helios/java/com/l2jmobius/gameserver/scripting/AbstractScriptingEngine.java deleted file mode 100644 index 90900438ec..0000000000 --- a/L2J_Mobius_3.0_Helios/java/com/l2jmobius/gameserver/scripting/AbstractScriptingEngine.java +++ /dev/null @@ -1,74 +0,0 @@ -/* - * This file is part of the L2J Mobius project. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * 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 - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package com.l2jmobius.gameserver.scripting; - -import java.util.Arrays; -import java.util.HashMap; -import java.util.Map; - -/** - * @author HorridoJoho - */ -public abstract class AbstractScriptingEngine implements IScriptingEngine -{ - private final String _engineName; - private final String _engineVersion; - private final String[] _commonFileExtensions; - private final Map _properties; - - protected AbstractScriptingEngine(String engineName, String engineVersion, String... commonFileExtensions) - { - if ((engineName == null) || engineName.isEmpty() || (engineVersion == null) || engineVersion.isEmpty() || (commonFileExtensions == null) || (commonFileExtensions.length == 0)) - { - throw new IllegalArgumentException(); - } - _engineName = engineName; - _engineVersion = engineVersion; - _commonFileExtensions = commonFileExtensions; - _properties = new HashMap<>(); - } - - @Override - public final String setProperty(String key, String value) - { - return _properties.put(key, value); - } - - @Override - public final String getProperty(String key) - { - return _properties.get(key); - } - - @Override - public final String getEngineName() - { - return _engineName; - } - - @Override - public final String getEngineVersion() - { - return _engineVersion; - } - - @Override - public final String[] getCommonFileExtensions() - { - return Arrays.copyOf(_commonFileExtensions, _commonFileExtensions.length); - } -} diff --git a/L2J_Mobius_3.0_Helios/java/com/l2jmobius/gameserver/scripting/IExecutionContext.java b/L2J_Mobius_3.0_Helios/java/com/l2jmobius/gameserver/scripting/IExecutionContext.java deleted file mode 100644 index eaf4860b8a..0000000000 --- a/L2J_Mobius_3.0_Helios/java/com/l2jmobius/gameserver/scripting/IExecutionContext.java +++ /dev/null @@ -1,71 +0,0 @@ -/* - * This file is part of the L2J Mobius project. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * 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 - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package com.l2jmobius.gameserver.scripting; - -import java.nio.file.Path; -import java.util.Map; -import java.util.Map.Entry; - -/** - * @author HorridoJoho - */ -public interface IExecutionContext -{ - /** - * Properties set here override the settings from the IScriptEngine
- * this class was created from. - * @param key the key - * @param value the value - * @return the previous value, or null when this key was not present before - */ - String setProperty(String key, String value); - - /** - * Executes all script in the iterable. - * @param sourcePaths the scripts to execute - * @return map of failed executions, Path=source file Throwable=thrown exception - * @throws Exception preparation for script execution failed - */ - Map executeScripts(Iterable sourcePaths) throws Exception; - - /** - * Executes a single file. - * @param sourcePath the script to execute - * @return entry of failed execution, Path=source file Throwable=thrown exception - * @throws Exception preparation for script execution failed - */ - Entry executeScript(Path sourcePath) throws Exception; - - /** - * Method to get the specified property value. - * @param key the key - * @return the value, or null if the key is not present - */ - String getProperty(String key); - - /** - * Method to get the current executing script file. - * @return the currently executing script file, null if non - */ - Path getCurrentExecutingScript(); - - /** - * Method to get the script engine this execution context belongs to. - * @return the script engine this execution context belongs to - */ - IScriptingEngine getScriptingEngine(); -} diff --git a/L2J_Mobius_3.0_Helios/java/com/l2jmobius/gameserver/scripting/IScriptingEngine.java b/L2J_Mobius_3.0_Helios/java/com/l2jmobius/gameserver/scripting/IScriptingEngine.java deleted file mode 100644 index 883172cce1..0000000000 --- a/L2J_Mobius_3.0_Helios/java/com/l2jmobius/gameserver/scripting/IScriptingEngine.java +++ /dev/null @@ -1,75 +0,0 @@ -/* - * This file is part of the L2J Mobius project. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * 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 - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package com.l2jmobius.gameserver.scripting; - -/** - * @author HorridoJoho - */ -public interface IScriptingEngine -{ - /** - * Sets script engine properties. The script values will be available
- * to the the insatnces created {@link IExecutionContext} implementation. - * @param key the key - * @param value the value - * @return the previous value, or null when this key was not present before - */ - String setProperty(String key, String value); - - /** - * Creates an execution context. - * @return the created execution context. - */ - IExecutionContext createExecutionContext(); - - /** - * Method to get the specified property value. - * @param key the key - * @return the value,or null if the key is not present - */ - String getProperty(String key); - - /** - * Method to get the engine name. - * @return the engine name - */ - String getEngineName(); - - /** - * Method to get the engine version. - * @return the engine version - */ - String getEngineVersion(); - - /** - * Method to get the scripting language name. - * @return the scripting engine name - */ - String getLanguageName(); - - /** - * Method to get the the language version. - * @return the language version - */ - String getLanguageVersion(); - - /** - * Method to retrive the commonly used file extensions for the language. - * @return the commonly used file extensions for the language - */ - String[] getCommonFileExtensions(); -} diff --git a/L2J_Mobius_3.0_Helios/java/com/l2jmobius/gameserver/scripting/ScriptEngineManager.java b/L2J_Mobius_3.0_Helios/java/com/l2jmobius/gameserver/scripting/ScriptEngineManager.java index 25a46c93b2..7f8d5fbd5b 100644 --- a/L2J_Mobius_3.0_Helios/java/com/l2jmobius/gameserver/scripting/ScriptEngineManager.java +++ b/L2J_Mobius_3.0_Helios/java/com/l2jmobius/gameserver/scripting/ScriptEngineManager.java @@ -17,7 +17,6 @@ package com.l2jmobius.gameserver.scripting; import java.io.File; -import java.io.FileInputStream; import java.io.IOException; import java.nio.file.FileVisitResult; import java.nio.file.Files; @@ -31,9 +30,6 @@ import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Map.Entry; -import java.util.Objects; -import java.util.Properties; -import java.util.ServiceLoader; import java.util.logging.Level; import java.util.logging.Logger; @@ -41,15 +37,16 @@ import org.w3c.dom.Document; import com.l2jmobius.Config; import com.l2jmobius.commons.util.IXmlReader; +import com.l2jmobius.gameserver.scripting.java.JavaExecutionContext; import com.l2jmobius.gameserver.scripting.java.JavaScriptingEngine; /** - * Caches script engines and provides functionality for executing and managing scripts. - * @author KenM, HorridoJoho + * @author Mobius */ public final class ScriptEngineManager implements IXmlReader { private static final Logger LOGGER = Logger.getLogger(ScriptEngineManager.class.getName()); + public static final Path SCRIPT_FOLDER = Config.SCRIPT_ROOT.toPath(); public static final Path MASTER_HANDLER_FILE = Paths.get(SCRIPT_FOLDER.toString(), "handlers", "MasterHandler.java"); public static final Path EFFECT_MASTER_HANDLER_FILE = Paths.get(SCRIPT_FOLDER.toString(), "handlers", "EffectMasterHandler.java"); @@ -57,19 +54,11 @@ public final class ScriptEngineManager implements IXmlReader public static final Path CONDITION_HANDLER_FILE = Paths.get(SCRIPT_FOLDER.toString(), "handlers", "ConditionMasterHandler.java"); public static final Path ONE_DAY_REWARD_MASTER_HANDLER = Paths.get(SCRIPT_FOLDER.toString(), "handlers", "DailyMissionMasterHandler.java"); - private IExecutionContext _javaExecutionContext = null; - static final List _exclusions = new ArrayList<>(); + private static final JavaExecutionContext _javaExecutionContext = new JavaScriptingEngine().createExecutionContext(); + protected static final List _exclusions = new ArrayList<>(); protected ScriptEngineManager() { - final Properties props = loadProperties(); - - // Default java engine implementation - registerEngine(new JavaScriptingEngine(), props); - - // Load external script engines - ServiceLoader.load(IScriptingEngine.class).forEach(engine -> registerEngine(engine, props)); - // Load Scripts.xml load(); } @@ -144,71 +133,6 @@ public final class ScriptEngineManager implements IXmlReader } } - private Properties loadProperties() - { - Properties props = null; - try (FileInputStream fis = new FileInputStream("config/ScriptEngine.ini")) - { - props = new Properties(); - props.load(fis); - } - catch (Exception e) - { - props = null; - LOGGER.warning("Couldn't load ScriptEngine.ini: " + e.getMessage()); - } - return props; - } - - private void registerEngine(IScriptingEngine engine, Properties props) - { - maybeSetProperties("language." + engine.getLanguageName() + ".", props, engine); - _javaExecutionContext = engine.createExecutionContext(); - LOGGER.info("ScriptEngine: " + engine.getEngineName() + " " + engine.getEngineVersion() + " (" + engine.getLanguageName() + " " + engine.getLanguageVersion() + ")"); - } - - private void maybeSetProperties(String propPrefix, Properties props, IScriptingEngine engine) - { - if (props == null) - { - return; - } - - for (Entry prop : props.entrySet()) - { - String key = (String) prop.getKey(); - String value = (String) prop.getValue(); - - if (key.startsWith(propPrefix)) - { - key = key.substring(propPrefix.length()); - if (value.startsWith("%") && value.endsWith("%")) - { - value = System.getProperty(value.substring(1, value.length() - 1)); - } - - engine.setProperty(key, value); - } - } - } - - public void executeScriptList() throws Exception - { - if (Config.ALT_DEV_NO_QUESTS) - { - return; - } - - final List files = new ArrayList<>(); - processDirectory(SCRIPT_FOLDER.toFile(), files); - - final Map invokationErrors = _javaExecutionContext.executeScripts(files); - for (Entry entry : invokationErrors.entrySet()) - { - LOGGER.log(Level.WARNING, "ScriptEngine: " + entry.getKey() + " failed execution!", entry.getValue()); - } - } - private void processDirectory(File dir, List files) { for (File file : dir.listFiles()) @@ -230,15 +154,12 @@ public final class ScriptEngineManager implements IXmlReader public void executeScript(Path sourceFile) throws Exception { - Objects.requireNonNull(sourceFile); - if (!sourceFile.isAbsolute()) { sourceFile = SCRIPT_FOLDER.resolve(sourceFile); } sourceFile = sourceFile.toAbsolutePath(); - Objects.requireNonNull(sourceFile, "ScriptFile: " + sourceFile + " does not have an extension to determine the script engine!"); final Entry error = _javaExecutionContext.executeScript(sourceFile); if (error != null) @@ -247,6 +168,23 @@ public final class ScriptEngineManager implements IXmlReader } } + public void executeScriptList() throws Exception + { + if (Config.ALT_DEV_NO_QUESTS) + { + return; + } + + final List files = new ArrayList<>(); + processDirectory(SCRIPT_FOLDER.toFile(), files); + + final Map invokationErrors = _javaExecutionContext.executeScripts(files); + for (Entry entry : invokationErrors.entrySet()) + { + LOGGER.log(Level.WARNING, "ScriptEngine: " + entry.getKey() + " failed execution!", entry.getValue()); + } + } + public Path getCurrentLoadingScript() { return _javaExecutionContext.getCurrentExecutingScript(); @@ -254,11 +192,11 @@ public final class ScriptEngineManager implements IXmlReader public static ScriptEngineManager getInstance() { - return SingletonHolder._instance; + return SingletonHolder.INSTANCE; } private static class SingletonHolder { - protected static final ScriptEngineManager _instance = new ScriptEngineManager(); + protected static final ScriptEngineManager INSTANCE = new ScriptEngineManager(); } } \ No newline at end of file diff --git a/L2J_Mobius_3.0_Helios/java/com/l2jmobius/gameserver/scripting/java/JavaCompilerException.java b/L2J_Mobius_3.0_Helios/java/com/l2jmobius/gameserver/scripting/java/JavaCompilerException.java deleted file mode 100644 index 241ffe91bd..0000000000 --- a/L2J_Mobius_3.0_Helios/java/com/l2jmobius/gameserver/scripting/java/JavaCompilerException.java +++ /dev/null @@ -1,28 +0,0 @@ -/* - * This file is part of the L2J Mobius project. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * 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 - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package com.l2jmobius.gameserver.scripting.java; - -/** - * @author HorridoJoho - */ -public final class JavaCompilerException extends RuntimeException -{ - public JavaCompilerException(String diagnostics) - { - super(diagnostics); - } -} diff --git a/L2J_Mobius_3.0_Helios/java/com/l2jmobius/gameserver/scripting/java/JavaExecutionContext.java b/L2J_Mobius_3.0_Helios/java/com/l2jmobius/gameserver/scripting/java/JavaExecutionContext.java index 70d4da0893..1eddebd693 100644 --- a/L2J_Mobius_3.0_Helios/java/com/l2jmobius/gameserver/scripting/java/JavaExecutionContext.java +++ b/L2J_Mobius_3.0_Helios/java/com/l2jmobius/gameserver/scripting/java/JavaExecutionContext.java @@ -22,9 +22,9 @@ import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.nio.charset.StandardCharsets; import java.nio.file.Path; +import java.util.ArrayList; import java.util.Arrays; -import java.util.LinkedHashMap; -import java.util.LinkedList; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; @@ -34,22 +34,20 @@ import org.openjavac.tools.Diagnostic; import org.openjavac.tools.DiagnosticCollector; import org.openjavac.tools.JavaFileObject; -import com.l2jmobius.gameserver.scripting.AbstractExecutionContext; import com.l2jmobius.gameserver.scripting.annotations.Disabled; /** * @author HorridoJoho */ -public final class JavaExecutionContext extends AbstractExecutionContext +public final class JavaExecutionContext extends JavaScriptingEngine { private static final Logger LOGGER = Logger.getLogger(JavaExecutionContext.class.getName()); - private static final List _options = new LinkedList<>(); + private static final List _options = new ArrayList<>(); + private static Path _currentExecutingScript; JavaExecutionContext(JavaScriptingEngine engine) { - super(engine); - // Set options. addOptionIfNotNull(_options, getProperty("source"), "-source"); addOptionIfNotNull(_options, getProperty("sourcepath"), "-sourcepath"); @@ -76,7 +74,7 @@ public final class JavaExecutionContext extends AbstractExecutionContext executeScripts(Iterable sourcePaths) throws Exception { final DiagnosticCollector fileManagerDiagnostics = new DiagnosticCollector<>(); final DiagnosticCollector compilationDiagnostics = new DiagnosticCollector<>(); - try (ScriptingFileManager fileManager = new ScriptingFileManager(getScriptingEngine().getCompiler().getStandardFileManager(fileManagerDiagnostics, null, StandardCharsets.UTF_8))) + try (ScriptingFileManager fileManager = new ScriptingFileManager(getCompiler().getStandardFileManager(fileManagerDiagnostics, null, StandardCharsets.UTF_8))) { // We really need an iterable of files or strings. - final List sourcePathStrings = new LinkedList<>(); + final List sourcePathStrings = new ArrayList<>(); for (Path sourcePath : sourcePaths) { sourcePathStrings.add(sourcePath.toString()); @@ -150,7 +147,7 @@ public final class JavaExecutionContext extends AbstractExecutionContext executionFailures = new LinkedHashMap<>(); + final Map executionFailures = new HashMap<>(); final Iterable compiledClasses = fileManager.getCompiledClasses(); for (Path sourcePath : sourcePaths) { @@ -199,7 +196,7 @@ public final class JavaExecutionContext extends AbstractExecutionContext executeScript(Path sourcePath) throws Exception { final Map executionFailures = executeScripts(Arrays.asList(sourcePath)); @@ -254,4 +250,9 @@ public final class JavaExecutionContext extends AbstractExecutionContext _properties = new HashMap<>(); + private final static JavaCompiler _compiler = JavacTool.create(); public JavaScriptingEngine() { - super("Java Engine", "10", "java"); - } - - private void determineCompilerOrThrow() - { - if (_compiler == null) + // Load config. + Properties props = new Properties(); + try (FileInputStream fis = new FileInputStream("config/ScriptEngine.ini")) { - _compiler = JavacTool.create(); + props.load(fis); + } + catch (Exception e) + { + LOGGER.warning("Could not load ScriptEngine.ini: " + e.getMessage()); } - if (_compiler == null) + // Set properties. + for (Entry prop : props.entrySet()) { - throw new IllegalStateException("No JavaCompiler service installed!"); - } - } - - private void ensureCompilerOrThrow() - { - if (_compiler == null) - { - synchronized (this) + String key = (String) prop.getKey(); + String value = (String) prop.getValue(); + + if (value.startsWith("%") && value.endsWith("%")) { - if (_compiler == null) - { - determineCompilerOrThrow(); - } + value = System.getProperty(value.substring(1, value.length() - 1)); } + + _properties.put(key, value); } } - JavaCompiler getCompiler() + public JavaExecutionContext createExecutionContext() { - return _compiler; - } - - @Override - public IExecutionContext createExecutionContext() - { - ensureCompilerOrThrow(); return new JavaExecutionContext(this); } - @Override - public String getLanguageName() + public final String getProperty(String key) { - return "Java"; + return _properties.get(key); } - @Override - public String getLanguageVersion() + public JavaCompiler getCompiler() { - ensureCompilerOrThrow(); - return Arrays.deepToString(_compiler.getSourceVersions().toArray(new SourceVersion[0])).replace("RELEASE_", ""); + return _compiler; } } \ No newline at end of file diff --git a/L2J_Mobius_3.0_Helios/java/com/l2jmobius/gameserver/scripting/java/ScriptingOutputFileObject.java b/L2J_Mobius_3.0_Helios/java/com/l2jmobius/gameserver/scripting/java/ScriptingOutputFileObject.java index 439cf5d260..9ab668887e 100644 --- a/L2J_Mobius_3.0_Helios/java/com/l2jmobius/gameserver/scripting/java/ScriptingOutputFileObject.java +++ b/L2J_Mobius_3.0_Helios/java/com/l2jmobius/gameserver/scripting/java/ScriptingOutputFileObject.java @@ -144,5 +144,4 @@ final class ScriptingOutputFileObject implements JavaFileObject { return null; } - } diff --git a/L2J_Mobius_4.0_GrandCrusade/dist/game/config/ScriptEngine.ini b/L2J_Mobius_4.0_GrandCrusade/dist/game/config/ScriptEngine.ini index 2d1e972e72..5004dca9fd 100644 --- a/L2J_Mobius_4.0_GrandCrusade/dist/game/config/ScriptEngine.ini +++ b/L2J_Mobius_4.0_GrandCrusade/dist/game/config/ScriptEngine.ini @@ -1,23 +1,23 @@ -############################################################################### -# Properties retrived with System.getProperty(String) can be used as values +# --------------------------------------------------------------------------- +# Script Engine Settings +# --------------------------------------------------------------------------- +# Properties retrieved with System.getProperty(String) can be used as values # by enclosing the property name with %. Eg.: %java.class.path% -############################################################################### - # The parent class loader for isolated script class loaders. # When this property is not specified, has an invalid value or is a class name which could not be found, the System classloader is used. # Values: System, ThreadContext or a fully qualified java class name -language.Java.classloader=System -#language.Java.classloader=ThreadContext +classloader=System +#classloader=ThreadContext -# Source compatibility -language.Java.source=1.8 +# Source compatibility. +source=1.8 # The java sourcepath, when you have a different datapack root, you must change this too. -language.Java.sourcepath=data/scripts +sourcepath=data/scripts -# The java classpath -language.Java.cp=%java.class.path% +# The java classpath. +cp=%java.class.path% -# The debug informations to generate for compiled class files -language.Java.g=source,lines,vars \ No newline at end of file +# The debug informations to generate for compiled class files. +g=source,lines,vars \ No newline at end of file diff --git a/L2J_Mobius_4.0_GrandCrusade/java/com/l2jmobius/gameserver/scripting/AbstractExecutionContext.java b/L2J_Mobius_4.0_GrandCrusade/java/com/l2jmobius/gameserver/scripting/AbstractExecutionContext.java deleted file mode 100644 index 296b54f06b..0000000000 --- a/L2J_Mobius_4.0_GrandCrusade/java/com/l2jmobius/gameserver/scripting/AbstractExecutionContext.java +++ /dev/null @@ -1,75 +0,0 @@ -/* - * This file is part of the L2J Mobius project. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * 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 - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package com.l2jmobius.gameserver.scripting; - -import java.nio.file.Path; -import java.util.HashMap; -import java.util.Map; - -/** - * @author HorridoJoho - * @param - */ -public abstract class AbstractExecutionContext implements IExecutionContext -{ - private final T _engine; - private final Map _properties; - private volatile Path _currentExecutingScipt; - - protected AbstractExecutionContext(T engine) - { - if (engine == null) - { - throw new IllegalArgumentException(); - } - _engine = engine; - _properties = new HashMap<>(); - } - - protected final void setCurrentExecutingScript(Path currentExecutingScript) - { - _currentExecutingScipt = currentExecutingScript; - } - - @Override - public final String setProperty(String key, String value) - { - return _properties.put(key, value); - } - - @Override - public final String getProperty(String key) - { - if (!_properties.containsKey(key)) - { - return _engine.getProperty(key); - } - return _properties.get(key); - } - - @Override - public final Path getCurrentExecutingScript() - { - return _currentExecutingScipt; - } - - @Override - public final T getScriptingEngine() - { - return _engine; - } -} diff --git a/L2J_Mobius_4.0_GrandCrusade/java/com/l2jmobius/gameserver/scripting/AbstractScriptingEngine.java b/L2J_Mobius_4.0_GrandCrusade/java/com/l2jmobius/gameserver/scripting/AbstractScriptingEngine.java deleted file mode 100644 index 90900438ec..0000000000 --- a/L2J_Mobius_4.0_GrandCrusade/java/com/l2jmobius/gameserver/scripting/AbstractScriptingEngine.java +++ /dev/null @@ -1,74 +0,0 @@ -/* - * This file is part of the L2J Mobius project. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * 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 - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package com.l2jmobius.gameserver.scripting; - -import java.util.Arrays; -import java.util.HashMap; -import java.util.Map; - -/** - * @author HorridoJoho - */ -public abstract class AbstractScriptingEngine implements IScriptingEngine -{ - private final String _engineName; - private final String _engineVersion; - private final String[] _commonFileExtensions; - private final Map _properties; - - protected AbstractScriptingEngine(String engineName, String engineVersion, String... commonFileExtensions) - { - if ((engineName == null) || engineName.isEmpty() || (engineVersion == null) || engineVersion.isEmpty() || (commonFileExtensions == null) || (commonFileExtensions.length == 0)) - { - throw new IllegalArgumentException(); - } - _engineName = engineName; - _engineVersion = engineVersion; - _commonFileExtensions = commonFileExtensions; - _properties = new HashMap<>(); - } - - @Override - public final String setProperty(String key, String value) - { - return _properties.put(key, value); - } - - @Override - public final String getProperty(String key) - { - return _properties.get(key); - } - - @Override - public final String getEngineName() - { - return _engineName; - } - - @Override - public final String getEngineVersion() - { - return _engineVersion; - } - - @Override - public final String[] getCommonFileExtensions() - { - return Arrays.copyOf(_commonFileExtensions, _commonFileExtensions.length); - } -} diff --git a/L2J_Mobius_4.0_GrandCrusade/java/com/l2jmobius/gameserver/scripting/IExecutionContext.java b/L2J_Mobius_4.0_GrandCrusade/java/com/l2jmobius/gameserver/scripting/IExecutionContext.java deleted file mode 100644 index eaf4860b8a..0000000000 --- a/L2J_Mobius_4.0_GrandCrusade/java/com/l2jmobius/gameserver/scripting/IExecutionContext.java +++ /dev/null @@ -1,71 +0,0 @@ -/* - * This file is part of the L2J Mobius project. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * 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 - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package com.l2jmobius.gameserver.scripting; - -import java.nio.file.Path; -import java.util.Map; -import java.util.Map.Entry; - -/** - * @author HorridoJoho - */ -public interface IExecutionContext -{ - /** - * Properties set here override the settings from the IScriptEngine
- * this class was created from. - * @param key the key - * @param value the value - * @return the previous value, or null when this key was not present before - */ - String setProperty(String key, String value); - - /** - * Executes all script in the iterable. - * @param sourcePaths the scripts to execute - * @return map of failed executions, Path=source file Throwable=thrown exception - * @throws Exception preparation for script execution failed - */ - Map executeScripts(Iterable sourcePaths) throws Exception; - - /** - * Executes a single file. - * @param sourcePath the script to execute - * @return entry of failed execution, Path=source file Throwable=thrown exception - * @throws Exception preparation for script execution failed - */ - Entry executeScript(Path sourcePath) throws Exception; - - /** - * Method to get the specified property value. - * @param key the key - * @return the value, or null if the key is not present - */ - String getProperty(String key); - - /** - * Method to get the current executing script file. - * @return the currently executing script file, null if non - */ - Path getCurrentExecutingScript(); - - /** - * Method to get the script engine this execution context belongs to. - * @return the script engine this execution context belongs to - */ - IScriptingEngine getScriptingEngine(); -} diff --git a/L2J_Mobius_4.0_GrandCrusade/java/com/l2jmobius/gameserver/scripting/IScriptingEngine.java b/L2J_Mobius_4.0_GrandCrusade/java/com/l2jmobius/gameserver/scripting/IScriptingEngine.java deleted file mode 100644 index 883172cce1..0000000000 --- a/L2J_Mobius_4.0_GrandCrusade/java/com/l2jmobius/gameserver/scripting/IScriptingEngine.java +++ /dev/null @@ -1,75 +0,0 @@ -/* - * This file is part of the L2J Mobius project. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * 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 - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package com.l2jmobius.gameserver.scripting; - -/** - * @author HorridoJoho - */ -public interface IScriptingEngine -{ - /** - * Sets script engine properties. The script values will be available
- * to the the insatnces created {@link IExecutionContext} implementation. - * @param key the key - * @param value the value - * @return the previous value, or null when this key was not present before - */ - String setProperty(String key, String value); - - /** - * Creates an execution context. - * @return the created execution context. - */ - IExecutionContext createExecutionContext(); - - /** - * Method to get the specified property value. - * @param key the key - * @return the value,or null if the key is not present - */ - String getProperty(String key); - - /** - * Method to get the engine name. - * @return the engine name - */ - String getEngineName(); - - /** - * Method to get the engine version. - * @return the engine version - */ - String getEngineVersion(); - - /** - * Method to get the scripting language name. - * @return the scripting engine name - */ - String getLanguageName(); - - /** - * Method to get the the language version. - * @return the language version - */ - String getLanguageVersion(); - - /** - * Method to retrive the commonly used file extensions for the language. - * @return the commonly used file extensions for the language - */ - String[] getCommonFileExtensions(); -} diff --git a/L2J_Mobius_4.0_GrandCrusade/java/com/l2jmobius/gameserver/scripting/ScriptEngineManager.java b/L2J_Mobius_4.0_GrandCrusade/java/com/l2jmobius/gameserver/scripting/ScriptEngineManager.java index 25a46c93b2..7f8d5fbd5b 100644 --- a/L2J_Mobius_4.0_GrandCrusade/java/com/l2jmobius/gameserver/scripting/ScriptEngineManager.java +++ b/L2J_Mobius_4.0_GrandCrusade/java/com/l2jmobius/gameserver/scripting/ScriptEngineManager.java @@ -17,7 +17,6 @@ package com.l2jmobius.gameserver.scripting; import java.io.File; -import java.io.FileInputStream; import java.io.IOException; import java.nio.file.FileVisitResult; import java.nio.file.Files; @@ -31,9 +30,6 @@ import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Map.Entry; -import java.util.Objects; -import java.util.Properties; -import java.util.ServiceLoader; import java.util.logging.Level; import java.util.logging.Logger; @@ -41,15 +37,16 @@ import org.w3c.dom.Document; import com.l2jmobius.Config; import com.l2jmobius.commons.util.IXmlReader; +import com.l2jmobius.gameserver.scripting.java.JavaExecutionContext; import com.l2jmobius.gameserver.scripting.java.JavaScriptingEngine; /** - * Caches script engines and provides functionality for executing and managing scripts. - * @author KenM, HorridoJoho + * @author Mobius */ public final class ScriptEngineManager implements IXmlReader { private static final Logger LOGGER = Logger.getLogger(ScriptEngineManager.class.getName()); + public static final Path SCRIPT_FOLDER = Config.SCRIPT_ROOT.toPath(); public static final Path MASTER_HANDLER_FILE = Paths.get(SCRIPT_FOLDER.toString(), "handlers", "MasterHandler.java"); public static final Path EFFECT_MASTER_HANDLER_FILE = Paths.get(SCRIPT_FOLDER.toString(), "handlers", "EffectMasterHandler.java"); @@ -57,19 +54,11 @@ public final class ScriptEngineManager implements IXmlReader public static final Path CONDITION_HANDLER_FILE = Paths.get(SCRIPT_FOLDER.toString(), "handlers", "ConditionMasterHandler.java"); public static final Path ONE_DAY_REWARD_MASTER_HANDLER = Paths.get(SCRIPT_FOLDER.toString(), "handlers", "DailyMissionMasterHandler.java"); - private IExecutionContext _javaExecutionContext = null; - static final List _exclusions = new ArrayList<>(); + private static final JavaExecutionContext _javaExecutionContext = new JavaScriptingEngine().createExecutionContext(); + protected static final List _exclusions = new ArrayList<>(); protected ScriptEngineManager() { - final Properties props = loadProperties(); - - // Default java engine implementation - registerEngine(new JavaScriptingEngine(), props); - - // Load external script engines - ServiceLoader.load(IScriptingEngine.class).forEach(engine -> registerEngine(engine, props)); - // Load Scripts.xml load(); } @@ -144,71 +133,6 @@ public final class ScriptEngineManager implements IXmlReader } } - private Properties loadProperties() - { - Properties props = null; - try (FileInputStream fis = new FileInputStream("config/ScriptEngine.ini")) - { - props = new Properties(); - props.load(fis); - } - catch (Exception e) - { - props = null; - LOGGER.warning("Couldn't load ScriptEngine.ini: " + e.getMessage()); - } - return props; - } - - private void registerEngine(IScriptingEngine engine, Properties props) - { - maybeSetProperties("language." + engine.getLanguageName() + ".", props, engine); - _javaExecutionContext = engine.createExecutionContext(); - LOGGER.info("ScriptEngine: " + engine.getEngineName() + " " + engine.getEngineVersion() + " (" + engine.getLanguageName() + " " + engine.getLanguageVersion() + ")"); - } - - private void maybeSetProperties(String propPrefix, Properties props, IScriptingEngine engine) - { - if (props == null) - { - return; - } - - for (Entry prop : props.entrySet()) - { - String key = (String) prop.getKey(); - String value = (String) prop.getValue(); - - if (key.startsWith(propPrefix)) - { - key = key.substring(propPrefix.length()); - if (value.startsWith("%") && value.endsWith("%")) - { - value = System.getProperty(value.substring(1, value.length() - 1)); - } - - engine.setProperty(key, value); - } - } - } - - public void executeScriptList() throws Exception - { - if (Config.ALT_DEV_NO_QUESTS) - { - return; - } - - final List files = new ArrayList<>(); - processDirectory(SCRIPT_FOLDER.toFile(), files); - - final Map invokationErrors = _javaExecutionContext.executeScripts(files); - for (Entry entry : invokationErrors.entrySet()) - { - LOGGER.log(Level.WARNING, "ScriptEngine: " + entry.getKey() + " failed execution!", entry.getValue()); - } - } - private void processDirectory(File dir, List files) { for (File file : dir.listFiles()) @@ -230,15 +154,12 @@ public final class ScriptEngineManager implements IXmlReader public void executeScript(Path sourceFile) throws Exception { - Objects.requireNonNull(sourceFile); - if (!sourceFile.isAbsolute()) { sourceFile = SCRIPT_FOLDER.resolve(sourceFile); } sourceFile = sourceFile.toAbsolutePath(); - Objects.requireNonNull(sourceFile, "ScriptFile: " + sourceFile + " does not have an extension to determine the script engine!"); final Entry error = _javaExecutionContext.executeScript(sourceFile); if (error != null) @@ -247,6 +168,23 @@ public final class ScriptEngineManager implements IXmlReader } } + public void executeScriptList() throws Exception + { + if (Config.ALT_DEV_NO_QUESTS) + { + return; + } + + final List files = new ArrayList<>(); + processDirectory(SCRIPT_FOLDER.toFile(), files); + + final Map invokationErrors = _javaExecutionContext.executeScripts(files); + for (Entry entry : invokationErrors.entrySet()) + { + LOGGER.log(Level.WARNING, "ScriptEngine: " + entry.getKey() + " failed execution!", entry.getValue()); + } + } + public Path getCurrentLoadingScript() { return _javaExecutionContext.getCurrentExecutingScript(); @@ -254,11 +192,11 @@ public final class ScriptEngineManager implements IXmlReader public static ScriptEngineManager getInstance() { - return SingletonHolder._instance; + return SingletonHolder.INSTANCE; } private static class SingletonHolder { - protected static final ScriptEngineManager _instance = new ScriptEngineManager(); + protected static final ScriptEngineManager INSTANCE = new ScriptEngineManager(); } } \ No newline at end of file diff --git a/L2J_Mobius_4.0_GrandCrusade/java/com/l2jmobius/gameserver/scripting/java/JavaCompilerException.java b/L2J_Mobius_4.0_GrandCrusade/java/com/l2jmobius/gameserver/scripting/java/JavaCompilerException.java deleted file mode 100644 index 241ffe91bd..0000000000 --- a/L2J_Mobius_4.0_GrandCrusade/java/com/l2jmobius/gameserver/scripting/java/JavaCompilerException.java +++ /dev/null @@ -1,28 +0,0 @@ -/* - * This file is part of the L2J Mobius project. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * 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 - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package com.l2jmobius.gameserver.scripting.java; - -/** - * @author HorridoJoho - */ -public final class JavaCompilerException extends RuntimeException -{ - public JavaCompilerException(String diagnostics) - { - super(diagnostics); - } -} diff --git a/L2J_Mobius_4.0_GrandCrusade/java/com/l2jmobius/gameserver/scripting/java/JavaExecutionContext.java b/L2J_Mobius_4.0_GrandCrusade/java/com/l2jmobius/gameserver/scripting/java/JavaExecutionContext.java index 70d4da0893..1eddebd693 100644 --- a/L2J_Mobius_4.0_GrandCrusade/java/com/l2jmobius/gameserver/scripting/java/JavaExecutionContext.java +++ b/L2J_Mobius_4.0_GrandCrusade/java/com/l2jmobius/gameserver/scripting/java/JavaExecutionContext.java @@ -22,9 +22,9 @@ import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.nio.charset.StandardCharsets; import java.nio.file.Path; +import java.util.ArrayList; import java.util.Arrays; -import java.util.LinkedHashMap; -import java.util.LinkedList; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; @@ -34,22 +34,20 @@ import org.openjavac.tools.Diagnostic; import org.openjavac.tools.DiagnosticCollector; import org.openjavac.tools.JavaFileObject; -import com.l2jmobius.gameserver.scripting.AbstractExecutionContext; import com.l2jmobius.gameserver.scripting.annotations.Disabled; /** * @author HorridoJoho */ -public final class JavaExecutionContext extends AbstractExecutionContext +public final class JavaExecutionContext extends JavaScriptingEngine { private static final Logger LOGGER = Logger.getLogger(JavaExecutionContext.class.getName()); - private static final List _options = new LinkedList<>(); + private static final List _options = new ArrayList<>(); + private static Path _currentExecutingScript; JavaExecutionContext(JavaScriptingEngine engine) { - super(engine); - // Set options. addOptionIfNotNull(_options, getProperty("source"), "-source"); addOptionIfNotNull(_options, getProperty("sourcepath"), "-sourcepath"); @@ -76,7 +74,7 @@ public final class JavaExecutionContext extends AbstractExecutionContext executeScripts(Iterable sourcePaths) throws Exception { final DiagnosticCollector fileManagerDiagnostics = new DiagnosticCollector<>(); final DiagnosticCollector compilationDiagnostics = new DiagnosticCollector<>(); - try (ScriptingFileManager fileManager = new ScriptingFileManager(getScriptingEngine().getCompiler().getStandardFileManager(fileManagerDiagnostics, null, StandardCharsets.UTF_8))) + try (ScriptingFileManager fileManager = new ScriptingFileManager(getCompiler().getStandardFileManager(fileManagerDiagnostics, null, StandardCharsets.UTF_8))) { // We really need an iterable of files or strings. - final List sourcePathStrings = new LinkedList<>(); + final List sourcePathStrings = new ArrayList<>(); for (Path sourcePath : sourcePaths) { sourcePathStrings.add(sourcePath.toString()); @@ -150,7 +147,7 @@ public final class JavaExecutionContext extends AbstractExecutionContext executionFailures = new LinkedHashMap<>(); + final Map executionFailures = new HashMap<>(); final Iterable compiledClasses = fileManager.getCompiledClasses(); for (Path sourcePath : sourcePaths) { @@ -199,7 +196,7 @@ public final class JavaExecutionContext extends AbstractExecutionContext executeScript(Path sourcePath) throws Exception { final Map executionFailures = executeScripts(Arrays.asList(sourcePath)); @@ -254,4 +250,9 @@ public final class JavaExecutionContext extends AbstractExecutionContext _properties = new HashMap<>(); + private final static JavaCompiler _compiler = JavacTool.create(); public JavaScriptingEngine() { - super("Java Engine", "10", "java"); - } - - private void determineCompilerOrThrow() - { - if (_compiler == null) + // Load config. + Properties props = new Properties(); + try (FileInputStream fis = new FileInputStream("config/ScriptEngine.ini")) { - _compiler = JavacTool.create(); + props.load(fis); + } + catch (Exception e) + { + LOGGER.warning("Could not load ScriptEngine.ini: " + e.getMessage()); } - if (_compiler == null) + // Set properties. + for (Entry prop : props.entrySet()) { - throw new IllegalStateException("No JavaCompiler service installed!"); - } - } - - private void ensureCompilerOrThrow() - { - if (_compiler == null) - { - synchronized (this) + String key = (String) prop.getKey(); + String value = (String) prop.getValue(); + + if (value.startsWith("%") && value.endsWith("%")) { - if (_compiler == null) - { - determineCompilerOrThrow(); - } + value = System.getProperty(value.substring(1, value.length() - 1)); } + + _properties.put(key, value); } } - JavaCompiler getCompiler() + public JavaExecutionContext createExecutionContext() { - return _compiler; - } - - @Override - public IExecutionContext createExecutionContext() - { - ensureCompilerOrThrow(); return new JavaExecutionContext(this); } - @Override - public String getLanguageName() + public final String getProperty(String key) { - return "Java"; + return _properties.get(key); } - @Override - public String getLanguageVersion() + public JavaCompiler getCompiler() { - ensureCompilerOrThrow(); - return Arrays.deepToString(_compiler.getSourceVersions().toArray(new SourceVersion[0])).replace("RELEASE_", ""); + return _compiler; } } \ No newline at end of file diff --git a/L2J_Mobius_4.0_GrandCrusade/java/com/l2jmobius/gameserver/scripting/java/ScriptingOutputFileObject.java b/L2J_Mobius_4.0_GrandCrusade/java/com/l2jmobius/gameserver/scripting/java/ScriptingOutputFileObject.java index 439cf5d260..9ab668887e 100644 --- a/L2J_Mobius_4.0_GrandCrusade/java/com/l2jmobius/gameserver/scripting/java/ScriptingOutputFileObject.java +++ b/L2J_Mobius_4.0_GrandCrusade/java/com/l2jmobius/gameserver/scripting/java/ScriptingOutputFileObject.java @@ -144,5 +144,4 @@ final class ScriptingOutputFileObject implements JavaFileObject { return null; } - } diff --git a/L2J_Mobius_5.0_Salvation/dist/game/config/ScriptEngine.ini b/L2J_Mobius_5.0_Salvation/dist/game/config/ScriptEngine.ini index 2d1e972e72..5004dca9fd 100644 --- a/L2J_Mobius_5.0_Salvation/dist/game/config/ScriptEngine.ini +++ b/L2J_Mobius_5.0_Salvation/dist/game/config/ScriptEngine.ini @@ -1,23 +1,23 @@ -############################################################################### -# Properties retrived with System.getProperty(String) can be used as values +# --------------------------------------------------------------------------- +# Script Engine Settings +# --------------------------------------------------------------------------- +# Properties retrieved with System.getProperty(String) can be used as values # by enclosing the property name with %. Eg.: %java.class.path% -############################################################################### - # The parent class loader for isolated script class loaders. # When this property is not specified, has an invalid value or is a class name which could not be found, the System classloader is used. # Values: System, ThreadContext or a fully qualified java class name -language.Java.classloader=System -#language.Java.classloader=ThreadContext +classloader=System +#classloader=ThreadContext -# Source compatibility -language.Java.source=1.8 +# Source compatibility. +source=1.8 # The java sourcepath, when you have a different datapack root, you must change this too. -language.Java.sourcepath=data/scripts +sourcepath=data/scripts -# The java classpath -language.Java.cp=%java.class.path% +# The java classpath. +cp=%java.class.path% -# The debug informations to generate for compiled class files -language.Java.g=source,lines,vars \ No newline at end of file +# The debug informations to generate for compiled class files. +g=source,lines,vars \ No newline at end of file diff --git a/L2J_Mobius_5.0_Salvation/java/com/l2jmobius/gameserver/scripting/AbstractExecutionContext.java b/L2J_Mobius_5.0_Salvation/java/com/l2jmobius/gameserver/scripting/AbstractExecutionContext.java deleted file mode 100644 index 296b54f06b..0000000000 --- a/L2J_Mobius_5.0_Salvation/java/com/l2jmobius/gameserver/scripting/AbstractExecutionContext.java +++ /dev/null @@ -1,75 +0,0 @@ -/* - * This file is part of the L2J Mobius project. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * 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 - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package com.l2jmobius.gameserver.scripting; - -import java.nio.file.Path; -import java.util.HashMap; -import java.util.Map; - -/** - * @author HorridoJoho - * @param - */ -public abstract class AbstractExecutionContext implements IExecutionContext -{ - private final T _engine; - private final Map _properties; - private volatile Path _currentExecutingScipt; - - protected AbstractExecutionContext(T engine) - { - if (engine == null) - { - throw new IllegalArgumentException(); - } - _engine = engine; - _properties = new HashMap<>(); - } - - protected final void setCurrentExecutingScript(Path currentExecutingScript) - { - _currentExecutingScipt = currentExecutingScript; - } - - @Override - public final String setProperty(String key, String value) - { - return _properties.put(key, value); - } - - @Override - public final String getProperty(String key) - { - if (!_properties.containsKey(key)) - { - return _engine.getProperty(key); - } - return _properties.get(key); - } - - @Override - public final Path getCurrentExecutingScript() - { - return _currentExecutingScipt; - } - - @Override - public final T getScriptingEngine() - { - return _engine; - } -} diff --git a/L2J_Mobius_5.0_Salvation/java/com/l2jmobius/gameserver/scripting/AbstractScriptingEngine.java b/L2J_Mobius_5.0_Salvation/java/com/l2jmobius/gameserver/scripting/AbstractScriptingEngine.java deleted file mode 100644 index 90900438ec..0000000000 --- a/L2J_Mobius_5.0_Salvation/java/com/l2jmobius/gameserver/scripting/AbstractScriptingEngine.java +++ /dev/null @@ -1,74 +0,0 @@ -/* - * This file is part of the L2J Mobius project. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * 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 - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package com.l2jmobius.gameserver.scripting; - -import java.util.Arrays; -import java.util.HashMap; -import java.util.Map; - -/** - * @author HorridoJoho - */ -public abstract class AbstractScriptingEngine implements IScriptingEngine -{ - private final String _engineName; - private final String _engineVersion; - private final String[] _commonFileExtensions; - private final Map _properties; - - protected AbstractScriptingEngine(String engineName, String engineVersion, String... commonFileExtensions) - { - if ((engineName == null) || engineName.isEmpty() || (engineVersion == null) || engineVersion.isEmpty() || (commonFileExtensions == null) || (commonFileExtensions.length == 0)) - { - throw new IllegalArgumentException(); - } - _engineName = engineName; - _engineVersion = engineVersion; - _commonFileExtensions = commonFileExtensions; - _properties = new HashMap<>(); - } - - @Override - public final String setProperty(String key, String value) - { - return _properties.put(key, value); - } - - @Override - public final String getProperty(String key) - { - return _properties.get(key); - } - - @Override - public final String getEngineName() - { - return _engineName; - } - - @Override - public final String getEngineVersion() - { - return _engineVersion; - } - - @Override - public final String[] getCommonFileExtensions() - { - return Arrays.copyOf(_commonFileExtensions, _commonFileExtensions.length); - } -} diff --git a/L2J_Mobius_5.0_Salvation/java/com/l2jmobius/gameserver/scripting/IExecutionContext.java b/L2J_Mobius_5.0_Salvation/java/com/l2jmobius/gameserver/scripting/IExecutionContext.java deleted file mode 100644 index eaf4860b8a..0000000000 --- a/L2J_Mobius_5.0_Salvation/java/com/l2jmobius/gameserver/scripting/IExecutionContext.java +++ /dev/null @@ -1,71 +0,0 @@ -/* - * This file is part of the L2J Mobius project. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * 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 - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package com.l2jmobius.gameserver.scripting; - -import java.nio.file.Path; -import java.util.Map; -import java.util.Map.Entry; - -/** - * @author HorridoJoho - */ -public interface IExecutionContext -{ - /** - * Properties set here override the settings from the IScriptEngine
- * this class was created from. - * @param key the key - * @param value the value - * @return the previous value, or null when this key was not present before - */ - String setProperty(String key, String value); - - /** - * Executes all script in the iterable. - * @param sourcePaths the scripts to execute - * @return map of failed executions, Path=source file Throwable=thrown exception - * @throws Exception preparation for script execution failed - */ - Map executeScripts(Iterable sourcePaths) throws Exception; - - /** - * Executes a single file. - * @param sourcePath the script to execute - * @return entry of failed execution, Path=source file Throwable=thrown exception - * @throws Exception preparation for script execution failed - */ - Entry executeScript(Path sourcePath) throws Exception; - - /** - * Method to get the specified property value. - * @param key the key - * @return the value, or null if the key is not present - */ - String getProperty(String key); - - /** - * Method to get the current executing script file. - * @return the currently executing script file, null if non - */ - Path getCurrentExecutingScript(); - - /** - * Method to get the script engine this execution context belongs to. - * @return the script engine this execution context belongs to - */ - IScriptingEngine getScriptingEngine(); -} diff --git a/L2J_Mobius_5.0_Salvation/java/com/l2jmobius/gameserver/scripting/IScriptingEngine.java b/L2J_Mobius_5.0_Salvation/java/com/l2jmobius/gameserver/scripting/IScriptingEngine.java deleted file mode 100644 index 883172cce1..0000000000 --- a/L2J_Mobius_5.0_Salvation/java/com/l2jmobius/gameserver/scripting/IScriptingEngine.java +++ /dev/null @@ -1,75 +0,0 @@ -/* - * This file is part of the L2J Mobius project. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * 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 - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package com.l2jmobius.gameserver.scripting; - -/** - * @author HorridoJoho - */ -public interface IScriptingEngine -{ - /** - * Sets script engine properties. The script values will be available
- * to the the insatnces created {@link IExecutionContext} implementation. - * @param key the key - * @param value the value - * @return the previous value, or null when this key was not present before - */ - String setProperty(String key, String value); - - /** - * Creates an execution context. - * @return the created execution context. - */ - IExecutionContext createExecutionContext(); - - /** - * Method to get the specified property value. - * @param key the key - * @return the value,or null if the key is not present - */ - String getProperty(String key); - - /** - * Method to get the engine name. - * @return the engine name - */ - String getEngineName(); - - /** - * Method to get the engine version. - * @return the engine version - */ - String getEngineVersion(); - - /** - * Method to get the scripting language name. - * @return the scripting engine name - */ - String getLanguageName(); - - /** - * Method to get the the language version. - * @return the language version - */ - String getLanguageVersion(); - - /** - * Method to retrive the commonly used file extensions for the language. - * @return the commonly used file extensions for the language - */ - String[] getCommonFileExtensions(); -} diff --git a/L2J_Mobius_5.0_Salvation/java/com/l2jmobius/gameserver/scripting/ScriptEngineManager.java b/L2J_Mobius_5.0_Salvation/java/com/l2jmobius/gameserver/scripting/ScriptEngineManager.java index 25a46c93b2..7f8d5fbd5b 100644 --- a/L2J_Mobius_5.0_Salvation/java/com/l2jmobius/gameserver/scripting/ScriptEngineManager.java +++ b/L2J_Mobius_5.0_Salvation/java/com/l2jmobius/gameserver/scripting/ScriptEngineManager.java @@ -17,7 +17,6 @@ package com.l2jmobius.gameserver.scripting; import java.io.File; -import java.io.FileInputStream; import java.io.IOException; import java.nio.file.FileVisitResult; import java.nio.file.Files; @@ -31,9 +30,6 @@ import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Map.Entry; -import java.util.Objects; -import java.util.Properties; -import java.util.ServiceLoader; import java.util.logging.Level; import java.util.logging.Logger; @@ -41,15 +37,16 @@ import org.w3c.dom.Document; import com.l2jmobius.Config; import com.l2jmobius.commons.util.IXmlReader; +import com.l2jmobius.gameserver.scripting.java.JavaExecutionContext; import com.l2jmobius.gameserver.scripting.java.JavaScriptingEngine; /** - * Caches script engines and provides functionality for executing and managing scripts. - * @author KenM, HorridoJoho + * @author Mobius */ public final class ScriptEngineManager implements IXmlReader { private static final Logger LOGGER = Logger.getLogger(ScriptEngineManager.class.getName()); + public static final Path SCRIPT_FOLDER = Config.SCRIPT_ROOT.toPath(); public static final Path MASTER_HANDLER_FILE = Paths.get(SCRIPT_FOLDER.toString(), "handlers", "MasterHandler.java"); public static final Path EFFECT_MASTER_HANDLER_FILE = Paths.get(SCRIPT_FOLDER.toString(), "handlers", "EffectMasterHandler.java"); @@ -57,19 +54,11 @@ public final class ScriptEngineManager implements IXmlReader public static final Path CONDITION_HANDLER_FILE = Paths.get(SCRIPT_FOLDER.toString(), "handlers", "ConditionMasterHandler.java"); public static final Path ONE_DAY_REWARD_MASTER_HANDLER = Paths.get(SCRIPT_FOLDER.toString(), "handlers", "DailyMissionMasterHandler.java"); - private IExecutionContext _javaExecutionContext = null; - static final List _exclusions = new ArrayList<>(); + private static final JavaExecutionContext _javaExecutionContext = new JavaScriptingEngine().createExecutionContext(); + protected static final List _exclusions = new ArrayList<>(); protected ScriptEngineManager() { - final Properties props = loadProperties(); - - // Default java engine implementation - registerEngine(new JavaScriptingEngine(), props); - - // Load external script engines - ServiceLoader.load(IScriptingEngine.class).forEach(engine -> registerEngine(engine, props)); - // Load Scripts.xml load(); } @@ -144,71 +133,6 @@ public final class ScriptEngineManager implements IXmlReader } } - private Properties loadProperties() - { - Properties props = null; - try (FileInputStream fis = new FileInputStream("config/ScriptEngine.ini")) - { - props = new Properties(); - props.load(fis); - } - catch (Exception e) - { - props = null; - LOGGER.warning("Couldn't load ScriptEngine.ini: " + e.getMessage()); - } - return props; - } - - private void registerEngine(IScriptingEngine engine, Properties props) - { - maybeSetProperties("language." + engine.getLanguageName() + ".", props, engine); - _javaExecutionContext = engine.createExecutionContext(); - LOGGER.info("ScriptEngine: " + engine.getEngineName() + " " + engine.getEngineVersion() + " (" + engine.getLanguageName() + " " + engine.getLanguageVersion() + ")"); - } - - private void maybeSetProperties(String propPrefix, Properties props, IScriptingEngine engine) - { - if (props == null) - { - return; - } - - for (Entry prop : props.entrySet()) - { - String key = (String) prop.getKey(); - String value = (String) prop.getValue(); - - if (key.startsWith(propPrefix)) - { - key = key.substring(propPrefix.length()); - if (value.startsWith("%") && value.endsWith("%")) - { - value = System.getProperty(value.substring(1, value.length() - 1)); - } - - engine.setProperty(key, value); - } - } - } - - public void executeScriptList() throws Exception - { - if (Config.ALT_DEV_NO_QUESTS) - { - return; - } - - final List files = new ArrayList<>(); - processDirectory(SCRIPT_FOLDER.toFile(), files); - - final Map invokationErrors = _javaExecutionContext.executeScripts(files); - for (Entry entry : invokationErrors.entrySet()) - { - LOGGER.log(Level.WARNING, "ScriptEngine: " + entry.getKey() + " failed execution!", entry.getValue()); - } - } - private void processDirectory(File dir, List files) { for (File file : dir.listFiles()) @@ -230,15 +154,12 @@ public final class ScriptEngineManager implements IXmlReader public void executeScript(Path sourceFile) throws Exception { - Objects.requireNonNull(sourceFile); - if (!sourceFile.isAbsolute()) { sourceFile = SCRIPT_FOLDER.resolve(sourceFile); } sourceFile = sourceFile.toAbsolutePath(); - Objects.requireNonNull(sourceFile, "ScriptFile: " + sourceFile + " does not have an extension to determine the script engine!"); final Entry error = _javaExecutionContext.executeScript(sourceFile); if (error != null) @@ -247,6 +168,23 @@ public final class ScriptEngineManager implements IXmlReader } } + public void executeScriptList() throws Exception + { + if (Config.ALT_DEV_NO_QUESTS) + { + return; + } + + final List files = new ArrayList<>(); + processDirectory(SCRIPT_FOLDER.toFile(), files); + + final Map invokationErrors = _javaExecutionContext.executeScripts(files); + for (Entry entry : invokationErrors.entrySet()) + { + LOGGER.log(Level.WARNING, "ScriptEngine: " + entry.getKey() + " failed execution!", entry.getValue()); + } + } + public Path getCurrentLoadingScript() { return _javaExecutionContext.getCurrentExecutingScript(); @@ -254,11 +192,11 @@ public final class ScriptEngineManager implements IXmlReader public static ScriptEngineManager getInstance() { - return SingletonHolder._instance; + return SingletonHolder.INSTANCE; } private static class SingletonHolder { - protected static final ScriptEngineManager _instance = new ScriptEngineManager(); + protected static final ScriptEngineManager INSTANCE = new ScriptEngineManager(); } } \ No newline at end of file diff --git a/L2J_Mobius_5.0_Salvation/java/com/l2jmobius/gameserver/scripting/java/JavaCompilerException.java b/L2J_Mobius_5.0_Salvation/java/com/l2jmobius/gameserver/scripting/java/JavaCompilerException.java deleted file mode 100644 index 241ffe91bd..0000000000 --- a/L2J_Mobius_5.0_Salvation/java/com/l2jmobius/gameserver/scripting/java/JavaCompilerException.java +++ /dev/null @@ -1,28 +0,0 @@ -/* - * This file is part of the L2J Mobius project. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * 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 - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package com.l2jmobius.gameserver.scripting.java; - -/** - * @author HorridoJoho - */ -public final class JavaCompilerException extends RuntimeException -{ - public JavaCompilerException(String diagnostics) - { - super(diagnostics); - } -} diff --git a/L2J_Mobius_5.0_Salvation/java/com/l2jmobius/gameserver/scripting/java/JavaExecutionContext.java b/L2J_Mobius_5.0_Salvation/java/com/l2jmobius/gameserver/scripting/java/JavaExecutionContext.java index 70d4da0893..1eddebd693 100644 --- a/L2J_Mobius_5.0_Salvation/java/com/l2jmobius/gameserver/scripting/java/JavaExecutionContext.java +++ b/L2J_Mobius_5.0_Salvation/java/com/l2jmobius/gameserver/scripting/java/JavaExecutionContext.java @@ -22,9 +22,9 @@ import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.nio.charset.StandardCharsets; import java.nio.file.Path; +import java.util.ArrayList; import java.util.Arrays; -import java.util.LinkedHashMap; -import java.util.LinkedList; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; @@ -34,22 +34,20 @@ import org.openjavac.tools.Diagnostic; import org.openjavac.tools.DiagnosticCollector; import org.openjavac.tools.JavaFileObject; -import com.l2jmobius.gameserver.scripting.AbstractExecutionContext; import com.l2jmobius.gameserver.scripting.annotations.Disabled; /** * @author HorridoJoho */ -public final class JavaExecutionContext extends AbstractExecutionContext +public final class JavaExecutionContext extends JavaScriptingEngine { private static final Logger LOGGER = Logger.getLogger(JavaExecutionContext.class.getName()); - private static final List _options = new LinkedList<>(); + private static final List _options = new ArrayList<>(); + private static Path _currentExecutingScript; JavaExecutionContext(JavaScriptingEngine engine) { - super(engine); - // Set options. addOptionIfNotNull(_options, getProperty("source"), "-source"); addOptionIfNotNull(_options, getProperty("sourcepath"), "-sourcepath"); @@ -76,7 +74,7 @@ public final class JavaExecutionContext extends AbstractExecutionContext executeScripts(Iterable sourcePaths) throws Exception { final DiagnosticCollector fileManagerDiagnostics = new DiagnosticCollector<>(); final DiagnosticCollector compilationDiagnostics = new DiagnosticCollector<>(); - try (ScriptingFileManager fileManager = new ScriptingFileManager(getScriptingEngine().getCompiler().getStandardFileManager(fileManagerDiagnostics, null, StandardCharsets.UTF_8))) + try (ScriptingFileManager fileManager = new ScriptingFileManager(getCompiler().getStandardFileManager(fileManagerDiagnostics, null, StandardCharsets.UTF_8))) { // We really need an iterable of files or strings. - final List sourcePathStrings = new LinkedList<>(); + final List sourcePathStrings = new ArrayList<>(); for (Path sourcePath : sourcePaths) { sourcePathStrings.add(sourcePath.toString()); @@ -150,7 +147,7 @@ public final class JavaExecutionContext extends AbstractExecutionContext executionFailures = new LinkedHashMap<>(); + final Map executionFailures = new HashMap<>(); final Iterable compiledClasses = fileManager.getCompiledClasses(); for (Path sourcePath : sourcePaths) { @@ -199,7 +196,7 @@ public final class JavaExecutionContext extends AbstractExecutionContext executeScript(Path sourcePath) throws Exception { final Map executionFailures = executeScripts(Arrays.asList(sourcePath)); @@ -254,4 +250,9 @@ public final class JavaExecutionContext extends AbstractExecutionContext _properties = new HashMap<>(); + private final static JavaCompiler _compiler = JavacTool.create(); public JavaScriptingEngine() { - super("Java Engine", "10", "java"); - } - - private void determineCompilerOrThrow() - { - if (_compiler == null) + // Load config. + Properties props = new Properties(); + try (FileInputStream fis = new FileInputStream("config/ScriptEngine.ini")) { - _compiler = JavacTool.create(); + props.load(fis); + } + catch (Exception e) + { + LOGGER.warning("Could not load ScriptEngine.ini: " + e.getMessage()); } - if (_compiler == null) + // Set properties. + for (Entry prop : props.entrySet()) { - throw new IllegalStateException("No JavaCompiler service installed!"); - } - } - - private void ensureCompilerOrThrow() - { - if (_compiler == null) - { - synchronized (this) + String key = (String) prop.getKey(); + String value = (String) prop.getValue(); + + if (value.startsWith("%") && value.endsWith("%")) { - if (_compiler == null) - { - determineCompilerOrThrow(); - } + value = System.getProperty(value.substring(1, value.length() - 1)); } + + _properties.put(key, value); } } - JavaCompiler getCompiler() + public JavaExecutionContext createExecutionContext() { - return _compiler; - } - - @Override - public IExecutionContext createExecutionContext() - { - ensureCompilerOrThrow(); return new JavaExecutionContext(this); } - @Override - public String getLanguageName() + public final String getProperty(String key) { - return "Java"; + return _properties.get(key); } - @Override - public String getLanguageVersion() + public JavaCompiler getCompiler() { - ensureCompilerOrThrow(); - return Arrays.deepToString(_compiler.getSourceVersions().toArray(new SourceVersion[0])).replace("RELEASE_", ""); + return _compiler; } } \ No newline at end of file diff --git a/L2J_Mobius_5.0_Salvation/java/com/l2jmobius/gameserver/scripting/java/ScriptingOutputFileObject.java b/L2J_Mobius_5.0_Salvation/java/com/l2jmobius/gameserver/scripting/java/ScriptingOutputFileObject.java index 439cf5d260..9ab668887e 100644 --- a/L2J_Mobius_5.0_Salvation/java/com/l2jmobius/gameserver/scripting/java/ScriptingOutputFileObject.java +++ b/L2J_Mobius_5.0_Salvation/java/com/l2jmobius/gameserver/scripting/java/ScriptingOutputFileObject.java @@ -144,5 +144,4 @@ final class ScriptingOutputFileObject implements JavaFileObject { return null; } - } diff --git a/L2J_Mobius_5.5_EtinasFate/dist/game/config/ScriptEngine.ini b/L2J_Mobius_5.5_EtinasFate/dist/game/config/ScriptEngine.ini index 2d1e972e72..5004dca9fd 100644 --- a/L2J_Mobius_5.5_EtinasFate/dist/game/config/ScriptEngine.ini +++ b/L2J_Mobius_5.5_EtinasFate/dist/game/config/ScriptEngine.ini @@ -1,23 +1,23 @@ -############################################################################### -# Properties retrived with System.getProperty(String) can be used as values +# --------------------------------------------------------------------------- +# Script Engine Settings +# --------------------------------------------------------------------------- +# Properties retrieved with System.getProperty(String) can be used as values # by enclosing the property name with %. Eg.: %java.class.path% -############################################################################### - # The parent class loader for isolated script class loaders. # When this property is not specified, has an invalid value or is a class name which could not be found, the System classloader is used. # Values: System, ThreadContext or a fully qualified java class name -language.Java.classloader=System -#language.Java.classloader=ThreadContext +classloader=System +#classloader=ThreadContext -# Source compatibility -language.Java.source=1.8 +# Source compatibility. +source=1.8 # The java sourcepath, when you have a different datapack root, you must change this too. -language.Java.sourcepath=data/scripts +sourcepath=data/scripts -# The java classpath -language.Java.cp=%java.class.path% +# The java classpath. +cp=%java.class.path% -# The debug informations to generate for compiled class files -language.Java.g=source,lines,vars \ No newline at end of file +# The debug informations to generate for compiled class files. +g=source,lines,vars \ No newline at end of file diff --git a/L2J_Mobius_5.5_EtinasFate/java/com/l2jmobius/gameserver/scripting/AbstractExecutionContext.java b/L2J_Mobius_5.5_EtinasFate/java/com/l2jmobius/gameserver/scripting/AbstractExecutionContext.java deleted file mode 100644 index 296b54f06b..0000000000 --- a/L2J_Mobius_5.5_EtinasFate/java/com/l2jmobius/gameserver/scripting/AbstractExecutionContext.java +++ /dev/null @@ -1,75 +0,0 @@ -/* - * This file is part of the L2J Mobius project. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * 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 - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package com.l2jmobius.gameserver.scripting; - -import java.nio.file.Path; -import java.util.HashMap; -import java.util.Map; - -/** - * @author HorridoJoho - * @param - */ -public abstract class AbstractExecutionContext implements IExecutionContext -{ - private final T _engine; - private final Map _properties; - private volatile Path _currentExecutingScipt; - - protected AbstractExecutionContext(T engine) - { - if (engine == null) - { - throw new IllegalArgumentException(); - } - _engine = engine; - _properties = new HashMap<>(); - } - - protected final void setCurrentExecutingScript(Path currentExecutingScript) - { - _currentExecutingScipt = currentExecutingScript; - } - - @Override - public final String setProperty(String key, String value) - { - return _properties.put(key, value); - } - - @Override - public final String getProperty(String key) - { - if (!_properties.containsKey(key)) - { - return _engine.getProperty(key); - } - return _properties.get(key); - } - - @Override - public final Path getCurrentExecutingScript() - { - return _currentExecutingScipt; - } - - @Override - public final T getScriptingEngine() - { - return _engine; - } -} diff --git a/L2J_Mobius_5.5_EtinasFate/java/com/l2jmobius/gameserver/scripting/AbstractScriptingEngine.java b/L2J_Mobius_5.5_EtinasFate/java/com/l2jmobius/gameserver/scripting/AbstractScriptingEngine.java deleted file mode 100644 index 90900438ec..0000000000 --- a/L2J_Mobius_5.5_EtinasFate/java/com/l2jmobius/gameserver/scripting/AbstractScriptingEngine.java +++ /dev/null @@ -1,74 +0,0 @@ -/* - * This file is part of the L2J Mobius project. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * 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 - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package com.l2jmobius.gameserver.scripting; - -import java.util.Arrays; -import java.util.HashMap; -import java.util.Map; - -/** - * @author HorridoJoho - */ -public abstract class AbstractScriptingEngine implements IScriptingEngine -{ - private final String _engineName; - private final String _engineVersion; - private final String[] _commonFileExtensions; - private final Map _properties; - - protected AbstractScriptingEngine(String engineName, String engineVersion, String... commonFileExtensions) - { - if ((engineName == null) || engineName.isEmpty() || (engineVersion == null) || engineVersion.isEmpty() || (commonFileExtensions == null) || (commonFileExtensions.length == 0)) - { - throw new IllegalArgumentException(); - } - _engineName = engineName; - _engineVersion = engineVersion; - _commonFileExtensions = commonFileExtensions; - _properties = new HashMap<>(); - } - - @Override - public final String setProperty(String key, String value) - { - return _properties.put(key, value); - } - - @Override - public final String getProperty(String key) - { - return _properties.get(key); - } - - @Override - public final String getEngineName() - { - return _engineName; - } - - @Override - public final String getEngineVersion() - { - return _engineVersion; - } - - @Override - public final String[] getCommonFileExtensions() - { - return Arrays.copyOf(_commonFileExtensions, _commonFileExtensions.length); - } -} diff --git a/L2J_Mobius_5.5_EtinasFate/java/com/l2jmobius/gameserver/scripting/IExecutionContext.java b/L2J_Mobius_5.5_EtinasFate/java/com/l2jmobius/gameserver/scripting/IExecutionContext.java deleted file mode 100644 index eaf4860b8a..0000000000 --- a/L2J_Mobius_5.5_EtinasFate/java/com/l2jmobius/gameserver/scripting/IExecutionContext.java +++ /dev/null @@ -1,71 +0,0 @@ -/* - * This file is part of the L2J Mobius project. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * 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 - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package com.l2jmobius.gameserver.scripting; - -import java.nio.file.Path; -import java.util.Map; -import java.util.Map.Entry; - -/** - * @author HorridoJoho - */ -public interface IExecutionContext -{ - /** - * Properties set here override the settings from the IScriptEngine
- * this class was created from. - * @param key the key - * @param value the value - * @return the previous value, or null when this key was not present before - */ - String setProperty(String key, String value); - - /** - * Executes all script in the iterable. - * @param sourcePaths the scripts to execute - * @return map of failed executions, Path=source file Throwable=thrown exception - * @throws Exception preparation for script execution failed - */ - Map executeScripts(Iterable sourcePaths) throws Exception; - - /** - * Executes a single file. - * @param sourcePath the script to execute - * @return entry of failed execution, Path=source file Throwable=thrown exception - * @throws Exception preparation for script execution failed - */ - Entry executeScript(Path sourcePath) throws Exception; - - /** - * Method to get the specified property value. - * @param key the key - * @return the value, or null if the key is not present - */ - String getProperty(String key); - - /** - * Method to get the current executing script file. - * @return the currently executing script file, null if non - */ - Path getCurrentExecutingScript(); - - /** - * Method to get the script engine this execution context belongs to. - * @return the script engine this execution context belongs to - */ - IScriptingEngine getScriptingEngine(); -} diff --git a/L2J_Mobius_5.5_EtinasFate/java/com/l2jmobius/gameserver/scripting/IScriptingEngine.java b/L2J_Mobius_5.5_EtinasFate/java/com/l2jmobius/gameserver/scripting/IScriptingEngine.java deleted file mode 100644 index 883172cce1..0000000000 --- a/L2J_Mobius_5.5_EtinasFate/java/com/l2jmobius/gameserver/scripting/IScriptingEngine.java +++ /dev/null @@ -1,75 +0,0 @@ -/* - * This file is part of the L2J Mobius project. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * 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 - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package com.l2jmobius.gameserver.scripting; - -/** - * @author HorridoJoho - */ -public interface IScriptingEngine -{ - /** - * Sets script engine properties. The script values will be available
- * to the the insatnces created {@link IExecutionContext} implementation. - * @param key the key - * @param value the value - * @return the previous value, or null when this key was not present before - */ - String setProperty(String key, String value); - - /** - * Creates an execution context. - * @return the created execution context. - */ - IExecutionContext createExecutionContext(); - - /** - * Method to get the specified property value. - * @param key the key - * @return the value,or null if the key is not present - */ - String getProperty(String key); - - /** - * Method to get the engine name. - * @return the engine name - */ - String getEngineName(); - - /** - * Method to get the engine version. - * @return the engine version - */ - String getEngineVersion(); - - /** - * Method to get the scripting language name. - * @return the scripting engine name - */ - String getLanguageName(); - - /** - * Method to get the the language version. - * @return the language version - */ - String getLanguageVersion(); - - /** - * Method to retrive the commonly used file extensions for the language. - * @return the commonly used file extensions for the language - */ - String[] getCommonFileExtensions(); -} diff --git a/L2J_Mobius_5.5_EtinasFate/java/com/l2jmobius/gameserver/scripting/ScriptEngineManager.java b/L2J_Mobius_5.5_EtinasFate/java/com/l2jmobius/gameserver/scripting/ScriptEngineManager.java index 25a46c93b2..7f8d5fbd5b 100644 --- a/L2J_Mobius_5.5_EtinasFate/java/com/l2jmobius/gameserver/scripting/ScriptEngineManager.java +++ b/L2J_Mobius_5.5_EtinasFate/java/com/l2jmobius/gameserver/scripting/ScriptEngineManager.java @@ -17,7 +17,6 @@ package com.l2jmobius.gameserver.scripting; import java.io.File; -import java.io.FileInputStream; import java.io.IOException; import java.nio.file.FileVisitResult; import java.nio.file.Files; @@ -31,9 +30,6 @@ import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Map.Entry; -import java.util.Objects; -import java.util.Properties; -import java.util.ServiceLoader; import java.util.logging.Level; import java.util.logging.Logger; @@ -41,15 +37,16 @@ import org.w3c.dom.Document; import com.l2jmobius.Config; import com.l2jmobius.commons.util.IXmlReader; +import com.l2jmobius.gameserver.scripting.java.JavaExecutionContext; import com.l2jmobius.gameserver.scripting.java.JavaScriptingEngine; /** - * Caches script engines and provides functionality for executing and managing scripts. - * @author KenM, HorridoJoho + * @author Mobius */ public final class ScriptEngineManager implements IXmlReader { private static final Logger LOGGER = Logger.getLogger(ScriptEngineManager.class.getName()); + public static final Path SCRIPT_FOLDER = Config.SCRIPT_ROOT.toPath(); public static final Path MASTER_HANDLER_FILE = Paths.get(SCRIPT_FOLDER.toString(), "handlers", "MasterHandler.java"); public static final Path EFFECT_MASTER_HANDLER_FILE = Paths.get(SCRIPT_FOLDER.toString(), "handlers", "EffectMasterHandler.java"); @@ -57,19 +54,11 @@ public final class ScriptEngineManager implements IXmlReader public static final Path CONDITION_HANDLER_FILE = Paths.get(SCRIPT_FOLDER.toString(), "handlers", "ConditionMasterHandler.java"); public static final Path ONE_DAY_REWARD_MASTER_HANDLER = Paths.get(SCRIPT_FOLDER.toString(), "handlers", "DailyMissionMasterHandler.java"); - private IExecutionContext _javaExecutionContext = null; - static final List _exclusions = new ArrayList<>(); + private static final JavaExecutionContext _javaExecutionContext = new JavaScriptingEngine().createExecutionContext(); + protected static final List _exclusions = new ArrayList<>(); protected ScriptEngineManager() { - final Properties props = loadProperties(); - - // Default java engine implementation - registerEngine(new JavaScriptingEngine(), props); - - // Load external script engines - ServiceLoader.load(IScriptingEngine.class).forEach(engine -> registerEngine(engine, props)); - // Load Scripts.xml load(); } @@ -144,71 +133,6 @@ public final class ScriptEngineManager implements IXmlReader } } - private Properties loadProperties() - { - Properties props = null; - try (FileInputStream fis = new FileInputStream("config/ScriptEngine.ini")) - { - props = new Properties(); - props.load(fis); - } - catch (Exception e) - { - props = null; - LOGGER.warning("Couldn't load ScriptEngine.ini: " + e.getMessage()); - } - return props; - } - - private void registerEngine(IScriptingEngine engine, Properties props) - { - maybeSetProperties("language." + engine.getLanguageName() + ".", props, engine); - _javaExecutionContext = engine.createExecutionContext(); - LOGGER.info("ScriptEngine: " + engine.getEngineName() + " " + engine.getEngineVersion() + " (" + engine.getLanguageName() + " " + engine.getLanguageVersion() + ")"); - } - - private void maybeSetProperties(String propPrefix, Properties props, IScriptingEngine engine) - { - if (props == null) - { - return; - } - - for (Entry prop : props.entrySet()) - { - String key = (String) prop.getKey(); - String value = (String) prop.getValue(); - - if (key.startsWith(propPrefix)) - { - key = key.substring(propPrefix.length()); - if (value.startsWith("%") && value.endsWith("%")) - { - value = System.getProperty(value.substring(1, value.length() - 1)); - } - - engine.setProperty(key, value); - } - } - } - - public void executeScriptList() throws Exception - { - if (Config.ALT_DEV_NO_QUESTS) - { - return; - } - - final List files = new ArrayList<>(); - processDirectory(SCRIPT_FOLDER.toFile(), files); - - final Map invokationErrors = _javaExecutionContext.executeScripts(files); - for (Entry entry : invokationErrors.entrySet()) - { - LOGGER.log(Level.WARNING, "ScriptEngine: " + entry.getKey() + " failed execution!", entry.getValue()); - } - } - private void processDirectory(File dir, List files) { for (File file : dir.listFiles()) @@ -230,15 +154,12 @@ public final class ScriptEngineManager implements IXmlReader public void executeScript(Path sourceFile) throws Exception { - Objects.requireNonNull(sourceFile); - if (!sourceFile.isAbsolute()) { sourceFile = SCRIPT_FOLDER.resolve(sourceFile); } sourceFile = sourceFile.toAbsolutePath(); - Objects.requireNonNull(sourceFile, "ScriptFile: " + sourceFile + " does not have an extension to determine the script engine!"); final Entry error = _javaExecutionContext.executeScript(sourceFile); if (error != null) @@ -247,6 +168,23 @@ public final class ScriptEngineManager implements IXmlReader } } + public void executeScriptList() throws Exception + { + if (Config.ALT_DEV_NO_QUESTS) + { + return; + } + + final List files = new ArrayList<>(); + processDirectory(SCRIPT_FOLDER.toFile(), files); + + final Map invokationErrors = _javaExecutionContext.executeScripts(files); + for (Entry entry : invokationErrors.entrySet()) + { + LOGGER.log(Level.WARNING, "ScriptEngine: " + entry.getKey() + " failed execution!", entry.getValue()); + } + } + public Path getCurrentLoadingScript() { return _javaExecutionContext.getCurrentExecutingScript(); @@ -254,11 +192,11 @@ public final class ScriptEngineManager implements IXmlReader public static ScriptEngineManager getInstance() { - return SingletonHolder._instance; + return SingletonHolder.INSTANCE; } private static class SingletonHolder { - protected static final ScriptEngineManager _instance = new ScriptEngineManager(); + protected static final ScriptEngineManager INSTANCE = new ScriptEngineManager(); } } \ No newline at end of file diff --git a/L2J_Mobius_5.5_EtinasFate/java/com/l2jmobius/gameserver/scripting/java/JavaCompilerException.java b/L2J_Mobius_5.5_EtinasFate/java/com/l2jmobius/gameserver/scripting/java/JavaCompilerException.java deleted file mode 100644 index 241ffe91bd..0000000000 --- a/L2J_Mobius_5.5_EtinasFate/java/com/l2jmobius/gameserver/scripting/java/JavaCompilerException.java +++ /dev/null @@ -1,28 +0,0 @@ -/* - * This file is part of the L2J Mobius project. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * 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 - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package com.l2jmobius.gameserver.scripting.java; - -/** - * @author HorridoJoho - */ -public final class JavaCompilerException extends RuntimeException -{ - public JavaCompilerException(String diagnostics) - { - super(diagnostics); - } -} diff --git a/L2J_Mobius_5.5_EtinasFate/java/com/l2jmobius/gameserver/scripting/java/JavaExecutionContext.java b/L2J_Mobius_5.5_EtinasFate/java/com/l2jmobius/gameserver/scripting/java/JavaExecutionContext.java index 70d4da0893..1eddebd693 100644 --- a/L2J_Mobius_5.5_EtinasFate/java/com/l2jmobius/gameserver/scripting/java/JavaExecutionContext.java +++ b/L2J_Mobius_5.5_EtinasFate/java/com/l2jmobius/gameserver/scripting/java/JavaExecutionContext.java @@ -22,9 +22,9 @@ import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.nio.charset.StandardCharsets; import java.nio.file.Path; +import java.util.ArrayList; import java.util.Arrays; -import java.util.LinkedHashMap; -import java.util.LinkedList; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; @@ -34,22 +34,20 @@ import org.openjavac.tools.Diagnostic; import org.openjavac.tools.DiagnosticCollector; import org.openjavac.tools.JavaFileObject; -import com.l2jmobius.gameserver.scripting.AbstractExecutionContext; import com.l2jmobius.gameserver.scripting.annotations.Disabled; /** * @author HorridoJoho */ -public final class JavaExecutionContext extends AbstractExecutionContext +public final class JavaExecutionContext extends JavaScriptingEngine { private static final Logger LOGGER = Logger.getLogger(JavaExecutionContext.class.getName()); - private static final List _options = new LinkedList<>(); + private static final List _options = new ArrayList<>(); + private static Path _currentExecutingScript; JavaExecutionContext(JavaScriptingEngine engine) { - super(engine); - // Set options. addOptionIfNotNull(_options, getProperty("source"), "-source"); addOptionIfNotNull(_options, getProperty("sourcepath"), "-sourcepath"); @@ -76,7 +74,7 @@ public final class JavaExecutionContext extends AbstractExecutionContext executeScripts(Iterable sourcePaths) throws Exception { final DiagnosticCollector fileManagerDiagnostics = new DiagnosticCollector<>(); final DiagnosticCollector compilationDiagnostics = new DiagnosticCollector<>(); - try (ScriptingFileManager fileManager = new ScriptingFileManager(getScriptingEngine().getCompiler().getStandardFileManager(fileManagerDiagnostics, null, StandardCharsets.UTF_8))) + try (ScriptingFileManager fileManager = new ScriptingFileManager(getCompiler().getStandardFileManager(fileManagerDiagnostics, null, StandardCharsets.UTF_8))) { // We really need an iterable of files or strings. - final List sourcePathStrings = new LinkedList<>(); + final List sourcePathStrings = new ArrayList<>(); for (Path sourcePath : sourcePaths) { sourcePathStrings.add(sourcePath.toString()); @@ -150,7 +147,7 @@ public final class JavaExecutionContext extends AbstractExecutionContext executionFailures = new LinkedHashMap<>(); + final Map executionFailures = new HashMap<>(); final Iterable compiledClasses = fileManager.getCompiledClasses(); for (Path sourcePath : sourcePaths) { @@ -199,7 +196,7 @@ public final class JavaExecutionContext extends AbstractExecutionContext executeScript(Path sourcePath) throws Exception { final Map executionFailures = executeScripts(Arrays.asList(sourcePath)); @@ -254,4 +250,9 @@ public final class JavaExecutionContext extends AbstractExecutionContext _properties = new HashMap<>(); + private final static JavaCompiler _compiler = JavacTool.create(); public JavaScriptingEngine() { - super("Java Engine", "10", "java"); - } - - private void determineCompilerOrThrow() - { - if (_compiler == null) + // Load config. + Properties props = new Properties(); + try (FileInputStream fis = new FileInputStream("config/ScriptEngine.ini")) { - _compiler = JavacTool.create(); + props.load(fis); + } + catch (Exception e) + { + LOGGER.warning("Could not load ScriptEngine.ini: " + e.getMessage()); } - if (_compiler == null) + // Set properties. + for (Entry prop : props.entrySet()) { - throw new IllegalStateException("No JavaCompiler service installed!"); - } - } - - private void ensureCompilerOrThrow() - { - if (_compiler == null) - { - synchronized (this) + String key = (String) prop.getKey(); + String value = (String) prop.getValue(); + + if (value.startsWith("%") && value.endsWith("%")) { - if (_compiler == null) - { - determineCompilerOrThrow(); - } + value = System.getProperty(value.substring(1, value.length() - 1)); } + + _properties.put(key, value); } } - JavaCompiler getCompiler() + public JavaExecutionContext createExecutionContext() { - return _compiler; - } - - @Override - public IExecutionContext createExecutionContext() - { - ensureCompilerOrThrow(); return new JavaExecutionContext(this); } - @Override - public String getLanguageName() + public final String getProperty(String key) { - return "Java"; + return _properties.get(key); } - @Override - public String getLanguageVersion() + public JavaCompiler getCompiler() { - ensureCompilerOrThrow(); - return Arrays.deepToString(_compiler.getSourceVersions().toArray(new SourceVersion[0])).replace("RELEASE_", ""); + return _compiler; } } \ No newline at end of file diff --git a/L2J_Mobius_5.5_EtinasFate/java/com/l2jmobius/gameserver/scripting/java/ScriptingOutputFileObject.java b/L2J_Mobius_5.5_EtinasFate/java/com/l2jmobius/gameserver/scripting/java/ScriptingOutputFileObject.java index 439cf5d260..9ab668887e 100644 --- a/L2J_Mobius_5.5_EtinasFate/java/com/l2jmobius/gameserver/scripting/java/ScriptingOutputFileObject.java +++ b/L2J_Mobius_5.5_EtinasFate/java/com/l2jmobius/gameserver/scripting/java/ScriptingOutputFileObject.java @@ -144,5 +144,4 @@ final class ScriptingOutputFileObject implements JavaFileObject { return null; } - } diff --git a/L2J_Mobius_6.0_Fafurion/dist/game/config/ScriptEngine.ini b/L2J_Mobius_6.0_Fafurion/dist/game/config/ScriptEngine.ini index 2d1e972e72..5004dca9fd 100644 --- a/L2J_Mobius_6.0_Fafurion/dist/game/config/ScriptEngine.ini +++ b/L2J_Mobius_6.0_Fafurion/dist/game/config/ScriptEngine.ini @@ -1,23 +1,23 @@ -############################################################################### -# Properties retrived with System.getProperty(String) can be used as values +# --------------------------------------------------------------------------- +# Script Engine Settings +# --------------------------------------------------------------------------- +# Properties retrieved with System.getProperty(String) can be used as values # by enclosing the property name with %. Eg.: %java.class.path% -############################################################################### - # The parent class loader for isolated script class loaders. # When this property is not specified, has an invalid value or is a class name which could not be found, the System classloader is used. # Values: System, ThreadContext or a fully qualified java class name -language.Java.classloader=System -#language.Java.classloader=ThreadContext +classloader=System +#classloader=ThreadContext -# Source compatibility -language.Java.source=1.8 +# Source compatibility. +source=1.8 # The java sourcepath, when you have a different datapack root, you must change this too. -language.Java.sourcepath=data/scripts +sourcepath=data/scripts -# The java classpath -language.Java.cp=%java.class.path% +# The java classpath. +cp=%java.class.path% -# The debug informations to generate for compiled class files -language.Java.g=source,lines,vars \ No newline at end of file +# The debug informations to generate for compiled class files. +g=source,lines,vars \ No newline at end of file diff --git a/L2J_Mobius_6.0_Fafurion/java/com/l2jmobius/gameserver/scripting/AbstractExecutionContext.java b/L2J_Mobius_6.0_Fafurion/java/com/l2jmobius/gameserver/scripting/AbstractExecutionContext.java deleted file mode 100644 index 296b54f06b..0000000000 --- a/L2J_Mobius_6.0_Fafurion/java/com/l2jmobius/gameserver/scripting/AbstractExecutionContext.java +++ /dev/null @@ -1,75 +0,0 @@ -/* - * This file is part of the L2J Mobius project. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * 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 - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package com.l2jmobius.gameserver.scripting; - -import java.nio.file.Path; -import java.util.HashMap; -import java.util.Map; - -/** - * @author HorridoJoho - * @param - */ -public abstract class AbstractExecutionContext implements IExecutionContext -{ - private final T _engine; - private final Map _properties; - private volatile Path _currentExecutingScipt; - - protected AbstractExecutionContext(T engine) - { - if (engine == null) - { - throw new IllegalArgumentException(); - } - _engine = engine; - _properties = new HashMap<>(); - } - - protected final void setCurrentExecutingScript(Path currentExecutingScript) - { - _currentExecutingScipt = currentExecutingScript; - } - - @Override - public final String setProperty(String key, String value) - { - return _properties.put(key, value); - } - - @Override - public final String getProperty(String key) - { - if (!_properties.containsKey(key)) - { - return _engine.getProperty(key); - } - return _properties.get(key); - } - - @Override - public final Path getCurrentExecutingScript() - { - return _currentExecutingScipt; - } - - @Override - public final T getScriptingEngine() - { - return _engine; - } -} diff --git a/L2J_Mobius_6.0_Fafurion/java/com/l2jmobius/gameserver/scripting/AbstractScriptingEngine.java b/L2J_Mobius_6.0_Fafurion/java/com/l2jmobius/gameserver/scripting/AbstractScriptingEngine.java deleted file mode 100644 index 90900438ec..0000000000 --- a/L2J_Mobius_6.0_Fafurion/java/com/l2jmobius/gameserver/scripting/AbstractScriptingEngine.java +++ /dev/null @@ -1,74 +0,0 @@ -/* - * This file is part of the L2J Mobius project. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * 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 - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package com.l2jmobius.gameserver.scripting; - -import java.util.Arrays; -import java.util.HashMap; -import java.util.Map; - -/** - * @author HorridoJoho - */ -public abstract class AbstractScriptingEngine implements IScriptingEngine -{ - private final String _engineName; - private final String _engineVersion; - private final String[] _commonFileExtensions; - private final Map _properties; - - protected AbstractScriptingEngine(String engineName, String engineVersion, String... commonFileExtensions) - { - if ((engineName == null) || engineName.isEmpty() || (engineVersion == null) || engineVersion.isEmpty() || (commonFileExtensions == null) || (commonFileExtensions.length == 0)) - { - throw new IllegalArgumentException(); - } - _engineName = engineName; - _engineVersion = engineVersion; - _commonFileExtensions = commonFileExtensions; - _properties = new HashMap<>(); - } - - @Override - public final String setProperty(String key, String value) - { - return _properties.put(key, value); - } - - @Override - public final String getProperty(String key) - { - return _properties.get(key); - } - - @Override - public final String getEngineName() - { - return _engineName; - } - - @Override - public final String getEngineVersion() - { - return _engineVersion; - } - - @Override - public final String[] getCommonFileExtensions() - { - return Arrays.copyOf(_commonFileExtensions, _commonFileExtensions.length); - } -} diff --git a/L2J_Mobius_6.0_Fafurion/java/com/l2jmobius/gameserver/scripting/IExecutionContext.java b/L2J_Mobius_6.0_Fafurion/java/com/l2jmobius/gameserver/scripting/IExecutionContext.java deleted file mode 100644 index eaf4860b8a..0000000000 --- a/L2J_Mobius_6.0_Fafurion/java/com/l2jmobius/gameserver/scripting/IExecutionContext.java +++ /dev/null @@ -1,71 +0,0 @@ -/* - * This file is part of the L2J Mobius project. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * 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 - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package com.l2jmobius.gameserver.scripting; - -import java.nio.file.Path; -import java.util.Map; -import java.util.Map.Entry; - -/** - * @author HorridoJoho - */ -public interface IExecutionContext -{ - /** - * Properties set here override the settings from the IScriptEngine
- * this class was created from. - * @param key the key - * @param value the value - * @return the previous value, or null when this key was not present before - */ - String setProperty(String key, String value); - - /** - * Executes all script in the iterable. - * @param sourcePaths the scripts to execute - * @return map of failed executions, Path=source file Throwable=thrown exception - * @throws Exception preparation for script execution failed - */ - Map executeScripts(Iterable sourcePaths) throws Exception; - - /** - * Executes a single file. - * @param sourcePath the script to execute - * @return entry of failed execution, Path=source file Throwable=thrown exception - * @throws Exception preparation for script execution failed - */ - Entry executeScript(Path sourcePath) throws Exception; - - /** - * Method to get the specified property value. - * @param key the key - * @return the value, or null if the key is not present - */ - String getProperty(String key); - - /** - * Method to get the current executing script file. - * @return the currently executing script file, null if non - */ - Path getCurrentExecutingScript(); - - /** - * Method to get the script engine this execution context belongs to. - * @return the script engine this execution context belongs to - */ - IScriptingEngine getScriptingEngine(); -} diff --git a/L2J_Mobius_6.0_Fafurion/java/com/l2jmobius/gameserver/scripting/IScriptingEngine.java b/L2J_Mobius_6.0_Fafurion/java/com/l2jmobius/gameserver/scripting/IScriptingEngine.java deleted file mode 100644 index 883172cce1..0000000000 --- a/L2J_Mobius_6.0_Fafurion/java/com/l2jmobius/gameserver/scripting/IScriptingEngine.java +++ /dev/null @@ -1,75 +0,0 @@ -/* - * This file is part of the L2J Mobius project. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * 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 - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package com.l2jmobius.gameserver.scripting; - -/** - * @author HorridoJoho - */ -public interface IScriptingEngine -{ - /** - * Sets script engine properties. The script values will be available
- * to the the insatnces created {@link IExecutionContext} implementation. - * @param key the key - * @param value the value - * @return the previous value, or null when this key was not present before - */ - String setProperty(String key, String value); - - /** - * Creates an execution context. - * @return the created execution context. - */ - IExecutionContext createExecutionContext(); - - /** - * Method to get the specified property value. - * @param key the key - * @return the value,or null if the key is not present - */ - String getProperty(String key); - - /** - * Method to get the engine name. - * @return the engine name - */ - String getEngineName(); - - /** - * Method to get the engine version. - * @return the engine version - */ - String getEngineVersion(); - - /** - * Method to get the scripting language name. - * @return the scripting engine name - */ - String getLanguageName(); - - /** - * Method to get the the language version. - * @return the language version - */ - String getLanguageVersion(); - - /** - * Method to retrive the commonly used file extensions for the language. - * @return the commonly used file extensions for the language - */ - String[] getCommonFileExtensions(); -} diff --git a/L2J_Mobius_6.0_Fafurion/java/com/l2jmobius/gameserver/scripting/ScriptEngineManager.java b/L2J_Mobius_6.0_Fafurion/java/com/l2jmobius/gameserver/scripting/ScriptEngineManager.java index 25a46c93b2..7f8d5fbd5b 100644 --- a/L2J_Mobius_6.0_Fafurion/java/com/l2jmobius/gameserver/scripting/ScriptEngineManager.java +++ b/L2J_Mobius_6.0_Fafurion/java/com/l2jmobius/gameserver/scripting/ScriptEngineManager.java @@ -17,7 +17,6 @@ package com.l2jmobius.gameserver.scripting; import java.io.File; -import java.io.FileInputStream; import java.io.IOException; import java.nio.file.FileVisitResult; import java.nio.file.Files; @@ -31,9 +30,6 @@ import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Map.Entry; -import java.util.Objects; -import java.util.Properties; -import java.util.ServiceLoader; import java.util.logging.Level; import java.util.logging.Logger; @@ -41,15 +37,16 @@ import org.w3c.dom.Document; import com.l2jmobius.Config; import com.l2jmobius.commons.util.IXmlReader; +import com.l2jmobius.gameserver.scripting.java.JavaExecutionContext; import com.l2jmobius.gameserver.scripting.java.JavaScriptingEngine; /** - * Caches script engines and provides functionality for executing and managing scripts. - * @author KenM, HorridoJoho + * @author Mobius */ public final class ScriptEngineManager implements IXmlReader { private static final Logger LOGGER = Logger.getLogger(ScriptEngineManager.class.getName()); + public static final Path SCRIPT_FOLDER = Config.SCRIPT_ROOT.toPath(); public static final Path MASTER_HANDLER_FILE = Paths.get(SCRIPT_FOLDER.toString(), "handlers", "MasterHandler.java"); public static final Path EFFECT_MASTER_HANDLER_FILE = Paths.get(SCRIPT_FOLDER.toString(), "handlers", "EffectMasterHandler.java"); @@ -57,19 +54,11 @@ public final class ScriptEngineManager implements IXmlReader public static final Path CONDITION_HANDLER_FILE = Paths.get(SCRIPT_FOLDER.toString(), "handlers", "ConditionMasterHandler.java"); public static final Path ONE_DAY_REWARD_MASTER_HANDLER = Paths.get(SCRIPT_FOLDER.toString(), "handlers", "DailyMissionMasterHandler.java"); - private IExecutionContext _javaExecutionContext = null; - static final List _exclusions = new ArrayList<>(); + private static final JavaExecutionContext _javaExecutionContext = new JavaScriptingEngine().createExecutionContext(); + protected static final List _exclusions = new ArrayList<>(); protected ScriptEngineManager() { - final Properties props = loadProperties(); - - // Default java engine implementation - registerEngine(new JavaScriptingEngine(), props); - - // Load external script engines - ServiceLoader.load(IScriptingEngine.class).forEach(engine -> registerEngine(engine, props)); - // Load Scripts.xml load(); } @@ -144,71 +133,6 @@ public final class ScriptEngineManager implements IXmlReader } } - private Properties loadProperties() - { - Properties props = null; - try (FileInputStream fis = new FileInputStream("config/ScriptEngine.ini")) - { - props = new Properties(); - props.load(fis); - } - catch (Exception e) - { - props = null; - LOGGER.warning("Couldn't load ScriptEngine.ini: " + e.getMessage()); - } - return props; - } - - private void registerEngine(IScriptingEngine engine, Properties props) - { - maybeSetProperties("language." + engine.getLanguageName() + ".", props, engine); - _javaExecutionContext = engine.createExecutionContext(); - LOGGER.info("ScriptEngine: " + engine.getEngineName() + " " + engine.getEngineVersion() + " (" + engine.getLanguageName() + " " + engine.getLanguageVersion() + ")"); - } - - private void maybeSetProperties(String propPrefix, Properties props, IScriptingEngine engine) - { - if (props == null) - { - return; - } - - for (Entry prop : props.entrySet()) - { - String key = (String) prop.getKey(); - String value = (String) prop.getValue(); - - if (key.startsWith(propPrefix)) - { - key = key.substring(propPrefix.length()); - if (value.startsWith("%") && value.endsWith("%")) - { - value = System.getProperty(value.substring(1, value.length() - 1)); - } - - engine.setProperty(key, value); - } - } - } - - public void executeScriptList() throws Exception - { - if (Config.ALT_DEV_NO_QUESTS) - { - return; - } - - final List files = new ArrayList<>(); - processDirectory(SCRIPT_FOLDER.toFile(), files); - - final Map invokationErrors = _javaExecutionContext.executeScripts(files); - for (Entry entry : invokationErrors.entrySet()) - { - LOGGER.log(Level.WARNING, "ScriptEngine: " + entry.getKey() + " failed execution!", entry.getValue()); - } - } - private void processDirectory(File dir, List files) { for (File file : dir.listFiles()) @@ -230,15 +154,12 @@ public final class ScriptEngineManager implements IXmlReader public void executeScript(Path sourceFile) throws Exception { - Objects.requireNonNull(sourceFile); - if (!sourceFile.isAbsolute()) { sourceFile = SCRIPT_FOLDER.resolve(sourceFile); } sourceFile = sourceFile.toAbsolutePath(); - Objects.requireNonNull(sourceFile, "ScriptFile: " + sourceFile + " does not have an extension to determine the script engine!"); final Entry error = _javaExecutionContext.executeScript(sourceFile); if (error != null) @@ -247,6 +168,23 @@ public final class ScriptEngineManager implements IXmlReader } } + public void executeScriptList() throws Exception + { + if (Config.ALT_DEV_NO_QUESTS) + { + return; + } + + final List files = new ArrayList<>(); + processDirectory(SCRIPT_FOLDER.toFile(), files); + + final Map invokationErrors = _javaExecutionContext.executeScripts(files); + for (Entry entry : invokationErrors.entrySet()) + { + LOGGER.log(Level.WARNING, "ScriptEngine: " + entry.getKey() + " failed execution!", entry.getValue()); + } + } + public Path getCurrentLoadingScript() { return _javaExecutionContext.getCurrentExecutingScript(); @@ -254,11 +192,11 @@ public final class ScriptEngineManager implements IXmlReader public static ScriptEngineManager getInstance() { - return SingletonHolder._instance; + return SingletonHolder.INSTANCE; } private static class SingletonHolder { - protected static final ScriptEngineManager _instance = new ScriptEngineManager(); + protected static final ScriptEngineManager INSTANCE = new ScriptEngineManager(); } } \ No newline at end of file diff --git a/L2J_Mobius_6.0_Fafurion/java/com/l2jmobius/gameserver/scripting/java/JavaCompilerException.java b/L2J_Mobius_6.0_Fafurion/java/com/l2jmobius/gameserver/scripting/java/JavaCompilerException.java deleted file mode 100644 index 241ffe91bd..0000000000 --- a/L2J_Mobius_6.0_Fafurion/java/com/l2jmobius/gameserver/scripting/java/JavaCompilerException.java +++ /dev/null @@ -1,28 +0,0 @@ -/* - * This file is part of the L2J Mobius project. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * 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 - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package com.l2jmobius.gameserver.scripting.java; - -/** - * @author HorridoJoho - */ -public final class JavaCompilerException extends RuntimeException -{ - public JavaCompilerException(String diagnostics) - { - super(diagnostics); - } -} diff --git a/L2J_Mobius_6.0_Fafurion/java/com/l2jmobius/gameserver/scripting/java/JavaExecutionContext.java b/L2J_Mobius_6.0_Fafurion/java/com/l2jmobius/gameserver/scripting/java/JavaExecutionContext.java index 70d4da0893..1eddebd693 100644 --- a/L2J_Mobius_6.0_Fafurion/java/com/l2jmobius/gameserver/scripting/java/JavaExecutionContext.java +++ b/L2J_Mobius_6.0_Fafurion/java/com/l2jmobius/gameserver/scripting/java/JavaExecutionContext.java @@ -22,9 +22,9 @@ import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.nio.charset.StandardCharsets; import java.nio.file.Path; +import java.util.ArrayList; import java.util.Arrays; -import java.util.LinkedHashMap; -import java.util.LinkedList; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; @@ -34,22 +34,20 @@ import org.openjavac.tools.Diagnostic; import org.openjavac.tools.DiagnosticCollector; import org.openjavac.tools.JavaFileObject; -import com.l2jmobius.gameserver.scripting.AbstractExecutionContext; import com.l2jmobius.gameserver.scripting.annotations.Disabled; /** * @author HorridoJoho */ -public final class JavaExecutionContext extends AbstractExecutionContext +public final class JavaExecutionContext extends JavaScriptingEngine { private static final Logger LOGGER = Logger.getLogger(JavaExecutionContext.class.getName()); - private static final List _options = new LinkedList<>(); + private static final List _options = new ArrayList<>(); + private static Path _currentExecutingScript; JavaExecutionContext(JavaScriptingEngine engine) { - super(engine); - // Set options. addOptionIfNotNull(_options, getProperty("source"), "-source"); addOptionIfNotNull(_options, getProperty("sourcepath"), "-sourcepath"); @@ -76,7 +74,7 @@ public final class JavaExecutionContext extends AbstractExecutionContext executeScripts(Iterable sourcePaths) throws Exception { final DiagnosticCollector fileManagerDiagnostics = new DiagnosticCollector<>(); final DiagnosticCollector compilationDiagnostics = new DiagnosticCollector<>(); - try (ScriptingFileManager fileManager = new ScriptingFileManager(getScriptingEngine().getCompiler().getStandardFileManager(fileManagerDiagnostics, null, StandardCharsets.UTF_8))) + try (ScriptingFileManager fileManager = new ScriptingFileManager(getCompiler().getStandardFileManager(fileManagerDiagnostics, null, StandardCharsets.UTF_8))) { // We really need an iterable of files or strings. - final List sourcePathStrings = new LinkedList<>(); + final List sourcePathStrings = new ArrayList<>(); for (Path sourcePath : sourcePaths) { sourcePathStrings.add(sourcePath.toString()); @@ -150,7 +147,7 @@ public final class JavaExecutionContext extends AbstractExecutionContext executionFailures = new LinkedHashMap<>(); + final Map executionFailures = new HashMap<>(); final Iterable compiledClasses = fileManager.getCompiledClasses(); for (Path sourcePath : sourcePaths) { @@ -199,7 +196,7 @@ public final class JavaExecutionContext extends AbstractExecutionContext executeScript(Path sourcePath) throws Exception { final Map executionFailures = executeScripts(Arrays.asList(sourcePath)); @@ -254,4 +250,9 @@ public final class JavaExecutionContext extends AbstractExecutionContext _properties = new HashMap<>(); + private final static JavaCompiler _compiler = JavacTool.create(); public JavaScriptingEngine() { - super("Java Engine", "10", "java"); - } - - private void determineCompilerOrThrow() - { - if (_compiler == null) + // Load config. + Properties props = new Properties(); + try (FileInputStream fis = new FileInputStream("config/ScriptEngine.ini")) { - _compiler = JavacTool.create(); + props.load(fis); + } + catch (Exception e) + { + LOGGER.warning("Could not load ScriptEngine.ini: " + e.getMessage()); } - if (_compiler == null) + // Set properties. + for (Entry prop : props.entrySet()) { - throw new IllegalStateException("No JavaCompiler service installed!"); - } - } - - private void ensureCompilerOrThrow() - { - if (_compiler == null) - { - synchronized (this) + String key = (String) prop.getKey(); + String value = (String) prop.getValue(); + + if (value.startsWith("%") && value.endsWith("%")) { - if (_compiler == null) - { - determineCompilerOrThrow(); - } + value = System.getProperty(value.substring(1, value.length() - 1)); } + + _properties.put(key, value); } } - JavaCompiler getCompiler() + public JavaExecutionContext createExecutionContext() { - return _compiler; - } - - @Override - public IExecutionContext createExecutionContext() - { - ensureCompilerOrThrow(); return new JavaExecutionContext(this); } - @Override - public String getLanguageName() + public final String getProperty(String key) { - return "Java"; + return _properties.get(key); } - @Override - public String getLanguageVersion() + public JavaCompiler getCompiler() { - ensureCompilerOrThrow(); - return Arrays.deepToString(_compiler.getSourceVersions().toArray(new SourceVersion[0])).replace("RELEASE_", ""); + return _compiler; } } \ No newline at end of file diff --git a/L2J_Mobius_6.0_Fafurion/java/com/l2jmobius/gameserver/scripting/java/ScriptingOutputFileObject.java b/L2J_Mobius_6.0_Fafurion/java/com/l2jmobius/gameserver/scripting/java/ScriptingOutputFileObject.java index 439cf5d260..9ab668887e 100644 --- a/L2J_Mobius_6.0_Fafurion/java/com/l2jmobius/gameserver/scripting/java/ScriptingOutputFileObject.java +++ b/L2J_Mobius_6.0_Fafurion/java/com/l2jmobius/gameserver/scripting/java/ScriptingOutputFileObject.java @@ -144,5 +144,4 @@ final class ScriptingOutputFileObject implements JavaFileObject { return null; } - } diff --git a/L2J_Mobius_CT_2.6_HighFive/dist/game/config/ScriptEngine.ini b/L2J_Mobius_CT_2.6_HighFive/dist/game/config/ScriptEngine.ini index 2d1e972e72..5004dca9fd 100644 --- a/L2J_Mobius_CT_2.6_HighFive/dist/game/config/ScriptEngine.ini +++ b/L2J_Mobius_CT_2.6_HighFive/dist/game/config/ScriptEngine.ini @@ -1,23 +1,23 @@ -############################################################################### -# Properties retrived with System.getProperty(String) can be used as values +# --------------------------------------------------------------------------- +# Script Engine Settings +# --------------------------------------------------------------------------- +# Properties retrieved with System.getProperty(String) can be used as values # by enclosing the property name with %. Eg.: %java.class.path% -############################################################################### - # The parent class loader for isolated script class loaders. # When this property is not specified, has an invalid value or is a class name which could not be found, the System classloader is used. # Values: System, ThreadContext or a fully qualified java class name -language.Java.classloader=System -#language.Java.classloader=ThreadContext +classloader=System +#classloader=ThreadContext -# Source compatibility -language.Java.source=1.8 +# Source compatibility. +source=1.8 # The java sourcepath, when you have a different datapack root, you must change this too. -language.Java.sourcepath=data/scripts +sourcepath=data/scripts -# The java classpath -language.Java.cp=%java.class.path% +# The java classpath. +cp=%java.class.path% -# The debug informations to generate for compiled class files -language.Java.g=source,lines,vars \ No newline at end of file +# The debug informations to generate for compiled class files. +g=source,lines,vars \ No newline at end of file diff --git a/L2J_Mobius_CT_2.6_HighFive/java/com/l2jmobius/gameserver/scripting/AbstractExecutionContext.java b/L2J_Mobius_CT_2.6_HighFive/java/com/l2jmobius/gameserver/scripting/AbstractExecutionContext.java deleted file mode 100644 index 296b54f06b..0000000000 --- a/L2J_Mobius_CT_2.6_HighFive/java/com/l2jmobius/gameserver/scripting/AbstractExecutionContext.java +++ /dev/null @@ -1,75 +0,0 @@ -/* - * This file is part of the L2J Mobius project. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * 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 - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package com.l2jmobius.gameserver.scripting; - -import java.nio.file.Path; -import java.util.HashMap; -import java.util.Map; - -/** - * @author HorridoJoho - * @param - */ -public abstract class AbstractExecutionContext implements IExecutionContext -{ - private final T _engine; - private final Map _properties; - private volatile Path _currentExecutingScipt; - - protected AbstractExecutionContext(T engine) - { - if (engine == null) - { - throw new IllegalArgumentException(); - } - _engine = engine; - _properties = new HashMap<>(); - } - - protected final void setCurrentExecutingScript(Path currentExecutingScript) - { - _currentExecutingScipt = currentExecutingScript; - } - - @Override - public final String setProperty(String key, String value) - { - return _properties.put(key, value); - } - - @Override - public final String getProperty(String key) - { - if (!_properties.containsKey(key)) - { - return _engine.getProperty(key); - } - return _properties.get(key); - } - - @Override - public final Path getCurrentExecutingScript() - { - return _currentExecutingScipt; - } - - @Override - public final T getScriptingEngine() - { - return _engine; - } -} diff --git a/L2J_Mobius_CT_2.6_HighFive/java/com/l2jmobius/gameserver/scripting/AbstractScriptingEngine.java b/L2J_Mobius_CT_2.6_HighFive/java/com/l2jmobius/gameserver/scripting/AbstractScriptingEngine.java deleted file mode 100644 index 90900438ec..0000000000 --- a/L2J_Mobius_CT_2.6_HighFive/java/com/l2jmobius/gameserver/scripting/AbstractScriptingEngine.java +++ /dev/null @@ -1,74 +0,0 @@ -/* - * This file is part of the L2J Mobius project. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * 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 - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package com.l2jmobius.gameserver.scripting; - -import java.util.Arrays; -import java.util.HashMap; -import java.util.Map; - -/** - * @author HorridoJoho - */ -public abstract class AbstractScriptingEngine implements IScriptingEngine -{ - private final String _engineName; - private final String _engineVersion; - private final String[] _commonFileExtensions; - private final Map _properties; - - protected AbstractScriptingEngine(String engineName, String engineVersion, String... commonFileExtensions) - { - if ((engineName == null) || engineName.isEmpty() || (engineVersion == null) || engineVersion.isEmpty() || (commonFileExtensions == null) || (commonFileExtensions.length == 0)) - { - throw new IllegalArgumentException(); - } - _engineName = engineName; - _engineVersion = engineVersion; - _commonFileExtensions = commonFileExtensions; - _properties = new HashMap<>(); - } - - @Override - public final String setProperty(String key, String value) - { - return _properties.put(key, value); - } - - @Override - public final String getProperty(String key) - { - return _properties.get(key); - } - - @Override - public final String getEngineName() - { - return _engineName; - } - - @Override - public final String getEngineVersion() - { - return _engineVersion; - } - - @Override - public final String[] getCommonFileExtensions() - { - return Arrays.copyOf(_commonFileExtensions, _commonFileExtensions.length); - } -} diff --git a/L2J_Mobius_CT_2.6_HighFive/java/com/l2jmobius/gameserver/scripting/IExecutionContext.java b/L2J_Mobius_CT_2.6_HighFive/java/com/l2jmobius/gameserver/scripting/IExecutionContext.java deleted file mode 100644 index eaf4860b8a..0000000000 --- a/L2J_Mobius_CT_2.6_HighFive/java/com/l2jmobius/gameserver/scripting/IExecutionContext.java +++ /dev/null @@ -1,71 +0,0 @@ -/* - * This file is part of the L2J Mobius project. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * 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 - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package com.l2jmobius.gameserver.scripting; - -import java.nio.file.Path; -import java.util.Map; -import java.util.Map.Entry; - -/** - * @author HorridoJoho - */ -public interface IExecutionContext -{ - /** - * Properties set here override the settings from the IScriptEngine
- * this class was created from. - * @param key the key - * @param value the value - * @return the previous value, or null when this key was not present before - */ - String setProperty(String key, String value); - - /** - * Executes all script in the iterable. - * @param sourcePaths the scripts to execute - * @return map of failed executions, Path=source file Throwable=thrown exception - * @throws Exception preparation for script execution failed - */ - Map executeScripts(Iterable sourcePaths) throws Exception; - - /** - * Executes a single file. - * @param sourcePath the script to execute - * @return entry of failed execution, Path=source file Throwable=thrown exception - * @throws Exception preparation for script execution failed - */ - Entry executeScript(Path sourcePath) throws Exception; - - /** - * Method to get the specified property value. - * @param key the key - * @return the value, or null if the key is not present - */ - String getProperty(String key); - - /** - * Method to get the current executing script file. - * @return the currently executing script file, null if non - */ - Path getCurrentExecutingScript(); - - /** - * Method to get the script engine this execution context belongs to. - * @return the script engine this execution context belongs to - */ - IScriptingEngine getScriptingEngine(); -} diff --git a/L2J_Mobius_CT_2.6_HighFive/java/com/l2jmobius/gameserver/scripting/IScriptingEngine.java b/L2J_Mobius_CT_2.6_HighFive/java/com/l2jmobius/gameserver/scripting/IScriptingEngine.java deleted file mode 100644 index 883172cce1..0000000000 --- a/L2J_Mobius_CT_2.6_HighFive/java/com/l2jmobius/gameserver/scripting/IScriptingEngine.java +++ /dev/null @@ -1,75 +0,0 @@ -/* - * This file is part of the L2J Mobius project. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * 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 - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package com.l2jmobius.gameserver.scripting; - -/** - * @author HorridoJoho - */ -public interface IScriptingEngine -{ - /** - * Sets script engine properties. The script values will be available
- * to the the insatnces created {@link IExecutionContext} implementation. - * @param key the key - * @param value the value - * @return the previous value, or null when this key was not present before - */ - String setProperty(String key, String value); - - /** - * Creates an execution context. - * @return the created execution context. - */ - IExecutionContext createExecutionContext(); - - /** - * Method to get the specified property value. - * @param key the key - * @return the value,or null if the key is not present - */ - String getProperty(String key); - - /** - * Method to get the engine name. - * @return the engine name - */ - String getEngineName(); - - /** - * Method to get the engine version. - * @return the engine version - */ - String getEngineVersion(); - - /** - * Method to get the scripting language name. - * @return the scripting engine name - */ - String getLanguageName(); - - /** - * Method to get the the language version. - * @return the language version - */ - String getLanguageVersion(); - - /** - * Method to retrive the commonly used file extensions for the language. - * @return the commonly used file extensions for the language - */ - String[] getCommonFileExtensions(); -} diff --git a/L2J_Mobius_CT_2.6_HighFive/java/com/l2jmobius/gameserver/scripting/ScriptEngineManager.java b/L2J_Mobius_CT_2.6_HighFive/java/com/l2jmobius/gameserver/scripting/ScriptEngineManager.java index ad71eabbd4..1df95e1992 100644 --- a/L2J_Mobius_CT_2.6_HighFive/java/com/l2jmobius/gameserver/scripting/ScriptEngineManager.java +++ b/L2J_Mobius_CT_2.6_HighFive/java/com/l2jmobius/gameserver/scripting/ScriptEngineManager.java @@ -17,7 +17,6 @@ package com.l2jmobius.gameserver.scripting; import java.io.File; -import java.io.FileInputStream; import java.io.IOException; import java.nio.file.FileVisitResult; import java.nio.file.Files; @@ -31,9 +30,6 @@ import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Map.Entry; -import java.util.Objects; -import java.util.Properties; -import java.util.ServiceLoader; import java.util.logging.Level; import java.util.logging.Logger; @@ -41,32 +37,25 @@ import org.w3c.dom.Document; import com.l2jmobius.Config; import com.l2jmobius.commons.util.IXmlReader; +import com.l2jmobius.gameserver.scripting.java.JavaExecutionContext; import com.l2jmobius.gameserver.scripting.java.JavaScriptingEngine; /** - * Caches script engines and provides functionality for executing and managing scripts. - * @author KenM, HorridoJoho + * @author Mobius */ public final class ScriptEngineManager implements IXmlReader { private static final Logger LOGGER = Logger.getLogger(ScriptEngineManager.class.getName()); + public static final Path SCRIPT_FOLDER = Config.SCRIPT_ROOT.toPath(); public static final Path MASTER_HANDLER_FILE = Paths.get(SCRIPT_FOLDER.toString(), "handlers", "MasterHandler.java"); public static final Path EFFECT_MASTER_HANDLER_FILE = Paths.get(SCRIPT_FOLDER.toString(), "handlers", "EffectMasterHandler.java"); - private IExecutionContext _javaExecutionContext = null; - static final List _exclusions = new ArrayList<>(); + private static final JavaExecutionContext _javaExecutionContext = new JavaScriptingEngine().createExecutionContext(); + protected static final List _exclusions = new ArrayList<>(); protected ScriptEngineManager() { - final Properties props = loadProperties(); - - // Default java engine implementation - registerEngine(new JavaScriptingEngine(), props); - - // Load external script engines - ServiceLoader.load(IScriptingEngine.class).forEach(engine -> registerEngine(engine, props)); - // Load Scripts.xml load(); } @@ -141,71 +130,6 @@ public final class ScriptEngineManager implements IXmlReader } } - private Properties loadProperties() - { - Properties props = null; - try (FileInputStream fis = new FileInputStream("config/ScriptEngine.ini")) - { - props = new Properties(); - props.load(fis); - } - catch (Exception e) - { - props = null; - LOGGER.warning("Couldn't load ScriptEngine.ini: " + e.getMessage()); - } - return props; - } - - private void registerEngine(IScriptingEngine engine, Properties props) - { - maybeSetProperties("language." + engine.getLanguageName() + ".", props, engine); - _javaExecutionContext = engine.createExecutionContext(); - LOGGER.info("ScriptEngine: " + engine.getEngineName() + " " + engine.getEngineVersion() + " (" + engine.getLanguageName() + " " + engine.getLanguageVersion() + ")"); - } - - private void maybeSetProperties(String propPrefix, Properties props, IScriptingEngine engine) - { - if (props == null) - { - return; - } - - for (Entry prop : props.entrySet()) - { - String key = (String) prop.getKey(); - String value = (String) prop.getValue(); - - if (key.startsWith(propPrefix)) - { - key = key.substring(propPrefix.length()); - if (value.startsWith("%") && value.endsWith("%")) - { - value = System.getProperty(value.substring(1, value.length() - 1)); - } - - engine.setProperty(key, value); - } - } - } - - public void executeScriptList() throws Exception - { - if (Config.ALT_DEV_NO_QUESTS) - { - return; - } - - final List files = new ArrayList<>(); - processDirectory(SCRIPT_FOLDER.toFile(), files); - - final Map invokationErrors = _javaExecutionContext.executeScripts(files); - for (Entry entry : invokationErrors.entrySet()) - { - LOGGER.log(Level.WARNING, "ScriptEngine: " + entry.getKey() + " failed execution!", entry.getValue()); - } - } - private void processDirectory(File dir, List files) { for (File file : dir.listFiles()) @@ -227,15 +151,12 @@ public final class ScriptEngineManager implements IXmlReader public void executeScript(Path sourceFile) throws Exception { - Objects.requireNonNull(sourceFile); - if (!sourceFile.isAbsolute()) { sourceFile = SCRIPT_FOLDER.resolve(sourceFile); } sourceFile = sourceFile.toAbsolutePath(); - Objects.requireNonNull(sourceFile, "ScriptFile: " + sourceFile + " does not have an extension to determine the script engine!"); final Entry error = _javaExecutionContext.executeScript(sourceFile); if (error != null) @@ -244,6 +165,23 @@ public final class ScriptEngineManager implements IXmlReader } } + public void executeScriptList() throws Exception + { + if (Config.ALT_DEV_NO_QUESTS) + { + return; + } + + final List files = new ArrayList<>(); + processDirectory(SCRIPT_FOLDER.toFile(), files); + + final Map invokationErrors = _javaExecutionContext.executeScripts(files); + for (Entry entry : invokationErrors.entrySet()) + { + LOGGER.log(Level.WARNING, "ScriptEngine: " + entry.getKey() + " failed execution!", entry.getValue()); + } + } + public Path getCurrentLoadingScript() { return _javaExecutionContext.getCurrentExecutingScript(); @@ -251,11 +189,11 @@ public final class ScriptEngineManager implements IXmlReader public static ScriptEngineManager getInstance() { - return SingletonHolder._instance; + return SingletonHolder.INSTANCE; } private static class SingletonHolder { - protected static final ScriptEngineManager _instance = new ScriptEngineManager(); + protected static final ScriptEngineManager INSTANCE = new ScriptEngineManager(); } } \ No newline at end of file diff --git a/L2J_Mobius_CT_2.6_HighFive/java/com/l2jmobius/gameserver/scripting/java/JavaCompilerException.java b/L2J_Mobius_CT_2.6_HighFive/java/com/l2jmobius/gameserver/scripting/java/JavaCompilerException.java deleted file mode 100644 index 241ffe91bd..0000000000 --- a/L2J_Mobius_CT_2.6_HighFive/java/com/l2jmobius/gameserver/scripting/java/JavaCompilerException.java +++ /dev/null @@ -1,28 +0,0 @@ -/* - * This file is part of the L2J Mobius project. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * 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 - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package com.l2jmobius.gameserver.scripting.java; - -/** - * @author HorridoJoho - */ -public final class JavaCompilerException extends RuntimeException -{ - public JavaCompilerException(String diagnostics) - { - super(diagnostics); - } -} diff --git a/L2J_Mobius_CT_2.6_HighFive/java/com/l2jmobius/gameserver/scripting/java/JavaExecutionContext.java b/L2J_Mobius_CT_2.6_HighFive/java/com/l2jmobius/gameserver/scripting/java/JavaExecutionContext.java index 70d4da0893..1eddebd693 100644 --- a/L2J_Mobius_CT_2.6_HighFive/java/com/l2jmobius/gameserver/scripting/java/JavaExecutionContext.java +++ b/L2J_Mobius_CT_2.6_HighFive/java/com/l2jmobius/gameserver/scripting/java/JavaExecutionContext.java @@ -22,9 +22,9 @@ import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.nio.charset.StandardCharsets; import java.nio.file.Path; +import java.util.ArrayList; import java.util.Arrays; -import java.util.LinkedHashMap; -import java.util.LinkedList; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; @@ -34,22 +34,20 @@ import org.openjavac.tools.Diagnostic; import org.openjavac.tools.DiagnosticCollector; import org.openjavac.tools.JavaFileObject; -import com.l2jmobius.gameserver.scripting.AbstractExecutionContext; import com.l2jmobius.gameserver.scripting.annotations.Disabled; /** * @author HorridoJoho */ -public final class JavaExecutionContext extends AbstractExecutionContext +public final class JavaExecutionContext extends JavaScriptingEngine { private static final Logger LOGGER = Logger.getLogger(JavaExecutionContext.class.getName()); - private static final List _options = new LinkedList<>(); + private static final List _options = new ArrayList<>(); + private static Path _currentExecutingScript; JavaExecutionContext(JavaScriptingEngine engine) { - super(engine); - // Set options. addOptionIfNotNull(_options, getProperty("source"), "-source"); addOptionIfNotNull(_options, getProperty("sourcepath"), "-sourcepath"); @@ -76,7 +74,7 @@ public final class JavaExecutionContext extends AbstractExecutionContext executeScripts(Iterable sourcePaths) throws Exception { final DiagnosticCollector fileManagerDiagnostics = new DiagnosticCollector<>(); final DiagnosticCollector compilationDiagnostics = new DiagnosticCollector<>(); - try (ScriptingFileManager fileManager = new ScriptingFileManager(getScriptingEngine().getCompiler().getStandardFileManager(fileManagerDiagnostics, null, StandardCharsets.UTF_8))) + try (ScriptingFileManager fileManager = new ScriptingFileManager(getCompiler().getStandardFileManager(fileManagerDiagnostics, null, StandardCharsets.UTF_8))) { // We really need an iterable of files or strings. - final List sourcePathStrings = new LinkedList<>(); + final List sourcePathStrings = new ArrayList<>(); for (Path sourcePath : sourcePaths) { sourcePathStrings.add(sourcePath.toString()); @@ -150,7 +147,7 @@ public final class JavaExecutionContext extends AbstractExecutionContext executionFailures = new LinkedHashMap<>(); + final Map executionFailures = new HashMap<>(); final Iterable compiledClasses = fileManager.getCompiledClasses(); for (Path sourcePath : sourcePaths) { @@ -199,7 +196,7 @@ public final class JavaExecutionContext extends AbstractExecutionContext executeScript(Path sourcePath) throws Exception { final Map executionFailures = executeScripts(Arrays.asList(sourcePath)); @@ -254,4 +250,9 @@ public final class JavaExecutionContext extends AbstractExecutionContext _properties = new HashMap<>(); + private final static JavaCompiler _compiler = JavacTool.create(); public JavaScriptingEngine() { - super("Java Engine", "10", "java"); - } - - private void determineCompilerOrThrow() - { - if (_compiler == null) + // Load config. + Properties props = new Properties(); + try (FileInputStream fis = new FileInputStream("config/ScriptEngine.ini")) { - _compiler = JavacTool.create(); + props.load(fis); + } + catch (Exception e) + { + LOGGER.warning("Could not load ScriptEngine.ini: " + e.getMessage()); } - if (_compiler == null) + // Set properties. + for (Entry prop : props.entrySet()) { - throw new IllegalStateException("No JavaCompiler service installed!"); - } - } - - private void ensureCompilerOrThrow() - { - if (_compiler == null) - { - synchronized (this) + String key = (String) prop.getKey(); + String value = (String) prop.getValue(); + + if (value.startsWith("%") && value.endsWith("%")) { - if (_compiler == null) - { - determineCompilerOrThrow(); - } + value = System.getProperty(value.substring(1, value.length() - 1)); } + + _properties.put(key, value); } } - JavaCompiler getCompiler() + public JavaExecutionContext createExecutionContext() { - return _compiler; - } - - @Override - public IExecutionContext createExecutionContext() - { - ensureCompilerOrThrow(); return new JavaExecutionContext(this); } - @Override - public String getLanguageName() + public final String getProperty(String key) { - return "Java"; + return _properties.get(key); } - @Override - public String getLanguageVersion() + public JavaCompiler getCompiler() { - ensureCompilerOrThrow(); - return Arrays.deepToString(_compiler.getSourceVersions().toArray(new SourceVersion[0])).replace("RELEASE_", ""); + return _compiler; } } \ No newline at end of file diff --git a/L2J_Mobius_CT_2.6_HighFive/java/com/l2jmobius/gameserver/scripting/java/ScriptingOutputFileObject.java b/L2J_Mobius_CT_2.6_HighFive/java/com/l2jmobius/gameserver/scripting/java/ScriptingOutputFileObject.java index 439cf5d260..9ab668887e 100644 --- a/L2J_Mobius_CT_2.6_HighFive/java/com/l2jmobius/gameserver/scripting/java/ScriptingOutputFileObject.java +++ b/L2J_Mobius_CT_2.6_HighFive/java/com/l2jmobius/gameserver/scripting/java/ScriptingOutputFileObject.java @@ -144,5 +144,4 @@ final class ScriptingOutputFileObject implements JavaFileObject { return null; } - } diff --git a/L2J_Mobius_Classic_2.0_Saviors/dist/game/config/ScriptEngine.ini b/L2J_Mobius_Classic_2.0_Saviors/dist/game/config/ScriptEngine.ini index 2d1e972e72..5004dca9fd 100644 --- a/L2J_Mobius_Classic_2.0_Saviors/dist/game/config/ScriptEngine.ini +++ b/L2J_Mobius_Classic_2.0_Saviors/dist/game/config/ScriptEngine.ini @@ -1,23 +1,23 @@ -############################################################################### -# Properties retrived with System.getProperty(String) can be used as values +# --------------------------------------------------------------------------- +# Script Engine Settings +# --------------------------------------------------------------------------- +# Properties retrieved with System.getProperty(String) can be used as values # by enclosing the property name with %. Eg.: %java.class.path% -############################################################################### - # The parent class loader for isolated script class loaders. # When this property is not specified, has an invalid value or is a class name which could not be found, the System classloader is used. # Values: System, ThreadContext or a fully qualified java class name -language.Java.classloader=System -#language.Java.classloader=ThreadContext +classloader=System +#classloader=ThreadContext -# Source compatibility -language.Java.source=1.8 +# Source compatibility. +source=1.8 # The java sourcepath, when you have a different datapack root, you must change this too. -language.Java.sourcepath=data/scripts +sourcepath=data/scripts -# The java classpath -language.Java.cp=%java.class.path% +# The java classpath. +cp=%java.class.path% -# The debug informations to generate for compiled class files -language.Java.g=source,lines,vars \ No newline at end of file +# The debug informations to generate for compiled class files. +g=source,lines,vars \ No newline at end of file diff --git a/L2J_Mobius_Classic_2.0_Saviors/java/com/l2jmobius/gameserver/scripting/AbstractExecutionContext.java b/L2J_Mobius_Classic_2.0_Saviors/java/com/l2jmobius/gameserver/scripting/AbstractExecutionContext.java deleted file mode 100644 index 296b54f06b..0000000000 --- a/L2J_Mobius_Classic_2.0_Saviors/java/com/l2jmobius/gameserver/scripting/AbstractExecutionContext.java +++ /dev/null @@ -1,75 +0,0 @@ -/* - * This file is part of the L2J Mobius project. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * 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 - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package com.l2jmobius.gameserver.scripting; - -import java.nio.file.Path; -import java.util.HashMap; -import java.util.Map; - -/** - * @author HorridoJoho - * @param - */ -public abstract class AbstractExecutionContext implements IExecutionContext -{ - private final T _engine; - private final Map _properties; - private volatile Path _currentExecutingScipt; - - protected AbstractExecutionContext(T engine) - { - if (engine == null) - { - throw new IllegalArgumentException(); - } - _engine = engine; - _properties = new HashMap<>(); - } - - protected final void setCurrentExecutingScript(Path currentExecutingScript) - { - _currentExecutingScipt = currentExecutingScript; - } - - @Override - public final String setProperty(String key, String value) - { - return _properties.put(key, value); - } - - @Override - public final String getProperty(String key) - { - if (!_properties.containsKey(key)) - { - return _engine.getProperty(key); - } - return _properties.get(key); - } - - @Override - public final Path getCurrentExecutingScript() - { - return _currentExecutingScipt; - } - - @Override - public final T getScriptingEngine() - { - return _engine; - } -} diff --git a/L2J_Mobius_Classic_2.0_Saviors/java/com/l2jmobius/gameserver/scripting/AbstractScriptingEngine.java b/L2J_Mobius_Classic_2.0_Saviors/java/com/l2jmobius/gameserver/scripting/AbstractScriptingEngine.java deleted file mode 100644 index 90900438ec..0000000000 --- a/L2J_Mobius_Classic_2.0_Saviors/java/com/l2jmobius/gameserver/scripting/AbstractScriptingEngine.java +++ /dev/null @@ -1,74 +0,0 @@ -/* - * This file is part of the L2J Mobius project. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * 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 - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package com.l2jmobius.gameserver.scripting; - -import java.util.Arrays; -import java.util.HashMap; -import java.util.Map; - -/** - * @author HorridoJoho - */ -public abstract class AbstractScriptingEngine implements IScriptingEngine -{ - private final String _engineName; - private final String _engineVersion; - private final String[] _commonFileExtensions; - private final Map _properties; - - protected AbstractScriptingEngine(String engineName, String engineVersion, String... commonFileExtensions) - { - if ((engineName == null) || engineName.isEmpty() || (engineVersion == null) || engineVersion.isEmpty() || (commonFileExtensions == null) || (commonFileExtensions.length == 0)) - { - throw new IllegalArgumentException(); - } - _engineName = engineName; - _engineVersion = engineVersion; - _commonFileExtensions = commonFileExtensions; - _properties = new HashMap<>(); - } - - @Override - public final String setProperty(String key, String value) - { - return _properties.put(key, value); - } - - @Override - public final String getProperty(String key) - { - return _properties.get(key); - } - - @Override - public final String getEngineName() - { - return _engineName; - } - - @Override - public final String getEngineVersion() - { - return _engineVersion; - } - - @Override - public final String[] getCommonFileExtensions() - { - return Arrays.copyOf(_commonFileExtensions, _commonFileExtensions.length); - } -} diff --git a/L2J_Mobius_Classic_2.0_Saviors/java/com/l2jmobius/gameserver/scripting/IExecutionContext.java b/L2J_Mobius_Classic_2.0_Saviors/java/com/l2jmobius/gameserver/scripting/IExecutionContext.java deleted file mode 100644 index eaf4860b8a..0000000000 --- a/L2J_Mobius_Classic_2.0_Saviors/java/com/l2jmobius/gameserver/scripting/IExecutionContext.java +++ /dev/null @@ -1,71 +0,0 @@ -/* - * This file is part of the L2J Mobius project. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * 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 - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package com.l2jmobius.gameserver.scripting; - -import java.nio.file.Path; -import java.util.Map; -import java.util.Map.Entry; - -/** - * @author HorridoJoho - */ -public interface IExecutionContext -{ - /** - * Properties set here override the settings from the IScriptEngine
- * this class was created from. - * @param key the key - * @param value the value - * @return the previous value, or null when this key was not present before - */ - String setProperty(String key, String value); - - /** - * Executes all script in the iterable. - * @param sourcePaths the scripts to execute - * @return map of failed executions, Path=source file Throwable=thrown exception - * @throws Exception preparation for script execution failed - */ - Map executeScripts(Iterable sourcePaths) throws Exception; - - /** - * Executes a single file. - * @param sourcePath the script to execute - * @return entry of failed execution, Path=source file Throwable=thrown exception - * @throws Exception preparation for script execution failed - */ - Entry executeScript(Path sourcePath) throws Exception; - - /** - * Method to get the specified property value. - * @param key the key - * @return the value, or null if the key is not present - */ - String getProperty(String key); - - /** - * Method to get the current executing script file. - * @return the currently executing script file, null if non - */ - Path getCurrentExecutingScript(); - - /** - * Method to get the script engine this execution context belongs to. - * @return the script engine this execution context belongs to - */ - IScriptingEngine getScriptingEngine(); -} diff --git a/L2J_Mobius_Classic_2.0_Saviors/java/com/l2jmobius/gameserver/scripting/IScriptingEngine.java b/L2J_Mobius_Classic_2.0_Saviors/java/com/l2jmobius/gameserver/scripting/IScriptingEngine.java deleted file mode 100644 index 883172cce1..0000000000 --- a/L2J_Mobius_Classic_2.0_Saviors/java/com/l2jmobius/gameserver/scripting/IScriptingEngine.java +++ /dev/null @@ -1,75 +0,0 @@ -/* - * This file is part of the L2J Mobius project. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * 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 - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package com.l2jmobius.gameserver.scripting; - -/** - * @author HorridoJoho - */ -public interface IScriptingEngine -{ - /** - * Sets script engine properties. The script values will be available
- * to the the insatnces created {@link IExecutionContext} implementation. - * @param key the key - * @param value the value - * @return the previous value, or null when this key was not present before - */ - String setProperty(String key, String value); - - /** - * Creates an execution context. - * @return the created execution context. - */ - IExecutionContext createExecutionContext(); - - /** - * Method to get the specified property value. - * @param key the key - * @return the value,or null if the key is not present - */ - String getProperty(String key); - - /** - * Method to get the engine name. - * @return the engine name - */ - String getEngineName(); - - /** - * Method to get the engine version. - * @return the engine version - */ - String getEngineVersion(); - - /** - * Method to get the scripting language name. - * @return the scripting engine name - */ - String getLanguageName(); - - /** - * Method to get the the language version. - * @return the language version - */ - String getLanguageVersion(); - - /** - * Method to retrive the commonly used file extensions for the language. - * @return the commonly used file extensions for the language - */ - String[] getCommonFileExtensions(); -} diff --git a/L2J_Mobius_Classic_2.0_Saviors/java/com/l2jmobius/gameserver/scripting/ScriptEngineManager.java b/L2J_Mobius_Classic_2.0_Saviors/java/com/l2jmobius/gameserver/scripting/ScriptEngineManager.java index 25a46c93b2..7f8d5fbd5b 100644 --- a/L2J_Mobius_Classic_2.0_Saviors/java/com/l2jmobius/gameserver/scripting/ScriptEngineManager.java +++ b/L2J_Mobius_Classic_2.0_Saviors/java/com/l2jmobius/gameserver/scripting/ScriptEngineManager.java @@ -17,7 +17,6 @@ package com.l2jmobius.gameserver.scripting; import java.io.File; -import java.io.FileInputStream; import java.io.IOException; import java.nio.file.FileVisitResult; import java.nio.file.Files; @@ -31,9 +30,6 @@ import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Map.Entry; -import java.util.Objects; -import java.util.Properties; -import java.util.ServiceLoader; import java.util.logging.Level; import java.util.logging.Logger; @@ -41,15 +37,16 @@ import org.w3c.dom.Document; import com.l2jmobius.Config; import com.l2jmobius.commons.util.IXmlReader; +import com.l2jmobius.gameserver.scripting.java.JavaExecutionContext; import com.l2jmobius.gameserver.scripting.java.JavaScriptingEngine; /** - * Caches script engines and provides functionality for executing and managing scripts. - * @author KenM, HorridoJoho + * @author Mobius */ public final class ScriptEngineManager implements IXmlReader { private static final Logger LOGGER = Logger.getLogger(ScriptEngineManager.class.getName()); + public static final Path SCRIPT_FOLDER = Config.SCRIPT_ROOT.toPath(); public static final Path MASTER_HANDLER_FILE = Paths.get(SCRIPT_FOLDER.toString(), "handlers", "MasterHandler.java"); public static final Path EFFECT_MASTER_HANDLER_FILE = Paths.get(SCRIPT_FOLDER.toString(), "handlers", "EffectMasterHandler.java"); @@ -57,19 +54,11 @@ public final class ScriptEngineManager implements IXmlReader public static final Path CONDITION_HANDLER_FILE = Paths.get(SCRIPT_FOLDER.toString(), "handlers", "ConditionMasterHandler.java"); public static final Path ONE_DAY_REWARD_MASTER_HANDLER = Paths.get(SCRIPT_FOLDER.toString(), "handlers", "DailyMissionMasterHandler.java"); - private IExecutionContext _javaExecutionContext = null; - static final List _exclusions = new ArrayList<>(); + private static final JavaExecutionContext _javaExecutionContext = new JavaScriptingEngine().createExecutionContext(); + protected static final List _exclusions = new ArrayList<>(); protected ScriptEngineManager() { - final Properties props = loadProperties(); - - // Default java engine implementation - registerEngine(new JavaScriptingEngine(), props); - - // Load external script engines - ServiceLoader.load(IScriptingEngine.class).forEach(engine -> registerEngine(engine, props)); - // Load Scripts.xml load(); } @@ -144,71 +133,6 @@ public final class ScriptEngineManager implements IXmlReader } } - private Properties loadProperties() - { - Properties props = null; - try (FileInputStream fis = new FileInputStream("config/ScriptEngine.ini")) - { - props = new Properties(); - props.load(fis); - } - catch (Exception e) - { - props = null; - LOGGER.warning("Couldn't load ScriptEngine.ini: " + e.getMessage()); - } - return props; - } - - private void registerEngine(IScriptingEngine engine, Properties props) - { - maybeSetProperties("language." + engine.getLanguageName() + ".", props, engine); - _javaExecutionContext = engine.createExecutionContext(); - LOGGER.info("ScriptEngine: " + engine.getEngineName() + " " + engine.getEngineVersion() + " (" + engine.getLanguageName() + " " + engine.getLanguageVersion() + ")"); - } - - private void maybeSetProperties(String propPrefix, Properties props, IScriptingEngine engine) - { - if (props == null) - { - return; - } - - for (Entry prop : props.entrySet()) - { - String key = (String) prop.getKey(); - String value = (String) prop.getValue(); - - if (key.startsWith(propPrefix)) - { - key = key.substring(propPrefix.length()); - if (value.startsWith("%") && value.endsWith("%")) - { - value = System.getProperty(value.substring(1, value.length() - 1)); - } - - engine.setProperty(key, value); - } - } - } - - public void executeScriptList() throws Exception - { - if (Config.ALT_DEV_NO_QUESTS) - { - return; - } - - final List files = new ArrayList<>(); - processDirectory(SCRIPT_FOLDER.toFile(), files); - - final Map invokationErrors = _javaExecutionContext.executeScripts(files); - for (Entry entry : invokationErrors.entrySet()) - { - LOGGER.log(Level.WARNING, "ScriptEngine: " + entry.getKey() + " failed execution!", entry.getValue()); - } - } - private void processDirectory(File dir, List files) { for (File file : dir.listFiles()) @@ -230,15 +154,12 @@ public final class ScriptEngineManager implements IXmlReader public void executeScript(Path sourceFile) throws Exception { - Objects.requireNonNull(sourceFile); - if (!sourceFile.isAbsolute()) { sourceFile = SCRIPT_FOLDER.resolve(sourceFile); } sourceFile = sourceFile.toAbsolutePath(); - Objects.requireNonNull(sourceFile, "ScriptFile: " + sourceFile + " does not have an extension to determine the script engine!"); final Entry error = _javaExecutionContext.executeScript(sourceFile); if (error != null) @@ -247,6 +168,23 @@ public final class ScriptEngineManager implements IXmlReader } } + public void executeScriptList() throws Exception + { + if (Config.ALT_DEV_NO_QUESTS) + { + return; + } + + final List files = new ArrayList<>(); + processDirectory(SCRIPT_FOLDER.toFile(), files); + + final Map invokationErrors = _javaExecutionContext.executeScripts(files); + for (Entry entry : invokationErrors.entrySet()) + { + LOGGER.log(Level.WARNING, "ScriptEngine: " + entry.getKey() + " failed execution!", entry.getValue()); + } + } + public Path getCurrentLoadingScript() { return _javaExecutionContext.getCurrentExecutingScript(); @@ -254,11 +192,11 @@ public final class ScriptEngineManager implements IXmlReader public static ScriptEngineManager getInstance() { - return SingletonHolder._instance; + return SingletonHolder.INSTANCE; } private static class SingletonHolder { - protected static final ScriptEngineManager _instance = new ScriptEngineManager(); + protected static final ScriptEngineManager INSTANCE = new ScriptEngineManager(); } } \ No newline at end of file diff --git a/L2J_Mobius_Classic_2.0_Saviors/java/com/l2jmobius/gameserver/scripting/java/JavaCompilerException.java b/L2J_Mobius_Classic_2.0_Saviors/java/com/l2jmobius/gameserver/scripting/java/JavaCompilerException.java deleted file mode 100644 index 241ffe91bd..0000000000 --- a/L2J_Mobius_Classic_2.0_Saviors/java/com/l2jmobius/gameserver/scripting/java/JavaCompilerException.java +++ /dev/null @@ -1,28 +0,0 @@ -/* - * This file is part of the L2J Mobius project. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * 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 - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package com.l2jmobius.gameserver.scripting.java; - -/** - * @author HorridoJoho - */ -public final class JavaCompilerException extends RuntimeException -{ - public JavaCompilerException(String diagnostics) - { - super(diagnostics); - } -} diff --git a/L2J_Mobius_Classic_2.0_Saviors/java/com/l2jmobius/gameserver/scripting/java/JavaExecutionContext.java b/L2J_Mobius_Classic_2.0_Saviors/java/com/l2jmobius/gameserver/scripting/java/JavaExecutionContext.java index 70d4da0893..1eddebd693 100644 --- a/L2J_Mobius_Classic_2.0_Saviors/java/com/l2jmobius/gameserver/scripting/java/JavaExecutionContext.java +++ b/L2J_Mobius_Classic_2.0_Saviors/java/com/l2jmobius/gameserver/scripting/java/JavaExecutionContext.java @@ -22,9 +22,9 @@ import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.nio.charset.StandardCharsets; import java.nio.file.Path; +import java.util.ArrayList; import java.util.Arrays; -import java.util.LinkedHashMap; -import java.util.LinkedList; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; @@ -34,22 +34,20 @@ import org.openjavac.tools.Diagnostic; import org.openjavac.tools.DiagnosticCollector; import org.openjavac.tools.JavaFileObject; -import com.l2jmobius.gameserver.scripting.AbstractExecutionContext; import com.l2jmobius.gameserver.scripting.annotations.Disabled; /** * @author HorridoJoho */ -public final class JavaExecutionContext extends AbstractExecutionContext +public final class JavaExecutionContext extends JavaScriptingEngine { private static final Logger LOGGER = Logger.getLogger(JavaExecutionContext.class.getName()); - private static final List _options = new LinkedList<>(); + private static final List _options = new ArrayList<>(); + private static Path _currentExecutingScript; JavaExecutionContext(JavaScriptingEngine engine) { - super(engine); - // Set options. addOptionIfNotNull(_options, getProperty("source"), "-source"); addOptionIfNotNull(_options, getProperty("sourcepath"), "-sourcepath"); @@ -76,7 +74,7 @@ public final class JavaExecutionContext extends AbstractExecutionContext executeScripts(Iterable sourcePaths) throws Exception { final DiagnosticCollector fileManagerDiagnostics = new DiagnosticCollector<>(); final DiagnosticCollector compilationDiagnostics = new DiagnosticCollector<>(); - try (ScriptingFileManager fileManager = new ScriptingFileManager(getScriptingEngine().getCompiler().getStandardFileManager(fileManagerDiagnostics, null, StandardCharsets.UTF_8))) + try (ScriptingFileManager fileManager = new ScriptingFileManager(getCompiler().getStandardFileManager(fileManagerDiagnostics, null, StandardCharsets.UTF_8))) { // We really need an iterable of files or strings. - final List sourcePathStrings = new LinkedList<>(); + final List sourcePathStrings = new ArrayList<>(); for (Path sourcePath : sourcePaths) { sourcePathStrings.add(sourcePath.toString()); @@ -150,7 +147,7 @@ public final class JavaExecutionContext extends AbstractExecutionContext executionFailures = new LinkedHashMap<>(); + final Map executionFailures = new HashMap<>(); final Iterable compiledClasses = fileManager.getCompiledClasses(); for (Path sourcePath : sourcePaths) { @@ -199,7 +196,7 @@ public final class JavaExecutionContext extends AbstractExecutionContext executeScript(Path sourcePath) throws Exception { final Map executionFailures = executeScripts(Arrays.asList(sourcePath)); @@ -254,4 +250,9 @@ public final class JavaExecutionContext extends AbstractExecutionContext _properties = new HashMap<>(); + private final static JavaCompiler _compiler = JavacTool.create(); public JavaScriptingEngine() { - super("Java Engine", "10", "java"); - } - - private void determineCompilerOrThrow() - { - if (_compiler == null) + // Load config. + Properties props = new Properties(); + try (FileInputStream fis = new FileInputStream("config/ScriptEngine.ini")) { - _compiler = JavacTool.create(); + props.load(fis); + } + catch (Exception e) + { + LOGGER.warning("Could not load ScriptEngine.ini: " + e.getMessage()); } - if (_compiler == null) + // Set properties. + for (Entry prop : props.entrySet()) { - throw new IllegalStateException("No JavaCompiler service installed!"); - } - } - - private void ensureCompilerOrThrow() - { - if (_compiler == null) - { - synchronized (this) + String key = (String) prop.getKey(); + String value = (String) prop.getValue(); + + if (value.startsWith("%") && value.endsWith("%")) { - if (_compiler == null) - { - determineCompilerOrThrow(); - } + value = System.getProperty(value.substring(1, value.length() - 1)); } + + _properties.put(key, value); } } - JavaCompiler getCompiler() + public JavaExecutionContext createExecutionContext() { - return _compiler; - } - - @Override - public IExecutionContext createExecutionContext() - { - ensureCompilerOrThrow(); return new JavaExecutionContext(this); } - @Override - public String getLanguageName() + public final String getProperty(String key) { - return "Java"; + return _properties.get(key); } - @Override - public String getLanguageVersion() + public JavaCompiler getCompiler() { - ensureCompilerOrThrow(); - return Arrays.deepToString(_compiler.getSourceVersions().toArray(new SourceVersion[0])).replace("RELEASE_", ""); + return _compiler; } } \ No newline at end of file diff --git a/L2J_Mobius_Classic_2.0_Saviors/java/com/l2jmobius/gameserver/scripting/java/ScriptingOutputFileObject.java b/L2J_Mobius_Classic_2.0_Saviors/java/com/l2jmobius/gameserver/scripting/java/ScriptingOutputFileObject.java index 439cf5d260..9ab668887e 100644 --- a/L2J_Mobius_Classic_2.0_Saviors/java/com/l2jmobius/gameserver/scripting/java/ScriptingOutputFileObject.java +++ b/L2J_Mobius_Classic_2.0_Saviors/java/com/l2jmobius/gameserver/scripting/java/ScriptingOutputFileObject.java @@ -144,5 +144,4 @@ final class ScriptingOutputFileObject implements JavaFileObject { return null; } - } diff --git a/L2J_Mobius_Classic_2.1_Zaken/dist/game/config/ScriptEngine.ini b/L2J_Mobius_Classic_2.1_Zaken/dist/game/config/ScriptEngine.ini index 2d1e972e72..5004dca9fd 100644 --- a/L2J_Mobius_Classic_2.1_Zaken/dist/game/config/ScriptEngine.ini +++ b/L2J_Mobius_Classic_2.1_Zaken/dist/game/config/ScriptEngine.ini @@ -1,23 +1,23 @@ -############################################################################### -# Properties retrived with System.getProperty(String) can be used as values +# --------------------------------------------------------------------------- +# Script Engine Settings +# --------------------------------------------------------------------------- +# Properties retrieved with System.getProperty(String) can be used as values # by enclosing the property name with %. Eg.: %java.class.path% -############################################################################### - # The parent class loader for isolated script class loaders. # When this property is not specified, has an invalid value or is a class name which could not be found, the System classloader is used. # Values: System, ThreadContext or a fully qualified java class name -language.Java.classloader=System -#language.Java.classloader=ThreadContext +classloader=System +#classloader=ThreadContext -# Source compatibility -language.Java.source=1.8 +# Source compatibility. +source=1.8 # The java sourcepath, when you have a different datapack root, you must change this too. -language.Java.sourcepath=data/scripts +sourcepath=data/scripts -# The java classpath -language.Java.cp=%java.class.path% +# The java classpath. +cp=%java.class.path% -# The debug informations to generate for compiled class files -language.Java.g=source,lines,vars \ No newline at end of file +# The debug informations to generate for compiled class files. +g=source,lines,vars \ No newline at end of file diff --git a/L2J_Mobius_Classic_2.1_Zaken/java/com/l2jmobius/gameserver/scripting/AbstractExecutionContext.java b/L2J_Mobius_Classic_2.1_Zaken/java/com/l2jmobius/gameserver/scripting/AbstractExecutionContext.java deleted file mode 100644 index 296b54f06b..0000000000 --- a/L2J_Mobius_Classic_2.1_Zaken/java/com/l2jmobius/gameserver/scripting/AbstractExecutionContext.java +++ /dev/null @@ -1,75 +0,0 @@ -/* - * This file is part of the L2J Mobius project. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * 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 - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package com.l2jmobius.gameserver.scripting; - -import java.nio.file.Path; -import java.util.HashMap; -import java.util.Map; - -/** - * @author HorridoJoho - * @param - */ -public abstract class AbstractExecutionContext implements IExecutionContext -{ - private final T _engine; - private final Map _properties; - private volatile Path _currentExecutingScipt; - - protected AbstractExecutionContext(T engine) - { - if (engine == null) - { - throw new IllegalArgumentException(); - } - _engine = engine; - _properties = new HashMap<>(); - } - - protected final void setCurrentExecutingScript(Path currentExecutingScript) - { - _currentExecutingScipt = currentExecutingScript; - } - - @Override - public final String setProperty(String key, String value) - { - return _properties.put(key, value); - } - - @Override - public final String getProperty(String key) - { - if (!_properties.containsKey(key)) - { - return _engine.getProperty(key); - } - return _properties.get(key); - } - - @Override - public final Path getCurrentExecutingScript() - { - return _currentExecutingScipt; - } - - @Override - public final T getScriptingEngine() - { - return _engine; - } -} diff --git a/L2J_Mobius_Classic_2.1_Zaken/java/com/l2jmobius/gameserver/scripting/AbstractScriptingEngine.java b/L2J_Mobius_Classic_2.1_Zaken/java/com/l2jmobius/gameserver/scripting/AbstractScriptingEngine.java deleted file mode 100644 index 90900438ec..0000000000 --- a/L2J_Mobius_Classic_2.1_Zaken/java/com/l2jmobius/gameserver/scripting/AbstractScriptingEngine.java +++ /dev/null @@ -1,74 +0,0 @@ -/* - * This file is part of the L2J Mobius project. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * 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 - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package com.l2jmobius.gameserver.scripting; - -import java.util.Arrays; -import java.util.HashMap; -import java.util.Map; - -/** - * @author HorridoJoho - */ -public abstract class AbstractScriptingEngine implements IScriptingEngine -{ - private final String _engineName; - private final String _engineVersion; - private final String[] _commonFileExtensions; - private final Map _properties; - - protected AbstractScriptingEngine(String engineName, String engineVersion, String... commonFileExtensions) - { - if ((engineName == null) || engineName.isEmpty() || (engineVersion == null) || engineVersion.isEmpty() || (commonFileExtensions == null) || (commonFileExtensions.length == 0)) - { - throw new IllegalArgumentException(); - } - _engineName = engineName; - _engineVersion = engineVersion; - _commonFileExtensions = commonFileExtensions; - _properties = new HashMap<>(); - } - - @Override - public final String setProperty(String key, String value) - { - return _properties.put(key, value); - } - - @Override - public final String getProperty(String key) - { - return _properties.get(key); - } - - @Override - public final String getEngineName() - { - return _engineName; - } - - @Override - public final String getEngineVersion() - { - return _engineVersion; - } - - @Override - public final String[] getCommonFileExtensions() - { - return Arrays.copyOf(_commonFileExtensions, _commonFileExtensions.length); - } -} diff --git a/L2J_Mobius_Classic_2.1_Zaken/java/com/l2jmobius/gameserver/scripting/IExecutionContext.java b/L2J_Mobius_Classic_2.1_Zaken/java/com/l2jmobius/gameserver/scripting/IExecutionContext.java deleted file mode 100644 index eaf4860b8a..0000000000 --- a/L2J_Mobius_Classic_2.1_Zaken/java/com/l2jmobius/gameserver/scripting/IExecutionContext.java +++ /dev/null @@ -1,71 +0,0 @@ -/* - * This file is part of the L2J Mobius project. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * 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 - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package com.l2jmobius.gameserver.scripting; - -import java.nio.file.Path; -import java.util.Map; -import java.util.Map.Entry; - -/** - * @author HorridoJoho - */ -public interface IExecutionContext -{ - /** - * Properties set here override the settings from the IScriptEngine
- * this class was created from. - * @param key the key - * @param value the value - * @return the previous value, or null when this key was not present before - */ - String setProperty(String key, String value); - - /** - * Executes all script in the iterable. - * @param sourcePaths the scripts to execute - * @return map of failed executions, Path=source file Throwable=thrown exception - * @throws Exception preparation for script execution failed - */ - Map executeScripts(Iterable sourcePaths) throws Exception; - - /** - * Executes a single file. - * @param sourcePath the script to execute - * @return entry of failed execution, Path=source file Throwable=thrown exception - * @throws Exception preparation for script execution failed - */ - Entry executeScript(Path sourcePath) throws Exception; - - /** - * Method to get the specified property value. - * @param key the key - * @return the value, or null if the key is not present - */ - String getProperty(String key); - - /** - * Method to get the current executing script file. - * @return the currently executing script file, null if non - */ - Path getCurrentExecutingScript(); - - /** - * Method to get the script engine this execution context belongs to. - * @return the script engine this execution context belongs to - */ - IScriptingEngine getScriptingEngine(); -} diff --git a/L2J_Mobius_Classic_2.1_Zaken/java/com/l2jmobius/gameserver/scripting/IScriptingEngine.java b/L2J_Mobius_Classic_2.1_Zaken/java/com/l2jmobius/gameserver/scripting/IScriptingEngine.java deleted file mode 100644 index 883172cce1..0000000000 --- a/L2J_Mobius_Classic_2.1_Zaken/java/com/l2jmobius/gameserver/scripting/IScriptingEngine.java +++ /dev/null @@ -1,75 +0,0 @@ -/* - * This file is part of the L2J Mobius project. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * 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 - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package com.l2jmobius.gameserver.scripting; - -/** - * @author HorridoJoho - */ -public interface IScriptingEngine -{ - /** - * Sets script engine properties. The script values will be available
- * to the the insatnces created {@link IExecutionContext} implementation. - * @param key the key - * @param value the value - * @return the previous value, or null when this key was not present before - */ - String setProperty(String key, String value); - - /** - * Creates an execution context. - * @return the created execution context. - */ - IExecutionContext createExecutionContext(); - - /** - * Method to get the specified property value. - * @param key the key - * @return the value,or null if the key is not present - */ - String getProperty(String key); - - /** - * Method to get the engine name. - * @return the engine name - */ - String getEngineName(); - - /** - * Method to get the engine version. - * @return the engine version - */ - String getEngineVersion(); - - /** - * Method to get the scripting language name. - * @return the scripting engine name - */ - String getLanguageName(); - - /** - * Method to get the the language version. - * @return the language version - */ - String getLanguageVersion(); - - /** - * Method to retrive the commonly used file extensions for the language. - * @return the commonly used file extensions for the language - */ - String[] getCommonFileExtensions(); -} diff --git a/L2J_Mobius_Classic_2.1_Zaken/java/com/l2jmobius/gameserver/scripting/ScriptEngineManager.java b/L2J_Mobius_Classic_2.1_Zaken/java/com/l2jmobius/gameserver/scripting/ScriptEngineManager.java index 25a46c93b2..7f8d5fbd5b 100644 --- a/L2J_Mobius_Classic_2.1_Zaken/java/com/l2jmobius/gameserver/scripting/ScriptEngineManager.java +++ b/L2J_Mobius_Classic_2.1_Zaken/java/com/l2jmobius/gameserver/scripting/ScriptEngineManager.java @@ -17,7 +17,6 @@ package com.l2jmobius.gameserver.scripting; import java.io.File; -import java.io.FileInputStream; import java.io.IOException; import java.nio.file.FileVisitResult; import java.nio.file.Files; @@ -31,9 +30,6 @@ import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Map.Entry; -import java.util.Objects; -import java.util.Properties; -import java.util.ServiceLoader; import java.util.logging.Level; import java.util.logging.Logger; @@ -41,15 +37,16 @@ import org.w3c.dom.Document; import com.l2jmobius.Config; import com.l2jmobius.commons.util.IXmlReader; +import com.l2jmobius.gameserver.scripting.java.JavaExecutionContext; import com.l2jmobius.gameserver.scripting.java.JavaScriptingEngine; /** - * Caches script engines and provides functionality for executing and managing scripts. - * @author KenM, HorridoJoho + * @author Mobius */ public final class ScriptEngineManager implements IXmlReader { private static final Logger LOGGER = Logger.getLogger(ScriptEngineManager.class.getName()); + public static final Path SCRIPT_FOLDER = Config.SCRIPT_ROOT.toPath(); public static final Path MASTER_HANDLER_FILE = Paths.get(SCRIPT_FOLDER.toString(), "handlers", "MasterHandler.java"); public static final Path EFFECT_MASTER_HANDLER_FILE = Paths.get(SCRIPT_FOLDER.toString(), "handlers", "EffectMasterHandler.java"); @@ -57,19 +54,11 @@ public final class ScriptEngineManager implements IXmlReader public static final Path CONDITION_HANDLER_FILE = Paths.get(SCRIPT_FOLDER.toString(), "handlers", "ConditionMasterHandler.java"); public static final Path ONE_DAY_REWARD_MASTER_HANDLER = Paths.get(SCRIPT_FOLDER.toString(), "handlers", "DailyMissionMasterHandler.java"); - private IExecutionContext _javaExecutionContext = null; - static final List _exclusions = new ArrayList<>(); + private static final JavaExecutionContext _javaExecutionContext = new JavaScriptingEngine().createExecutionContext(); + protected static final List _exclusions = new ArrayList<>(); protected ScriptEngineManager() { - final Properties props = loadProperties(); - - // Default java engine implementation - registerEngine(new JavaScriptingEngine(), props); - - // Load external script engines - ServiceLoader.load(IScriptingEngine.class).forEach(engine -> registerEngine(engine, props)); - // Load Scripts.xml load(); } @@ -144,71 +133,6 @@ public final class ScriptEngineManager implements IXmlReader } } - private Properties loadProperties() - { - Properties props = null; - try (FileInputStream fis = new FileInputStream("config/ScriptEngine.ini")) - { - props = new Properties(); - props.load(fis); - } - catch (Exception e) - { - props = null; - LOGGER.warning("Couldn't load ScriptEngine.ini: " + e.getMessage()); - } - return props; - } - - private void registerEngine(IScriptingEngine engine, Properties props) - { - maybeSetProperties("language." + engine.getLanguageName() + ".", props, engine); - _javaExecutionContext = engine.createExecutionContext(); - LOGGER.info("ScriptEngine: " + engine.getEngineName() + " " + engine.getEngineVersion() + " (" + engine.getLanguageName() + " " + engine.getLanguageVersion() + ")"); - } - - private void maybeSetProperties(String propPrefix, Properties props, IScriptingEngine engine) - { - if (props == null) - { - return; - } - - for (Entry prop : props.entrySet()) - { - String key = (String) prop.getKey(); - String value = (String) prop.getValue(); - - if (key.startsWith(propPrefix)) - { - key = key.substring(propPrefix.length()); - if (value.startsWith("%") && value.endsWith("%")) - { - value = System.getProperty(value.substring(1, value.length() - 1)); - } - - engine.setProperty(key, value); - } - } - } - - public void executeScriptList() throws Exception - { - if (Config.ALT_DEV_NO_QUESTS) - { - return; - } - - final List files = new ArrayList<>(); - processDirectory(SCRIPT_FOLDER.toFile(), files); - - final Map invokationErrors = _javaExecutionContext.executeScripts(files); - for (Entry entry : invokationErrors.entrySet()) - { - LOGGER.log(Level.WARNING, "ScriptEngine: " + entry.getKey() + " failed execution!", entry.getValue()); - } - } - private void processDirectory(File dir, List files) { for (File file : dir.listFiles()) @@ -230,15 +154,12 @@ public final class ScriptEngineManager implements IXmlReader public void executeScript(Path sourceFile) throws Exception { - Objects.requireNonNull(sourceFile); - if (!sourceFile.isAbsolute()) { sourceFile = SCRIPT_FOLDER.resolve(sourceFile); } sourceFile = sourceFile.toAbsolutePath(); - Objects.requireNonNull(sourceFile, "ScriptFile: " + sourceFile + " does not have an extension to determine the script engine!"); final Entry error = _javaExecutionContext.executeScript(sourceFile); if (error != null) @@ -247,6 +168,23 @@ public final class ScriptEngineManager implements IXmlReader } } + public void executeScriptList() throws Exception + { + if (Config.ALT_DEV_NO_QUESTS) + { + return; + } + + final List files = new ArrayList<>(); + processDirectory(SCRIPT_FOLDER.toFile(), files); + + final Map invokationErrors = _javaExecutionContext.executeScripts(files); + for (Entry entry : invokationErrors.entrySet()) + { + LOGGER.log(Level.WARNING, "ScriptEngine: " + entry.getKey() + " failed execution!", entry.getValue()); + } + } + public Path getCurrentLoadingScript() { return _javaExecutionContext.getCurrentExecutingScript(); @@ -254,11 +192,11 @@ public final class ScriptEngineManager implements IXmlReader public static ScriptEngineManager getInstance() { - return SingletonHolder._instance; + return SingletonHolder.INSTANCE; } private static class SingletonHolder { - protected static final ScriptEngineManager _instance = new ScriptEngineManager(); + protected static final ScriptEngineManager INSTANCE = new ScriptEngineManager(); } } \ No newline at end of file diff --git a/L2J_Mobius_Classic_2.1_Zaken/java/com/l2jmobius/gameserver/scripting/java/JavaCompilerException.java b/L2J_Mobius_Classic_2.1_Zaken/java/com/l2jmobius/gameserver/scripting/java/JavaCompilerException.java deleted file mode 100644 index 241ffe91bd..0000000000 --- a/L2J_Mobius_Classic_2.1_Zaken/java/com/l2jmobius/gameserver/scripting/java/JavaCompilerException.java +++ /dev/null @@ -1,28 +0,0 @@ -/* - * This file is part of the L2J Mobius project. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * 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 - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package com.l2jmobius.gameserver.scripting.java; - -/** - * @author HorridoJoho - */ -public final class JavaCompilerException extends RuntimeException -{ - public JavaCompilerException(String diagnostics) - { - super(diagnostics); - } -} diff --git a/L2J_Mobius_Classic_2.1_Zaken/java/com/l2jmobius/gameserver/scripting/java/JavaExecutionContext.java b/L2J_Mobius_Classic_2.1_Zaken/java/com/l2jmobius/gameserver/scripting/java/JavaExecutionContext.java index 70d4da0893..1eddebd693 100644 --- a/L2J_Mobius_Classic_2.1_Zaken/java/com/l2jmobius/gameserver/scripting/java/JavaExecutionContext.java +++ b/L2J_Mobius_Classic_2.1_Zaken/java/com/l2jmobius/gameserver/scripting/java/JavaExecutionContext.java @@ -22,9 +22,9 @@ import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.nio.charset.StandardCharsets; import java.nio.file.Path; +import java.util.ArrayList; import java.util.Arrays; -import java.util.LinkedHashMap; -import java.util.LinkedList; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; @@ -34,22 +34,20 @@ import org.openjavac.tools.Diagnostic; import org.openjavac.tools.DiagnosticCollector; import org.openjavac.tools.JavaFileObject; -import com.l2jmobius.gameserver.scripting.AbstractExecutionContext; import com.l2jmobius.gameserver.scripting.annotations.Disabled; /** * @author HorridoJoho */ -public final class JavaExecutionContext extends AbstractExecutionContext +public final class JavaExecutionContext extends JavaScriptingEngine { private static final Logger LOGGER = Logger.getLogger(JavaExecutionContext.class.getName()); - private static final List _options = new LinkedList<>(); + private static final List _options = new ArrayList<>(); + private static Path _currentExecutingScript; JavaExecutionContext(JavaScriptingEngine engine) { - super(engine); - // Set options. addOptionIfNotNull(_options, getProperty("source"), "-source"); addOptionIfNotNull(_options, getProperty("sourcepath"), "-sourcepath"); @@ -76,7 +74,7 @@ public final class JavaExecutionContext extends AbstractExecutionContext executeScripts(Iterable sourcePaths) throws Exception { final DiagnosticCollector fileManagerDiagnostics = new DiagnosticCollector<>(); final DiagnosticCollector compilationDiagnostics = new DiagnosticCollector<>(); - try (ScriptingFileManager fileManager = new ScriptingFileManager(getScriptingEngine().getCompiler().getStandardFileManager(fileManagerDiagnostics, null, StandardCharsets.UTF_8))) + try (ScriptingFileManager fileManager = new ScriptingFileManager(getCompiler().getStandardFileManager(fileManagerDiagnostics, null, StandardCharsets.UTF_8))) { // We really need an iterable of files or strings. - final List sourcePathStrings = new LinkedList<>(); + final List sourcePathStrings = new ArrayList<>(); for (Path sourcePath : sourcePaths) { sourcePathStrings.add(sourcePath.toString()); @@ -150,7 +147,7 @@ public final class JavaExecutionContext extends AbstractExecutionContext executionFailures = new LinkedHashMap<>(); + final Map executionFailures = new HashMap<>(); final Iterable compiledClasses = fileManager.getCompiledClasses(); for (Path sourcePath : sourcePaths) { @@ -199,7 +196,7 @@ public final class JavaExecutionContext extends AbstractExecutionContext executeScript(Path sourcePath) throws Exception { final Map executionFailures = executeScripts(Arrays.asList(sourcePath)); @@ -254,4 +250,9 @@ public final class JavaExecutionContext extends AbstractExecutionContext _properties = new HashMap<>(); + private final static JavaCompiler _compiler = JavacTool.create(); public JavaScriptingEngine() { - super("Java Engine", "10", "java"); - } - - private void determineCompilerOrThrow() - { - if (_compiler == null) + // Load config. + Properties props = new Properties(); + try (FileInputStream fis = new FileInputStream("config/ScriptEngine.ini")) { - _compiler = JavacTool.create(); + props.load(fis); + } + catch (Exception e) + { + LOGGER.warning("Could not load ScriptEngine.ini: " + e.getMessage()); } - if (_compiler == null) + // Set properties. + for (Entry prop : props.entrySet()) { - throw new IllegalStateException("No JavaCompiler service installed!"); - } - } - - private void ensureCompilerOrThrow() - { - if (_compiler == null) - { - synchronized (this) + String key = (String) prop.getKey(); + String value = (String) prop.getValue(); + + if (value.startsWith("%") && value.endsWith("%")) { - if (_compiler == null) - { - determineCompilerOrThrow(); - } + value = System.getProperty(value.substring(1, value.length() - 1)); } + + _properties.put(key, value); } } - JavaCompiler getCompiler() + public JavaExecutionContext createExecutionContext() { - return _compiler; - } - - @Override - public IExecutionContext createExecutionContext() - { - ensureCompilerOrThrow(); return new JavaExecutionContext(this); } - @Override - public String getLanguageName() + public final String getProperty(String key) { - return "Java"; + return _properties.get(key); } - @Override - public String getLanguageVersion() + public JavaCompiler getCompiler() { - ensureCompilerOrThrow(); - return Arrays.deepToString(_compiler.getSourceVersions().toArray(new SourceVersion[0])).replace("RELEASE_", ""); + return _compiler; } } \ No newline at end of file diff --git a/L2J_Mobius_Classic_2.1_Zaken/java/com/l2jmobius/gameserver/scripting/java/ScriptingOutputFileObject.java b/L2J_Mobius_Classic_2.1_Zaken/java/com/l2jmobius/gameserver/scripting/java/ScriptingOutputFileObject.java index 439cf5d260..9ab668887e 100644 --- a/L2J_Mobius_Classic_2.1_Zaken/java/com/l2jmobius/gameserver/scripting/java/ScriptingOutputFileObject.java +++ b/L2J_Mobius_Classic_2.1_Zaken/java/com/l2jmobius/gameserver/scripting/java/ScriptingOutputFileObject.java @@ -144,5 +144,4 @@ final class ScriptingOutputFileObject implements JavaFileObject { return null; } - } diff --git a/L2J_Mobius_Classic_2.2_Antharas/dist/game/config/ScriptEngine.ini b/L2J_Mobius_Classic_2.2_Antharas/dist/game/config/ScriptEngine.ini index 2d1e972e72..5004dca9fd 100644 --- a/L2J_Mobius_Classic_2.2_Antharas/dist/game/config/ScriptEngine.ini +++ b/L2J_Mobius_Classic_2.2_Antharas/dist/game/config/ScriptEngine.ini @@ -1,23 +1,23 @@ -############################################################################### -# Properties retrived with System.getProperty(String) can be used as values +# --------------------------------------------------------------------------- +# Script Engine Settings +# --------------------------------------------------------------------------- +# Properties retrieved with System.getProperty(String) can be used as values # by enclosing the property name with %. Eg.: %java.class.path% -############################################################################### - # The parent class loader for isolated script class loaders. # When this property is not specified, has an invalid value or is a class name which could not be found, the System classloader is used. # Values: System, ThreadContext or a fully qualified java class name -language.Java.classloader=System -#language.Java.classloader=ThreadContext +classloader=System +#classloader=ThreadContext -# Source compatibility -language.Java.source=1.8 +# Source compatibility. +source=1.8 # The java sourcepath, when you have a different datapack root, you must change this too. -language.Java.sourcepath=data/scripts +sourcepath=data/scripts -# The java classpath -language.Java.cp=%java.class.path% +# The java classpath. +cp=%java.class.path% -# The debug informations to generate for compiled class files -language.Java.g=source,lines,vars \ No newline at end of file +# The debug informations to generate for compiled class files. +g=source,lines,vars \ No newline at end of file diff --git a/L2J_Mobius_Classic_2.2_Antharas/java/com/l2jmobius/gameserver/scripting/AbstractExecutionContext.java b/L2J_Mobius_Classic_2.2_Antharas/java/com/l2jmobius/gameserver/scripting/AbstractExecutionContext.java deleted file mode 100644 index 296b54f06b..0000000000 --- a/L2J_Mobius_Classic_2.2_Antharas/java/com/l2jmobius/gameserver/scripting/AbstractExecutionContext.java +++ /dev/null @@ -1,75 +0,0 @@ -/* - * This file is part of the L2J Mobius project. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * 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 - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package com.l2jmobius.gameserver.scripting; - -import java.nio.file.Path; -import java.util.HashMap; -import java.util.Map; - -/** - * @author HorridoJoho - * @param - */ -public abstract class AbstractExecutionContext implements IExecutionContext -{ - private final T _engine; - private final Map _properties; - private volatile Path _currentExecutingScipt; - - protected AbstractExecutionContext(T engine) - { - if (engine == null) - { - throw new IllegalArgumentException(); - } - _engine = engine; - _properties = new HashMap<>(); - } - - protected final void setCurrentExecutingScript(Path currentExecutingScript) - { - _currentExecutingScipt = currentExecutingScript; - } - - @Override - public final String setProperty(String key, String value) - { - return _properties.put(key, value); - } - - @Override - public final String getProperty(String key) - { - if (!_properties.containsKey(key)) - { - return _engine.getProperty(key); - } - return _properties.get(key); - } - - @Override - public final Path getCurrentExecutingScript() - { - return _currentExecutingScipt; - } - - @Override - public final T getScriptingEngine() - { - return _engine; - } -} diff --git a/L2J_Mobius_Classic_2.2_Antharas/java/com/l2jmobius/gameserver/scripting/AbstractScriptingEngine.java b/L2J_Mobius_Classic_2.2_Antharas/java/com/l2jmobius/gameserver/scripting/AbstractScriptingEngine.java deleted file mode 100644 index 90900438ec..0000000000 --- a/L2J_Mobius_Classic_2.2_Antharas/java/com/l2jmobius/gameserver/scripting/AbstractScriptingEngine.java +++ /dev/null @@ -1,74 +0,0 @@ -/* - * This file is part of the L2J Mobius project. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * 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 - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package com.l2jmobius.gameserver.scripting; - -import java.util.Arrays; -import java.util.HashMap; -import java.util.Map; - -/** - * @author HorridoJoho - */ -public abstract class AbstractScriptingEngine implements IScriptingEngine -{ - private final String _engineName; - private final String _engineVersion; - private final String[] _commonFileExtensions; - private final Map _properties; - - protected AbstractScriptingEngine(String engineName, String engineVersion, String... commonFileExtensions) - { - if ((engineName == null) || engineName.isEmpty() || (engineVersion == null) || engineVersion.isEmpty() || (commonFileExtensions == null) || (commonFileExtensions.length == 0)) - { - throw new IllegalArgumentException(); - } - _engineName = engineName; - _engineVersion = engineVersion; - _commonFileExtensions = commonFileExtensions; - _properties = new HashMap<>(); - } - - @Override - public final String setProperty(String key, String value) - { - return _properties.put(key, value); - } - - @Override - public final String getProperty(String key) - { - return _properties.get(key); - } - - @Override - public final String getEngineName() - { - return _engineName; - } - - @Override - public final String getEngineVersion() - { - return _engineVersion; - } - - @Override - public final String[] getCommonFileExtensions() - { - return Arrays.copyOf(_commonFileExtensions, _commonFileExtensions.length); - } -} diff --git a/L2J_Mobius_Classic_2.2_Antharas/java/com/l2jmobius/gameserver/scripting/IExecutionContext.java b/L2J_Mobius_Classic_2.2_Antharas/java/com/l2jmobius/gameserver/scripting/IExecutionContext.java deleted file mode 100644 index eaf4860b8a..0000000000 --- a/L2J_Mobius_Classic_2.2_Antharas/java/com/l2jmobius/gameserver/scripting/IExecutionContext.java +++ /dev/null @@ -1,71 +0,0 @@ -/* - * This file is part of the L2J Mobius project. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * 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 - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package com.l2jmobius.gameserver.scripting; - -import java.nio.file.Path; -import java.util.Map; -import java.util.Map.Entry; - -/** - * @author HorridoJoho - */ -public interface IExecutionContext -{ - /** - * Properties set here override the settings from the IScriptEngine
- * this class was created from. - * @param key the key - * @param value the value - * @return the previous value, or null when this key was not present before - */ - String setProperty(String key, String value); - - /** - * Executes all script in the iterable. - * @param sourcePaths the scripts to execute - * @return map of failed executions, Path=source file Throwable=thrown exception - * @throws Exception preparation for script execution failed - */ - Map executeScripts(Iterable sourcePaths) throws Exception; - - /** - * Executes a single file. - * @param sourcePath the script to execute - * @return entry of failed execution, Path=source file Throwable=thrown exception - * @throws Exception preparation for script execution failed - */ - Entry executeScript(Path sourcePath) throws Exception; - - /** - * Method to get the specified property value. - * @param key the key - * @return the value, or null if the key is not present - */ - String getProperty(String key); - - /** - * Method to get the current executing script file. - * @return the currently executing script file, null if non - */ - Path getCurrentExecutingScript(); - - /** - * Method to get the script engine this execution context belongs to. - * @return the script engine this execution context belongs to - */ - IScriptingEngine getScriptingEngine(); -} diff --git a/L2J_Mobius_Classic_2.2_Antharas/java/com/l2jmobius/gameserver/scripting/IScriptingEngine.java b/L2J_Mobius_Classic_2.2_Antharas/java/com/l2jmobius/gameserver/scripting/IScriptingEngine.java deleted file mode 100644 index 883172cce1..0000000000 --- a/L2J_Mobius_Classic_2.2_Antharas/java/com/l2jmobius/gameserver/scripting/IScriptingEngine.java +++ /dev/null @@ -1,75 +0,0 @@ -/* - * This file is part of the L2J Mobius project. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * 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 - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package com.l2jmobius.gameserver.scripting; - -/** - * @author HorridoJoho - */ -public interface IScriptingEngine -{ - /** - * Sets script engine properties. The script values will be available
- * to the the insatnces created {@link IExecutionContext} implementation. - * @param key the key - * @param value the value - * @return the previous value, or null when this key was not present before - */ - String setProperty(String key, String value); - - /** - * Creates an execution context. - * @return the created execution context. - */ - IExecutionContext createExecutionContext(); - - /** - * Method to get the specified property value. - * @param key the key - * @return the value,or null if the key is not present - */ - String getProperty(String key); - - /** - * Method to get the engine name. - * @return the engine name - */ - String getEngineName(); - - /** - * Method to get the engine version. - * @return the engine version - */ - String getEngineVersion(); - - /** - * Method to get the scripting language name. - * @return the scripting engine name - */ - String getLanguageName(); - - /** - * Method to get the the language version. - * @return the language version - */ - String getLanguageVersion(); - - /** - * Method to retrive the commonly used file extensions for the language. - * @return the commonly used file extensions for the language - */ - String[] getCommonFileExtensions(); -} diff --git a/L2J_Mobius_Classic_2.2_Antharas/java/com/l2jmobius/gameserver/scripting/ScriptEngineManager.java b/L2J_Mobius_Classic_2.2_Antharas/java/com/l2jmobius/gameserver/scripting/ScriptEngineManager.java index 25a46c93b2..7f8d5fbd5b 100644 --- a/L2J_Mobius_Classic_2.2_Antharas/java/com/l2jmobius/gameserver/scripting/ScriptEngineManager.java +++ b/L2J_Mobius_Classic_2.2_Antharas/java/com/l2jmobius/gameserver/scripting/ScriptEngineManager.java @@ -17,7 +17,6 @@ package com.l2jmobius.gameserver.scripting; import java.io.File; -import java.io.FileInputStream; import java.io.IOException; import java.nio.file.FileVisitResult; import java.nio.file.Files; @@ -31,9 +30,6 @@ import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Map.Entry; -import java.util.Objects; -import java.util.Properties; -import java.util.ServiceLoader; import java.util.logging.Level; import java.util.logging.Logger; @@ -41,15 +37,16 @@ import org.w3c.dom.Document; import com.l2jmobius.Config; import com.l2jmobius.commons.util.IXmlReader; +import com.l2jmobius.gameserver.scripting.java.JavaExecutionContext; import com.l2jmobius.gameserver.scripting.java.JavaScriptingEngine; /** - * Caches script engines and provides functionality for executing and managing scripts. - * @author KenM, HorridoJoho + * @author Mobius */ public final class ScriptEngineManager implements IXmlReader { private static final Logger LOGGER = Logger.getLogger(ScriptEngineManager.class.getName()); + public static final Path SCRIPT_FOLDER = Config.SCRIPT_ROOT.toPath(); public static final Path MASTER_HANDLER_FILE = Paths.get(SCRIPT_FOLDER.toString(), "handlers", "MasterHandler.java"); public static final Path EFFECT_MASTER_HANDLER_FILE = Paths.get(SCRIPT_FOLDER.toString(), "handlers", "EffectMasterHandler.java"); @@ -57,19 +54,11 @@ public final class ScriptEngineManager implements IXmlReader public static final Path CONDITION_HANDLER_FILE = Paths.get(SCRIPT_FOLDER.toString(), "handlers", "ConditionMasterHandler.java"); public static final Path ONE_DAY_REWARD_MASTER_HANDLER = Paths.get(SCRIPT_FOLDER.toString(), "handlers", "DailyMissionMasterHandler.java"); - private IExecutionContext _javaExecutionContext = null; - static final List _exclusions = new ArrayList<>(); + private static final JavaExecutionContext _javaExecutionContext = new JavaScriptingEngine().createExecutionContext(); + protected static final List _exclusions = new ArrayList<>(); protected ScriptEngineManager() { - final Properties props = loadProperties(); - - // Default java engine implementation - registerEngine(new JavaScriptingEngine(), props); - - // Load external script engines - ServiceLoader.load(IScriptingEngine.class).forEach(engine -> registerEngine(engine, props)); - // Load Scripts.xml load(); } @@ -144,71 +133,6 @@ public final class ScriptEngineManager implements IXmlReader } } - private Properties loadProperties() - { - Properties props = null; - try (FileInputStream fis = new FileInputStream("config/ScriptEngine.ini")) - { - props = new Properties(); - props.load(fis); - } - catch (Exception e) - { - props = null; - LOGGER.warning("Couldn't load ScriptEngine.ini: " + e.getMessage()); - } - return props; - } - - private void registerEngine(IScriptingEngine engine, Properties props) - { - maybeSetProperties("language." + engine.getLanguageName() + ".", props, engine); - _javaExecutionContext = engine.createExecutionContext(); - LOGGER.info("ScriptEngine: " + engine.getEngineName() + " " + engine.getEngineVersion() + " (" + engine.getLanguageName() + " " + engine.getLanguageVersion() + ")"); - } - - private void maybeSetProperties(String propPrefix, Properties props, IScriptingEngine engine) - { - if (props == null) - { - return; - } - - for (Entry prop : props.entrySet()) - { - String key = (String) prop.getKey(); - String value = (String) prop.getValue(); - - if (key.startsWith(propPrefix)) - { - key = key.substring(propPrefix.length()); - if (value.startsWith("%") && value.endsWith("%")) - { - value = System.getProperty(value.substring(1, value.length() - 1)); - } - - engine.setProperty(key, value); - } - } - } - - public void executeScriptList() throws Exception - { - if (Config.ALT_DEV_NO_QUESTS) - { - return; - } - - final List files = new ArrayList<>(); - processDirectory(SCRIPT_FOLDER.toFile(), files); - - final Map invokationErrors = _javaExecutionContext.executeScripts(files); - for (Entry entry : invokationErrors.entrySet()) - { - LOGGER.log(Level.WARNING, "ScriptEngine: " + entry.getKey() + " failed execution!", entry.getValue()); - } - } - private void processDirectory(File dir, List files) { for (File file : dir.listFiles()) @@ -230,15 +154,12 @@ public final class ScriptEngineManager implements IXmlReader public void executeScript(Path sourceFile) throws Exception { - Objects.requireNonNull(sourceFile); - if (!sourceFile.isAbsolute()) { sourceFile = SCRIPT_FOLDER.resolve(sourceFile); } sourceFile = sourceFile.toAbsolutePath(); - Objects.requireNonNull(sourceFile, "ScriptFile: " + sourceFile + " does not have an extension to determine the script engine!"); final Entry error = _javaExecutionContext.executeScript(sourceFile); if (error != null) @@ -247,6 +168,23 @@ public final class ScriptEngineManager implements IXmlReader } } + public void executeScriptList() throws Exception + { + if (Config.ALT_DEV_NO_QUESTS) + { + return; + } + + final List files = new ArrayList<>(); + processDirectory(SCRIPT_FOLDER.toFile(), files); + + final Map invokationErrors = _javaExecutionContext.executeScripts(files); + for (Entry entry : invokationErrors.entrySet()) + { + LOGGER.log(Level.WARNING, "ScriptEngine: " + entry.getKey() + " failed execution!", entry.getValue()); + } + } + public Path getCurrentLoadingScript() { return _javaExecutionContext.getCurrentExecutingScript(); @@ -254,11 +192,11 @@ public final class ScriptEngineManager implements IXmlReader public static ScriptEngineManager getInstance() { - return SingletonHolder._instance; + return SingletonHolder.INSTANCE; } private static class SingletonHolder { - protected static final ScriptEngineManager _instance = new ScriptEngineManager(); + protected static final ScriptEngineManager INSTANCE = new ScriptEngineManager(); } } \ No newline at end of file diff --git a/L2J_Mobius_Classic_2.2_Antharas/java/com/l2jmobius/gameserver/scripting/java/JavaCompilerException.java b/L2J_Mobius_Classic_2.2_Antharas/java/com/l2jmobius/gameserver/scripting/java/JavaCompilerException.java deleted file mode 100644 index 241ffe91bd..0000000000 --- a/L2J_Mobius_Classic_2.2_Antharas/java/com/l2jmobius/gameserver/scripting/java/JavaCompilerException.java +++ /dev/null @@ -1,28 +0,0 @@ -/* - * This file is part of the L2J Mobius project. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * 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 - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package com.l2jmobius.gameserver.scripting.java; - -/** - * @author HorridoJoho - */ -public final class JavaCompilerException extends RuntimeException -{ - public JavaCompilerException(String diagnostics) - { - super(diagnostics); - } -} diff --git a/L2J_Mobius_Classic_2.2_Antharas/java/com/l2jmobius/gameserver/scripting/java/JavaExecutionContext.java b/L2J_Mobius_Classic_2.2_Antharas/java/com/l2jmobius/gameserver/scripting/java/JavaExecutionContext.java index 70d4da0893..1eddebd693 100644 --- a/L2J_Mobius_Classic_2.2_Antharas/java/com/l2jmobius/gameserver/scripting/java/JavaExecutionContext.java +++ b/L2J_Mobius_Classic_2.2_Antharas/java/com/l2jmobius/gameserver/scripting/java/JavaExecutionContext.java @@ -22,9 +22,9 @@ import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.nio.charset.StandardCharsets; import java.nio.file.Path; +import java.util.ArrayList; import java.util.Arrays; -import java.util.LinkedHashMap; -import java.util.LinkedList; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; @@ -34,22 +34,20 @@ import org.openjavac.tools.Diagnostic; import org.openjavac.tools.DiagnosticCollector; import org.openjavac.tools.JavaFileObject; -import com.l2jmobius.gameserver.scripting.AbstractExecutionContext; import com.l2jmobius.gameserver.scripting.annotations.Disabled; /** * @author HorridoJoho */ -public final class JavaExecutionContext extends AbstractExecutionContext +public final class JavaExecutionContext extends JavaScriptingEngine { private static final Logger LOGGER = Logger.getLogger(JavaExecutionContext.class.getName()); - private static final List _options = new LinkedList<>(); + private static final List _options = new ArrayList<>(); + private static Path _currentExecutingScript; JavaExecutionContext(JavaScriptingEngine engine) { - super(engine); - // Set options. addOptionIfNotNull(_options, getProperty("source"), "-source"); addOptionIfNotNull(_options, getProperty("sourcepath"), "-sourcepath"); @@ -76,7 +74,7 @@ public final class JavaExecutionContext extends AbstractExecutionContext executeScripts(Iterable sourcePaths) throws Exception { final DiagnosticCollector fileManagerDiagnostics = new DiagnosticCollector<>(); final DiagnosticCollector compilationDiagnostics = new DiagnosticCollector<>(); - try (ScriptingFileManager fileManager = new ScriptingFileManager(getScriptingEngine().getCompiler().getStandardFileManager(fileManagerDiagnostics, null, StandardCharsets.UTF_8))) + try (ScriptingFileManager fileManager = new ScriptingFileManager(getCompiler().getStandardFileManager(fileManagerDiagnostics, null, StandardCharsets.UTF_8))) { // We really need an iterable of files or strings. - final List sourcePathStrings = new LinkedList<>(); + final List sourcePathStrings = new ArrayList<>(); for (Path sourcePath : sourcePaths) { sourcePathStrings.add(sourcePath.toString()); @@ -150,7 +147,7 @@ public final class JavaExecutionContext extends AbstractExecutionContext executionFailures = new LinkedHashMap<>(); + final Map executionFailures = new HashMap<>(); final Iterable compiledClasses = fileManager.getCompiledClasses(); for (Path sourcePath : sourcePaths) { @@ -199,7 +196,7 @@ public final class JavaExecutionContext extends AbstractExecutionContext executeScript(Path sourcePath) throws Exception { final Map executionFailures = executeScripts(Arrays.asList(sourcePath)); @@ -254,4 +250,9 @@ public final class JavaExecutionContext extends AbstractExecutionContext _properties = new HashMap<>(); + private final static JavaCompiler _compiler = JavacTool.create(); public JavaScriptingEngine() { - super("Java Engine", "10", "java"); - } - - private void determineCompilerOrThrow() - { - if (_compiler == null) + // Load config. + Properties props = new Properties(); + try (FileInputStream fis = new FileInputStream("config/ScriptEngine.ini")) { - _compiler = JavacTool.create(); + props.load(fis); + } + catch (Exception e) + { + LOGGER.warning("Could not load ScriptEngine.ini: " + e.getMessage()); } - if (_compiler == null) + // Set properties. + for (Entry prop : props.entrySet()) { - throw new IllegalStateException("No JavaCompiler service installed!"); - } - } - - private void ensureCompilerOrThrow() - { - if (_compiler == null) - { - synchronized (this) + String key = (String) prop.getKey(); + String value = (String) prop.getValue(); + + if (value.startsWith("%") && value.endsWith("%")) { - if (_compiler == null) - { - determineCompilerOrThrow(); - } + value = System.getProperty(value.substring(1, value.length() - 1)); } + + _properties.put(key, value); } } - JavaCompiler getCompiler() + public JavaExecutionContext createExecutionContext() { - return _compiler; - } - - @Override - public IExecutionContext createExecutionContext() - { - ensureCompilerOrThrow(); return new JavaExecutionContext(this); } - @Override - public String getLanguageName() + public final String getProperty(String key) { - return "Java"; + return _properties.get(key); } - @Override - public String getLanguageVersion() + public JavaCompiler getCompiler() { - ensureCompilerOrThrow(); - return Arrays.deepToString(_compiler.getSourceVersions().toArray(new SourceVersion[0])).replace("RELEASE_", ""); + return _compiler; } } \ No newline at end of file diff --git a/L2J_Mobius_Classic_2.2_Antharas/java/com/l2jmobius/gameserver/scripting/java/ScriptingOutputFileObject.java b/L2J_Mobius_Classic_2.2_Antharas/java/com/l2jmobius/gameserver/scripting/java/ScriptingOutputFileObject.java index 439cf5d260..9ab668887e 100644 --- a/L2J_Mobius_Classic_2.2_Antharas/java/com/l2jmobius/gameserver/scripting/java/ScriptingOutputFileObject.java +++ b/L2J_Mobius_Classic_2.2_Antharas/java/com/l2jmobius/gameserver/scripting/java/ScriptingOutputFileObject.java @@ -144,5 +144,4 @@ final class ScriptingOutputFileObject implements JavaFileObject { return null; } - } diff --git a/L2J_Mobius_Classic_2.3_SevenSigns/dist/game/config/ScriptEngine.ini b/L2J_Mobius_Classic_2.3_SevenSigns/dist/game/config/ScriptEngine.ini index 2d1e972e72..5004dca9fd 100644 --- a/L2J_Mobius_Classic_2.3_SevenSigns/dist/game/config/ScriptEngine.ini +++ b/L2J_Mobius_Classic_2.3_SevenSigns/dist/game/config/ScriptEngine.ini @@ -1,23 +1,23 @@ -############################################################################### -# Properties retrived with System.getProperty(String) can be used as values +# --------------------------------------------------------------------------- +# Script Engine Settings +# --------------------------------------------------------------------------- +# Properties retrieved with System.getProperty(String) can be used as values # by enclosing the property name with %. Eg.: %java.class.path% -############################################################################### - # The parent class loader for isolated script class loaders. # When this property is not specified, has an invalid value or is a class name which could not be found, the System classloader is used. # Values: System, ThreadContext or a fully qualified java class name -language.Java.classloader=System -#language.Java.classloader=ThreadContext +classloader=System +#classloader=ThreadContext -# Source compatibility -language.Java.source=1.8 +# Source compatibility. +source=1.8 # The java sourcepath, when you have a different datapack root, you must change this too. -language.Java.sourcepath=data/scripts +sourcepath=data/scripts -# The java classpath -language.Java.cp=%java.class.path% +# The java classpath. +cp=%java.class.path% -# The debug informations to generate for compiled class files -language.Java.g=source,lines,vars \ No newline at end of file +# The debug informations to generate for compiled class files. +g=source,lines,vars \ No newline at end of file diff --git a/L2J_Mobius_Classic_2.3_SevenSigns/java/com/l2jmobius/gameserver/scripting/AbstractExecutionContext.java b/L2J_Mobius_Classic_2.3_SevenSigns/java/com/l2jmobius/gameserver/scripting/AbstractExecutionContext.java deleted file mode 100644 index 296b54f06b..0000000000 --- a/L2J_Mobius_Classic_2.3_SevenSigns/java/com/l2jmobius/gameserver/scripting/AbstractExecutionContext.java +++ /dev/null @@ -1,75 +0,0 @@ -/* - * This file is part of the L2J Mobius project. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * 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 - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package com.l2jmobius.gameserver.scripting; - -import java.nio.file.Path; -import java.util.HashMap; -import java.util.Map; - -/** - * @author HorridoJoho - * @param - */ -public abstract class AbstractExecutionContext implements IExecutionContext -{ - private final T _engine; - private final Map _properties; - private volatile Path _currentExecutingScipt; - - protected AbstractExecutionContext(T engine) - { - if (engine == null) - { - throw new IllegalArgumentException(); - } - _engine = engine; - _properties = new HashMap<>(); - } - - protected final void setCurrentExecutingScript(Path currentExecutingScript) - { - _currentExecutingScipt = currentExecutingScript; - } - - @Override - public final String setProperty(String key, String value) - { - return _properties.put(key, value); - } - - @Override - public final String getProperty(String key) - { - if (!_properties.containsKey(key)) - { - return _engine.getProperty(key); - } - return _properties.get(key); - } - - @Override - public final Path getCurrentExecutingScript() - { - return _currentExecutingScipt; - } - - @Override - public final T getScriptingEngine() - { - return _engine; - } -} diff --git a/L2J_Mobius_Classic_2.3_SevenSigns/java/com/l2jmobius/gameserver/scripting/AbstractScriptingEngine.java b/L2J_Mobius_Classic_2.3_SevenSigns/java/com/l2jmobius/gameserver/scripting/AbstractScriptingEngine.java deleted file mode 100644 index 90900438ec..0000000000 --- a/L2J_Mobius_Classic_2.3_SevenSigns/java/com/l2jmobius/gameserver/scripting/AbstractScriptingEngine.java +++ /dev/null @@ -1,74 +0,0 @@ -/* - * This file is part of the L2J Mobius project. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * 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 - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package com.l2jmobius.gameserver.scripting; - -import java.util.Arrays; -import java.util.HashMap; -import java.util.Map; - -/** - * @author HorridoJoho - */ -public abstract class AbstractScriptingEngine implements IScriptingEngine -{ - private final String _engineName; - private final String _engineVersion; - private final String[] _commonFileExtensions; - private final Map _properties; - - protected AbstractScriptingEngine(String engineName, String engineVersion, String... commonFileExtensions) - { - if ((engineName == null) || engineName.isEmpty() || (engineVersion == null) || engineVersion.isEmpty() || (commonFileExtensions == null) || (commonFileExtensions.length == 0)) - { - throw new IllegalArgumentException(); - } - _engineName = engineName; - _engineVersion = engineVersion; - _commonFileExtensions = commonFileExtensions; - _properties = new HashMap<>(); - } - - @Override - public final String setProperty(String key, String value) - { - return _properties.put(key, value); - } - - @Override - public final String getProperty(String key) - { - return _properties.get(key); - } - - @Override - public final String getEngineName() - { - return _engineName; - } - - @Override - public final String getEngineVersion() - { - return _engineVersion; - } - - @Override - public final String[] getCommonFileExtensions() - { - return Arrays.copyOf(_commonFileExtensions, _commonFileExtensions.length); - } -} diff --git a/L2J_Mobius_Classic_2.3_SevenSigns/java/com/l2jmobius/gameserver/scripting/IExecutionContext.java b/L2J_Mobius_Classic_2.3_SevenSigns/java/com/l2jmobius/gameserver/scripting/IExecutionContext.java deleted file mode 100644 index eaf4860b8a..0000000000 --- a/L2J_Mobius_Classic_2.3_SevenSigns/java/com/l2jmobius/gameserver/scripting/IExecutionContext.java +++ /dev/null @@ -1,71 +0,0 @@ -/* - * This file is part of the L2J Mobius project. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * 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 - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package com.l2jmobius.gameserver.scripting; - -import java.nio.file.Path; -import java.util.Map; -import java.util.Map.Entry; - -/** - * @author HorridoJoho - */ -public interface IExecutionContext -{ - /** - * Properties set here override the settings from the IScriptEngine
- * this class was created from. - * @param key the key - * @param value the value - * @return the previous value, or null when this key was not present before - */ - String setProperty(String key, String value); - - /** - * Executes all script in the iterable. - * @param sourcePaths the scripts to execute - * @return map of failed executions, Path=source file Throwable=thrown exception - * @throws Exception preparation for script execution failed - */ - Map executeScripts(Iterable sourcePaths) throws Exception; - - /** - * Executes a single file. - * @param sourcePath the script to execute - * @return entry of failed execution, Path=source file Throwable=thrown exception - * @throws Exception preparation for script execution failed - */ - Entry executeScript(Path sourcePath) throws Exception; - - /** - * Method to get the specified property value. - * @param key the key - * @return the value, or null if the key is not present - */ - String getProperty(String key); - - /** - * Method to get the current executing script file. - * @return the currently executing script file, null if non - */ - Path getCurrentExecutingScript(); - - /** - * Method to get the script engine this execution context belongs to. - * @return the script engine this execution context belongs to - */ - IScriptingEngine getScriptingEngine(); -} diff --git a/L2J_Mobius_Classic_2.3_SevenSigns/java/com/l2jmobius/gameserver/scripting/IScriptingEngine.java b/L2J_Mobius_Classic_2.3_SevenSigns/java/com/l2jmobius/gameserver/scripting/IScriptingEngine.java deleted file mode 100644 index 883172cce1..0000000000 --- a/L2J_Mobius_Classic_2.3_SevenSigns/java/com/l2jmobius/gameserver/scripting/IScriptingEngine.java +++ /dev/null @@ -1,75 +0,0 @@ -/* - * This file is part of the L2J Mobius project. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * 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 - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package com.l2jmobius.gameserver.scripting; - -/** - * @author HorridoJoho - */ -public interface IScriptingEngine -{ - /** - * Sets script engine properties. The script values will be available
- * to the the insatnces created {@link IExecutionContext} implementation. - * @param key the key - * @param value the value - * @return the previous value, or null when this key was not present before - */ - String setProperty(String key, String value); - - /** - * Creates an execution context. - * @return the created execution context. - */ - IExecutionContext createExecutionContext(); - - /** - * Method to get the specified property value. - * @param key the key - * @return the value,or null if the key is not present - */ - String getProperty(String key); - - /** - * Method to get the engine name. - * @return the engine name - */ - String getEngineName(); - - /** - * Method to get the engine version. - * @return the engine version - */ - String getEngineVersion(); - - /** - * Method to get the scripting language name. - * @return the scripting engine name - */ - String getLanguageName(); - - /** - * Method to get the the language version. - * @return the language version - */ - String getLanguageVersion(); - - /** - * Method to retrive the commonly used file extensions for the language. - * @return the commonly used file extensions for the language - */ - String[] getCommonFileExtensions(); -} diff --git a/L2J_Mobius_Classic_2.3_SevenSigns/java/com/l2jmobius/gameserver/scripting/ScriptEngineManager.java b/L2J_Mobius_Classic_2.3_SevenSigns/java/com/l2jmobius/gameserver/scripting/ScriptEngineManager.java index 25a46c93b2..7f8d5fbd5b 100644 --- a/L2J_Mobius_Classic_2.3_SevenSigns/java/com/l2jmobius/gameserver/scripting/ScriptEngineManager.java +++ b/L2J_Mobius_Classic_2.3_SevenSigns/java/com/l2jmobius/gameserver/scripting/ScriptEngineManager.java @@ -17,7 +17,6 @@ package com.l2jmobius.gameserver.scripting; import java.io.File; -import java.io.FileInputStream; import java.io.IOException; import java.nio.file.FileVisitResult; import java.nio.file.Files; @@ -31,9 +30,6 @@ import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Map.Entry; -import java.util.Objects; -import java.util.Properties; -import java.util.ServiceLoader; import java.util.logging.Level; import java.util.logging.Logger; @@ -41,15 +37,16 @@ import org.w3c.dom.Document; import com.l2jmobius.Config; import com.l2jmobius.commons.util.IXmlReader; +import com.l2jmobius.gameserver.scripting.java.JavaExecutionContext; import com.l2jmobius.gameserver.scripting.java.JavaScriptingEngine; /** - * Caches script engines and provides functionality for executing and managing scripts. - * @author KenM, HorridoJoho + * @author Mobius */ public final class ScriptEngineManager implements IXmlReader { private static final Logger LOGGER = Logger.getLogger(ScriptEngineManager.class.getName()); + public static final Path SCRIPT_FOLDER = Config.SCRIPT_ROOT.toPath(); public static final Path MASTER_HANDLER_FILE = Paths.get(SCRIPT_FOLDER.toString(), "handlers", "MasterHandler.java"); public static final Path EFFECT_MASTER_HANDLER_FILE = Paths.get(SCRIPT_FOLDER.toString(), "handlers", "EffectMasterHandler.java"); @@ -57,19 +54,11 @@ public final class ScriptEngineManager implements IXmlReader public static final Path CONDITION_HANDLER_FILE = Paths.get(SCRIPT_FOLDER.toString(), "handlers", "ConditionMasterHandler.java"); public static final Path ONE_DAY_REWARD_MASTER_HANDLER = Paths.get(SCRIPT_FOLDER.toString(), "handlers", "DailyMissionMasterHandler.java"); - private IExecutionContext _javaExecutionContext = null; - static final List _exclusions = new ArrayList<>(); + private static final JavaExecutionContext _javaExecutionContext = new JavaScriptingEngine().createExecutionContext(); + protected static final List _exclusions = new ArrayList<>(); protected ScriptEngineManager() { - final Properties props = loadProperties(); - - // Default java engine implementation - registerEngine(new JavaScriptingEngine(), props); - - // Load external script engines - ServiceLoader.load(IScriptingEngine.class).forEach(engine -> registerEngine(engine, props)); - // Load Scripts.xml load(); } @@ -144,71 +133,6 @@ public final class ScriptEngineManager implements IXmlReader } } - private Properties loadProperties() - { - Properties props = null; - try (FileInputStream fis = new FileInputStream("config/ScriptEngine.ini")) - { - props = new Properties(); - props.load(fis); - } - catch (Exception e) - { - props = null; - LOGGER.warning("Couldn't load ScriptEngine.ini: " + e.getMessage()); - } - return props; - } - - private void registerEngine(IScriptingEngine engine, Properties props) - { - maybeSetProperties("language." + engine.getLanguageName() + ".", props, engine); - _javaExecutionContext = engine.createExecutionContext(); - LOGGER.info("ScriptEngine: " + engine.getEngineName() + " " + engine.getEngineVersion() + " (" + engine.getLanguageName() + " " + engine.getLanguageVersion() + ")"); - } - - private void maybeSetProperties(String propPrefix, Properties props, IScriptingEngine engine) - { - if (props == null) - { - return; - } - - for (Entry prop : props.entrySet()) - { - String key = (String) prop.getKey(); - String value = (String) prop.getValue(); - - if (key.startsWith(propPrefix)) - { - key = key.substring(propPrefix.length()); - if (value.startsWith("%") && value.endsWith("%")) - { - value = System.getProperty(value.substring(1, value.length() - 1)); - } - - engine.setProperty(key, value); - } - } - } - - public void executeScriptList() throws Exception - { - if (Config.ALT_DEV_NO_QUESTS) - { - return; - } - - final List files = new ArrayList<>(); - processDirectory(SCRIPT_FOLDER.toFile(), files); - - final Map invokationErrors = _javaExecutionContext.executeScripts(files); - for (Entry entry : invokationErrors.entrySet()) - { - LOGGER.log(Level.WARNING, "ScriptEngine: " + entry.getKey() + " failed execution!", entry.getValue()); - } - } - private void processDirectory(File dir, List files) { for (File file : dir.listFiles()) @@ -230,15 +154,12 @@ public final class ScriptEngineManager implements IXmlReader public void executeScript(Path sourceFile) throws Exception { - Objects.requireNonNull(sourceFile); - if (!sourceFile.isAbsolute()) { sourceFile = SCRIPT_FOLDER.resolve(sourceFile); } sourceFile = sourceFile.toAbsolutePath(); - Objects.requireNonNull(sourceFile, "ScriptFile: " + sourceFile + " does not have an extension to determine the script engine!"); final Entry error = _javaExecutionContext.executeScript(sourceFile); if (error != null) @@ -247,6 +168,23 @@ public final class ScriptEngineManager implements IXmlReader } } + public void executeScriptList() throws Exception + { + if (Config.ALT_DEV_NO_QUESTS) + { + return; + } + + final List files = new ArrayList<>(); + processDirectory(SCRIPT_FOLDER.toFile(), files); + + final Map invokationErrors = _javaExecutionContext.executeScripts(files); + for (Entry entry : invokationErrors.entrySet()) + { + LOGGER.log(Level.WARNING, "ScriptEngine: " + entry.getKey() + " failed execution!", entry.getValue()); + } + } + public Path getCurrentLoadingScript() { return _javaExecutionContext.getCurrentExecutingScript(); @@ -254,11 +192,11 @@ public final class ScriptEngineManager implements IXmlReader public static ScriptEngineManager getInstance() { - return SingletonHolder._instance; + return SingletonHolder.INSTANCE; } private static class SingletonHolder { - protected static final ScriptEngineManager _instance = new ScriptEngineManager(); + protected static final ScriptEngineManager INSTANCE = new ScriptEngineManager(); } } \ No newline at end of file diff --git a/L2J_Mobius_Classic_2.3_SevenSigns/java/com/l2jmobius/gameserver/scripting/java/JavaCompilerException.java b/L2J_Mobius_Classic_2.3_SevenSigns/java/com/l2jmobius/gameserver/scripting/java/JavaCompilerException.java deleted file mode 100644 index 241ffe91bd..0000000000 --- a/L2J_Mobius_Classic_2.3_SevenSigns/java/com/l2jmobius/gameserver/scripting/java/JavaCompilerException.java +++ /dev/null @@ -1,28 +0,0 @@ -/* - * This file is part of the L2J Mobius project. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * 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 - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package com.l2jmobius.gameserver.scripting.java; - -/** - * @author HorridoJoho - */ -public final class JavaCompilerException extends RuntimeException -{ - public JavaCompilerException(String diagnostics) - { - super(diagnostics); - } -} diff --git a/L2J_Mobius_Classic_2.3_SevenSigns/java/com/l2jmobius/gameserver/scripting/java/JavaExecutionContext.java b/L2J_Mobius_Classic_2.3_SevenSigns/java/com/l2jmobius/gameserver/scripting/java/JavaExecutionContext.java index 70d4da0893..1eddebd693 100644 --- a/L2J_Mobius_Classic_2.3_SevenSigns/java/com/l2jmobius/gameserver/scripting/java/JavaExecutionContext.java +++ b/L2J_Mobius_Classic_2.3_SevenSigns/java/com/l2jmobius/gameserver/scripting/java/JavaExecutionContext.java @@ -22,9 +22,9 @@ import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.nio.charset.StandardCharsets; import java.nio.file.Path; +import java.util.ArrayList; import java.util.Arrays; -import java.util.LinkedHashMap; -import java.util.LinkedList; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; @@ -34,22 +34,20 @@ import org.openjavac.tools.Diagnostic; import org.openjavac.tools.DiagnosticCollector; import org.openjavac.tools.JavaFileObject; -import com.l2jmobius.gameserver.scripting.AbstractExecutionContext; import com.l2jmobius.gameserver.scripting.annotations.Disabled; /** * @author HorridoJoho */ -public final class JavaExecutionContext extends AbstractExecutionContext +public final class JavaExecutionContext extends JavaScriptingEngine { private static final Logger LOGGER = Logger.getLogger(JavaExecutionContext.class.getName()); - private static final List _options = new LinkedList<>(); + private static final List _options = new ArrayList<>(); + private static Path _currentExecutingScript; JavaExecutionContext(JavaScriptingEngine engine) { - super(engine); - // Set options. addOptionIfNotNull(_options, getProperty("source"), "-source"); addOptionIfNotNull(_options, getProperty("sourcepath"), "-sourcepath"); @@ -76,7 +74,7 @@ public final class JavaExecutionContext extends AbstractExecutionContext executeScripts(Iterable sourcePaths) throws Exception { final DiagnosticCollector fileManagerDiagnostics = new DiagnosticCollector<>(); final DiagnosticCollector compilationDiagnostics = new DiagnosticCollector<>(); - try (ScriptingFileManager fileManager = new ScriptingFileManager(getScriptingEngine().getCompiler().getStandardFileManager(fileManagerDiagnostics, null, StandardCharsets.UTF_8))) + try (ScriptingFileManager fileManager = new ScriptingFileManager(getCompiler().getStandardFileManager(fileManagerDiagnostics, null, StandardCharsets.UTF_8))) { // We really need an iterable of files or strings. - final List sourcePathStrings = new LinkedList<>(); + final List sourcePathStrings = new ArrayList<>(); for (Path sourcePath : sourcePaths) { sourcePathStrings.add(sourcePath.toString()); @@ -150,7 +147,7 @@ public final class JavaExecutionContext extends AbstractExecutionContext executionFailures = new LinkedHashMap<>(); + final Map executionFailures = new HashMap<>(); final Iterable compiledClasses = fileManager.getCompiledClasses(); for (Path sourcePath : sourcePaths) { @@ -199,7 +196,7 @@ public final class JavaExecutionContext extends AbstractExecutionContext executeScript(Path sourcePath) throws Exception { final Map executionFailures = executeScripts(Arrays.asList(sourcePath)); @@ -254,4 +250,9 @@ public final class JavaExecutionContext extends AbstractExecutionContext _properties = new HashMap<>(); + private final static JavaCompiler _compiler = JavacTool.create(); public JavaScriptingEngine() { - super("Java Engine", "10", "java"); - } - - private void determineCompilerOrThrow() - { - if (_compiler == null) + // Load config. + Properties props = new Properties(); + try (FileInputStream fis = new FileInputStream("config/ScriptEngine.ini")) { - _compiler = JavacTool.create(); + props.load(fis); + } + catch (Exception e) + { + LOGGER.warning("Could not load ScriptEngine.ini: " + e.getMessage()); } - if (_compiler == null) + // Set properties. + for (Entry prop : props.entrySet()) { - throw new IllegalStateException("No JavaCompiler service installed!"); - } - } - - private void ensureCompilerOrThrow() - { - if (_compiler == null) - { - synchronized (this) + String key = (String) prop.getKey(); + String value = (String) prop.getValue(); + + if (value.startsWith("%") && value.endsWith("%")) { - if (_compiler == null) - { - determineCompilerOrThrow(); - } + value = System.getProperty(value.substring(1, value.length() - 1)); } + + _properties.put(key, value); } } - JavaCompiler getCompiler() + public JavaExecutionContext createExecutionContext() { - return _compiler; - } - - @Override - public IExecutionContext createExecutionContext() - { - ensureCompilerOrThrow(); return new JavaExecutionContext(this); } - @Override - public String getLanguageName() + public final String getProperty(String key) { - return "Java"; + return _properties.get(key); } - @Override - public String getLanguageVersion() + public JavaCompiler getCompiler() { - ensureCompilerOrThrow(); - return Arrays.deepToString(_compiler.getSourceVersions().toArray(new SourceVersion[0])).replace("RELEASE_", ""); + return _compiler; } } \ No newline at end of file diff --git a/L2J_Mobius_Classic_2.3_SevenSigns/java/com/l2jmobius/gameserver/scripting/java/ScriptingOutputFileObject.java b/L2J_Mobius_Classic_2.3_SevenSigns/java/com/l2jmobius/gameserver/scripting/java/ScriptingOutputFileObject.java index 439cf5d260..9ab668887e 100644 --- a/L2J_Mobius_Classic_2.3_SevenSigns/java/com/l2jmobius/gameserver/scripting/java/ScriptingOutputFileObject.java +++ b/L2J_Mobius_Classic_2.3_SevenSigns/java/com/l2jmobius/gameserver/scripting/java/ScriptingOutputFileObject.java @@ -144,5 +144,4 @@ final class ScriptingOutputFileObject implements JavaFileObject { return null; } - }