start tidying up after schema->schema2 migration

This commit is contained in:
Adam 2022-04-20 11:28:30 +02:00
parent 448dd4a5b8
commit 8bf393f9c3
10 changed files with 507 additions and 499 deletions

View file

@ -1,68 +1,68 @@
package dev.adamko.kafkatorio.schema.events
import dev.adamko.kafkatorio.schema.SerializerProvider
import dev.adamko.kafkatorio.schema.SerializerProviderDeserializer
import dev.adamko.kafkatorio.schema.common.Tick
import kotlin.reflect.KClass
import kotlin.reflect.KType
import kotlin.reflect.typeOf
import kotlinx.serialization.KSerializer
import kotlinx.serialization.Serializable
//
//import dev.adamko.kafkatorio.schema.SerializerProvider
//import dev.adamko.kafkatorio.schema.SerializerProviderDeserializer
//import dev.adamko.kafkatorio.schema.common.Tick
//import kotlin.reflect.KClass
//import kotlin.reflect.KType
//import kotlin.reflect.typeOf
//import kotlinx.serialization.KSerializer
//import kotlinx.serialization.Serializable
//
//
////@Serializable
////data class KafkatorioPacket(
//// val modVersion: String,
//// val tick: Tick,
//// val data: KafkatorioPacketData,
////)
//
//
//@Serializable(with = KafkatorioPacketData.JsonSerializer::class)
//sealed class KafkatorioPacketData {
// abstract val updateType: KafkatorioPacketDataType
//
// object JsonSerializer : SerializerProviderDeserializer<KafkatorioPacketData>(
// KafkatorioPacketData::class,
// KafkatorioPacketData::updateType.name,
// { json -> KafkatorioPacketDataType.entries.firstOrNull { it.name == json } }
// )
//}
//
//
///** Discriminator for [KafkatorioPacketData] */
//@Serializable
//data class KafkatorioPacket(
// val modVersion: String,
// val tick: Tick,
// val data: KafkatorioPacketData,
//)
@Serializable(with = KafkatorioPacketData.JsonSerializer::class)
sealed class KafkatorioPacketData {
abstract val updateType: KafkatorioPacketDataType
object JsonSerializer : SerializerProviderDeserializer<KafkatorioPacketData>(
KafkatorioPacketData::class,
KafkatorioPacketData::updateType.name,
{ json -> KafkatorioPacketDataType.entries.firstOrNull { it.name == json } }
)
}
/** Discriminator for [KafkatorioPacketData] */
@Serializable
enum class KafkatorioPacketDataType(
override val serializer: KSerializer<out KafkatorioPacketData>
) : SerializerProvider<KafkatorioPacketData> {
// non-keyed
CONFIG(ConfigurationUpdate.serializer()),
CONSOLE_CHAT(ConsoleChatUpdate.serializer()),
CONSOLE_COMMAND(ConsoleCommandUpdate.serializer()),
PROTOTYPES(PrototypesUpdate.serializer()),
SURFACE(SurfaceUpdate.serializer()),
// keyed
ENTITY(EntityUpdate.serializer()),
MAP_CHUNK(MapChunkUpdate.serializer()),
PLAYER(PlayerUpdate.serializer()),
;
companion object {
val entries: Set<KafkatorioPacketDataType> = values().toSet()
}
}
//inline fun <reified T: KafkatorioPacketData> KafkatorioPacketDataType.isType (): KClass<out T> {
// return when (this) {
// KafkatorioPacketDataType.CONFIG -> ConfigurationUpdate::class
// KafkatorioPacketDataType.CONSOLE_CHAT -> ConsoleChatUpdate::class
// KafkatorioPacketDataType.CONSOLE_COMMAND -> ConsoleCommandUpdate::class
// KafkatorioPacketDataType.PROTOTYPES -> PrototypesUpdate::class
// KafkatorioPacketDataType.SURFACE -> SurfaceUpdate::class
// KafkatorioPacketDataType.ENTITY -> EntityUpdate::class
// KafkatorioPacketDataType.MAP_CHUNK -> MapChunkUpdate::class
// KafkatorioPacketDataType.PLAYER -> PlayerUpdate::class
//enum class KafkatorioPacketDataType(
// override val serializer: KSerializer<out KafkatorioPacketData>
//) : SerializerProvider<KafkatorioPacketData> {
//
// // non-keyed
// CONFIG(ConfigurationUpdate.serializer()),
// CONSOLE_CHAT(ConsoleChatUpdate.serializer()),
// CONSOLE_COMMAND(ConsoleCommandUpdate.serializer()),
// PROTOTYPES(PrototypesUpdate.serializer()),
// SURFACE(SurfaceUpdate.serializer()),
//
// // keyed
// ENTITY(EntityUpdate.serializer()),
// MAP_CHUNK(MapChunkUpdate.serializer()),
// PLAYER(PlayerUpdate.serializer()),
// ;
//
// companion object {
// val entries: Set<KafkatorioPacketDataType> = values().toSet()
// }
//}
//
////inline fun <reified T: KafkatorioPacketData> KafkatorioPacketDataType.isType (): KClass<out T> {
//// return when (this) {
//// KafkatorioPacketDataType.CONFIG -> ConfigurationUpdate::class
//// KafkatorioPacketDataType.CONSOLE_CHAT -> ConsoleChatUpdate::class
//// KafkatorioPacketDataType.CONSOLE_COMMAND -> ConsoleCommandUpdate::class
//// KafkatorioPacketDataType.PROTOTYPES -> PrototypesUpdate::class
//// KafkatorioPacketDataType.SURFACE -> SurfaceUpdate::class
//// KafkatorioPacketDataType.ENTITY -> EntityUpdate::class
//// KafkatorioPacketDataType.MAP_CHUNK -> MapChunkUpdate::class
//// KafkatorioPacketDataType.PLAYER -> PlayerUpdate::class
//// }
////}

