@@ -29,12 +29,11 @@ of this software and associated documentation files (the "Software"), to deal
29
29
import bwapi .ClientData .GameData ;
30
30
import bwapi .ClientData .Shape ;
31
31
import com .sun .jna .Native ;
32
+ import com .sun .jna .Pointer ;
32
33
import com .sun .jna .platform .win32 .Kernel32 ;
33
34
import com .sun .jna .win32 .W32APIOptions ;
34
35
35
36
import java .io .RandomAccessFile ;
36
- import java .nio .ByteBuffer ;
37
- import java .nio .ByteOrder ;
38
37
39
38
class Client {
40
39
interface MappingKernel extends Kernel32 {
@@ -51,14 +50,13 @@ public interface EventHandler {
51
50
52
51
private static final int SUPPORTED_BWAPI_VERSION = 10003 ;
53
52
static final int MAX_COUNT = 19999 ;
54
- static final int MAX_STRING_SIZE = 1024 ;
55
53
56
54
private ClientData clientData ;
57
55
private ClientData .GameData gameData ;
58
56
private boolean connected = false ;
59
57
private RandomAccessFile pipeObjectHandle = null ;
60
- private ByteBuffer mapFileHandle = null ;
61
- private ByteBuffer gameTableFileHandle = null ;
58
+ private WrappedBuffer mapFileHandle = null ;
59
+ private WrappedBuffer gameTableFileHandle = null ;
62
60
63
61
private boolean debugConnection = false ;
64
62
@@ -69,7 +67,7 @@ public interface EventHandler {
69
67
/**
70
68
* For test purposes only
71
69
*/
72
- Client (ByteBuffer buffer ) {
70
+ Client (final WrappedBuffer buffer ) {
73
71
clientData = new ClientData (buffer );
74
72
gameData = clientData .new GameData (0 );
75
73
}
@@ -86,7 +84,7 @@ boolean isConnected() {
86
84
return connected ;
87
85
}
88
86
89
- void reconnect (){
87
+ void reconnect () {
90
88
while (!connect ()) {
91
89
sleep (1000 );
92
90
}
@@ -101,11 +99,10 @@ void disconnect() {
101
99
return ;
102
100
}
103
101
104
- if (pipeObjectHandle != null ) {
102
+ if (pipeObjectHandle != null ) {
105
103
try {
106
104
pipeObjectHandle .close ();
107
- }
108
- catch (Exception e ) {
105
+ } catch (Exception e ) {
109
106
e .printStackTrace ();
110
107
}
111
108
pipeObjectHandle = null ;
@@ -127,21 +124,19 @@ boolean connect() {
127
124
int gameTableIndex = -1 ;
128
125
129
126
try {
130
- gameTableFileHandle = Kernel32 .INSTANCE .MapViewOfFile (
131
- MappingKernel .INSTANCE .OpenFileMapping (READ_WRITE , false , "Local\\ bwapi_shared_memory_game_list" ), READ_WRITE , 0 , 0 , GameTable .SIZE )
132
- .getByteBuffer (0 , GameTable .SIZE );
133
- gameTableFileHandle .order (ByteOrder .LITTLE_ENDIAN );
134
- }
135
- catch (Exception e ) {
127
+ final Pointer gameTableView = Kernel32 .INSTANCE .MapViewOfFile (MappingKernel .INSTANCE
128
+ .OpenFileMapping (READ_WRITE , false , "Local\\ bwapi_shared_memory_game_list" ), READ_WRITE ,
129
+ 0 , 0 , GameTable .SIZE );
130
+ gameTableFileHandle = new WrappedBuffer (gameTableView , GameTable .SIZE );
131
+ } catch (Exception e ) {
136
132
System .err .println ("Game table mapping not found." );
137
133
return false ;
138
134
}
139
135
140
136
GameTable gameTable ;
141
137
try {
142
138
gameTable = new GameTable (gameTableFileHandle );
143
- }
144
- catch (Exception e ) {
139
+ } catch (Exception e ) {
145
140
System .err .println ("Unable to map Game table." );
146
141
if (debugConnection ) {
147
142
e .printStackTrace ();
@@ -150,11 +145,11 @@ boolean connect() {
150
145
}
151
146
152
147
int latest = 0 ;
153
- for (int i = 0 ; i < GameTable .MAX_GAME_INSTANCES ; i ++) {
148
+ for (int i = 0 ; i < GameTable .MAX_GAME_INSTANCES ; i ++) {
154
149
GameInstance gameInstance = gameTable .gameInstances [i ];
155
150
System .out .println (i + " | " + gameInstance .serverProcessID + " | " + (gameInstance .isConnected ? 1 : 0 ) + " | " + gameInstance .lastKeepAliveTime );
156
151
if (gameInstance .serverProcessID != 0 && !gameInstance .isConnected ) {
157
- if ( gameTableIndex == -1 || latest == 0 || gameInstance .lastKeepAliveTime < latest ) {
152
+ if (gameTableIndex == -1 || latest == 0 || gameInstance .lastKeepAliveTime < latest ) {
158
153
latest = gameInstance .lastKeepAliveTime ;
159
154
gameTableIndex = i ;
160
155
}
@@ -173,9 +168,8 @@ boolean connect() {
173
168
final String sharedMemoryName = "Local\\ bwapi_shared_memory_" + serverProcID ;
174
169
final String communicationPipe = "\\ \\ .\\ pipe\\ bwapi_pipe_" + serverProcID ;
175
170
try {
176
- pipeObjectHandle = new RandomAccessFile (communicationPipe , "rw" );
177
- }
178
- catch (Exception e ) {
171
+ pipeObjectHandle = new RandomAccessFile (communicationPipe , "rw" );
172
+ } catch (Exception e ) {
179
173
System .err .println ("Unable to open communications pipe: " + communicationPipe );
180
174
if (debugConnection ) {
181
175
e .printStackTrace ();
@@ -186,11 +180,11 @@ boolean connect() {
186
180
System .out .println ("Connected" );
187
181
188
182
try {
189
- mapFileHandle = Kernel32 .INSTANCE .MapViewOfFile (MappingKernel .INSTANCE
183
+ final Pointer mapFileView = Kernel32 .INSTANCE .MapViewOfFile (MappingKernel .INSTANCE
190
184
.OpenFileMapping (READ_WRITE , false , sharedMemoryName ), READ_WRITE ,
191
- 0 , 0 , GameData .SIZE ). getByteBuffer ( 0 , GameData . SIZE ) ;
192
- }
193
- catch (Exception e ) {
185
+ 0 , 0 , GameData .SIZE );
186
+ mapFileHandle = new WrappedBuffer ( mapFileView , GameData . SIZE );
187
+ } catch (Exception e ) {
194
188
System .err .println ("Unable to open shared memory mapping: " + sharedMemoryName );
195
189
if (debugConnection ) {
196
190
e .printStackTrace ();
@@ -202,8 +196,7 @@ boolean connect() {
202
196
try {
203
197
clientData = new ClientData (mapFileHandle );
204
198
gameData = clientData .new GameData (0 );
205
- }
206
- catch (Exception e ) {
199
+ } catch (Exception e ) {
207
200
System .err .println ("Unable to map game data." );
208
201
if (debugConnection ) {
209
202
e .printStackTrace ();
@@ -223,8 +216,7 @@ boolean connect() {
223
216
while (code != 2 ) {
224
217
try {
225
218
code = pipeObjectHandle .readByte ();
226
- }
227
- catch (Exception e ) {
219
+ } catch (Exception e ) {
228
220
System .err .println ("Unable to read pipe object." );
229
221
if (debugConnection ) {
230
222
e .printStackTrace ();
@@ -243,8 +235,7 @@ void update(final EventHandler handler) {
243
235
byte code = 1 ;
244
236
try {
245
237
pipeObjectHandle .writeByte (code );
246
- }
247
- catch (Exception e ) {
238
+ } catch (Exception e ) {
248
239
System .err .println ("failed, disconnecting" );
249
240
if (debugConnection ) {
250
241
e .printStackTrace ();
@@ -255,8 +246,7 @@ void update(final EventHandler handler) {
255
246
while (code != 2 ) {
256
247
try {
257
248
code = pipeObjectHandle .readByte ();
258
- }
259
- catch (Exception e ) {
249
+ } catch (Exception e ) {
260
250
System .err .println ("failed, disconnecting" );
261
251
if (debugConnection ) {
262
252
e .printStackTrace ();
@@ -280,13 +270,8 @@ int addString(final String string) {
280
270
throw new IllegalStateException ("Too many strings!" );
281
271
}
282
272
283
- //truncate string if its size equals or exceeds 1024
284
- final String stringTruncated = string .length () >= MAX_STRING_SIZE
285
- ? string .substring (0 , MAX_STRING_SIZE - 1 )
286
- : string ;
287
-
288
273
gameData .setStringCount (stringCount + 1 );
289
- gameData .setStrings (stringCount , stringTruncated );
274
+ gameData .setStrings (stringCount , string );
290
275
return stringCount ;
291
276
}
292
277
@@ -318,10 +303,9 @@ ClientData.UnitCommand addUnitCommand() {
318
303
}
319
304
320
305
private void sleep (final int millis ) {
321
- try {
306
+ try {
322
307
Thread .sleep (millis );
323
- }
324
- catch (Exception ignored ) {
308
+ } catch (Exception ignored ) {
325
309
}
326
310
}
327
311
}
0 commit comments