feat: godot-engine-source-4.3-stable
This commit is contained in:
parent
c59a7dcade
commit
7125d019b5
11149 changed files with 5070401 additions and 0 deletions
89
engine/platform/android/api/api.cpp
Normal file
89
engine/platform/android/api/api.cpp
Normal file
|
|
@ -0,0 +1,89 @@
|
|||
/**************************************************************************/
|
||||
/* api.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 "api.h"
|
||||
|
||||
#include "java_class_wrapper.h"
|
||||
#include "jni_singleton.h"
|
||||
|
||||
#include "core/config/engine.h"
|
||||
|
||||
#if !defined(ANDROID_ENABLED)
|
||||
static JavaClassWrapper *java_class_wrapper = nullptr;
|
||||
#endif
|
||||
|
||||
void register_android_api() {
|
||||
#if !defined(ANDROID_ENABLED)
|
||||
// On Android platforms, the `java_class_wrapper` instantiation and the
|
||||
// `JNISingleton` registration occurs in
|
||||
// `platform/android/java_godot_lib_jni.cpp#Java_org_godotengine_godot_GodotLib_setup`
|
||||
java_class_wrapper = memnew(JavaClassWrapper); // Dummy
|
||||
GDREGISTER_CLASS(JNISingleton);
|
||||
#endif
|
||||
|
||||
GDREGISTER_CLASS(JavaClass);
|
||||
GDREGISTER_CLASS(JavaClassWrapper);
|
||||
Engine::get_singleton()->add_singleton(Engine::Singleton("JavaClassWrapper", JavaClassWrapper::get_singleton()));
|
||||
}
|
||||
|
||||
void unregister_android_api() {
|
||||
#if !defined(ANDROID_ENABLED)
|
||||
memdelete(java_class_wrapper);
|
||||
#endif
|
||||
}
|
||||
|
||||
void JavaClassWrapper::_bind_methods() {
|
||||
ClassDB::bind_method(D_METHOD("wrap", "name"), &JavaClassWrapper::wrap);
|
||||
}
|
||||
|
||||
#if !defined(ANDROID_ENABLED)
|
||||
|
||||
Variant JavaClass::callp(const StringName &, const Variant **, int, Callable::CallError &) {
|
||||
return Variant();
|
||||
}
|
||||
|
||||
JavaClass::JavaClass() {
|
||||
}
|
||||
|
||||
Variant JavaObject::callp(const StringName &, const Variant **, int, Callable::CallError &) {
|
||||
return Variant();
|
||||
}
|
||||
|
||||
JavaClassWrapper *JavaClassWrapper::singleton = nullptr;
|
||||
|
||||
Ref<JavaClass> JavaClassWrapper::wrap(const String &) {
|
||||
return Ref<JavaClass>();
|
||||
}
|
||||
|
||||
JavaClassWrapper::JavaClassWrapper() {
|
||||
singleton = this;
|
||||
}
|
||||
|
||||
#endif
|
||||
37
engine/platform/android/api/api.h
Normal file
37
engine/platform/android/api/api.h
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
/**************************************************************************/
|
||||
/* api.h */
|
||||
/**************************************************************************/
|
||||
/* 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. */
|
||||
/**************************************************************************/
|
||||
|
||||
#ifndef ANDROID_API_H
|
||||
#define ANDROID_API_H
|
||||
|
||||
void register_android_api();
|
||||
void unregister_android_api();
|
||||
|
||||
#endif // ANDROID_API_H
|
||||
251
engine/platform/android/api/java_class_wrapper.h
Normal file
251
engine/platform/android/api/java_class_wrapper.h
Normal file
|
|
@ -0,0 +1,251 @@
|
|||
/**************************************************************************/
|
||||
/* java_class_wrapper.h */
|
||||
/**************************************************************************/
|
||||
/* 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. */
|
||||
/**************************************************************************/
|
||||
|
||||
#ifndef JAVA_CLASS_WRAPPER_H
|
||||
#define JAVA_CLASS_WRAPPER_H
|
||||
|
||||
#include "core/object/ref_counted.h"
|
||||
|
||||
#ifdef ANDROID_ENABLED
|
||||
#include <android/log.h>
|
||||
#include <jni.h>
|
||||
#endif
|
||||
|
||||
#ifdef ANDROID_ENABLED
|
||||
class JavaObject;
|
||||
#endif
|
||||
|
||||
class JavaClass : public RefCounted {
|
||||
GDCLASS(JavaClass, RefCounted);
|
||||
|
||||
#ifdef ANDROID_ENABLED
|
||||
enum ArgumentType{
|
||||
ARG_TYPE_VOID,
|
||||
ARG_TYPE_BOOLEAN,
|
||||
ARG_TYPE_BYTE,
|
||||
ARG_TYPE_CHAR,
|
||||
ARG_TYPE_SHORT,
|
||||
ARG_TYPE_INT,
|
||||
ARG_TYPE_LONG,
|
||||
ARG_TYPE_FLOAT,
|
||||
ARG_TYPE_DOUBLE,
|
||||
ARG_TYPE_STRING, //special case
|
||||
ARG_TYPE_CLASS,
|
||||
ARG_ARRAY_BIT = 1 << 16,
|
||||
ARG_NUMBER_CLASS_BIT = 1 << 17,
|
||||
ARG_TYPE_MASK = (1 << 16) - 1
|
||||
};
|
||||
|
||||
RBMap<StringName, Variant> constant_map;
|
||||
|
||||
struct MethodInfo {
|
||||
bool _static = false;
|
||||
Vector<uint32_t> param_types;
|
||||
Vector<StringName> param_sigs;
|
||||
uint32_t return_type = 0;
|
||||
jmethodID method;
|
||||
};
|
||||
|
||||
_FORCE_INLINE_ static void _convert_to_variant_type(int p_sig, Variant::Type &r_type, float &likelihood) {
|
||||
likelihood = 1.0;
|
||||
r_type = Variant::NIL;
|
||||
|
||||
switch (p_sig) {
|
||||
case ARG_TYPE_VOID:
|
||||
r_type = Variant::NIL;
|
||||
break;
|
||||
case ARG_TYPE_BOOLEAN | ARG_NUMBER_CLASS_BIT:
|
||||
case ARG_TYPE_BOOLEAN:
|
||||
r_type = Variant::BOOL;
|
||||
break;
|
||||
case ARG_TYPE_BYTE | ARG_NUMBER_CLASS_BIT:
|
||||
case ARG_TYPE_BYTE:
|
||||
r_type = Variant::INT;
|
||||
likelihood = 0.1;
|
||||
break;
|
||||
case ARG_TYPE_CHAR | ARG_NUMBER_CLASS_BIT:
|
||||
case ARG_TYPE_CHAR:
|
||||
r_type = Variant::INT;
|
||||
likelihood = 0.2;
|
||||
break;
|
||||
case ARG_TYPE_SHORT | ARG_NUMBER_CLASS_BIT:
|
||||
case ARG_TYPE_SHORT:
|
||||
r_type = Variant::INT;
|
||||
likelihood = 0.3;
|
||||
break;
|
||||
case ARG_TYPE_INT | ARG_NUMBER_CLASS_BIT:
|
||||
case ARG_TYPE_INT:
|
||||
r_type = Variant::INT;
|
||||
likelihood = 1.0;
|
||||
break;
|
||||
case ARG_TYPE_LONG | ARG_NUMBER_CLASS_BIT:
|
||||
case ARG_TYPE_LONG:
|
||||
r_type = Variant::INT;
|
||||
likelihood = 0.5;
|
||||
break;
|
||||
case ARG_TYPE_FLOAT | ARG_NUMBER_CLASS_BIT:
|
||||
case ARG_TYPE_FLOAT:
|
||||
r_type = Variant::FLOAT;
|
||||
likelihood = 1.0;
|
||||
break;
|
||||
case ARG_TYPE_DOUBLE | ARG_NUMBER_CLASS_BIT:
|
||||
case ARG_TYPE_DOUBLE:
|
||||
r_type = Variant::FLOAT;
|
||||
likelihood = 0.5;
|
||||
break;
|
||||
case ARG_TYPE_STRING:
|
||||
r_type = Variant::STRING;
|
||||
break;
|
||||
case ARG_TYPE_CLASS:
|
||||
r_type = Variant::OBJECT;
|
||||
break;
|
||||
case ARG_ARRAY_BIT | ARG_TYPE_VOID:
|
||||
r_type = Variant::NIL;
|
||||
break;
|
||||
case ARG_ARRAY_BIT | ARG_TYPE_BOOLEAN:
|
||||
r_type = Variant::ARRAY;
|
||||
break;
|
||||
case ARG_ARRAY_BIT | ARG_TYPE_BYTE:
|
||||
r_type = Variant::PACKED_BYTE_ARRAY;
|
||||
likelihood = 1.0;
|
||||
break;
|
||||
case ARG_ARRAY_BIT | ARG_TYPE_CHAR:
|
||||
r_type = Variant::PACKED_BYTE_ARRAY;
|
||||
likelihood = 0.5;
|
||||
break;
|
||||
case ARG_ARRAY_BIT | ARG_TYPE_SHORT:
|
||||
r_type = Variant::PACKED_INT32_ARRAY;
|
||||
likelihood = 0.3;
|
||||
break;
|
||||
case ARG_ARRAY_BIT | ARG_TYPE_INT:
|
||||
r_type = Variant::PACKED_INT32_ARRAY;
|
||||
likelihood = 1.0;
|
||||
break;
|
||||
case ARG_ARRAY_BIT | ARG_TYPE_LONG:
|
||||
r_type = Variant::PACKED_INT32_ARRAY;
|
||||
likelihood = 0.5;
|
||||
break;
|
||||
case ARG_ARRAY_BIT | ARG_TYPE_FLOAT:
|
||||
r_type = Variant::PACKED_FLOAT32_ARRAY;
|
||||
likelihood = 1.0;
|
||||
break;
|
||||
case ARG_ARRAY_BIT | ARG_TYPE_DOUBLE:
|
||||
r_type = Variant::PACKED_FLOAT32_ARRAY;
|
||||
likelihood = 0.5;
|
||||
break;
|
||||
case ARG_ARRAY_BIT | ARG_TYPE_STRING:
|
||||
r_type = Variant::PACKED_STRING_ARRAY;
|
||||
break;
|
||||
case ARG_ARRAY_BIT | ARG_TYPE_CLASS:
|
||||
r_type = Variant::ARRAY;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
_FORCE_INLINE_ static bool _convert_object_to_variant(JNIEnv *env, jobject obj, Variant &var, uint32_t p_sig);
|
||||
|
||||
bool _call_method(JavaObject *p_instance, const StringName &p_method, const Variant **p_args, int p_argcount, Callable::CallError &r_error, Variant &ret);
|
||||
|
||||
friend class JavaClassWrapper;
|
||||
HashMap<StringName, List<MethodInfo>> methods;
|
||||
jclass _class;
|
||||
#endif
|
||||
|
||||
public:
|
||||
virtual Variant callp(const StringName &p_method, const Variant **p_args, int p_argcount, Callable::CallError &r_error) override;
|
||||
|
||||
JavaClass();
|
||||
};
|
||||
|
||||
class JavaObject : public RefCounted {
|
||||
GDCLASS(JavaObject, RefCounted);
|
||||
|
||||
#ifdef ANDROID_ENABLED
|
||||
Ref<JavaClass> base_class;
|
||||
friend class JavaClass;
|
||||
|
||||
jobject instance;
|
||||
#endif
|
||||
|
||||
public:
|
||||
virtual Variant callp(const StringName &p_method, const Variant **p_args, int p_argcount, Callable::CallError &r_error) override;
|
||||
|
||||
#ifdef ANDROID_ENABLED
|
||||
JavaObject(const Ref<JavaClass> &p_base, jobject *p_instance);
|
||||
~JavaObject();
|
||||
#endif
|
||||
};
|
||||
|
||||
class JavaClassWrapper : public Object {
|
||||
GDCLASS(JavaClassWrapper, Object);
|
||||
|
||||
#ifdef ANDROID_ENABLED
|
||||
RBMap<String, Ref<JavaClass>> class_cache;
|
||||
friend class JavaClass;
|
||||
jmethodID getDeclaredMethods;
|
||||
jmethodID getFields;
|
||||
jmethodID getParameterTypes;
|
||||
jmethodID getReturnType;
|
||||
jmethodID getModifiers;
|
||||
jmethodID getName;
|
||||
jmethodID Class_getName;
|
||||
jmethodID Field_getName;
|
||||
jmethodID Field_getModifiers;
|
||||
jmethodID Field_get;
|
||||
jmethodID Boolean_booleanValue;
|
||||
jmethodID Byte_byteValue;
|
||||
jmethodID Character_characterValue;
|
||||
jmethodID Short_shortValue;
|
||||
jmethodID Integer_integerValue;
|
||||
jmethodID Long_longValue;
|
||||
jmethodID Float_floatValue;
|
||||
jmethodID Double_doubleValue;
|
||||
|
||||
bool _get_type_sig(JNIEnv *env, jobject obj, uint32_t &sig, String &strsig);
|
||||
#endif
|
||||
|
||||
static JavaClassWrapper *singleton;
|
||||
|
||||
protected:
|
||||
static void _bind_methods();
|
||||
|
||||
public:
|
||||
static JavaClassWrapper *get_singleton() { return singleton; }
|
||||
|
||||
Ref<JavaClass> wrap(const String &p_class);
|
||||
|
||||
#ifdef ANDROID_ENABLED
|
||||
JavaClassWrapper(jobject p_activity = nullptr);
|
||||
#else
|
||||
JavaClassWrapper();
|
||||
#endif
|
||||
};
|
||||
|
||||
#endif // JAVA_CLASS_WRAPPER_H
|
||||
251
engine/platform/android/api/jni_singleton.h
Normal file
251
engine/platform/android/api/jni_singleton.h
Normal file
|
|
@ -0,0 +1,251 @@
|
|||
/**************************************************************************/
|
||||
/* jni_singleton.h */
|
||||
/**************************************************************************/
|
||||
/* 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. */
|
||||
/**************************************************************************/
|
||||
|
||||
#ifndef JNI_SINGLETON_H
|
||||
#define JNI_SINGLETON_H
|
||||
|
||||
#include "core/config/engine.h"
|
||||
#include "core/variant/variant.h"
|
||||
|
||||
#ifdef ANDROID_ENABLED
|
||||
#include "jni_utils.h"
|
||||
#endif
|
||||
|
||||
class JNISingleton : public Object {
|
||||
GDCLASS(JNISingleton, Object);
|
||||
|
||||
#ifdef ANDROID_ENABLED
|
||||
struct MethodData {
|
||||
jmethodID method;
|
||||
Variant::Type ret_type;
|
||||
Vector<Variant::Type> argtypes;
|
||||
};
|
||||
|
||||
jobject instance;
|
||||
RBMap<StringName, MethodData> method_map;
|
||||
#endif
|
||||
|
||||
public:
|
||||
virtual Variant callp(const StringName &p_method, const Variant **p_args, int p_argcount, Callable::CallError &r_error) override {
|
||||
#ifdef ANDROID_ENABLED
|
||||
RBMap<StringName, MethodData>::Element *E = method_map.find(p_method);
|
||||
|
||||
// Check the method we're looking for is in the JNISingleton map and that
|
||||
// the arguments match.
|
||||
bool call_error = !E || E->get().argtypes.size() != p_argcount;
|
||||
if (!call_error) {
|
||||
for (int i = 0; i < p_argcount; i++) {
|
||||
if (!Variant::can_convert(p_args[i]->get_type(), E->get().argtypes[i])) {
|
||||
call_error = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (call_error) {
|
||||
// The method is not in this map, defaulting to the regular instance calls.
|
||||
return Object::callp(p_method, p_args, p_argcount, r_error);
|
||||
}
|
||||
|
||||
ERR_FAIL_NULL_V(instance, Variant());
|
||||
|
||||
r_error.error = Callable::CallError::CALL_OK;
|
||||
|
||||
jvalue *v = nullptr;
|
||||
|
||||
if (p_argcount) {
|
||||
v = (jvalue *)alloca(sizeof(jvalue) * p_argcount);
|
||||
}
|
||||
|
||||
JNIEnv *env = get_jni_env();
|
||||
|
||||
int res = env->PushLocalFrame(16);
|
||||
|
||||
ERR_FAIL_COND_V(res != 0, Variant());
|
||||
|
||||
List<jobject> to_erase;
|
||||
for (int i = 0; i < p_argcount; i++) {
|
||||
jvalret vr = _variant_to_jvalue(env, E->get().argtypes[i], p_args[i]);
|
||||
v[i] = vr.val;
|
||||
if (vr.obj) {
|
||||
to_erase.push_back(vr.obj);
|
||||
}
|
||||
}
|
||||
|
||||
Variant ret;
|
||||
|
||||
switch (E->get().ret_type) {
|
||||
case Variant::NIL: {
|
||||
env->CallVoidMethodA(instance, E->get().method, v);
|
||||
} break;
|
||||
case Variant::BOOL: {
|
||||
ret = env->CallBooleanMethodA(instance, E->get().method, v) == JNI_TRUE;
|
||||
} break;
|
||||
case Variant::INT: {
|
||||
ret = env->CallIntMethodA(instance, E->get().method, v);
|
||||
} break;
|
||||
case Variant::FLOAT: {
|
||||
ret = env->CallFloatMethodA(instance, E->get().method, v);
|
||||
} break;
|
||||
case Variant::STRING: {
|
||||
jobject o = env->CallObjectMethodA(instance, E->get().method, v);
|
||||
ret = jstring_to_string((jstring)o, env);
|
||||
env->DeleteLocalRef(o);
|
||||
} break;
|
||||
case Variant::PACKED_STRING_ARRAY: {
|
||||
jobjectArray arr = (jobjectArray)env->CallObjectMethodA(instance, E->get().method, v);
|
||||
|
||||
ret = _jobject_to_variant(env, arr);
|
||||
|
||||
env->DeleteLocalRef(arr);
|
||||
} break;
|
||||
case Variant::PACKED_INT32_ARRAY: {
|
||||
jintArray arr = (jintArray)env->CallObjectMethodA(instance, E->get().method, v);
|
||||
|
||||
int fCount = env->GetArrayLength(arr);
|
||||
Vector<int> sarr;
|
||||
sarr.resize(fCount);
|
||||
|
||||
int *w = sarr.ptrw();
|
||||
env->GetIntArrayRegion(arr, 0, fCount, w);
|
||||
ret = sarr;
|
||||
env->DeleteLocalRef(arr);
|
||||
} break;
|
||||
case Variant::PACKED_INT64_ARRAY: {
|
||||
jlongArray arr = (jlongArray)env->CallObjectMethodA(instance, E->get().method, v);
|
||||
|
||||
int fCount = env->GetArrayLength(arr);
|
||||
Vector<int64_t> sarr;
|
||||
sarr.resize(fCount);
|
||||
|
||||
int64_t *w = sarr.ptrw();
|
||||
env->GetLongArrayRegion(arr, 0, fCount, w);
|
||||
ret = sarr;
|
||||
env->DeleteLocalRef(arr);
|
||||
} break;
|
||||
case Variant::PACKED_FLOAT32_ARRAY: {
|
||||
jfloatArray arr = (jfloatArray)env->CallObjectMethodA(instance, E->get().method, v);
|
||||
|
||||
int fCount = env->GetArrayLength(arr);
|
||||
Vector<float> sarr;
|
||||
sarr.resize(fCount);
|
||||
|
||||
float *w = sarr.ptrw();
|
||||
env->GetFloatArrayRegion(arr, 0, fCount, w);
|
||||
ret = sarr;
|
||||
env->DeleteLocalRef(arr);
|
||||
} break;
|
||||
case Variant::PACKED_FLOAT64_ARRAY: {
|
||||
jdoubleArray arr = (jdoubleArray)env->CallObjectMethodA(instance, E->get().method, v);
|
||||
|
||||
int fCount = env->GetArrayLength(arr);
|
||||
Vector<double> sarr;
|
||||
sarr.resize(fCount);
|
||||
|
||||
double *w = sarr.ptrw();
|
||||
env->GetDoubleArrayRegion(arr, 0, fCount, w);
|
||||
ret = sarr;
|
||||
env->DeleteLocalRef(arr);
|
||||
} break;
|
||||
case Variant::DICTIONARY: {
|
||||
jobject obj = env->CallObjectMethodA(instance, E->get().method, v);
|
||||
ret = _jobject_to_variant(env, obj);
|
||||
env->DeleteLocalRef(obj);
|
||||
|
||||
} break;
|
||||
default: {
|
||||
env->PopLocalFrame(nullptr);
|
||||
ERR_FAIL_V(Variant());
|
||||
} break;
|
||||
}
|
||||
|
||||
while (to_erase.size()) {
|
||||
env->DeleteLocalRef(to_erase.front()->get());
|
||||
to_erase.pop_front();
|
||||
}
|
||||
|
||||
env->PopLocalFrame(nullptr);
|
||||
|
||||
return ret;
|
||||
#else // ANDROID_ENABLED
|
||||
|
||||
// Defaulting to the regular instance calls.
|
||||
return Object::callp(p_method, p_args, p_argcount, r_error);
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifdef ANDROID_ENABLED
|
||||
jobject get_instance() const {
|
||||
return instance;
|
||||
}
|
||||
|
||||
void set_instance(jobject p_instance) {
|
||||
instance = p_instance;
|
||||
}
|
||||
|
||||
void add_method(const StringName &p_name, jmethodID p_method, const Vector<Variant::Type> &p_args, Variant::Type p_ret_type) {
|
||||
MethodData md;
|
||||
md.method = p_method;
|
||||
md.argtypes = p_args;
|
||||
md.ret_type = p_ret_type;
|
||||
method_map[p_name] = md;
|
||||
}
|
||||
|
||||
void add_signal(const StringName &p_name, const Vector<Variant::Type> &p_args) {
|
||||
MethodInfo mi;
|
||||
mi.name = p_name;
|
||||
for (int i = 0; i < p_args.size(); i++) {
|
||||
mi.arguments.push_back(PropertyInfo(p_args[i], "arg" + itos(i + 1)));
|
||||
}
|
||||
ADD_SIGNAL(mi);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
JNISingleton() {
|
||||
#ifdef ANDROID_ENABLED
|
||||
instance = nullptr;
|
||||
#endif
|
||||
}
|
||||
|
||||
~JNISingleton() {
|
||||
#ifdef ANDROID_ENABLED
|
||||
method_map.clear();
|
||||
if (instance) {
|
||||
JNIEnv *env = get_jni_env();
|
||||
ERR_FAIL_NULL(env);
|
||||
|
||||
env->DeleteGlobalRef(instance);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
};
|
||||
|
||||
#endif // JNI_SINGLETON_H
|
||||
Loading…
Add table
Add a link
Reference in a new issue