| 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 | |
| 31 | #pragma once |
| 32 | |
| 33 | #include "java_class_wrapper.h" |
| 34 | |
| 35 | #include "core/config/engine.h" |
| 36 | #include "core/templates/rb_map.h" |
| 37 | #include "core/variant/variant.h" |
| 38 | |
| 39 | class JNISingleton : public Object { |
| 40 | GDCLASS(JNISingleton, Object); |
| 41 | |
| 42 | struct MethodData { |
| 43 | Variant::Type ret_type; |
| 44 | Vector<Variant::Type> argtypes; |
| 45 | }; |
| 46 | |
| 47 | RBMap<StringName, MethodData> method_map; |
| 48 | Ref<JavaObject> wrapped_object; |
| 49 | |
| 50 | protected: |
| 51 | static void _bind_methods() { |
| 52 | ClassDB::bind_method(D_METHOD("has_java_method", "method"), &JNISingleton::has_java_method); |
| 53 | } |
| 54 | |
| 55 | public: |
| 56 | virtual Variant callp(const StringName &p_method, const Variant **p_args, int p_argcount, Callable::CallError &r_error) override { |
| 57 | // Godot methods take precedence. |
| 58 | Variant ret = Object::callp(p_method, p_args, p_argcount, r_error); |
| 59 | if (r_error.error == Callable::CallError::CALL_OK) { |
| 60 | return ret; |
| 61 | } |
| 62 | |
| 63 | // Check the method we're looking for is in the JNISingleton map. |
| 64 | // This is done because JNISingletons register methods differently than wrapped JavaClass / JavaObject to allow |
| 65 | // for access to private methods annotated with the @UsedByGodot annotation. |
| 66 | // In the future, we should remove access to private methods and require that JNISingletons' methods exposed to |
| 67 | // GDScript be all public, similarly to what we do for wrapped JavaClass / JavaObject methods. Doing so will |
| 68 | // also allow dropping and deprecating the @UsedByGodot annotation. |
| 69 | RBMap<StringName, MethodData>::Element *E = method_map.find(p_method); |
| 70 | if (E) { |
| 71 | if (wrapped_object.is_valid()) { |
| 72 | return wrapped_object->callp(p_method, p_args, p_argcount, r_error); |
| 73 | } else { |
| 74 | r_error.error = Callable::CallError::CALL_ERROR_INSTANCE_IS_NULL; |
| 75 | } |
| 76 | } |
| 77 | return Variant(); |
| 78 | } |
| 79 | |
| 80 | Ref<JavaObject> get_wrapped_object() const { |
| 81 | return wrapped_object; |
| 82 | } |
| 83 | |
| 84 | bool has_java_method(const StringName &p_method) const { |
| 85 | return method_map.has(p_method); |
| 86 | } |
| 87 | |
| 88 | void add_method(const StringName &p_name, const Vector<Variant::Type> &p_args, Variant::Type p_ret_type) { |
| 89 | MethodData md; |
| 90 | md.argtypes = p_args; |
| 91 | md.ret_type = p_ret_type; |
| 92 | method_map[p_name] = md; |
| 93 | } |
| 94 | |
| 95 | void add_signal(const StringName &p_name, const Vector<Variant::Type> &p_args) { |
| 96 | MethodInfo mi; |
| 97 | mi.name = p_name; |
| 98 | for (int i = 0; i < p_args.size(); i++) { |
| 99 | mi.arguments.push_back(PropertyInfo(p_args[i], "arg" + itos(i + 1))); |
| 100 | } |
| 101 | ADD_SIGNAL(mi); |
| 102 | } |
| 103 | |
| 104 | JNISingleton() {} |
| 105 | |
| 106 | JNISingleton(const Ref<JavaObject> &p_wrapped_object) { |
| 107 | wrapped_object = p_wrapped_object; |
| 108 | } |
| 109 | |
| 110 | ~JNISingleton() { |
| 111 | method_map.clear(); |
| 112 | wrapped_object.unref(); |
| 113 | } |
| 114 | }; |