Renamed trunk folder.

This commit is contained in:
MobiusDev
2016-08-14 11:56:36 +00:00
parent 0bb4e57aad
commit 69cbd04d91
19923 changed files with 0 additions and 0 deletions

View File

@ -0,0 +1,75 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*/
package com.l2jmobius.gameserver.scripting;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;
/**
* @author HorridoJoho
* @param <T>
*/
public abstract class AbstractExecutionContext<T extends IScriptingEngine> implements IExecutionContext
{
private final T _engine;
private final Map<String, String> _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;
}
}

View File

@ -0,0 +1,74 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*/
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<String, String> _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);
}
}

View File

@ -0,0 +1,71 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*/
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<br>
* 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<Path, Throwable> executeScripts(Iterable<Path> 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<Path, Throwable> 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();
}

View File

@ -0,0 +1,75 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*/
package com.l2jmobius.gameserver.scripting;
/**
* @author HorridoJoho
*/
public interface IScriptingEngine
{
/**
* Sets script engine properties. The script values will be available<br>
* 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();
}

View File

@ -0,0 +1,99 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*/
package com.l2jmobius.gameserver.scripting;
import java.nio.file.Path;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Abstract class for classes that are meant to be implemented by scripts.<BR>
* @author KenM
*/
public abstract class ManagedScript
{
private static final Logger LOGGER = Logger.getLogger(ManagedScript.class.getName());
private final Path _scriptFile;
private long _lastLoadTime;
private boolean _isActive;
public ManagedScript()
{
_scriptFile = getScriptPath();
setLastLoadTime(System.currentTimeMillis());
}
public abstract Path getScriptPath();
/**
* Attempts to reload this script and to refresh the necessary bindings with it ScriptControler.<BR>
* Subclasses of this class should override this method to properly refresh their bindings when necessary.
* @return true if and only if the script was reloaded, false otherwise.
*/
public boolean reload()
{
try
{
ScriptEngineManager.getInstance().executeScript(getScriptFile());
return true;
}
catch (Exception e)
{
LOGGER.log(Level.WARNING, "Failed to reload script!", e);
return false;
}
}
public abstract boolean unload();
public void setActive(boolean status)
{
_isActive = status;
}
public boolean isActive()
{
return _isActive;
}
/**
* @return Returns the scriptFile.
*/
public Path getScriptFile()
{
return _scriptFile;
}
/**
* @param lastLoadTime The lastLoadTime to set.
*/
protected void setLastLoadTime(long lastLoadTime)
{
_lastLoadTime = lastLoadTime;
}
/**
* @return Returns the lastLoadTime.
*/
protected long getLastLoadTime()
{
return _lastLoadTime;
}
public abstract String getScriptName();
}

View File

@ -0,0 +1,314 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*/
package com.l2jmobius.gameserver.scripting;
import java.io.File;
import java.io.FileInputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
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;
import com.l2jmobius.Config;
import com.l2jmobius.gameserver.scripting.java.JavaScriptingEngine;
/**
* Caches script engines and provides functionality for executing and managing scripts.
* @author KenM, HorridoJoho
*/
public final class ScriptEngineManager
{
private static final Logger LOGGER = Logger.getLogger(ScriptEngineManager.class.getName());
public static final Path SCRIPT_FOLDER = Paths.get(Config.DATAPACK_ROOT.getAbsolutePath(), "data", "scripts");
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(ScriptEngineManager.SCRIPT_FOLDER.toString(), "handlers", "SkillConditionMasterHandler.java");
public static final Path CONDITION_HANDLER_FILE = Paths.get(ScriptEngineManager.SCRIPT_FOLDER.toString(), "handlers", "ConditionMasterHandler.java");
public static final Path ONE_DAY_REWARD_MASTER_HANDLER = Paths.get(SCRIPT_FOLDER.toString(), "handlers", "DailyMissionMasterHandler.java");
private final Map<String, IExecutionContext> _extEngines = new HashMap<>();
private IExecutionContext _currentExecutionContext = null;
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));
}
private Properties loadProperties()
{
Properties props = null;
try (FileInputStream fis = new FileInputStream("config/ScriptEngines.ini"))
{
props = new Properties();
props.load(fis);
}
catch (Exception e)
{
props = null;
LOGGER.warning("Couldn't load ScriptEngines.properties: " + e.getMessage());
}
return props;
}
private void registerEngine(IScriptingEngine engine, Properties props)
{
maybeSetProperties("language." + engine.getLanguageName() + ".", props, engine);
final IExecutionContext context = engine.createExecutionContext();
for (String commonExtension : engine.getCommonFileExtensions())
{
_extEngines.put(commonExtension, context);
}
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<Object, Object> 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);
}
}
}
private IExecutionContext getEngineByExtension(String ext)
{
return _extEngines.get(ext);
}
private String getFileExtension(Path p)
{
final String name = p.getFileName().toString();
final int lastDotIdx = name.lastIndexOf('.');
if (lastDotIdx == -1)
{
return null;
}
final String extension = name.substring(lastDotIdx + 1);
if (extension.isEmpty())
{
return null;
}
return extension;
}
private void checkExistingFile(String messagePre, Path filePath) throws Exception
{
if (!Files.exists(filePath))
{
throw new Exception(messagePre + ": " + filePath + " does not exists!");
}
else if (!Files.isRegularFile(filePath))
{
throw new Exception(messagePre + ": " + filePath + " is not a file!");
}
}
public void executeMasterHandler() throws Exception
{
executeScript(MASTER_HANDLER_FILE);
}
public void executeEffectMasterHandler() throws Exception
{
executeScript(EFFECT_MASTER_HANDLER_FILE);
}
public void executeSkillConditionMasterHandler() throws Exception
{
executeScript(SKILL_CONDITION_HANDLER_FILE);
}
public void executeConditionMasterHandler() throws Exception
{
executeScript(CONDITION_HANDLER_FILE);
}
public void executeDailyMissionMasterHandler() throws Exception
{
executeScript(ONE_DAY_REWARD_MASTER_HANDLER);
}
public void executeScriptList() throws Exception
{
if (Config.ALT_DEV_NO_QUESTS)
{
return;
}
final Map<IExecutionContext, List<Path>> files = new LinkedHashMap<>();
processDirectory(SCRIPT_FOLDER.toFile(), files);
for (Entry<IExecutionContext, List<Path>> entry : files.entrySet())
{
_currentExecutionContext = entry.getKey();
try
{
final Map<Path, Throwable> invokationErrors = entry.getKey().executeScripts(entry.getValue());
for (Entry<Path, Throwable> entry2 : invokationErrors.entrySet())
{
LOGGER.log(Level.WARNING, "ScriptEngine: " + entry2.getKey() + " failed execution!", entry2.getValue());
}
}
finally
{
_currentExecutionContext = null;
}
}
}
private void processDirectory(File dir, Map<IExecutionContext, List<Path>> files)
{
for (File file : dir.listFiles())
{
if (file.isDirectory())
{
processDirectory(file, files);
}
else
{
processFile(file, files);
}
}
}
private void processFile(File file, Map<IExecutionContext, List<Path>> files)
{
switch (file.getName())
{
case "package-info.java":
case "MasterHandler.java":
case "EffectMasterHandler.java":
case "SkillConditionMasterHandler.java":
case "ConditionMasterHandler.java":
case "DailyMissionMasterHandler.java":
{
return;
}
}
Path sourceFile = file.toPath();
try
{
checkExistingFile("ScriptFile", sourceFile);
}
catch (Exception e)
{
LOGGER.warning(e.getMessage());
return;
}
sourceFile = sourceFile.toAbsolutePath();
final String ext = getFileExtension(sourceFile);
if (ext == null)
{
LOGGER.warning("ScriptFile: " + sourceFile + " does not have an extension to determine the script engine!");
return;
}
final IExecutionContext engine = getEngineByExtension(ext);
if (engine == null)
{
return;
}
files.computeIfAbsent(engine, k -> new LinkedList<>()).add(sourceFile);
}
public void executeScript(Path sourceFile) throws Exception
{
Objects.requireNonNull(sourceFile);
if (!sourceFile.isAbsolute())
{
sourceFile = SCRIPT_FOLDER.resolve(sourceFile);
}
// throws exception if not exists or not file
checkExistingFile("ScriptFile", sourceFile);
sourceFile = sourceFile.toAbsolutePath();
final String ext = getFileExtension(sourceFile);
Objects.requireNonNull(sourceFile, "ScriptFile: " + sourceFile + " does not have an extension to determine the script engine!");
final IExecutionContext engine = getEngineByExtension(ext);
Objects.requireNonNull(engine, "ScriptEngine: No engine registered for extension " + ext + "!");
_currentExecutionContext = engine;
try
{
final Entry<Path, Throwable> error = engine.executeScript(sourceFile);
if (error != null)
{
throw new Exception("ScriptEngine: " + error.getKey() + " failed execution!", error.getValue());
}
}
finally
{
_currentExecutionContext = null;
}
}
public Path getCurrentLoadingScript()
{
return _currentExecutionContext != null ? _currentExecutionContext.getCurrentExecutingScript() : null;
}
public static ScriptEngineManager getInstance()
{
return SingletonHolder._instance;
}
private static class SingletonHolder
{
protected static final ScriptEngineManager _instance = new ScriptEngineManager();
}
}

