Skip to content
This repository has been archived by the owner on Dec 28, 2021. It is now read-only.

Commit

Permalink
fix build scripts not generating jars
Browse files Browse the repository at this point in the history
add extendClass option to LDtkProject annotation

update ldtk-processor to handle new extendClass param
  • Loading branch information
LeHaine committed Mar 14, 2021
1 parent a050071 commit 94697f1
Show file tree
Hide file tree
Showing 11 changed files with 109 additions and 57 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
allprojects {
group = "com.lehaine"
version = "0.8.1-b4"
version = "0.8.1-b5"
}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
package com.lehaine.ldtk

import kotlin.reflect.KClass

@Target(AnnotationTarget.CLASS)
annotation class LDtkProject(val ldtkFileLocation: String, val name: String = "")
@Retention(AnnotationRetention.RUNTIME)
annotation class LDtkProject(
val ldtkFileLocation: String,
val name: String = "",
val extendClass: KClass<out Project> = Project::class
)
44 changes: 28 additions & 16 deletions ldtk-api/src/commonMain/kotlin/com/lehaine/ldtk/Project.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ import com.soywiz.korio.file.std.resourcesVfs

open class Project(val projectFilePath: String) {

val bgColorInt: Int
val bgColorHex: String
val worldLayout: WorldLayout
val defs: Definitions
var bgColorInt: Int = 0
protected set
var bgColorHex: String = "#000000"
protected set
var worldLayout: WorldLayout? = null
protected set
var defs: Definitions? = null
protected set

val tilesets = mutableMapOf<Int, Tileset>()
private val assetCache = mutableMapOf<String, ByteArray>()
Expand All @@ -31,16 +35,18 @@ open class Project(val projectFilePath: String) {
}
}

