jni_singleton.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /**************************************************************************/
  2. /* jni_singleton.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. #pragma once
  31. #include "java_class_wrapper.h"
  32. #include "core/config/engine.h"
  33. #include "core/variant/variant.h"
  34. class JNISingleton : public Object {
  35. GDCLASS(JNISingleton, Object);
  36. struct MethodData {
  37. Variant::Type ret_type;
  38. Vector<Variant::Type> argtypes;
  39. };
  40. RBMap<StringName, MethodData> method_map;
  41. Ref<JavaObject> wrapped_object;
  42. protected:
  43. static void _bind_methods() {
  44. ClassDB::bind_method(D_METHOD("has_java_method", "method"), &JNISingleton::has_java_method);
  45. }
  46. public:
  47. virtual Variant callp(const StringName &p_method, const Variant **p_args, int p_argcount, Callable::CallError &r_error) override {
  48. // Godot methods take precedence.
  49. Variant ret = Object::callp(p_method, p_args, p_argcount, r_error);
  50. if (r_error.error == Callable::CallError::CALL_OK) {
  51. return ret;
  52. }
  53. // Check the method we're looking for is in the JNISingleton map.
  54. RBMap<StringName, MethodData>::Element *E = method_map.find(p_method);
  55. if (E) {
  56. if (wrapped_object.is_valid()) {
  57. // Check that the arguments match.
  58. int method_arg_count = E->get().argtypes.size();
  59. if (p_argcount < method_arg_count) {
  60. r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  61. } else if (p_argcount > method_arg_count) {
  62. r_error.error = Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS;
  63. } else {
  64. // Check the arguments are valid.
  65. bool arguments_valid = true;
  66. for (int i = 0; i < p_argcount; i++) {
  67. if (!Variant::can_convert(p_args[i]->get_type(), E->get().argtypes[i])) {
  68. arguments_valid = false;
  69. break;
  70. }
  71. }
  72. if (arguments_valid) {
  73. return wrapped_object->callp(p_method, p_args, p_argcount, r_error);
  74. } else {
  75. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  76. }
  77. }
  78. } else {
  79. r_error.error = Callable::CallError::CALL_ERROR_INSTANCE_IS_NULL;
  80. }
  81. }
  82. return Variant();
  83. }
  84. Ref<JavaObject> get_wrapped_object() const {
  85. return wrapped_object;
  86. }
  87. bool has_java_method(const StringName &p_method) const {
  88. return method_map.has(p_method);
  89. }
  90. void add_method(const StringName &p_name, const Vector<Variant::Type> &p_args, Variant::Type p_ret_type) {
  91. MethodData md;
  92. md.argtypes = p_args;
  93. md.ret_type = p_ret_type;
  94. method_map[p_name] = md;
  95. }
  96. void add_signal(const StringName &p_name, const Vector<Variant::Type> &p_args) {
  97. MethodInfo mi;
  98. mi.name = p_name;
  99. for (int i = 0; i < p_args.size(); i++) {
  100. mi.arguments.push_back(PropertyInfo(p_args[i], "arg" + itos(i + 1)));
  101. }
  102. ADD_SIGNAL(mi);
  103. }
  104. JNISingleton() {}
  105. JNISingleton(const Ref<JavaObject> &p_wrapped_object) {
  106. wrapped_object = p_wrapped_object;
  107. }
  108. ~JNISingleton() {
  109. method_map.clear();
  110. wrapped_object.unref();
  111. }
  112. };