backing up before finally deciding on how to sync Kotlin and Typescript data model

This commit is contained in:
Adam 2021-11-10 17:34:01 +01:00
parent 7dd7baef34
commit b3af697636
23 changed files with 679 additions and 97 deletions

View file

@ -1,6 +1,14 @@
import org.gradle.process.internal.DefaultExecSpec
import org.jetbrains.kotlin.gradle.plugin.KotlinCompilation
import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinJsCompilation
import org.jetbrains.kotlin.gradle.targets.js.npm.npmProject
import org.jetbrains.kotlin.util.parseSpaceSeparatedArgs
plugins {
id("dev.adamko.factoriowebmap.archetype.base")
kotlin("multiplatform")
id("dev.adamko.factoriowebmap.archetype.kotlin-multiplatform")
kotlin("plugin.serialization")
}
val projectId: String by project.extra
@ -13,6 +21,8 @@ kotlin {
compilations.configureEach {
kotlinOptions {
jvmTarget = "11"
apiVersion = "1.5"
languageVersion = "1.5"
}
}
}
@ -20,8 +30,12 @@ kotlin {
sourceSets {
val jvmMain by getting {
dependencies {
api("com.google.protobuf:protobuf-javalite:3.19.1")
api("com.google.protobuf:protobuf-kotlin-lite:3.19.1")
implementation("com.github.ntrrgc:ts-generator:1.1.1")
implementation("org.jetbrains.kotlin:kotlin-reflect")
val kotlinXSerializationVersion = "1.3.0"
implementation("org.jetbrains.kotlinx:kotlinx-serialization-core:$kotlinXSerializationVersion")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:$kotlinXSerializationVersion")
}
}
val jvmTest by getting {
@ -32,11 +46,24 @@ kotlin {
//<editor-fold desc="JS">
js(IR) {
browser()
moduleName = "data-model"
browser { }
binaries.executable()
}
sourceSets {
val jsMain by getting {
val jsMain: KotlinSourceSet by getting {
dependencies {
implementation(npm("lua-types", "^2.11.0"))
implementation(npm("@types/node", "^12.0.8"))
implementation(npm("typescript-to-lua", "1.1.1"))
implementation(npm("typescript", "4.4.4"))
implementation(npm("typed-factorio", "0.11.0-packagetypes-1", false))
}
}
val jsTest by getting {
@ -52,11 +79,113 @@ kotlin {
languageVersion = "1.5"
// enableLanguageFeature("InlineClasses") // language feature name
optIn("kotlin.OptIn")
optIn("kotlin.RequiresOptIn")
optIn("kotlin.ExperimentalStdlibApi")
optIn("kotlin.time.ExperimentalTime")
optIn("kotlinx.coroutines.ExperimentalCoroutinesApi")
optIn("kotlin.js.ExperimentalJsExport")
optIn("kotlinx.serialization.ExperimentalSerializationApi")
// progressiveMode = true // false by default
}
}
}
//kotlin.js()
// .compilations
// .getByName(KotlinCompilation.MAIN_COMPILATION_NAME)
// .npmProject
// .useTool()
afterEvaluate {
// val nodeJsExecTstl by tasks.creating(NodeJsExec::class) {
// group = project.name
//
// nodeArgs += listOf(
// "tstl",
// )
// }
val kotlinJsCompilation: KotlinJsCompilation =
kotlin.targets
.flatMap { it.compilations }
.filterIsInstance<KotlinJsCompilation>()
.first { it.name == KotlinCompilation.MAIN_COMPILATION_NAME }
val npmProject = kotlinJsCompilation.npmProject
tasks.create<Exec>("tstl") {
group = project.name
standardOutput = System.out
errorOutput = System.err
workingDir = npmProject.dir
executable =
when (npmProject.nodeJs.requireConfigured().isWindows) {
true -> "npx.cmd"
false -> "npx"
}
args(parseSpaceSeparatedArgs("tstl --out $temporaryDir"))
}
tasks.create("asd") {
group = project.name
doLast {
val execSpec =
project.objects.newInstance(DefaultExecSpec::class.java).apply {
isIgnoreExitValue = false
standardOutput = System.out
errorOutput = System.err
}
kotlin.targets
.flatMap { it.compilations }
.filterIsInstance<KotlinJsCompilation>()
.filter { it.name == KotlinCompilation.MAIN_COMPILATION_NAME }
.forEach {
org.jetbrains.kotlin.gradle.targets.js.nodejs.NodeJsExec.create(it, "blahDeBlah") {
}
execSpec.workingDir = it.npmProject.dir
execSpec.executable =
when (it.npmProject.nodeJs.requireConfigured().isWindows) {
true -> "npx.cmd"
false -> "npx"
}
execSpec.args("tstl", "--version")
// execSpec.commandLine
// it.npmProject.useTool(
// execSpec,
// "typescript-to-lua/dist/tstl.js",
// listOf(),
// listOf( "--outDir","$buildDir/testAsd"),
// listOf("--version"),
// listOf(),
// )
}
exec {
execSpec.copyTo(this)
}
}
}
}

View file

@ -0,0 +1,56 @@
package dev.adamko.factorioevents.model
import kotlin.js.ExperimentalJsExport
import kotlin.js.JsExport
//
//@JsExport
//@ExperimentalJsExport
//data class FactorioEvent(
// val modVersion: String,
// val tick: F_uint,
// val eventType: String,
// val data: FactorioObjectData,
//)
//
//@JsExport
//abstract class FactorioObjectData {
// abstract val objectName: String
//}
//
//@JsExport
//data class PlayerData(
// override val objectName: String,
// val name: String,
// val characterUnitNumber: F_uint,
// val associatedCharactersUnitNumbers: List<F_uint>,
// val position: PositionData,
//) : FactorioObjectData()
//
//
//@JsExport
//data class PositionData(
// val x: F_int,
// val y: F_int,
//)
//
//
//
//typealias F_float = Float
//typealias F_double = Double
//typealias F_int = Int
//typealias F_int8 = Byte
//
//
////@JsExport
////@ExperimentalJsExport
//typealias F_uint = UInt
////expect class F_uint
//
////expect value class F_uint internal constructor(internal val data: Int)
//expect value class F_uint8 internal constructor(internal val data: Int)
//expect value class F_uint16 internal constructor(internal val data: Int)
//expect value class F_uint64 internal constructor(internal val data: Int)
//
//typealias F_table = Any?
//

View file

@ -0,0 +1,13 @@
package dev.adamko.factorioevents.model
import kotlin.js.*
//
//@JsModule("typed-factorio")
//external class uint
//
//actual typealias F_uint = uint
//actual typealias F_uint8 = UByte
//actual typealias F_uint16 = UShort
//actual typealias F_uint64 = ULong
//

View file

@ -0,0 +1,97 @@
@file:Suppress("ClassName")
package dev.adamko.factorioevents.model
import kotlinx.serialization.Serializable
/*
* The basic types used in the Factorio API. While Lua only supports the `boolean`, `number`,
* `string`, and `table` types (as they relate to the types listed below), the API docs will
* specify what C++ types these are converted to or from internally.
*/
/**
* Tables are enclosed in curly brackets, like this `{}`
*
* [View documentation](https://lua-api.factorio.com/next/Builtin-Types.html#table)
*/
typealias F_table = MutableMap<Any, Any>
sealed interface FBuiltInType {
sealed interface FNumber {
/**
* A floating-point number. This is a single-precision floating point number. Whilst Lua only uses double-precision
* numbers, when a function takes a float, the game engine will immediately convert the double-precision number to
* single-precision.
*
* [View documentation](https://lua-api.factorio.com/next/Builtin-Types.html#float)
*/
@Serializable
@JvmInline
value class F_float(val float: Float) : FNumber
/**
* A double-precision floating-point number. This is the same data type as all Lua numbers use.
*
* [View documentation](https://lua-api.factorio.com/next/Builtin-Types.html#double)
*/
@Serializable
@JvmInline
value class F_double(val double: Double) : FNumber
/**
* 32-bit signed integer. Possible values are -2,147,483,648 to 2,147,483,647.
*
* [View documentation](https://lua-api.factorio.com/next/Builtin-Types.html#int)
*/
@Serializable
@JvmInline
value class F_int(val int: Int) : FNumber
/**
* 8-bit signed integer. Possible values are -128 to 127.
*
* [View documentation](https://lua-api.factorio.com/next/Builtin-Types.html#int8)
*/
@Serializable
@JvmInline
value class F_int8(val int8: Byte) : FNumber
/**
* 32-bit unsigned integer. Possible values are 0 to 4,294,967,295.
*
* [View documentation](https://lua-api.factorio.com/next/Builtin-Types.html#uint)
*/
@Serializable
@JvmInline
value class F_uint(val uint: UInt) : FNumber
/**
* 8-bit unsigned integer. Possible values are 0 to 255.
*
* [View documentation](https://lua-api.factorio.com/next/Builtin-Types.html#uint8)
*/
@Serializable
@JvmInline
value class F_uint8(val uint8: UByte) : FNumber
/**
* 16-bit unsigned integer. Possible values are 0 to 65535.
*
* [View documentation](https://lua-api.factorio.com/next/Builtin-Types.html#uint16)
*/
@Serializable
@JvmInline
value class F_uint16(val uint16: UShort) : FNumber
/**
* 64-bit unsigned integer. Possible values are 0 to 18,446,744,073,709,551,615.
*
* [View documentation](https://lua-api.factorio.com/next/Builtin-Types.html#uint64)
*/
@Serializable
@JvmInline
value class F_uint64(val uint64: ULong) : FNumber
}
}

View file

@ -0,0 +1,58 @@
package dev.adamko.factorioevents.model
import dev.adamko.factorioevents.model.FBuiltInType.FNumber.F_float
import dev.adamko.factorioevents.model.FBuiltInType.FNumber.F_int
import dev.adamko.factorioevents.model.FBuiltInType.FNumber.F_uint
import kotlinx.serialization.*
import kotlinx.serialization.builtins.*
import kotlinx.serialization.json.*
import kotlinx.serialization.modules.*
@Serializable
data class FactorioEvent(
val modVersion: String,
val tick: UInt,
val eventType: String,
val data: FactorioObjectData,
)
@JsonClassDiscriminator("object_name")
sealed interface FactorioObjectData {
val objectName: String
}
@SerialName("player")
data class PlayerData(
override val objectName: String,
val characterUnitNumber: UInt,
val associatedCharacterUnitNumbers: List<UInt>,
val positionData: PositionData,
) : FactorioObjectData
data class EntityData(
override val objectName: String,
val name: String,
val type: String,
val active: Boolean,
val healthRatio: Float,
val health: Float?,
val surface_index: Int,
val unit_number: UInt?,
val position: PositionData,
val player_index: UInt?,
) : FactorioObjectData
data class SurfaceData(
override val objectName: String,
val name: String,
val index: UInt,
val daytime: Float,
) : FactorioObjectData
data class PositionData(
val x: Int,
val y: Int,
)

View file

@ -0,0 +1,46 @@
package dev.adamko.factorioevents.model
import kotlin.reflect.KClass
import kotlin.reflect.KProperty
import me.ntrrgc.tsGenerator.ClassTransformer
import me.ntrrgc.tsGenerator.TypeScriptGenerator
import me.ntrrgc.tsGenerator.camelCaseToSnakeCase
fun main() {
val gen =
TypeScriptGenerator(
rootClasses =
buildSet {
add(FactorioEvent::class)
addAll(FactorioObjectData::class.sealedSubclasses)
},
mappings = mapOf(
// builtin Factorio numeric types > `typed-factorio/generated/builtin-types.d.ts`
Float::class to "float",
Double::class to "double",
Int::class to "int",
Byte::class to "int8",
UInt::class to "uint",
UByte::class to "uint8",
UShort::class to "uint16",
ULong::class to "uint64",
),
classTransformers = listOf(
PropertyNameTransformer,
)
)
println(gen.definitionsText)
}
object PropertyNameTransformer : ClassTransformer {
override fun transformPropertyName(
propertyName: String,
property: KProperty<*>,
klass: KClass<*>
): String {
return camelCaseToSnakeCase(propertyName)
}
}