jni_utils.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. /**************************************************************************/
  2. /* jni_utils.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. #include "jni_utils.h"
  31. #include "api/java_class_wrapper.h"
  32. jvalret _variant_to_jvalue(JNIEnv *env, Variant::Type p_type, const Variant *p_arg, bool force_jobject) {
  33. jvalret v;
  34. switch (p_type) {
  35. case Variant::BOOL: {
  36. if (force_jobject) {
  37. jclass bclass = env->FindClass("java/lang/Boolean");
  38. jmethodID ctor = env->GetMethodID(bclass, "<init>", "(Z)V");
  39. jvalue val;
  40. val.z = (bool)(*p_arg);
  41. jobject obj = env->NewObjectA(bclass, ctor, &val);
  42. v.val.l = obj;
  43. v.obj = obj;
  44. env->DeleteLocalRef(bclass);
  45. } else {
  46. v.val.z = *p_arg;
  47. }
  48. } break;
  49. case Variant::INT: {
  50. if (force_jobject) {
  51. jclass bclass = env->FindClass("java/lang/Integer");
  52. jmethodID ctor = env->GetMethodID(bclass, "<init>", "(I)V");
  53. jvalue val;
  54. val.i = (int)(*p_arg);
  55. jobject obj = env->NewObjectA(bclass, ctor, &val);
  56. v.val.l = obj;
  57. v.obj = obj;
  58. env->DeleteLocalRef(bclass);
  59. } else {
  60. v.val.i = *p_arg;
  61. }
  62. } break;
  63. case Variant::FLOAT: {
  64. if (force_jobject) {
  65. jclass bclass = env->FindClass("java/lang/Double");
  66. jmethodID ctor = env->GetMethodID(bclass, "<init>", "(D)V");
  67. jvalue val;
  68. val.d = (double)(*p_arg);
  69. jobject obj = env->NewObjectA(bclass, ctor, &val);
  70. v.val.l = obj;
  71. v.obj = obj;
  72. env->DeleteLocalRef(bclass);
  73. } else {
  74. v.val.f = *p_arg;
  75. }
  76. } break;
  77. case Variant::STRING: {
  78. String s = *p_arg;
  79. jstring jStr = env->NewStringUTF(s.utf8().get_data());
  80. v.val.l = jStr;
  81. v.obj = jStr;
  82. } break;
  83. case Variant::PACKED_STRING_ARRAY: {
  84. Vector<String> sarray = *p_arg;
  85. jobjectArray arr = env->NewObjectArray(sarray.size(), env->FindClass("java/lang/String"), env->NewStringUTF(""));
  86. for (int j = 0; j < sarray.size(); j++) {
  87. jstring str = env->NewStringUTF(sarray[j].utf8().get_data());
  88. env->SetObjectArrayElement(arr, j, str);
  89. env->DeleteLocalRef(str);
  90. }
  91. v.val.l = arr;
  92. v.obj = arr;
  93. } break;
  94. case Variant::DICTIONARY: {
  95. Dictionary dict = *p_arg;
  96. jclass dclass = env->FindClass("org/godotengine/godot/Dictionary");
  97. jmethodID ctor = env->GetMethodID(dclass, "<init>", "()V");
  98. jobject jdict = env->NewObject(dclass, ctor);
  99. Array keys = dict.keys();
  100. jobjectArray jkeys = env->NewObjectArray(keys.size(), env->FindClass("java/lang/String"), env->NewStringUTF(""));
  101. for (int j = 0; j < keys.size(); j++) {
  102. jstring str = env->NewStringUTF(String(keys[j]).utf8().get_data());
  103. env->SetObjectArrayElement(jkeys, j, str);
  104. env->DeleteLocalRef(str);
  105. }
  106. jmethodID set_keys = env->GetMethodID(dclass, "set_keys", "([Ljava/lang/String;)V");
  107. jvalue val;
  108. val.l = jkeys;
  109. env->CallVoidMethodA(jdict, set_keys, &val);
  110. env->DeleteLocalRef(jkeys);
  111. jobjectArray jvalues = env->NewObjectArray(keys.size(), env->FindClass("java/lang/Object"), nullptr);
  112. for (int j = 0; j < keys.size(); j++) {
  113. Variant var = dict[keys[j]];
  114. jvalret valret = _variant_to_jvalue(env, var.get_type(), &var, true);
  115. env->SetObjectArrayElement(jvalues, j, valret.val.l);
  116. if (valret.obj) {
  117. env->DeleteLocalRef(valret.obj);
  118. }
  119. }
  120. jmethodID set_values = env->GetMethodID(dclass, "set_values", "([Ljava/lang/Object;)V");
  121. val.l = jvalues;
  122. env->CallVoidMethodA(jdict, set_values, &val);
  123. env->DeleteLocalRef(jvalues);
  124. env->DeleteLocalRef(dclass);
  125. v.val.l = jdict;
  126. v.obj = jdict;
  127. } break;
  128. case Variant::PACKED_INT32_ARRAY: {
  129. Vector<int> array = *p_arg;
  130. jintArray arr = env->NewIntArray(array.size());
  131. const int *r = array.ptr();
  132. env->SetIntArrayRegion(arr, 0, array.size(), r);
  133. v.val.l = arr;
  134. v.obj = arr;
  135. } break;
  136. case Variant::PACKED_INT64_ARRAY: {
  137. Vector<int64_t> array = *p_arg;
  138. jlongArray arr = env->NewLongArray(array.size());
  139. const int64_t *r = array.ptr();
  140. env->SetLongArrayRegion(arr, 0, array.size(), r);
  141. v.val.l = arr;
  142. v.obj = arr;
  143. } break;
  144. case Variant::PACKED_BYTE_ARRAY: {
  145. Vector<uint8_t> array = *p_arg;
  146. jbyteArray arr = env->NewByteArray(array.size());
  147. const uint8_t *r = array.ptr();
  148. env->SetByteArrayRegion(arr, 0, array.size(), reinterpret_cast<const signed char *>(r));
  149. v.val.l = arr;
  150. v.obj = arr;
  151. } break;
  152. case Variant::PACKED_FLOAT32_ARRAY: {
  153. Vector<float> array = *p_arg;
  154. jfloatArray arr = env->NewFloatArray(array.size());
  155. const float *r = array.ptr();
  156. env->SetFloatArrayRegion(arr, 0, array.size(), r);
  157. v.val.l = arr;
  158. v.obj = arr;
  159. } break;
  160. case Variant::PACKED_FLOAT64_ARRAY: {
  161. Vector<double> array = *p_arg;
  162. jdoubleArray arr = env->NewDoubleArray(array.size());
  163. const double *r = array.ptr();
  164. env->SetDoubleArrayRegion(arr, 0, array.size(), r);
  165. v.val.l = arr;
  166. v.obj = arr;
  167. } break;
  168. case Variant::OBJECT: {
  169. Ref<JavaObject> generic_object = *p_arg;
  170. if (generic_object.is_valid()) {
  171. jobject obj = env->NewLocalRef(generic_object->get_instance());
  172. v.val.l = obj;
  173. v.obj = obj;
  174. } else {
  175. v.val.i = 0;
  176. }
  177. } break;
  178. default: {
  179. v.val.i = 0;
  180. } break;
  181. }
  182. return v;
  183. }
  184. String _get_class_name(JNIEnv *env, jclass cls, bool *array) {
  185. jclass cclass = env->FindClass("java/lang/Class");
  186. jmethodID getName = env->GetMethodID(cclass, "getName", "()Ljava/lang/String;");
  187. jstring clsName = (jstring)env->CallObjectMethod(cls, getName);
  188. if (array) {
  189. jmethodID isArray = env->GetMethodID(cclass, "isArray", "()Z");
  190. jboolean isarr = env->CallBooleanMethod(cls, isArray);
  191. (*array) = isarr != 0;
  192. }
  193. String name = jstring_to_string(clsName, env);
  194. env->DeleteLocalRef(clsName);
  195. return name;
  196. }
  197. Variant _jobject_to_variant(JNIEnv *env, jobject obj) {
  198. if (obj == nullptr) {
  199. return Variant();
  200. }
  201. jclass c = env->GetObjectClass(obj);
  202. bool array;
  203. String name = _get_class_name(env, c, &array);
  204. if (name == "java.lang.String") {
  205. return jstring_to_string((jstring)obj, env);
  206. }
  207. if (name == "[Ljava.lang.String;") {
  208. jobjectArray arr = (jobjectArray)obj;
  209. int stringCount = env->GetArrayLength(arr);
  210. Vector<String> sarr;
  211. for (int i = 0; i < stringCount; i++) {
  212. jstring string = (jstring)env->GetObjectArrayElement(arr, i);
  213. sarr.push_back(jstring_to_string(string, env));
  214. env->DeleteLocalRef(string);
  215. }
  216. return sarr;
  217. }
  218. if (name == "java.lang.Boolean") {
  219. jmethodID boolValue = env->GetMethodID(c, "booleanValue", "()Z");
  220. bool ret = env->CallBooleanMethod(obj, boolValue);
  221. return ret;
  222. }
  223. if (name == "java.lang.Integer" || name == "java.lang.Long") {
  224. jclass nclass = env->FindClass("java/lang/Number");
  225. jmethodID longValue = env->GetMethodID(nclass, "longValue", "()J");
  226. jlong ret = env->CallLongMethod(obj, longValue);
  227. return ret;
  228. }
  229. if (name == "[I") {
  230. jintArray arr = (jintArray)obj;
  231. int fCount = env->GetArrayLength(arr);
  232. Vector<int> sarr;
  233. sarr.resize(fCount);
  234. int *w = sarr.ptrw();
  235. env->GetIntArrayRegion(arr, 0, fCount, w);
  236. return sarr;
  237. }
  238. if (name == "[J") {
  239. jlongArray arr = (jlongArray)obj;
  240. int fCount = env->GetArrayLength(arr);
  241. Vector<int64_t> sarr;
  242. sarr.resize(fCount);
  243. int64_t *w = sarr.ptrw();
  244. env->GetLongArrayRegion(arr, 0, fCount, w);
  245. return sarr;
  246. }
  247. if (name == "[B") {
  248. jbyteArray arr = (jbyteArray)obj;
  249. int fCount = env->GetArrayLength(arr);
  250. Vector<uint8_t> sarr;
  251. sarr.resize(fCount);
  252. uint8_t *w = sarr.ptrw();
  253. env->GetByteArrayRegion(arr, 0, fCount, reinterpret_cast<signed char *>(w));
  254. return sarr;
  255. }
  256. if (name == "java.lang.Float" || name == "java.lang.Double") {
  257. jclass nclass = env->FindClass("java/lang/Number");
  258. jmethodID doubleValue = env->GetMethodID(nclass, "doubleValue", "()D");
  259. double ret = env->CallDoubleMethod(obj, doubleValue);
  260. return ret;
  261. }
  262. if (name == "[D") {
  263. jdoubleArray arr = (jdoubleArray)obj;
  264. int fCount = env->GetArrayLength(arr);
  265. PackedFloat64Array packed_array;
  266. packed_array.resize(fCount);
  267. double *w = packed_array.ptrw();
  268. for (int i = 0; i < fCount; i++) {
  269. double n;
  270. env->GetDoubleArrayRegion(arr, i, 1, &n);
  271. w[i] = n;
  272. }
  273. return packed_array;
  274. }
  275. if (name == "[F") {
  276. jfloatArray arr = (jfloatArray)obj;
  277. int fCount = env->GetArrayLength(arr);
  278. PackedFloat32Array packed_array;
  279. packed_array.resize(fCount);
  280. float *w = packed_array.ptrw();
  281. for (int i = 0; i < fCount; i++) {
  282. float n;
  283. env->GetFloatArrayRegion(arr, i, 1, &n);
  284. w[i] = n;
  285. }
  286. return packed_array;
  287. }
  288. if (name == "[Ljava.lang.Object;") {
  289. jobjectArray arr = (jobjectArray)obj;
  290. int objCount = env->GetArrayLength(arr);
  291. Array varr;
  292. for (int i = 0; i < objCount; i++) {
  293. jobject jobj = env->GetObjectArrayElement(arr, i);
  294. Variant v = _jobject_to_variant(env, jobj);
  295. varr.push_back(v);
  296. env->DeleteLocalRef(jobj);
  297. }
  298. return varr;
  299. }
  300. if (name == "java.util.HashMap" || name == "org.godotengine.godot.Dictionary") {
  301. Dictionary ret;
  302. jclass oclass = c;
  303. jmethodID get_keys = env->GetMethodID(oclass, "get_keys", "()[Ljava/lang/String;");
  304. jobjectArray arr = (jobjectArray)env->CallObjectMethod(obj, get_keys);
  305. PackedStringArray keys = _jobject_to_variant(env, arr);
  306. env->DeleteLocalRef(arr);
  307. jmethodID get_values = env->GetMethodID(oclass, "get_values", "()[Ljava/lang/Object;");
  308. arr = (jobjectArray)env->CallObjectMethod(obj, get_values);
  309. Array vals = _jobject_to_variant(env, arr);
  310. env->DeleteLocalRef(arr);
  311. for (int i = 0; i < keys.size(); i++) {
  312. ret[keys[i]] = vals[i];
  313. }
  314. return ret;
  315. }
  316. Ref<JavaObject> generic_object(memnew(JavaObject(JavaClassWrapper::get_singleton()->wrap(name), obj)));
  317. env->DeleteLocalRef(c);
  318. return generic_object;
  319. }
  320. Variant::Type get_jni_type(const String &p_type) {
  321. static struct {
  322. const char *name;
  323. Variant::Type type;
  324. } _type_to_vtype[] = {
  325. { "void", Variant::NIL },
  326. { "boolean", Variant::BOOL },
  327. { "int", Variant::INT },
  328. { "long", Variant::INT },
  329. { "float", Variant::FLOAT },
  330. { "double", Variant::FLOAT },
  331. { "java.lang.String", Variant::STRING },
  332. { "[I", Variant::PACKED_INT32_ARRAY },
  333. { "[J", Variant::PACKED_INT64_ARRAY },
  334. { "[B", Variant::PACKED_BYTE_ARRAY },
  335. { "[F", Variant::PACKED_FLOAT32_ARRAY },
  336. { "[D", Variant::PACKED_FLOAT64_ARRAY },
  337. { "[Ljava.lang.String;", Variant::PACKED_STRING_ARRAY },
  338. { "org.godotengine.godot.Dictionary", Variant::DICTIONARY },
  339. { nullptr, Variant::NIL }
  340. };
  341. int idx = 0;
  342. while (_type_to_vtype[idx].name) {
  343. if (p_type == _type_to_vtype[idx].name) {
  344. return _type_to_vtype[idx].type;
  345. }
  346. idx++;
  347. }
  348. return Variant::OBJECT;
  349. }
  350. String get_jni_sig(const String &p_type) {
  351. static struct {
  352. const char *name;
  353. const char *sig;
  354. } _type_to_vtype[] = {
  355. { "void", "V" },
  356. { "boolean", "Z" },
  357. { "int", "I" },
  358. { "long", "J" },
  359. { "float", "F" },
  360. { "double", "D" },
  361. { "java.lang.String", "Ljava/lang/String;" },
  362. { "org.godotengine.godot.Dictionary", "Lorg/godotengine/godot/Dictionary;" },
  363. { "[I", "[I" },
  364. { "[J", "[J" },
  365. { "[B", "[B" },
  366. { "[F", "[F" },
  367. { "[D", "[D" },
  368. { "[Ljava.lang.String;", "[Ljava/lang/String;" },
  369. { nullptr, "V" }
  370. };
  371. int idx = 0;
  372. while (_type_to_vtype[idx].name) {
  373. if (p_type == _type_to_vtype[idx].name) {
  374. return _type_to_vtype[idx].sig;
  375. }
  376. idx++;
  377. }
  378. return "L" + p_type.replace(".", "/") + ";";
  379. }