Skip to content

Commit 466c13b

Browse files
committed
dont actually need to read a whole int, byte is enough
1 parent 8f04a2e commit 466c13b

File tree

2 files changed

+36
-69
lines changed

2 files changed

+36
-69
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,4 @@
8282
<scope>test</scope>
8383
</dependency>
8484
</dependencies>
85-
</project>
85+
</project>

src/main/java/bwapi/Client.java

Lines changed: 35 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ of this software and associated documentation files (the "Software"), to deal
3232
import com.sun.jna.platform.win32.Kernel32;
3333
import com.sun.jna.win32.W32APIOptions;
3434

35-
import java.io.FileNotFoundException;
36-
import java.io.IOException;
3735
import java.io.RandomAccessFile;
3836
import java.nio.ByteBuffer;
3937
import java.nio.ByteOrder;
@@ -48,7 +46,7 @@ class Client {
4846

4947
private static final int maxNumGames = 8;
5048
private static final int gameTableSize = GAME_SIZE * maxNumGames;
51-
private LittleEndianPipe pipe;
49+
private RandomAccessFile pipe;
5250
private ClientData.GameData data;
5351

5452
Client() throws Exception {
@@ -75,11 +73,11 @@ public GameData data() {
7573
}
7674

7775
private void connect(final int procID) throws Exception {
78-
pipe = new LittleEndianPipe("\\\\.\\pipe\\bwapi_pipe_" + procID, "rw");
76+
pipe = new RandomAccessFile("\\\\.\\pipe\\bwapi_pipe_" + procID, "rw");
7977

80-
int code = 1;
78+
byte code = 1;
8179
while (code != 2) {
82-
code = pipe.readInt();
80+
code = pipe.readByte();
8381
}
8482

8583
final ByteBuffer sharedMemory = Kernel32.INSTANCE.MapViewOfFile(MappingKernel.INSTANCE
@@ -96,10 +94,10 @@ private void connect(final int procID) throws Exception {
9694
}
9795

9896
void update(final EventHandler handler) throws Exception {
99-
int code = 1;
100-
pipe.writeInt(code);
97+
byte code = 1;
98+
pipe.writeByte(code);
10199
while (code != 2) {
102-
code = pipe.readInt();
100+
code = pipe.readByte();
103101
}
104102
for (int i = 0; i < data.getEventCount(); ++i) {
105103
handler.operation(data.getEvents(i));
@@ -116,68 +114,37 @@ public interface EventHandler {
116114
void operation(ClientData.Event event);
117115
}
118116

119-
class LittleEndianPipe {
120-
private final RandomAccessFile pipe;
121117

122-
LittleEndianPipe(final String name, final String mode) throws FileNotFoundException {
123-
pipe = new RandomAccessFile(name, mode);
124-
}
125-
126-
final int readByte() throws IOException {
127-
return pipe.readByte();
128-
}
129-
130-
final void writeByte(final int b) throws IOException {
131-
pipe.writeByte(b);
132-
}
133-
134-
final int readInt() throws IOException {
135-
final int b1 = readByte();
136-
final int b2 = readByte();
137-
final int b3 = readByte();
138-
final int b4 = readByte();
139-
return (b4 << 24) | (b3 << 16) | (b2 << 8) | b1;
140-
}
141-
142-
final void writeInt(final int i) throws IOException {
143-
writeByte(i >> 24);
144-
writeByte((i >> 16) & 0xff);
145-
writeByte((i >> 8) & 0xff);
146-
writeByte(i & 0xff);
147-
}
118+
public String eventString(final int s) {
119+
return data.getEventStrings(s);
148120
}
149121

122+
public int addString(final String s) {
123+
int stringCount = data.getStringCount();
124+
if (stringCount >= 19999) throw new IllegalStateException("Too many shapes!");
125+
data.setStringCount(stringCount + 1);
126+
data.setStrings(stringCount, s);
127+
return stringCount;
128+
}
150129

151-
public String eventString(final int s) {
152-
return data.getEventStrings(s);
153-
}
154-
155-
public int addString(final String s) {
156-
int stringCount = data.getStringCount();
157-
if (stringCount >= 19999) throw new IllegalStateException("Too many shapes!");
158-
data.setStringCount(stringCount + 1);
159-
data.setStrings(stringCount, s);
160-
return stringCount;
161-
}
162-
163-
public Shape addShape() {
164-
int shapeCount = data.getShapeCount();
165-
if (shapeCount >= 19999) throw new IllegalStateException("Too many shapes!");
166-
data.setShapeCount(shapeCount + 1);
167-
return data.getShapes(shapeCount);
168-
}
130+
public Shape addShape() {
131+
int shapeCount = data.getShapeCount();
132+
if (shapeCount >= 19999) throw new IllegalStateException("Too many shapes!");
133+
data.setShapeCount(shapeCount + 1);
134+
return data.getShapes(shapeCount);
135+
}
169136

170-
public Command addCommand() {
171-
final int commandCount = data.getCommandCount();
172-
if (commandCount >= 19999) throw new IllegalStateException("Too many commands!");
173-
data.setCommandCount(commandCount + 1);
174-
return data.getCommands(commandCount);
175-
}
137+
public Command addCommand() {
138+
final int commandCount = data.getCommandCount();
139+
if (commandCount >= 19999) throw new IllegalStateException("Too many commands!");
140+
data.setCommandCount(commandCount + 1);
141+
return data.getCommands(commandCount);
142+
}
176143

177-
public ClientData.UnitCommand addUnitCommand() {
178-
int unitCommandCount = data.getUnitCommandCount();
179-
if (unitCommandCount >= 19999) throw new IllegalStateException("Too many unit commands!");
180-
data.setUnitCommandCount(unitCommandCount + 1);
181-
return data.getUnitCommands(unitCommandCount);
182-
}
183-
}
144+
public ClientData.UnitCommand addUnitCommand() {
145+
int unitCommandCount = data.getUnitCommandCount();
146+
if (unitCommandCount >= 19999) throw new IllegalStateException("Too many unit commands!");
147+
data.setUnitCommandCount(unitCommandCount + 1);
148+
return data.getUnitCommands(unitCommandCount);
149+
}
150+
}

0 commit comments

Comments
 (0)