java_class_wrapper.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /**************************************************************************/
  2. /* java_class_wrapper.h */
  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. #ifndef JAVA_CLASS_WRAPPER_H
  31. #define JAVA_CLASS_WRAPPER_H
  32. #include "core/reference.h"
  33. #ifdef ANDROID_ENABLED
  34. #include <android/log.h>
  35. #include <jni.h>
  36. #endif
  37. #ifdef ANDROID_ENABLED
  38. class JavaObject;
  39. #endif
  40. class JavaClass : public Reference {
  41. GDCLASS(JavaClass, Reference);
  42. #ifdef ANDROID_ENABLED
  43. enum ArgumentType{
  44. ARG_TYPE_VOID,
  45. ARG_TYPE_BOOLEAN,
  46. ARG_TYPE_BYTE,
  47. ARG_TYPE_CHAR,
  48. ARG_TYPE_SHORT,
  49. ARG_TYPE_INT,
  50. ARG_TYPE_LONG,
  51. ARG_TYPE_FLOAT,
  52. ARG_TYPE_DOUBLE,
  53. ARG_TYPE_STRING, //special case
  54. ARG_TYPE_CLASS,
  55. ARG_ARRAY_BIT = 1 << 16,
  56. ARG_NUMBER_CLASS_BIT = 1 << 17,
  57. ARG_TYPE_MASK = (1 << 16) - 1
  58. };
  59. Map<StringName, Variant> constant_map;
  60. struct MethodInfo {
  61. bool _static;
  62. Vector<uint32_t> param_types;
  63. Vector<StringName> param_sigs;
  64. uint32_t return_type;
  65. jmethodID method;
  66. };
  67. _FORCE_INLINE_ static void _convert_to_variant_type(int p_sig, Variant::Type &r_type, float &likelihood) {
  68. likelihood = 1.0;
  69. r_type = Variant::NIL;
  70. switch (p_sig) {
  71. case ARG_TYPE_VOID:
  72. r_type = Variant::NIL;
  73. break;
  74. case ARG_TYPE_BOOLEAN | ARG_NUMBER_CLASS_BIT:
  75. case ARG_TYPE_BOOLEAN:
  76. r_type = Variant::BOOL;
  77. break;
  78. case ARG_TYPE_BYTE | ARG_NUMBER_CLASS_BIT:
  79. case ARG_TYPE_BYTE:
  80. r_type = Variant::INT;
  81. likelihood = 0.1;
  82. break;
  83. case ARG_TYPE_CHAR | ARG_NUMBER_CLASS_BIT:
  84. case ARG_TYPE_CHAR:
  85. r_type = Variant::INT;
  86. likelihood = 0.2;
  87. break;
  88. case ARG_TYPE_SHORT | ARG_NUMBER_CLASS_BIT:
  89. case ARG_TYPE_SHORT:
  90. r_type = Variant::INT;
  91. likelihood = 0.3;
  92. break;
  93. case ARG_TYPE_INT | ARG_NUMBER_CLASS_BIT:
  94. case ARG_TYPE_INT:
  95. r_type = Variant::INT;
  96. likelihood = 1.0;
  97. break;
  98. case ARG_TYPE_LONG | ARG_NUMBER_CLASS_BIT:
  99. case ARG_TYPE_LONG:
  100. r_type = Variant::INT;
  101. likelihood = 0.5;
  102. break;
  103. case ARG_TYPE_FLOAT | ARG_NUMBER_CLASS_BIT:
  104. case ARG_TYPE_FLOAT:
  105. r_type = Variant::REAL;
  106. likelihood = 1.0;
  107. break;
  108. case ARG_TYPE_DOUBLE | ARG_NUMBER_CLASS_BIT:
  109. case ARG_TYPE_DOUBLE:
  110. r_type = Variant::REAL;
  111. likelihood = 0.5;
  112. break;
  113. case ARG_TYPE_STRING:
  114. r_type = Variant::STRING;
  115. break;
  116. case ARG_TYPE_CLASS:
  117. r_type = Variant::OBJECT;
  118. break;
  119. case ARG_ARRAY_BIT | ARG_TYPE_VOID:
  120. r_type = Variant::NIL;
  121. break;
  122. case ARG_ARRAY_BIT | ARG_TYPE_BOOLEAN:
  123. r_type = Variant::ARRAY;
  124. break;
  125. case ARG_ARRAY_BIT | ARG_TYPE_BYTE:
  126. r_type = Variant::POOL_BYTE_ARRAY;
  127. likelihood = 1.0;
  128. break;
  129. case ARG_ARRAY_BIT | ARG_TYPE_CHAR:
  130. r_type = Variant::POOL_BYTE_ARRAY;
  131. likelihood = 0.5;
  132. break;
  133. case ARG_ARRAY_BIT | ARG_TYPE_SHORT:
  134. r_type = Variant::POOL_INT_ARRAY;
  135. likelihood = 0.3;
  136. break;
  137. case ARG_ARRAY_BIT | ARG_TYPE_INT:
  138. r_type = Variant::POOL_INT_ARRAY;
  139. likelihood = 1.0;
  140. break;
  141. case ARG_ARRAY_BIT | ARG_TYPE_LONG:
  142. r_type = Variant::POOL_INT_ARRAY;
  143. likelihood = 0.5;
  144. break;
  145. case ARG_ARRAY_BIT | ARG_TYPE_FLOAT:
  146. r_type = Variant::POOL_REAL_ARRAY;
  147. likelihood = 1.0;
  148. break;
  149. case ARG_ARRAY_BIT | ARG_TYPE_DOUBLE:
  150. r_type = Variant::POOL_REAL_ARRAY;
  151. likelihood = 0.5;
  152. break;
  153. case ARG_ARRAY_BIT | ARG_TYPE_STRING:
  154. r_type = Variant::POOL_STRING_ARRAY;
  155. break;
  156. case ARG_ARRAY_BIT | ARG_TYPE_CLASS:
  157. r_type = Variant::ARRAY;
  158. break;
  159. }
  160. }
  161. _FORCE_INLINE_ static bool _convert_object_to_variant(JNIEnv *env, jobject obj, Variant &var, uint32_t p_sig);
  162. bool _call_method(JavaObject *p_instance, const StringName &p_method, const Variant **p_args, int p_argcount, Variant::CallError &r_error, Variant &ret);
  163. friend class JavaClassWrapper;
  164. Map<StringName, List<MethodInfo>> methods;
  165. jclass _class;
  166. #endif
  167. public:
  168. virtual Variant call(const StringName &p_method, const Variant **p_args, int p_argcount, Variant::CallError &r_error);
  169. JavaClass();
  170. };
  171. class JavaObject : public Reference {
  172. GDCLASS(JavaObject, Reference);
  173. #ifdef ANDROID_ENABLED
  174. Ref<JavaClass> base_class;
  175. friend class JavaClass;
  176. jobject instance;
  177. #endif
  178. public:
  179. virtual Variant call(const StringName &p_method, const Variant **p_args, int p_argcount, Variant::CallError &r_error);
  180. #ifdef ANDROID_ENABLED
  181. JavaObject(const Ref<JavaClass> &p_base, jobject *p_instance);
  182. ~JavaObject();
  183. #endif
  184. };
  185. class JavaClassWrapper : public Object {
  186. GDCLASS(JavaClassWrapper, Object);
  187. #ifdef ANDROID_ENABLED
  188. Map<String, Ref<JavaClass>> class_cache;
  189. friend class JavaClass;
  190. jclass activityClass;
  191. jmethodID findClass;
  192. jmethodID getDeclaredMethods;
  193. jmethodID getFields;
  194. jmethodID getParameterTypes;
  195. jmethodID getReturnType;
  196. jmethodID getModifiers;
  197. jmethodID getName;
  198. jmethodID Class_getName;
  199. jmethodID Field_getName;
  200. jmethodID Field_getModifiers;
  201. jmethodID Field_get;
  202. jmethodID Boolean_booleanValue;
  203. jmethodID Byte_byteValue;
  204. jmethodID Character_characterValue;
  205. jmethodID Short_shortValue;
  206. jmethodID Integer_integerValue;
  207. jmethodID Long_longValue;
  208. jmethodID Float_floatValue;
  209. jmethodID Double_doubleValue;
  210. jobject classLoader;
  211. bool _get_type_sig(JNIEnv *env, jobject obj, uint32_t &sig, String &strsig);
  212. #endif
  213. static JavaClassWrapper *singleton;
  214. protected:
  215. static void _bind_methods();
  216. public:
  217. static JavaClassWrapper *get_singleton() { return singleton; }
  218. Ref<JavaClass> wrap(const String &p_class);
  219. #ifdef ANDROID_ENABLED
  220. JavaClassWrapper(jobject p_activity = NULL);
  221. #else
  222. JavaClassWrapper();
  223. #endif
  224. };
  225. #endif // JAVA_CLASS_WRAPPER_H