diff --git a/src/integration-test/kotlin/de/bixilon/minosoft/MinosoftSIT.kt b/src/integration-test/kotlin/de/bixilon/minosoft/MinosoftSIT.kt index 7befb7361..142694f52 100644 --- a/src/integration-test/kotlin/de/bixilon/minosoft/MinosoftSIT.kt +++ b/src/integration-test/kotlin/de/bixilon/minosoft/MinosoftSIT.kt @@ -1,6 +1,6 @@ /* * Minosoft - * Copyright (C) 2020-2022 Moritz Zwerger + * Copyright (C) 2020-2023 Moritz Zwerger * * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. * @@ -76,6 +76,8 @@ internal object MinosoftSIT { } fun loadPixlyzerData() { - IT.VERSION = ITUtil.loadPixlyzerData(IT.TEST_VERSION_NAME) + val (version, registries) = ITUtil.loadPixlyzerData(IT.TEST_VERSION_NAME) + IT.VERSION = version + IT.REGISTRIES = registries } } diff --git a/src/integration-test/kotlin/de/bixilon/minosoft/data/container/ContainerTestUtil.kt b/src/integration-test/kotlin/de/bixilon/minosoft/data/container/ContainerTestUtil.kt index 6b1dc37eb..a50526577 100644 --- a/src/integration-test/kotlin/de/bixilon/minosoft/data/container/ContainerTestUtil.kt +++ b/src/integration-test/kotlin/de/bixilon/minosoft/data/container/ContainerTestUtil.kt @@ -28,8 +28,8 @@ import de.bixilon.minosoft.util.KUtil.minosoft object ContainerTestUtil { private val container = ContainerType(minosoft("test"), factory = GenericContainerFactory) - private val chest = IT.VERSION.registries!!.containerTypeRegistry[Generic9x3Container]!! - private val furnace = IT.VERSION.registries!!.containerTypeRegistry[FurnaceContainer]!! + private val chest = IT.REGISTRIES.containerTypeRegistry[Generic9x3Container]!! + private val furnace = IT.REGISTRIES.containerTypeRegistry[FurnaceContainer]!! init { diff --git a/src/integration-test/kotlin/de/bixilon/minosoft/data/registries/VersionRegistry.kt b/src/integration-test/kotlin/de/bixilon/minosoft/data/registries/VersionRegistry.kt new file mode 100644 index 000000000..ed037421e --- /dev/null +++ b/src/integration-test/kotlin/de/bixilon/minosoft/data/registries/VersionRegistry.kt @@ -0,0 +1,22 @@ +/* + * Minosoft + * Copyright (C) 2020-2023 Moritz Zwerger + * + * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with this program. If not, see . + * + * This software is not affiliated with Mojang AB, the original developer of Minecraft. + */ + +package de.bixilon.minosoft.data.registries + +import de.bixilon.minosoft.data.registries.registries.Registries +import de.bixilon.minosoft.protocol.versions.Version + +data class VersionRegistry( + val version: Version, + val registries: Registries, +) diff --git a/src/integration-test/kotlin/de/bixilon/minosoft/data/registries/blocks/BlockTest.kt b/src/integration-test/kotlin/de/bixilon/minosoft/data/registries/blocks/BlockTest.kt index c6f146d38..fd5e7e745 100644 --- a/src/integration-test/kotlin/de/bixilon/minosoft/data/registries/blocks/BlockTest.kt +++ b/src/integration-test/kotlin/de/bixilon/minosoft/data/registries/blocks/BlockTest.kt @@ -32,7 +32,7 @@ abstract class BlockTest { } fun retrieveBlock(name: ResourceLocation) { - val block = IT.VERSION.registries!!.blockRegistry[name] + val block = IT.REGISTRIES.blockRegistry[name] Assert.assertNotNull(block) block!! assertEquals(block.identifier, name) diff --git a/src/integration-test/kotlin/de/bixilon/minosoft/data/registries/integrated/IntegratedRegistryIT.kt b/src/integration-test/kotlin/de/bixilon/minosoft/data/registries/integrated/IntegratedRegistryIT.kt index 341d2dfe3..48c4dae02 100644 --- a/src/integration-test/kotlin/de/bixilon/minosoft/data/registries/integrated/IntegratedRegistryIT.kt +++ b/src/integration-test/kotlin/de/bixilon/minosoft/data/registries/integrated/IntegratedRegistryIT.kt @@ -1,6 +1,6 @@ /* * Minosoft - * Copyright (C) 2020-2022 Moritz Zwerger + * Copyright (C) 2020-2023 Moritz Zwerger * * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. * @@ -25,12 +25,12 @@ class IntegratedRegistryIT { fun integratedSharpness() { val expected = WeaponEnchantment.Sharpness - val current = IT.VERSION.registries!!.enchantmentRegistry["minecraft:sharpness".toResourceLocation()] + val current = IT.REGISTRIES.enchantmentRegistry["minecraft:sharpness".toResourceLocation()] Assert.assertSame(current, expected) } fun goldenApple() { - val current = IT.VERSION.registries!!.itemRegistry["minecraft:golden_apple".toResourceLocation()] + val current = IT.REGISTRIES.itemRegistry["minecraft:golden_apple".toResourceLocation()] if (current !is AppleItem.GoldenAppleItem) { Assert.fail("Not an golden apple: $current") } diff --git a/src/integration-test/kotlin/de/bixilon/minosoft/data/registries/items/ItemTest.kt b/src/integration-test/kotlin/de/bixilon/minosoft/data/registries/items/ItemTest.kt index f6316e289..0cf53377e 100644 --- a/src/integration-test/kotlin/de/bixilon/minosoft/data/registries/items/ItemTest.kt +++ b/src/integration-test/kotlin/de/bixilon/minosoft/data/registries/items/ItemTest.kt @@ -31,7 +31,7 @@ abstract class ItemTest { } fun retrieveItem(name: ResourceLocation) { - val item = IT.VERSION.registries!!.itemRegistry[name] + val item = IT.REGISTRIES.itemRegistry[name] Assert.assertNotNull(item) item!! assertEquals(item.identifier, name) @@ -39,7 +39,7 @@ abstract class ItemTest { } fun retrieveItem(factory: ItemFactory) { - val item = IT.VERSION.registries!!.itemRegistry[factory] + val item = IT.REGISTRIES.itemRegistry[factory] Assert.assertNotNull(item) item!! assertEquals(item.identifier, factory.identifier) diff --git a/src/integration-test/kotlin/de/bixilon/minosoft/data/registries/versions/PixLyzerLoadingTest.kt b/src/integration-test/kotlin/de/bixilon/minosoft/data/registries/versions/PixLyzerLoadingTest.kt index 7ae0c0077..d4347b462 100644 --- a/src/integration-test/kotlin/de/bixilon/minosoft/data/registries/versions/PixLyzerLoadingTest.kt +++ b/src/integration-test/kotlin/de/bixilon/minosoft/data/registries/versions/PixLyzerLoadingTest.kt @@ -1,6 +1,6 @@ /* * Minosoft - * Copyright (C) 2020-2022 Moritz Zwerger + * Copyright (C) 2020-2023 Moritz Zwerger * * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. * @@ -14,10 +14,10 @@ package de.bixilon.minosoft.data.registries.versions import de.bixilon.minosoft.data.entities.entities.monster.Zombie +import de.bixilon.minosoft.data.registries.VersionRegistry import de.bixilon.minosoft.data.registries.blocks.MinecraftBlocks import de.bixilon.minosoft.data.registries.item.MinecraftItems import de.bixilon.minosoft.protocol.protocol.VersionSupport -import de.bixilon.minosoft.protocol.versions.Version import de.bixilon.minosoft.protocol.versions.Versions import de.bixilon.minosoft.test.ITUtil import org.testng.Assert @@ -26,10 +26,8 @@ import org.testng.annotations.Test @Test(groups = ["pixlyzer"], dependsOnGroups = ["version"], singleThreaded = false, threadPoolSize = 8) class PixLyzerLoadingTest { - private fun Version.test() { + private fun VersionRegistry.test() { val registries = this.registries - Assert.assertNotNull(this.registries) - registries!! Assert.assertNotNull(registries.blockRegistry[MinecraftBlocks.DIRT]) Assert.assertNotNull(registries.itemRegistry[MinecraftItems.COAL]) Assert.assertNotNull(registries.entityTypeRegistry[Zombie]) @@ -102,7 +100,7 @@ class PixLyzerLoadingTest { fun latest() { val version = Versions.getById(VersionSupport.LATEST_VERSION)!! println("Latest version $version") - ITUtil.loadPixlyzerData(version) - version.test() + val registries = ITUtil.loadPixlyzerData(version) + VersionRegistry(version, registries).test() } } diff --git a/src/integration-test/kotlin/de/bixilon/minosoft/protocol/network/connection/play/ConnectionTestUtil.kt b/src/integration-test/kotlin/de/bixilon/minosoft/protocol/network/connection/play/ConnectionTestUtil.kt index bc30239fa..97d8a03e9 100644 --- a/src/integration-test/kotlin/de/bixilon/minosoft/protocol/network/connection/play/ConnectionTestUtil.kt +++ b/src/integration-test/kotlin/de/bixilon/minosoft/protocol/network/connection/play/ConnectionTestUtil.kt @@ -1,6 +1,6 @@ /* * Minosoft - * Copyright (C) 2020-2022 Moritz Zwerger + * Copyright (C) 2020-2023 Moritz Zwerger * * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. * @@ -39,7 +39,7 @@ object ConnectionTestUtil { connection::account.forceSet(OfflineAccount("dummy")) connection::version.forceSet(IT.VERSION) connection::registries.forceSet(Registries()) - connection.registries.parentRegistries = IT.VERSION.registries + connection.registries.parent = IT.REGISTRIES connection::world.forceSet(createWorld(connection)) connection::player.forceSet(LocalPlayerEntity(connection.account, connection, null)) connection::network.forceSet(TestNetwork()) diff --git a/src/integration-test/kotlin/de/bixilon/minosoft/test/IT.kt b/src/integration-test/kotlin/de/bixilon/minosoft/test/IT.kt index b7746c35f..1a5555e83 100644 --- a/src/integration-test/kotlin/de/bixilon/minosoft/test/IT.kt +++ b/src/integration-test/kotlin/de/bixilon/minosoft/test/IT.kt @@ -1,6 +1,6 @@ /* * Minosoft - * Copyright (C) 2020-2022 Moritz Zwerger + * Copyright (C) 2020-2023 Moritz Zwerger * * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. * @@ -14,6 +14,7 @@ package de.bixilon.minosoft.test import de.bixilon.kutil.cast.CastUtil.unsafeNull +import de.bixilon.minosoft.data.registries.registries.Registries import de.bixilon.minosoft.protocol.versions.Version import org.objenesis.ObjenesisStd @@ -21,6 +22,7 @@ object IT { val OBJENESIS = ObjenesisStd() const val TEST_VERSION_NAME = "1.18.2" var VERSION: Version = unsafeNull() + var REGISTRIES: Registries = unsafeNull() val references: MutableList = mutableListOf() diff --git a/src/integration-test/kotlin/de/bixilon/minosoft/test/ITUtil.kt b/src/integration-test/kotlin/de/bixilon/minosoft/test/ITUtil.kt index e36174a0b..423c82f17 100644 --- a/src/integration-test/kotlin/de/bixilon/minosoft/test/ITUtil.kt +++ b/src/integration-test/kotlin/de/bixilon/minosoft/test/ITUtil.kt @@ -1,6 +1,6 @@ /* * Minosoft - * Copyright (C) 2020-2022 Moritz Zwerger + * Copyright (C) 2020-2023 Moritz Zwerger * * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. * @@ -15,6 +15,8 @@ package de.bixilon.minosoft.test import de.bixilon.kutil.latch.CountUpAndDownLatch import de.bixilon.minosoft.config.profile.profiles.resources.ResourcesProfile +import de.bixilon.minosoft.data.registries.VersionRegistry +import de.bixilon.minosoft.data.registries.registries.Registries import de.bixilon.minosoft.protocol.versions.Version import de.bixilon.minosoft.protocol.versions.Versions @@ -25,18 +27,13 @@ object ITUtil { return ResourcesProfile() } - fun loadPixlyzerData(name: String): Version { + fun loadPixlyzerData(name: String): VersionRegistry { val version = Versions[name]!! - if (version.registries != null) { - // already loaded - return version - } - - loadPixlyzerData(version) - return version + val registries = loadPixlyzerData(version) + return VersionRegistry(version, registries) } - fun loadPixlyzerData(version: Version) { - version.load(profile, CountUpAndDownLatch(0)) + fun loadPixlyzerData(version: Version): Registries { + return version.load(profile, CountUpAndDownLatch(0)) } } diff --git a/src/main/java/de/bixilon/minosoft/data/registries/registries/Registries.kt b/src/main/java/de/bixilon/minosoft/data/registries/registries/Registries.kt index dbe873eb7..7dcb25644 100644 --- a/src/main/java/de/bixilon/minosoft/data/registries/registries/Registries.kt +++ b/src/main/java/de/bixilon/minosoft/data/registries/registries/Registries.kt @@ -1,6 +1,6 @@ /* * Minosoft - * Copyright (C) 2020-2022 Moritz Zwerger + * Copyright (C) 2020-2023 Moritz Zwerger * * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. * @@ -75,7 +75,7 @@ import java.lang.reflect.Field import kotlin.reflect.jvm.javaField -class Registries { +class Registries : Parentable { val registries: MutableMap> = mutableMapOf() var shapes: Array = emptyArray() val motifRegistry: Registry = register("motif", Registry(codec = Motif)) @@ -135,17 +135,19 @@ class Registries { private var isFlattened = false - var parentRegistries: Registries? = null + override var parent: Registries? = null set(value) { field = value - for (parentableField in PARENTABLE_FIELDS) { - PARENTABLE_SET_PARENT_METHOD(parentableField.get(this), value?.let { parentableField.get(it) }) + for (field in PARENTABLE_FIELDS) { + val method = field.get(this) ?: continue + val value = value?.let { field.get(it) } + PARENTABLE_SET_PARENT_METHOD(method, value) } } fun getEntityDataIndex(field: EntityDataField): Int? { - return entityDataIndexMap[field] ?: parentRegistries?.getEntityDataIndex(field) + return entityDataIndexMap[field] ?: parent?.getEntityDataIndex(field) } fun load(version: Version, pixlyzerData: Map, latch: CountUpAndDownLatch) { diff --git a/src/main/java/de/bixilon/minosoft/protocol/network/connection/play/PlayConnection.kt b/src/main/java/de/bixilon/minosoft/protocol/network/connection/play/PlayConnection.kt index 25b45d6b7..027912672 100644 --- a/src/main/java/de/bixilon/minosoft/protocol/network/connection/play/PlayConnection.kt +++ b/src/main/java/de/bixilon/minosoft/protocol/network/connection/play/PlayConnection.kt @@ -183,8 +183,7 @@ class PlayConnection( val taskWorker = TaskWorker(errorHandler = { _, exception -> if (error == null) error = exception }, criticalErrorHandler = { _, exception -> if (error == null) error = exception }) taskWorker += { events.fire(RegistriesLoadEvent(this, registries, RegistriesLoadEvent.States.PRE)) - version.load(profiles.resources, latch) - registries.parentRegistries = version.registries + registries.parent = version.load(profiles.resources, latch) events.fire(RegistriesLoadEvent(this, registries, RegistriesLoadEvent.States.POST)) } @@ -230,7 +229,6 @@ class PlayConnection( state = PlayConnectionStates.ESTABLISHING } catch (exception: Throwable) { Log.log(LogMessageType.VERSION_LOADING, level = LogLevels.FATAL) { exception } - version.unload() if (this::assetsManager.isInitialized) { assetsManager.unload() } diff --git a/src/main/java/de/bixilon/minosoft/protocol/versions/Version.kt b/src/main/java/de/bixilon/minosoft/protocol/versions/Version.kt index 73b199258..9165bb973 100644 --- a/src/main/java/de/bixilon/minosoft/protocol/versions/Version.kt +++ b/src/main/java/de/bixilon/minosoft/protocol/versions/Version.kt @@ -1,6 +1,6 @@ /* * Minosoft - * Copyright (C) 2020-2022 Moritz Zwerger + * Copyright (C) 2020-2023 Moritz Zwerger * * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. * @@ -35,22 +35,10 @@ class Version( val c2sPackets: Map>, ) { val sortingId: Int = (versionId == -1).decide(Int.MAX_VALUE, versionId) - var registries: Registries? = null - private set - @Synchronized - fun load(profile: ResourcesProfile, latch: CountUpAndDownLatch) { - if (registries != null) { - // already loaded - return - } - registries = RegistriesLoader.load(profile, this, latch) - } - - @Synchronized - fun unload() { - this.registries = null + fun load(profile: ResourcesProfile, latch: CountUpAndDownLatch): Registries { + return RegistriesLoader.load(profile, this, latch) } override fun toString(): String {