View File

@ -0,0 +1,32 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*/
package com.l2jmobius.gameserver.scripting.annotations;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* @author UnAfraid
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Disabled
{
}

View File

@ -0,0 +1,28 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*/
package com.l2jmobius.gameserver.scripting.java;
/**
* @author HorridoJoho
*/
public final class JavaCompilerException extends RuntimeException
{
public JavaCompilerException(String diagnostics)
{
super(diagnostics);
}
}

View File

@ -0,0 +1,249 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*/
package com.l2jmobius.gameserver.scripting.java;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.logging.Logger;
import javax.tools.Diagnostic;
import javax.tools.DiagnosticCollector;
import javax.tools.JavaFileObject;
import com.l2jmobius.gameserver.scripting.AbstractExecutionContext;
import com.l2jmobius.gameserver.scripting.annotations.Disabled;
/**
* @author HorridoJoho
*/
public final class JavaExecutionContext extends AbstractExecutionContext<JavaScriptingEngine>
{
private static final Logger LOGGER = Logger.getLogger(JavaExecutionContext.class.getName());
JavaExecutionContext(JavaScriptingEngine engine)
{
super(engine);
}
private boolean addOptionIfNotNull(List<String> list, String nullChecked, String before)
{
if (nullChecked == null)
{
return false;
}
if (before.endsWith(":"))
{
list.add(before + nullChecked);
}
else
{
list.add(before);
list.add(nullChecked);
}
return true;
}
private ClassLoader determineScriptParentClassloader()
{
final String classloader = getProperty("classloader");
if (classloader == null)
{
return ClassLoader.getSystemClassLoader();
}
switch (classloader)
{
case "ThreadContext":
return Thread.currentThread().getContextClassLoader();
case "System":
return ClassLoader.getSystemClassLoader();
default:
try
{
return Class.forName(classloader).getClassLoader();
}
catch (ClassNotFoundException e)
{
return ClassLoader.getSystemClassLoader();
}
}
}
@Override
public Map<Path, Throwable> executeScripts(Iterable<Path> sourcePaths) throws Exception
{
final DiagnosticCollector<JavaFileObject> fileManagerDiagnostics = new DiagnosticCollector<>();
final DiagnosticCollector<JavaFileObject> compilationDiagnostics = new DiagnosticCollector<>();
try (ScriptingFileManager fileManager = new ScriptingFileManager(getScriptingEngine().getCompiler().getStandardFileManager(fileManagerDiagnostics, null, StandardCharsets.UTF_8)))
{
final List<String> options = new LinkedList<>();
addOptionIfNotNull(options, getProperty("source"), "-source");
addOptionIfNotNull(options, getProperty("sourcepath"), "-sourcepath");
if (!addOptionIfNotNull(options, getProperty("cp"), "-cp") && !addOptionIfNotNull(options, getProperty("classpath"), "-classpath"))
{
addOptionIfNotNull(options, System.getProperty("java.class.path"), "-cp");
}
addOptionIfNotNull(options, getProperty("g"), "-g:");
// we always add the target JVM to the current running version
final String targetVersion = System.getProperty("java.specification.version");
if (!targetVersion.contains("."))
{
options.add("-target");
options.add(targetVersion);
}
else
{
final String[] versionSplit = targetVersion.split("\\.");
if (versionSplit.length > 1)
{
options.add("-target");
options.add(versionSplit[0] + '.' + versionSplit[1]);
}
else
{
throw new JavaCompilerException("Could not determine target version!");
}
}
// we really need an iterable of files or strings
final List<String> sourcePathStrings = new LinkedList<>();
for (Path sourcePath : sourcePaths)
{
sourcePathStrings.add(sourcePath.toString());
}
final StringWriter strOut = new StringWriter();
final PrintWriter out = new PrintWriter(strOut);
final boolean compilationSuccess = getScriptingEngine().getCompiler().getTask(out, fileManager, compilationDiagnostics, options, null, fileManager.getJavaFileObjectsFromStrings(sourcePathStrings)).call();
if (!compilationSuccess)
{
out.println();
out.println("----------------");
out.println("File diagnostics");
out.println("----------------");
for (Diagnostic<? extends JavaFileObject> diagnostic : fileManagerDiagnostics.getDiagnostics())
{
out.println("\t" + diagnostic.getKind().toString() + ": " + diagnostic.getSource().getName() + ", Line " + diagnostic.getLineNumber() + ", Column " + diagnostic.getColumnNumber());
out.println("\t\tcode: " + diagnostic.getCode());
out.println("\t\tmessage: " + diagnostic.getMessage(null));
}
out.println();
out.println("-----------------------");
out.println("Compilation diagnostics");
out.println("-----------------------");
for (Diagnostic<? extends JavaFileObject> diagnostic : compilationDiagnostics.getDiagnostics())
{
out.println("\t" + diagnostic.getKind().toString() + ": " + diagnostic.getSource().getName() + ", Line " + diagnostic.getLineNumber() + ", Column " + diagnostic.getColumnNumber());
out.println("\t\tcode: " + diagnostic.getCode());
out.println("\t\tmessage: " + diagnostic.getMessage(null));
}
throw new JavaCompilerException(strOut.toString());
}
final ClassLoader parentClassLoader = determineScriptParentClassloader();
final Map<Path, Throwable> executionFailures = new LinkedHashMap<>();
final Iterable<ScriptingOutputFileObject> compiledClasses = fileManager.getCompiledClasses();
for (Path sourcePath : sourcePaths)
{
boolean found = false;
for (ScriptingOutputFileObject compiledClass : compiledClasses)
{
final Path compiledSourcePath = compiledClass.getSourcePath();
// sourePath can be relative, so we have to use endsWith
if ((compiledSourcePath != null) && (compiledSourcePath.equals(sourcePath) || compiledSourcePath.endsWith(sourcePath)))
{
final String javaName = compiledClass.getJavaName();
if (javaName.indexOf('$') != -1)
{
continue;
}
found = true;
setCurrentExecutingScript(compiledSourcePath);
try
{
final ScriptingClassLoader loader = new ScriptingClassLoader(parentClassLoader, compiledClasses);
final Class<?> javaClass = loader.loadClass(javaName);
Method mainMethod = null;
for (Method m : javaClass.getMethods())
{
if (m.getName().equals("main") && Modifier.isStatic(m.getModifiers()) && (m.getParameterCount() == 1) && (m.getParameterTypes()[0] == String[].class))
{
mainMethod = m;
break;
}
}
if ((mainMethod != null) && !javaClass.isAnnotationPresent(Disabled.class))
{
mainMethod.invoke(null, (Object) new String[]
{
compiledSourcePath.toString()
});
}
}
catch (Exception e)
{
executionFailures.put(compiledSourcePath, e);
}
finally
{
setCurrentExecutingScript(null);
}
break;
}
}
if (!found)
{
LOGGER.severe("Compilation successfull, but class coresponding to " + sourcePath.toString() + " not found!");
}
}
return executionFailures;
}
}
@Override
public Entry<Path, Throwable> executeScript(Path sourcePath) throws Exception
{
final Map<Path, Throwable> executionFailures = executeScripts(Arrays.asList(sourcePath));
if (!executionFailures.isEmpty())
{
return executionFailures.entrySet().iterator().next();
}
return null;
}
}

