Skip to content

Commit 1b0a8a7

Browse files
committed
make sure the bwapi version is correct, as the dumped offsets are for a specific version
1 parent a1c6b1b commit 1b0a8a7

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

src/main/java/bwapi/Client.java

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,19 @@ of this software and associated documentation files (the "Software"), to deal
3939
import java.nio.ByteOrder;
4040

4141
class Client {
42-
public static final int READ_WRITE = 0x1 | 0x2 | 0x4;
42+
private static final int READ_WRITE = 0x1 | 0x2 | 0x4;
4343
private static final int GAME_SIZE = 4 // ServerProcID
4444
+ 4 // IsConnected
4545
+ 4 // LastKeepAliveTime
4646
;
47+
private static final int BWAPI_VERSION = 10002;
4748

4849
private static final int maxNumGames = 8;
4950
private static final int gameTableSize = GAME_SIZE * maxNumGames;
5051
private LittleEndianPipe pipe;
5152
private ClientData.GameData data;
5253

53-
public Client() throws Exception {
54+
Client() throws Exception {
5455
final ByteBuffer gameList = Kernel32.INSTANCE.MapViewOfFile(MappingKernel.INSTANCE.OpenFileMapping(READ_WRITE, false, "Local\\bwapi_shared_memory_game_list"), READ_WRITE, 0, 0, gameTableSize).getByteBuffer(0, GAME_SIZE * 8);
5556
gameList.order(ByteOrder.LITTLE_ENDIAN);
5657
for (int i = 0; i < 8; ++i) {
@@ -61,8 +62,8 @@ public Client() throws Exception {
6162
try {
6263
this.connect(procID);
6364
return;
64-
} catch (Exception ignored) {
65-
System.err.println(ignored);
65+
} catch (final Exception exception) {
66+
System.err.println(exception);
6667
}
6768
}
6869
}
@@ -75,19 +76,27 @@ public GameData data() {
7576

7677
private void connect(final int procID) throws Exception {
7778
pipe = new LittleEndianPipe("\\\\.\\pipe\\bwapi_pipe_" + procID, "rw");
78-
ByteBuffer sharedMemory = Kernel32.INSTANCE.MapViewOfFile(MappingKernel.INSTANCE
79-
.OpenFileMapping(READ_WRITE, false, "Local\\bwapi_shared_memory_" + procID), READ_WRITE,
80-
0, 0, GameData.SIZE).getByteBuffer(0, GameData.SIZE);
81-
sharedMemory.order(ByteOrder.LITTLE_ENDIAN);
79+
8280
int code = 1;
8381
while (code != 2) {
8482
code = pipe.readInt();
8583
}
84+
85+
final ByteBuffer sharedMemory = Kernel32.INSTANCE.MapViewOfFile(MappingKernel.INSTANCE
86+
.OpenFileMapping(READ_WRITE, false, "Local\\bwapi_shared_memory_" + procID), READ_WRITE,
87+
0, 0, GameData.SIZE).getByteBuffer(0, GameData.SIZE);
88+
sharedMemory.order(ByteOrder.LITTLE_ENDIAN);
8689
data = new ClientData(sharedMemory).new GameData(0);
87-
System.out.println("Connected to BWAPI@" + procID + " with version " + data.getClient_version() + ": " + data.getRevision());
90+
91+
final int clientVersion = data.getClient_version();
92+
if (clientVersion != BWAPI_VERSION) {
93+
throw new Exception("BWAPI version mismatch, expected: " + BWAPI_VERSION + ", got: " + clientVersion);
94+
}
95+
96+
System.out.println("Connected to BWAPI@" + procID + " with version " + clientVersion + ": " + data.getRevision());
8897
}
8998

90-
public void update(final EventHandler handler) throws Exception {
99+
void update(final EventHandler handler) throws Exception {
91100
int code = 1;
92101
pipe.writeInt(code);
93102
while (code != 2) {

0 commit comments

Comments
 (0)