Skip to content

Commit 8b79250

Browse files
author
Jasper Geurtz
committed
apply override
1 parent bf020d9 commit 8b79250

File tree

2 files changed

+42
-77
lines changed

2 files changed

+42
-77
lines changed

src/main/java/bwapi/WrappedBufferOffset.java

Lines changed: 41 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package bwapi;
22

33
/**
4-
* Like WrappedBuffer but with offsets.
4+
* Like WrappedBuffer but to only copy the dynamic parts of the source WrappedBuffer.
55
*
6-
* <p> </p>For now this hardcodes a few things (size, offsets).
6+
* <p> For now this hardcodes a few things (size, offsets).
7+
* TODO: the `put` operations might not need the check (always dynamic?)
8+
* TODO: this class might not even be required if the Game class caches all initial static calls.
79
*/
810
class WrappedBufferOffset extends WrappedBuffer {
9-
1011
private WrappedBuffer sourceBuffer;
1112

1213
WrappedBufferOffset() {
@@ -27,109 +28,70 @@ void initialize() {
2728
}
2829
}
2930

30-
byte getByte(final int offset) {
31-
int newOffset = DynamicData.offset(offset);
32-
if (newOffset == -1) {
33-
return sourceBuffer.getByte(offset);
34-
} else {
35-
return UNSAFE.getByte(address + newOffset);
31+
long applyOffset(int offset) {
32+
// Only 4 region checks.
33+
for (MemRegion r : DynamicData.MEM_REGIONS) {
34+
if (offset >= r.start && offset < r.end) {
35+
return address + r.offset + (offset - r.start);
36+
}
3637
}
38+
return sourceBuffer.address + offset;
39+
}
40+
41+
byte getByte(final int offset) {
42+
return UNSAFE.getByte(applyOffset(offset));
3743
}
3844

3945
void putByte(final int offset, final byte value) {
40-
int newOffset = DynamicData.offset(offset);
41-
if (newOffset == -1) {
42-
sourceBuffer.putByte(offset, value);
43-
} else {
44-
UNSAFE.putByte(address + newOffset, value);
45-
}
46+
UNSAFE.putByte(applyOffset(offset), value);
4647
}
4748

4849
short getShort(final int offset) {
49-
int newOffset = DynamicData.offset(offset);
50-
if (newOffset == -1) {
51-
return sourceBuffer.getShort(offset);
52-
} else {
53-
return UNSAFE.getShort(address + newOffset);
54-
}
50+
return UNSAFE.getShort(applyOffset(offset));
5551
}
5652

5753
void putShort(final int offset, final short value) {
58-
int newOffset = DynamicData.offset(offset);
59-
if (newOffset == -1) {
60-
sourceBuffer.putShort(offset, value);
61-
} else {
62-
UNSAFE.putShort(address + newOffset, value);
63-
}
54+
UNSAFE.putShort(applyOffset(offset), value);
6455
}
6556

6657
int getInt(final int offset) {
67-
int newOffset = DynamicData.offset(offset);
68-
if (newOffset == -1) {
69-
return sourceBuffer.getInt(offset);
70-
} else {
71-
return UNSAFE.getInt(address + newOffset);
72-
}
58+
return UNSAFE.getInt(applyOffset(offset));
7359
}
7460

7561
void putInt(final int offset, final int value) {
76-
int newOffset = DynamicData.offset(offset);
77-
if (newOffset == -1) {
78-
sourceBuffer.putInt(offset, value);
79-
} else {
80-
UNSAFE.putInt(address + newOffset, value);
81-
}
62+
UNSAFE.putInt(applyOffset(offset), value);
8263
}
8364

8465
double getDouble(final int offset) {
85-
int newOffset = DynamicData.offset(offset);
86-
if (newOffset == -1) {
87-
return sourceBuffer.getDouble(offset);
88-
} else {
89-
return UNSAFE.getDouble(address + newOffset);
90-
}
66+
return UNSAFE.getDouble(applyOffset(offset));
9167
}
9268

9369
void putDouble(final int offset, final double value) {
94-
int newOffset = DynamicData.offset(offset);
95-
if (newOffset == -1) {
96-
sourceBuffer.putDouble(offset, value);
97-
} else {
98-
UNSAFE.putDouble(address + newOffset, value);
99-
}
70+
UNSAFE.putDouble(applyOffset(offset), value);
10071
}
10172

73+
10274
String getString(final int offset, final int maxLen) {
103-
int newOffset = DynamicData.offset(offset);
104-
if (newOffset == -1) {
105-
return sourceBuffer.getString(offset, maxLen);
106-
}
107-
else {
108-
final char[] buf = new char[maxLen];
109-
long pos = newOffset + address;
110-
for (int i = 0; i < maxLen; i++) {
111-
byte b = UNSAFE.getByte(pos);
112-
if (b == 0) break;
113-
buf[i] = (char) (b & 0xff);
114-
pos++;
115-
}
116-
return new String(buf, 0, (int) (pos - newOffset - address));
75+
final char[] buf = new char[maxLen];
76+
long start = applyOffset(offset);
77+
long pos = start;
78+
for (int i = 0; i < maxLen; i++) {
79+
byte b = UNSAFE.getByte(pos);
80+
if (b == 0) break;
81+
buf[i] = (char) (b & 0xff);
82+
pos++;
11783
}
84+
return new String(buf, 0, (int) (pos - start));
85+
11886
}
11987

12088
void putString(final int offset, final int maxLen, final String string) {
121-
int newOffset = DynamicData.offset(offset);
122-
if (newOffset == -1) {
123-
sourceBuffer.putString(offset, maxLen, string);
124-
}
125-
else {
126-
long pos = newOffset + address;
127-
for (int i = 0; i < Math.min(string.length(), maxLen - 1); i++) {
128-
UNSAFE.putByte(pos, (byte) string.charAt(i));
129-
pos++;
130-
}
131-
UNSAFE.putByte(pos, (byte) 0);
89+
long pos = applyOffset(offset);
90+
for (int i = 0; i < Math.min(string.length(), maxLen - 1); i++) {
91+
UNSAFE.putByte(pos, (byte) string.charAt(i));
92+
pos++;
13293
}
94+
UNSAFE.putByte(pos, (byte) 0);
13395
}
13496

13597
private void copyBuffer(long sourceOffset, long destinationOffset, int size) {
@@ -158,9 +120,12 @@ static final class DynamicData {
158120

159121
static final int STRINGSHAPES_END = 32242636;
160122
static final int UNITFINDER_START = 32962644;
123+
124+
// ~5MB
161125
static final int SIZE = OFFSET_3 + (UNITFINDER_START - STRINGSHAPES_END);
162126

163127
static int offset(int offset) {
128+
// Only 4 region checks.
164129
for (MemRegion r : MEM_REGIONS) {
165130
if (offset >= r.start && offset < r.end) {
166131
return r.offset + (offset - r.start);

src/test/java/bwapi/SynchronizationEnvironment.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ void runGame(int onEndFrame) {
8888
if (configuration.getAsync()) {
8989
final long MEGABYTE = 1024 * 1024;
9090
long memoryFree = Runtime.getRuntime().freeMemory() / MEGABYTE;
91-
long memoryRequired = configuration.getAsyncFrameBufferCapacity() * ClientData.GameData.SIZE / MEGABYTE;
91+
long memoryRequired = (long)configuration.getAsyncFrameBufferCapacity() * WrappedBufferOffset.DynamicData.SIZE / MEGABYTE;
9292
assertTrue(
9393
"Unit test needs to be run with sufficient memory to allocate frame buffer. Has "
9494
+ memoryFree

0 commit comments

Comments
 (0)