View File

@ -0,0 +1,138 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*/
package com.l2jmobius.gameserver.scripting.java;
import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.ServiceLoader;
import javax.lang.model.SourceVersion;
import javax.tools.JavaCompiler;
import javax.tools.ToolProvider;
import com.l2jmobius.gameserver.scripting.AbstractScriptingEngine;
import com.l2jmobius.gameserver.scripting.IExecutionContext;
/**
* @author HorridoJoho
*/
public final class JavaScriptingEngine extends AbstractScriptingEngine
{
private volatile JavaCompiler _compiler;
public JavaScriptingEngine()
{
super("L2J Java Engine", "1.0", "java");
}
private void determineCompilerOrThrow()
{
final String preferedCompiler = getProperty("preferedCompiler");
LinkedList<JavaCompiler> allCompilers = null;
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
if (compiler != null)
{
if ((preferedCompiler == null) || compiler.getClass().getName().equals(preferedCompiler))
{
_compiler = compiler;
return;
}
allCompilers = new LinkedList<>();
allCompilers.add(compiler);
}
final ServiceLoader<JavaCompiler> thirdPartyCompilers = ServiceLoader.load(JavaCompiler.class);
Iterator<JavaCompiler> compilersIterator = thirdPartyCompilers.iterator();
while (compilersIterator.hasNext())
{
compiler = compilersIterator.next();
if ((preferedCompiler == null) || compiler.getClass().getName().equals(preferedCompiler))
{
_compiler = compiler;
return;
}
if (allCompilers == null)
{
allCompilers = new LinkedList<>();
}
allCompilers.add(compilersIterator.next());
}
if (allCompilers != null)
{
compilersIterator = allCompilers.iterator();
while (compilersIterator.hasNext())
{
compiler = compilersIterator.next();
if ((preferedCompiler == null) || compiler.getClass().getName().equals(preferedCompiler))
{
break;
}
}
}
if (compiler == null)
{
throw new IllegalStateException("No javax.tools.JavaCompiler service installed!");
}
_compiler = compiler;
}
private void ensureCompilerOrThrow()
{
if (_compiler == null)
{
synchronized (this)
{
if (_compiler == null)
{
determineCompilerOrThrow();
}
}
}
}
JavaCompiler getCompiler()
{
return _compiler;
}
@Override
public IExecutionContext createExecutionContext()
{
ensureCompilerOrThrow();
return new JavaExecutionContext(this);
}
@Override
public String getLanguageName()
{
return "Java";
}
@Override
public String getLanguageVersion()
{
ensureCompilerOrThrow();
return Arrays.deepToString(_compiler.getSourceVersions().toArray(new SourceVersion[0])).replace("RELEASE_", "");
}
}

