L2ClientDat decoder.

This commit is contained in:
MobiusDev
2018-05-13 13:35:03 +00:00
parent 574f600afa
commit 836beafd0f
211 changed files with 26666 additions and 0 deletions

View File

@ -0,0 +1,188 @@
/*
* 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.util;
import java.io.EOFException;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
public class ByteReader
{
private static Charset defaultCharset = Charset.forName("cp1252");
private static Charset utf16leCharset = Charset.forName("utf-16le");
public static char readChar(ByteBuffer buffer)
{
return (char) buffer.get();
}
public static int readUByte(ByteBuffer buffer)
{
return buffer.get() & 255;
}
public static int readInt(ByteBuffer buffer)
{
return Integer.reverseBytes(buffer.getInt());
}
public static int readUInt(ByteBuffer buffer)
{
return ByteReader.readInt(buffer);
}
public static short readShort(ByteBuffer buffer)
{
return Short.reverseBytes(buffer.getShort());
}
public static double readDouble(ByteBuffer buffer)
{
return Double.longBitsToDouble(Long.reverseBytes(buffer.getLong()));
}
public static long readLong(ByteBuffer buffer)
{
return Long.reverseBytes(buffer.getLong());
}
public static float readFloat(ByteBuffer buffer)
{
return Float.intBitsToFloat(Integer.reverseBytes(buffer.getInt()));
}
public static int readCompactInt(ByteBuffer input) throws IOException
{
int output = 0;
boolean signed = false;
for (int i = 0; i < 5; ++i)
{
int x = input.get() & 255;
if (x < 0)
{
throw new EOFException();
}
if (i == 0)
{
if ((x & 128) > 0)
{
signed = true;
}
output |= x & 63;
if ((x & 64) != 0)
{
continue;
}
break;
}
if (i == 4)
{
output |= (x & 31) << 27;
continue;
}
output |= (x & 127) << (6 + ((i - 1) * 7));
if ((x & 128) == 0)
{
break;
}
}
if (signed)
{
output *= -1;
}
return output;
}
public static String readRGB(ByteBuffer buffer)
{
String g;
String b;
String r = Integer.toHexString(buffer.get() & 255).toUpperCase();
if (r.length() < 2)
{
r = "0" + r;
}
if ((g = Integer.toHexString(buffer.get() & 255).toUpperCase()).length() < 2)
{
g = "0" + g;
}
if ((b = Integer.toHexString(buffer.get() & 255).toUpperCase()).length() < 2)
{
b = "0" + b;
}
return r + g + b;
}
public static String readRGBA(ByteBuffer buffer)
{
String a = Integer.toHexString(buffer.get() & 255).toUpperCase();
if (a.length() < 2)
{
a = "0" + a;
}
return a + ByteReader.readRGB(buffer);
}
public static String readUtfString(ByteBuffer buffer, boolean isRaw) throws Exception
{
int size = ByteReader.readInt(buffer);
if (size <= 0)
{
return "";
}
if (size > 1000000)
{
throw new Exception("To much data.");
}
byte[] bytes = new byte[size];
try
{
for (int i = 0; i < size; i += 2)
{
bytes[i + 1] = buffer.get();
bytes[i] = buffer.get();
}
}
catch (Exception e)
{
e.printStackTrace();
}
return ByteReader.checkAndReplaceNewLine(isRaw, new String(new String(bytes, "Unicode").getBytes("UTF-8"), "UTF-8"));
}
public static String readString(ByteBuffer input, boolean isRaw) throws IOException
{
int len = ByteReader.readCompactInt(input);
if (len == 0)
{
return "";
}
byte[] bytes = new byte[len > 0 ? len : -2 * len];
input.get(bytes);
return ByteReader.checkAndReplaceNewLine(isRaw, new String(bytes, 0, bytes.length - (len > 0 ? 1 : 2), len > 0 ? defaultCharset : utf16leCharset).intern());
}
private static String checkAndReplaceNewLine(boolean isRaw, String str)
{
if (!isRaw && str.contains("\r\n"))
{
str = str.replace("\r\n", "\\r\\n");
}
return str;
}
}

View File

@ -0,0 +1,211 @@
/*
* 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.util;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.charset.Charset;
public class ByteWriter
{
private static final ByteOrder BYTE_ORDER = ByteOrder.LITTLE_ENDIAN;
private static Charset defaultCharset = Charset.forName("ascii");
private static Charset utf16leCharset = Charset.forName("utf-16le");
public static Buffer writeChar(byte value)
{
ByteBuffer buffer = ByteBuffer.allocate(1).order(BYTE_ORDER);
buffer.put(value);
return buffer;
}
public static Buffer writeCompactInt(int count)
{
byte[] b = ByteWriter.compactIntToByteArray(count);
ByteBuffer buffer = ByteBuffer.allocate(b.length).order(BYTE_ORDER);
buffer.put(b);
return buffer;
}
public static Buffer writeByte(byte value)
{
ByteBuffer buffer = ByteBuffer.allocate(1).order(BYTE_ORDER);
buffer.put(value);
return buffer;
}
public static Buffer writeUByte(short value)
{
ByteBuffer buffer = ByteBuffer.allocate(1).order(BYTE_ORDER);
buffer.put((byte) value);
return buffer;
}
public static Buffer writeInt(int value)
{
ByteBuffer buffer = ByteBuffer.allocate(4).order(BYTE_ORDER);
buffer.putInt(value);
return buffer;
}
public static Buffer writeUInt(int value)
{
ByteBuffer buffer = ByteBuffer.allocate(4).order(BYTE_ORDER);
buffer.putInt(value);
return buffer;
}
public static Buffer writeShort(short value)
{
ByteBuffer buffer = ByteBuffer.allocate(2).order(BYTE_ORDER);
buffer.putShort(value);
return buffer;
}
public static Buffer writeUShort(int value)
{
ByteBuffer buffer = ByteBuffer.allocate(2).order(BYTE_ORDER);
buffer.put((byte) (value & 255));
buffer.put((byte) ((value & 65280) >> 8));
return buffer;
}
public static Buffer writeRGB(String rgb)
{
ByteBuffer buffer = ByteBuffer.allocate(3).order(BYTE_ORDER);
buffer.put((byte) Integer.parseInt(rgb.substring(0, 2), 16));
buffer.put((byte) Integer.parseInt(rgb.substring(2, 4), 16));
buffer.put((byte) Integer.parseInt(rgb.substring(4, 6), 16));
return buffer;
}
public static Buffer writeRGBA(String rgba)
{
ByteBuffer buffer = ByteBuffer.allocate(4).order(BYTE_ORDER);
buffer.put((byte[]) ByteWriter.writeRGB(rgba.substring(0, 6)).array());
buffer.put((byte) Integer.parseInt(rgba.substring(6, 8), 16));
return buffer;
}
public static Buffer writeUtfString(String str, boolean isRaw)
{
int size = str.length();
if (size <= 0)
{
return ByteBuffer.allocate(4).order(BYTE_ORDER).putInt(0);
}
if (!isRaw)
{
str = ByteWriter.checkAndReplaceNewLine(str);
size = str.length();
}
ByteBuffer buffer = ByteBuffer.allocate((size * 2) + 4).order(BYTE_ORDER);
buffer.putInt(size * 2);
for (int i = 0; i < size; ++i)
{
buffer.putChar(str.charAt(i));
}
return buffer;
}
public static Buffer writeString(String s, boolean isRaw)
{
if ((s == null) || s.isEmpty())
{
return ByteWriter.writeCompactInt(0);
}
if (!isRaw)
{
s = ByteWriter.checkAndReplaceNewLine(s);
}
s = s + '\u0000';
boolean def = defaultCharset.newEncoder().canEncode(s);
byte[] bytes = s.getBytes(def ? defaultCharset : utf16leCharset);
byte[] bSize = ByteWriter.compactIntToByteArray(def ? bytes.length : (-bytes.length) / 2);
ByteBuffer buffer = ByteBuffer.allocate(bytes.length + bSize.length).order(BYTE_ORDER);
buffer.put(bSize);
buffer.put(bytes);
return buffer;
}
public static Buffer writeDouble(double value)
{
ByteBuffer buffer = ByteBuffer.allocate(8).order(BYTE_ORDER);
buffer.putDouble(value);
return buffer;
}
public static Buffer writeFloat(float value)
{
ByteBuffer buffer = ByteBuffer.allocate(4).order(BYTE_ORDER);
buffer.putFloat(value);
return buffer;
}
public static Buffer writeLong(long value)
{
ByteBuffer buffer = ByteBuffer.allocate(8).order(BYTE_ORDER);
buffer.putLong(value);
return buffer;
}
private static byte[] compactIntToByteArray(int v)
{
boolean negative = v < 0;
v = Math.abs(v);
int[] bytes = new int[]
{
v & 63,
(v >> 6) & 127,
(v >> 13) & 127,
(v >> 20) & 127,
(v >> 27) & 127
};
if (negative)
{
int[] arrn = bytes;
arrn[0] = arrn[0] | 128;
}
int size = 5;
for (int i = 4; (i > 0) && (bytes[i] == 0); --i)
{
--size;
}
byte[] res = new byte[size];
for (int i = 0; i < size; ++i)
{
if (i != (size - 1))
{
int[] arrn = bytes;
int n = i;
arrn[n] = arrn[n] | (i == 0 ? 64 : 128);
}
res[i] = (byte) bytes[i];
}
return res;
}
private static String checkAndReplaceNewLine(String str)
{
if (str.contains("\\r\\n"))
{
str = str.replace("\\r\\n", "\r\n");
}
return str;
}
}

View File

@ -0,0 +1,85 @@
/*
* 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.util;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;
import com.l2jmobius.config.ConfigDebug;
import com.l2jmobius.xml.Variant;
public class DebugUtil
{
private static Logger _log = LogManager.getLogger(DebugUtil.class);
public static void debug(String message)
{
if (ConfigDebug.DAT_DEBUG_MSG)
{
_log.info(message);
}
}
public static void debugPos(int pos, String name, Variant val)
{
if (ConfigDebug.DAT_DEBUG_POS)
{
_log.info("pos: " + pos + " " + name + ": " + val);
if ((ConfigDebug.DAT_DEBUG_POS_LIMIT != 0) && (pos > ConfigDebug.DAT_DEBUG_POS_LIMIT))
{
System.exit(0);
}
}
}
public static void save(ByteBuffer buffer, File path)
{
if (ConfigDebug.SAVE_DECODE)
{
try
{
String unpackDirPath = path.getParent() + "/" + "!decrypted";
File decryptedDir = new File(unpackDirPath);
decryptedDir.mkdir();
File file = new File(decryptedDir + "/" + path.getName());
FileOutputStream fos = new FileOutputStream(file);
fos.write(buffer.array());
fos.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
public static Logger getLogger()
{
return _log;
}
static
{
DOMConfigurator.configure("./config/log4j.xml");
}
}

View File

@ -0,0 +1,338 @@
/*
* 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.util;
import java.io.File;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.tools.Diagnostic;
import javax.tools.DiagnosticCollector;
import javax.tools.JavaCompiler;
import javax.tools.JavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;
public class Util
{
public static void printBytes(String name, byte[] buffer)
{
StringBuilder builder = new StringBuilder(buffer.length);
builder.append(name).append(": [");
for (byte b : buffer)
{
builder.append(b).append(" ");
}
builder.append("]");
DebugUtil.getLogger().info(builder.toString());
}
public static boolean compareBuffers(byte[] b1, byte[] b2)
{
if ((b1 == null) || (b2 == null) || (b1.length != b2.length))
{
return false;
}
for (int i = 0; i < b1.length; ++i)
{
if (b1[i] == b2[i])
{
continue;
}
return false;
}
return true;
}
private static String printData(byte[] data, int len)
{
int a;
int charpoint;
byte t1;
StringBuilder result = new StringBuilder();
int counter = 0;
for (int i = 0; i < len; ++i)
{
if ((counter % 16) == 0)
{
result.append(Util.fillHex(i, 4) + ": ");
}
result.append(Util.fillHex(data[i] & 255, 2) + " ");
if (++counter != 16)
{
continue;
}
result.append(" ");
charpoint = i - 15;
for (a = 0; a < 16; ++a)
{
if (((t1 = data[charpoint++]) > 31) && (t1 < 128))
{
result.append((char) t1);
continue;
}
result.append('.');
}
result.append("\n");
counter = 0;
}
int rest = data.length % 16;
if (rest > 0)
{
for (int i = 0; i < (17 - rest); ++i)
{
result.append(" ");
}
charpoint = data.length - rest;
for (a = 0; a < rest; ++a)
{
if (((t1 = data[charpoint++]) > 31) && (t1 < 128))
{
result.append((char) t1);
continue;
}
result.append('.');
}
result.append("\n");
}
return result.toString();
}
private static String fillHex(int data, int digits)
{
String number = Integer.toHexString(data);
for (int i = number.length(); i < digits; ++i)
{
number = "0" + number;
}
return number;
}
public static String printData(byte[] blop)
{
return Util.printData(blop, blop.length);
}
public static List<File> loadFiles(String dir, String prefix)
{
ArrayList<File> list = new ArrayList<>();
File folder = new File(dir);
for (File listOfFile : folder.listFiles())
{
if (listOfFile.isFile())
{
if (!listOfFile.getName().endsWith(prefix))
{
continue;
}
list.add(listOfFile);
continue;
}
if (!listOfFile.isDirectory())
{
continue;
}
File folder2 = new File(dir + listOfFile.getName() + "/");
for (File aListOfFiles2 : folder2.listFiles())
{
if (!aListOfFiles2.getName().endsWith(prefix))
{
continue;
}
list.add(aListOfFiles2);
}
}
return list;
}
public static String[] getDirsNames(String dir, String prefix)
{
ArrayList<String> list = new ArrayList<>();
File folder = new File(dir);
File[] listOfFiles = folder.listFiles();
if (listOfFiles == null)
{
return null;
}
for (File listOfFile : listOfFiles)
{
if (!listOfFile.isDirectory() || !listOfFile.getName().endsWith(prefix))
{
continue;
}
list.add(listOfFile.getName());
}
String[] text = new String[list.size()];
int i = 0;
Iterator<String> iterator = list.iterator();
while (iterator.hasNext())
{
text[i] = iterator.next();
++i;
}
return text;
}
public static String[] getFilesNames(String dir, String prefix)
{
ArrayList<String> list = new ArrayList<>();
File folder = new File(dir);
File[] listOfFiles = folder.listFiles();
if (listOfFiles == null)
{
return null;
}
for (File listOfFile : listOfFiles)
{
if (!listOfFile.isFile() || !listOfFile.getName().endsWith(prefix))
{
continue;
}
list.add(listOfFile.getName().replace(prefix, ""));
}
String[] text = new String[list.size()];
int i = 0;
Iterator<String> iterator = list.iterator();
while (iterator.hasNext())
{
text[i] = iterator.next();
++i;
}
return text;
}
public static String printData(ByteBuffer buf)
{
byte[] data = new byte[buf.remaining()];
buf.get(data);
String hex = Util.printData(data, data.length);
buf.position(buf.position() - data.length);
return hex;
}
public static List<String> splitList(String s)
{
if (s.startsWith("{"))
{
s = s.substring(1, s.length() - 1);
}
ArrayList<String> res = new ArrayList<>();
StringBuffer buff = new StringBuffer();
int level = 0;
for (char part : s.toCharArray())
{
if ((part == '{') || (part == '['))
{
++level;
}
else if ((part == '}') || (part == ']'))
{
--level;
}
else if ((part == ';') && (level == 0))
{
res.add(buff.toString());
buff = new StringBuffer();
continue;
}
buff.append(part);
}
res.add(buff.toString());
return res;
}
public static Map<String, String> stringToMap(String id)
{
LinkedHashMap<String, String> map = new LinkedHashMap<>();
for (String str : id.split("\t"))
{
if (!str.contains("="))
{
continue;
}
int index = str.indexOf("=");
String key = str.substring(0, index);
String val = str.substring(index + 1, str.length());
map.put(key, val);
}
return map;
}
public static String mapToString(Map<String, String> map)
{
StringBuilder builder = new StringBuilder();
for (String key : map.keySet())
{
builder.append(key).append("=").append(map.get(key)).append("\t");
}
return builder.toString();
}
@SuppressWarnings(
{
"rawtypes",
"unchecked"
})
public static void compileJavaClass(String sourceFile)
{
try
{
DiagnosticCollector diagnostics = new DiagnosticCollector();
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null);
Iterable<? extends JavaFileObject> compilationUnit = fileManager.getJavaFileObjectsFromFiles(Util.loadFiles(sourceFile, ".java"));
JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, diagnostics, null, null, compilationUnit);
if (!task.call().booleanValue())
{
for (Object diagnostic : diagnostics.getDiagnostics())
{
final Diagnostic d = (Diagnostic) diagnostic;
System.out.format("Error on line %d in %s%n", d.getLineNumber(), ((JavaFileObject) d.getSource()).toUri());
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static Object loadJavaClass(String className, String path)
{
try
{
@SuppressWarnings("resource")
URLClassLoader classLoader = new URLClassLoader(new URL[]
{
new File(path).toURI().toURL()
});
Class<?> loadedClass = classLoader.loadClass(className);
return loadedClass.newInstance();
}
catch (Exception e)
{
e.printStackTrace();
return null;
}
}
}