From 0a3ffbe5947c87c314b41d830e2eafc1699e06e6 Mon Sep 17 00:00:00 2001 From: chippmann Date: Sun, 3 Nov 2024 11:51:59 +0300 Subject: [PATCH] Add support for abstract class registration --- .../filebuilder/ClassRegistrarFileBuilder.kt | 58 +++++++------------ .../entrygenerator/model/RegisteredClass.kt | 10 +--- .../src/main/kotlin/godot/core/KtClass.kt | 3 +- .../kotlin/godot/registration/Registration.kt | 9 ++- 4 files changed, 33 insertions(+), 47 deletions(-) diff --git a/kt/entry-generation/godot-entry-generator/src/main/kotlin/godot/entrygenerator/filebuilder/ClassRegistrarFileBuilder.kt b/kt/entry-generation/godot-entry-generator/src/main/kotlin/godot/entrygenerator/filebuilder/ClassRegistrarFileBuilder.kt index f367f6104f..524004c73b 100644 --- a/kt/entry-generation/godot-entry-generator/src/main/kotlin/godot/entrygenerator/filebuilder/ClassRegistrarFileBuilder.kt +++ b/kt/entry-generation/godot-entry-generator/src/main/kotlin/godot/entrygenerator/filebuilder/ClassRegistrarFileBuilder.kt @@ -45,11 +45,6 @@ class ClassRegistrarFileBuilder( ) } } - .let { classBuilder -> - if (registeredClass.isAbstract) { - classBuilder.addKdoc("Registrar for abstract class. Does not register any members as it's only used for default value providing if any properties with default values are provided in the abstract class. Members of this abstract class are registered by the inheriting registrars") - } else classBuilder - } private val className = ClassName(registeredClass.containingPackage, registeredClass.name) @@ -59,29 +54,24 @@ class ClassRegistrarFileBuilder( .addParameter("registry", ClassName(godotRegistrationPackage, GodotKotlinJvmTypes.classRegistry)) .beginControlFlow("with(registry)") //START: with registry .let { funSpecBuilder -> - if (!registeredClass.isAbstract) { - val superClasses = registeredClass.supertypes.mapNotNull { supertype -> - //Used to implement script inheritance methods, so we remove base types and abstract parents. - val value = if (supertype is RegisteredClass && !supertype.isAbstract) { - "\"${supertype.registeredName}\"" - } else { - null - } - value - }.reduceOrNull { statement, name -> "$statement,$name" } ?: "" - funSpecBuilder.beginControlFlow( - "registerClass<%T>(listOf($superClasses),·%T::class,·${registeredClass.isTool},·%S,·%S,·%S,·%S)·{", - className, - className, - registeredClass.godotBaseClass, - registeredClass.registeredName, - registeredClass.relativeSourcePath, - compilationTimeRelativeRegistrationFilePath, - ) //START: registerClass - } else { - funSpecBuilder - .addComment("Abstract classes don't need to have any members to be registered") - } + val superClasses = registeredClass.supertypes.mapNotNull { supertype -> + //Used to implement script inheritance methods, so we remove base types + val value = if (supertype is RegisteredClass) { + "\"${supertype.registeredName}\"" + } else { + null + } + value + }.reduceOrNull { statement, name -> "$statement,$name" } ?: "" + funSpecBuilder.beginControlFlow( + "registerClass<%T>(listOf($superClasses),·%T::class,·${registeredClass.isAbstract},·${registeredClass.isTool},·%S,·%S,·%S,·%S)·{", + className, + className, + registeredClass.godotBaseClass, + registeredClass.registeredName, + registeredClass.relativeSourcePath, + compilationTimeRelativeRegistrationFilePath, + ) //START: registerClass } @@ -92,18 +82,14 @@ class ClassRegistrarFileBuilder( if (!registeredClass.isAbstract) { ConstructorRegistrationGenerator.generate(registeredClass, className, registerClassControlFlow) - FunctionRegistrationGenerator.generate(registeredClass, className, registerClassControlFlow) - SignalRegistrationGenerator.generate(registeredClass, className, registerClassControlFlow) - PropertyRegistrationGenerator.generate(registeredClass, className, registerClassControlFlow) } + FunctionRegistrationGenerator.generate(registeredClass, className, registerClassControlFlow) + SignalRegistrationGenerator.generate(registeredClass, className, registerClassControlFlow) + PropertyRegistrationGenerator.generate(registeredClass, className, registerClassControlFlow) classRegistrarBuilder.addFunction( registerClassControlFlow - .let { funSpecBuilder -> - if (!registeredClass.isAbstract) { - funSpecBuilder.endControlFlow() //END: registerClass - } else funSpecBuilder - } + .endControlFlow() //END: registerClass .endControlFlow() //END: with registry .build() ) diff --git a/kt/entry-generation/godot-entry-generator/src/main/kotlin/godot/entrygenerator/model/RegisteredClass.kt b/kt/entry-generation/godot-entry-generator/src/main/kotlin/godot/entrygenerator/model/RegisteredClass.kt index 31d4b0931c..5635a257a1 100644 --- a/kt/entry-generation/godot-entry-generator/src/main/kotlin/godot/entrygenerator/model/RegisteredClass.kt +++ b/kt/entry-generation/godot-entry-generator/src/main/kotlin/godot/entrygenerator/model/RegisteredClass.kt @@ -23,11 +23,7 @@ data class RegisteredClass( get() = annotations.getAnnotation() != null internal val godotBaseClass: String - get() = if (isAbstract) { - "" - } else { - supertypes - .first { it.annotations.hasAnnotation() } - .name - } + get() = supertypes + .first { it.annotations.hasAnnotation() } + .name } diff --git a/kt/godot-library/src/main/kotlin/godot/core/KtClass.kt b/kt/godot-library/src/main/kotlin/godot/core/KtClass.kt index 243a2e9ed8..225ef3e336 100644 --- a/kt/godot-library/src/main/kotlin/godot/core/KtClass.kt +++ b/kt/godot-library/src/main/kotlin/godot/core/KtClass.kt @@ -13,7 +13,8 @@ data class KtClass( private val _functions: Map>, private val _notificationFunctions: List Unit>, private val _signalInfos: Map, - val baseGodotClass: String + val baseGodotClass: String, + val isAbstract: Boolean, ) { val registeredSupertypes: Array get() = _registeredSupertypes.toTypedArray() diff --git a/kt/godot-library/src/main/kotlin/godot/registration/Registration.kt b/kt/godot-library/src/main/kotlin/godot/registration/Registration.kt index 7acb047795..9cf7281de0 100644 --- a/kt/godot-library/src/main/kotlin/godot/registration/Registration.kt +++ b/kt/godot-library/src/main/kotlin/godot/registration/Registration.kt @@ -77,7 +77,8 @@ class ClassBuilderDsl( private val relativeSourcePath: String, private val compilationTimeRelativeRegistrationFilePath: String, private val superClasses: List, - private val baseGodotClass: String + private val baseGodotClass: String, + private val isAbstract: Boolean, ) { private val constructors = mutableMapOf>() @@ -1290,7 +1291,8 @@ class ClassBuilderDsl( _functions = functions, _notificationFunctions = notificationFunctions, _signalInfos = signals, - baseGodotClass = baseGodotClass + baseGodotClass = baseGodotClass, + isAbstract = isAbstract, ) } } @@ -1305,6 +1307,7 @@ class ClassRegistry( fun registerClass( superClass: List, kClass: KClass, + isAbstract: Boolean = false, isTool: Boolean = false, baseGodotClass: String, registeredName: String, @@ -1312,7 +1315,7 @@ class ClassRegistry( compilationTimeRelativeRegistrationFilePath: String, cb: ClassBuilderDsl.() -> Unit ) { - val builder = ClassBuilderDsl(registeredName, relativeSourcePath, compilationTimeRelativeRegistrationFilePath, superClass, baseGodotClass) + val builder = ClassBuilderDsl(registeredName, relativeSourcePath, compilationTimeRelativeRegistrationFilePath, superClass, baseGodotClass, isAbstract) builder.cb() TypeManager.registerUserType(registeredName, kClass) registerClass(builder.build())