2016-06-18 12:46:12 +00:00
|
|
|
/**************************************************************************/
|
|
|
|
/* 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. */
|
|
|
|
/**************************************************************************/
|
2018-01-04 23:50:27 +00:00
|
|
|
|
2014-09-03 02:13:40 +00:00
|
|
|
#ifndef JAVA_CLASS_WRAPPER_H
|
|
|
|
#define JAVA_CLASS_WRAPPER_H
|
|
|
|
|
2021-06-04 16:03:15 +00:00
|
|
|
#include "core/object/ref_counted.h"
|
2020-01-19 19:02:40 +00:00
|
|
|
|
|
|
|
#ifdef ANDROID_ENABLED
|
2014-09-03 02:13:40 +00:00
|
|
|
#include <android/log.h>
|
|
|
|
#include <jni.h>
|
2020-01-19 19:02:40 +00:00
|
|
|
#endif
|
2014-09-03 02:13:40 +00:00
|
|
|
|
2020-01-19 19:02:40 +00:00
|
|
|
#ifdef ANDROID_ENABLED
|
2014-09-03 02:13:40 +00:00
|
|
|
class JavaObject;
|
2020-01-19 19:02:40 +00:00
|
|
|
#endif
|
2014-09-03 02:13:40 +00:00
|
|
|
|
2021-06-04 16:03:15 +00:00
|
|
|
class JavaClass : public RefCounted {
|
|
|
|
GDCLASS(JavaClass, RefCounted);
|
2014-09-03 02:13:40 +00:00
|
|
|
|
2020-01-19 19:02:40 +00:00
|
|
|
#ifdef ANDROID_ENABLED
|
|
|
|
enum ArgumentType{
|
2014-09-03 02:13:40 +00:00
|
|
|
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
|
|
|
|
};
|
|
|
|
|
2022-05-13 13:04:37 +00:00
|
|
|
RBMap<StringName, Variant> constant_map;
|
2014-09-03 02:13:40 +00:00
|
|
|
|
|
|
|
struct MethodInfo {
|
2020-11-24 09:12:55 +00:00
|
|
|
bool _static = false;
|
2014-09-03 02:13:40 +00:00
|
|
|
Vector<uint32_t> param_types;
|
|
|
|
Vector<StringName> param_sigs;
|
2020-11-24 09:12:55 +00:00
|
|
|
uint32_t return_type = 0;
|
2014-09-03 02:13:40 +00:00
|
|
|
jmethodID method;
|
|
|
|
};
|
|
|
|
|
2017-03-24 20:45:31 +00:00
|
|
|
_FORCE_INLINE_ static void _convert_to_variant_type(int p_sig, Variant::Type &r_type, float &likelihood) {
|
|
|
|
likelihood = 1.0;
|
2014-09-03 02:13:40 +00:00
|
|
|
r_type = Variant::NIL;
|
2017-03-05 15:44:50 +00:00
|
|
|
|
2014-09-03 02:13:40 +00:00
|
|
|
switch (p_sig) {
|
2020-05-10 11:00:47 +00:00
|
|
|
case ARG_TYPE_VOID:
|
|
|
|
r_type = Variant::NIL;
|
|
|
|
break;
|
2014-09-03 02:13:40 +00:00
|
|
|
case ARG_TYPE_BOOLEAN | ARG_NUMBER_CLASS_BIT:
|
2020-05-10 11:00:47 +00:00
|
|
|
case ARG_TYPE_BOOLEAN:
|
|
|
|
r_type = Variant::BOOL;
|
|
|
|
break;
|
2014-09-03 02:13:40 +00:00
|
|
|
case ARG_TYPE_BYTE | ARG_NUMBER_CLASS_BIT:
|
|
|
|
case ARG_TYPE_BYTE:
|
|
|
|
r_type = Variant::INT;
|
2017-03-24 20:45:31 +00:00
|
|
|
likelihood = 0.1;
|
2014-09-03 02:13:40 +00:00
|
|
|
break;
|
|
|
|
case ARG_TYPE_CHAR | ARG_NUMBER_CLASS_BIT:
|
|
|
|
case ARG_TYPE_CHAR:
|
|
|
|
r_type = Variant::INT;
|
2017-03-24 20:45:31 +00:00
|
|
|
likelihood = 0.2;
|
2014-09-03 02:13:40 +00:00
|
|
|
break;
|
|
|
|
case ARG_TYPE_SHORT | ARG_NUMBER_CLASS_BIT:
|
|
|
|
case ARG_TYPE_SHORT:
|
|
|
|
r_type = Variant::INT;
|
2017-03-24 20:45:31 +00:00
|
|
|
likelihood = 0.3;
|
2014-09-03 02:13:40 +00:00
|
|
|
break;
|
|
|
|
case ARG_TYPE_INT | ARG_NUMBER_CLASS_BIT:
|
|
|
|
case ARG_TYPE_INT:
|
|
|
|
r_type = Variant::INT;
|
2017-03-24 20:45:31 +00:00
|
|
|
likelihood = 1.0;
|
2014-09-03 02:13:40 +00:00
|
|
|
break;
|
|
|
|
case ARG_TYPE_LONG | ARG_NUMBER_CLASS_BIT:
|
|
|
|
case ARG_TYPE_LONG:
|
|
|
|
r_type = Variant::INT;
|
2017-03-24 20:45:31 +00:00
|
|
|
likelihood = 0.5;
|
2014-09-03 02:13:40 +00:00
|
|
|
break;
|
|
|
|
case ARG_TYPE_FLOAT | ARG_NUMBER_CLASS_BIT:
|
|
|
|
case ARG_TYPE_FLOAT:
|
Variant: Added 64-bit packed arrays, renamed Variant::REAL to FLOAT.
- Renames PackedIntArray to PackedInt32Array.
- Renames PackedFloatArray to PackedFloat32Array.
- Adds PackedInt64Array and PackedFloat64Array.
- Renames Variant::REAL to Variant::FLOAT for consistency.
Packed arrays are for storing large amount of data and creating stuff like
meshes, buffers. textures, etc. Forcing them to be 64 is a huge waste of
memory. That said, many users requested the ability to have 64 bits packed
arrays for their games, so this is just an optional added type.
For Variant, the float datatype is always 64 bits, and exposed as `float`.
We still have `real_t` which is the datatype that can change from 32 to 64
bits depending on a compile flag (not entirely working right now, but that's
the idea). It affects math related datatypes and code only.
Neither Variant nor PackedArray make use of real_t, which is only intended
for math precision, so the term is removed from there to keep only float.
2020-02-24 18:20:53 +00:00
|
|
|
r_type = Variant::FLOAT;
|
2017-03-24 20:45:31 +00:00
|
|
|
likelihood = 1.0;
|
2014-09-03 02:13:40 +00:00
|
|
|
break;
|
|
|
|
case ARG_TYPE_DOUBLE | ARG_NUMBER_CLASS_BIT:
|
|
|
|
case ARG_TYPE_DOUBLE:
|
Variant: Added 64-bit packed arrays, renamed Variant::REAL to FLOAT.
- Renames PackedIntArray to PackedInt32Array.
- Renames PackedFloatArray to PackedFloat32Array.
- Adds PackedInt64Array and PackedFloat64Array.
- Renames Variant::REAL to Variant::FLOAT for consistency.
Packed arrays are for storing large amount of data and creating stuff like
meshes, buffers. textures, etc. Forcing them to be 64 is a huge waste of
memory. That said, many users requested the ability to have 64 bits packed
arrays for their games, so this is just an optional added type.
For Variant, the float datatype is always 64 bits, and exposed as `float`.
We still have `real_t` which is the datatype that can change from 32 to 64
bits depending on a compile flag (not entirely working right now, but that's
the idea). It affects math related datatypes and code only.
Neither Variant nor PackedArray make use of real_t, which is only intended
for math precision, so the term is removed from there to keep only float.
2020-02-24 18:20:53 +00:00
|
|
|
r_type = Variant::FLOAT;
|
2017-03-24 20:45:31 +00:00
|
|
|
likelihood = 0.5;
|
2014-09-03 02:13:40 +00:00
|
|
|
break;
|
2020-05-10 11:00:47 +00:00
|
|
|
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;
|
2017-01-11 03:52:51 +00:00
|
|
|
case ARG_ARRAY_BIT | ARG_TYPE_BYTE:
|
2020-02-17 21:06:54 +00:00
|
|
|
r_type = Variant::PACKED_BYTE_ARRAY;
|
2017-03-24 20:45:31 +00:00
|
|
|
likelihood = 1.0;
|
2017-01-11 03:52:51 +00:00
|
|
|
break;
|
|
|
|
case ARG_ARRAY_BIT | ARG_TYPE_CHAR:
|
2020-02-17 21:06:54 +00:00
|
|
|
r_type = Variant::PACKED_BYTE_ARRAY;
|
2017-03-24 20:45:31 +00:00
|
|
|
likelihood = 0.5;
|
2017-01-11 03:52:51 +00:00
|
|
|
break;
|
|
|
|
case ARG_ARRAY_BIT | ARG_TYPE_SHORT:
|
Variant: Added 64-bit packed arrays, renamed Variant::REAL to FLOAT.
- Renames PackedIntArray to PackedInt32Array.
- Renames PackedFloatArray to PackedFloat32Array.
- Adds PackedInt64Array and PackedFloat64Array.
- Renames Variant::REAL to Variant::FLOAT for consistency.
Packed arrays are for storing large amount of data and creating stuff like
meshes, buffers. textures, etc. Forcing them to be 64 is a huge waste of
memory. That said, many users requested the ability to have 64 bits packed
arrays for their games, so this is just an optional added type.
For Variant, the float datatype is always 64 bits, and exposed as `float`.
We still have `real_t` which is the datatype that can change from 32 to 64
bits depending on a compile flag (not entirely working right now, but that's
the idea). It affects math related datatypes and code only.
Neither Variant nor PackedArray make use of real_t, which is only intended
for math precision, so the term is removed from there to keep only float.
2020-02-24 18:20:53 +00:00
|
|
|
r_type = Variant::PACKED_INT32_ARRAY;
|
2017-03-24 20:45:31 +00:00
|
|
|
likelihood = 0.3;
|
2017-01-11 03:52:51 +00:00
|
|
|
break;
|
|
|
|
case ARG_ARRAY_BIT | ARG_TYPE_INT:
|
Variant: Added 64-bit packed arrays, renamed Variant::REAL to FLOAT.
- Renames PackedIntArray to PackedInt32Array.
- Renames PackedFloatArray to PackedFloat32Array.
- Adds PackedInt64Array and PackedFloat64Array.
- Renames Variant::REAL to Variant::FLOAT for consistency.
Packed arrays are for storing large amount of data and creating stuff like
meshes, buffers. textures, etc. Forcing them to be 64 is a huge waste of
memory. That said, many users requested the ability to have 64 bits packed
arrays for their games, so this is just an optional added type.
For Variant, the float datatype is always 64 bits, and exposed as `float`.
We still have `real_t` which is the datatype that can change from 32 to 64
bits depending on a compile flag (not entirely working right now, but that's
the idea). It affects math related datatypes and code only.
Neither Variant nor PackedArray make use of real_t, which is only intended
for math precision, so the term is removed from there to keep only float.
2020-02-24 18:20:53 +00:00
|
|
|
r_type = Variant::PACKED_INT32_ARRAY;
|
2017-03-24 20:45:31 +00:00
|
|
|
likelihood = 1.0;
|
2017-01-11 03:52:51 +00:00
|
|
|
break;
|
|
|
|
case ARG_ARRAY_BIT | ARG_TYPE_LONG:
|
Variant: Added 64-bit packed arrays, renamed Variant::REAL to FLOAT.
- Renames PackedIntArray to PackedInt32Array.
- Renames PackedFloatArray to PackedFloat32Array.
- Adds PackedInt64Array and PackedFloat64Array.
- Renames Variant::REAL to Variant::FLOAT for consistency.
Packed arrays are for storing large amount of data and creating stuff like
meshes, buffers. textures, etc. Forcing them to be 64 is a huge waste of
memory. That said, many users requested the ability to have 64 bits packed
arrays for their games, so this is just an optional added type.
For Variant, the float datatype is always 64 bits, and exposed as `float`.
We still have `real_t` which is the datatype that can change from 32 to 64
bits depending on a compile flag (not entirely working right now, but that's
the idea). It affects math related datatypes and code only.
Neither Variant nor PackedArray make use of real_t, which is only intended
for math precision, so the term is removed from there to keep only float.
2020-02-24 18:20:53 +00:00
|
|
|
r_type = Variant::PACKED_INT32_ARRAY;
|
2017-03-24 20:45:31 +00:00
|
|
|
likelihood = 0.5;
|
2017-01-11 03:52:51 +00:00
|
|
|
break;
|
|
|
|
case ARG_ARRAY_BIT | ARG_TYPE_FLOAT:
|
Variant: Added 64-bit packed arrays, renamed Variant::REAL to FLOAT.
- Renames PackedIntArray to PackedInt32Array.
- Renames PackedFloatArray to PackedFloat32Array.
- Adds PackedInt64Array and PackedFloat64Array.
- Renames Variant::REAL to Variant::FLOAT for consistency.
Packed arrays are for storing large amount of data and creating stuff like
meshes, buffers. textures, etc. Forcing them to be 64 is a huge waste of
memory. That said, many users requested the ability to have 64 bits packed
arrays for their games, so this is just an optional added type.
For Variant, the float datatype is always 64 bits, and exposed as `float`.
We still have `real_t` which is the datatype that can change from 32 to 64
bits depending on a compile flag (not entirely working right now, but that's
the idea). It affects math related datatypes and code only.
Neither Variant nor PackedArray make use of real_t, which is only intended
for math precision, so the term is removed from there to keep only float.
2020-02-24 18:20:53 +00:00
|
|
|
r_type = Variant::PACKED_FLOAT32_ARRAY;
|
2017-03-24 20:45:31 +00:00
|
|
|
likelihood = 1.0;
|
2017-01-11 03:52:51 +00:00
|
|
|
break;
|
|
|
|
case ARG_ARRAY_BIT | ARG_TYPE_DOUBLE:
|
Variant: Added 64-bit packed arrays, renamed Variant::REAL to FLOAT.
- Renames PackedIntArray to PackedInt32Array.
- Renames PackedFloatArray to PackedFloat32Array.
- Adds PackedInt64Array and PackedFloat64Array.
- Renames Variant::REAL to Variant::FLOAT for consistency.
Packed arrays are for storing large amount of data and creating stuff like
meshes, buffers. textures, etc. Forcing them to be 64 is a huge waste of
memory. That said, many users requested the ability to have 64 bits packed
arrays for their games, so this is just an optional added type.
For Variant, the float datatype is always 64 bits, and exposed as `float`.
We still have `real_t` which is the datatype that can change from 32 to 64
bits depending on a compile flag (not entirely working right now, but that's
the idea). It affects math related datatypes and code only.
Neither Variant nor PackedArray make use of real_t, which is only intended
for math precision, so the term is removed from there to keep only float.
2020-02-24 18:20:53 +00:00
|
|
|
r_type = Variant::PACKED_FLOAT32_ARRAY;
|
2017-03-24 20:45:31 +00:00
|
|
|
likelihood = 0.5;
|
2017-01-11 03:52:51 +00:00
|
|
|
break;
|
2020-05-10 11:00:47 +00:00
|
|
|
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;
|
2014-09-03 02:13:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_FORCE_INLINE_ static bool _convert_object_to_variant(JNIEnv *env, jobject obj, Variant &var, uint32_t p_sig);
|
|
|
|
|
2020-02-19 19:27:19 +00:00
|
|
|
bool _call_method(JavaObject *p_instance, const StringName &p_method, const Variant **p_args, int p_argcount, Callable::CallError &r_error, Variant &ret);
|
2014-09-03 02:13:40 +00:00
|
|
|
|
|
|
|
friend class JavaClassWrapper;
|
2022-05-13 13:04:37 +00:00
|
|
|
HashMap<StringName, List<MethodInfo>> methods;
|
2014-09-03 02:13:40 +00:00
|
|
|
jclass _class;
|
2020-01-19 19:02:40 +00:00
|
|
|
#endif
|
2014-09-03 02:13:40 +00:00
|
|
|
|
|
|
|
public:
|
2022-03-09 13:58:40 +00:00
|
|
|
virtual Variant callp(const StringName &p_method, const Variant **p_args, int p_argcount, Callable::CallError &r_error) override;
|
2014-09-03 02:13:40 +00:00
|
|
|
|
|
|
|
JavaClass();
|
|
|
|
};
|
|
|
|
|
2021-06-04 16:03:15 +00:00
|
|
|
class JavaObject : public RefCounted {
|
|
|
|
GDCLASS(JavaObject, RefCounted);
|
2014-09-03 02:13:40 +00:00
|
|
|
|
2020-01-19 19:02:40 +00:00
|
|
|
#ifdef ANDROID_ENABLED
|
2014-09-03 02:13:40 +00:00
|
|
|
Ref<JavaClass> base_class;
|
|
|
|
friend class JavaClass;
|
|
|
|
|
|
|
|
jobject instance;
|
2020-01-19 19:02:40 +00:00
|
|
|
#endif
|
2014-09-03 02:13:40 +00:00
|
|
|
|
|
|
|
public:
|
2022-03-09 13:58:40 +00:00
|
|
|
virtual Variant callp(const StringName &p_method, const Variant **p_args, int p_argcount, Callable::CallError &r_error) override;
|
2014-09-03 02:13:40 +00:00
|
|
|
|
2020-01-19 19:02:40 +00:00
|
|
|
#ifdef ANDROID_ENABLED
|
2014-09-03 02:13:40 +00:00
|
|
|
JavaObject(const Ref<JavaClass> &p_base, jobject *p_instance);
|
|
|
|
~JavaObject();
|
2020-01-19 19:02:40 +00:00
|
|
|
#endif
|
2014-09-03 02:13:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class JavaClassWrapper : public Object {
|
2017-01-03 02:03:46 +00:00
|
|
|
GDCLASS(JavaClassWrapper, Object);
|
2014-09-03 02:13:40 +00:00
|
|
|
|
2020-01-19 19:02:40 +00:00
|
|
|
#ifdef ANDROID_ENABLED
|
2022-05-13 13:04:37 +00:00
|
|
|
RBMap<String, Ref<JavaClass>> class_cache;
|
2014-09-03 02:13:40 +00:00
|
|
|
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);
|
2020-01-19 19:02:40 +00:00
|
|
|
#endif
|
2014-09-03 02:13:40 +00:00
|
|
|
|
|
|
|
static JavaClassWrapper *singleton;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
static void _bind_methods();
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
public:
|
2014-09-03 02:13:40 +00:00
|
|
|
static JavaClassWrapper *get_singleton() { return singleton; }
|
|
|
|
|
|
|
|
Ref<JavaClass> wrap(const String &p_class);
|
|
|
|
|
2020-01-19 19:02:40 +00:00
|
|
|
#ifdef ANDROID_ENABLED
|
2020-04-01 23:20:12 +00:00
|
|
|
JavaClassWrapper(jobject p_activity = nullptr);
|
2020-01-19 19:02:40 +00:00
|
|
|
#else
|
|
|
|
JavaClassWrapper();
|
|
|
|
#endif
|
2014-09-03 02:13:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // JAVA_CLASS_WRAPPER_H
|