diff --git a/kt/api-generator/src/main/kotlin/godot/codegen/extensions/TypedExtensions.kt b/kt/api-generator/src/main/kotlin/godot/codegen/extensions/TypedExtensions.kt index fc340577e1..b8b68fd1a0 100644 --- a/kt/api-generator/src/main/kotlin/godot/codegen/extensions/TypedExtensions.kt +++ b/kt/api-generator/src/main/kotlin/godot/codegen/extensions/TypedExtensions.kt @@ -51,7 +51,7 @@ const val bitfieldPrefix = "bitfield::" fun TypedTrait.isCoreType() = isTypedArray() || GodotTypes.coreTypes.find { s -> s == this.type } != null fun TypedTrait.isPrimitive() = GodotTypes.primitives.find { s -> s == this.type } != null -fun TypedTrait.isCoreTypeReimplementedInKotlin() = GodotTypes.coreTypesReimplementedInKotlin.find { s -> +fun TypedTrait.isLocalCopyCoreTypes() = GodotTypes.localCopyCoreTypes.find { s -> s == this.type } != null fun TypedTrait.isEnum() = type?.startsWith(enumPrefix) ?: false diff --git a/kt/api-generator/src/main/kotlin/godot/codegen/services/impl/GenerationService.kt b/kt/api-generator/src/main/kotlin/godot/codegen/services/impl/GenerationService.kt index a8d3b60d91..23039f168c 100644 --- a/kt/api-generator/src/main/kotlin/godot/codegen/services/impl/GenerationService.kt +++ b/kt/api-generator/src/main/kotlin/godot/codegen/services/impl/GenerationService.kt @@ -25,8 +25,8 @@ import godot.codegen.extensions.applyJvmNameIfNecessary import godot.codegen.extensions.getDefaultValueKotlinString import godot.codegen.extensions.getTypeClassName import godot.codegen.extensions.isBitField -import godot.codegen.extensions.isCoreTypeReimplementedInKotlin import godot.codegen.extensions.isEnum +import godot.codegen.extensions.isLocalCopyCoreTypes import godot.codegen.extensions.jvmVariantTypeValue import godot.codegen.models.custom.AdditionalImport import godot.codegen.models.enriched.EnrichedClass @@ -167,7 +167,7 @@ class GenerationService( for (property in enrichedClass.properties) { val propertySpec = generateProperty(enrichedClass, property) ?: continue classTypeBuilder.addProperty(propertySpec) - if (property.hasValidSetterInClass && property.isCoreTypeReimplementedInKotlin()) { + if (property.hasValidSetterInClass && property.isLocalCopyCoreTypes()) { classTypeBuilder.addFunction(generateCoreTypeHelper(enrichedClass, property)) } } @@ -505,7 +505,7 @@ class GenerationService( ) } - if (property.isCoreTypeReimplementedInKotlin()) { + if (property.isLocalCopyCoreTypes()) { propertySpecBuilder.addAnnotation(CORE_TYPE_LOCAL_COPY) } @@ -519,7 +519,6 @@ class GenerationService( val parameterName = property.name val propertyFunSpec = FunSpec.builder("${parameterName}Mutate").addModifiers(KModifier.FINAL) - return propertyFunSpec .addParameter( ParameterSpec.builder( diff --git a/kt/godot-library/src/main/kotlin/godot/core/Variant.kt b/kt/godot-library/src/main/kotlin/godot/core/Variant.kt index 1de19bf5ff..7d176d0bae 100644 --- a/kt/godot-library/src/main/kotlin/godot/core/Variant.kt +++ b/kt/godot-library/src/main/kotlin/godot/core/Variant.kt @@ -364,7 +364,10 @@ enum class VariantType(override val id: Int) : VariantConverter { }, _RID(23) { override fun toUnsafeKotlin(buffer: ByteBuffer) = RID(buffer.long) - override fun toUnsafeGodot(buffer: ByteBuffer, any: Any?) = toGodotNativeCoreType(buffer, any) + override fun toUnsafeGodot(buffer: ByteBuffer, any: Any?) { + require(any is RID) + buffer.putLong(any.id) + } }, OBJECT(24) { override fun toUnsafeKotlin(buffer: ByteBuffer) = buffer.obj diff --git a/kt/godot-library/src/main/kotlin/godot/core/bridge/RID.kt b/kt/godot-library/src/main/kotlin/godot/core/bridge/RID.kt index 865a36dc9c..8e445a1f98 100644 --- a/kt/godot-library/src/main/kotlin/godot/core/bridge/RID.kt +++ b/kt/godot-library/src/main/kotlin/godot/core/bridge/RID.kt @@ -1,88 +1,58 @@ @file:Suppress("PackageDirectoryMismatch") package godot.core -import godot.core.memory.MemoryManager -import godot.core.memory.TransferContext -import godot.util.VoidPtr -@Suppress("MemberVisibilityCanBePrivate") -class RID : NativeCoreType, Comparable { +class RID( + /** + * Returns the ID of the referenced low-level resource. + */ + val id: Long +) : Comparable { - val id: Int - get() = getID() - - //INTERNAL - internal constructor(_handle: VoidPtr) { - this._handle = _handle - } //CONSTRUCTOR - constructor() { - _handle = Bridge.engine_call_constructor() - MemoryManager.registerNativeCoreType(this, VariantType._RID) - } + /** + * Constructs an empty RID with the invalid ID 0. + */ + constructor(): this(0L) - constructor(from: RID) { - _handle = Bridge.engine_call_constructor(from._handle) - MemoryManager.registerNativeCoreType(this, VariantType._RID) - } + /** + * Constructs a RID as a copy of the given RID. + */ + constructor(from: RID): this(from.id) //API /** - * Returns the ID of the referenced resource. + * Returns `true` if the RID is not `0`. */ - fun getID(): Int { - Bridge.engine_call_getID(_handle) - return TransferContext.readReturnValue(VariantCaster.INT) as Int - } + fun isValid() = id != 0L /** - * Returns `true` if the RID is not `0`. + * Returns `true` if the RID is `0`. */ - fun isValid(): Boolean { - Bridge.engine_call_isValid(_handle) - return TransferContext.readReturnValue(VariantType.BOOL) as Boolean - } + fun isNull() = id == 0L //UTILITIES - override fun compareTo(other: RID): Int { - TransferContext.writeArguments(VariantType._RID to other) - Bridge.engine_call_compareTo(_handle) - return when { - this == other -> 0 - TransferContext.readReturnValue(VariantType.BOOL) as Boolean -> -1 - - else -> 1 - } + override fun compareTo(other: RID) = when { + this == other -> 0 + this.id < other.id -> -1 + else -> 1 } override fun equals(other: Any?): Boolean { return when (other) { is RID -> { - TransferContext.writeArguments(VariantType._RID to other) - Bridge.engine_call_equals(_handle) - TransferContext.readReturnValue(VariantType.BOOL) as Boolean + id == other.id } - else -> false } } override fun hashCode(): Int { - return _handle.hashCode() + return id.toInt() } override fun toString(): String { return "RID($id)" } - - @Suppress("FunctionName") - private object Bridge { - external fun engine_call_constructor(): VoidPtr - external fun engine_call_constructor(from: VoidPtr): VoidPtr - external fun engine_call_getID(_handle: VoidPtr) - external fun engine_call_isValid(_handle: VoidPtr) - external fun engine_call_compareTo(_handle: VoidPtr) - external fun engine_call_equals(_handle: VoidPtr) - } } diff --git a/kt/plugins/godot-gradle-plugin/src/main/resources/godot/gradle/godot-kotlin-graal-jni-config.json b/kt/plugins/godot-gradle-plugin/src/main/resources/godot/gradle/godot-kotlin-graal-jni-config.json index 4ff2cfdc46..ab6d53d9ff 100644 --- a/kt/plugins/godot-gradle-plugin/src/main/resources/godot/gradle/godot-kotlin-graal-jni-config.json +++ b/kt/plugins/godot-gradle-plugin/src/main/resources/godot/gradle/godot-kotlin-graal-jni-config.json @@ -228,20 +228,6 @@ { "name" : "engine_call_equals", "parameterTypes" : ["long"] } ] }, - { - "name" : "godot.core.RID$Bridge", - "fields" : [ - { "name" : "INSTANCE" } - ], - "methods" : [ - { "name" : "engine_call_constructor", "parameterTypes" : [] }, - { "name" : "engine_call_constructor", "parameterTypes" : ["long"] }, - { "name" : "engine_call_getID", "parameterTypes" : ["long"] }, - { "name" : "engine_call_isValid", "parameterTypes" : ["long"] }, - { "name" : "engine_call_compareTo", "parameterTypes" : ["long"] }, - { "name" : "engine_call_equals", "parameterTypes" : ["long"] } - ] - }, { "name" : "godot.core.NodePath$Bridge", "fields" : [ diff --git a/kt/tools-common/src/main/kotlin/godot/tools/common/constants/Classes.kt b/kt/tools-common/src/main/kotlin/godot/tools/common/constants/Classes.kt index c8aca19b1c..41577c716e 100644 --- a/kt/tools-common/src/main/kotlin/godot/tools/common/constants/Classes.kt +++ b/kt/tools-common/src/main/kotlin/godot/tools/common/constants/Classes.kt @@ -161,7 +161,8 @@ object GodotTypes { variant ) - val coreTypesReimplementedInKotlin = listOf( + // We don't include RID because this core type can't be modified anyway and is a simple wrapper around Long. + val localCopyCoreTypes = listOf( aabb, basis, color, @@ -177,7 +178,7 @@ object GodotTypes { vector3i, vector4, vector4i, - projection + projection, ) val primitives = listOf( diff --git a/src/jvm_wrapper/bridge/rid_bridge.cpp b/src/jvm_wrapper/bridge/rid_bridge.cpp deleted file mode 100644 index d253288d2b..0000000000 --- a/src/jvm_wrapper/bridge/rid_bridge.cpp +++ /dev/null @@ -1,48 +0,0 @@ -#include "rid_bridge.h" - -#include "bridges_utils.h" -#include "jvm_wrapper/memory/transfer_context.h" - -using namespace bridges; - -uintptr_t RidBridge::engine_call_constructor(JNIEnv* p_raw_env, jobject p_instance) { - return reinterpret_cast(memnew(RID)); -} - -uintptr_t RidBridge::engine_call_constructor_arg(JNIEnv* p_raw_env, jobject p_instance, jlong p_raw_ptr) { - if (auto* rid {from_uint_to_ptr(p_raw_ptr)}) { - return reinterpret_cast(memnew(RID(*rid))); - } else { - return reinterpret_cast(memnew(RID)); - } -} - -void RidBridge::engine_call_getID(JNIEnv* p_raw_env, jobject p_instance, jlong p_raw_ptr) { - jni::Env env {p_raw_env}; - Variant variant {from_uint_to_ptr(p_raw_ptr)->get_id()}; - TransferContext::get_instance().write_return_value(env, variant); -} - -void RidBridge::engine_call_isValid(JNIEnv* p_raw_env, jobject p_instance, jlong p_raw_ptr) { - jni::Env env {p_raw_env}; - Variant variant {from_uint_to_ptr(p_raw_ptr)->is_valid()}; - TransferContext::get_instance().write_return_value(env, variant); -} - -void RidBridge::engine_call_compareTo(JNIEnv* p_raw_env, jobject p_instance, jlong p_raw_ptr) { - jni::Env env {p_raw_env}; - Variant args[1] = {}; - TransferContext::get_instance().read_args(env, args); - Variant variant {*from_uint_to_ptr(p_raw_ptr) < args[0].operator ::RID()}; - TransferContext::get_instance().write_return_value(env, variant); -} - -void RidBridge::engine_call_equals(JNIEnv* p_raw_env, jobject p_instance, jlong p_raw_ptr) { - jni::Env env {p_raw_env}; - Variant args[1] = {}; - TransferContext::get_instance().read_args(env, args); - Variant variant {*from_uint_to_ptr(p_raw_ptr) == args[0].operator ::RID()}; - TransferContext::get_instance().write_return_value(env, variant); -} - -RidBridge::~RidBridge() = default; \ No newline at end of file diff --git a/src/jvm_wrapper/bridge/rid_bridge.h b/src/jvm_wrapper/bridge/rid_bridge.h deleted file mode 100644 index bf776b64b7..0000000000 --- a/src/jvm_wrapper/bridge/rid_bridge.h +++ /dev/null @@ -1,33 +0,0 @@ -#ifndef GODOT_JVM_RIDBRIDGE_H -#define GODOT_JVM_RIDBRIDGE_H - -#include "jvm_wrapper/jvm_singleton_wrapper.h" - -namespace bridges { - - JVM_SINGLETON_WRAPPER(RidBridge, "godot.core.RID$Bridge") { - SINGLETON_CLASS(RidBridge) - - // clang-format off - INIT_JNI_BINDINGS( - INIT_NATIVE_METHOD("engine_call_constructor","()J", RidBridge::engine_call_constructor) - INIT_NATIVE_METHOD("engine_call_constructor","(J)J", RidBridge::engine_call_constructor_arg) - INIT_NATIVE_METHOD("engine_call_getID", ("(J)V"), RidBridge::engine_call_getID) - INIT_NATIVE_METHOD("engine_call_isValid","(J)V", RidBridge::engine_call_isValid) - INIT_NATIVE_METHOD("engine_call_compareTo","(J)V", RidBridge::engine_call_compareTo) - INIT_NATIVE_METHOD("engine_call_equals", ("(J)V"), RidBridge::engine_call_equals) - ) - // clang-format on - - public: - - static uintptr_t engine_call_constructor(JNIEnv* p_raw_env, jobject p_instance); - static uintptr_t engine_call_constructor_arg(JNIEnv* p_raw_env, jobject p_instance, jlong p_raw_ptr); - static void engine_call_getID(JNIEnv* p_raw_env, jobject p_instance, jlong p_raw_ptr); - static void engine_call_isValid(JNIEnv* p_raw_env, jobject p_instance, jlong p_raw_ptr); - static void engine_call_compareTo(JNIEnv* p_raw_env, jobject p_instance, jlong p_raw_ptr); - static void engine_call_equals(JNIEnv* p_raw_env, jobject p_instance, jlong p_raw_ptr); - }; -}// namespace bridge - -#endif// GODOT_JVM_RIDBRIDGE_H diff --git a/src/jvm_wrapper/memory/memory_manager.cpp b/src/jvm_wrapper/memory/memory_manager.cpp index c774c62737..be3bd81598 100644 --- a/src/jvm_wrapper/memory/memory_manager.cpp +++ b/src/jvm_wrapper/memory/memory_manager.cpp @@ -44,9 +44,6 @@ void MemoryManager::unref_native_core_types(JNIEnv* p_raw_env, jobject p_instanc Variant::Type variant_type {static_cast(var_type)}; switch (variant_type) { - case Variant::RID: - memdelete(reinterpret_cast(p_raw_ptr)); - break; case Variant::CALLABLE: memdelete(reinterpret_cast(p_raw_ptr)); break; @@ -227,3 +224,4 @@ void MemoryManager::direct_object_deletion(jni::Env& p_env, Object* p_obj) { } MemoryManager::~MemoryManager() = default; + diff --git a/src/kt_variant.h b/src/kt_variant.h index de1488fc71..d2fa768393 100644 --- a/src/kt_variant.h +++ b/src/kt_variant.h @@ -130,7 +130,7 @@ namespace ktvariant { &Variant::operator StringName>; to_kt_array[Variant::NODE_PATH] = to_kvariant_fromNATIVECORETYPE < Variant::NODE_PATH, NodePath, &Variant::operator NodePath>; - to_kt_array[Variant::RID] = to_kvariant_fromNATIVECORETYPE < Variant::RID, RID, &Variant::operator ::RID>; + to_kt_array[Variant::RID] = to_kvariant_fromCORETYPE; to_kt_array[Variant::PACKED_BYTE_ARRAY] = to_kvariant_fromNATIVECORETYPE < Variant::PACKED_BYTE_ARRAY, PackedByteArray, &Variant::operator PackedByteArray>; to_kt_array[Variant::PACKED_INT32_ARRAY] = to_kvariant_fromNATIVECORETYPE < Variant::PACKED_INT32_ARRAY, @@ -260,7 +260,7 @@ namespace ktvariant { to_gd_array[Variant::ARRAY] = from_kvariant_tokVariantNativeCoreTypeValue; to_gd_array[Variant::STRING_NAME] = from_kvariant_tokVariantNativeCoreTypeValue; to_gd_array[Variant::NODE_PATH] = from_kvariant_tokVariantNativeCoreTypeValue; - to_gd_array[Variant::RID] = from_kvariant_tokVariantNativeCoreTypeValue; + to_gd_array[Variant::RID] = from_kvariant_to_kVariantCoreTypeValue; to_gd_array[Variant::PACKED_BYTE_ARRAY] = from_kvariant_tokVariantNativeCoreTypeValue; to_gd_array[Variant::PACKED_INT32_ARRAY] = from_kvariant_tokVariantNativeCoreTypeValue; to_gd_array[Variant::PACKED_INT64_ARRAY] = from_kvariant_tokVariantNativeCoreTypeValue; diff --git a/src/lifecycle/jvm_manager.cpp b/src/lifecycle/jvm_manager.cpp index dc4f6bfd9c..dc0a348cfd 100644 --- a/src/lifecycle/jvm_manager.cpp +++ b/src/lifecycle/jvm_manager.cpp @@ -15,12 +15,9 @@ #include "jvm_wrapper/bridge/packed_string_array_bridge.h" #include "jvm_wrapper/bridge/packed_vector2_array_bridge.h" #include "jvm_wrapper/bridge/packed_vector3_array_bridge.h" -#include "jvm_wrapper/bridge/rid_bridge.h" #include "jvm_wrapper/bridge/string_name_bridge.h" #include "jvm_wrapper/bridge/variant_array_bridge.h" #include "jvm_wrapper/memory/memory_manager.h" -#include "jvm_wrapper/memory/transfer_context.h" -#include "jvm_wrapper/registration/kt_class.h" #include "kotlin_callable_custom.h" #include "jvm_wrapper/bridge/kt_callable_bridge.h" #include "jvm_wrapper/bridge/packed_vector4_array_bridge.h" @@ -117,7 +114,6 @@ bool JvmManager::initialize_jni_classes(jni::Env& p_env, ClassLoader* class_load && bridges::CallableBridge::initialize(p_env, class_loader) && bridges::KtCallableBridge::initialize(p_env, class_loader) && bridges::DictionaryBridge::initialize(p_env, class_loader) - && bridges::RidBridge::initialize(p_env, class_loader) && bridges::StringNameBridge::initialize(p_env, class_loader) && bridges::NodePathBridge::initialize(p_env, class_loader) && bridges::VariantArrayBridge::initialize(p_env, class_loader) @@ -144,7 +140,6 @@ void JvmManager::destroy_jni_classes() { bridges::CallableBridge::destroy(); bridges::DictionaryBridge::destroy(); - bridges::RidBridge::destroy(); bridges::StringNameBridge::destroy(); bridges::NodePathBridge::destroy(); bridges::VariantArrayBridge::destroy();