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)
}
}

View file

@ -3,7 +3,9 @@ import org.apache.tools.ant.filters.ReplaceTokens
import org.jetbrains.kotlin.util.parseSpaceSeparatedArgs
plugins {
idea
id("dev.adamko.factoriowebmap.archetype.node")
id("dev.adamko.tstl")
}
val projectId: String by project.extra
@ -33,7 +35,7 @@ val tstlTask = tasks.register<NpmTask>("typescriptToLua") {
val intermediateOutputDir = temporaryDir
args.set(parseSpaceSeparatedArgs("-- --outDir $intermediateOutputDir"))
val outputDir = modBuildDir.dir("typescriptToLua")
val outputDir: Directory = modBuildDir.dir("typescriptToLua")
outputs.dir(outputDir)
.withPropertyName("outputDir")
@ -54,19 +56,17 @@ val tstlTask = tasks.register<NpmTask>("typescriptToLua") {
idea {
module {
sourceDirs.add(layout.projectDirectory.dir("src/main/typescript").asFile)
excludeDirs.add(layout.projectDirectory.dir("src/main/typescript/node_modules").asFile)
resourceDirs.add(layout.projectDirectory.dir("src/main/resources").asFile)
resourceDirs.add(layout.projectDirectory.dir("infra").asFile)
excludeDirs.add(layout.projectDirectory.dir("infra/factorio-server").asFile)
sourceDirs.add(mkdir(layout.projectDirectory.dir("src/main/typescript").asFile))
excludeDirs.add(mkdir(layout.projectDirectory.dir("src/main/typescript/node_modules").asFile))
resourceDirs.add(mkdir(layout.projectDirectory.dir("src/main/resources").asFile))
resourceDirs.add(mkdir(layout.projectDirectory.dir("infra").asFile))
excludeDirs.add(mkdir(layout.projectDirectory.dir("infra/factorio-server").asFile))
}
}
val typescriptSrcSet =
project.objects.sourceDirectorySet("typescript", "typescript").apply {
srcDir(mkdir("$projectDir/src/main/typescript"))
srcDir(layout.projectDirectory.dir("src/main/typescript"))
destinationDirectory.set(modBuildDir.dir("typescriptToLua").asFile)
compiledBy(tstlTask) {
@ -228,33 +228,3 @@ tasks.register("downloadFactorioApiDocs") {
logger.lifecycle("Downloaded Factorio API json: $apiFile")
}
}
//tasks.register("downloadLuaProtobuf") {
// group = projectId
//
// val luaProtobufVersion = "0.3.3"
//
// val target = uri("https://github.com/starwing/lua-protobuf/archive/refs/tags/$luaProtobufVersion.zip")
// val downloadedFile = file("$temporaryDir/luaProtobuf-$luaProtobufVersion.zip")
//
// val luaProto = modBuildDir.file(apiFilename)
// outputs.file(apiFile)
//
// doLast {
//
// ant.invokeMethod(
// "get", mapOf(
// "src" to target,
// "dest" to downloadedFile,
// "verbose" to true,
// )
// )
//
// val json = downloadedFile.readText()
// val prettyJson = groovy.json.JsonOutput.prettyPrint(json)
//
// apiFile.asFile.writeText(prettyJson)
//
// logger.lifecycle("Downloaded Factorio API json: $apiFile")
// }
//}

View file

@ -3,7 +3,7 @@ version: "3.9"
services:
factorio-server:
image: factoriotools/factorio:1.1.42
image: factoriotools/factorio:1.1.46
# logging:
# driver: "json-file"
container_name: "factorio-server"

View file

@ -8,7 +8,7 @@
import {Serdes} from "./serdes/serdes"
import {FactorioEvent, JsonTable} from "./model/model";
const mapEventIdToName = new LuaTable<defines.events, keyof typeof defines.events>()
const mapEventIdToName = new LuaTable<defines.Events, keyof typeof defines.events>()
for (const [k, v] of pairs(defines.events)) {
mapEventIdToName.set(v, k)
}

View file

@ -37,9 +37,9 @@
}
},
"is-core-module": {
"version": "2.7.0",
"resolved": "https://registry.npmjs.org/is-core-module/-/is-core-module-2.7.0.tgz",
"integrity": "sha512-ByY+tjCciCr+9nLryBYcSD50EOGWt95c7tIsKTG1J2ixKKXPvF7Ej3AVd+UfDydAJom3biBGDBALaO79ktwgEQ==",
"version": "2.8.0",
"resolved": "https://registry.npmjs.org/is-core-module/-/is-core-module-2.8.0.tgz",
"integrity": "sha512-vd15qHsaqrRL7dtH6QNuy0ndJmRDrS9HAM1CAiSifNUFv4x1a0CCVsj18hJ1mShxIG6T2i1sO78MkP56r0nYRw==",
"requires": {
"has": "^1.0.3"
}
@ -74,30 +74,27 @@
"integrity": "sha512-GNzQvQTOIP6RyTfE2Qxb8ZVlNmw0n88vp1szwWRimP02mnTsx3Wtn5qRdqY9w2XduFNUgvOwhNnQsjwCp+kqaQ=="
},
"typed-factorio": {
"version": "0.8.0",
"resolved": "https://registry.npmjs.org/typed-factorio/-/typed-factorio-0.8.0.tgz",
"integrity": "sha512-vuAJfkTBSQKD8mHCN7CAbwJJ73y+xfxLBPlFsYjjd015tMPv8Yrq1hBXxoUFZ1ZWGzNgcFL9RwZ6TspsIH7WJw==",
"version": "0.11.0-packagetypes-1",
"resolved": "https://registry.npmjs.org/typed-factorio/-/typed-factorio-0.11.0-packagetypes-1.tgz",
"integrity": "sha512-oiN9XjODry434DPcgx3MNyC22/cOrbeU5/q0uM+Tj8BjJ7B42DYH+SR21tEtrHaWHNUJNX5iXD8h1Bk0N3Qvmw==",
"requires": {
"lua-types": "^2.10.1",
"typescript-to-lua": "^1.0.0"
}
},
"typescript": {
"version": "4.4.4",
"resolved": "https://registry.npmjs.org/typescript/-/typescript-4.4.4.tgz",
"integrity": "sha512-DqGhF5IKoBl8WNf8C1gu8q0xZSInh9j1kJJMqT3a94w1JzVaBU4EXOSMrz9yDqMT0xt3selp83fuFMQ0uzv6qA=="
},
"typescript-to-lua": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/typescript-to-lua/-/typescript-to-lua-1.0.1.tgz",
"integrity": "sha512-j5mb+8HgUUY8hhAKoNy6KUMeamEN1fsTtMvSbPaxmVkWw40pHdLi3umsQG+Qo7wM6IK+RQffFxbIWEjleF+xpw==",
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/typescript-to-lua/-/typescript-to-lua-1.1.1.tgz",
"integrity": "sha512-13vk/FSQyqbBkXNvg73LpT0tsPKdepJHPYR6TNtbc9l1ClSxAEUvkB3k49MAwSQ6LoN8Ui4Bgyu9dKFoDB315Q==",
"requires": {
"enhanced-resolve": "^5.8.2",
"resolve": "^1.15.1",
"source-map": "^0.7.3",
"typescript": "~4.3.5"
},
"dependencies": {
"typescript": {
"version": "4.3.5",
"resolved": "https://registry.npmjs.org/typescript/-/typescript-4.3.5.tgz",
"integrity": "sha512-DqQgihaQ9cUrskJo9kIyW/+g0Vxsk8cDtZ52a3NGh0YNTfpUSArXSohyUGnvbPazEPLu398C0UxmKSOrPumUzA=="
}
"source-map": "^0.7.3"
}
}
}

View file

@ -13,8 +13,12 @@
"dependencies": {
"lua-types": "^2.11.0",
"@types/node": "^12.0.8",
"typescript-to-lua": "1.0.1",
"typed-factorio": "0.8.0"
"typescript-to-lua": "1.1.1",
"typed-factorio": "0.11.0-packagetypes-1",
"typescript": "4.4.4"
},
"//": {
"typed-factorio": "0.10.0"
},
"peerDependencies": {},
"optionalDependencies": {},