From 098f7ff3c24097ab55316b2a0845ead85dda69ce Mon Sep 17 00:00:00 2001 From: EnZaXD <60033407+FlorianMichael@users.noreply.github.com> Date: Sun, 24 Mar 2024 11:10:20 +0100 Subject: [PATCH] Allow platforms to use the mapping system (#3754) --- .../viaversion/api/data/MappingDataBase.java | 18 +++-- .../api/data/MappingDataLoader.java | 72 +++++++++++-------- .../protocol/ProtocolManagerImpl.java | 2 +- .../blockconnections/ConnectionData.java | 4 +- .../data/MappingData.java | 4 +- .../data/MappingData.java | 2 +- .../data/MappingData.java | 3 +- .../data/MappingData.java | 2 +- .../data/MappingData.java | 3 +- 9 files changed, 63 insertions(+), 47 deletions(-) diff --git a/api/src/main/java/com/viaversion/viaversion/api/data/MappingDataBase.java b/api/src/main/java/com/viaversion/viaversion/api/data/MappingDataBase.java index 5d5202696..74daf6002 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/data/MappingDataBase.java +++ b/api/src/main/java/com/viaversion/viaversion/api/data/MappingDataBase.java @@ -70,7 +70,7 @@ public class MappingDataBase implements MappingData { getLogger().info("Loading " + unmappedVersion + " -> " + mappedVersion + " mappings..."); } - final CompoundTag data = readNBTFile("mappings-" + unmappedVersion + "to" + mappedVersion + ".nbt"); + final CompoundTag data = readMappingsFile("mappings-" + unmappedVersion + "to" + mappedVersion + ".nbt"); blockMappings = loadMappings(data, "blocks"); blockStateMappings = loadMappings(data, "blockstates"); blockEntityMappings = loadMappings(data, "blockentities"); @@ -82,8 +82,8 @@ public class MappingDataBase implements MappingData { attributeMappings = loadMappings(data, "attributes"); itemMappings = loadBiMappings(data, "items"); - final CompoundTag unmappedIdentifierData = MappingDataLoader.loadNBT("identifiers-" + unmappedVersion + ".nbt", true); - final CompoundTag mappedIdentifierData = MappingDataLoader.loadNBT("identifiers-" + mappedVersion + ".nbt", true); + final CompoundTag unmappedIdentifierData = readIdentifiersFile("identifiers-" + unmappedVersion + ".nbt"); + final CompoundTag mappedIdentifierData = readIdentifiersFile("identifiers-" + mappedVersion + ".nbt"); if (unmappedIdentifierData != null && mappedIdentifierData != null) { entityMappings = loadFullMappings(data, unmappedIdentifierData, mappedIdentifierData, "entities"); argumentTypeMappings = loadFullMappings(data, unmappedIdentifierData, mappedIdentifierData, "argumenttypes"); @@ -114,16 +114,20 @@ public class MappingDataBase implements MappingData { loadExtras(data); } - protected @Nullable CompoundTag readNBTFile(final String name) { - return MappingDataLoader.loadNBT(name); + protected @Nullable CompoundTag readMappingsFile(final String name) { + return MappingDataLoader.INSTANCE.loadNBT(name); + } + + protected @Nullable CompoundTag readIdentifiersFile(final String name) { + return MappingDataLoader.INSTANCE.loadNBT(name, true); } protected @Nullable Mappings loadMappings(final CompoundTag data, final String key) { - return MappingDataLoader.loadMappings(data, key); + return MappingDataLoader.INSTANCE.loadMappings(data, key); } protected @Nullable FullMappings loadFullMappings(final CompoundTag data, final CompoundTag unmappedIdentifiers, final CompoundTag mappedIdentifiers, final String key) { - return MappingDataLoader.loadFullMappings(data, unmappedIdentifiers, mappedIdentifiers, key); + return MappingDataLoader.INSTANCE.loadFullMappings(data, unmappedIdentifiers, mappedIdentifiers, key); } protected @Nullable BiMappings loadBiMappings(final CompoundTag data, final String key) { diff --git a/api/src/main/java/com/viaversion/viaversion/api/data/MappingDataLoader.java b/api/src/main/java/com/viaversion/viaversion/api/data/MappingDataLoader.java index eb99ff7e4..05b76c6d1 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/data/MappingDataLoader.java +++ b/api/src/main/java/com/viaversion/viaversion/api/data/MappingDataLoader.java @@ -51,18 +51,28 @@ import java.util.Map; import java.util.stream.Collectors; import org.checkerframework.checker.nullness.qual.Nullable; -public final class MappingDataLoader { +public class MappingDataLoader { - private static final Map MAPPINGS_CACHE = new HashMap<>(); - private static final TagReader MAPPINGS_READER = NBTIO.reader(CompoundTag.class).named(); + public static final TagReader MAPPINGS_READER = NBTIO.reader(CompoundTag.class).named(); private static final byte DIRECT_ID = 0; private static final byte SHIFTS_ID = 1; private static final byte CHANGES_ID = 2; private static final byte IDENTITY_ID = 3; - private static boolean cacheValid = true; - public static void clearCache() { - MAPPINGS_CACHE.clear(); + public static final MappingDataLoader INSTANCE = new MappingDataLoader(MappingDataLoader.class, "assets/viaversion/data/"); + + private final Map mappingsCache = new HashMap<>(); + private final Class dataLoaderClass; + private final String dataPath; + private boolean cacheValid = true; + + public MappingDataLoader(final Class dataLoaderClass, final String dataPath) { + this.dataLoaderClass = dataLoaderClass; + this.dataPath = dataPath; + } + + public void clearCache() { + mappingsCache.clear(); cacheValid = false; } @@ -71,8 +81,8 @@ public final class MappingDataLoader { * * @return loaded json object, or null if not found or invalid */ - public static @Nullable JsonObject loadFromDataDir(final String name) { - final File file = new File(Via.getPlatform().getDataFolder(), name); + public @Nullable JsonObject loadFromDataDir(final String name) { + final File file = getFile(name); if (!file.exists()) { return loadData(name); } @@ -94,7 +104,7 @@ public final class MappingDataLoader { * * @return loaded json object from bundled resources if present */ - public static @Nullable JsonObject loadData(final String name) { + public @Nullable JsonObject loadData(final String name) { final InputStream stream = getResource(name); if (stream == null) { return null; @@ -107,12 +117,12 @@ public final class MappingDataLoader { } } - public static @Nullable CompoundTag loadNBT(final String name, final boolean cache) { + public @Nullable CompoundTag loadNBT(final String name, final boolean cache) { if (!cacheValid) { return loadNBTFromFile(name); } - CompoundTag data = MAPPINGS_CACHE.get(name); + CompoundTag data = mappingsCache.get(name); if (data != null) { return data; } @@ -120,16 +130,16 @@ public final class MappingDataLoader { data = loadNBTFromFile(name); if (cache && data != null) { - MAPPINGS_CACHE.put(name, data); + mappingsCache.put(name, data); } return data; } - public static @Nullable CompoundTag loadNBT(final String name) { + public @Nullable CompoundTag loadNBT(final String name) { return loadNBT(name, false); } - public static @Nullable CompoundTag loadNBTFromFile(final String name) { + public @Nullable CompoundTag loadNBTFromFile(final String name) { final InputStream resource = getResource(name); if (resource == null) { return null; @@ -142,7 +152,7 @@ public final class MappingDataLoader { } } - public static @Nullable Mappings loadMappings(final CompoundTag mappingsTag, final String key) { + public @Nullable Mappings loadMappings(final CompoundTag mappingsTag, final String key) { return loadMappings(mappingsTag, key, size -> { final int[] array = new int[size]; Arrays.fill(array, -1); @@ -151,12 +161,12 @@ public final class MappingDataLoader { } @Beta - public static @Nullable Mappings loadMappings( - final CompoundTag mappingsTag, - final String key, - final MappingHolderSupplier holderSupplier, - final AddConsumer addConsumer, - final MappingsSupplier mappingsSupplier + public @Nullable Mappings loadMappings( + final CompoundTag mappingsTag, + final String key, + final MappingHolderSupplier holderSupplier, + final AddConsumer addConsumer, + final MappingsSupplier mappingsSupplier ) { final CompoundTag tag = mappingsTag.getCompoundTag(key); if (tag == null) { @@ -227,7 +237,7 @@ public final class MappingDataLoader { return mappingsSupplier.create(mappings, mappedSizeTag.asInt()); } - public static FullMappings loadFullMappings(final CompoundTag mappingsTag, final CompoundTag unmappedIdentifiers, final CompoundTag mappedIdentifiers, final String key) { + public FullMappings loadFullMappings(final CompoundTag mappingsTag, final CompoundTag unmappedIdentifiers, final CompoundTag mappedIdentifiers, final String key) { final ListTag unmappedElements = unmappedIdentifiers.getListTag(key, StringTag.class); final ListTag mappedElements = mappedIdentifiers.getListTag(key, StringTag.class); if (unmappedElements == null || mappedElements == null) { @@ -240,9 +250,9 @@ public final class MappingDataLoader { } return new FullMappingsBase( - unmappedElements.stream().map(StringTag::getValue).collect(Collectors.toList()), - mappedElements.stream().map(StringTag::getValue).collect(Collectors.toList()), - mappings + unmappedElements.stream().map(StringTag::getValue).collect(Collectors.toList()), + mappedElements.stream().map(StringTag::getValue).collect(Collectors.toList()), + mappings ); } @@ -252,7 +262,7 @@ public final class MappingDataLoader { * @param object json object * @return map with indexes hashed by their id value */ - public static Object2IntMap indexedObjectToMap(final JsonObject object) { + public Object2IntMap indexedObjectToMap(final JsonObject object) { final Object2IntMap map = new Object2IntOpenHashMap<>(object.size(), .99F); map.defaultReturnValue(-1); for (final Map.Entry entry : object.entrySet()) { @@ -267,7 +277,7 @@ public final class MappingDataLoader { * @param array json array * @return map with indexes hashed by their id value */ - public static Object2IntMap arrayToMap(final JsonArray array) { + public Object2IntMap arrayToMap(final JsonArray array) { final Object2IntMap map = new Object2IntOpenHashMap<>(array.size(), .99F); map.defaultReturnValue(-1); for (int i = 0; i < array.size(); i++) { @@ -276,8 +286,12 @@ public final class MappingDataLoader { return map; } - public static @Nullable InputStream getResource(final String name) { - return MappingDataLoader.class.getClassLoader().getResourceAsStream("assets/viaversion/data/" + name); + public @Nullable InputStream getResource(final String name) { + return dataLoaderClass.getClassLoader().getResourceAsStream(dataPath + name); + } + + public File getFile(final String name) { + return new File(Via.getPlatform().getDataFolder(), name); } @FunctionalInterface diff --git a/common/src/main/java/com/viaversion/viaversion/protocol/ProtocolManagerImpl.java b/common/src/main/java/com/viaversion/viaversion/protocol/ProtocolManagerImpl.java index d8b59310c..e11d464c8 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocol/ProtocolManagerImpl.java +++ b/common/src/main/java/com/viaversion/viaversion/protocol/ProtocolManagerImpl.java @@ -526,7 +526,7 @@ public class ProtocolManagerImpl implements ProtocolManager { mappingLoaderFutures = null; // Clear cached mapping files - MappingDataLoader.clearCache(); + MappingDataLoader.INSTANCE.clearCache(); } private Function mappingLoaderThrowable(Class protocolClass) { diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/blockconnections/ConnectionData.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/blockconnections/ConnectionData.java index ea4c80711..c4f800bee 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/blockconnections/ConnectionData.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/blockconnections/ConnectionData.java @@ -167,7 +167,7 @@ public final class ConnectionData { } Via.getPlatform().getLogger().info("Loading block connection mappings ..."); - ListTag blockStates = MappingDataLoader.loadNBT("blockstates-1.13.nbt").getListTag("blockstates", StringTag.class); + ListTag blockStates = MappingDataLoader.INSTANCE.loadNBT("blockstates-1.13.nbt").getListTag("blockstates", StringTag.class); for (int id = 0; id < blockStates.size(); id++) { String key = blockStates.get(id).getValue(); KEY_TO_ID.put(key, id); @@ -178,7 +178,7 @@ public final class ConnectionData { if (!Via.getConfig().isReduceBlockStorageMemory()) { blockConnectionData = new Int2ObjectOpenHashMap<>(2048); - ListTag blockConnectionMappings = MappingDataLoader.loadNBT("blockConnections.nbt").getListTag("data", CompoundTag.class); + ListTag blockConnectionMappings = MappingDataLoader.INSTANCE.loadNBT("blockConnections.nbt").getListTag("data", CompoundTag.class); for (CompoundTag blockTag : blockConnectionMappings) { BlockData blockData = new BlockData(); for (Entry entry : blockTag.entrySet()) { diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/data/MappingData.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/data/MappingData.java index 4c96d655f..c5a654111 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/data/MappingData.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/data/MappingData.java @@ -81,7 +81,7 @@ public class MappingData extends MappingDataBase { blockMappings.setNewId(1557, 3986); // chiseled stone bricks } - JsonObject object = MappingDataLoader.loadFromDataDir("channelmappings-1.13.json"); + JsonObject object = MappingDataLoader.INSTANCE.loadFromDataDir("channelmappings-1.13.json"); if (object != null) { for (Map.Entry entry : object.entrySet()) { String oldChannel = entry.getKey(); @@ -145,7 +145,7 @@ public class MappingData extends MappingDataBase { protected @Nullable BiMappings loadBiMappings(final CompoundTag data, final String key) { // Special cursed case if (key.equals("items")) { - return (BiMappings) MappingDataLoader.loadMappings(data, "items", size -> { + return (BiMappings) MappingDataLoader.INSTANCE.loadMappings(data, "items", size -> { final Int2IntBiHashMap map = new Int2IntBiHashMap(size); map.defaultReturnValue(-1); return map; diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_14to1_13_2/data/MappingData.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_14to1_13_2/data/MappingData.java index 1dd8b7461..4e6329663 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_14to1_13_2/data/MappingData.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_14to1_13_2/data/MappingData.java @@ -35,7 +35,7 @@ public class MappingData extends MappingDataBase { @Override public void loadExtras(final CompoundTag data) { - final CompoundTag heightmap = MappingDataLoader.loadNBT("heightmap-1.14.nbt"); + final CompoundTag heightmap = MappingDataLoader.INSTANCE.loadNBT("heightmap-1.14.nbt"); final IntArrayTag motionBlocking = heightmap.getIntArrayTag("motionBlocking"); this.motionBlocking = new IntOpenHashSet(motionBlocking.getValue()); diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16_2to1_16_1/data/MappingData.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16_2to1_16_1/data/MappingData.java index 882c7178b..583ac36ff 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16_2to1_16_1/data/MappingData.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16_2to1_16_1/data/MappingData.java @@ -19,7 +19,6 @@ package com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.data; import com.github.steveice10.opennbt.tag.builtin.CompoundTag; import com.github.steveice10.opennbt.tag.builtin.ListTag; -import com.github.steveice10.opennbt.tag.builtin.Tag; import com.viaversion.viaversion.api.data.MappingDataBase; import com.viaversion.viaversion.api.data.MappingDataLoader; import java.util.HashMap; @@ -35,7 +34,7 @@ public class MappingData extends MappingDataBase { @Override public void loadExtras(final CompoundTag data) { - dimensionRegistry = MappingDataLoader.loadNBTFromFile("dimension-registry-1.16.2.nbt"); + dimensionRegistry = MappingDataLoader.INSTANCE.loadNBTFromFile("dimension-registry-1.16.2.nbt"); // Data of each dimension final ListTag dimensions = dimensionRegistry.getCompoundTag("minecraft:dimension_type").getListTag("value", CompoundTag.class); diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_19_4to1_19_3/data/MappingData.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_19_4to1_19_3/data/MappingData.java index d0214747c..3cf93cfed 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_19_4to1_19_3/data/MappingData.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_19_4to1_19_3/data/MappingData.java @@ -31,7 +31,7 @@ public final class MappingData extends MappingDataBase { @Override protected void loadExtras(final CompoundTag data) { - damageTypesRegistry = MappingDataLoader.loadNBTFromFile("damage-types-1.19.4.nbt"); + damageTypesRegistry = MappingDataLoader.INSTANCE.loadNBTFromFile("damage-types-1.19.4.nbt"); } public CompoundTag damageTypesRegistry() { diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_19to1_18_2/data/MappingData.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_19to1_18_2/data/MappingData.java index 269c0f281..38cf16e2c 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_19to1_18_2/data/MappingData.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_19to1_18_2/data/MappingData.java @@ -20,7 +20,6 @@ package com.viaversion.viaversion.protocols.protocol1_19to1_18_2.data; import com.github.steveice10.opennbt.tag.builtin.CompoundTag; import com.github.steveice10.opennbt.tag.builtin.ListTag; import com.github.steveice10.opennbt.tag.builtin.NumberTag; -import com.github.steveice10.opennbt.tag.builtin.Tag; import com.viaversion.viaversion.api.data.MappingDataBase; import com.viaversion.viaversion.api.data.MappingDataLoader; import it.unimi.dsi.fastutil.ints.Int2ObjectMap; @@ -37,7 +36,7 @@ public final class MappingData extends MappingDataBase { @Override protected void loadExtras(final CompoundTag daata) { - final ListTag chatTypes = MappingDataLoader.loadNBTFromFile("chat-types-1.19.nbt").getListTag("values", CompoundTag.class); + final ListTag chatTypes = MappingDataLoader.INSTANCE.loadNBTFromFile("chat-types-1.19.nbt").getListTag("values", CompoundTag.class); for (final CompoundTag chatType : chatTypes) { final NumberTag idTag = chatType.getNumberTag("id"); defaultChatTypes.put(idTag.asInt(), chatType);