Skip to content

Commit ad35b1b

Browse files
committed
truncate strings that are too large to draw, like BWAPI does
1 parent a9e55ec commit ad35b1b

File tree

5 files changed

+105
-29
lines changed

5 files changed

+105
-29
lines changed

src/main/java/bwapi/Client.java

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public interface EventHandler {
5151

5252
private static final int SUPPORTED_BWAPI_VERSION = 10003;
5353
static final int MAX_COUNT = 19999;
54+
static final int MAX_STRING_SIZE = 1024;
5455

5556
private ClientData clientData;
5657
private ClientData.GameData gameData;
@@ -244,31 +245,45 @@ String eventString(final int s) {
244245
return gameData.getEventStrings(s);
245246
}
246247

247-
int addString(final String s) {
248+
int addString(final String string) {
248249
int stringCount = gameData.getStringCount();
249-
if (stringCount >= MAX_COUNT) throw new IllegalStateException("Too many strings!");
250+
if (stringCount >= MAX_COUNT) {
251+
throw new IllegalStateException("Too many strings!");
252+
}
253+
254+
//truncate string if its size equals or exceeds 1024
255+
final String stringTruncated = string.length() >= MAX_STRING_SIZE
256+
? string.substring(0, MAX_STRING_SIZE - 1)
257+
: string;
258+
250259
gameData.setStringCount(stringCount + 1);
251-
gameData.setStrings(stringCount, s);
260+
gameData.setStrings(stringCount, stringTruncated);
252261
return stringCount;
253262
}
254263

255264
Shape addShape() {
256265
int shapeCount = gameData.getShapeCount();
257-
if (shapeCount >= MAX_COUNT) throw new IllegalStateException("Too many shapes!");
266+
if (shapeCount >= MAX_COUNT) {
267+
throw new IllegalStateException("Too many shapes!");
268+
}
258269
gameData.setShapeCount(shapeCount + 1);
259270
return gameData.getShapes(shapeCount);
260271
}
261272

262273
Command addCommand() {
263274
final int commandCount = gameData.getCommandCount();
264-
if (commandCount >= MAX_COUNT) throw new IllegalStateException("Too many commands!");
275+
if (commandCount >= MAX_COUNT) {
276+
throw new IllegalStateException("Too many commands!");
277+
}
265278
gameData.setCommandCount(commandCount + 1);
266279
return gameData.getCommands(commandCount);
267280
}
268281

269282
ClientData.UnitCommand addUnitCommand() {
270283
int unitCommandCount = gameData.getUnitCommandCount();
271-
if (unitCommandCount >= MAX_COUNT) throw new IllegalStateException("Too many unit commands!");
284+
if (unitCommandCount >= MAX_COUNT) {
285+
throw new IllegalStateException("Too many unit commands!");
286+
}
272287
gameData.setUnitCommandCount(unitCommandCount + 1);
273288
return gameData.getUnitCommands(unitCommandCount);
274289
}

src/main/java/bwapi/WrappedBuffer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ String getString(final int offset, final int maxLen) {
7676
}
7777

7878
void putString(final int offset, final int maxLen, final String string) {
79-
if (string.length() + 1 >= maxLen) {
79+
if (string.length() >= maxLen) {
8080
throw new StringIndexOutOfBoundsException();
8181
}
8282
long pos = offset + address;

src/test/java/bwapi/BWEMTest.java

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -56,31 +56,10 @@ public boolean equals(Object object) {
5656
mapData.put("(4)Roadrunner.scx", new BWEMMap(26, 12, 35));
5757
}
5858

59-
60-
Game initGame(String mapName) throws IOException {
61-
String location = "src/test/resources/" + mapName + "_frame0_buffer.bin";
62-
63-
// load bytebuffer
64-
byte[] compressedBytes = Files.readAllBytes(Paths.get(location));
65-
ByteArrayOutputStream out = new ByteArrayOutputStream();
66-
InflaterOutputStream zin = new InflaterOutputStream(out);
67-
zin.write(compressedBytes);
68-
zin.flush();
69-
zin.close();
70-
byte[] bytes = out.toByteArray();
71-
ByteBuffer buffer = ByteBuffer.allocateDirect(bytes.length);
72-
buffer.put(bytes);
73-
74-
Client client = new Client(buffer);
75-
Game game = new Game(client);
76-
game.init();
77-
return game;
78-
}
79-
8059
@Test
8160
public void checkSSCAITMaps() throws IOException {
8261
for (String mapName : mapData.keySet()) {
83-
Game game = initGame(mapName);
62+
Game game = GameBuilder.createGame(mapName);
8463
BWEM bwem = new BWEM(game);
8564
bwem.initialize();
8665
assertEquals(new BWEMMap(bwem.getMap()), mapData.get(mapName));

src/test/java/bwapi/DrawTest.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package bwapi;
2+
3+
import org.junit.Test;
4+
5+
import java.io.IOException;
6+
7+
public class DrawTest {
8+
9+
10+
@Test
11+
public void drawingHugeStringShouldTruncate() throws IOException {
12+
final String test = "L: PvP Jadien @ (4)Gladiator1.1.scx (PvPRobo, Fingerprint2Gate, FingerprintDragoonRange, FingerprintGatewayFirst, FingerprintMannerPylon, FingerprintProxyGateway)\n" +
13+
"W: PvP Jadien @ (3)Power Bond.scx (PvP2GateDTExpand)\n" +
14+
"W: PvP Jadien @ (2)Overwatch(n).scx (PvP2GateDTExpand, Fingerprint1GateCore, Fingerprint4GateGoon, FingerprintDragoonRange, FingerprintGatewayFirst, FingerprintMannerPylon)\n" +
15+
"L: PvP Jadien @ (4)Fighting Spirit.scx (PvPProxy2Gate, Fingerprint1GateCore, Fingerprint2Gate, FingerprintDragoonRange, FingerprintGatewayFirst, FingerprintMannerPylon)\n" +
16+
"L: TvT Atlas Wing @ (2)Overwatch(n).scx (TvT1FacFE, TvT2BaseBC, FingerprintBio)\n" +
17+
"W: TvT Epsilon Squadron @ (4)Gladiator1.1.scx (TvT2FacTanks, TvT5Fac, FingerprintBio)\n" +
18+
"W: TvT Epsilon Squadron @ (4)Fighting Spirit.scx (TvT2FacTanks, TvT5Fac, Fingerprint2Rax1113, FingerprintBio)\n" +
19+
"W: TvT Mar Sara @ (2)Tres Pass.scx (TvT2FacTanks, TvT5Fac, FingerprintBio)\n" +
20+
"W: TvT Epsilon Squadron @ (4)Fighting Spirit.scx (TvT2FacTanks, TvT5Fac, FingerprintBio)\n" +
21+
"W: TvT Antiga @ (4)CircuitBreakers1.0.scx (TvT1RaxFE, TvT5Fac, FingerprintBio)\n" +
22+
"W: TvT Epsilon Squadron @ (4)CircuitBreakers1.0.scx (TvT2FacTanks, TvT5Fac, FingerprintBio)\n" +
23+
"W: TvT Elite Guard @ (2)Overwatch(n).scx (TvT1FacFE, TvT2BaseBC, FingerprintBBS, FingerprintBio)\n" +
24+
"W: TvT Delta Squadron @ (4)Fighting Spirit.scx (TvT1RaxFE, TvT2Base2Port, TvT2BaseBC, FingerprintBBS, FingerprintBio)\n" +
25+
"W: TvT Mar Sara @ (2)Tres Pass.scx (TvT1FacPort, TvT5Fac, FingerprintBBS, FingerprintBio)\n" +
26+
"W: TvT Delta Squadron @ (3)Power Bond.scx (TvT1FacFE, TvT2Base2Port, TvT2BaseBC, FingerprintBio)\n" +
27+
"L: TvT Delta Squadron @ (2)Overwatch(n).scx (TvE1RaxSCVMarine, FingerprintBio)\n" +
28+
"W: TvT Epsilon Squadron @ (4)Fighting Spirit.scx (TvT2FacTanks, TvT5Fac, FingerprintBio)\n" +
29+
"W: TvT Cronus Wing @ (2)Overwatch(n).scx (TvT2Port, TvT2Base2Port, TvT2BaseBC, FingerprintBio)\n" +
30+
"W: TvT Epsilon Squadron @ (4)CircuitBreakers1.0.scx (TvT2FacTanks, TvT5Fac, FingerprintBio)\n" +
31+
"W: TvT Antiga @ (3)Power Bond.scx (TvT1RaxFE, TvT5Fac, FingerprintBio)\n" +
32+
"W: TvT Cronus Wing @ (4)Gladiator1.1.scx (TvT2FacTanks, TvT2Base2Port, TvT2BaseBC, FingerprintBBS, FingerprintBio)\n" +
33+
"L: TvT Kel-Morian Combine @ (4)Gladiator1.1.scx (TvT14CC, TvT5Fac)\n" +
34+
"W: TvT Cronus Wing @ (4)Fighting Spirit.scx (TvT1FacPort, TvT2Base2Port, TvT2BaseBC, FingerprintBBS, FingerprintBio)\n" +
35+
"W: TvT Cronus Wing @ (2)Overwatch(n).scx (TvT2FacTanks, TvT2Base2Port, TvT2BaseBC, FingerprintBio)\n" +
36+
"W: TvT Kel-Morian Combine @ (4)CircuitBreakers1.0.scx (TvE2RaxSCVMarine, FingerprintBio)\n" +
37+
"W: TvT Atlas Wing @ (4)Fighting Spirit.scx (TvT1FacFE, TvT2BaseBC, FingerprintBBS, FingerprintBio)\n" +
38+
"W: TvT Epsilon Squadron @ (2)Tres Pass.scx (TvT2FacTanks, TvT5Fac, FingerprintBio, FingerprintWorkerRush)\n" +
39+
"W: TvT Antiga @ (4)Gladiator1.1.scx (TvT1RaxFE, TvT5Fac, Fingerprint2Rax1113, FingerprintBio)\n" +
40+
"L: TvT Delta Squadron @ (4)CircuitBreakers1.0.scx (TvEProxyBBS)\n" +
41+
"L: TvT Cronus Wing @ (4)Fighting Spirit.scx (TvEProxyBBS, FingerprintBio)";
42+
43+
final Game game = GameBuilder.createGame();
44+
45+
game.drawTextScreen(0, 0, test);
46+
}
47+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package bwapi;
2+
3+
import java.io.ByteArrayOutputStream;
4+
import java.io.IOException;
5+
import java.nio.ByteBuffer;
6+
import java.nio.file.Files;
7+
import java.nio.file.Paths;
8+
import java.util.zip.InflaterOutputStream;
9+
10+
public class GameBuilder {
11+
12+
public static Game createGame() throws IOException {
13+
return createGame("(2)Benzene.scx");
14+
}
15+
16+
public static Game createGame(String mapName) throws IOException {
17+
final String location = "src/test/resources/" + mapName + "_frame0_buffer.bin";
18+
19+
// load bytebuffer
20+
final byte[] compressedBytes = Files.readAllBytes(Paths.get(location));
21+
final ByteArrayOutputStream out = new ByteArrayOutputStream();
22+
final InflaterOutputStream zin = new InflaterOutputStream(out);
23+
zin.write(compressedBytes);
24+
zin.flush();
25+
zin.close();
26+
final byte[] bytes = out.toByteArray();
27+
final ByteBuffer buffer = ByteBuffer.allocateDirect(bytes.length);
28+
buffer.put(bytes);
29+
30+
final Client client = new Client(buffer);
31+
final Game game = new Game(client);
32+
game.init();
33+
return game;
34+
}
35+
}

0 commit comments

Comments
 (0)