Skip to content

Commit

Permalink
Add support for abstract class registration
Browse files Browse the repository at this point in the history
  • Loading branch information
chippmann committed Nov 3, 2024
1 parent 76fe8ab commit 0a3ffbe
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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
}


Expand All @@ -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()
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,7 @@ data class RegisteredClass(
get() = annotations.getAnnotation<ToolAnnotation>() != null

internal val godotBaseClass: String
get() = if (isAbstract) {
""
} else {
supertypes
.first { it.annotations.hasAnnotation<GodotBaseTypeAnnotation>() }
.name
}
get() = supertypes
.first { it.annotations.hasAnnotation<GodotBaseTypeAnnotation>() }
.name
}
3 changes: 2 additions & 1 deletion kt/godot-library/src/main/kotlin/godot/core/KtClass.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ data class KtClass<T : KtObject>(
private val _functions: Map<String, KtFunction<T, *>>,
private val _notificationFunctions: List<Any.(Int) -> Unit>,
private val _signalInfos: Map<String, KtSignalInfo>,
val baseGodotClass: String
val baseGodotClass: String,
val isAbstract: Boolean,
) {
val registeredSupertypes: Array<String>
get() = _registeredSupertypes.toTypedArray()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ class ClassBuilderDsl<T : KtObject>(
private val relativeSourcePath: String,
private val compilationTimeRelativeRegistrationFilePath: String,
private val superClasses: List<String>,
private val baseGodotClass: String
private val baseGodotClass: String,
private val isAbstract: Boolean,
) {
private val constructors = mutableMapOf<Int, KtConstructor<T>>()

Expand Down Expand Up @@ -1290,7 +1291,8 @@ class ClassBuilderDsl<T : KtObject>(
_functions = functions,
_notificationFunctions = notificationFunctions,
_signalInfos = signals,
baseGodotClass = baseGodotClass
baseGodotClass = baseGodotClass,
isAbstract = isAbstract,
)
}
}
Expand All @@ -1305,14 +1307,15 @@ class ClassRegistry(
fun <T : KtObject> registerClass(
superClass: List<String>,
kClass: KClass<out KtObject>,
isAbstract: Boolean = false,
isTool: Boolean = false,
baseGodotClass: String,
registeredName: String,
relativeSourcePath: String,
compilationTimeRelativeRegistrationFilePath: String,
cb: ClassBuilderDsl<T>.() -> Unit
) {
val builder = ClassBuilderDsl<T>(registeredName, relativeSourcePath, compilationTimeRelativeRegistrationFilePath, superClass, baseGodotClass)
val builder = ClassBuilderDsl<T>(registeredName, relativeSourcePath, compilationTimeRelativeRegistrationFilePath, superClass, baseGodotClass, isAbstract)
builder.cb()
TypeManager.registerUserType(registeredName, kClass)
registerClass(builder.build())
Expand Down

0 comments on commit 0a3ffbe

Please sign in to comment.