jni_utils.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*************************************************************************/
  2. /* jni_utils.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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_UTILS_H
  31. #define JNI_UTILS_H
  32. #include "string_android.h"
  33. #include <core/engine.h>
  34. #include <core/variant.h>
  35. #include <jni.h>
  36. struct jvalret {
  37. jobject obj;
  38. jvalue val;
  39. jvalret() { obj = NULL; }
  40. };
  41. jvalret _variant_to_jvalue(JNIEnv *env, Variant::Type p_type, const Variant *p_arg, bool force_jobject = false);
  42. String _get_class_name(JNIEnv *env, jclass cls, bool *array);
  43. Variant _jobject_to_variant(JNIEnv *env, jobject obj);
  44. Variant::Type get_jni_type(const String &p_type);
  45. const char *get_jni_sig(const String &p_type);
  46. class JNISingleton : public Object {
  47. GDCLASS(JNISingleton, Object);
  48. struct MethodData {
  49. jmethodID method;
  50. Variant::Type ret_type;
  51. Vector<Variant::Type> argtypes;
  52. };
  53. jobject instance;
  54. Map<StringName, MethodData> method_map;
  55. public:
  56. virtual Variant call(const StringName &p_method, const Variant **p_args, int p_argcount, Variant::CallError &r_error) {
  57. ERR_FAIL_COND_V(!instance, Variant());
  58. r_error.error = Variant::CallError::CALL_OK;
  59. Map<StringName, MethodData>::Element *E = method_map.find(p_method);
  60. if (!E) {
  61. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  62. return Variant();
  63. }
  64. int ac = E->get().argtypes.size();
  65. if (ac < p_argcount) {
  66. r_error.error = Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  67. r_error.argument = ac;
  68. return Variant();
  69. }
  70. if (ac > p_argcount) {
  71. r_error.error = Variant::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS;
  72. r_error.argument = ac;
  73. return Variant();
  74. }
  75. for (int i = 0; i < p_argcount; i++) {
  76. if (!Variant::can_convert(p_args[i]->get_type(), E->get().argtypes[i])) {
  77. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  78. r_error.argument = i;
  79. r_error.expected = E->get().argtypes[i];
  80. }
  81. }
  82. jvalue *v = NULL;
  83. if (p_argcount) {
  84. v = (jvalue *)alloca(sizeof(jvalue) * p_argcount);
  85. }
  86. JNIEnv *env = ThreadAndroid::get_env();
  87. int res = env->PushLocalFrame(16);
  88. ERR_FAIL_COND_V(res != 0, Variant());
  89. List<jobject> to_erase;
  90. for (int i = 0; i < p_argcount; i++) {
  91. jvalret vr = _variant_to_jvalue(env, E->get().argtypes[i], p_args[i]);
  92. v[i] = vr.val;
  93. if (vr.obj)
  94. to_erase.push_back(vr.obj);
  95. }
  96. Variant ret;
  97. switch (E->get().ret_type) {
  98. case Variant::NIL: {
  99. env->CallVoidMethodA(instance, E->get().method, v);
  100. } break;
  101. case Variant::BOOL: {
  102. ret = env->CallBooleanMethodA(instance, E->get().method, v) == JNI_TRUE;
  103. } break;
  104. case Variant::INT: {
  105. ret = env->CallIntMethodA(instance, E->get().method, v);
  106. } break;
  107. case Variant::REAL: {
  108. ret = env->CallFloatMethodA(instance, E->get().method, v);
  109. } break;
  110. case Variant::STRING: {
  111. jobject o = env->CallObjectMethodA(instance, E->get().method, v);
  112. ret = jstring_to_string((jstring)o, env);
  113. env->DeleteLocalRef(o);
  114. } break;
  115. case Variant::POOL_STRING_ARRAY: {
  116. jobjectArray arr = (jobjectArray)env->CallObjectMethodA(instance, E->get().method, v);
  117. ret = _jobject_to_variant(env, arr);
  118. env->DeleteLocalRef(arr);
  119. } break;
  120. case Variant::POOL_INT_ARRAY: {
  121. jintArray arr = (jintArray)env->CallObjectMethodA(instance, E->get().method, v);
  122. int fCount = env->GetArrayLength(arr);
  123. PoolVector<int> sarr;
  124. sarr.resize(fCount);
  125. PoolVector<int>::Write w = sarr.write();
  126. env->GetIntArrayRegion(arr, 0, fCount, w.ptr());
  127. w.release();
  128. ret = sarr;
  129. env->DeleteLocalRef(arr);
  130. } break;
  131. case Variant::POOL_REAL_ARRAY: {
  132. jfloatArray arr = (jfloatArray)env->CallObjectMethodA(instance, E->get().method, v);
  133. int fCount = env->GetArrayLength(arr);
  134. PoolVector<float> sarr;
  135. sarr.resize(fCount);
  136. PoolVector<float>::Write w = sarr.write();
  137. env->GetFloatArrayRegion(arr, 0, fCount, w.ptr());
  138. w.release();
  139. ret = sarr;
  140. env->DeleteLocalRef(arr);
  141. } break;
  142. case Variant::DICTIONARY: {
  143. jobject obj = env->CallObjectMethodA(instance, E->get().method, v);
  144. ret = _jobject_to_variant(env, obj);
  145. env->DeleteLocalRef(obj);
  146. } break;
  147. default: {
  148. env->PopLocalFrame(NULL);
  149. ERR_FAIL_V(Variant());
  150. } break;
  151. }
  152. while (to_erase.size()) {
  153. env->DeleteLocalRef(to_erase.front()->get());
  154. to_erase.pop_front();
  155. }
  156. env->PopLocalFrame(NULL);
  157. return ret;
  158. }
  159. jobject get_instance() const {
  160. return instance;
  161. }
  162. void set_instance(jobject p_instance) {
  163. instance = p_instance;
  164. }
  165. void add_method(const StringName &p_name, jmethodID p_method, const Vector<Variant::Type> &p_args, Variant::Type p_ret_type) {
  166. MethodData md;
  167. md.method = p_method;
  168. md.argtypes = p_args;
  169. md.ret_type = p_ret_type;
  170. method_map[p_name] = md;
  171. }
  172. JNISingleton() {
  173. instance = NULL;
  174. }
  175. };
  176. #endif // JNI_UTILS_H