View File

@ -0,0 +1,55 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*/
package com.l2jmobius.gameserver.scripting.java;
import java.util.logging.Logger;
/**
* @author HorridoJoho
*/
public final class ScriptingClassLoader extends ClassLoader
{
public static final Logger LOGGER = Logger.getLogger(ScriptingClassLoader.class.getName());
private Iterable<ScriptingOutputFileObject> _compiledClasses;
ScriptingClassLoader(ClassLoader parent, Iterable<ScriptingOutputFileObject> compiledClasses)
{
super(parent);
_compiledClasses = compiledClasses;
}
void removeCompiledClasses()
{
_compiledClasses = null;
}
@Override
protected Class<?> findClass(String name) throws ClassNotFoundException
{
for (ScriptingOutputFileObject compiledClass : _compiledClasses)
{
if (compiledClass.getJavaName().equals(name))
{
final byte[] classBytes = compiledClass.getJavaData();
return defineClass(name, classBytes, 0, classBytes.length);
}
}
return super.findClass(name);
}
}

View File

@ -0,0 +1,185 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*/
package com.l2jmobius.gameserver.scripting.java;
import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Set;
import javax.tools.FileObject;
import javax.tools.JavaFileObject;
import javax.tools.JavaFileObject.Kind;
import javax.tools.StandardJavaFileManager;
/**
* @author HorridoJoho
*/
final class ScriptingFileManager implements StandardJavaFileManager
{
private final StandardJavaFileManager _wrapped;
private final LinkedList<ScriptingOutputFileObject> _classOutputs = new LinkedList<>();
public ScriptingFileManager(StandardJavaFileManager wrapped)
{
_wrapped = wrapped;
}
Iterable<ScriptingOutputFileObject> getCompiledClasses()
{
return Collections.unmodifiableCollection(_classOutputs);
}
@Override
public int isSupportedOption(String option)
{
return _wrapped.isSupportedOption(option);
}
@Override
public ClassLoader getClassLoader(Location location)
{
return _wrapped.getClassLoader(location);
}
@Override
public Iterable<JavaFileObject> list(Location location, String packageName, Set<Kind> kinds, boolean recurse) throws IOException
{
return _wrapped.list(location, packageName, kinds, recurse);
}
@Override
public String inferBinaryName(Location location, JavaFileObject file)
{
return _wrapped.inferBinaryName(location, file);
}
@Override
public boolean isSameFile(FileObject a, FileObject b)
{
return _wrapped.isSameFile(a, b);
}
@Override
public boolean handleOption(String current, Iterator<String> remaining)
{
return _wrapped.handleOption(current, remaining);
}
@Override
public boolean hasLocation(Location location)
{
return _wrapped.hasLocation(location);
}
@Override
public JavaFileObject getJavaFileForInput(Location location, String className, Kind kind) throws IOException
{
return _wrapped.getJavaFileForInput(location, className, kind);
}
@Override
public JavaFileObject getJavaFileForOutput(Location location, String className, Kind kind, FileObject sibling) throws IOException
{
if (kind != Kind.CLASS)
{
return _wrapped.getJavaFileForOutput(location, className, kind, sibling);
}
if (className.contains("/"))
{
className = className.replace('/', '.');
}
ScriptingOutputFileObject fileObject;
if (sibling != null)
{
fileObject = new ScriptingOutputFileObject(Paths.get(sibling.getName()), className, className.substring(className.lastIndexOf('.') + 1));
}
else
{
fileObject = new ScriptingOutputFileObject(null, className, className.substring(className.lastIndexOf('.') + 1));
}
_classOutputs.add(fileObject);
return fileObject;
}
@Override
public FileObject getFileForInput(Location location, String packageName, String relativeName) throws IOException
{
return _wrapped.getFileForInput(location, packageName, relativeName);
}
@Override
public FileObject getFileForOutput(Location location, String packageName, String relativeName, FileObject sibling) throws IOException
{
return _wrapped.getFileForOutput(location, packageName, relativeName, sibling);
}
@Override
public void flush() throws IOException
{
_wrapped.flush();
}
@Override
public void close() throws IOException
{
_wrapped.close();
}
@Override
public Iterable<? extends JavaFileObject> getJavaFileObjectsFromFiles(Iterable<? extends File> files)
{
return _wrapped.getJavaFileObjectsFromFiles(files);
}
@Override
public Iterable<? extends JavaFileObject> getJavaFileObjects(File... files)
{
return _wrapped.getJavaFileObjects(files);
}
@Override
public Iterable<? extends JavaFileObject> getJavaFileObjectsFromStrings(Iterable<String> names)
{
return _wrapped.getJavaFileObjectsFromStrings(names);
}
@Override
public Iterable<? extends JavaFileObject> getJavaFileObjects(String... names)
{
return _wrapped.getJavaFileObjects(names);
}
@Override
public void setLocation(Location location, Iterable<? extends File> path) throws IOException
{
_wrapped.setLocation(location, path);
}
@Override
public Iterable<? extends File> getLocation(Location location)
{
return _wrapped.getLocation(location);
}
}

