jni_singleton.h 7.4 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-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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/engine.h>
  33. #include <core/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, Variant::CallError &r_error) {
  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 = Variant::CallError::CALL_OK;
  69. jvalue *v = NULL;
  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::REAL: {
  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::POOL_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::POOL_INT_ARRAY: {
  108. jintArray arr = (jintArray)env->CallObjectMethodA(instance, E->get().method, v);
  109. int fCount = env->GetArrayLength(arr);
  110. PoolVector<int> sarr;
  111. sarr.resize(fCount);
  112. PoolVector<int>::Write w = sarr.write();
  113. env->GetIntArrayRegion(arr, 0, fCount, w.ptr());
  114. w.release();
  115. ret = sarr;
  116. env->DeleteLocalRef(arr);
  117. } break;
  118. case Variant::POOL_REAL_ARRAY: {
  119. jfloatArray arr = (jfloatArray)env->CallObjectMethodA(instance, E->get().method, v);
  120. int fCount = env->GetArrayLength(arr);
  121. PoolVector<float> sarr;
  122. sarr.resize(fCount);
  123. PoolVector<float>::Write w = sarr.write();
  124. env->GetFloatArrayRegion(arr, 0, fCount, w.ptr());
  125. w.release();
  126. ret = sarr;
  127. env->DeleteLocalRef(arr);
  128. } break;
  129. case Variant::DICTIONARY: {
  130. jobject obj = env->CallObjectMethodA(instance, E->get().method, v);
  131. ret = _jobject_to_variant(env, obj);
  132. env->DeleteLocalRef(obj);
  133. } break;
  134. default: {
  135. env->PopLocalFrame(NULL);
  136. ERR_FAIL_V(Variant());
  137. } break;
  138. }
  139. while (to_erase.size()) {
  140. env->DeleteLocalRef(to_erase.front()->get());
  141. to_erase.pop_front();
  142. }
  143. env->PopLocalFrame(NULL);
  144. return ret;
  145. #else // ANDROID_ENABLED
  146. // Defaulting to the regular instance calls.
  147. return Object::call(p_method, p_args, p_argcount, r_error);
  148. #endif
  149. }
  150. #ifdef ANDROID_ENABLED
  151. jobject get_instance() const {
  152. return instance;
  153. }
  154. void set_instance(jobject p_instance) {
  155. instance = p_instance;
  156. }
  157. void add_method(const StringName &p_name, jmethodID p_method, const Vector<Variant::Type> &p_args, Variant::Type p_ret_type) {
  158. MethodData md;
  159. md.method = p_method;
  160. md.argtypes = p_args;
  161. md.ret_type = p_ret_type;
  162. method_map[p_name] = md;
  163. }
  164. void add_signal(const StringName &p_name, const Vector<Variant::Type> &p_args) {
  165. if (p_args.size() == 0)
  166. ADD_SIGNAL(MethodInfo(p_name));
  167. else if (p_args.size() == 1)
  168. ADD_SIGNAL(MethodInfo(p_name, PropertyInfo(p_args[0], "arg1")));
  169. else if (p_args.size() == 2)
  170. ADD_SIGNAL(MethodInfo(p_name, PropertyInfo(p_args[0], "arg1"), PropertyInfo(p_args[1], "arg2")));
  171. else if (p_args.size() == 3)
  172. ADD_SIGNAL(MethodInfo(p_name, PropertyInfo(p_args[0], "arg1"), PropertyInfo(p_args[1], "arg2"), PropertyInfo(p_args[2], "arg3")));
  173. else if (p_args.size() == 4)
  174. 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")));
  175. else if (p_args.size() == 5)
  176. 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")));
  177. }
  178. #endif
  179. JNISingleton() {
  180. #ifdef ANDROID_ENABLED
  181. instance = NULL;
  182. #endif
  183. }
  184. };
  185. #endif // JNI_SINGLETON_H