generalize network registry item and enum reading

This commit is contained in:
Bixilon 2022-09-03 22:06:20 +02:00
parent 919340fbac
commit a08e81b1c7
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
16 changed files with 43 additions and 26 deletions

View File

@ -27,7 +27,7 @@ object GlobalPositionEntityDataType : EntityDataType<GlobalPosition> {
if (buffer.versionId < ProtocolVersions.V_1_19_PRE2) { // ToDo: find out version if (buffer.versionId < ProtocolVersions.V_1_19_PRE2) { // ToDo: find out version
return buffer.readNBT()?.toJsonObject()?.toGlobalPosition(buffer.connection) return buffer.readNBT()?.toJsonObject()?.toGlobalPosition(buffer.connection)
} }
val dimension = buffer.connection.registries.dimensionRegistry[buffer.readResourceLocation()] val dimension = buffer.readLegacyRegistryItem(buffer.connection.registries.dimensionRegistry)
val position = buffer.readBlockPosition() val position = buffer.readBlockPosition()
return GlobalPosition(dimension, position) return GlobalPosition(dimension, position)
} }

View File

@ -144,17 +144,17 @@ class RenderWindow(
tintManager.init(connection.assetsManager) tintManager.init(connection.assetsManager)
Log.log(LogMessageType.RENDERING_LOADING, LogLevels.VERBOSE) { "Creating context (${stopwatch.labTime()})..." } Log.log(LogMessageType.RENDERING_LOADING, LogLevels.VERBOSE) { "Creating context (after ${stopwatch.labTime()})..." }
renderSystem.init() renderSystem.init()
Log.log(LogMessageType.RENDERING_LOADING, LogLevels.VERBOSE) { "Enabling all open gl features (${stopwatch.labTime()})..." } Log.log(LogMessageType.RENDERING_LOADING, LogLevels.VERBOSE) { "Enabling all open gl features (after ${stopwatch.labTime()})..." }
renderSystem.reset() renderSystem.reset()
// Init stage // Init stage
val initLatch = CountUpAndDownLatch(1, latch) val initLatch = CountUpAndDownLatch(1, latch)
Log.log(LogMessageType.RENDERING_LOADING, LogLevels.VERBOSE) { "Generating font and gathering textures (${stopwatch.labTime()})..." } Log.log(LogMessageType.RENDERING_LOADING, LogLevels.VERBOSE) { "Generating font and gathering textures (after ${stopwatch.labTime()})..." }
textureManager.dynamicTextures.load(initLatch) textureManager.dynamicTextures.load(initLatch)
textureManager.loadDefaultSkins(connection) textureManager.loadDefaultSkins(connection)
textureManager.loadDefaultTextures() textureManager.loadDefaultTextures()
@ -164,7 +164,7 @@ class RenderWindow(
framebufferManager.init() framebufferManager.init()
Log.log(LogMessageType.RENDERING_LOADING, LogLevels.VERBOSE) { "Initializing renderer (${stopwatch.labTime()})..." } Log.log(LogMessageType.RENDERING_LOADING, LogLevels.VERBOSE) { "Initializing renderer (after ${stopwatch.labTime()})..." }
lightMap.init() lightMap.init()
skeletalManager.init() skeletalManager.init()
renderer.init(initLatch) renderer.init(initLatch)
@ -174,27 +174,27 @@ class RenderWindow(
initLatch.await() initLatch.await()
// Post init stage // Post init stage
Log.log(LogMessageType.RENDERING_LOADING, LogLevels.VERBOSE) { "Preloading textures (${stopwatch.labTime()})..." } Log.log(LogMessageType.RENDERING_LOADING, LogLevels.VERBOSE) { "Preloading textures (after ${stopwatch.labTime()})..." }
textureManager.staticTextures.preLoad(latch) textureManager.staticTextures.preLoad(latch)
Log.log(LogMessageType.RENDERING_LOADING, LogLevels.VERBOSE) { "Loading textures (${stopwatch.labTime()})..." } Log.log(LogMessageType.RENDERING_LOADING, LogLevels.VERBOSE) { "Loading textures (after ${stopwatch.labTime()})..." }
textureManager.staticTextures.load(latch) textureManager.staticTextures.load(latch)
font.postInit(latch) font.postInit(latch)
Log.log(LogMessageType.RENDERING_LOADING, LogLevels.VERBOSE) { "Post loading renderer (${stopwatch.labTime()})..." } Log.log(LogMessageType.RENDERING_LOADING, LogLevels.VERBOSE) { "Post loading renderer (after ${stopwatch.labTime()})..." }
shaderManager.postInit() shaderManager.postInit()
skeletalManager.postInit() skeletalManager.postInit()
renderer.postInit(latch) renderer.postInit(latch)
framebufferManager.postInit() framebufferManager.postInit()
Log.log(LogMessageType.RENDERING_LOADING, LogLevels.VERBOSE) { "Loading skeletal meshes ${stopwatch.totalTime()}" } Log.log(LogMessageType.RENDERING_LOADING, LogLevels.VERBOSE) { "Loading skeletal meshes (after ${stopwatch.labTime()})" }
for (model in modelLoader.entities.skeletal.values) { for (model in modelLoader.entities.skeletal.values) {
model.loadMesh(this) model.loadMesh(this)
} }
Log.log(LogMessageType.RENDERING_LOADING, LogLevels.VERBOSE) { "Registering callbacks (${stopwatch.labTime()})..." } Log.log(LogMessageType.RENDERING_LOADING, LogLevels.VERBOSE) { "Registering callbacks (after ${stopwatch.labTime()})..." }
connection.registerEvent(CallbackEventInvoker.of<WindowFocusChangeEvent> { connection.registerEvent(CallbackEventInvoker.of<WindowFocusChangeEvent> {
renderingState = it.focused.decide(RenderingStates.RUNNING, RenderingStates.SLOW) renderingState = it.focused.decide(RenderingStates.RUNNING, RenderingStates.SLOW)

View File

@ -24,7 +24,7 @@ import de.bixilon.minosoft.util.logging.LogMessageType
@LoadPacket @LoadPacket
class ItemCooldownS2CP(buffer: PlayInByteBuffer) : PlayS2CPacket { class ItemCooldownS2CP(buffer: PlayInByteBuffer) : PlayS2CPacket {
val item = buffer.connection.registries.itemRegistry[buffer.readVarInt()] val item = buffer.readRegistryItem(buffer.connection.registries.itemRegistry)
val time = buffer.readVarInt() val time = buffer.readVarInt()
override fun handle(connection: PlayConnection) { override fun handle(connection: PlayConnection) {

View File

@ -29,9 +29,9 @@ import de.bixilon.minosoft.util.logging.LogMessageType
@LoadPacket @LoadPacket
class ParticleS2CP(buffer: PlayInByteBuffer) : PlayS2CPacket { class ParticleS2CP(buffer: PlayInByteBuffer) : PlayS2CPacket {
val type: ParticleType = if (buffer.versionId < ProtocolVersions.V_14W19A) { val type: ParticleType = if (buffer.versionId < ProtocolVersions.V_14W19A) {
buffer.connection.registries.particleTypeRegistry[buffer.readResourceLocation()]!! buffer.readLegacyRegistryItem(buffer.connection.registries.particleTypeRegistry)!!
} else if (buffer.versionId >= ProtocolVersions.V_22W17A) { // ToDo: maybe this was even earlier, should only differ some snapshots } else if (buffer.versionId >= ProtocolVersions.V_22W17A) { // ToDo: maybe this was even earlier, should only differ some snapshots
buffer.connection.registries.particleTypeRegistry[buffer.readVarInt()] buffer.readRegistryItem(buffer.connection.registries.particleTypeRegistry)
} else { } else {
buffer.connection.registries.particleTypeRegistry[buffer.readInt()] buffer.connection.registries.particleTypeRegistry[buffer.readInt()]
} }

View File

@ -62,7 +62,7 @@ class RespawnS2CP(buffer: PlayInByteBuffer) : PlayS2CPacket {
}].type }].type
} }
buffer.versionId < ProtocolVersions.V_1_16_2_PRE3 || buffer.versionId >= ProtocolVersions.V_22W19A -> { buffer.versionId < ProtocolVersions.V_1_16_2_PRE3 || buffer.versionId >= ProtocolVersions.V_22W19A -> {
buffer.connection.registries.dimensionRegistry[buffer.readResourceLocation()]!!.type buffer.readLegacyRegistryItem(buffer.connection.registries.dimensionRegistry)!!.type
} }
else -> { else -> {
DimensionProperties.deserialize(buffer.readNBT().asJsonObject()) // current dimension data DimensionProperties.deserialize(buffer.readNBT().asJsonObject()) // current dimension data

View File

@ -35,7 +35,7 @@ class StatisticsS2CP(buffer: PlayInByteBuffer) : PlayS2CPacket {
val name = buffer.readResourceLocation() val name = buffer.readResourceLocation()
val value = buffer.readVarInt() val value = buffer.readVarInt()
} else { } else {
val type = buffer.connection.registries.statisticRegistry[buffer.readVarInt()] val type = buffer.readRegistryItem(buffer.connection.registries.statisticRegistry)
val keyId = buffer.readVarInt() val keyId = buffer.readVarInt()
val key: Any = when (type.unit) { val key: Any = when (type.unit) {
StatisticUnits.BLOCK -> buffer.connection.registries.blockRegistry[keyId] StatisticUnits.BLOCK -> buffer.connection.registries.blockRegistry[keyId]

View File

@ -33,7 +33,7 @@ class BlockActionS2CP(buffer: PlayInByteBuffer) : PlayS2CPacket {
} }
val data1: Byte = buffer.readByte() val data1: Byte = buffer.readByte()
val data2: Byte = buffer.readByte() val data2: Byte = buffer.readByte()
val block: Block = buffer.connection.registries.blockRegistry[buffer.readVarInt()] val block: Block = buffer.readRegistryItem(buffer.connection.registries.blockRegistry)
override fun handle(connection: PlayConnection) { override fun handle(connection: PlayConnection) {
val blockEntity = connection.world.getOrPutBlockEntity(position) ?: return val blockEntity = connection.world.getOrPutBlockEntity(position) ?: return

View File

@ -33,7 +33,7 @@ class BlockDataS2CP(buffer: PlayInByteBuffer) : PlayS2CPacket {
buffer.readBlockPosition() buffer.readBlockPosition()
} }
val type = if (buffer.versionId >= V_21W37A) { val type = if (buffer.versionId >= V_21W37A) {
buffer.connection.registries.blockEntityTypeRegistry[buffer.readVarInt()].resourceLocation buffer.readRegistryItem(buffer.connection.registries.blockEntityTypeRegistry).resourceLocation
} else { } else {
buffer.connection.registries.blockDataTypeRegistry.getOrNull(buffer.readUnsignedByte())?.resourceLocation buffer.connection.registries.blockDataTypeRegistry.getOrNull(buffer.readUnsignedByte())?.resourceLocation
} }

View File

@ -27,7 +27,7 @@ import de.bixilon.minosoft.util.logging.LogMessageType
@LoadPacket @LoadPacket
class LegacyBlockBreakS2CP(buffer: PlayInByteBuffer) : PlayS2CPacket { class LegacyBlockBreakS2CP(buffer: PlayInByteBuffer) : PlayS2CPacket {
val blockPosition: Vec3i = buffer.readBlockPosition() val blockPosition: Vec3i = buffer.readBlockPosition()
val blockState: BlockState? = buffer.connection.registries.blockStateRegistry[buffer.readVarInt()] val blockState: BlockState? = buffer.connection.registries.blockStateRegistry.getOrNull(buffer.readVarInt())
val actions: Actions = Actions[buffer.readVarInt()] val actions: Actions = Actions[buffer.readVarInt()]
val successful: Boolean = buffer.readBoolean() val successful: Boolean = buffer.readBoolean()

View File

@ -41,10 +41,10 @@ class OpenContainerS2CP(buffer: PlayInByteBuffer) : PlayS2CPacket {
buffer.connection.registries.containerTypeRegistry[buffer.readUnsignedByte()] buffer.connection.registries.containerTypeRegistry[buffer.readUnsignedByte()]
} }
buffer.versionId >= V_1_14 -> { // ToDo: This is completely guessed buffer.versionId >= V_1_14 -> { // ToDo: This is completely guessed
buffer.connection.registries.containerTypeRegistry[buffer.readVarInt()] buffer.readRegistryItem(buffer.connection.registries.containerTypeRegistry)
} }
else -> { else -> {
buffer.connection.registries.containerTypeRegistry[buffer.readResourceLocation()]!! buffer.readLegacyRegistryItem(buffer.connection.registries.containerTypeRegistry)!!
} }
} }
val title: ChatComponent = buffer.readChatComponent() val title: ChatComponent = buffer.readChatComponent()

View File

@ -24,7 +24,7 @@ import de.bixilon.minosoft.util.logging.LogMessageType
@LoadPacket @LoadPacket
class EntityAnimationS2CP(buffer: PlayInByteBuffer) : PlayS2CPacket { class EntityAnimationS2CP(buffer: PlayInByteBuffer) : PlayS2CPacket {
val entityId: Int = buffer.readVarInt() val entityId: Int = buffer.readVarInt()
val animation: EntityAnimations = buffer.connection.registries.entityAnimationRegistry[buffer.readVarInt()]!! val animation: EntityAnimations = buffer.readEnum(buffer.connection.registries.entityAnimationRegistry)!!
override fun log(reducedLog: Boolean) { override fun log(reducedLog: Boolean) {

View File

@ -42,9 +42,9 @@ class EntityPaintingS2CP(buffer: PlayInByteBuffer) : PlayS2CPacket {
init { init {
val motif: Motif? = if (buffer.versionId < ProtocolVersions.V_18W02A) { val motif: Motif? = if (buffer.versionId < ProtocolVersions.V_18W02A) {
buffer.connection.registries.motifRegistry[buffer.readResourceLocation()] buffer.readLegacyRegistryItem(buffer.connection.registries.motifRegistry)
} else { } else {
buffer.connection.registries.motifRegistry[buffer.readVarInt()] buffer.readRegistryItem(buffer.connection.registries.motifRegistry)
} }
val position: Vec3i val position: Vec3i
val direction: Directions val direction: Directions

View File

@ -24,7 +24,7 @@ import de.bixilon.minosoft.util.logging.LogMessageType
@LoadPacket @LoadPacket
class EntitySoundS2CP(buffer: PlayInByteBuffer) : PlayS2CPacket { class EntitySoundS2CP(buffer: PlayInByteBuffer) : PlayS2CPacket {
val soundEvent: ResourceLocation = buffer.connection.registries.soundEventRegistry[buffer.readVarInt()]!! val soundEvent: ResourceLocation = buffer.readRegistryItem(buffer.connection.registries.soundEventRegistry)
val category: SoundCategories = SoundCategories[buffer.readVarInt()] val category: SoundCategories = SoundCategories[buffer.readVarInt()]
val entityId: Int = buffer.readVarInt() val entityId: Int = buffer.readVarInt()
val volume: Float = buffer.readFloat() val volume: Float = buffer.readFloat()

View File

@ -42,7 +42,7 @@ class SoundEventS2CP(buffer: PlayInByteBuffer) : PlayS2CPacket {
// category was moved to the top // category was moved to the top
this.category = SoundCategories[buffer.readVarInt()] this.category = SoundCategories[buffer.readVarInt()]
} }
soundEvent = buffer.connection.registries.soundEventRegistry[buffer.readVarInt()]!! soundEvent = buffer.readRegistryItem(buffer.connection.registries.soundEventRegistry)
if (buffer.versionId >= ProtocolVersions.V_17W15A && buffer.versionId < ProtocolVersions.V_17W18A) { if (buffer.versionId >= ProtocolVersions.V_17W15A && buffer.versionId < ProtocolVersions.V_17W18A) {
buffer.readString() // parrot entity type buffer.readString() // parrot entity type
} }

View File

@ -25,7 +25,7 @@ object TitleS2CF : PlayPacketFactory {
override val direction: PacketDirection = PacketDirection.SERVER_TO_CLIENT override val direction: PacketDirection = PacketDirection.SERVER_TO_CLIENT
override fun createPacket(buffer: PlayInByteBuffer): TitleS2CP { override fun createPacket(buffer: PlayInByteBuffer): TitleS2CP {
return when (buffer.connection.registries.titleActionsRegistry[buffer.readVarInt()]!!) { return when (buffer.readEnum(buffer.connection.registries.titleActionsRegistry)!!) {
TitleActions.TITLE_TEXT -> TitleTextS2CP(buffer) TitleActions.TITLE_TEXT -> TitleTextS2CP(buffer)
TitleActions.SUBTITLE -> SubtitleS2CP(buffer) TitleActions.SUBTITLE -> SubtitleS2CP(buffer)
TitleActions.HOTBAR_TEXT -> HotbarTextS2CP(buffer) TitleActions.HOTBAR_TEXT -> HotbarTextS2CP(buffer)

View File

@ -32,6 +32,10 @@ import de.bixilon.minosoft.data.registries.particle.data.BlockParticleData
import de.bixilon.minosoft.data.registries.particle.data.DustParticleData import de.bixilon.minosoft.data.registries.particle.data.DustParticleData
import de.bixilon.minosoft.data.registries.particle.data.ItemParticleData import de.bixilon.minosoft.data.registries.particle.data.ItemParticleData
import de.bixilon.minosoft.data.registries.particle.data.ParticleData import de.bixilon.minosoft.data.registries.particle.data.ParticleData
import de.bixilon.minosoft.data.registries.registries.registry.AbstractRegistry
import de.bixilon.minosoft.data.registries.registries.registry.EnumRegistry
import de.bixilon.minosoft.data.registries.registries.registry.Registry
import de.bixilon.minosoft.data.registries.registries.registry.RegistryItem
import de.bixilon.minosoft.data.text.ChatComponent import de.bixilon.minosoft.data.text.ChatComponent
import de.bixilon.minosoft.protocol.PlayerPublicKey import de.bixilon.minosoft.protocol.PlayerPublicKey
import de.bixilon.minosoft.protocol.network.connection.play.PlayConnection import de.bixilon.minosoft.protocol.network.connection.play.PlayConnection
@ -257,6 +261,7 @@ class PlayInByteBuffer : InByteBuffer {
} }
textures = PlayerTextures.of(value, signature) textures = PlayerTextures.of(value, signature)
} }
else -> Log.log(LogMessageType.NETWORK_PACKETS_IN, LogLevels.WARN) { "Unknown player property $name: $value" } else -> Log.log(LogMessageType.NETWORK_PACKETS_IN, LogLevels.WARN) { "Unknown player property $name: $value" }
} }
} }
@ -338,4 +343,16 @@ class PlayInByteBuffer : InByteBuffer {
BitSet.valueOf(readLongArray()) BitSet.valueOf(readLongArray())
} }
} }
fun <T> readRegistryItem(registry: AbstractRegistry<T>): T {
return registry[readVarInt()]
}
fun <T : RegistryItem> readLegacyRegistryItem(registry: Registry<T>): T? {
return registry[readResourceLocation()]
}
fun <T : Enum<*>> readEnum(registry: EnumRegistry<T>): T? {
return registry[readVarInt()]
}
} }