View file

@ -1,95 +1,95 @@
package dev.adamko.kafkatorio.schema.events
import dev.adamko.kafkatorio.schema.common.PlayerIndex
import dev.adamko.kafkatorio.schema.common.SurfaceIndex
import dev.adamko.kafkatorio.schema.prototypes.FactorioPrototype
import kotlinx.serialization.EncodeDefault
import kotlinx.serialization.Serializable
/** No key, no caching. Must be sent instantly. */
sealed class KafkatorioInstantPacketData : KafkatorioPacketData()
/* *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** */
@Serializable
data class ConfigurationUpdate(
val factorioData: ConfigurationUpdateGameData,
val allMods: List<ConfigurationUpdateModData>,
val modStartupSettingsChange: Boolean,
val migrationApplied: Boolean,
) : KafkatorioInstantPacketData() {
@EncodeDefault
override val updateType: KafkatorioPacketDataType = KafkatorioPacketDataType.CONFIG
@Serializable
data class ConfigurationUpdateGameData(
val oldVersion: String? = null,
val newVersion: String? = null,
)
@Serializable
data class ConfigurationUpdateModData(
val modName: String,
val currentVersion: String? = null,
val previousVersion: String? = null,
)
}
/* *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** */
@Serializable
data class ConsoleChatUpdate(
val authorPlayerIndex: PlayerIndex?,
val content: String,
) : KafkatorioInstantPacketData() {
@EncodeDefault
override val updateType: KafkatorioPacketDataType = KafkatorioPacketDataType.CONSOLE_CHAT
}
/* *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** */
@Serializable
data class ConsoleCommandUpdate(
val authorPlayerIndex: PlayerIndex?,
val command: String,
val parameters: String,
) : KafkatorioInstantPacketData() {
@EncodeDefault
override val updateType: KafkatorioPacketDataType = KafkatorioPacketDataType.CONSOLE_COMMAND
}
/* *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** */
@Serializable
data class PrototypesUpdate(
val prototypes: List<FactorioPrototype>,
) : KafkatorioInstantPacketData() {
@EncodeDefault
override val updateType: KafkatorioPacketDataType = KafkatorioPacketDataType.PROTOTYPES
}
/* *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** */
@Serializable
data class SurfaceUpdate(
val index: SurfaceIndex,
val daytime: Double,
val name: String,
) : KafkatorioInstantPacketData() {
@EncodeDefault
override val updateType: KafkatorioPacketDataType = KafkatorioPacketDataType.SURFACE
}
/* *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** */
//
//import dev.adamko.kafkatorio.schema.common.PlayerIndex
//import dev.adamko.kafkatorio.schema.common.SurfaceIndex
//import dev.adamko.kafkatorio.schema.prototypes.FactorioPrototype
//import kotlinx.serialization.EncodeDefault
//import kotlinx.serialization.Serializable
//
//
///** No key, no caching. Must be sent instantly. */
//sealed class KafkatorioInstantPacketData : KafkatorioPacketData()
//
//
///* *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** */
//
//
//@Serializable
//data class ConfigurationUpdate(
// val factorioData: ConfigurationUpdateGameData,
// val allMods: List<ConfigurationUpdateModData>,
// val modStartupSettingsChange: Boolean,
// val migrationApplied: Boolean,
//) : KafkatorioInstantPacketData() {
// @EncodeDefault
// override val updateType: KafkatorioPacketDataType = KafkatorioPacketDataType.CONFIG
//
// @Serializable
// data class ConfigurationUpdateGameData(
// val oldVersion: String? = null,
// val newVersion: String? = null,
// )
//
// @Serializable
// data class ConfigurationUpdateModData(
// val modName: String,
// val currentVersion: String? = null,
// val previousVersion: String? = null,
// )
//}
//
//
///* *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** */
//
//
//@Serializable
//data class ConsoleChatUpdate(
// val authorPlayerIndex: PlayerIndex?,
// val content: String,
//) : KafkatorioInstantPacketData() {
// @EncodeDefault
// override val updateType: KafkatorioPacketDataType = KafkatorioPacketDataType.CONSOLE_CHAT
//}
//
//
///* *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** */
//
//
//@Serializable
//data class ConsoleCommandUpdate(
// val authorPlayerIndex: PlayerIndex?,
// val command: String,
// val parameters: String,
//) : KafkatorioInstantPacketData() {
// @EncodeDefault
// override val updateType: KafkatorioPacketDataType = KafkatorioPacketDataType.CONSOLE_COMMAND
//}
//
//
///* *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** */
//
//
//@Serializable
//data class PrototypesUpdate(
// val prototypes: List<FactorioPrototype>,
//) : KafkatorioInstantPacketData() {
// @EncodeDefault
// override val updateType: KafkatorioPacketDataType = KafkatorioPacketDataType.PROTOTYPES
//}
//
//
///* *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** */
//
//
//@Serializable
//data class SurfaceUpdate(
// val index: SurfaceIndex,
// val daytime: Double,
// val name: String,
//) : KafkatorioInstantPacketData() {
// @EncodeDefault
// override val updateType: KafkatorioPacketDataType = KafkatorioPacketDataType.SURFACE
//}
//
//
///* *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** */