View File

@ -0,0 +1,147 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*/
package com.l2jmobius.gameserver.scripting.java;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;
import java.net.URI;
import java.nio.file.Path;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.NestingKind;
import javax.tools.JavaFileObject;
/**
* @author HorridoJoho
*/
final class ScriptingOutputFileObject implements JavaFileObject
{
private final Path _sourcePath;
private final String _javaName;
private final String _javaSimpleName;
private final ByteArrayOutputStream _out;
public ScriptingOutputFileObject(Path sourcePath, String javaName, String javaSimpleName)
{
_sourcePath = sourcePath;
_javaName = javaName;
_javaSimpleName = javaSimpleName;
_out = new ByteArrayOutputStream();
}
public Path getSourcePath()
{
return _sourcePath;
}
public String getJavaName()
{
return _javaName;
}
public String getJavaSimpleName()
{
return _javaSimpleName;
}
public byte[] getJavaData()
{
return _out.toByteArray();
}
@Override
public URI toUri()
{
return null;
}
@Override
public String getName()
{
return null;
}
@Override
public InputStream openInputStream()
{
return null;
}
@Override
public OutputStream openOutputStream()
{
return _out;
}
@Override
public Reader openReader(boolean ignoreEncodingErrors)
{
return null;
}
@Override
public CharSequence getCharContent(boolean ignoreEncodingErrors)
{
return null;
}
@Override
public Writer openWriter()
{
return null;
}
@Override
public long getLastModified()
{
return 0;
}
@Override
public boolean delete()
{
return false;
}
@Override
public Kind getKind()
{
return Kind.CLASS;
}
@Override
public boolean isNameCompatible(String simpleName, Kind kind)
{
return (kind == Kind.CLASS) && (_javaSimpleName == simpleName);
}
@Override
public NestingKind getNestingKind()
{
return NestingKind.TOP_LEVEL;
}
@Override
public Modifier getAccessLevel()
{
return null;
}
}