3
0
Mirror von https://github.com/ViaVersion/ViaBackwards.git synchronisiert 2024-12-26 08:10:10 +01:00

Make mapping loading considerably faster

Dieser Commit ist enthalten in:
KennyTV 2020-09-09 11:00:31 +02:00
Ursprung c6d8a83ac9
Commit b1af6ad721
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: 6BE3B555EBC5982B
3 geänderte Dateien mit 27 neuen und 75 gelöschten Zeilen

Datei anzeigen

@ -6,7 +6,7 @@ import us.myles.ViaVersion.api.data.MappingDataLoader;
import us.myles.ViaVersion.util.GsonUtil; import us.myles.ViaVersion.util.GsonUtil;
import us.myles.viaversion.libs.fastutil.ints.Int2ObjectMap; import us.myles.viaversion.libs.fastutil.ints.Int2ObjectMap;
import us.myles.viaversion.libs.fastutil.ints.Int2ObjectOpenHashMap; import us.myles.viaversion.libs.fastutil.ints.Int2ObjectOpenHashMap;
import us.myles.viaversion.libs.gson.JsonArray; import us.myles.viaversion.libs.fastutil.objects.Object2IntMap;
import us.myles.viaversion.libs.gson.JsonElement; import us.myles.viaversion.libs.gson.JsonElement;
import us.myles.viaversion.libs.gson.JsonIOException; import us.myles.viaversion.libs.gson.JsonIOException;
import us.myles.viaversion.libs.gson.JsonObject; import us.myles.viaversion.libs.gson.JsonObject;
@ -56,10 +56,11 @@ public class VBMappingDataLoader {
} }
public static void mapIdentifiers(short[] output, JsonObject oldIdentifiers, JsonObject newIdentifiers, JsonObject diffIdentifiers, boolean warnOnMissing) { public static void mapIdentifiers(short[] output, JsonObject oldIdentifiers, JsonObject newIdentifiers, JsonObject diffIdentifiers, boolean warnOnMissing) {
Object2IntMap newIdentifierMap = MappingDataLoader.indexedObjectToMap(newIdentifiers);
for (Map.Entry<String, JsonElement> entry : oldIdentifiers.entrySet()) { for (Map.Entry<String, JsonElement> entry : oldIdentifiers.entrySet()) {
String key = entry.getValue().getAsString(); String key = entry.getValue().getAsString();
Map.Entry<String, JsonElement> value = MappingDataLoader.findValue(newIdentifiers, key); int mappedId = newIdentifierMap.getInt(key);
if (value == null) { if (mappedId == -1) {
if (diffIdentifiers != null) { if (diffIdentifiers != null) {
// Search in diff mappings // Search in diff mappings
JsonPrimitive diffValueJson = diffIdentifiers.getAsJsonPrimitive(key); JsonPrimitive diffValueJson = diffIdentifiers.getAsJsonPrimitive(key);
@ -78,11 +79,11 @@ public class VBMappingDataLoader {
} }
if (diffValue != null) { if (diffValue != null) {
value = MappingDataLoader.findValue(newIdentifiers, diffValue); mappedId = newIdentifierMap.getInt(diffValue);
} }
} }
if (value == null) { if (mappedId == -1) {
// Nothing found :( // Nothing found :(
if (warnOnMissing && !Via.getConfig().isSuppressConversionWarnings() || Via.getManager().isDebug()) { if (warnOnMissing && !Via.getConfig().isSuppressConversionWarnings() || Via.getManager().isDebug()) {
ViaBackwards.getPlatform().getLogger().warning("No key for " + entry.getValue() + " :( "); ViaBackwards.getPlatform().getLogger().warning("No key for " + entry.getValue() + " :( ");
@ -91,39 +92,7 @@ public class VBMappingDataLoader {
} }
} }
output[Integer.parseInt(entry.getKey())] = Short.parseShort(value.getKey()); output[Integer.parseInt(entry.getKey())] = (short) mappedId;
}
}
public static void mapIdentifiers(short[] output, JsonArray oldIdentifiers, JsonArray newIdentifiers, JsonObject diffIdentifiers, boolean warnOnMissing) {
int i = -1;
for (JsonElement oldIdentifier : oldIdentifiers) {
i++;
String key = oldIdentifier.getAsString();
Integer index = MappingDataLoader.findIndex(newIdentifiers, key);
if (index == null) {
// Search in diff mappings
if (diffIdentifiers != null) {
JsonPrimitive diffValue = diffIdentifiers.getAsJsonPrimitive(key);
if (diffValue == null) {
if (warnOnMissing && !Via.getConfig().isSuppressConversionWarnings() || Via.getManager().isDebug()) {
ViaBackwards.getPlatform().getLogger().warning("No diff key for " + key + " :( ");
}
continue;
}
String mappedName = diffValue.getAsString();
if (mappedName.isEmpty()) continue; // "empty" remaps
index = MappingDataLoader.findIndex(newIdentifiers, mappedName);
}
if (index == null) {
if (warnOnMissing && !Via.getConfig().isSuppressConversionWarnings() || Via.getManager().isDebug()) {
ViaBackwards.getPlatform().getLogger().warning("No key for " + key + " :( ");
}
continue;
}
}
output[i] = index.shortValue();
} }
} }
@ -144,32 +113,32 @@ public class VBMappingDataLoader {
} }
public static Int2ObjectMap<MappedItem> loadItemMappings(JsonObject oldMapping, JsonObject newMapping, JsonObject diffMapping) { public static Int2ObjectMap<MappedItem> loadItemMappings(JsonObject oldMapping, JsonObject newMapping, JsonObject diffMapping) {
Map<Integer, MappedItem> itemMapping = new HashMap<>(); Int2ObjectMap<MappedItem> itemMapping = new Int2ObjectOpenHashMap<>(diffMapping.size(), 1F);
Object2IntMap newIdenfierMap = MappingDataLoader.indexedObjectToMap(newMapping);
Object2IntMap oldIdenfierMap = MappingDataLoader.indexedObjectToMap(oldMapping);
for (Map.Entry<String, JsonElement> entry : diffMapping.entrySet()) { for (Map.Entry<String, JsonElement> entry : diffMapping.entrySet()) {
JsonObject object = entry.getValue().getAsJsonObject(); JsonObject object = entry.getValue().getAsJsonObject();
String mappedIdName = object.getAsJsonPrimitive("id").getAsString(); String mappedIdName = object.getAsJsonPrimitive("id").getAsString();
Map.Entry<String, JsonElement> value = MappingDataLoader.findValue(newMapping, mappedIdName); int mappedId = newIdenfierMap.getInt(mappedIdName);
if (value == null) { if (mappedId == -1) {
if (!Via.getConfig().isSuppressConversionWarnings() || Via.getManager().isDebug()) { if (!Via.getConfig().isSuppressConversionWarnings() || Via.getManager().isDebug()) {
ViaBackwards.getPlatform().getLogger().warning("No key for " + mappedIdName + " :( "); ViaBackwards.getPlatform().getLogger().warning("No key for " + mappedIdName + " :( ");
} }
continue; continue;
} }
Map.Entry<String, JsonElement> oldEntry = MappingDataLoader.findValue(oldMapping, entry.getKey()); int oldId = oldIdenfierMap.getInt(entry.getKey());
if (oldEntry == null) { if (oldId == -1) {
if (!Via.getConfig().isSuppressConversionWarnings() || Via.getManager().isDebug()) { if (!Via.getConfig().isSuppressConversionWarnings() || Via.getManager().isDebug()) {
ViaBackwards.getPlatform().getLogger().warning("No old entry for " + mappedIdName + " :( "); ViaBackwards.getPlatform().getLogger().warning("No old entry for " + mappedIdName + " :( ");
} }
continue; continue;
} }
int id = Integer.parseInt(oldEntry.getKey());
int mappedId = Integer.parseInt(value.getKey());
String name = object.getAsJsonPrimitive("name").getAsString(); String name = object.getAsJsonPrimitive("name").getAsString();
itemMapping.put(id, new MappedItem(mappedId, name)); itemMapping.put(oldId, new MappedItem(mappedId, name));
} }
return new Int2ObjectOpenHashMap<>(itemMapping, 1F); return itemMapping;
} }
} }

