| 1 | /**************************************************************************/ |
| 2 | /* godot_plugin_jni.cpp */ |
| 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 | #include "godot_plugin_jni.h" |
| 32 | |
| 33 | #include "api/java_class_wrapper.h" |
| 34 | #include "api/jni_singleton.h" |
| 35 | #include "jni_utils.h" |
| 36 | |
| 37 | #include "core/config/engine.h" |
| 38 | #include "core/error/error_macros.h" |
| 39 | |
| 40 | static HashMap<String, JNISingleton *> jni_singletons; |
| 41 | |
| 42 | void unregister_plugins_singletons() { |
| 43 | for (const KeyValue<String, JNISingleton *> &E : jni_singletons) { |
| 44 | Engine::get_singleton()->remove_singleton(E.key); |
| 45 | |
| 46 | if (E.value) { |
| 47 | memdelete(E.value); |
| 48 | } |
| 49 | } |
| 50 | jni_singletons.clear(); |
| 51 | } |
| 52 | |
| 53 | extern "C" { |
| 54 | |
| 55 | JNIEXPORT jboolean JNICALL Java_org_godotengine_godot_plugin_GodotPlugin_nativeRegisterSingleton(JNIEnv *env, jclass clazz, jstring name, jobject obj) { |
| 56 | String singname = jstring_to_string(name, env); |
| 57 | |
| 58 | ERR_FAIL_COND_V(jni_singletons.has(singname), false); |
| 59 | |
| 60 | jclass java_class = env->GetObjectClass(obj); |
| 61 | Ref<JavaClass> java_class_wrapped = JavaClassWrapper::get_singleton()->wrap_jclass(java_class, true); |
| 62 | env->DeleteLocalRef(java_class); |
| 63 | |
| 64 | Ref<JavaObject> plugin_object = memnew(JavaObject(java_class_wrapped, obj)); |
| 65 | JNISingleton *plugin_singleton = memnew(JNISingleton(plugin_object)); |
| 66 | jni_singletons[singname] = plugin_singleton; |
| 67 | |
| 68 | Engine::get_singleton()->add_singleton(Engine::Singleton(singname, plugin_singleton)); |
| 69 | return true; |
| 70 | } |
| 71 | |
| 72 | JNIEXPORT void JNICALL Java_org_godotengine_godot_plugin_GodotPlugin_nativeRegisterMethod(JNIEnv *env, jclass clazz, jstring sname, jstring name, jstring ret, jobjectArray args) { |
| 73 | String singname = jstring_to_string(sname, env); |
| 74 | |
| 75 | ERR_FAIL_COND(!jni_singletons.has(singname)); |
| 76 | |
| 77 | JNISingleton *s = jni_singletons.get(singname); |
| 78 | |
| 79 | String mname = jstring_to_string(name, env); |
| 80 | String retval = jstring_to_string(ret, env); |
| 81 | Vector<Variant::Type> types; |
| 82 | |
| 83 | int stringCount = env->GetArrayLength(args); |
| 84 | |
| 85 | for (int i = 0; i < stringCount; i++) { |
| 86 | jstring string = (jstring)env->GetObjectArrayElement(args, i); |
| 87 | const String rawString = jstring_to_string(string, env); |
| 88 | types.push_back(get_jni_type(rawString)); |
| 89 | } |
| 90 | |
| 91 | s->add_method(mname, types, get_jni_type(retval)); |
| 92 | } |
| 93 | |
| 94 | JNIEXPORT void JNICALL Java_org_godotengine_godot_plugin_GodotPlugin_nativeRegisterSignal(JNIEnv *env, jclass clazz, jstring j_plugin_name, jstring j_signal_name, jobjectArray j_signal_param_types) { |
| 95 | String singleton_name = jstring_to_string(j_plugin_name, env); |
| 96 | |
| 97 | ERR_FAIL_COND(!jni_singletons.has(singleton_name)); |
| 98 | |
| 99 | JNISingleton *singleton = jni_singletons.get(singleton_name); |
| 100 | |
| 101 | String signal_name = jstring_to_string(j_signal_name, env); |
| 102 | Vector<Variant::Type> types; |
| 103 | |
| 104 | int stringCount = env->GetArrayLength(j_signal_param_types); |
| 105 | |
| 106 | for (int i = 0; i < stringCount; i++) { |
| 107 | jstring j_signal_param_type = (jstring)env->GetObjectArrayElement(j_signal_param_types, i); |
| 108 | const String signal_param_type = jstring_to_string(j_signal_param_type, env); |
| 109 | types.push_back(get_jni_type(signal_param_type)); |
| 110 | } |
| 111 | |
| 112 | singleton->add_signal(signal_name, types); |
| 113 | } |
| 114 | |
| 115 | JNIEXPORT void JNICALL Java_org_godotengine_godot_plugin_GodotPlugin_nativeEmitSignal(JNIEnv *env, jclass clazz, jstring j_plugin_name, jstring j_signal_name, jobjectArray j_signal_params) { |
| 116 | String singleton_name = jstring_to_string(j_plugin_name, env); |
| 117 | |
| 118 | ERR_FAIL_COND(!jni_singletons.has(singleton_name)); |
| 119 | |
| 120 | JNISingleton *singleton = jni_singletons.get(singleton_name); |
| 121 | |
| 122 | String signal_name = jstring_to_string(j_signal_name, env); |
| 123 | |
| 124 | int count = env->GetArrayLength(j_signal_params); |
| 125 | |
| 126 | Variant *variant_params = (Variant *)alloca(sizeof(Variant) * count); |
| 127 | const Variant **args = (const Variant **)alloca(sizeof(Variant *) * count); |
| 128 | |
| 129 | for (int i = 0; i < count; i++) { |
| 130 | jobject j_param = env->GetObjectArrayElement(j_signal_params, i); |
| 131 | memnew_placement(&variant_params[i], Variant(_jobject_to_variant(env, j_param))); |
| 132 | args[i] = &variant_params[i]; |
| 133 | env->DeleteLocalRef(j_param); |
| 134 | } |
| 135 | |
| 136 | singleton->emit_signalp(StringName(signal_name), args, count); |
| 137 | |
| 138 | // Manually invoke the destructor to decrease the reference counts for the variant arguments. |
| 139 | for (int i = 0; i < count; i++) { |
| 140 | variant_params[i].~Variant(); |
| 141 | } |
| 142 | } |
| 143 | } |