jni_singleton.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*************************************************************************/
  2. /* jni_singleton.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  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. #ifndef JNI_SINGLETON_H
  31. #define JNI_SINGLETON_H
  32. #include <core/config/engine.h>
  33. #include <core/variant/variant.h>
  34. #ifdef ANDROID_ENABLED
  35. #include <platform/android/jni_utils.h>
  36. #endif
  37. class JNISingleton : public Object {
  38. GDCLASS(JNISingleton, Object);
  39. #ifdef ANDROID_ENABLED
  40. struct MethodData {
  41. jmethodID method;
  42. Variant::Type ret_type;
  43. Vector<Variant::Type> argtypes;
  44. };
  45. jobject instance;
  46. Map<StringName, MethodData> method_map;
  47. #endif
  48. public:
  49. virtual Variant call(const StringName &p_method, const Variant **p_args, int p_argcount, Callable::CallError &r_error) override {
  50. #ifdef ANDROID_ENABLED
  51. Map<StringName, MethodData>::Element *E = method_map.find(p_method);
  52. // Check the method we're looking for is in the JNISingleton map and that
  53. // the arguments match.
  54. bool call_error = !E || E->get().argtypes.size() != p_argcount;
  55. if (!call_error) {
  56. for (int i = 0; i < p_argcount; i++) {
  57. if (!Variant::can_convert(p_args[i]->get_type(), E->get().argtypes[i])) {
  58. call_error = true;
  59. break;
  60. }
  61. }
  62. }
  63. if (call_error) {
  64. // The method is not in this map, defaulting to the regular instance calls.
  65. return Object::call(p_method, p_args, p_argcount, r_error);
  66. }
  67. ERR_FAIL_COND_V(!instance, Variant());
  68. r_error.error = Callable::CallError::CALL_OK;
  69. jvalue *v = nullptr;
  70. if (p_argcount) {
  71. v = (jvalue *)alloca(sizeof(jvalue) * p_argcount);
  72. }
  73. JNIEnv *env = get_jni_env();
  74. int res = env->PushLocalFrame(16);
  75. ERR_FAIL_COND_V(res != 0, Variant());
  76. List<jobject> to_erase;
  77. for (int i = 0; i < p_argcount; i++) {
  78. jvalret vr = _variant_to_jvalue(env, E->get().argtypes[i], p_args[i]);
  79. v[i] = vr.val;
  80. if (vr.obj)
  81. to_erase.push_back(vr.obj);
  82. }
  83. Variant ret;
  84. switch (E->get().ret_type) {
  85. case Variant::NIL: {
  86. env->CallVoidMethodA(instance, E->get().method, v);
  87. } break;
  88. case Variant::BOOL: {
  89. ret = env->CallBooleanMethodA(instance, E->get().method, v) == JNI_TRUE;
  90. } break;
  91. case Variant::INT: {
  92. ret = env->CallIntMethodA(instance, E->get().method, v);
  93. } break;
  94. case Variant::FLOAT: {
  95. ret = env->CallFloatMethodA(instance, E->get().method, v);
  96. } break;
  97. case Variant::STRING: {
  98. jobject o = env->CallObjectMethodA(instance, E->get().method, v);
  99. ret = jstring_to_string((jstring)o, env);
  100. env->DeleteLocalRef(o);
  101. } break;
  102. case Variant::PACKED_STRING_ARRAY: {
  103. jobjectArray arr = (jobjectArray)env->CallObjectMethodA(instance, E->get().method, v);
  104. ret = _jobject_to_variant(env, arr);
  105. env->DeleteLocalRef(arr);
  106. } break;
  107. case Variant::PACKED_INT32_ARRAY: {
  108. jintArray arr = (jintArray)env->CallObjectMethodA(instance, E->get().method, v);
  109. int fCount = env->GetArrayLength(arr);
  110. Vector<int> sarr;
  111. sarr.resize(fCount);
  112. int *w = sarr.ptrw();
  113. env->GetIntArrayRegion(arr, 0, fCount, w);
  114. ret = sarr;
  115. env->DeleteLocalRef(arr);
  116. } break;
  117. case Variant::PACKED_FLOAT32_ARRAY: {
  118. jfloatArray arr = (jfloatArray)env->CallObjectMethodA(instance, E->get().method, v);
  119. int fCount = env->GetArrayLength(arr);
  120. Vector<float> sarr;
  121. sarr.resize(fCount);
  122. float *w = sarr.ptrw();
  123. env->GetFloatArrayRegion(arr, 0, fCount, w);
  124. ret = sarr;
  125. env->DeleteLocalRef(arr);
  126. } break;
  127. #ifndef _MSC_VER
  128. #warning This is missing 64 bits arrays, I have no idea how to do it in JNI
  129. #endif
  130. case Variant::DICTIONARY: {
  131. jobject obj = env->CallObjectMethodA(instance, E->get().method, v);
  132. ret = _jobject_to_variant(env, obj);
  133. env->DeleteLocalRef(obj);
  134. } break;
  135. default: {
  136. env->PopLocalFrame(nullptr);
  137. ERR_FAIL_V(Variant());
  138. } break;
  139. }
  140. while (to_erase.size()) {
  141. env->DeleteLocalRef(to_erase.front()->get());
  142. to_erase.pop_front();
  143. }
  144. env->PopLocalFrame(nullptr);
  145. return ret;
  146. #else // ANDROID_ENABLED
  147. // Defaulting to the regular instance calls.
  148. return Object::call(p_method, p_args, p_argcount, r_error);
  149. #endif
  150. }
  151. #ifdef ANDROID_ENABLED
  152. jobject get_instance() const {
  153. return instance;
  154. }
  155. void set_instance(jobject p_instance) {
  156. instance = p_instance;
  157. }
  158. void add_method(const StringName &p_name, jmethodID p_method, const Vector<Variant::Type> &p_args, Variant::Type p_ret_type) {
  159. MethodData md;
  160. md.method = p_method;
  161. md.argtypes = p_args;
  162. md.ret_type = p_ret_type;
  163. method_map[p_name] = md;
  164. }
  165. void add_signal(const StringName &p_name, const Vector<Variant::Type> &p_args) {
  166. if (p_args.size() == 0)
  167. ADD_SIGNAL(MethodInfo(p_name));
  168. else if (p_args.size() == 1)
  169. ADD_SIGNAL(MethodInfo(p_name, PropertyInfo(p_args[0], "arg1")));
  170. else if (p_args.size() == 2)
  171. ADD_SIGNAL(MethodInfo(p_name, PropertyInfo(p_args[0], "arg1"), PropertyInfo(p_args[1], "arg2")));
  172. else if (p_args.size() == 3)
  173. ADD_SIGNAL(MethodInfo(p_name, PropertyInfo(p_args[0], "arg1"), PropertyInfo(p_args[1], "arg2"), PropertyInfo(p_args[2], "arg3")));
  174. else if (p_args.size() == 4)
  175. ADD_SIGNAL(MethodInfo(p_name, PropertyInfo(p_args[0], "arg1"), PropertyInfo(p_args[1], "arg2"), PropertyInfo(p_args[2], "arg3"), PropertyInfo(p_args[3], "arg4")));
  176. else if (p_args.size() == 5)
  177. ADD_SIGNAL(MethodInfo(p_name, PropertyInfo(p_args[0], "arg1"), PropertyInfo(p_args[1], "arg2"), PropertyInfo(p_args[2], "arg3"), PropertyInfo(p_args[3], "arg4"), PropertyInfo(p_args[4], "arg5")));
  178. }
  179. #endif
  180. JNISingleton() {
  181. #ifdef ANDROID_ENABLED
  182. instance = nullptr;
  183. #endif
  184. }
  185. };
  186. #endif // JNI_SINGLETON_H