@@ -29,12 +29,11 @@ of this software and associated documentation files (the "Software"), to deal
2929import bwapi .ClientData .GameData ;
3030import bwapi .ClientData .Shape ;
3131import com .sun .jna .Native ;
32+ import com .sun .jna .Pointer ;
3233import com .sun .jna .platform .win32 .Kernel32 ;
3334import com .sun .jna .win32 .W32APIOptions ;
3435
3536import java .io .RandomAccessFile ;
36- import java .nio .ByteBuffer ;
37- import java .nio .ByteOrder ;
3837
3938class Client {
4039 interface MappingKernel extends Kernel32 {
@@ -51,14 +50,13 @@ public interface EventHandler {
5150
5251 private static final int SUPPORTED_BWAPI_VERSION = 10003 ;
5352 static final int MAX_COUNT = 19999 ;
54- static final int MAX_STRING_SIZE = 1024 ;
5553
5654 private ClientData clientData ;
5755 private ClientData .GameData gameData ;
5856 private boolean connected = false ;
5957 private RandomAccessFile pipeObjectHandle = null ;
60- private ByteBuffer mapFileHandle = null ;
61- private ByteBuffer gameTableFileHandle = null ;
58+ private WrappedBuffer mapFileHandle = null ;
59+ private WrappedBuffer gameTableFileHandle = null ;
6260
6361 private boolean debugConnection = false ;
6462
@@ -69,7 +67,7 @@ public interface EventHandler {
6967 /**
7068 * For test purposes only
7169 */
72- Client (ByteBuffer buffer ) {
70+ Client (final WrappedBuffer buffer ) {
7371 clientData = new ClientData (buffer );
7472 gameData = clientData .new GameData (0 );
7573 }
@@ -86,7 +84,7 @@ boolean isConnected() {
8684 return connected ;
8785 }
8886
89- void reconnect (){
87+ void reconnect () {
9088 while (!connect ()) {
9189 sleep (1000 );
9290 }
@@ -101,11 +99,10 @@ void disconnect() {
10199 return ;
102100 }
103101
104- if (pipeObjectHandle != null ) {
102+ if (pipeObjectHandle != null ) {
105103 try {
106104 pipeObjectHandle .close ();
107- }
108- catch (Exception e ) {
105+ } catch (Exception e ) {
109106 e .printStackTrace ();
110107 }
111108 pipeObjectHandle = null ;
@@ -127,21 +124,19 @@ boolean connect() {
127124 int gameTableIndex = -1 ;
128125
129126 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 ) {
136132 System .err .println ("Game table mapping not found." );
137133 return false ;
138134 }
139135
140136 GameTable gameTable ;
141137 try {
142138 gameTable = new GameTable (gameTableFileHandle );
143- }
144- catch (Exception e ) {
139+ } catch (Exception e ) {
145140 System .err .println ("Unable to map Game table." );
146141 if (debugConnection ) {
147142 e .printStackTrace ();
@@ -150,11 +145,11 @@ boolean connect() {
150145 }
151146
152147 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 ++) {
154149 GameInstance gameInstance = gameTable .gameInstances [i ];
155150 System .out .println (i + " | " + gameInstance .serverProcessID + " | " + (gameInstance .isConnected ? 1 : 0 ) + " | " + gameInstance .lastKeepAliveTime );
156151 if (gameInstance .serverProcessID != 0 && !gameInstance .isConnected ) {
157- if ( gameTableIndex == -1 || latest == 0 || gameInstance .lastKeepAliveTime < latest ) {
152+ if (gameTableIndex == -1 || latest == 0 || gameInstance .lastKeepAliveTime < latest ) {
158153 latest = gameInstance .lastKeepAliveTime ;
159154 gameTableIndex = i ;
160155 }
@@ -173,9 +168,8 @@ boolean connect() {
173168 final String sharedMemoryName = "Local\\ bwapi_shared_memory_" + serverProcID ;
174169 final String communicationPipe = "\\ \\ .\\ pipe\\ bwapi_pipe_" + serverProcID ;
175170 try {
176- pipeObjectHandle = new RandomAccessFile (communicationPipe , "rw" );
177- }
178- catch (Exception e ) {
171+ pipeObjectHandle = new RandomAccessFile (communicationPipe , "rw" );
172+ } catch (Exception e ) {
179173 System .err .println ("Unable to open communications pipe: " + communicationPipe );
180174 if (debugConnection ) {
181175 e .printStackTrace ();
@@ -186,11 +180,11 @@ boolean connect() {
186180 System .out .println ("Connected" );
187181
188182 try {
189- mapFileHandle = Kernel32 .INSTANCE .MapViewOfFile (MappingKernel .INSTANCE
183+ final Pointer mapFileView = Kernel32 .INSTANCE .MapViewOfFile (MappingKernel .INSTANCE
190184 .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 ) {
194188 System .err .println ("Unable to open shared memory mapping: " + sharedMemoryName );
195189 if (debugConnection ) {
196190 e .printStackTrace ();
@@ -202,8 +196,7 @@ boolean connect() {
202196 try {
203197 clientData = new ClientData (mapFileHandle );
204198 gameData = clientData .new GameData (0 );
205- }
206- catch (Exception e ) {
199+ } catch (Exception e ) {
207200 System .err .println ("Unable to map game data." );
208201 if (debugConnection ) {
209202 e .printStackTrace ();
@@ -223,8 +216,7 @@ boolean connect() {
223216 while (code != 2 ) {
224217 try {
225218 code = pipeObjectHandle .readByte ();
226- }
227- catch (Exception e ) {
219+ } catch (Exception e ) {
228220 System .err .println ("Unable to read pipe object." );
229221 if (debugConnection ) {
230222 e .printStackTrace ();
@@ -243,8 +235,7 @@ void update(final EventHandler handler) {
243235 byte code = 1 ;
244236 try {
245237 pipeObjectHandle .writeByte (code );
246- }
247- catch (Exception e ) {
238+ } catch (Exception e ) {
248239 System .err .println ("failed, disconnecting" );
249240 if (debugConnection ) {
250241 e .printStackTrace ();
@@ -255,8 +246,7 @@ void update(final EventHandler handler) {
255246 while (code != 2 ) {
256247 try {
257248 code = pipeObjectHandle .readByte ();
258- }
259- catch (Exception e ) {
249+ } catch (Exception e ) {
260250 System .err .println ("failed, disconnecting" );
261251 if (debugConnection ) {
262252 e .printStackTrace ();
@@ -280,13 +270,8 @@ int addString(final String string) {
280270 throw new IllegalStateException ("Too many strings!" );
281271 }
282272
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-
288273 gameData .setStringCount (stringCount + 1 );
289- gameData .setStrings (stringCount , stringTruncated );
274+ gameData .setStrings (stringCount , string );
290275 return stringCount ;
291276 }
292277
@@ -318,10 +303,9 @@ ClientData.UnitCommand addUnitCommand() {
318303 }
319304
320305 private void sleep (final int millis ) {
321- try {
306+ try {
322307 Thread .sleep (millis );
323- }
324- catch (Exception ignored ) {
308+ } catch (Exception ignored ) {
325309 }
326310 }
327311}
0 commit comments