Datei anzeigen

@ -8,18 +8,6 @@ import java.util.Arrays;
public class VBMappings extends Mappings { public class VBMappings extends Mappings {
public VBMappings(JsonObject oldMapping, JsonObject newMapping, boolean warnOnMissing) {
this(oldMapping, newMapping, null, warnOnMissing);
}
public VBMappings(int size, JsonObject oldMapping, JsonObject newMapping, JsonObject diffMapping) {
this(size, oldMapping, newMapping, diffMapping, true);
}
public VBMappings(JsonObject oldMapping, JsonObject newMapping, JsonObject diffMapping) {
this(oldMapping, newMapping, diffMapping, true);
}
public VBMappings(int size, JsonObject oldMapping, JsonObject newMapping, JsonObject diffMapping, boolean warnOnMissing) { public VBMappings(int size, JsonObject oldMapping, JsonObject newMapping, JsonObject diffMapping, boolean warnOnMissing) {
super(create(size, oldMapping, newMapping, diffMapping, warnOnMissing)); super(create(size, oldMapping, newMapping, diffMapping, warnOnMissing));
} }
@ -28,12 +16,12 @@ public class VBMappings extends Mappings {
super(create(oldMapping.entrySet().size(), oldMapping, newMapping, diffMapping, warnOnMissing)); super(create(oldMapping.entrySet().size(), oldMapping, newMapping, diffMapping, warnOnMissing));
} }
public VBMappings(JsonArray oldMapping, JsonArray newMapping, JsonObject diffMapping) { public VBMappings(JsonObject oldMapping, JsonObject newMapping, boolean warnOnMissing) {
super(create(oldMapping, newMapping, diffMapping, true)); this(oldMapping, newMapping, null, warnOnMissing);
} }
public VBMappings(JsonArray oldMapping, JsonArray newMapping, JsonObject diffMapping, boolean warnOnMissing) { public VBMappings(JsonArray oldMapping, JsonArray newMapping, JsonObject diffMapping, boolean warnOnMissing) {
super(create(oldMapping, newMapping, diffMapping, warnOnMissing)); super(oldMapping.size(), oldMapping, newMapping, diffMapping, warnOnMissing);
} }
private static short[] create(int size, JsonObject oldMapping, JsonObject newMapping, JsonObject diffMapping, boolean warnOnMissing) { private static short[] create(int size, JsonObject oldMapping, JsonObject newMapping, JsonObject diffMapping, boolean warnOnMissing) {
@ -42,11 +30,4 @@ public class VBMappings extends Mappings {
VBMappingDataLoader.mapIdentifiers(oldToNew, oldMapping, newMapping, diffMapping, warnOnMissing); VBMappingDataLoader.mapIdentifiers(oldToNew, oldMapping, newMapping, diffMapping, warnOnMissing);
return oldToNew; return oldToNew;
} }
private static short[] create(JsonArray oldMapping, JsonArray newMapping, JsonObject diffMapping, boolean warnOnMissing) {
short[] oldToNew = new short[oldMapping.size()];
Arrays.fill(oldToNew, (short) -1);
VBMappingDataLoader.mapIdentifiers(oldToNew, oldMapping, newMapping, diffMapping, warnOnMissing);
return oldToNew;
}
} }

