Skip to content

Commit b814987

Browse files
author
Jasper Geurtz
committed
refactor some utils/client stuff to seperate folder
1 parent d375191 commit b814987

25 files changed

+177
-123
lines changed

pom.xml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
<version>3.2.5</version>
6363
<configuration>
6464
<rerunFailingTestsCount>1</rerunFailingTestsCount>
65-
<argLine>--add-opens java.base/java.nio=ALL-UNNAMED -Xms1g -Xmx1g</argLine>
65+
<argLine>--add-opens java.base/java.nio=ALL-UNNAMED -Xms2g -Xmx2g</argLine>
6666
</configuration>
6767
</plugin>
6868
<plugin>
@@ -96,7 +96,7 @@
9696
<plugin>
9797
<groupId>org.apache.maven.plugins</groupId>
9898
<artifactId>maven-javadoc-plugin</artifactId>
99-
<version>3.4.1</version>
99+
<version>3.11.2</version>
100100
<configuration>
101101
<additionalOptions>-Xdoclint:none</additionalOptions>
102102
<additionalJOption>-Xdoclint:none</additionalJOption>
@@ -121,14 +121,14 @@
121121
<dependency>
122122
<groupId>net.java.dev.jna</groupId>
123123
<artifactId>jna</artifactId>
124-
<version>5.12.1</version>
124+
<version>5.17.0</version>
125125
</dependency>
126126

127127
<!-- https://mvnrepository.com/artifact/net.java.dev.jna/jna-platform -->
128128
<dependency>
129129
<groupId>net.java.dev.jna</groupId>
130130
<artifactId>jna-platform</artifactId>
131-
<version>5.12.1</version>
131+
<version>5.17.0</version>
132132
</dependency>
133133

134134
<!-- https://mvnrepository.com/artifact/junit/junit -->
@@ -151,7 +151,7 @@
151151
<dependency>
152152
<groupId>org.assertj</groupId>
153153
<artifactId>assertj-core</artifactId>
154-
<version>3.23.1</version>
154+
<version>3.27.3</version>
155155
<scope>test</scope>
156156
</dependency>
157157

src/main/java/bwapi/BWClient.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package bwapi;
22

3+
import bwapi.client.Client;
4+
import bwapi.client.PerformanceMetrics;
5+
36
import java.util.Objects;
47

