Refactored Player and NPC packets

This commit is contained in:
thedaneeffect committed 2023-01-05 13:31:06 -05:00
1 parent 22587cdb5a
commit 65b84fdeaa
11 files changed
+2196 -2044

No files matched your search

+100 -101
View File
@@ -1,123 +1,122 @@
public class ChatCompression {
public final class ChatCompression {
private static final char[] cbuf = new char[100];
private static final Buffer buf = new Buffer(new byte[100]);
private static final char[] charBuffer = new char[100];
private static final Buffer buffer = new Buffer(new byte[100]);
private static final char[] TABLE = { //
// only this first row is actually combined with anything else
' ', 'e', 't', 'a', 'o', 'i', 'h', 'n', 's', 'r', 'd', 'l', 'u', //
// the rest are just 'accepted' values.
'm', 'w', 'c', 'y', 'f', 'g', 'p', 'b', 'v', 'k', 'x', 'j', 'q', 'z', //
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', //
' ', '!', '?', '.', ',', ':', ';', '(', ')', '-', '&', '*', '\\', '\'', '@', '#', '+', '=', '\u00A3', '$', '%', '"', '[', ']' //
};
private static final char[] TABLE = {
// only this first row is actually combined with anything else
' ', 'e', 't', 'a', 'o', 'i', 'h', 'n', 's', 'r', 'd', 'l', 'u',
// the rest are just 'accepted' values.
'm', 'w', 'c', 'y', 'f', 'g', 'p', 'b', 'v', 'k', 'x', 'j', 'q', 'z',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
' ', '!', '?', '.', ',', ':', ';', '(', ')', '-', '&', '*', '\\', '\'', '@', '#', '+', '=', '£', '$', '%', '"', '[', ']'
};
private static final int RANGE = ' ' + '\u00A3'; // least significant char + most significant char (in terms of value)
private static final int RANGE = ' ' + '£'; // least significant char + most significant char (in terms of value)
public static String unpack(int expectedLength, Buffer buffer) {
int length = 0;
int carry = -1;
public static String unpack(int length, Buffer in) {
int pos = 0;
int carry = -1;
for (int i = 0; i < expectedLength; i++) {
int value = buffer.read8U();
int nibble = (value >> 4) & 0xf;
for (int i = 0; i < length; i++) {
int value = in.read8U();
int nibble = (value >> 4) & 0b1111;
if (carry == -1) {
if (nibble < 13) {
cbuf[length++] = TABLE[nibble];
} else {
carry = nibble;
}
} else {
cbuf[length++] = TABLE[((carry << 4) + nibble) - RANGE];
carry = -1;
}
if (carry == -1) {
if (nibble < 13) {
charBuffer[pos++] = TABLE[nibble];
} else {
carry = nibble;
}
} else {
charBuffer[pos++] = TABLE[((carry << 4) + nibble) - RANGE];
carry = -1;
}
nibble = value & 0xf;
nibble = value & 0xf;
if (carry == -1) {
if (nibble < 13) {
cbuf[length++] = TABLE[nibble];
} else {
carry = nibble;
}
} else {
cbuf[length++] = TABLE[((carry << 4) + nibble) - RANGE];
carry = -1;
}
}
if (carry == -1) {
if (nibble < 13) {
charBuffer[pos++] = TABLE[nibble];
} else {
carry = nibble;
}
} else {
charBuffer[pos++] = TABLE[((carry << 4) + nibble) - RANGE];
carry = -1;
}
}
// basic sentence casing
// "hi. i'm a line." -> "Hi. I'm a line."
boolean uppercase = true;
for (int i = 0; i < length; i++) {
char c = cbuf[i];
if (uppercase && (c >= 'a') && (c <= 'z')) {
cbuf[i] -= 'a' - 'A';
uppercase = false;
}
// basic sentence casing
// "hi. i'm a line." -> "Hi. I'm a line."
boolean uppercase = true;
for (int i = 0; i < pos; i++) {
char c = charBuffer[i];
if (uppercase && (c >= 'a') && (c <= 'z')) {
charBuffer[i] -= 'a' - 'A';
uppercase = false;
}
if ((c == '.') || (c == '!') || (c == '?')) {
uppercase = true;
}
}
if ((c == '.') || (c == '!') || (c == '?')) {
uppercase = true;
}
}
return new String(cbuf, 0, length);
}
return new String(charBuffer, 0, pos);
}
public static void pack(String s, Buffer dst) {
if (s.length() > 80) {
s = s.substring(0, 80);
}
s = s.toLowerCase();
public static void pack(String in, Buffer out) {
if (in.length() > 80) {
in = in.substring(0, 80);
}
in = in.toLowerCase();
int carry = -1;
for (int i = 0; i < s.length(); i++) {
char ch = s.charAt(i);
int carry = -1;
for (int i = 0; i < in.length(); i++) {
char ch = in.charAt(i);
int index = 0;
for (int j = 0; j < TABLE.length; j++) {
if (ch != TABLE[j]) {
continue;
}
index = j;
break;
}
int index = 0;
for (int j = 0; j < TABLE.length; j++) {
if (ch == TABLE[j]) {
index = j;
break;
}
}
if (index > 12) {
index += RANGE;
}
if (index > 12) {
index += RANGE;
}
if (carry == -1) {
if (index < 13) {
carry = index;
} else {
dst.write8(index);
}
} else if (index < 13) {
dst.write8((carry << 4) + index);
carry = -1;
} else {
dst.write8((carry << 4) + (index >> 4));
carry = index & 0xf;
}
}
if (carry == -1) {
if (index < 13) {
carry = index;
} else {
out.write8(index);
}
} else if (index < 13) {
out.write8((carry << 4) + index);
carry = -1;
} else {
out.write8((carry << 4) + (index >> 4));
carry = index & 0xf;
}
}
if (carry != -1) {
dst.write8(carry << 4);
}
}
if (carry != -1) {
out.write8(carry << 4);
}
}
public static String format(String string) {
buf.position = 0;
pack(string, buf);
int length = buf.position;
buf.position = 0;
return unpack(length, buf);
}
public static String format(String string) {
buffer.position = 0;
pack(string, buffer);
int length = buffer.position;
buffer.position = 0;
return unpack(length, buffer);
}
private ChatCompression() {
private ChatCompression() {
}
}
}