View file

@ -1,135 +1,135 @@
package dev.adamko.kafkatorio.schema.events
import dev.adamko.kafkatorio.schema.common.Colour
import dev.adamko.kafkatorio.schema.common.EntityIdentifiers
import dev.adamko.kafkatorio.schema.common.EntityIdentifiersData
import dev.adamko.kafkatorio.schema.common.ForceIndex
import dev.adamko.kafkatorio.schema.common.MapChunkPosition
import dev.adamko.kafkatorio.schema.common.MapEntityPosition
import dev.adamko.kafkatorio.schema.common.MapTileDictionary
import dev.adamko.kafkatorio.schema.common.PlayerIndex
import dev.adamko.kafkatorio.schema.common.PrototypeName
import dev.adamko.kafkatorio.schema.common.SurfaceIndex
import dev.adamko.kafkatorio.schema.common.Tick
import dev.adamko.kafkatorio.schema.common.UnitNumber
import kotlinx.serialization.EncodeDefault
import kotlinx.serialization.Serializable
sealed interface KafkatorioKeyedPacketKey
/** Has a key, so it can be debounced */
sealed class KafkatorioKeyedPacketData : KafkatorioPacketData() {
/** Count how many events this packet is aggregating data from */
abstract val eventCounts: Map<String, UInt>?
}
/* *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** */
sealed interface EntityUpdateKey : EntityIdentifiers, KafkatorioKeyedPacketKey {
/** A [unitNumber] is required for caching */
override val unitNumber: UnitNumber
override val name: String
override val protoType: String
}
@Serializable
data class EntityUpdate(
override val unitNumber: UnitNumber,
override val name: String,
override val protoType: String,
override val eventCounts: Map<String, UInt>? = null,
val chunkPosition: MapChunkPosition? = null,
val graphicsVariation: UByte? = null,
val health: Float? = null,
val isActive: Boolean? = null,
val isRotatable: Boolean? = null,
val lastUser: UInt? = null,
val localisedDescription: String? = null,
val localisedName: String? = null,
val prototype: PrototypeName? = null,
) : KafkatorioKeyedPacketData(), EntityUpdateKey {
@EncodeDefault
override val updateType: KafkatorioPacketDataType = KafkatorioPacketDataType.ENTITY
}
/* *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** */
sealed interface MapChunkUpdateKey : KafkatorioKeyedPacketKey {
val chunkPosition: MapChunkPosition
val surfaceIndex: SurfaceIndex
}
@Serializable
data class MapChunkUpdate(
override val chunkPosition: MapChunkPosition,
override val surfaceIndex: SurfaceIndex,
override val eventCounts: Map<String, UInt>? = null,
val player: PlayerIndex? = null,
val robot: EntityIdentifiersData? = null,
val force: ForceIndex? = null,
/** updated tiles - might be partial and not all tiles in the chunk */
val tileDictionary: MapTileDictionary? = null,
val isDeleted: Boolean? = null,
) : KafkatorioKeyedPacketData(), MapChunkUpdateKey {
@EncodeDefault
override val updateType: KafkatorioPacketDataType = KafkatorioPacketDataType.MAP_CHUNK
}
/* *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** */
sealed interface PlayerUpdateKey : KafkatorioKeyedPacketKey {
val index: PlayerIndex
}
@Serializable
data class PlayerUpdate(
override val index: PlayerIndex,
override val eventCounts: Map<String, UInt>? = null,
val characterUnitNumber: UnitNumber? = null,
val chatColour: Colour? = null,
val colour: Colour? = null,
val name: String? = null,
val afkTime: Tick? = null,
val ticksToRespawn: Tick? = null,
val forceIndex: ForceIndex? = null,
val isAdmin: Boolean? = null,
val isConnected: Boolean? = null,
val isShowOnMap: Boolean? = null,
val isSpectator: Boolean? = null,
val lastOnline: Tick? = null,
val onlineTime: Tick? = null,
val position: MapEntityPosition? = null,
val surfaceIndex: SurfaceIndex? = null,
val tag: String? = null,
val diedCause: EntityIdentifiersData? = null,
val bannedReason: String? = null,
val kickedReason: String? = null,
val disconnectReason: String? = null,
/** `true` when a player is removed (deleted) from the game */
val isRemoved: Boolean? = null,
) : KafkatorioKeyedPacketData(), PlayerUpdateKey {
@EncodeDefault
override val updateType: KafkatorioPacketDataType = KafkatorioPacketDataType.PLAYER
}
/* *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** */
//import dev.adamko.kafkatorio.schema.common.Colour
//import dev.adamko.kafkatorio.schema.common.EntityIdentifiers
//import dev.adamko.kafkatorio.schema.common.EntityIdentifiersData
//import dev.adamko.kafkatorio.schema.common.ForceIndex
//import dev.adamko.kafkatorio.schema.common.MapChunkPosition
//import dev.adamko.kafkatorio.schema.common.MapEntityPosition
//import dev.adamko.kafkatorio.schema.common.MapTileDictionary
//import dev.adamko.kafkatorio.schema.common.PlayerIndex
//import dev.adamko.kafkatorio.schema.common.PrototypeName
//import dev.adamko.kafkatorio.schema.common.SurfaceIndex
//import dev.adamko.kafkatorio.schema.common.Tick
//import dev.adamko.kafkatorio.schema.common.UnitNumber
//import kotlinx.serialization.EncodeDefault
//import kotlinx.serialization.Serializable
//
//
//sealed interface KafkatorioKeyedPacketKey
//
//
///** Has a key, so it can be debounced */
//sealed class KafkatorioKeyedPacketData : KafkatorioPacketData() {
// /** Count how many events this packet is aggregating data from */
// abstract val eventCounts: Map<String, UInt>?
//}
//
//
///* *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** */
//
//
//sealed interface EntityUpdateKey : EntityIdentifiers, KafkatorioKeyedPacketKey {
// /** A [unitNumber] is required for caching */
// override val unitNumber: UnitNumber
// override val name: String
// override val protoType: String
//}
//
//
//@Serializable
//data class EntityUpdate(
// override val unitNumber: UnitNumber,
// override val name: String,
// override val protoType: String,
//
// override val eventCounts: Map<String, UInt>? = null,
//
// val chunkPosition: MapChunkPosition? = null,
// val graphicsVariation: UByte? = null,
// val health: Float? = null,
// val isActive: Boolean? = null,
// val isRotatable: Boolean? = null,
// val lastUser: UInt? = null,
// val localisedDescription: String? = null,
// val localisedName: String? = null,
// val prototype: PrototypeName? = null,
//) : KafkatorioKeyedPacketData(), EntityUpdateKey {
// @EncodeDefault
// override val updateType: KafkatorioPacketDataType = KafkatorioPacketDataType.ENTITY
//}
//
//
///* *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** */
//
//
//sealed interface MapChunkUpdateKey : KafkatorioKeyedPacketKey {
// val chunkPosition: MapChunkPosition
// val surfaceIndex: SurfaceIndex
//}
//
//
//@Serializable
//data class MapChunkUpdate(
// override val chunkPosition: MapChunkPosition,
// override val surfaceIndex: SurfaceIndex,
//
// override val eventCounts: Map<String, UInt>? = null,
//
// val player: PlayerIndex? = null,
// val robot: EntityIdentifiersData? = null,
// val force: ForceIndex? = null,
// /** updated tiles - might be partial and not all tiles in the chunk */
// val tileDictionary: MapTileDictionary? = null,
// val isDeleted: Boolean? = null,
//) : KafkatorioKeyedPacketData(), MapChunkUpdateKey {
// @EncodeDefault
// override val updateType: KafkatorioPacketDataType = KafkatorioPacketDataType.MAP_CHUNK
//}
//
//
///* *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** */
//
//
//sealed interface PlayerUpdateKey : KafkatorioKeyedPacketKey {
// val index: PlayerIndex
//}
//
//
//@Serializable
//data class PlayerUpdate(
// override val index: PlayerIndex,
//
// override val eventCounts: Map<String, UInt>? = null,
//
// val characterUnitNumber: UnitNumber? = null,
// val chatColour: Colour? = null,
// val colour: Colour? = null,
// val name: String? = null,
//
// val afkTime: Tick? = null,
// val ticksToRespawn: Tick? = null,
// val forceIndex: ForceIndex? = null,
// val isAdmin: Boolean? = null,
// val isConnected: Boolean? = null,
// val isShowOnMap: Boolean? = null,
// val isSpectator: Boolean? = null,
// val lastOnline: Tick? = null,
// val onlineTime: Tick? = null,
// val position: MapEntityPosition? = null,
// val surfaceIndex: SurfaceIndex? = null,
// val tag: String? = null,
// val diedCause: EntityIdentifiersData? = null,
//
// val bannedReason: String? = null,
// val kickedReason: String? = null,
// val disconnectReason: String? = null,
// /** `true` when a player is removed (deleted) from the game */
// val isRemoved: Boolean? = null,
//) : KafkatorioKeyedPacketData(), PlayerUpdateKey {
// @EncodeDefault
// override val updateType: KafkatorioPacketDataType = KafkatorioPacketDataType.PLAYER
//}
//
//
///* *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** */

