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

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