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