3
0
Mirror von https://github.com/ViaVersion/ViaVersion.git synchronisiert 2024-11-19 14:30:16 +01:00

Allow platforms to use the mapping system (#3754)

Dieser Commit ist enthalten in:
EnZaXD 2024-03-24 11:10:20 +01:00 committet von GitHub
Ursprung 18f04bf8ea
Commit 098f7ff3c2
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: B5690EEEBB952194
9 geänderte Dateien mit 63 neuen und 47 gelöschten Zeilen

Datei anzeigen

@ -70,7 +70,7 @@ public class MappingDataBase implements MappingData {
getLogger().info("Loading " + unmappedVersion + " -> " + mappedVersion + " mappings..."); 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"); blockMappings = loadMappings(data, "blocks");
blockStateMappings = loadMappings(data, "blockstates"); blockStateMappings = loadMappings(data, "blockstates");
blockEntityMappings = loadMappings(data, "blockentities"); blockEntityMappings = loadMappings(data, "blockentities");
@ -82,8 +82,8 @@ public class MappingDataBase implements MappingData {
attributeMappings = loadMappings(data, "attributes"); attributeMappings = loadMappings(data, "attributes");
itemMappings = loadBiMappings(data, "items"); itemMappings = loadBiMappings(data, "items");
final CompoundTag unmappedIdentifierData = MappingDataLoader.loadNBT("identifiers-" + unmappedVersion + ".nbt", true); final CompoundTag unmappedIdentifierData = readIdentifiersFile("identifiers-" + unmappedVersion + ".nbt");
final CompoundTag mappedIdentifierData = MappingDataLoader.loadNBT("identifiers-" + mappedVersion + ".nbt", true); final CompoundTag mappedIdentifierData = readIdentifiersFile("identifiers-" + mappedVersion + ".nbt");
if (unmappedIdentifierData != null && mappedIdentifierData != null) { if (unmappedIdentifierData != null && mappedIdentifierData != null) {
entityMappings = loadFullMappings(data, unmappedIdentifierData, mappedIdentifierData, "entities"); entityMappings = loadFullMappings(data, unmappedIdentifierData, mappedIdentifierData, "entities");
argumentTypeMappings = loadFullMappings(data, unmappedIdentifierData, mappedIdentifierData, "argumenttypes"); argumentTypeMappings = loadFullMappings(data, unmappedIdentifierData, mappedIdentifierData, "argumenttypes");
@ -114,16 +114,20 @@ public class MappingDataBase implements MappingData {
loadExtras(data); loadExtras(data);
} }
protected @Nullable CompoundTag readNBTFile(final String name) { protected @Nullable CompoundTag readMappingsFile(final String name) {
return MappingDataLoader.loadNBT(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) { 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) { 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) { protected @Nullable BiMappings loadBiMappings(final CompoundTag data, final String key) {

Datei anzeigen

@ -51,18 +51,28 @@ import java.util.Map;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import org.checkerframework.checker.nullness.qual.Nullable; import org.checkerframework.checker.nullness.qual.Nullable;
public final class MappingDataLoader { public class MappingDataLoader {
private static final Map<String, CompoundTag> MAPPINGS_CACHE = new HashMap<>(); public static final TagReader<CompoundTag> MAPPINGS_READER = NBTIO.reader(CompoundTag.class).named();
private static final TagReader<CompoundTag> MAPPINGS_READER = NBTIO.reader(CompoundTag.class).named();
private static final byte DIRECT_ID = 0; private static final byte DIRECT_ID = 0;
private static final byte SHIFTS_ID = 1; private static final byte SHIFTS_ID = 1;
private static final byte CHANGES_ID = 2; private static final byte CHANGES_ID = 2;
private static final byte IDENTITY_ID = 3; private static final byte IDENTITY_ID = 3;
private static boolean cacheValid = true;
public static void clearCache() { public static final MappingDataLoader INSTANCE = new MappingDataLoader(MappingDataLoader.class, "assets/viaversion/data/");
MAPPINGS_CACHE.clear();
private final Map<String, CompoundTag> 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; cacheValid = false;
} }
@ -71,8 +81,8 @@ public final class MappingDataLoader {
* *
* @return loaded json object, or null if not found or invalid * @return loaded json object, or null if not found or invalid
*/ */
public static @Nullable JsonObject loadFromDataDir(final String name) { public @Nullable JsonObject loadFromDataDir(final String name) {
final File file = new File(Via.getPlatform().getDataFolder(), name); final File file = getFile(name);
if (!file.exists()) { if (!file.exists()) {
return loadData(name); return loadData(name);
} }
@ -94,7 +104,7 @@ public final class MappingDataLoader {
* *
* @return loaded json object from bundled resources if present * @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); final InputStream stream = getResource(name);
if (stream == null) { if (stream == null) {
return 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) { if (!cacheValid) {
return loadNBTFromFile(name); return loadNBTFromFile(name);
} }
CompoundTag data = MAPPINGS_CACHE.get(name); CompoundTag data = mappingsCache.get(name);
if (data != null) { if (data != null) {
return data; return data;
} }
@ -120,16 +130,16 @@ public final class MappingDataLoader {
data = loadNBTFromFile(name); data = loadNBTFromFile(name);
if (cache && data != null) { if (cache && data != null) {
MAPPINGS_CACHE.put(name, data); mappingsCache.put(name, data);
} }
return data; return data;
} }
public static @Nullable CompoundTag loadNBT(final String name) { public @Nullable CompoundTag loadNBT(final String name) {
return loadNBT(name, false); return loadNBT(name, false);
} }
public static @Nullable CompoundTag loadNBTFromFile(final String name) { public @Nullable CompoundTag loadNBTFromFile(final String name) {
final InputStream resource = getResource(name); final InputStream resource = getResource(name);
if (resource == null) { if (resource == null) {
return 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 -> { return loadMappings(mappingsTag, key, size -> {
final int[] array = new int[size]; final int[] array = new int[size];
Arrays.fill(array, -1); Arrays.fill(array, -1);
@ -151,12 +161,12 @@ public final class MappingDataLoader {
} }
@Beta @Beta
public static <M extends Mappings, V> @Nullable Mappings loadMappings( public <M extends Mappings, V> @Nullable Mappings loadMappings(
final CompoundTag mappingsTag, final CompoundTag mappingsTag,
final String key, final String key,
final MappingHolderSupplier<V> holderSupplier, final MappingHolderSupplier<V> holderSupplier,
final AddConsumer<V> addConsumer, final AddConsumer<V> addConsumer,
final MappingsSupplier<M, V> mappingsSupplier final MappingsSupplier<M, V> mappingsSupplier
) { ) {
final CompoundTag tag = mappingsTag.getCompoundTag(key); final CompoundTag tag = mappingsTag.getCompoundTag(key);
if (tag == null) { if (tag == null) {
@ -227,7 +237,7 @@ public final class MappingDataLoader {
return mappingsSupplier.create(mappings, mappedSizeTag.asInt()); 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<StringTag> unmappedElements = unmappedIdentifiers.getListTag(key, StringTag.class); final ListTag<StringTag> unmappedElements = unmappedIdentifiers.getListTag(key, StringTag.class);
final ListTag<StringTag> mappedElements = mappedIdentifiers.getListTag(key, StringTag.class); final ListTag<StringTag> mappedElements = mappedIdentifiers.getListTag(key, StringTag.class);
if (unmappedElements == null || mappedElements == null) { if (unmappedElements == null || mappedElements == null) {
@ -240,9 +250,9 @@ public final class MappingDataLoader {
} }
return new FullMappingsBase( return new FullMappingsBase(
unmappedElements.stream().map(StringTag::getValue).collect(Collectors.toList()), unmappedElements.stream().map(StringTag::getValue).collect(Collectors.toList()),
mappedElements.stream().map(StringTag::getValue).collect(Collectors.toList()), mappedElements.stream().map(StringTag::getValue).collect(Collectors.toList()),
mappings mappings
); );
} }
@ -252,7 +262,7 @@ public final class MappingDataLoader {
* @param object json object * @param object json object
* @return map with indexes hashed by their id value * @return map with indexes hashed by their id value
*/ */
public static Object2IntMap<String> indexedObjectToMap(final JsonObject object) { public Object2IntMap<String> indexedObjectToMap(final JsonObject object) {
final Object2IntMap<String> map = new Object2IntOpenHashMap<>(object.size(), .99F); final Object2IntMap<String> map = new Object2IntOpenHashMap<>(object.size(), .99F);
map.defaultReturnValue(-1); map.defaultReturnValue(-1);
for (final Map.Entry<String, JsonElement> entry : object.entrySet()) { for (final Map.Entry<String, JsonElement> entry : object.entrySet()) {
@ -267,7 +277,7 @@ public final class MappingDataLoader {
* @param array json array * @param array json array
* @return map with indexes hashed by their id value * @return map with indexes hashed by their id value
*/ */
public static Object2IntMap<String> arrayToMap(final JsonArray array) { public Object2IntMap<String> arrayToMap(final JsonArray array) {
final Object2IntMap<String> map = new Object2IntOpenHashMap<>(array.size(), .99F); final Object2IntMap<String> map = new Object2IntOpenHashMap<>(array.size(), .99F);
map.defaultReturnValue(-1); map.defaultReturnValue(-1);
for (int i = 0; i < array.size(); i++) { for (int i = 0; i < array.size(); i++) {
@ -276,8 +286,12 @@ public final class MappingDataLoader {
return map; return map;
} }
public static @Nullable InputStream getResource(final String name) { public @Nullable InputStream getResource(final String name) {
return MappingDataLoader.class.getClassLoader().getResourceAsStream("assets/viaversion/data/" + name); return dataLoaderClass.getClassLoader().getResourceAsStream(dataPath + name);
}
public File getFile(final String name) {
return new File(Via.getPlatform().getDataFolder(), name);
} }
@FunctionalInterface @FunctionalInterface

Datei anzeigen

@ -526,7 +526,7 @@ public class ProtocolManagerImpl implements ProtocolManager {
mappingLoaderFutures = null; mappingLoaderFutures = null;
// Clear cached mapping files // Clear cached mapping files
MappingDataLoader.clearCache(); MappingDataLoader.INSTANCE.clearCache();
} }
private Function<Throwable, Void> mappingLoaderThrowable(Class<? extends Protocol> protocolClass) { private Function<Throwable, Void> mappingLoaderThrowable(Class<? extends Protocol> protocolClass) {

Datei anzeigen

@ -167,7 +167,7 @@ public final class ConnectionData {
} }
Via.getPlatform().getLogger().info("Loading block connection mappings ..."); Via.getPlatform().getLogger().info("Loading block connection mappings ...");
ListTag<StringTag> blockStates = MappingDataLoader.loadNBT("blockstates-1.13.nbt").getListTag("blockstates", StringTag.class); ListTag<StringTag> blockStates = MappingDataLoader.INSTANCE.loadNBT("blockstates-1.13.nbt").getListTag("blockstates", StringTag.class);
for (int id = 0; id < blockStates.size(); id++) { for (int id = 0; id < blockStates.size(); id++) {
String key = blockStates.get(id).getValue(); String key = blockStates.get(id).getValue();
KEY_TO_ID.put(key, id); KEY_TO_ID.put(key, id);
@ -178,7 +178,7 @@ public final class ConnectionData {
if (!Via.getConfig().isReduceBlockStorageMemory()) { if (!Via.getConfig().isReduceBlockStorageMemory()) {
blockConnectionData = new Int2ObjectOpenHashMap<>(2048); blockConnectionData = new Int2ObjectOpenHashMap<>(2048);
ListTag<CompoundTag> blockConnectionMappings = MappingDataLoader.loadNBT("blockConnections.nbt").getListTag("data", CompoundTag.class); ListTag<CompoundTag> blockConnectionMappings = MappingDataLoader.INSTANCE.loadNBT("blockConnections.nbt").getListTag("data", CompoundTag.class);
for (CompoundTag blockTag : blockConnectionMappings) { for (CompoundTag blockTag : blockConnectionMappings) {
BlockData blockData = new BlockData(); BlockData blockData = new BlockData();
for (Entry<String, Tag> entry : blockTag.entrySet()) { for (Entry<String, Tag> entry : blockTag.entrySet()) {

Datei anzeigen

@ -81,7 +81,7 @@ public class MappingData extends MappingDataBase {
blockMappings.setNewId(1557, 3986); // chiseled stone bricks 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) { if (object != null) {
for (Map.Entry<String, JsonElement> entry : object.entrySet()) { for (Map.Entry<String, JsonElement> entry : object.entrySet()) {
String oldChannel = entry.getKey(); String oldChannel = entry.getKey();
@ -145,7 +145,7 @@ public class MappingData extends MappingDataBase {
protected @Nullable BiMappings loadBiMappings(final CompoundTag data, final String key) { protected @Nullable BiMappings loadBiMappings(final CompoundTag data, final String key) {
// Special cursed case // Special cursed case
if (key.equals("items")) { 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); final Int2IntBiHashMap map = new Int2IntBiHashMap(size);
map.defaultReturnValue(-1); map.defaultReturnValue(-1);
return map; return map;

Datei anzeigen

@ -35,7 +35,7 @@ public class MappingData extends MappingDataBase {
@Override @Override
public void loadExtras(final CompoundTag data) { 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"); final IntArrayTag motionBlocking = heightmap.getIntArrayTag("motionBlocking");
this.motionBlocking = new IntOpenHashSet(motionBlocking.getValue()); this.motionBlocking = new IntOpenHashSet(motionBlocking.getValue());

Datei anzeigen

@ -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.CompoundTag;
import com.github.steveice10.opennbt.tag.builtin.ListTag; 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.MappingDataBase;
import com.viaversion.viaversion.api.data.MappingDataLoader; import com.viaversion.viaversion.api.data.MappingDataLoader;
import java.util.HashMap; import java.util.HashMap;
@ -35,7 +34,7 @@ public class MappingData extends MappingDataBase {
@Override @Override
public void loadExtras(final CompoundTag data) { 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 // Data of each dimension
final ListTag<CompoundTag> dimensions = dimensionRegistry.getCompoundTag("minecraft:dimension_type").getListTag("value", CompoundTag.class); final ListTag<CompoundTag> dimensions = dimensionRegistry.getCompoundTag("minecraft:dimension_type").getListTag("value", CompoundTag.class);

Datei anzeigen

@ -31,7 +31,7 @@ public final class MappingData extends MappingDataBase {
@Override @Override
protected void loadExtras(final CompoundTag data) { 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() { public CompoundTag damageTypesRegistry() {

Datei anzeigen

@ -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.CompoundTag;
import com.github.steveice10.opennbt.tag.builtin.ListTag; import com.github.steveice10.opennbt.tag.builtin.ListTag;
import com.github.steveice10.opennbt.tag.builtin.NumberTag; 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.MappingDataBase;
import com.viaversion.viaversion.api.data.MappingDataLoader; import com.viaversion.viaversion.api.data.MappingDataLoader;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap; import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
@ -37,7 +36,7 @@ public final class MappingData extends MappingDataBase {
@Override @Override
protected void loadExtras(final CompoundTag daata) { protected void loadExtras(final CompoundTag daata) {
final ListTag<CompoundTag> chatTypes = MappingDataLoader.loadNBTFromFile("chat-types-1.19.nbt").getListTag("values", CompoundTag.class); final ListTag<CompoundTag> chatTypes = MappingDataLoader.INSTANCE.loadNBTFromFile("chat-types-1.19.nbt").getListTag("values", CompoundTag.class);
for (final CompoundTag chatType : chatTypes) { for (final CompoundTag chatType : chatTypes) {
final NumberTag idTag = chatType.getNumberTag("id"); final NumberTag idTag = chatType.getNumberTag("id");
defaultChatTypes.put(idTag.asInt(), chatType); defaultChatTypes.put(idTag.asInt(), chatType);