58
/**
@@ -42,7 +45,7 @@ public BWClientConfiguration getConfiguration() {
4245
/**
4346
* @return Whether the current frame should be subject to timing.
4447
*/
45-
boolean doTime() {
48+
public boolean doTime() {
4649
return ! configuration.getUnlimitedFrameZero() || (client.isConnected() && client.liveClientData().gameData().getFrameCount() > 0);
4750
}
4851

src/main/java/bwapi/BWClientConfiguration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ public boolean getLogVerbosely() {
155155
return logVerbosely;
156156
}
157157

158-
void log(String value) {
158+
public void log(String value) {
159159
if (logVerbosely) {
160160
System.out.println(value);
161161
}

src/main/java/bwapi/BotWrapper.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
package bwapi;
22

3+
import bwapi.client.FrameBuffer;
4+
import bwapi.client.PerformanceMetrics;
5+
import bwapi.client.WrappedBuffer;
6+
37
import java.util.concurrent.locks.ReentrantLock;
48

59
/**
610
* Manages invocation of bot event handlers
711
*/
8-
class BotWrapper {
12+
public class BotWrapper {
913
private final ClientData liveClientData = new ClientData();
1014
private final BWClientConfiguration configuration;
1115
private final BWEventListener eventListener;
@@ -23,13 +27,13 @@ class BotWrapper {
2327
BotWrapper(BWClientConfiguration configuration, BWEventListener eventListener) {
2428
this.configuration = configuration;
2529
this.eventListener = eventListener;
26-
frameBuffer = configuration.getAsync() ? new FrameBuffer(configuration) : null;
30+
frameBuffer = configuration.getAsync() ? new FrameBuffer(configuration, ClientData.GameData.SIZE) : null;
2731
}
2832

2933
/**
3034
* Resets the BotWrapper for a new botGame.
3135
*/
32-
void startNewGame(WrappedBuffer liveData, PerformanceMetrics performanceMetrics) {
36+
public void startNewGame(WrappedBuffer liveData, PerformanceMetrics performanceMetrics) {
3337
if (configuration.getAsync()) {
3438
frameBuffer.initialize(liveData, performanceMetrics);
3539
}
@@ -246,7 +250,8 @@ private void handleEvents() {
246250
}
247251

248252
if (configuration.getAsync()) {
249-
performanceMetrics.getFramesBehind().record(Math.max(1, frameBuffer.framesBuffered()) - 1);
253+
performanceMetrics.getFramesBehind().record(
254+
Math.max(1, frameBuffer.framesBuffered()) - 1);
250255
}
251256

252257
performanceMetrics.getBotResponse().timeIf(

src/main/java/bwapi/ClientData.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
package bwapi;
22

3+
import bwapi.client.WrappedBuffer;
34
import com.sun.jna.Pointer;
45

5-
final class ClientData {
6+
public final class ClientData {
67
private WrappedBuffer buffer;
78
private GameData gameData;
8-
ClientData() {
9+
public ClientData() {
910
gameData = new ClientData.GameData(0);
1011
}
11-
GameData gameData() {
12+
public GameData gameData() {
1213
return gameData;
1314
}
14-
void setBuffer(WrappedBuffer buffer) {
15+
public void setBuffer(WrappedBuffer buffer) {
1516
this.buffer = buffer;
1617
}
1718
void setPointer(Pointer pointer) {
@@ -66,13 +67,13 @@ void setExtra(int value) {
6667
buffer.putInt(myOffset + 20, value);
6768
}
6869
}
69-
class GameData {
70-
static final int SIZE = 33017048;
70+
public class GameData {
71+
public static final int SIZE = 33017048;
7172
private int myOffset;
7273
GameData(int myOffset) {
7374
this.myOffset = myOffset;
7475
}
75-
int getClient_version() {
76+
public int getClient_version() {
7677
int offset = myOffset + 0;
7778
return buffer.getInt(offset);
7879
}
@@ -534,7 +535,7 @@ int getNeutral() {
534535
void setNeutral(int value) {
535536
buffer.putInt(myOffset + 10586616, value);
536537
}
537-
int getEventCount() {
538+
public int getEventCount() {
538539
int offset = myOffset + 10586620;
539540
return buffer.getInt(offset);
540541
}

src/main/java/bwapi/Game.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package bwapi;
22

33
import bwapi.ClientData.GameData;
4+
import bwapi.utils.BuildingPlacer;
45

56
import java.util.*;
67
import java.util.stream.Collectors;

src/main/java/bwapi/GameDataUtils.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ of this software and associated documentation files (the "Software"), to deal
3030
* These functions live outside GameData because GameData is auto-generated.
3131
*/
3232
class GameDataUtils {
33-
3433
static final int MAX_COUNT = 19999;
3534
private static final int MAX_STRING_SIZE = 1024;
3635

src/main/java/bwapi/Client.java renamed to src/main/java/bwapi/client/Client.java

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,14 @@ of this software and associated documentation files (the "Software"), to deal
2323
SOFTWARE.
2424
*/
2525

26-
package bwapi;
26+
package bwapi.client;
2727

28-
class Client {
28+
import bwapi.BWClient;
29+
import bwapi.ClientData;
30+
import bwapi.client.*;
31+
32+
33+
public class Client {
2934
private static final int SUPPORTED_BWAPI_VERSION = 10003;
3035

3136
private ClientData clientData;
@@ -35,34 +40,36 @@ class Client {
3540
private WrappedBuffer gameTableShm = null;
3641
private final ClientConnection clientConnector;
3742

38-
Client(BWClient bwClient) {
43+
public Client(BWClient bwClient) {
3944
this.bwClient = bwClient;
4045
boolean windowsOs = System.getProperty("os.name").toLowerCase().contains("win");
41-
clientConnector = windowsOs ? new ClientConnectionW32() : new ClientConnectionPosix();
46+
clientConnector = windowsOs ?
47+
new ClientConnectionW32(ClientData.GameData.SIZE) :
48+
new ClientConnectionPosix(ClientData.GameData.SIZE);
4249
}
4350

4451
/**
4552
* For test purposes only
4653
*/
47-
Client(final WrappedBuffer buffer) {
54+
public Client(final WrappedBuffer buffer) {
4855
clientData = new ClientData();
4956
clientData.setBuffer(buffer);
5057
clientConnector = null;
5158
}
5259

53-
ClientData liveClientData() {
60+
public ClientData liveClientData() {
5461
return clientData;
5562
}
5663

57-
WrappedBuffer mapFile() {
64+
public WrappedBuffer mapFile() {
5865
return mapShm;
5966
}
6067

61-
boolean isConnected() {
68+
public boolean isConnected() {
6269
return connected;
6370
}
6471

65-
void reconnect() {
72+
public void reconnect() {
6673
while (!connect()) {
6774
sleep(1000);
6875
}
@@ -114,7 +121,7 @@ boolean connect() {
114121

115122
int oldest = Integer.MAX_VALUE;
116123
for (int i = 0; i < GameTable.MAX_GAME_INSTANCES; i++) {
117-
GameInstance gameInstance = gameTable.gameInstances[i];
124+
GameTable.GameInstance gameInstance = gameTable.gameInstances[i];
118125
System.out.println(i + " | " + gameInstance.serverProcessID + " | " + (gameInstance.isConnected ? 1 : 0) + " | " + gameInstance.lastKeepAliveTime);
119126
if (gameInstance.serverProcessID != 0 && !gameInstance.isConnected) {
120127
if (gameTableIndex == -1 || gameInstance.lastKeepAliveTime < oldest) {
@@ -193,7 +200,7 @@ boolean connect() {
193200
return true;
194201
}
195202

196-
void sendFrameReceiveFrame() {
203+
public void sendFrameReceiveFrame() {
197204
final PerformanceMetrics metrics = bwClient.getPerformanceMetrics();
198205

199206
// Tell BWAPI that we are done with the current frame

src/main/java/bwapi/ClientConnection.java renamed to src/main/java/bwapi/client/ClientConnection.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
package bwapi;
1+
package bwapi.client;
22

33
import java.io.IOException;
44

55
/**
66
* Client - Server connection abstraction
77
*/
8-
interface ClientConnection {
8+
public interface ClientConnection {
99
void disconnect();
1010

1111
WrappedBuffer getGameTable();

src/main/java/bwapi/ClientConnectionPosix.java renamed to src/main/java/bwapi/client/ClientConnectionPosix.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package bwapi;
1+
package bwapi.client;
22

33
import com.sun.jna.LastErrorException;
44
import com.sun.jna.Library;
@@ -20,7 +20,7 @@
2020
* To be used with OpenBWs BWAPI including server support. At time of writing, only the following fork includes this:
2121
* https://github.com/basil-ladder/openbw
2222
*/
23-
class ClientConnectionPosix implements ClientConnection {
23+
public class ClientConnectionPosix implements ClientConnection {
2424
interface LibCExt extends LibC {
2525
LibCExt INSTANCE = Native.load(LibCExt.class);
2626

@@ -40,6 +40,11 @@ interface PosixShm extends Library {
4040
}
4141

4242
private AFUNIXSocket syncSocket = null;
43+
private final int gameDataSize;
44+
45+
public ClientConnectionPosix(int gameDataSize) {
46+
this.gameDataSize = gameDataSize;
47+
}
4348

4449
@Override
4550
public void disconnect() {
@@ -65,9 +70,13 @@ public WrappedBuffer getGameTable() {
6570
public WrappedBuffer getSharedMemory(int serverProcID) {
6671
String sharedMemoryName = "/bwapi_shared_memory_" + serverProcID;
6772
try {
68-
Pointer mapFileView = LibCExt.INSTANCE.mmap(Pointer.NULL, ClientData.GameData.SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, LibRT.INSTANCE.shm_open(sharedMemoryName, Fcntl.O_RDWR, 0),
73+
Pointer mapFileView = LibCExt.INSTANCE.mmap(
74+
Pointer.NULL,
75+
gameDataSize,
76+
PROT_READ | PROT_WRITE, MAP_SHARED,
77+
LibRT.INSTANCE.shm_open(sharedMemoryName, Fcntl.O_RDWR, 0),
6978
0);
70-
return new WrappedBuffer(mapFileView, ClientData.GameData.SIZE);
79+
return new WrappedBuffer(mapFileView, gameDataSize);
7180
} catch (Exception e) {
7281
throw new SharedMemoryConnectionException(sharedMemoryName, e);
7382
}

src/main/java/bwapi/ClientConnectionW32.java renamed to src/main/java/bwapi/client/ClientConnectionW32.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package bwapi;
1+
package bwapi.client;
22

33
import com.sun.jna.Native;
44
import com.sun.jna.Pointer;
@@ -12,7 +12,7 @@
1212
/**
1313
* Default Windows BWAPI pipe connection with shared memory.
1414
*/
15-
class ClientConnectionW32 implements ClientConnection {
15+
public class ClientConnectionW32 implements ClientConnection {
1616
private static final int READ_WRITE = 0x1 | 0x2 | 0x4;
1717

1818
interface MappingKernel extends Kernel32 {
@@ -23,6 +23,11 @@ interface MappingKernel extends Kernel32 {
2323

2424

2525
private RandomAccessFile pipeObjectHandle = null;
26+
private final int gameDataSize;
27+
28+
public ClientConnectionW32(int gameDataSize) {
29+
this.gameDataSize = gameDataSize;
30+
}
2631

2732
@Override
2833
public void disconnect() {
@@ -50,8 +55,8 @@ public WrappedBuffer getSharedMemory(int serverProcID) {
5055
try {
5156
final Pointer mapFileView = Kernel32.INSTANCE.MapViewOfFile(MappingKernel.INSTANCE
5257
.OpenFileMapping(READ_WRITE, false, sharedMemoryName), READ_WRITE,
53-
0, 0, ClientData.GameData.SIZE);
54-
return new WrappedBuffer(mapFileView, ClientData.GameData.SIZE);
58+
0, 0, gameDataSize);
59+
return new WrappedBuffer(mapFileView, gameDataSize);
5560
} catch (Exception e) {
5661
throw new SharedMemoryConnectionException(sharedMemoryName, e);
5762
}

0 commit comments

Comments
 (0)