summaryrefslogtreecommitdiff
path: root/plugins/hello_gdextension/src/main/cpp/jni/utils.h
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/hello_gdextension/src/main/cpp/jni/utils.h')
-rw-r--r--plugins/hello_gdextension/src/main/cpp/jni/utils.h75
1 files changed, 75 insertions, 0 deletions
diff --git a/plugins/hello_gdextension/src/main/cpp/jni/utils.h b/plugins/hello_gdextension/src/main/cpp/jni/utils.h
new file mode 100644
index 0000000..e55cb6b
--- /dev/null
+++ b/plugins/hello_gdextension/src/main/cpp/jni/utils.h
@@ -0,0 +1,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