Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/platform/android/api/jni_singleton.h
11353 views
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
RBMap<StringName, MethodData>::Element *E = method_map.find(p_method);
65
if (E) {
66
if (wrapped_object.is_valid()) {
67
// Check that the arguments match.
68
int method_arg_count = E->get().argtypes.size();
69
if (p_argcount < method_arg_count) {
70
r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
71
} else if (p_argcount > method_arg_count) {
72
r_error.error = Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS;
73
} else {
74
// Check the arguments are valid.
75
bool arguments_valid = true;
76
for (int i = 0; i < p_argcount; i++) {
77
if (!Variant::can_convert(p_args[i]->get_type(), E->get().argtypes[i])) {
78
arguments_valid = false;
79
break;
80
}
81
}
82
83
if (arguments_valid) {
84
return wrapped_object->callp(p_method, p_args, p_argcount, r_error);
85
} else {
86
r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
87
}
88
}
89
} else {
90
r_error.error = Callable::CallError::CALL_ERROR_INSTANCE_IS_NULL;
91
}
92
}
93
return Variant();
94
}
95
96
Ref<JavaObject> get_wrapped_object() const {
97
return wrapped_object;
98
}
99
100
bool has_java_method(const StringName &p_method) const {
101
return method_map.has(p_method);
102
}
103
104
void add_method(const StringName &p_name, const Vector<Variant::Type> &p_args, Variant::Type p_ret_type) {
105
MethodData md;
106
md.argtypes = p_args;
107
md.ret_type = p_ret_type;
108
method_map[p_name] = md;
109
}
110
111
void add_signal(const StringName &p_name, const Vector<Variant::Type> &p_args) {
112
MethodInfo mi;
113
mi.name = p_name;
114
for (int i = 0; i < p_args.size(); i++) {
115
mi.arguments.push_back(PropertyInfo(p_args[i], "arg" + itos(i + 1)));
116
}
117
ADD_SIGNAL(mi);
118
}
119
120
JNISingleton() {}
121
122
JNISingleton(const Ref<JavaObject> &p_wrapped_object) {
123
wrapped_object = p_wrapped_object;
124
}
125
126
~JNISingleton() {
127
method_map.clear();
128
wrapped_object.unref();
129
}
130
};
131
132