init {
val jsonString = runBlockingNoSuspensions {
resourcesVfs[projectFilePath].readString()
fun load() {
val jsonString = loadLDtkJson()

if (jsonString.isEmpty()) {
error("An empty file was passed in.")
}

val json = LDtkApi.parseLDtkFile(jsonString) ?: error("Unable to parse LDtk file content!")
defs = json.defs

json.levelDefinitions.forEach { levelJson ->
val level = instantiateLevel(this, levelJson)
val level = instantiateLevel(levelJson)
level?.let {
_allUntypedLevels.add(it)
}
Expand All @@ -55,31 +61,37 @@ open class Project(val projectFilePath: String) {
bgColorInt = hexToInt(json.bgColor)
}

open fun instantiateLevel(project: Project, json: LevelDefinition): Level? {
return Level(project, json)
open fun instantiateLevel(json: LevelDefinition): Level? {
return Level(this, json)
}

protected open fun loadLDtkJson(): String {
return runBlockingNoSuspensions {
resourcesVfs[projectFilePath].readString()
}
}

fun getAsset(relativePath: String): ByteArray {
if (assetCache.contains(relativePath)) {
return assetCache[relativePath] ?: error("Unable to load asset from asset cache!")
open fun getAsset(assetPath: String): ByteArray {
if (assetCache.contains(assetPath)) {
return assetCache[assetPath] ?: error("Unable to load asset from asset cache!")
}

return runBlockingNoSuspensions {
resourcesVfs[relativePath].readBytes()
resourcesVfs[assetPath].readBytes()
}
}

fun getLayerDef(uid: Int?, identifier: String? = ""): LayerDefinition? {
if (uid == null && identifier == null) {
return null
}
return defs.layers.find { it.uid == uid || it.identifier == identifier }
return defs?.layers?.find { it.uid == uid || it.identifier == identifier }
}

fun getTilesetDef(uid: Int?, identifier: String? = ""): TilesetDefinition? {
if (uid == null && identifier == null) {
return null
}
return defs.tilesets.find { it.uid == uid || it.identifier == identifier }
return defs?.tilesets?.find { it.uid == uid || it.identifier == identifier }
}
}
4 changes: 4 additions & 0 deletions ldtk-processor/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,18 @@ publishing {
publications {
create<MavenPublication>("ldtk-processor") {
artifactId = "ldtk-processor"
from(components["java"])
}
}
}

dependencies {
implementation(kotlin("stdlib"))
implementation(project(":ldtk-api"))
// implementation("com.lehaine:ldtk-api:$version")
implementation("com.squareup:kotlinpoet:1.7.2")
implementation("com.squareup:kotlinpoet-metadata:1.7.2")
implementation("com.squareup:kotlinpoet-metadata-specs:1.7.2")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.1.0")
implementation("com.google.auto.service:auto-service:1.0-rc7")
kapt("com.google.auto.service:auto-service:1.0-rc7")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.lehaine.ldtk.LDtkApi.LAYER_PREFIX
import com.lehaine.ldtk.LDtkApi.LEVEL_SUFFIX
import com.squareup.kotlinpoet.*
import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy
import com.squareup.kotlinpoet.metadata.KotlinPoetMetadataPreview
import java.io.File
import java.nio.file.Files
import java.nio.file.Path
Expand All @@ -16,6 +17,7 @@ import javax.annotation.processing.Processor
import javax.annotation.processing.RoundEnvironment
import javax.lang.model.SourceVersion
import javax.lang.model.element.TypeElement
import javax.lang.model.type.MirroredTypeException
import javax.tools.StandardLocation
import kotlin.reflect.KClass

Expand Down Expand Up @@ -50,23 +52,31 @@ open class ProjectProcessor : AbstractProcessor() {
return SourceVersion.latest()
}

@KotlinPoetMetadataPreview
override fun process(annotations: MutableSet<out TypeElement>, roundEnv: RoundEnvironment): Boolean {
roundEnv.getElementsAnnotatedWith(LDtkProject::class.java)
.forEach {
val ldtkProject = it.getAnnotation(LDtkProject::class.java)
var extendClass: TypeName? = null
try {
ldtkProject.extendClass
} catch (ex: MirroredTypeException) {
extendClass = ex.typeMirror.asTypeName()
}

val ldtkFileLocation = ldtkProject.ldtkFileLocation
val className = if (ldtkProject.name.isBlank()) {
"${it.simpleName}_"
} else {
ldtkProject.name
}
val pkg = processingEnv.elementUtils.getPackageOf(it).toString()
generateProject(className, pkg, ldtkFileLocation)
generateProject(className, pkg, ldtkFileLocation, extendClass!!)
}
return true
}

private fun generateProject(className: String, pkg: String, ldtkFileLocation: String) {
private fun generateProject(className: String, pkg: String, ldtkFileLocation: String, extendClass: TypeName) {
try {
val resource =
processingEnv.filer.createResource(StandardLocation.SOURCE_OUTPUT, "", "tmp_${className}", null)
Expand All @@ -80,15 +90,14 @@ open class ProjectProcessor : AbstractProcessor() {

val fileSpec = FileSpec.builder(pkg, className).indent(FILE_INDENT)
val projectClassSpec = TypeSpec.classBuilder(className).apply {
superclass(Project::class)
superclass(extendClass)
addSuperclassConstructorParameter("%S", ldtkFileLocation)
addFunction(
FunSpec.builder("instantiateLevel")
.addModifiers(KModifier.OVERRIDE)
.addParameter("project", Project::class)
.addParameter("json", LevelDefinition::class)
.returns(Level::class)
.addStatement("return %L$LEVEL_SUFFIX(project, json)", className)
.addStatement("return %L$LEVEL_SUFFIX(this, json)", className)
.build()
)
}
Expand All @@ -113,12 +122,15 @@ open class ProjectProcessor : AbstractProcessor() {
PropertySpec.builder(
"allLevels",
List::class.asTypeName().parameterizedBy(levelClassType)
).initializer(
CodeBlock.builder()
.beginControlFlow("allUntypedLevels.map")
.addStatement("it as %T", levelClassType)
.endControlFlow()
.build()
).getter(
FunSpec.getterBuilder().addCode(
CodeBlock.builder()
// .beginControlFlow("allUntypedLevels.map")
// .addStatement("it as %T", levelClassType)
// .endControlFlow()
.addStatement("return allUntypedLevels as List<%T>", levelClassType)
.build()
).build()
).build()
)

Expand Down
4 changes: 3 additions & 1 deletion libgdx-backend/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

repositories {
mavenCentral()
mavenLocal()
}

tasks.named<Jar>("jar") {
Expand All @@ -13,13 +13,15 @@ publishing {
publications {
create<MavenPublication>("libgdx-backend") {
artifactId = "libgdx-backend"
from(components["java"])
}
}
}

dependencies {
implementation(kotlin("stdlib"))
implementation(project(":ldtk-api"))
// implementation("com.lehaine:ldtk-api:$version")
implementation("com.badlogicgames.gdx:gdx-backend-lwjgl3:1.9.12")
implementation("com.badlogicgames.gdx:gdx-platform:1.9.12")
}
5 changes: 5 additions & 0 deletions libgdx-ldtk-processor/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

repositories {
mavenCentral()
mavenLocal()
}

tasks.named<Jar>("jar") {
Expand All @@ -13,6 +14,7 @@ publishing {
publications {
create<MavenPublication>("libgdx-ldtk-processor") {
artifactId = "libgdx-ldtk-processor"
from(components["java"])
}
}
}
Expand All @@ -22,6 +24,9 @@ dependencies {
implementation(project(":ldtk-api"))
implementation(project(":libgdx-backend"))
implementation(project(":ldtk-processor"))
// implementation("com.lehaine:ldtk-api-jvm:$version")
// implementation("com.lehaine:libgdx-backend:$version")
// implementation("com.lehaine:ldtk-processor:$version")
implementation("com.google.auto.service:auto-service:1.0-rc7")
kapt("com.google.auto.service:auto-service:1.0-rc7")
}
4 changes: 4 additions & 0 deletions samples/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import org.jetbrains.kotlin.gradle.plugin.KotlinPlatformType
repositories {
maven(url = "https://jitpack.io")
mavenCentral()
mavenLocal()
}

configurations.all {
Expand All @@ -23,6 +24,9 @@ dependencies {
// implementation("com.lehaine.kt-ldtk-api:ldtk-api:-SNAPSHOT")
// implementation("com.lehaine.kt-ldtk-api:libgdx-backend:-SNAPSHOT")
// kapt("com.lehaine.kt-ldtk-api:libgdx-ldtk-processor:-SNAPSHOT")
// implementation("com.lehaine:ldtk-api:$version")
// implementation("com.lehaine:libgdx-backend:$version")
// kapt("com.lehaine:libgdx-ldtk-processor:$version")
implementation("com.badlogicgames.gdx:gdx-backend-lwjgl3:1.9.12")
implementation("com.badlogicgames.gdx:gdx-platform:1.9.12:natives-desktop")
implementation("com.badlogicgames.gdx:gdx:1.9.12")
Expand Down
48 changes: 24 additions & 24 deletions samples/src/main/java/com/lehaine/samples/SampleJava.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,30 @@
public class SampleJava {
public static void main(String[] args) {
// create new LDtk world
JavaWorld world = new JavaWorld();

// get a level
JavaWorld.JavaWorldLevel level = world.getAllLevels().get(0);

// iterate over a layers tiles
for (LayerAutoLayer.AutoTile tile : level.getLayerCavern_background().getAutoTiles()) {
// logic for handling the tile
int x = tile.getRenderX();
}

// iterate over entities
for (JavaWorld.EntityMob mob : level.getLayerEntities().getAllMob()) {
JavaWorld.MobType type = mob.type;
// field arrays / lists
List<Point> patrolPoint = mob.getPatrol();
int health = mob.getHealth();
}

for (JavaWorld.EntityItem item : level.getLayerEntities().getAllItem()) {
if (item.type == JavaWorld.Items.Pickaxe) {
// spawn pickaxe
}
}
// JavaWorld world = new JavaWorld();
//
// // get a level
// JavaWorld.JavaWorldLevel level = world.getAllLevels().get(0);
//
// // iterate over a layers tiles
// for (LayerAutoLayer.AutoTile tile : level.getLayerCavern_background().getAutoTiles()) {
// // logic for handling the tile
// int x = tile.getRenderX();
// }
//
// // iterate over entities
// for (JavaWorld.EntityMob mob : level.getLayerEntities().getAllMob()) {
// JavaWorld.MobType type = mob.type;
// // field arrays / lists
// List<Point> patrolPoint = mob.getPatrol();
// int health = mob.getHealth();
// }
//
// for (JavaWorld.EntityItem item : level.getLayerEntities().getAllItem()) {
// if (item.type == JavaWorld.Items.Pickaxe) {
// // spawn pickaxe
// }
// }
}
}

Expand Down
2 changes: 1 addition & 1 deletion samples/src/main/kotlin/com/lehaine/samples/GdxTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class GdxApp : ApplicationListener {
private lateinit var unitTestWorldTiles: Texture
private lateinit var camera: OrthographicCamera
private lateinit var viewport: FitViewport
private val world = World()
private val world = World().apply { load() }

private var currentWorldIdx = 0
private var worldBgImage: TextureRegion? = null
Expand Down
10 changes: 8 additions & 2 deletions samples/src/main/kotlin/com/lehaine/samples/Sample.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,23 @@ package com.lehaine.samples

import com.lehaine.ldtk.LDtkProject
import com.lehaine.ldtk.Point
import com.lehaine.ldtk.Project
import java.io.BufferedReader
import java.io.InputStream
import java.io.InputStreamReader

// designate class for loading and attaching LDtk file to
@LDtkProject(ldtkFileLocation = "sample.ldtk", name = "World")
class _World
open class _World(projectFilePath: String)

@LDtkProject(ldtkFileLocation = "unitTest.ldtk", name = "UnitTestWorld")
class _UnitTestWorld

fun main(args: Array<String>) {
// create new LDtk world
val world = World()
world.load()


// get a level
val level: World.WorldLevel = world.allLevels[0]
Expand All @@ -27,7 +33,7 @@ fun main(args: Array<String>) {
// access entity fields
val type: World.MobType = mob.type // generated enum class
// field arrays / lists
val patrolPoints: List<Point>? = mob.patrol // points
val patrolPoints: List<Point> = mob.patrol // points
val health: Int = mob.health
}

Expand Down

0 comments on commit 94697f1

Please sign in to comment.