Datei anzeigen

@ -20,6 +20,7 @@ import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.Protocol1_13To1_12_2;
import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.data.StatisticMappings; import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.data.StatisticMappings;
import us.myles.viaversion.libs.fastutil.ints.Int2ObjectMap; import us.myles.viaversion.libs.fastutil.ints.Int2ObjectMap;
import us.myles.viaversion.libs.fastutil.ints.Int2ObjectOpenHashMap; import us.myles.viaversion.libs.fastutil.ints.Int2ObjectOpenHashMap;
import us.myles.viaversion.libs.fastutil.objects.Object2IntMap;
import us.myles.viaversion.libs.gson.JsonElement; import us.myles.viaversion.libs.gson.JsonElement;
import us.myles.viaversion.libs.gson.JsonObject; import us.myles.viaversion.libs.gson.JsonObject;
import us.myles.viaversion.libs.gson.JsonPrimitive; import us.myles.viaversion.libs.gson.JsonPrimitive;
@ -50,11 +51,12 @@ public class BackwardsMappings extends nl.matsv.viabackwards.api.data.BackwardsM
// Has lots of compat layers, so we can't use the default Via method // Has lots of compat layers, so we can't use the default Via method
private static void mapIdentifiers(short[] output, JsonObject newIdentifiers, JsonObject oldIdentifiers, JsonObject mapping) { private static void mapIdentifiers(short[] output, JsonObject newIdentifiers, JsonObject oldIdentifiers, JsonObject mapping) {
Object2IntMap newIdentifierMap = MappingDataLoader.indexedObjectToMap(oldIdentifiers);
for (Map.Entry<String, JsonElement> entry : newIdentifiers.entrySet()) { for (Map.Entry<String, JsonElement> entry : newIdentifiers.entrySet()) {
String key = entry.getValue().getAsString(); String key = entry.getValue().getAsString();
Map.Entry<String, JsonElement> value = MappingDataLoader.findValue(oldIdentifiers, key); int value = newIdentifierMap.getInt(key);
short hardId = -1; short hardId = -1;
if (value == null) { if (value == -1) {
JsonPrimitive replacement = mapping.getAsJsonPrimitive(key); JsonPrimitive replacement = mapping.getAsJsonPrimitive(key);
int propertyIndex; int propertyIndex;
if (replacement == null && (propertyIndex = key.indexOf('[')) != -1) { if (replacement == null && (propertyIndex = key.indexOf('[')) != -1) {
@ -64,12 +66,12 @@ public class BackwardsMappings extends nl.matsv.viabackwards.api.data.BackwardsM
if (replacement.getAsString().startsWith("id:")) { if (replacement.getAsString().startsWith("id:")) {
String id = replacement.getAsString().replace("id:", ""); String id = replacement.getAsString().replace("id:", "");
hardId = Short.parseShort(id); hardId = Short.parseShort(id);
value = MappingDataLoader.findValue(oldIdentifiers, oldIdentifiers.getAsJsonPrimitive(id).getAsString()); value = newIdentifierMap.getInt(oldIdentifiers.getAsJsonPrimitive(id).getAsString());
} else { } else {
value = MappingDataLoader.findValue(oldIdentifiers, replacement.getAsString()); value = newIdentifierMap.getInt(replacement.getAsString());
} }
} }
if (value == null) { if (value == -1) {
if (!Via.getConfig().isSuppressConversionWarnings() || Via.getManager().isDebug()) { if (!Via.getConfig().isSuppressConversionWarnings() || Via.getManager().isDebug()) {
if (replacement != null) { if (replacement != null) {
ViaBackwards.getPlatform().getLogger().warning("No key for " + entry.getValue() + "/" + replacement.getAsString() + " :( "); ViaBackwards.getPlatform().getLogger().warning("No key for " + entry.getValue() + "/" + replacement.getAsString() + " :( ");
@ -80,7 +82,7 @@ public class BackwardsMappings extends nl.matsv.viabackwards.api.data.BackwardsM
continue; continue;
} }
} }
output[Integer.parseInt(entry.getKey())] = hardId != -1 ? hardId : Short.parseShort(value.getKey()); output[Integer.parseInt(entry.getKey())] = hardId != -1 ? hardId : (short) value;
} }
} }