View file

@ -1,58 +1,58 @@
package dev.adamko.kafkatorio.schema.prototypes
import dev.adamko.kafkatorio.schema.SerializerProvider
import dev.adamko.kafkatorio.schema.SerializerProviderDeserializer
import dev.adamko.kafkatorio.schema.common.Colour
import dev.adamko.kafkatorio.schema.common.PrototypeName
import dev.adamko.kafkatorio.schema.prototypes.FactorioPrototype.PrototypeObjectName
import kotlinx.serialization.Contextual
import kotlinx.serialization.DeserializationStrategy
import kotlinx.serialization.EncodeDefault
import kotlinx.serialization.KSerializer
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.JsonContentPolymorphicSerializer
import kotlinx.serialization.json.JsonElement
import kotlinx.serialization.json.contentOrNull
import kotlinx.serialization.json.jsonObject
import kotlinx.serialization.json.jsonPrimitive
@Serializable(with = FactorioPrototype.JsonSerializer::class)
sealed class FactorioPrototype {
@EncodeDefault
abstract val prototypeObjectName: PrototypeObjectName
enum class PrototypeObjectName(
override val serializer: KSerializer<out FactorioPrototype>
) : SerializerProvider<FactorioPrototype> {
LuaTilePrototype(MapTilePrototype.serializer()),
;
companion object {
val entries: Set<PrototypeObjectName> = values().toSet()
}
}
object JsonSerializer : SerializerProviderDeserializer<FactorioPrototype>(
FactorioPrototype::class,
FactorioPrototype::prototypeObjectName.name,
{ json -> PrototypeObjectName.entries.firstOrNull { it.name == json } }
)
}
@Serializable
data class MapTilePrototype(
val name: PrototypeName,
val layer: UInt,
val mapColour: Colour,
@Contextual
val collisionMasks: List<String>,
val order: String,
/** Can the tile be mined for resources? */
val canBeMined: Boolean,
) : FactorioPrototype() {
@EncodeDefault
override val prototypeObjectName: PrototypeObjectName = PrototypeObjectName.LuaTilePrototype
}
//
//import dev.adamko.kafkatorio.schema.SerializerProvider
//import dev.adamko.kafkatorio.schema.SerializerProviderDeserializer
//import dev.adamko.kafkatorio.schema.common.Colour
//import dev.adamko.kafkatorio.schema.common.PrototypeName
//import dev.adamko.kafkatorio.schema.prototypes.FactorioPrototype.PrototypeObjectName
//import kotlinx.serialization.Contextual
//import kotlinx.serialization.DeserializationStrategy
//import kotlinx.serialization.EncodeDefault
//import kotlinx.serialization.KSerializer
//import kotlinx.serialization.Serializable
//import kotlinx.serialization.json.JsonContentPolymorphicSerializer
//import kotlinx.serialization.json.JsonElement
//import kotlinx.serialization.json.contentOrNull
//import kotlinx.serialization.json.jsonObject
//import kotlinx.serialization.json.jsonPrimitive
//
//
//@Serializable(with = FactorioPrototype.JsonSerializer::class)
//sealed class FactorioPrototype {
// @EncodeDefault
// abstract val prototypeObjectName: PrototypeObjectName
//
// enum class PrototypeObjectName(
// override val serializer: KSerializer<out FactorioPrototype>
// ) : SerializerProvider<FactorioPrototype> {
// LuaTilePrototype(MapTilePrototype.serializer()),
// ;
//
// companion object {
// val entries: Set<PrototypeObjectName> = values().toSet()
// }
// }
//
// object JsonSerializer : SerializerProviderDeserializer<FactorioPrototype>(
// FactorioPrototype::class,
// FactorioPrototype::prototypeObjectName.name,
// { json -> PrototypeObjectName.entries.firstOrNull { it.name == json } }
// )
//}
//
//
//
//@Serializable
//data class MapTilePrototype(
// val name: PrototypeName,
// val layer: UInt,
// val mapColour: Colour,
// @Contextual
// val collisionMasks: List<String>,
// val order: String,
// /** Can the tile be mined for resources? */
// val canBeMined: Boolean,
//) : FactorioPrototype() {
// @EncodeDefault
// override val prototypeObjectName: PrototypeObjectName = PrototypeObjectName.LuaTilePrototype
//}

