Skip to content

Commit 359d785

Browse files
authored
RID is no longer a native coretype (#689)
* Convert RID to a Kotlin coretype
1 parent 5784336 commit 359d785

File tree

11 files changed

+38
-167
lines changed

11 files changed

+38
-167
lines changed

kt/api-generator/src/main/kotlin/godot/codegen/extensions/TypedExtensions.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ const val bitfieldPrefix = "bitfield::"
5151

5252
fun TypedTrait.isCoreType() = isTypedArray() || GodotTypes.coreTypes.find { s -> s == this.type } != null
5353
fun TypedTrait.isPrimitive() = GodotTypes.primitives.find { s -> s == this.type } != null
54-
fun TypedTrait.isCoreTypeReimplementedInKotlin() = GodotTypes.coreTypesReimplementedInKotlin.find { s ->
54+
fun TypedTrait.isLocalCopyCoreTypes() = GodotTypes.localCopyCoreTypes.find { s ->
5555
s == this.type
5656
} != null
5757
fun TypedTrait.isEnum() = type?.startsWith(enumPrefix) ?: false

kt/api-generator/src/main/kotlin/godot/codegen/services/impl/GenerationService.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ import godot.codegen.extensions.applyJvmNameIfNecessary
2525
import godot.codegen.extensions.getDefaultValueKotlinString
2626
import godot.codegen.extensions.getTypeClassName
2727
import godot.codegen.extensions.isBitField
28-
import godot.codegen.extensions.isCoreTypeReimplementedInKotlin
2928
import godot.codegen.extensions.isEnum
29+
import godot.codegen.extensions.isLocalCopyCoreTypes
3030
import godot.codegen.extensions.jvmVariantTypeValue
3131
import godot.codegen.models.custom.AdditionalImport
3232
import godot.codegen.models.enriched.EnrichedClass
@@ -167,7 +167,7 @@ class GenerationService(
167167
for (property in enrichedClass.properties) {
168168
val propertySpec = generateProperty(enrichedClass, property) ?: continue
169169
classTypeBuilder.addProperty(propertySpec)
170-
if (property.hasValidSetterInClass && property.isCoreTypeReimplementedInKotlin()) {
170+
if (property.hasValidSetterInClass && property.isLocalCopyCoreTypes()) {
171171
classTypeBuilder.addFunction(generateCoreTypeHelper(enrichedClass, property))
172172
}
173173
}
@@ -505,7 +505,7 @@ class GenerationService(
505505
)
506506
}
507507

508-
if (property.isCoreTypeReimplementedInKotlin()) {
508+
if (property.isLocalCopyCoreTypes()) {
509509
propertySpecBuilder.addAnnotation(CORE_TYPE_LOCAL_COPY)
510510
}
511511

@@ -519,7 +519,6 @@ class GenerationService(
519519
val parameterName = property.name
520520
val propertyFunSpec = FunSpec.builder("${parameterName}Mutate").addModifiers(KModifier.FINAL)
521521

522-
523522
return propertyFunSpec
524523
.addParameter(
525524
ParameterSpec.builder(

kt/godot-library/src/main/kotlin/godot/core/Variant.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,10 @@ enum class VariantType(override val id: Int) : VariantConverter {
364364
},
365365
_RID(23) {
366366
override fun toUnsafeKotlin(buffer: ByteBuffer) = RID(buffer.long)
367-
override fun toUnsafeGodot(buffer: ByteBuffer, any: Any?) = toGodotNativeCoreType<RID>(buffer, any)
367+
override fun toUnsafeGodot(buffer: ByteBuffer, any: Any?) {
368+
require(any is RID)
369+
buffer.putLong(any.id)
370+
}
368371
},
369372
OBJECT(24) {
370373
override fun toUnsafeKotlin(buffer: ByteBuffer) = buffer.obj
Lines changed: 24 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,58 @@
11
@file:Suppress("PackageDirectoryMismatch")
22

33
package godot.core
4-
import godot.core.memory.MemoryManager
5-
import godot.core.memory.TransferContext
6-
import godot.util.VoidPtr
74

8-
@Suppress("MemberVisibilityCanBePrivate")
9-
class RID : NativeCoreType, Comparable<RID> {
5+
class RID(
6+
/**
7+
* Returns the ID of the referenced low-level resource.
8+
*/
9+
val id: Long
10+
) : Comparable<RID> {
1011

11-
val id: Int
12-
get() = getID()
13-
14-
//INTERNAL
15-
internal constructor(_handle: VoidPtr) {
16-
this._handle = _handle
17-
}
1812

1913
//CONSTRUCTOR
20-
constructor() {
21-
_handle = Bridge.engine_call_constructor()
22-
MemoryManager.registerNativeCoreType(this, VariantType._RID)
23-
}
14+
/**
15+
* Constructs an empty RID with the invalid ID 0.
16+
*/
17+
constructor(): this(0L)
2418

25-
constructor(from: RID) {
26-
_handle = Bridge.engine_call_constructor(from._handle)
27-
MemoryManager.registerNativeCoreType(this, VariantType._RID)
28-
}
19+
/**
20+
* Constructs a RID as a copy of the given RID.
21+
*/
22+
constructor(from: RID): this(from.id)
2923

3024
//API
3125
/**
32-
* Returns the ID of the referenced resource.
26+
* Returns `true` if the RID is not `0`.
3327
*/
34-
fun getID(): Int {
35-
Bridge.engine_call_getID(_handle)
36-
return TransferContext.readReturnValue(VariantCaster.INT) as Int
37-
}
28+
fun isValid() = id != 0L
3829

3930
/**
40-
* Returns `true` if the RID is not `0`.
31+
* Returns `true` if the RID is `0`.
4132
*/
42-
fun isValid(): Boolean {
43-
Bridge.engine_call_isValid(_handle)
44-
return TransferContext.readReturnValue(VariantType.BOOL) as Boolean
45-
}
33+
fun isNull() = id == 0L
4634

4735
//UTILITIES
48-
override fun compareTo(other: RID): Int {
49-
TransferContext.writeArguments(VariantType._RID to other)
50-
Bridge.engine_call_compareTo(_handle)
51-
return when {
52-
this == other -> 0
53-
TransferContext.readReturnValue(VariantType.BOOL) as Boolean -> -1
54-
55-
else -> 1
56-
}
36+
override fun compareTo(other: RID) = when {
37+
this == other -> 0
38+
this.id < other.id -> -1
39+
else -> 1
5740
}
5841

5942
override fun equals(other: Any?): Boolean {
6043
return when (other) {
6144
is RID -> {
62-
TransferContext.writeArguments(VariantType._RID to other)
63-
Bridge.engine_call_equals(_handle)
64-
TransferContext.readReturnValue(VariantType.BOOL) as Boolean
45+
id == other.id
6546
}
66-
6747
else -> false
6848
}
6949
}
7050

7151
override fun hashCode(): Int {
72-
return _handle.hashCode()
52+
return id.toInt()
7353
}
7454

7555
override fun toString(): String {
7656
return "RID($id)"
7757
}
78-
79-
@Suppress("FunctionName")
80-
private object Bridge {
81-
external fun engine_call_constructor(): VoidPtr
82-
external fun engine_call_constructor(from: VoidPtr): VoidPtr
83-
external fun engine_call_getID(_handle: VoidPtr)
84-
external fun engine_call_isValid(_handle: VoidPtr)
85-
external fun engine_call_compareTo(_handle: VoidPtr)
86-
external fun engine_call_equals(_handle: VoidPtr)
87-
}
8858
}

kt/plugins/godot-gradle-plugin/src/main/resources/godot/gradle/godot-kotlin-graal-jni-config.json

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -228,20 +228,6 @@
228228
{ "name" : "engine_call_equals", "parameterTypes" : ["long"] }
229229
]
230230
},
231-
{
232-
"name" : "godot.core.RID$Bridge",
233-
"fields" : [
234-
{ "name" : "INSTANCE" }
235-
],
236-
"methods" : [
237-
{ "name" : "engine_call_constructor", "parameterTypes" : [] },
238-
{ "name" : "engine_call_constructor", "parameterTypes" : ["long"] },
239-
{ "name" : "engine_call_getID", "parameterTypes" : ["long"] },
240-
{ "name" : "engine_call_isValid", "parameterTypes" : ["long"] },
241-
{ "name" : "engine_call_compareTo", "parameterTypes" : ["long"] },
242-
{ "name" : "engine_call_equals", "parameterTypes" : ["long"] }
243-
]
244-
},
245231
{
246232
"name" : "godot.core.NodePath$Bridge",
247233
"fields" : [

kt/tools-common/src/main/kotlin/godot/tools/common/constants/Classes.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,8 @@ object GodotTypes {
161161
variant
162162
)
163163

164-
val coreTypesReimplementedInKotlin = listOf(
164+
// We don't include RID because this core type can't be modified anyway and is a simple wrapper around Long.
165+
val localCopyCoreTypes = listOf(
165166
aabb,
166167
basis,
167168
color,
@@ -177,7 +178,7 @@ object GodotTypes {
177178
vector3i,
178179
vector4,
179180
vector4i,
180-
projection
181+
projection,
181182
)
182183

183184
val primitives = listOf(

src/jvm_wrapper/bridge/rid_bridge.cpp

Lines changed: 0 additions & 48 deletions
This file was deleted.

src/jvm_wrapper/bridge/rid_bridge.h

Lines changed: 0 additions & 33 deletions
This file was deleted.

src/jvm_wrapper/memory/memory_manager.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,6 @@ void MemoryManager::unref_native_core_types(JNIEnv* p_raw_env, jobject p_instanc
4444

4545
Variant::Type variant_type {static_cast<Variant::Type>(var_type)};
4646
switch (variant_type) {
47-
case Variant::RID:
48-
memdelete(reinterpret_cast<RID*>(p_raw_ptr));
49-
break;
5047
case Variant::CALLABLE:
5148
memdelete(reinterpret_cast<Callable*>(p_raw_ptr));
5249
break;
@@ -227,3 +224,4 @@ void MemoryManager::direct_object_deletion(jni::Env& p_env, Object* p_obj) {
227224
}
228225

229226
MemoryManager::~MemoryManager() = default;
227+

src/kt_variant.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ namespace ktvariant {
130130
&Variant::operator StringName>;
131131
to_kt_array[Variant::NODE_PATH] = to_kvariant_fromNATIVECORETYPE < Variant::NODE_PATH, NodePath,
132132
&Variant::operator NodePath>;
133-
to_kt_array[Variant::RID] = to_kvariant_fromNATIVECORETYPE < Variant::RID, RID, &Variant::operator ::RID>;
133+
to_kt_array[Variant::RID] = to_kvariant_fromCORETYPE<Variant::RID, RID>;
134134
to_kt_array[Variant::PACKED_BYTE_ARRAY] = to_kvariant_fromNATIVECORETYPE < Variant::PACKED_BYTE_ARRAY,
135135
PackedByteArray, &Variant::operator PackedByteArray>;
136136
to_kt_array[Variant::PACKED_INT32_ARRAY] = to_kvariant_fromNATIVECORETYPE < Variant::PACKED_INT32_ARRAY,
@@ -260,7 +260,7 @@ namespace ktvariant {
260260
to_gd_array[Variant::ARRAY] = from_kvariant_tokVariantNativeCoreTypeValue<Array>;
261261
to_gd_array[Variant::STRING_NAME] = from_kvariant_tokVariantNativeCoreTypeValue<StringName>;
262262
to_gd_array[Variant::NODE_PATH] = from_kvariant_tokVariantNativeCoreTypeValue<NodePath>;
263-
to_gd_array[Variant::RID] = from_kvariant_tokVariantNativeCoreTypeValue<RID>;
263+
to_gd_array[Variant::RID] = from_kvariant_to_kVariantCoreTypeValue<RID>;
264264
to_gd_array[Variant::PACKED_BYTE_ARRAY] = from_kvariant_tokVariantNativeCoreTypeValue<PackedByteArray>;
265265
to_gd_array[Variant::PACKED_INT32_ARRAY] = from_kvariant_tokVariantNativeCoreTypeValue<PackedInt32Array>;
266266
to_gd_array[Variant::PACKED_INT64_ARRAY] = from_kvariant_tokVariantNativeCoreTypeValue<PackedInt64Array>;

0 commit comments

Comments
 (0)