1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144 | /**************************************************************************/
/* godot_plugin_jni.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#include "godot_plugin_jni.h"
#include "api/java_class_wrapper.h"
#include "api/jni_singleton.h"
#include "jni_utils.h"
#include "core/config/engine.h"
#include "core/error/error_macros.h"
static HashMap<String, JNISingleton *> jni_singletons;
void unregister_plugins_singletons() {
for (const KeyValue<String, JNISingleton *> &E : jni_singletons) {
Engine::get_singleton()->remove_singleton(E.key);
if (E.value) {
memdelete(E.value);
}
}
jni_singletons.clear();
}
extern "C" {
JNIEXPORT jboolean JNICALL Java_org_godotengine_godot_plugin_GodotPlugin_nativeRegisterSingleton(JNIEnv *env, jclass clazz, jstring name, jobject obj) {
String singname = jstring_to_string(name, env);
ERR_FAIL_COND_V(jni_singletons.has(singname), false);
jclass java_class = env->GetObjectClass(obj);
Ref<JavaClass> java_class_wrapped = JavaClassWrapper::get_singleton()->wrap_jclass(java_class, true);
env->DeleteLocalRef(java_class);
Ref<JavaObject> plugin_object = memnew(JavaObject(java_class_wrapped, obj));
JNISingleton *plugin_singleton = memnew(JNISingleton(plugin_object));
jni_singletons[singname] = plugin_singleton;
Engine::get_singleton()->add_singleton(Engine::Singleton(singname, plugin_singleton));
return true;
}
JNIEXPORT void JNICALL Java_org_godotengine_godot_plugin_GodotPlugin_nativeRegisterMethod(JNIEnv *env, jclass clazz, jstring sname, jstring name, jstring ret, jobjectArray args) {
String singname = jstring_to_string(sname, env);
ERR_FAIL_COND(!jni_singletons.has(singname));
JNISingleton *s = jni_singletons.get(singname);
String mname = jstring_to_string(name, env);
String retval = jstring_to_string(ret, env);
Vector<Variant::Type> types;
int stringCount = env->GetArrayLength(args);
for (int i = 0; i < stringCount; i++) {
jstring string = (jstring)env->GetObjectArrayElement(args, i);
const String rawString = jstring_to_string(string, env);
types.push_back(get_jni_type(rawString));
}
s->add_method(mname, types, get_jni_type(retval));
}
JNIEXPORT void JNICALL Java_org_godotengine_godot_plugin_GodotPlugin_nativeRegisterSignal(JNIEnv *env, jclass clazz, jstring j_plugin_name, jstring j_signal_name, jobjectArray j_signal_param_types) {
String singleton_name = jstring_to_string(j_plugin_name, env);
ERR_FAIL_COND(!jni_singletons.has(singleton_name));
JNISingleton *singleton = jni_singletons.get(singleton_name);
String signal_name = jstring_to_string(j_signal_name, env);
Vector<Variant::Type> types;
int stringCount = env->GetArrayLength(j_signal_param_types);
for (int i = 0; i < stringCount; i++) {
jstring j_signal_param_type = (jstring)env->GetObjectArrayElement(j_signal_param_types, i);
const String signal_param_type = jstring_to_string(j_signal_param_type, env);
types.push_back(get_jni_type(signal_param_type));
}
singleton->add_signal(signal_name, types);
}
JNIEXPORT void JNICALL Java_org_godotengine_godot_plugin_GodotPlugin_nativeEmitSignal(JNIEnv *env, jclass clazz, jstring j_plugin_name, jstring j_signal_name, jobjectArray j_signal_params) {
String singleton_name = jstring_to_string(j_plugin_name, env);
ERR_FAIL_COND(!jni_singletons.has(singleton_name));
JNISingleton *singleton = jni_singletons.get(singleton_name);
String signal_name = jstring_to_string(j_signal_name, env);
int count = env->GetArrayLength(j_signal_params);
Variant *variant_params = (Variant *)alloca(sizeof(Variant) * count);<--- Obsolete function 'alloca' called. [+]The obsolete function 'alloca' is called. In C++11 and later it is recommended to use std::array<> or a dynamically allocated array instead. The function 'alloca' is dangerous for many reasons (http://stackoverflow.com/questions/1018853/why-is-alloca-not-considered-good-practice and http://linux.die.net/man/3/alloca).
const Variant **args = (const Variant **)alloca(sizeof(Variant *) * count);<--- Obsolete function 'alloca' called. [+]The obsolete function 'alloca' is called. In C++11 and later it is recommended to use std::array<> or a dynamically allocated array instead. The function 'alloca' is dangerous for many reasons (http://stackoverflow.com/questions/1018853/why-is-alloca-not-considered-good-practice and http://linux.die.net/man/3/alloca).
for (int i = 0; i < count; i++) {
jobject j_param = env->GetObjectArrayElement(j_signal_params, i);
ERR_FAIL_NULL(j_param);
memnew_placement(&variant_params[i], Variant(_jobject_to_variant(env, j_param)));
args[i] = &variant_params[i];
env->DeleteLocalRef(j_param);
}
singleton->emit_signalp(StringName(signal_name), args, count);
// Manually invoke the destructor to decrease the reference counts for the variant arguments.
for (int i = 0; i < count; i++) {
variant_params[i].~Variant();
}
}
}
|