Merged with released L2J-Unity files.

This commit is contained in:
mobiusdev
2016-06-12 01:34:09 +00:00
parent e003e87887
commit 635557f5da
18352 changed files with 3245113 additions and 2892959 deletions

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;
}
}