Skip to content

Commit

Permalink
Add RenderModule code generation
Browse files Browse the repository at this point in the history
  • Loading branch information
fredboy committed May 14, 2024
1 parent 4e63a1e commit f50a0d3
Show file tree
Hide file tree
Showing 26 changed files with 127 additions and 172 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,5 @@ Thumbs.db

release-*/
keystore.properties

*/build/
4 changes: 3 additions & 1 deletion core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ java.sourceCompatibility = JavaVersion.VERSION_17
sourceSets.main.java.srcDirs = ["src/"]

dependencies {
implementation project(':dagger-multibind-annotations')
ksp project(':dagger-multibind-ksp')

api "com.badlogicgames.gdx:gdx:$gdxVersion"
api "com.google.guava:guava:$guavaVersion-android"
api "com.google.dagger:dagger:$daggerVersion"
implementation 'org.jetbrains:annotations:23.1.0'
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion"
implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:$kotlinSerializationVersion"
annotationProcessor "com.google.dagger:dagger-compiler:$daggerVersion"
ksp project(':dagger-multibind-ksp')
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import ru.deadsoftware.cavedroid.misc.utils.forEachBlockInArea
import javax.inject.Inject

@GameScope
@GameRenderer
class BackgroundBlocksRenderer @Inject constructor(
gameWorld: GameWorld,
mobsController: MobsController
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import ru.deadsoftware.cavedroid.game.debug.DebugInfoStringsProvider
import ru.deadsoftware.cavedroid.game.mobs.MobsController
import ru.deadsoftware.cavedroid.game.model.block.Block
import ru.deadsoftware.cavedroid.game.world.GameWorld
import ru.deadsoftware.cavedroid.misc.Assets
import ru.deadsoftware.cavedroid.misc.utils.bl
import ru.deadsoftware.cavedroid.misc.utils.drawString
import ru.deadsoftware.cavedroid.misc.utils.forEachBlockInArea
import ru.deadsoftware.cavedroid.misc.utils.px
import javax.inject.Inject

@GameRenderer
@GameScope
class DebugRenderer @Inject constructor(
private val mainConfig: MainConfig,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import ru.deadsoftware.cavedroid.misc.utils.px
import javax.inject.Inject

@GameScope
@GameRenderer
class DropsRenderer @Inject constructor(
private val dropController: DropController,
private val gameWorld: GameWorld,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import ru.deadsoftware.cavedroid.misc.utils.forEachBlockInArea
import javax.inject.Inject

@GameScope
@GameRenderer
class ForegroundBlocksRenderer @Inject constructor(
gameWorld: GameWorld,
mobsController: MobsController
Expand Down
10 changes: 10 additions & 0 deletions core/src/ru/deadsoftware/cavedroid/game/render/GameRenderer.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package ru.deadsoftware.cavedroid.game.render

import ru.fredboy.cavedroid.ksp.annotations.GenerateSetMultibindingsModule

@GenerateSetMultibindingsModule(
interfaceClass = IGameRenderer::class,
modulePackage = "ru.deadsoftware.cavedroid.game.render",
moduleName = "RenderModule"
)
annotation class GameRenderer
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import ru.deadsoftware.cavedroid.misc.utils.px
import javax.inject.Inject

@GameScope
@GameRenderer
class HudRenderer @Inject constructor(
private val gameWorld: GameWorld,
private val mobsController: MobsController,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import ru.deadsoftware.cavedroid.misc.utils.px
import javax.inject.Inject

@GameScope
@GameRenderer
class MobsRenderer @Inject constructor(
private val mobsController: MobsController,
private val gameWorld: GameWorld,
Expand Down
59 changes: 0 additions & 59 deletions core/src/ru/deadsoftware/cavedroid/game/render/RenderModule.kt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import ru.deadsoftware.cavedroid.misc.utils.drawSprite
import javax.inject.Inject

@GameScope
@GameRenderer
class TouchControlsRenderer @Inject constructor(
private val mainConfig: MainConfig,
private val mobsController: MobsController,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import ru.deadsoftware.cavedroid.game.ui.windows.GameWindowsManager
import javax.inject.Inject

@GameScope
@GameRenderer
class WindowsRenderer @Inject constructor(
private val creativeWindowRenderer: CreativeWindowRenderer,
private val survivalWindowRenderer: SurvivalWindowRenderer,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package ru.fredboy.cavedroid.ksp.annotations

import kotlin.reflect.KClass

@Target(AnnotationTarget.ANNOTATION_CLASS)
@Retention(AnnotationRetention.SOURCE)
annotation class GenerateSetMultibindingsModule(
val interfaceClass: KClass<*>,
val modulePackage: String,
val moduleName: String,
)

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package ru.fredboy.cavedroid.ksp.processor

import com.google.devtools.ksp.processing.*
import com.google.devtools.ksp.symbol.KSAnnotated
import com.google.devtools.ksp.symbol.KSClassDeclaration
import com.google.devtools.ksp.symbol.KSType
import com.squareup.kotlinpoet.*
import com.squareup.kotlinpoet.ksp.toClassName
import com.squareup.kotlinpoet.ksp.writeTo
import ru.fredboy.cavedroid.ksp.annotations.GenerateSetMultibindingsModule

class GenerateSetMultibindingsSymbolProcessor(
private val codeGenerator: CodeGenerator,
private val logger: KSPLogger,
) : SymbolProcessor {

private fun generateModule(
interfaceName: ClassName,
moduleName: ClassName,
classes: List<ClassName>
): FileSpec? {
if (classes.isEmpty()) {
return null
}

val bindings = classes.map { clazz ->
FunSpec.builder("bind${clazz.simpleName}")
.addAnnotation(ClassName("dagger", "Binds"))
.addAnnotation(ClassName("dagger.multibindings", "IntoSet"))
.addParameter(ParameterSpec("impl", clazz))
.returns(interfaceName)
.addCode("return impl")
.build()
}

val moduleObject = TypeSpec.objectBuilder(moduleName)
.addAnnotation(ClassName("dagger", "Module"))
.addFunctions(bindings)
.build()

return FileSpec.builder(moduleName)
.addType(moduleObject)
.build()

}

private fun processAnnotation(resolver: Resolver, annotation: KSClassDeclaration) {
val args = annotation.annotations.first {
it.shortName.getShortName() == "GenerateSetMultibindingsModule"
}.arguments.takeIf { it.size == 3 } ?: run {
logger.error("GenerateSetMultibindingsModule should have 3 arguments")
throw IllegalArgumentException()
}

val interfaceName = args.first { it.name?.getShortName() == "interfaceClass" }.value as KSType
val modulePackage = args.first { it.name?.getShortName() == "modulePackage" }.value as String
val moduleName = args.first { it.name?.getShortName() == "moduleName" }.value as String

val moduleClassName = ClassName(modulePackage, moduleName)
val elements = resolver.getSymbolsWithAnnotation(annotation.qualifiedName!!.asString())
.filterIsInstance<KSClassDeclaration>()
.map(KSClassDeclaration::toClassName)
.toList()

logger.info("Found elements: ${elements.joinToString()}")

generateModule(
interfaceName = interfaceName.toClassName(),
moduleName = moduleClassName,
classes = elements
)?.writeTo(codeGenerator, Dependencies(true))
}

override fun process(resolver: Resolver): List<KSAnnotated> {
val annotations = resolver.getAnnotatedClasses(GenerateSetMultibindingsModule::class.qualifiedName!!, logger)
logger.info("Found annotations: ${annotations.joinToString { it.qualifiedName!!.asString() }}")
annotations.forEach { processAnnotation(resolver, it) }
return emptyList()
}

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
package ru.fredboy.cavedroid.ksp.processor

import com.google.devtools.ksp.processing.KSPLogger
import com.google.devtools.ksp.processing.Resolver
import com.google.devtools.ksp.symbol.KSClassDeclaration
import com.google.devtools.ksp.symbol.KSNode
import com.google.devtools.ksp.validate
import kotlin.reflect.KClass

internal fun Resolver.getAnnotatedClasses(
packageName: String,
annotationClass: KClass<*>
annotation: String,
logger: KSPLogger,
): Sequence<KSClassDeclaration> {
return getSymbolsWithAnnotation(annotationClass.qualifiedName.orEmpty())
logger.info("Resolving annotation $annotation")
return getSymbolsWithAnnotation(annotation)
.filterIsInstance<KSClassDeclaration>()
.filter { it.packageName.getShortName() == packageName }
.filter(KSNode::validate)
}
Loading

0 comments on commit f50a0d3

Please sign in to comment.