java_class_wrapper.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*************************************************************************/
  2. /* java_class_wrapper.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 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: r_type = Variant::NIL; break;
  72. case ARG_TYPE_BOOLEAN | ARG_NUMBER_CLASS_BIT:
  73. case ARG_TYPE_BOOLEAN: r_type = Variant::BOOL; break;
  74. case ARG_TYPE_BYTE | ARG_NUMBER_CLASS_BIT:
  75. case ARG_TYPE_BYTE:
  76. r_type = Variant::INT;
  77. likelihood = 0.1;
  78. break;
  79. case ARG_TYPE_CHAR | ARG_NUMBER_CLASS_BIT:
  80. case ARG_TYPE_CHAR:
  81. r_type = Variant::INT;
  82. likelihood = 0.2;
  83. break;
  84. case ARG_TYPE_SHORT | ARG_NUMBER_CLASS_BIT:
  85. case ARG_TYPE_SHORT:
  86. r_type = Variant::INT;
  87. likelihood = 0.3;
  88. break;
  89. case ARG_TYPE_INT | ARG_NUMBER_CLASS_BIT:
  90. case ARG_TYPE_INT:
  91. r_type = Variant::INT;
  92. likelihood = 1.0;
  93. break;
  94. case ARG_TYPE_LONG | ARG_NUMBER_CLASS_BIT:
  95. case ARG_TYPE_LONG:
  96. r_type = Variant::INT;
  97. likelihood = 0.5;
  98. break;
  99. case ARG_TYPE_FLOAT | ARG_NUMBER_CLASS_BIT:
  100. case ARG_TYPE_FLOAT:
  101. r_type = Variant::REAL;
  102. likelihood = 1.0;
  103. break;
  104. case ARG_TYPE_DOUBLE | ARG_NUMBER_CLASS_BIT:
  105. case ARG_TYPE_DOUBLE:
  106. r_type = Variant::REAL;
  107. likelihood = 0.5;
  108. break;
  109. case ARG_TYPE_STRING: r_type = Variant::STRING; break;
  110. case ARG_TYPE_CLASS: r_type = Variant::OBJECT; break;
  111. case ARG_ARRAY_BIT | ARG_TYPE_VOID: r_type = Variant::NIL; break;
  112. case ARG_ARRAY_BIT | ARG_TYPE_BOOLEAN: r_type = Variant::ARRAY; break;
  113. case ARG_ARRAY_BIT | ARG_TYPE_BYTE:
  114. r_type = Variant::POOL_BYTE_ARRAY;
  115. likelihood = 1.0;
  116. break;
  117. case ARG_ARRAY_BIT | ARG_TYPE_CHAR:
  118. r_type = Variant::POOL_BYTE_ARRAY;
  119. likelihood = 0.5;
  120. break;
  121. case ARG_ARRAY_BIT | ARG_TYPE_SHORT:
  122. r_type = Variant::POOL_INT_ARRAY;
  123. likelihood = 0.3;
  124. break;
  125. case ARG_ARRAY_BIT | ARG_TYPE_INT:
  126. r_type = Variant::POOL_INT_ARRAY;
  127. likelihood = 1.0;
  128. break;
  129. case ARG_ARRAY_BIT | ARG_TYPE_LONG:
  130. r_type = Variant::POOL_INT_ARRAY;
  131. likelihood = 0.5;
  132. break;
  133. case ARG_ARRAY_BIT | ARG_TYPE_FLOAT:
  134. r_type = Variant::POOL_REAL_ARRAY;
  135. likelihood = 1.0;
  136. break;
  137. case ARG_ARRAY_BIT | ARG_TYPE_DOUBLE:
  138. r_type = Variant::POOL_REAL_ARRAY;
  139. likelihood = 0.5;
  140. break;
  141. case ARG_ARRAY_BIT | ARG_TYPE_STRING: r_type = Variant::POOL_STRING_ARRAY; break;
  142. case ARG_ARRAY_BIT | ARG_TYPE_CLASS: r_type = Variant::ARRAY; break;
  143. }
  144. }
  145. _FORCE_INLINE_ static bool _convert_object_to_variant(JNIEnv *env, jobject obj, Variant &var, uint32_t p_sig);
  146. bool _call_method(JavaObject *p_instance, const StringName &p_method, const Variant **p_args, int p_argcount, Variant::CallError &r_error, Variant &ret);
  147. friend class JavaClassWrapper;
  148. Map<StringName, List<MethodInfo> > methods;
  149. jclass _class;
  150. #endif
  151. public:
  152. virtual Variant call(const StringName &p_method, const Variant **p_args, int p_argcount, Variant::CallError &r_error);
  153. JavaClass();
  154. };
  155. class JavaObject : public Reference {
  156. GDCLASS(JavaObject, Reference);
  157. #ifdef ANDROID_ENABLED
  158. Ref<JavaClass> base_class;
  159. friend class JavaClass;
  160. jobject instance;
  161. #endif
  162. public:
  163. virtual Variant call(const StringName &p_method, const Variant **p_args, int p_argcount, Variant::CallError &r_error);
  164. #ifdef ANDROID_ENABLED
  165. JavaObject(const Ref<JavaClass> &p_base, jobject *p_instance);
  166. ~JavaObject();
  167. #endif
  168. };
  169. class JavaClassWrapper : public Object {
  170. GDCLASS(JavaClassWrapper, Object);
  171. #ifdef ANDROID_ENABLED
  172. Map<String, Ref<JavaClass> > class_cache;
  173. friend class JavaClass;
  174. jclass activityClass;
  175. jmethodID findClass;
  176. jmethodID getDeclaredMethods;
  177. jmethodID getFields;
  178. jmethodID getParameterTypes;
  179. jmethodID getReturnType;
  180. jmethodID getModifiers;
  181. jmethodID getName;
  182. jmethodID Class_getName;
  183. jmethodID Field_getName;
  184. jmethodID Field_getModifiers;
  185. jmethodID Field_get;
  186. jmethodID Boolean_booleanValue;
  187. jmethodID Byte_byteValue;
  188. jmethodID Character_characterValue;
  189. jmethodID Short_shortValue;
  190. jmethodID Integer_integerValue;
  191. jmethodID Long_longValue;
  192. jmethodID Float_floatValue;
  193. jmethodID Double_doubleValue;
  194. jobject classLoader;
  195. bool _get_type_sig(JNIEnv *env, jobject obj, uint32_t &sig, String &strsig);
  196. #endif
  197. static JavaClassWrapper *singleton;
  198. protected:
  199. static void _bind_methods();
  200. public:
  201. static JavaClassWrapper *get_singleton() { return singleton; }
  202. Ref<JavaClass> wrap(const String &p_class);
  203. #ifdef ANDROID_ENABLED
  204. JavaClassWrapper(jobject p_activity = NULL);
  205. #else
  206. JavaClassWrapper();
  207. #endif
  208. };
  209. #endif // JAVA_CLASS_WRAPPER_H