This repository has been archived by the owner on Mar 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 53
/
ludii_game_wrapper.cc
259 lines (207 loc) · 9.16 KB
/
ludii_game_wrapper.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
// Author: Dennis Soemers
// - Affiliation: Maastricht University, DKE, Digital Ludeme Project (Ludii
// developer)
// - Github: https://github.com/DennisSoemers/
// - Email: [email protected] (or [email protected])
#include "ludii_game_wrapper.h"
#include "jni_utils.h"
namespace Ludii {
// NOTE: String descriptions of signatures of Java methods can be found by
// navigating to directory containing the .class files and using:
//
// javap -s <ClassName.class>
LudiiGameWrapper::LudiiGameWrapper(const std::string lud_path) {
JNIEnv* jenv = JNIUtils::GetEnv();
jclass ludiiGameWrapperClass = JNIUtils::LudiiGameWrapperClass();
// Find the LudiiGameWrapper Java construct method
jmethodID ludiiGameWrapperConstruct =
jenv->GetStaticMethodID(ludiiGameWrapperClass, "construct",
"(Ljava/lang/String;)Lutils/LudiiGameWrapper;");
JNIUtils::CheckJniException(jenv);
// Convert our lud path into a Java string
jstring java_lud_path = jenv->NewStringUTF(lud_path.c_str());
JNIUtils::CheckJniException(jenv);
// Call our Java construct method to instantiate new object
jobject local_ref = jenv->CallStaticObjectMethod(
ludiiGameWrapperClass, ludiiGameWrapperConstruct, java_lud_path);
JNIUtils::CheckJniException(jenv);
ludiiGameWrapperJavaObject = jenv->NewGlobalRef(local_ref);
jenv->DeleteLocalRef(local_ref);
// Find method IDs for the two tensor shape Java methods that we may be
// calling frequently
stateTensorsShapeMethodID =
jenv->GetMethodID(ludiiGameWrapperClass, "stateTensorsShape", "()[I");
JNIUtils::CheckJniException(jenv);
moveTensorsShapeMethodID =
jenv->GetMethodID(ludiiGameWrapperClass, "moveTensorsShape", "()[I");
JNIUtils::CheckJniException(jenv);
// Find the method ID for the stateTensorChannelNames() method in Java
stateTensorChannelNamesMethodID =
jenv->GetMethodID(ludiiGameWrapperClass, "stateTensorChannelNames",
"()[Ljava/lang/String;");
JNIUtils::CheckJniException(jenv);
// Find the method ID for the numPlayers() method in Java
numPlayersMethodID =
jenv->GetMethodID(ludiiGameWrapperClass, "numPlayers", "()I");
JNIUtils::CheckJniException(jenv);
// Clean up memory
jenv->DeleteLocalRef(java_lud_path);
JNIUtils::CheckJniException(jenv);
}
LudiiGameWrapper::LudiiGameWrapper(
const std::string lud_path, const std::vector<std::string> game_options) {
JNIEnv* jenv = JNIUtils::GetEnv();
jclass ludiiGameWrapperClass = JNIUtils::LudiiGameWrapperClass();
// Find the LudiiGameWrapper Java construct method (with extra argument for
// options)
jmethodID ludiiGameWrapperConstruct = jenv->GetStaticMethodID(
ludiiGameWrapperClass, "construct",
"(Ljava/lang/String;[Ljava/lang/String;)Lutils/LudiiGameWrapper;");
JNIUtils::CheckJniException(jenv);
// Convert our lud path into a Java string
jstring java_lud_path = jenv->NewStringUTF(lud_path.c_str());
JNIUtils::CheckJniException(jenv);
// Convert vector of game options into array of Java strings
const jobjectArray java_game_options = (jobjectArray)jenv->NewObjectArray(
game_options.size(), jenv->FindClass("java/lang/String"), nullptr);
JNIUtils::CheckJniException(jenv);
for (size_t i = 0; i < game_options.size(); ++i) {
jstring jstr = jenv->NewStringUTF(game_options[i].c_str());
jenv->SetObjectArrayElement(java_game_options, i, jstr);
jenv->DeleteLocalRef(jstr);
}
// Call our Java construct method to instantiate new object
jobject local_ref = jenv->CallStaticObjectMethod(
ludiiGameWrapperClass, ludiiGameWrapperConstruct, java_lud_path,
java_game_options);
JNIUtils::CheckJniException(jenv);
ludiiGameWrapperJavaObject = jenv->NewGlobalRef(local_ref);
jenv->DeleteLocalRef(local_ref);
// Find method IDs for the two tensor shape Java methods that we may be
// calling frequently
stateTensorsShapeMethodID =
jenv->GetMethodID(ludiiGameWrapperClass, "stateTensorsShape", "()[I");
JNIUtils::CheckJniException(jenv);
moveTensorsShapeMethodID =
jenv->GetMethodID(ludiiGameWrapperClass, "moveTensorsShape", "()[I");
JNIUtils::CheckJniException(jenv);
// Find the method ID for the stateTensorChannelNames() method in Java
stateTensorChannelNamesMethodID =
jenv->GetMethodID(ludiiGameWrapperClass, "stateTensorChannelNames",
"()[Ljava/lang/String;");
JNIUtils::CheckJniException(jenv);
// Find the method ID for the numPlayers() method in Java
numPlayersMethodID =
jenv->GetMethodID(ludiiGameWrapperClass, "numPlayers", "()I");
JNIUtils::CheckJniException(jenv);
// Clean up memory
jenv->DeleteLocalRef(java_lud_path);
jenv->DeleteLocalRef(java_game_options);
}
LudiiGameWrapper::LudiiGameWrapper(LudiiGameWrapper const& other) {
JNIEnv* jenv = JNIUtils::GetEnv();
// We can just copy the pointer to the same Java Game object
ludiiGameWrapperJavaObject =
jenv->NewGlobalRef(other.ludiiGameWrapperJavaObject);
JNIUtils::CheckJniException(jenv);
// We can just copy all the pointers to methods
stateTensorsShapeMethodID = other.stateTensorsShapeMethodID;
moveTensorsShapeMethodID = other.moveTensorsShapeMethodID;
stateTensorChannelNamesMethodID = other.stateTensorChannelNamesMethodID;
numPlayersMethodID = other.numPlayersMethodID;
}
LudiiGameWrapper& LudiiGameWrapper::operator=(LudiiGameWrapper const& other) {
JNIEnv* jenv = JNIUtils::GetEnv();
// We can just copy the pointer to the same Java Game object
ludiiGameWrapperJavaObject =
jenv->NewGlobalRef(other.ludiiGameWrapperJavaObject);
JNIUtils::CheckJniException(jenv);
// We can just copy all the pointers to methods
stateTensorsShapeMethodID = other.stateTensorsShapeMethodID;
moveTensorsShapeMethodID = other.moveTensorsShapeMethodID;
stateTensorChannelNamesMethodID = other.stateTensorChannelNamesMethodID;
numPlayersMethodID = other.numPlayersMethodID;
return *this;
}
LudiiGameWrapper::~LudiiGameWrapper() {
JNIEnv* jenv = JNIUtils::GetEnv();
if (jenv) {
jenv->DeleteGlobalRef(ludiiGameWrapperJavaObject);
}
}
const std::array<int, 3>& LudiiGameWrapper::StateTensorsShape() {
if (not stateTensorsShape) {
JNIEnv* jenv = JNIUtils::GetEnv();
// Get our array of Java ints
const jintArray jint_array = static_cast<jintArray>(jenv->CallObjectMethod(
ludiiGameWrapperJavaObject, stateTensorsShapeMethodID));
JNIUtils::CheckJniException(jenv);
jint* jints = jenv->GetIntArrayElements(jint_array, nullptr);
JNIUtils::CheckJniException(jenv);
// Create our C++ array of 3 ints
stateTensorsShape = std::make_unique<std::array<int, 3>>(
std::array<int, 3>{jints[0], jints[1], jints[2]});
// Allow JVM to clean up memory now that we have our own ints
jenv->ReleaseIntArrayElements(jint_array, jints, 0);
jenv->DeleteLocalRef(jint_array);
}
return *stateTensorsShape;
}
const std::array<int, 3>& LudiiGameWrapper::MoveTensorsShape() {
if (not moveTensorsShape) {
JNIEnv* jenv = JNIUtils::GetEnv();
// Get our array of Java ints
const jintArray jint_array = static_cast<jintArray>(jenv->CallObjectMethod(
ludiiGameWrapperJavaObject, moveTensorsShapeMethodID));
JNIUtils::CheckJniException(jenv);
jint* jints = jenv->GetIntArrayElements(jint_array, nullptr);
JNIUtils::CheckJniException(jenv);
// Create our C++ array of 3 ints
moveTensorsShape = std::make_unique<std::array<int, 3>>(
std::array<int, 3>{jints[0], jints[1], jints[2]});
// Allow JVM to clean up memory now that we have our own ints
jenv->ReleaseIntArrayElements(jint_array, jints, 0);
jenv->DeleteLocalRef(jint_array);
}
return *moveTensorsShape;
}
int LudiiGameWrapper::NumPlayers() {
JNIEnv* jenv = JNIUtils::GetEnv();
const int numPlayers =
(int)jenv->CallIntMethod(ludiiGameWrapperJavaObject, numPlayersMethodID);
JNIUtils::CheckJniException(JNIUtils::GetEnv());
return numPlayers;
}
const std::vector<std::string> LudiiGameWrapper::stateTensorChannelNames() {
JNIEnv* jenv = JNIUtils::GetEnv();
std::vector<std::string> channelNames;
const jobjectArray java_arr =
static_cast<jobjectArray>(jenv->CallObjectMethod(
ludiiGameWrapperJavaObject, stateTensorChannelNamesMethodID));
JNIUtils::CheckJniException(jenv);
const int len = jenv->GetArrayLength(java_arr);
JNIUtils::CheckJniException(jenv);
for (int i = 0; i < len; ++i) {
jstring jstr = (jstring)(jenv->GetObjectArrayElement(java_arr, i));
JNIUtils::CheckJniException(jenv);
// Convert Java string to C++ string
const jsize jstr_len = jenv->GetStringUTFLength(jstr);
JNIUtils::CheckJniException(jenv);
const char* chars = jenv->GetStringUTFChars(jstr, (jboolean*)0);
JNIUtils::CheckJniException(jenv);
std::string str(chars, jstr_len);
channelNames.push_back(str);
// Allow JVM to clean up memory
jenv->ReleaseStringUTFChars(jstr, chars);
jenv->DeleteLocalRef(jstr);
}
jenv->DeleteLocalRef(java_arr);
return channelNames;
}
} // namespace Ludii