Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/platform/android/api/jni_singleton.cpp
46004 views
1
/**************************************************************************/
2
/* jni_singleton.cpp */
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
#include "jni_singleton.h"
32
33
#include "core/object/class_db.h"
34
35
void JNISingleton::_bind_methods() {
36
ClassDB::bind_method(D_METHOD("has_java_method", "method"), &JNISingleton::has_java_method);
37
}
38
39
Variant JNISingleton::callp(const StringName &p_method, const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
40
// Godot methods take precedence.
41
Variant ret = Object::callp(p_method, p_args, p_argcount, r_error);
42
if (r_error.error == Callable::CallError::CALL_OK) {
43
return ret;
44
}
45
46
// Check the method we're looking for is in the JNISingleton map.
47
// This is done because JNISingletons register methods differently than wrapped JavaClass / JavaObject to allow
48
// for access to private methods annotated with the @UsedByGodot annotation.
49
// In the future, we should remove access to private methods and require that JNISingletons' methods exposed to
50
// GDScript be all public, similarly to what we do for wrapped JavaClass / JavaObject methods. Doing so will
51
// also allow dropping and deprecating the @UsedByGodot annotation.
52
RBMap<StringName, MethodData>::Element *E = method_map.find(p_method);
53
if (E) {
54
if (wrapped_object.is_valid()) {
55
return wrapped_object->callp(p_method, p_args, p_argcount, r_error);
56
} else {
57
r_error.error = Callable::CallError::CALL_ERROR_INSTANCE_IS_NULL;
58
}
59
}
60
return Variant();
61
}
62
63
void JNISingleton::add_method(const StringName &p_name, const Vector<Variant::Type> &p_args, Variant::Type p_ret_type) {
64
MethodData md;
65
md.argtypes = p_args;
66
md.ret_type = p_ret_type;
67
method_map[p_name] = md;
68
}
69
70
void JNISingleton::add_signal(const StringName &p_name, const Vector<Variant::Type> &p_args) {
71
MethodInfo mi;
72
mi.name = p_name;
73
for (int i = 0; i < p_args.size(); i++) {
74
mi.arguments.push_back(PropertyInfo(p_args[i], "arg" + itos(i + 1)));
75
}
76
ADD_SIGNAL(mi);
77
}
78
79