Path: blob/master/platform/android/api/jni_singleton.h
11353 views
/**************************************************************************/1/* jni_singleton.h */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#pragma once3132#include "java_class_wrapper.h"3334#include "core/config/engine.h"35#include "core/templates/rb_map.h"36#include "core/variant/variant.h"3738class JNISingleton : public Object {39GDCLASS(JNISingleton, Object);4041struct MethodData {42Variant::Type ret_type;43Vector<Variant::Type> argtypes;44};4546RBMap<StringName, MethodData> method_map;47Ref<JavaObject> wrapped_object;4849protected:50static void _bind_methods() {51ClassDB::bind_method(D_METHOD("has_java_method", "method"), &JNISingleton::has_java_method);52}5354public:55virtual Variant callp(const StringName &p_method, const Variant **p_args, int p_argcount, Callable::CallError &r_error) override {56// Godot methods take precedence.57Variant ret = Object::callp(p_method, p_args, p_argcount, r_error);58if (r_error.error == Callable::CallError::CALL_OK) {59return ret;60}6162// Check the method we're looking for is in the JNISingleton map.63RBMap<StringName, MethodData>::Element *E = method_map.find(p_method);64if (E) {65if (wrapped_object.is_valid()) {66// Check that the arguments match.67int method_arg_count = E->get().argtypes.size();68if (p_argcount < method_arg_count) {69r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;70} else if (p_argcount > method_arg_count) {71r_error.error = Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS;72} else {73// Check the arguments are valid.74bool arguments_valid = true;75for (int i = 0; i < p_argcount; i++) {76if (!Variant::can_convert(p_args[i]->get_type(), E->get().argtypes[i])) {77arguments_valid = false;78break;79}80}8182if (arguments_valid) {83return wrapped_object->callp(p_method, p_args, p_argcount, r_error);84} else {85r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;86}87}88} else {89r_error.error = Callable::CallError::CALL_ERROR_INSTANCE_IS_NULL;90}91}92return Variant();93}9495Ref<JavaObject> get_wrapped_object() const {96return wrapped_object;97}9899bool has_java_method(const StringName &p_method) const {100return method_map.has(p_method);101}102103void add_method(const StringName &p_name, const Vector<Variant::Type> &p_args, Variant::Type p_ret_type) {104MethodData md;105md.argtypes = p_args;106md.ret_type = p_ret_type;107method_map[p_name] = md;108}109110void add_signal(const StringName &p_name, const Vector<Variant::Type> &p_args) {111MethodInfo mi;112mi.name = p_name;113for (int i = 0; i < p_args.size(); i++) {114mi.arguments.push_back(PropertyInfo(p_args[i], "arg" + itos(i + 1)));115}116ADD_SIGNAL(mi);117}118119JNISingleton() {}120121JNISingleton(const Ref<JavaObject> &p_wrapped_object) {122wrapped_object = p_wrapped_object;123}124125~JNISingleton() {126method_map.clear();127wrapped_object.unref();128}129};130131132