| 1 | /**************************************************************************/ |
| 2 | /* jni_utils.h */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #pragma once |
| 32 | |
| 33 | #include "thread_jandroid.h" |
| 34 | |
| 35 | #include "core/config/engine.h" |
| 36 | #include "core/string/ustring.h" |
| 37 | #include "core/variant/variant.h" |
| 38 | |
| 39 | #include <jni.h> |
| 40 | |
| 41 | jobject _variant_to_jobject(JNIEnv *env, Variant::Type p_type, const Variant *p_arg, int p_depth = 0); |
| 42 | |
| 43 | String _get_class_name(JNIEnv *env, jclass cls, bool *array); |
| 44 | |
| 45 | Variant _jobject_to_variant(JNIEnv *env, jobject obj, int p_depth = 0); |
| 46 | |
| 47 | Variant::Type get_jni_type(const String &p_type); |
| 48 | |
| 49 | /** |
| 50 | * Convert a Godot Callable to a org.godotengine.godot.variant.Callable java object. |
| 51 | * @param p_env JNI environment instance |
| 52 | * @param p_callable Callable parameter to convert. If null or invalid type, a null jobject is returned. |
| 53 | * @return org.godotengine.godot.variant.Callable jobject or null |
| 54 | */ |
| 55 | jobject callable_to_jcallable(JNIEnv *p_env, const Variant &p_callable); |
| 56 | |
| 57 | /** |
| 58 | * Convert a org.godotengine.godot.variant.Callable java object to a Godot Callable variant. |
| 59 | * @param p_env JNI environment instance |
| 60 | * @param p_jcallable_obj org.godotengine.godot.variant.Callable java object to convert. |
| 61 | * @return Callable variant |
| 62 | */ |
| 63 | Callable jcallable_to_callable(JNIEnv *p_env, jobject p_jcallable_obj); |
| 64 | |
| 65 | /** |
| 66 | * Converts a java.lang.CharSequence object to a Godot String. |
| 67 | * @param p_env JNI environment instance |
| 68 | * @param p_charsequence java.lang.CharSequence object to convert |
| 69 | * @return Godot String instance. |
| 70 | */ |
| 71 | String charsequence_to_string(JNIEnv *p_env, jobject p_charsequence); |
| 72 | |
| 73 | /** |
| 74 | * Converts JNI jstring to Godot String. |
| 75 | * @param source Source JNI string. If null an empty string is returned. |
| 76 | * @param env JNI environment instance. If null obtained by get_jni_env(). |
| 77 | * @return Godot string instance. |
| 78 | */ |
| 79 | static inline String jstring_to_string(jstring source, JNIEnv *env = nullptr) { |
| 80 | String result; |
| 81 | if (source) { |
| 82 | if (!env) { |
| 83 | env = get_jni_env(); |
| 84 | } |
| 85 | const char *const source_utf8 = env->GetStringUTFChars(source, nullptr); |
| 86 | if (source_utf8) { |
| 87 | result.append_utf8(source_utf8); |
| 88 | env->ReleaseStringUTFChars(source, source_utf8); |
| 89 | } |
| 90 | } |
| 91 | return result; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Set up thread-safe Android ClassLoader (used by jni_find_class() below). |
| 96 | */ |
| 97 | void setup_android_class_loader(); |
| 98 | void cleanup_android_class_loader(); |
| 99 | |
| 100 | /** |
| 101 | * Thread-safe JNI class finder using Android ClassLoader. |
| 102 | * Works on any thread, unlike standard FindClass which may fail on non-main threads. |
| 103 | * @param p_env JNI environment instance. |
| 104 | * @param p_class_name Class name to find. |
| 105 | * @return jclass reference or null if not found. |
| 106 | */ |
| 107 | jclass jni_find_class(JNIEnv *p_env, const char *p_class_name); |