Code improvements.
This commit is contained in:
@ -48,8 +48,7 @@ public final class ComplexBlock implements IBlock
|
||||
|
||||
private int _getCellHeight(int geoX, int geoY)
|
||||
{
|
||||
final short height = (short) (_getCellData(geoX, geoY) & 0x0FFF0);
|
||||
return height >> 1;
|
||||
return (short) (_getCellData(geoX, geoY) & 0x0FFF0) >> 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -52,8 +52,7 @@ public class JavaCompiler
|
||||
|
||||
public Map<String, byte[]> compile(String source, String fileName)
|
||||
{
|
||||
final PrintWriter err = new PrintWriter(System.err);
|
||||
return compile(source, fileName, err, null, null);
|
||||
return compile(source, fileName, new PrintWriter(System.err), null, null);
|
||||
}
|
||||
|
||||
public Map<String, byte[]> compile(String fileName, String source, Writer err)
|
||||
|
@ -89,8 +89,7 @@ public class JavaScriptEngine extends AbstractScriptEngine implements Compilable
|
||||
{
|
||||
final Map<String, byte[]> classBytesCopy = new HashMap<>();
|
||||
classBytesCopy.putAll(_classBytes);
|
||||
final MemoryClassLoader loader = new MemoryClassLoader(classBytesCopy, _classPath, JavaScriptEngine.getParentLoader(ctx));
|
||||
_class = JavaScriptEngine.parseMain(loader, ctx);
|
||||
_class = JavaScriptEngine.parseMain(new MemoryClassLoader(classBytesCopy, _classPath, JavaScriptEngine.getParentLoader(ctx)), ctx);
|
||||
}
|
||||
return JavaScriptEngine.evalClass(_class, ctx);
|
||||
}
|
||||
@ -111,8 +110,7 @@ public class JavaScriptEngine extends AbstractScriptEngine implements Compilable
|
||||
@Override
|
||||
public Object eval(String str, ScriptContext ctx) throws ScriptException
|
||||
{
|
||||
final Class<?> clazz = parse(str, ctx);
|
||||
return evalClass(clazz, ctx);
|
||||
return evalClass(parse(str, ctx), ctx);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -153,26 +151,17 @@ public class JavaScriptEngine extends AbstractScriptEngine implements Compilable
|
||||
final String sourcePath = getSourcePath(ctx);
|
||||
final String classPath = getClassPath(ctx);
|
||||
|
||||
Writer err = ctx.getErrorWriter();
|
||||
if (err == null)
|
||||
{
|
||||
err = new StringWriter();
|
||||
}
|
||||
final Writer err = ctx.getErrorWriter() == null ? new StringWriter() : ctx.getErrorWriter();
|
||||
|
||||
final Map<String, byte[]> classBytes = compiler.compile(fileName, str, err, sourcePath, classPath);
|
||||
|
||||
if (classBytes == null)
|
||||
{
|
||||
if (err instanceof StringWriter)
|
||||
{
|
||||
throw new ScriptException(((StringWriter) err).toString());
|
||||
}
|
||||
throw new ScriptException("compilation failed");
|
||||
throw err instanceof StringWriter ? new ScriptException(((StringWriter) err).toString()) : new ScriptException("compilation failed");
|
||||
}
|
||||
|
||||
// create a ClassLoader to load classes from MemoryJavaFileManager
|
||||
final MemoryClassLoader loader = new MemoryClassLoader(classBytes, classPath, getParentLoader(ctx));
|
||||
return parseMain(loader, ctx);
|
||||
return parseMain((new MemoryClassLoader(classBytes, classPath, getParentLoader(ctx))), ctx);
|
||||
}
|
||||
|
||||
protected static Class<?> parseMain(MemoryClassLoader loader, ScriptContext ctx) throws ScriptException
|
||||
@ -218,11 +207,7 @@ public class JavaScriptEngine extends AbstractScriptEngine implements Compilable
|
||||
// if class with "main" method, then
|
||||
// return first class
|
||||
final Iterator<Class<?>> itr = classes.iterator();
|
||||
if (itr.hasNext())
|
||||
{
|
||||
return itr.next();
|
||||
}
|
||||
return null;
|
||||
return itr.hasNext() ? itr.next() : null;
|
||||
}
|
||||
|
||||
private JavaCompiledScript compile(String str, ScriptContext ctx) throws ScriptException
|
||||
@ -231,20 +216,11 @@ public class JavaScriptEngine extends AbstractScriptEngine implements Compilable
|
||||
final String sourcePath = getSourcePath(ctx);
|
||||
final String classPath = getClassPath(ctx);
|
||||
|
||||
Writer err = ctx.getErrorWriter();
|
||||
if (err == null)
|
||||
{
|
||||
err = new StringWriter();
|
||||
}
|
||||
|
||||
final Writer err = ctx.getErrorWriter() == null ? new StringWriter() : ctx.getErrorWriter();
|
||||
final Map<String, byte[]> classBytes = compiler.compile(fileName, str, err, sourcePath, classPath);
|
||||
if (classBytes == null)
|
||||
{
|
||||
if (err instanceof StringWriter)
|
||||
{
|
||||
throw new ScriptException(((StringWriter) err).toString());
|
||||
}
|
||||
throw new ScriptException("compilation failed");
|
||||
throw err instanceof StringWriter ? new ScriptException(((StringWriter) err).toString()) : new ScriptException("compilation failed");
|
||||
}
|
||||
|
||||
return new JavaCompiledScript(this, classBytes, classPath);
|
||||
@ -255,14 +231,9 @@ public class JavaScriptEngine extends AbstractScriptEngine implements Compilable
|
||||
// find a public class with public static main method
|
||||
for (Class<?> clazz : classes)
|
||||
{
|
||||
final int modifiers = clazz.getModifiers();
|
||||
if (Modifier.isPublic(modifiers))
|
||||
if (Modifier.isPublic(clazz.getModifiers()) && (findMainMethod(clazz) != null))
|
||||
{
|
||||
final Method mainMethod = findMainMethod(clazz);
|
||||
if (mainMethod != null)
|
||||
{
|
||||
return clazz;
|
||||
}
|
||||
return clazz;
|
||||
}
|
||||
}
|
||||
|
||||
@ -270,8 +241,7 @@ public class JavaScriptEngine extends AbstractScriptEngine implements Compilable
|
||||
// has public static main method
|
||||
for (Class<?> clazz : classes)
|
||||
{
|
||||
final Method mainMethod = findMainMethod(clazz);
|
||||
if (mainMethod != null)
|
||||
if (findMainMethod(clazz) != null)
|
||||
{
|
||||
return clazz;
|
||||
}
|
||||
@ -326,11 +296,7 @@ public class JavaScriptEngine extends AbstractScriptEngine implements Compilable
|
||||
private static String getFileName(ScriptContext ctx)
|
||||
{
|
||||
final int scope = ctx.getAttributesScope("javax.script.filename");
|
||||
if (scope != -1)
|
||||
{
|
||||
return ctx.getAttribute("javax.script.filename", scope).toString();
|
||||
}
|
||||
return "$unnamed.java";
|
||||
return scope != -1 ? ctx.getAttribute("javax.script.filename", scope).toString() : "$unnamed.java";
|
||||
}
|
||||
|
||||
// for certain variables, we look for System properties. This is
|
||||
@ -359,14 +325,7 @@ public class JavaScriptEngine extends AbstractScriptEngine implements Compilable
|
||||
|
||||
private static String getSourcePath(ScriptContext ctx)
|
||||
{
|
||||
final int scope = ctx.getAttributesScope(SOURCEPATH);
|
||||
if (scope != -1)
|
||||
{
|
||||
return ctx.getAttribute(SOURCEPATH).toString();
|
||||
}
|
||||
|
||||
// look for "com.sun.script.java.sourcepath"
|
||||
return System.getProperty(SYSPROP_PREFIX + SOURCEPATH);
|
||||
return ctx.getAttributesScope(SOURCEPATH) != -1 ? ctx.getAttribute(SOURCEPATH).toString() : System.getProperty(SYSPROP_PREFIX + SOURCEPATH);
|
||||
}
|
||||
|
||||
private static final String CLASSPATH = "classpath";
|
||||
@ -392,14 +351,7 @@ public class JavaScriptEngine extends AbstractScriptEngine implements Compilable
|
||||
|
||||
private static String getMainClassName(ScriptContext ctx)
|
||||
{
|
||||
final int scope = ctx.getAttributesScope(MAINCLASS);
|
||||
if (scope != -1)
|
||||
{
|
||||
return ctx.getAttribute(MAINCLASS).toString();
|
||||
}
|
||||
|
||||
// look for "com.sun.script.java.mainClass"
|
||||
return System.getProperty("com.sun.script.java.mainClass");
|
||||
return ctx.getAttributesScope(MAINCLASS) != -1 ? ctx.getAttribute(MAINCLASS).toString() : System.getProperty("com.sun.script.java.mainClass");
|
||||
}
|
||||
|
||||
private static final String PARENTLOADER = "parentLoader";
|
||||
@ -456,13 +408,10 @@ public class JavaScriptEngine extends AbstractScriptEngine implements Compilable
|
||||
mainMethod.setAccessible(true);
|
||||
}
|
||||
|
||||
// get "command line" args for the main method
|
||||
final String args[] = getArguments(ctx);
|
||||
|
||||
// call main method
|
||||
mainMethod.invoke(null, new Object[]
|
||||
{
|
||||
args
|
||||
getArguments(ctx)
|
||||
});
|
||||
}
|
||||
|
||||
@ -495,5 +444,4 @@ public class JavaScriptEngine extends AbstractScriptEngine implements Compilable
|
||||
}
|
||||
return buf.toString();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -114,8 +114,7 @@ public final class MemoryJavaFileManager extends EclipseFileManager
|
||||
public void close() throws IOException
|
||||
{
|
||||
out.close();
|
||||
final ByteArrayOutputStream bos = (ByteArrayOutputStream) out;
|
||||
classBytes.put(name, bos.toByteArray());
|
||||
classBytes.put(name, ((ByteArrayOutputStream) out).toByteArray());
|
||||
}
|
||||
};
|
||||
}
|
||||
|
@ -161,23 +161,20 @@ public class MMOConnection<T extends MMOClient<?>>
|
||||
|
||||
final int remaining = temp.remaining();
|
||||
_primaryWriteBuffer.flip();
|
||||
final int limit = _primaryWriteBuffer.limit();
|
||||
|
||||
if (remaining >= _primaryWriteBuffer.remaining())
|
||||
{
|
||||
temp.put(_primaryWriteBuffer);
|
||||
_selectorThread.recycleBuffer(_primaryWriteBuffer);
|
||||
_primaryWriteBuffer = temp;
|
||||
}
|
||||
else
|
||||
{
|
||||
_primaryWriteBuffer.limit(remaining);
|
||||
temp.put(_primaryWriteBuffer);
|
||||
_primaryWriteBuffer.limit(limit);
|
||||
_primaryWriteBuffer.limit(_primaryWriteBuffer.limit());
|
||||
_primaryWriteBuffer.compact();
|
||||
_secondaryWriteBuffer = _primaryWriteBuffer;
|
||||
_primaryWriteBuffer = temp;
|
||||
}
|
||||
_primaryWriteBuffer = temp;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -285,79 +285,80 @@ public final class SelectorThread<T extends MMOClient<?>>extends Thread
|
||||
|
||||
private final void readPacket(SelectionKey key, MMOConnection<T> con)
|
||||
{
|
||||
if (!con.isClosed())
|
||||
if (con.isClosed())
|
||||
{
|
||||
ByteBuffer buf;
|
||||
if ((buf = con.getReadBuffer()) == null)
|
||||
{
|
||||
buf = READ_BUFFER;
|
||||
}
|
||||
return;
|
||||
}
|
||||
ByteBuffer buf;
|
||||
if ((buf = con.getReadBuffer()) == null)
|
||||
{
|
||||
buf = READ_BUFFER;
|
||||
}
|
||||
|
||||
// if we try to to do a read with no space in the buffer it will read 0 bytes going into infinite loop
|
||||
if (buf.position() == buf.limit())
|
||||
{
|
||||
System.exit(0);
|
||||
}
|
||||
|
||||
int result = -2;
|
||||
|
||||
try
|
||||
{
|
||||
result = con.read(buf);
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
// error handling goes bellow
|
||||
}
|
||||
|
||||
if (result > 0)
|
||||
{
|
||||
buf.flip();
|
||||
|
||||
// if we try to to do a read with no space in the buffer it will read 0 bytes going into infinite loop
|
||||
if (buf.position() == buf.limit())
|
||||
{
|
||||
System.exit(0);
|
||||
}
|
||||
final T client = con.getClient();
|
||||
|
||||
int result = -2;
|
||||
|
||||
try
|
||||
for (int i = 0; i < MAX_READ_PER_PASS; i++)
|
||||
{
|
||||
result = con.read(buf);
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
// error handling goes bellow
|
||||
}
|
||||
|
||||
if (result > 0)
|
||||
{
|
||||
buf.flip();
|
||||
|
||||
final T client = con.getClient();
|
||||
|
||||
for (int i = 0; i < MAX_READ_PER_PASS; i++)
|
||||
if (!tryReadPacket(key, client, buf, con))
|
||||
{
|
||||
if (!tryReadPacket(key, client, buf, con))
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// only reachable if MAX_READ_PER_PASS has been reached
|
||||
// check if there are some more bytes in buffer
|
||||
// and allocate/compact to prevent content lose.
|
||||
if (buf.remaining() > 0)
|
||||
{
|
||||
// did we use the READ_BUFFER ?
|
||||
if (buf == READ_BUFFER)
|
||||
{
|
||||
// move the pending byte to the connections READ_BUFFER
|
||||
allocateReadBuffer(con);
|
||||
}
|
||||
else
|
||||
{
|
||||
// move the first byte to the beginning :)
|
||||
buf.compact();
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
else
|
||||
|
||||
// only reachable if MAX_READ_PER_PASS has been reached
|
||||
// check if there are some more bytes in buffer
|
||||
// and allocate/compact to prevent content lose.
|
||||
if (buf.remaining() > 0)
|
||||
{
|
||||
switch (result)
|
||||
// did we use the READ_BUFFER ?
|
||||
if (buf == READ_BUFFER)
|
||||
{
|
||||
case 0:
|
||||
case -1:
|
||||
{
|
||||
closeConnectionImpl(key, con);
|
||||
break;
|
||||
}
|
||||
case -2:
|
||||
{
|
||||
con.getClient().onForcedDisconnection();
|
||||
closeConnectionImpl(key, con);
|
||||
break;
|
||||
}
|
||||
// move the pending byte to the connections READ_BUFFER
|
||||
allocateReadBuffer(con);
|
||||
}
|
||||
else
|
||||
{
|
||||
// move the first byte to the beginning :)
|
||||
buf.compact();
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
switch (result)
|
||||
{
|
||||
case 0:
|
||||
case -1:
|
||||
{
|
||||
closeConnectionImpl(key, con);
|
||||
break;
|
||||
}
|
||||
case -2:
|
||||
{
|
||||
con.getClient().onForcedDisconnection();
|
||||
closeConnectionImpl(key, con);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -406,38 +407,35 @@ public final class SelectorThread<T extends MMOClient<?>>extends Thread
|
||||
buf.position(pos + dataPending);
|
||||
}
|
||||
|
||||
// if we are done with this buffer
|
||||
if (!buf.hasRemaining())
|
||||
if (buf.hasRemaining())
|
||||
{
|
||||
if (buf != READ_BUFFER)
|
||||
{
|
||||
con.setReadBuffer(null);
|
||||
recycleBuffer(buf);
|
||||
}
|
||||
else
|
||||
{
|
||||
READ_BUFFER.clear();
|
||||
}
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
return true;
|
||||
if (buf != READ_BUFFER)
|
||||
{
|
||||
con.setReadBuffer(null);
|
||||
recycleBuffer(buf);
|
||||
}
|
||||
else
|
||||
{
|
||||
READ_BUFFER.clear();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// we don`t have enough bytes for the dataPacket so we need
|
||||
// to read
|
||||
// we don`t have enough bytes for the dataPacket so we need to read
|
||||
key.interestOps(key.interestOps() | SelectionKey.OP_READ);
|
||||
|
||||
// move it`s position
|
||||
buf.position(buf.position() - HEADER_SIZE);
|
||||
// did we use the READ_BUFFER ?
|
||||
if (buf == READ_BUFFER)
|
||||
{
|
||||
// move it`s position
|
||||
buf.position(buf.position() - HEADER_SIZE);
|
||||
// move the pending byte to the connections READ_BUFFER
|
||||
allocateReadBuffer(con);
|
||||
}
|
||||
else
|
||||
{
|
||||
buf.position(buf.position() - HEADER_SIZE);
|
||||
buf.compact();
|
||||
}
|
||||
return false;
|
||||
@ -453,31 +451,31 @@ public final class SelectorThread<T extends MMOClient<?>>extends Thread
|
||||
|
||||
private final void parseClientPacket(int pos, ByteBuffer buf, int dataSize, T client)
|
||||
{
|
||||
final boolean ret = client.decrypt(buf, dataSize);
|
||||
|
||||
if (ret && buf.hasRemaining())
|
||||
if (!client.decrypt(buf, dataSize) || !buf.hasRemaining())
|
||||
{
|
||||
// apply limit
|
||||
final int limit = buf.limit();
|
||||
buf.limit(pos + dataSize);
|
||||
final ReceivablePacket<T> cp = _packetHandler.handlePacket(buf, client);
|
||||
|
||||
if (cp != null)
|
||||
{
|
||||
cp._buf = buf;
|
||||
cp._sbuf = STRING_BUFFER;
|
||||
cp._client = client;
|
||||
|
||||
if (cp.read())
|
||||
{
|
||||
_executor.execute(cp);
|
||||
}
|
||||
|
||||
cp._buf = null;
|
||||
cp._sbuf = null;
|
||||
}
|
||||
buf.limit(limit);
|
||||
return;
|
||||
}
|
||||
|
||||
// apply limit
|
||||
final int limit = buf.limit();
|
||||
buf.limit(pos + dataSize);
|
||||
final ReceivablePacket<T> cp = _packetHandler.handlePacket(buf, client);
|
||||
|
||||
if (cp != null)
|
||||
{
|
||||
cp._buf = buf;
|
||||
cp._sbuf = STRING_BUFFER;
|
||||
cp._client = client;
|
||||
|
||||
if (cp.read())
|
||||
{
|
||||
_executor.execute(cp);
|
||||
}
|
||||
|
||||
cp._buf = null;
|
||||
cp._sbuf = null;
|
||||
}
|
||||
buf.limit(limit);
|
||||
}
|
||||
|
||||
private final void writeClosePacket(MMOConnection<T> con)
|
||||
@ -605,15 +603,12 @@ public final class SelectorThread<T extends MMOClient<?>>extends Thread
|
||||
|
||||
WRITE_BUFFER.flip();
|
||||
|
||||
if (DIRECT_WRITE_BUFFER.remaining() >= WRITE_BUFFER.limit())
|
||||
{
|
||||
DIRECT_WRITE_BUFFER.put(WRITE_BUFFER);
|
||||
}
|
||||
else
|
||||
if (DIRECT_WRITE_BUFFER.remaining() < WRITE_BUFFER.limit())
|
||||
{
|
||||
con.createWriteBuffer(WRITE_BUFFER);
|
||||
break;
|
||||
}
|
||||
DIRECT_WRITE_BUFFER.put(WRITE_BUFFER);
|
||||
}
|
||||
}
|
||||
return hasPending;
|
||||
|
Reference in New Issue
Block a user