View file

@ -2,7 +2,6 @@ package dev.adamko.kafkatorio.schema2
import dev.adamko.kafkatorio.schema.common.PlayerIndex
import dev.adamko.kafkatorio.schema.common.SurfaceIndex
import dev.adamko.kafkatorio.schema.prototypes.FactorioPrototype
import dev.adamko.kafkatorio.schema.prototypes.FactorioPrototype2
import kotlinx.serialization.Serializable

View file

@ -1,147 +1,147 @@
package dev.adamko.kafkatorio.events.schema
import dev.adamko.kafkatorio.schema.events.KafkatorioInstantPacketData
import dev.adamko.kafkatorio.schema.events.KafkatorioKeyedPacketData
import dev.adamko.kafkatorio.schema.events.KafkatorioKeyedPacketKey
//import dev.adamko.kafkatorio.schema.events.KafkatorioPacket
import dev.adamko.kafkatorio.schema.prototypes.FactorioPrototype
import kotlin.io.path.Path
import kotlin.io.path.createDirectories
import me.ntrrgc.tsGenerator.TypeScriptGenerator
import org.intellij.lang.annotations.Language
/**
* Convert Kotlin classes to Typescript classes, using [TypeScriptGenerator]
*/
fun main(args: Array<String>) {
val gen = TypeScriptGenerator(
rootClasses = buildSet {
// add(KafkatorioPacket::class)
// addAll(KafkatorioPacket::class.sealedSubclasses)
add(KafkatorioInstantPacketData::class)
addAll(KafkatorioInstantPacketData::class.sealedSubclasses)
add(KafkatorioKeyedPacketData::class)
addAll(KafkatorioKeyedPacketData::class.sealedSubclasses)
addAll(KafkatorioKeyedPacketKey::class.sealedSubclasses)
add(FactorioPrototype::class)
addAll(FactorioPrototype::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(
ValueClassTransformer,
// PropertyNameTransformer,
),
// voidType = VoidType.UNDEFINED,
)
val definitions = gen.definitionsText
when (val filename = args.firstOrNull()) {
null -> println(definitions)
else -> {
val outPath = Path(filename)
outPath.createDirectories()
splitDefinitions(definitions)
.forEach { (defFile, def) ->
val outFile = outPath.resolve(defFile).toFile()
outFile.printWriter().use {
it.println(kt2tsHeader)
it.print(def)
}
println("Generated file: $outFile")
}
}
}
}
/** Split the TypeScript interfaces into different files */
private fun splitDefinitions(definitions: String): Map<String, String> {
return definitions
.split("\n\n")
.groupBy { def ->
when {
def isInterface "KafkatorioPacket"
|| def extendsAny listOf("KafkatorioPacket")
|| def isType "KafkatorioPacketDataType"
-> "kafkatorio-packet.d.ts"
def isInterface "KafkatorioInstantPacketData"
|| def extends "KafkatorioInstantPacketData"
|| def isType "KafkatorioInstantPacketDataType"
|| def isInterface "ConfigurationUpdateGameData"
|| def isInterface "ConfigurationUpdateModData"
-> "kafkatorio-instant-packet.d.ts"
def isInterface "KafkatorioKeyedPacket"
|| def isInterface "KafkatorioKeyedPacketData"
|| def extends "KafkatorioKeyedPacket"
|| def isType "KafkatorioKeyedPacketDataType"
|| def isInterface "KafkatorioKeyedPacketKey"
-> "kafkatorio-keyed-packet.d.ts"
def isInterface "FactorioPrototypes"
|| def isInterface "FactorioPrototype"
|| def extends "FactorioPrototype"
|| def isType "PrototypeObjectName"
-> "prototype.d.ts"
else -> "schema.d.ts"
}
}
.mapValues { (_, def) ->
def
.sorted()
.joinToString("\n\n", postfix = "\n")
.replace(" ", " ")
}
}
@Language("TypeScript")
val kt2tsHeader = """
|// Generated by TypeScriptGenerator - do not edit this file manually
|
""".trimMargin()
private infix fun String.extends(types: String): Boolean =
contains(Regex(".+extends.*(${types})"))
private infix fun String.extendsAny(types: List<String>): Boolean =
extends(types.joinToString("|") { "$it,".replaceAfterLast(',', "") })
private infix fun String.isInterface(type: String): Boolean =
contains(Regex("interface $type.*"))
private infix fun String.isType(type: String): Boolean =
contains(Regex("type $type.*"))
//object PropertyNameTransformer : ClassTransformer {
// override fun transformPropertyName(
// propertyName: String,
// property: KProperty<*>,
// klass: KClass<*>
// ): String {
// return camelCaseToSnakeCase(propertyName)
//import dev.adamko.kafkatorio.schema.events.KafkatorioInstantPacketData
//import dev.adamko.kafkatorio.schema.events.KafkatorioKeyedPacketData
//import dev.adamko.kafkatorio.schema.events.KafkatorioKeyedPacketKey
////import dev.adamko.kafkatorio.schema.events.KafkatorioPacket
//import dev.adamko.kafkatorio.schema.prototypes.FactorioPrototype
//import kotlin.io.path.Path
//import kotlin.io.path.createDirectories
//import me.ntrrgc.tsGenerator.TypeScriptGenerator
//import org.intellij.lang.annotations.Language
//
///**
// * Convert Kotlin classes to Typescript classes, using [TypeScriptGenerator]
// */
//fun main(args: Array<String>) {
//
// val gen = TypeScriptGenerator(
// rootClasses = buildSet {
//// add(KafkatorioPacket::class)
//// addAll(KafkatorioPacket::class.sealedSubclasses)
//
// add(KafkatorioInstantPacketData::class)
// addAll(KafkatorioInstantPacketData::class.sealedSubclasses)
//
// add(KafkatorioKeyedPacketData::class)
// addAll(KafkatorioKeyedPacketData::class.sealedSubclasses)
// addAll(KafkatorioKeyedPacketKey::class.sealedSubclasses)
//
// add(FactorioPrototype::class)
// addAll(FactorioPrototype::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(
// ValueClassTransformer,
//// PropertyNameTransformer,
// ),
//// voidType = VoidType.UNDEFINED,
// )
//
// val definitions = gen.definitionsText
//
// when (val filename = args.firstOrNull()) {
// null -> println(definitions)
// else -> {
// val outPath = Path(filename)
// outPath.createDirectories()
//
// splitDefinitions(definitions)
// .forEach { (defFile, def) ->
// val outFile = outPath.resolve(defFile).toFile()
// outFile.printWriter().use {
// it.println(kt2tsHeader)
// it.print(def)
// }
// println("Generated file: $outFile")
// }
//
// }
// }
//}
//
//
///** Split the TypeScript interfaces into different files */
//private fun splitDefinitions(definitions: String): Map<String, String> {
// return definitions
// .split("\n\n")
// .groupBy { def ->
//
// when {
// def isInterface "KafkatorioPacket"
// || def extendsAny listOf("KafkatorioPacket")
// || def isType "KafkatorioPacketDataType"
// -> "kafkatorio-packet.d.ts"
//
// def isInterface "KafkatorioInstantPacketData"
// || def extends "KafkatorioInstantPacketData"
// || def isType "KafkatorioInstantPacketDataType"
//
// || def isInterface "ConfigurationUpdateGameData"
// || def isInterface "ConfigurationUpdateModData"
// -> "kafkatorio-instant-packet.d.ts"
//
// def isInterface "KafkatorioKeyedPacket"
// || def isInterface "KafkatorioKeyedPacketData"
// || def extends "KafkatorioKeyedPacket"
// || def isType "KafkatorioKeyedPacketDataType"
//
// || def isInterface "KafkatorioKeyedPacketKey"
// -> "kafkatorio-keyed-packet.d.ts"
//
// def isInterface "FactorioPrototypes"
// || def isInterface "FactorioPrototype"
// || def extends "FactorioPrototype"
// || def isType "PrototypeObjectName"
// -> "prototype.d.ts"
//
// else -> "schema.d.ts"
// }
// }
// .mapValues { (_, def) ->
// def
// .sorted()
// .joinToString("\n\n", postfix = "\n")
// .replace(" ", " ")
// }
//}
//
//@Language("TypeScript")
//val kt2tsHeader = """
// |// Generated by TypeScriptGenerator - do not edit this file manually
// |
//""".trimMargin()
//
//private infix fun String.extends(types: String): Boolean =
// contains(Regex(".+extends.*(${types})"))
//
//private infix fun String.extendsAny(types: List<String>): Boolean =
// extends(types.joinToString("|") { "$it,".replaceAfterLast(',', "") })
//
//private infix fun String.isInterface(type: String): Boolean =
// contains(Regex("interface $type.*"))
//
//
//private infix fun String.isType(type: String): Boolean =
// contains(Regex("type $type.*"))
//
//
////object PropertyNameTransformer : ClassTransformer {
//// override fun transformPropertyName(
//// propertyName: String,
//// property: KProperty<*>,
//// klass: KClass<*>
//// ): String {
//// return camelCaseToSnakeCase(propertyName)
//// }
////}

View file

@ -10,6 +10,7 @@ import dev.adamko.kxstsgen.core.TsTypeRef
import kotlin.io.path.Path
import kotlin.io.path.createDirectories
import kotlinx.serialization.builtins.serializer
import org.intellij.lang.annotations.Language
@ExperimentalUnsignedTypes
@ -53,3 +54,9 @@ fun main(args: Array<String>) {
}
}
}
@Language("TypeScript")
val kt2tsHeader = """
|// Generated by kxs-ts-gen - do not edit this file manually
|
""".trimMargin()