More fixes to mapping data loading

This commit is contained in:
Nassim Jahnke 2023-03-04 17:17:47 +01:00
parent b94c71b699
commit 712bbcdc02
No known key found for this signature in database
GPG Key ID: 6BE3B555EBC5982B
25 changed files with 131 additions and 16 deletions

View File

@ -27,9 +27,12 @@ import org.checkerframework.checker.nullness.qual.Nullable;
/**
* Mappings containing the full string identifier mappings.
*/
public interface FullMappings {
public interface FullMappings extends Mappings {
Mappings mappings();
@Deprecated
default Mappings mappings() {
return this;
}
/**
* Returns the unmapped integer id for the given identifier, or -1 if not found.
@ -70,4 +73,6 @@ public interface FullMappings {
* @return mapped string identifier, or null if not found
*/
@Nullable String mappedIdentifier(String identifier);
FullMappings createInverse();
}

View File

@ -44,6 +44,14 @@ public class FullMappingsBase implements FullMappings {
this. mappedIdToString = mappedIdentifiers.toArray(EMPTY_ARRAY);
}
private FullMappingsBase(final Object2IntMap<String> stringToId, final Object2IntMap<String> mappedStringToId, final String[] idToString, final String[] mappedIdToString, final Mappings mappings) {
this.stringToId = stringToId;
this.mappedStringToId = mappedStringToId;
this.idToString = idToString;
this.mappedIdToString = mappedIdToString;
this.mappings = mappings;
}
@Override
public Mappings mappings() {
return mappings;
@ -82,6 +90,31 @@ public class FullMappingsBase implements FullMappings {
return mappedId != -1 ? mappedIdentifier(mappedId) : null;
}
@Override
public int getNewId(int id) {
return mappings.getNewId(id);
}
@Override
public void setNewId(int id, int mappedId) {
mappings.setNewId(id, mappedId);
}
@Override
public int size() {
return mappings.size();
}
@Override
public int mappedSize() {
return mappings.mappedSize();
}
@Override
public FullMappings createInverse() {
return new FullMappingsBase(mappedStringToId, stringToId, mappedIdToString, idToString, mappings.createInverse());
}
private static Object2IntMap<String> toInverseMap(final List<String> list) {
final Object2IntMap<String> map = new Object2IntOpenHashMap<>(list.size());
map.defaultReturnValue(-1);

View File

@ -51,4 +51,9 @@ public class IdentityMappings implements Mappings {
public int mappedSize() {
return mappedSize;
}
@Override
public Mappings createInverse() {
return new IdentityMappings(mappedSize, size);
}
}

View File

@ -22,12 +22,14 @@
*/
package com.viaversion.viaversion.api.data;
import com.viaversion.viaversion.util.Int2IntBiHashMap;
import com.viaversion.viaversion.util.Int2IntBiMap;
import it.unimi.dsi.fastutil.ints.Int2IntMap;
public class Int2IntMapBiMappings implements BiMappings {
private final Int2IntBiMap mappings;
private final BiMappings inverse;
private final Int2IntMapBiMappings inverse;
protected Int2IntMapBiMappings(final Int2IntBiMap mappings) {
this.mappings = mappings;
@ -35,7 +37,7 @@ public class Int2IntMapBiMappings implements BiMappings {
mappings.defaultReturnValue(-1);
}
private Int2IntMapBiMappings(final Int2IntBiMap mappings, final BiMappings inverse) {
private Int2IntMapBiMappings(final Int2IntBiMap mappings, final Int2IntMapBiMappings inverse) {
this.mappings = mappings;
this.inverse = inverse;
}
@ -68,4 +70,13 @@ public class Int2IntMapBiMappings implements BiMappings {
public int mappedSize() {
return mappings.inverse().size();
}
@Override
public Mappings createInverse() {
final Int2IntBiMap inverseCopy = new Int2IntBiHashMap(inverse.mappings.size());
for (final Int2IntMap.Entry entry : inverse.mappings.int2IntEntrySet()) {
inverseCopy.put(entry.getIntKey(), entry.getIntValue());
}
return of(inverseCopy);
}
}

View File

@ -62,4 +62,13 @@ public class Int2IntMapMappings implements Mappings {
public int mappedSize() {
return mappedIds;
}
@Override
public Mappings createInverse() {
final Int2IntMap inverse = new Int2IntOpenHashMap();
for (final Int2IntMap.Entry entry : mappings.int2IntEntrySet()) {
inverse.put(entry.getIntValue(), entry.getIntKey());
}
return of(inverse, size());
}
}

View File

@ -84,4 +84,9 @@ public class IntArrayBiMappings implements BiMappings {
public int mappedSize() {
return mappings.mappedSize();
}
@Override
public Mappings createInverse() {
return mappings.createInverse();
}
}

View File

@ -22,6 +22,8 @@
*/
package com.viaversion.viaversion.api.data;
import java.util.Arrays;
public class IntArrayMappings implements Mappings {
private final int[] mappings;
private final int mappedIds;
@ -59,6 +61,17 @@ public class IntArrayMappings implements Mappings {
return mappedIds;
}
@Override
public Mappings createInverse() {
final int[] inverse = new int[mappedIds];
Arrays.fill(inverse, -1);
for (int id = 0; id < mappings.length; id++) {
final int mappedId = mappings[id];
inverse[mappedId] = id;
}
return of(inverse, mappings.length);
}
public int[] raw() {
return mappings;
}

View File

@ -65,7 +65,7 @@ public class MappingDataBase implements MappingData {
getLogger().info("Loading " + unmappedVersion + " -> " + mappedVersion + " mappings...");
}
final CompoundTag data = MappingDataLoader.loadNBT("mappings-" + unmappedVersion + "to" + mappedVersion + ".nbt");
final CompoundTag data = readNBTFile("mappings-" + unmappedVersion + "to" + mappedVersion + ".nbt");
blockMappings = loadMappings(data, "blocks");
blockStateMappings = loadMappings(data, "blockstates");
blockEntityMappings = loadMappings(data, "blockentities");
@ -106,15 +106,23 @@ public class MappingDataBase implements MappingData {
loadExtras(data);
}
protected @Nullable CompoundTag readNBTFile(final String name) {
return MappingDataLoader.loadNBT(name);
}
protected @Nullable Mappings loadMappings(final CompoundTag data, final String key) {
return MappingDataLoader.loadMappings(data, key);
return shouldLoad(key) ? MappingDataLoader.loadMappings(data, key) : null;
}
protected @Nullable FullMappings loadFullMappings(final CompoundTag data, final CompoundTag unmappedIdentifiers, final CompoundTag mappedIdentifiers, final String key) {
return MappingDataLoader.loadFullMappings(data, unmappedIdentifiers, mappedIdentifiers, key);
return shouldLoad(key) ? MappingDataLoader.loadFullMappings(data, unmappedIdentifiers, mappedIdentifiers, key) : null;
}
protected @Nullable BiMappings loadBiMappings(final CompoundTag data, final String key) {
if (!shouldLoad(key)) {
return null;
}
final Mappings mappings = loadMappings(data, key);
return mappings != null ? IntArrayBiMappings.of((IntArrayMappings) mappings) : null;
}
@ -156,7 +164,7 @@ public class MappingDataBase implements MappingData {
@Override
public int getNewParticleId(final int id) {
return checkValidity(id, particleMappings.mappings().getNewId(id), "particles");
return checkValidity(id, particleMappings.getNewId(id), "particles");
}
@Override
@ -239,6 +247,10 @@ public class MappingDataBase implements MappingData {
return mappedId;
}
protected boolean shouldLoad(final String key) {
return true;
}
protected void loadExtras(final CompoundTag data) {
}
}

View File

@ -43,6 +43,7 @@ import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Map;
import java.util.stream.Collectors;
import org.checkerframework.checker.nullness.qual.Nullable;
@ -109,7 +110,11 @@ public final class MappingDataLoader {
}
public static @Nullable Mappings loadMappings(final CompoundTag mappingsTag, final String key) {
return loadMappings(mappingsTag, key, int[]::new, (array, id, mappedId) -> array[id] = mappedId, IntArrayMappings::of);
return loadMappings(mappingsTag, key, size -> {
final int[] array = new int[size];
Arrays.fill(array, -1);
return array;
}, (array, id, mappedId) -> array[id] = mappedId, IntArrayMappings::of);
}
@Beta
@ -134,10 +139,10 @@ public final class MappingDataLoader {
return IntArrayMappings.of(valuesTag.getValue(), mappedSizeTag.asInt());
} else if (strategy == SHIFTS_ID) {
final IntArrayTag shiftsAtTag = tag.get("at");
final IntArrayTag shiftsTag = tag.get("val");
final IntArrayTag shiftsTag = tag.get("to");
final IntTag sizeTag = tag.get("size");
final int[] shiftsAt = shiftsAtTag.getValue();
final int[] shifts = shiftsTag.getValue();
final int[] shiftsTo = shiftsTag.getValue();
final int size = sizeTag.asInt();
mappings = holderSupplier.get(size);
@ -153,9 +158,9 @@ public final class MappingDataLoader {
for (int i = 0; i < shiftsAt.length; i++) {
final int from = shiftsAt[i];
final int to = i == shiftsAt.length - 1 ? size : shiftsAt[i + 1];
final int shiftBy = shifts[i];
int mappedId = shiftsTo[i];
for (int id = from; id < to; id++) {
addConsumer.addTo(mappings, id, id + shiftBy);
addConsumer.addTo(mappings, id, mappedId++);
}
}
} else if (strategy == CHANGES_ID) {

View File

@ -83,6 +83,13 @@ public interface Mappings {
*/
int mappedSize();
/**
* Creates a new Mappings instance of the same type with the same mappings, but with the keys and values swapped.
*
* @return a new Mappings instance with the same mappings, but with the keys and values swapped
*/
Mappings createInverse();
static <T extends Mappings> Builder<T> builder(final MappingsSupplier<T> supplier) {
return new Builder<>(supplier);
}

View File

@ -24,6 +24,7 @@ package com.viaversion.viaversion.api.data.entity;
import com.viaversion.viaversion.api.data.BiMappings;
import com.viaversion.viaversion.api.data.IdentityMappings;
import com.viaversion.viaversion.api.data.Mappings;
public final class IdentityBiMappings extends IdentityMappings implements BiMappings {
@ -35,4 +36,9 @@ public final class IdentityBiMappings extends IdentityMappings implements BiMapp
public BiMappings inverse() {
return this;
}
@Override
public Mappings createInverse() {
return new IdentityBiMappings(mappedSize(), size());
}
}

View File

@ -146,7 +146,11 @@ 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", Int2IntBiHashMap::new, Int2IntBiHashMap::put, (v, mappedSize) -> Int2IntMapBiMappings.of(v));
return (BiMappings) MappingDataLoader.loadMappings(data, "items", size -> {
final Int2IntBiHashMap map = new Int2IntBiHashMap(size);
map.defaultReturnValue(-1);
return map;
}, Int2IntBiHashMap::put, (v, mappedSize) -> Int2IntMapBiMappings.of(v));
} else {
return super.loadBiMappings(data, key);
}

View File

@ -228,7 +228,7 @@ public abstract class EntityRewriter<C extends ClientboundPacketType, T extends
public void mapTypes() {
Preconditions.checkArgument(typeMappings == null, "Type mappings have already been set - manual type mappings should be set *after* this");
Preconditions.checkNotNull(protocol.getMappingData().getEntityMappings(), "Protocol does not have entity mappings");
typeMappings = protocol.getMappingData().getEntityMappings().mappings();
typeMappings = protocol.getMappingData().getEntityMappings();
}
/**

View File

@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.0.1-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.0.2-bin.zip
networkTimeout=10000
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists