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
|
#ifndef UTILS_H
#define UTILS_H
#include <android/log.h>
#include <godot_cpp/variant/string.hpp>
#include <jni.h>
#define LOG_TAG "HelloGDExtension"
#define ALOG_ASSERT(_cond, ...) \
if (!(_cond)) __android_log_assert("conditional", LOG_TAG, __VA_ARGS__)
#define ALOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)
#define ALOGW(...) __android_log_print(ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__)
#define ALOGV(...) __android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__)
/** Auxiliary macros */
#define __JNI_METHOD_BUILD(package, class_name, method) \
Java_ ## package ## _ ## class_name ## _ ## method
#define __JNI_METHOD_EVAL(package, class_name, method) \
__JNI_METHOD_BUILD(package, class_name, method)
/**
* Expands the JNI signature for a JNI method.
*
* Requires to redefine the macros JNI_PACKAGE_NAME and JNI_CLASS_NAME.
* Not doing so will raise preprocessor errors during build.
*
* JNI_PACKAGE_NAME must be the JNI representation Java class package name.
* JNI_CLASS_NAME must be the JNI representation of the Java class name.
*
* For example, for the class com.example.package.SomeClass:
* JNI_PACKAGE_NAME: com_example_package
* JNI_CLASS_NAME: SomeClass
*
* Note that underscores in Java package and class names are replaced with "_1"
* in their JNI representations.
*/
#define JNI_METHOD(method) \
__JNI_METHOD_EVAL(JNI_PACKAGE_NAME, JNI_CLASS_NAME, method)
/**
* Expands a Java class name using slashes as package separators into its
* JNI type string representation.
*
* For example, to get the JNI type representation of a Java String:
* JAVA_TYPE("java/lang/String")
*/
#define JAVA_TYPE(class_name) "L" class_name ";"
/**
* Default definitions for the macros required in JNI_METHOD.
* Used to raise build errors if JNI_METHOD is used without redefining them.
*/
#define JNI_CLASS_NAME "Error: JNI_CLASS_NAME not redefined"
#define JNI_PACKAGE_NAME "Error: JNI_PACKAGE_NAME not redefined"
/**
* Converts JNI jstring to Godot String.
* @param source Source JNI string. If null an empty string is returned.
* @param env JNI environment instance.
* @return Godot string instance.
*/
static inline godot::String jstring_to_string(JNIEnv *env, jstring source) {
if (env && source) {
const char *const source_utf8 = env->GetStringUTFChars(source, NULL);
if (source_utf8) {
godot::String result(source_utf8);
env->ReleaseStringUTFChars(source, source_utf8);
return result;
}
}
return godot::String();
}
#endif // UTILS_H
|