@@ -47,8 +47,7 @@ void android_main(android_app *app)
47
47
Thread::setName (" Main" );
48
48
49
49
try {
50
- app_dummy ();
51
- char *argv[] = {strdup (PROJECT_NAME), NULL };
50
+ char *argv[] = {strdup (PROJECT_NAME), nullptr };
52
51
main (ARRLEN (argv) - 1 , argv);
53
52
free (argv[0 ]);
54
53
} catch (std::exception &e) {
@@ -64,85 +63,73 @@ void android_main(android_app *app)
64
63
exit (retval);
65
64
}
66
65
67
- /* handler for finished message box input */
68
- /* Intentionally NOT in namespace porting */
69
- /* TODO this doesn't work as expected, no idea why but there's a workaround */
70
- /* for it right now */
66
+ /* *
67
+ * Handler for finished message box input
68
+ * Intentionally NOT in namespace porting
69
+ * ToDo: this doesn't work as expected, there's a workaround for it right now
70
+ */
71
71
extern " C" {
72
72
JNIEXPORT void JNICALL Java_net_minetest_minetest_GameActivity_putMessageBoxResult (
73
- JNIEnv * env, jclass thiz, jstring text)
73
+ JNIEnv *env, jclass thiz, jstring text)
74
74
{
75
- errorstream << " Java_net_minetest_minetest_GameActivity_putMessageBoxResult got: "
76
- << std::string (( const char *)env-> GetStringChars (text, 0 ))
77
- << std::endl;
75
+ errorstream <<
76
+ " Java_net_minetest_minetest_GameActivity_putMessageBoxResult got: " <<
77
+ std::string (( const char *) env-> GetStringChars (text, nullptr )) << std::endl;
78
78
}
79
79
}
80
80
81
81
namespace porting {
82
-
83
- std::string path_storage = DIR_DELIM " sdcard" DIR_DELIM;
84
-
85
- android_app* app_global;
86
- JNIEnv* jnienv;
82
+ android_app *app_global;
83
+ JNIEnv *jnienv;
87
84
jclass nativeActivity;
88
85
89
- jclass findClass (std::string classname)
86
+ jclass findClass (const std::string & classname)
90
87
{
91
- if (jnienv == 0 ) {
92
- return 0 ;
93
- }
88
+ if (jnienv == nullptr )
89
+ return nullptr ;
94
90
95
91
jclass nativeactivity = jnienv->FindClass (" android/app/NativeActivity" );
96
- jmethodID getClassLoader =
97
- jnienv->GetMethodID (nativeactivity," getClassLoader" ,
98
- " ()Ljava/lang/ClassLoader;" );
99
- jobject cls =
100
- jnienv->CallObjectMethod (app_global->activity ->clazz , getClassLoader);
92
+ jmethodID getClassLoader = jnienv->GetMethodID (
93
+ nativeactivity, " getClassLoader" , " ()Ljava/lang/ClassLoader;" );
94
+ jobject cls = jnienv->CallObjectMethod (
95
+ app_global->activity ->clazz , getClassLoader);
101
96
jclass classLoader = jnienv->FindClass (" java/lang/ClassLoader" );
102
- jmethodID findClass =
103
- jnienv->GetMethodID (classLoader, " loadClass" ,
97
+ jmethodID findClass = jnienv->GetMethodID (classLoader, " loadClass" ,
104
98
" (Ljava/lang/String;)Ljava/lang/Class;" );
105
- jstring strClassName =
106
- jnienv->NewStringUTF (classname.c_str ());
99
+ jstring strClassName = jnienv->NewStringUTF (classname.c_str ());
107
100
return (jclass) jnienv->CallObjectMethod (cls, findClass, strClassName);
108
101
}
109
102
110
103
void initAndroid ()
111
104
{
112
- porting::jnienv = NULL ;
105
+ porting::jnienv = nullptr ;
113
106
JavaVM *jvm = app_global->activity ->vm ;
114
107
JavaVMAttachArgs lJavaVMAttachArgs;
115
108
lJavaVMAttachArgs.version = JNI_VERSION_1_6;
116
109
lJavaVMAttachArgs.name = PROJECT_NAME_C " NativeThread" ;
117
- lJavaVMAttachArgs.group = NULL ;
118
- #ifdef NDEBUG
119
- // This is a ugly hack as arm v7a non debuggable builds crash without this
120
- // printf ... if someone finds out why please fix it!
121
- infostream << " Attaching native thread. " << std::endl;
122
- #endif
123
- if ( jvm->AttachCurrentThread (&porting::jnienv, &lJavaVMAttachArgs) == JNI_ERR) {
110
+ lJavaVMAttachArgs.group = nullptr ;
111
+
112
+ if (jvm->AttachCurrentThread (&porting::jnienv, &lJavaVMAttachArgs) == JNI_ERR) {
124
113
errorstream << " Failed to attach native thread to jvm" << std::endl;
125
114
exit (-1 );
126
115
}
127
116
128
117
nativeActivity = findClass (" net/minetest/minetest/GameActivity" );
129
- if (nativeActivity == 0 ) {
118
+ if (nativeActivity == nullptr )
130
119
errorstream <<
131
120
" porting::initAndroid unable to find java native activity class" <<
132
121
std::endl;
133
- }
134
122
135
123
#ifdef GPROF
136
- /* in the start-up code */
124
+ // in the start-up code
137
125
__android_log_print (ANDROID_LOG_ERROR, PROJECT_NAME_C,
138
126
" Initializing GPROF profiler" );
139
- monstartup (" libminetest .so" );
127
+ monstartup (" libMinetest .so" );
140
128
#endif
141
129
}
142
130
143
131
void cleanupAndroid ()
144
132
{
145
-
146
133
#ifdef GPROF
147
134
errorstream << " Shutting down GPROF profiler" << std::endl;
148
135
setenv (" CPUPROFILE" , (path_user + DIR_DELIM + " gmon.out" ).c_str (), 1 );
@@ -157,7 +144,7 @@ static std::string javaStringToUTF8(jstring js)
157
144
{
158
145
std::string str;
159
146
// Get string as a UTF-8 c-string
160
- const char *c_str = jnienv->GetStringUTFChars (js, NULL );
147
+ const char *c_str = jnienv->GetStringUTFChars (js, nullptr );
161
148
// Save it
162
149
str = c_str;
163
150
// And free the c-string
@@ -166,17 +153,15 @@ static std::string javaStringToUTF8(jstring js)
166
153
}
167
154
168
155
// Calls static method if obj is NULL
169
- static std::string getAndroidPath (jclass cls, jobject obj, jclass cls_File,
170
- jmethodID mt_getAbsPath, const char *getter)
156
+ static std::string getAndroidPath (
157
+ jclass cls, jobject obj, jmethodID mt_getAbsPath, const char *getter)
171
158
{
172
159
// Get getter method
173
160
jmethodID mt_getter;
174
161
if (obj)
175
- mt_getter = jnienv->GetMethodID (cls, getter,
176
- " ()Ljava/io/File;" );
162
+ mt_getter = jnienv->GetMethodID (cls, getter, " ()Ljava/io/File;" );
177
163
else
178
- mt_getter = jnienv->GetStaticMethodID (cls, getter,
179
- " ()Ljava/io/File;" );
164
+ mt_getter = jnienv->GetStaticMethodID (cls, getter, " ()Ljava/io/File;" );
180
165
181
166
// Call getter
182
167
jobject ob_file;
@@ -186,8 +171,7 @@ static std::string getAndroidPath(jclass cls, jobject obj, jclass cls_File,
186
171
ob_file = jnienv->CallStaticObjectMethod (cls, mt_getter);
187
172
188
173
// Call getAbsolutePath
189
- jstring js_path = (jstring) jnienv->CallObjectMethod (ob_file,
190
- mt_getAbsPath);
174
+ auto js_path = (jstring) jnienv->CallObjectMethod (ob_file, mt_getAbsPath);
191
175
192
176
return javaStringToUTF8 (js_path);
193
177
}
@@ -201,26 +185,24 @@ void initializePathsAndroid()
201
185
// Get getAbsolutePath method
202
186
jmethodID mt_getAbsPath = jnienv->GetMethodID (cls_File,
203
187
" getAbsolutePath" , " ()Ljava/lang/String;" );
188
+ std::string path_storage = getAndroidPath (cls_Env, nullptr ,
189
+ mt_getAbsPath, " getExternalStorageDirectory" );
204
190
205
- path_cache = getAndroidPath (nativeActivity, app_global->activity ->clazz ,
206
- cls_File, mt_getAbsPath, " getCacheDir" );
207
- path_storage = getAndroidPath (cls_Env, NULL , cls_File, mt_getAbsPath,
208
- " getExternalStorageDirectory" );
209
191
path_user = path_storage + DIR_DELIM + PROJECT_NAME_C;
210
192
path_share = path_storage + DIR_DELIM + PROJECT_NAME_C;
211
-
193
+ path_cache = getAndroidPath (nativeActivity,
194
+ app_global->activity ->clazz , mt_getAbsPath, " getCacheDir" );
212
195
migrateCachePath ();
213
196
}
214
197
215
- void showInputDialog (const std::string& acceptButton, const std::string& hint,
216
- const std::string& current, int editType)
198
+ void showInputDialog (const std::string & acceptButton, const std::string & hint,
199
+ const std::string & current, int editType)
217
200
{
218
- jmethodID showdialog = jnienv->GetMethodID (nativeActivity," showDialog" ,
201
+ jmethodID showdialog = jnienv->GetMethodID (nativeActivity, " showDialog" ,
219
202
" (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;I)V" );
220
203
221
- if (showdialog == 0 ) {
222
- assert (" porting::showInputDialog unable to find java show dialog method" == 0 );
223
- }
204
+ FATAL_ERROR_IF (showdialog == nullptr ,
205
+ " porting::showInputDialog unable to find java show dialog method" );
224
206
225
207
jstring jacceptButton = jnienv->NewStringUTF (acceptButton.c_str ());
226
208
jstring jhint = jnienv->NewStringUTF (hint.c_str ());
@@ -236,9 +218,8 @@ int getInputDialogState()
236
218
jmethodID dialogstate = jnienv->GetMethodID (nativeActivity,
237
219
" getDialogState" , " ()I" );
238
220
239
- if (dialogstate == 0 ) {
240
- assert (" porting::getInputDialogState unable to find java dialog state method" == 0 );
241
- }
221
+ FATAL_ERROR_IF (dialogstate == nullptr ,
222
+ " porting::getInputDialogState unable to find java dialog state method" );
242
223
243
224
return jnienv->CallIntMethod (app_global->activity ->clazz , dialogstate);
244
225
}
@@ -248,14 +229,13 @@ std::string getInputDialogValue()
248
229
jmethodID dialogvalue = jnienv->GetMethodID (nativeActivity,
249
230
" getDialogValue" , " ()Ljava/lang/String;" );
250
231
251
- if (dialogvalue == 0 ) {
252
- assert (" porting::getInputDialogValue unable to find java dialog value method" == 0 );
253
- }
232
+ FATAL_ERROR_IF (dialogvalue == nullptr ,
233
+ " porting::getInputDialogValue unable to find java dialog value method" );
254
234
255
235
jobject result = jnienv->CallObjectMethod (app_global->activity ->clazz ,
256
236
dialogvalue);
257
237
258
- const char * javachars = jnienv->GetStringUTFChars ((jstring) result,0 );
238
+ const char * javachars = jnienv->GetStringUTFChars ((jstring) result, nullptr );
259
239
std::string text (javachars);
260
240
jnienv->ReleaseStringUTFChars ((jstring) result, javachars);
261
241
@@ -269,12 +249,11 @@ float getDisplayDensity()
269
249
static float value = 0 ;
270
250
271
251
if (firstrun) {
272
- jmethodID getDensity = jnienv->GetMethodID (nativeActivity, " getDensity " ,
273
- " ()F" );
252
+ jmethodID getDensity = jnienv->GetMethodID (nativeActivity,
253
+ " getDensity " , " ()F" );
274
254
275
- if (getDensity == 0 ) {
276
- assert (" porting::getDisplayDensity unable to find java getDensity method" == 0 );
277
- }
255
+ FATAL_ERROR_IF (getDensity == nullptr ,
256
+ " porting::getDisplayDensity unable to find java getDensity method" );
278
257
279
258
value = jnienv->CallFloatMethod (app_global->activity ->clazz , getDensity);
280
259
firstrun = false ;
@@ -291,19 +270,17 @@ v2u32 getDisplaySize()
291
270
jmethodID getDisplayWidth = jnienv->GetMethodID (nativeActivity,
292
271
" getDisplayWidth" , " ()I" );
293
272
294
- if (getDisplayWidth == 0 ) {
295
- assert (" porting::getDisplayWidth unable to find java getDisplayWidth method" == 0 );
296
- }
273
+ FATAL_ERROR_IF (getDisplayWidth == nullptr ,
274
+ " porting::getDisplayWidth unable to find java getDisplayWidth method" );
297
275
298
276
retval.X = jnienv->CallIntMethod (app_global->activity ->clazz ,
299
277
getDisplayWidth);
300
278
301
279
jmethodID getDisplayHeight = jnienv->GetMethodID (nativeActivity,
302
280
" getDisplayHeight" , " ()I" );
303
281
304
- if (getDisplayHeight == 0 ) {
305
- assert (" porting::getDisplayHeight unable to find java getDisplayHeight method" == 0 );
306
- }
282
+ FATAL_ERROR_IF (getDisplayHeight == nullptr ,
283
+ " porting::getDisplayHeight unable to find java getDisplayHeight method" );
307
284
308
285
retval.Y = jnienv->CallIntMethod (app_global->activity ->clazz ,
309
286
getDisplayHeight);
0 commit comments