Skip to content

Commit

Permalink
Refactored: Buffer
Browse files Browse the repository at this point in the history
Removed: Buffer#readStringBytes
Changed Game#login buffer size from 5000 to 1024.
Changed all `Buffer buffer` parameters to `Buffer in`
  • Loading branch information
galsjel committed Jan 8, 2023
1 parent 54deb95 commit 9bac90c
Show file tree
Hide file tree
Showing 12 changed files with 249 additions and 268 deletions.
164 changes: 70 additions & 94 deletions src/main/java/Buffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,107 +30,93 @@ public class Buffer extends DoublyLinkedList.Node {

private static final int[] BITMASK = {0, 1, 3, 7, 15, 31, 63, 127, 255, 511, 1023, 2047, 4095, 8191, 16383, 32767, 65535, 0x1ffff, 0x3ffff, 0x7ffff, 0xfffff, 0x1fffff, 0x3fffff, 0x7fffff, 0xffffff, 0x1ffffff, 0x3ffffff, 0x7ffffff, 0xfffffff, 0x1fffffff, 0x3fffffff, 0x7fffffff, -1};

public static Buffer create(int sizeType) {
Buffer buffer = new Buffer();
buffer.position = 0;

if (sizeType == 0) {
buffer.data = new byte[100];
} else if (sizeType == 1) {
buffer.data = new byte[5000];
} else {
buffer.data = new byte[30000];
}

return buffer;
}

public byte[] data;
public int position;
public int bitPosition;
public ISAACRandom random;

public Buffer() {
public Buffer(int size) {
this(new byte[size]);
}

public Buffer(byte[] src) {
data = src;
position = 0;
}

public void writeOp(int i) {
data[position++] = (byte) (i + random.nextInt());
public void writeOp(int op) {
data[position++] = (byte) (op + random.nextInt());
}

public void writeSize(int i) {
data[position - i - 1] = (byte) i;
public void writeSize(int value) {
data[position - value - 1] = (byte) value;
}

public void write8(int i) {
data[position++] = (byte) i;
public void write8(int value) {
data[position++] = (byte) value;
}

public void write8C(int i) {
data[position++] = (byte) -i;
public void write8C(int value) {
data[position++] = (byte) -value;
}

public void write8S(int j) {
data[position++] = (byte) (128 - j);
public void write8S(int value) {
data[position++] = (byte) (128 - value);
}

public void write16(int i) {
data[position++] = (byte) (i >> 8);
data[position++] = (byte) i;
public void write16(int value) {
data[position++] = (byte) (value >> 8);
data[position++] = (byte) value;
}

public void write16A(int j) {
data[position++] = (byte) (j >> 8);
data[position++] = (byte) (j + 128);
public void write16A(int value) {
data[position++] = (byte) (value >> 8);
data[position++] = (byte) (value + 128);
}

public void write16LE(int i) {
data[position++] = (byte) i;
data[position++] = (byte) (i >> 8);
public void write16LE(int value) {
data[position++] = (byte) value;
data[position++] = (byte) (value >> 8);
}

public void write16LEA(int j) {
data[position++] = (byte) (j + 128);
data[position++] = (byte) (j >> 8);
public void write16LEA(int value) {
data[position++] = (byte) (value + 128);
data[position++] = (byte) (value >> 8);
}

public void write24(int i) {
data[position++] = (byte) (i >> 16);
data[position++] = (byte) (i >> 8);
data[position++] = (byte) i;
public void write24(int value) {
data[position++] = (byte) (value >> 16);
data[position++] = (byte) (value >> 8);
data[position++] = (byte) value;
}

public void write32(int i) {
data[position++] = (byte) (i >> 24);
data[position++] = (byte) (i >> 16);
data[position++] = (byte) (i >> 8);
data[position++] = (byte) i;
public void write32(int value) {
data[position++] = (byte) (value >> 24);
data[position++] = (byte) (value >> 16);
data[position++] = (byte) (value >> 8);
data[position++] = (byte) value;
}

public void write32LE(int j) {
data[position++] = (byte) j;
data[position++] = (byte) (j >> 8);
data[position++] = (byte) (j >> 16);
data[position++] = (byte) (j >> 24);
public void write32LE(int value) {
data[position++] = (byte) value;
data[position++] = (byte) (value >> 8);
data[position++] = (byte) (value >> 16);
data[position++] = (byte) (value >> 24);
}

public void write64(long l) {
data[position++] = (byte) (int) (l >> 56);
data[position++] = (byte) (int) (l >> 48);
data[position++] = (byte) (int) (l >> 40);
data[position++] = (byte) (int) (l >> 32);
data[position++] = (byte) (int) (l >> 24);
data[position++] = (byte) (int) (l >> 16);
data[position++] = (byte) (int) (l >> 8);
data[position++] = (byte) (int) l;
public void write64(long value) {
data[position++] = (byte) (int) (value >> 56);
data[position++] = (byte) (int) (value >> 48);
data[position++] = (byte) (int) (value >> 40);
data[position++] = (byte) (int) (value >> 32);
data[position++] = (byte) (int) (value >> 24);
data[position++] = (byte) (int) (value >> 16);
data[position++] = (byte) (int) (value >> 8);
data[position++] = (byte) (int) value;
}

public void writeString(String s) {
write(s.getBytes(StandardCharsets.ISO_8859_1));
public void writeString(String value) {
write(value.getBytes(StandardCharsets.ISO_8859_1));
write8('\n');
}

Expand Down Expand Up @@ -183,8 +169,7 @@ public int read8US() {
* @return the value.
*/
public int readSmart() {
int i = data[position] & 0xff;
if (i < 128) {
if ((data[position] & 0xff) < 128) {
return read8U() - 64;
} else {
return read16U() - 49152;
Expand All @@ -197,8 +182,7 @@ public int readSmart() {
* @return the value.
*/
public int readSmartU() {
int i = data[position] & 0xff;
if (i < 128) {
if ((data[position] & 0xff) < 128) {
return read8U();
} else {
return read16U() - 32768;
Expand Down Expand Up @@ -227,29 +211,29 @@ public int read16ULEA() {

public int read16() {
position += 2;
int i = ((data[position - 2] & 0xff) << 8) + (data[position - 1] & 0xff);
if (i > 32767) {
i -= 0x10000;
int value = ((data[position - 2] & 0xff) << 8) + (data[position - 1] & 0xff);
if (value > 32767) {
value -= 65536;
}
return i;
return value;
}

public int read16LE() {
position += 2;
int j = ((data[position - 1] & 0xff) << 8) + (data[position - 2] & 0xff);
if (j > 0x7fff) {
j -= 0x10000;
int value = ((data[position - 1] & 0xff) << 8) + (data[position - 2] & 0xff);
if (value > 32767) {
value -= 65536;
}
return j;
return value;
}

public int read16LEA() {
position += 2;
int j = ((data[position - 1] & 0xff) << 8) + ((data[position - 2] - 128) & 0xff);
if (j > 0x7fff) {
j -= 0x10000;
int value = ((data[position - 1] & 0xff) << 8) + ((data[position - 2] - 128) & 0xff);
if (value > 32767) {
value -= 65536;
}
return j;
return value;
}

public int read24() {
Expand All @@ -273,29 +257,21 @@ public int read32RME() {
}

public long read64() {
long l = (long) read32() & 0xffffffffL;
long l1 = (long) read32() & 0xffffffffL;
return (l << 32) + l1;
long msi = (long) read32() & 0xffffffffL;
long lsi = (long) read32() & 0xffffffffL;
return (msi << 32) + lsi;
}

public String readString() {
int start = position;
while (data[position++] != '\n') {
while (true) {
if (data[position++] == '\n') {
break;
}
}
return new String(data, start, position - start - 1);
}

public byte[] readStringRaw() {
int start = position;
while (data[position++] != '\n') {
}
byte[] raw = new byte[position - start - 1];
for (int j = start; j < (position - 1); j++) {
raw[j - start] = data[j];
}
return raw;
}

public void read(byte[] dst, int off, int len) {
System.arraycopy(data, position, dst, off, len);
position += len;
Expand Down
20 changes: 10 additions & 10 deletions src/main/java/FloType.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ public static void unpack(FileArchive archive) throws IOException {
if (instances == null) {
instances = new FloType[count];
}
for (int j = 0; j < count; j++) {
if (instances[j] == null) {
instances[j] = new FloType();
for (int i = 0; i < count; i++) {
if (instances[i] == null) {
instances[i] = new FloType();
}
instances[j].read(buffer);
instances[i].read(buffer);
}
}

Expand All @@ -36,27 +36,27 @@ public static void unpack(FileArchive archive) throws IOException {
public FloType() {
}

public void read(Buffer buffer) {
public void read(Buffer in) {
while (true) {
int code = buffer.read8U();
int code = in.read8U();
if (code == 0) {
return;
} else if (code == 1) {
rgb = buffer.read24();
rgb = in.read24();
setColor(rgb);
} else if (code == 2) {
textureID = buffer.read8U();
textureID = in.read8U();
} else if (code == 3) {
} else if (code == 5) {
occludes = false;
} else if (code == 6) {
buffer.readString(); // name
in.readString(); // name
} else if (code == 7) {
int hue = this.hue;
int saturation = this.saturation;
int lightness = this.lightness;
int chroma = this.chroma;
int rgb = buffer.read24();
int rgb = in.read24();
setColor(rgb);
this.hue = hue;
this.saturation = saturation;
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ public static void setLowmem() {
*/
public String modalMessage;
public int privateChatSetting;
public Buffer login = Buffer.create(1);
public Buffer login = new Buffer(1024);
public boolean waveEnabled = true;
public int[] flameGradient;
public int[] flameGradient0;
Expand Down Expand Up @@ -437,7 +437,7 @@ public static void setLowmem() {
*/
public boolean awaitingSync = false;
public String[] friendName = new String[200];
public Buffer in = Buffer.create(1);
public Buffer in = new Buffer(5000);
/**
* The interface id of the container that the obj being dragged belongs to.
*/
Expand Down Expand Up @@ -589,7 +589,7 @@ public static void setLowmem() {
public int sidebarInterfaceID = -1;
public int[] flameBuffer0;
public int[] flameBuffer1;
public Buffer out = Buffer.create(1);
public Buffer out = new Buffer(5000);
public int lastAddress;
public int splitPrivateChat;
public Image8 imageInvback;
Expand Down
Loading

0 comments on commit 9bac90c

Please sign in to comment.