Async mapping data loading

This commit is contained in:
KennyTV 2020-04-03 20:01:00 +02:00 committed by Nassim
parent ecceac660d
commit f263f757de
14 changed files with 78 additions and 43 deletions

View File

@ -14,9 +14,10 @@ import nl.matsv.viabackwards.api.ViaBackwardsPlatform;
import org.bukkit.plugin.java.JavaPlugin;
public class BukkitPlugin extends JavaPlugin implements ViaBackwardsPlatform {
@Override
public void onEnable() {
this.init(getDataFolder());
init(getDataFolder());
}
@Override

View File

@ -11,11 +11,21 @@
package nl.matsv.viabackwards.api;
import us.myles.ViaVersion.api.protocol.Protocol;
import us.myles.ViaVersion.api.protocol.ProtocolRegistry;
import us.myles.ViaVersion.api.remapper.PacketRemapper;
import us.myles.ViaVersion.packets.State;
import java.util.concurrent.CompletableFuture;
public abstract class BackwardsProtocol extends Protocol {
protected BackwardsProtocol() {
}
protected BackwardsProtocol(boolean hasMappingDataToLoad) {
super(hasMappingDataToLoad);
}
public void out(State state, int oldPacketID, int newPacketID) {
this.registerOutgoing(state, oldPacketID, newPacketID, null);
}
@ -31,4 +41,18 @@ public abstract class BackwardsProtocol extends Protocol {
public void in(State state, int oldPacketID, int newPacketID, PacketRemapper packetRemapper) {
this.registerIncoming(state, oldPacketID, newPacketID, packetRemapper);
}
/**
* Waits for the given protocol to be loaded to then asynchronously execute the runnable for this protocol.
*/
protected void executeAsyncAfterLoaded(Class<? extends Protocol> protocolClass, Runnable runnable) {
CompletableFuture<Void> future = ProtocolRegistry.getMappingLoaderFuture(protocolClass);
if (future == null) {
runnable.run();
return;
}
// If the protocol to depend on has been loaded, execute the new runnable async and schedule it for necessary completion
future.whenComplete((v, t) -> ProtocolRegistry.addMappingLoaderFuture(getClass(), runnable));
}
}

View File

@ -9,7 +9,6 @@ import us.myles.viaversion.libs.gson.*;
import java.io.*;
import java.util.Map;
//TODO merge data loading with VV's MappingDataLoader, diff is in data folder and data inputstream
public class VBMappingDataLoader {
public static JsonObject loadFromDataDir(String name) {

View File

@ -1,19 +1,28 @@
package nl.matsv.viabackwards.api.rewriters;
import nl.matsv.viabackwards.api.BackwardsProtocol;
import nl.matsv.viabackwards.api.data.VBSoundMappings;
import us.myles.ViaVersion.api.remapper.PacketRemapper;
import us.myles.ViaVersion.api.rewriters.IdRewriteFunction;
import us.myles.ViaVersion.api.type.Type;
import us.myles.ViaVersion.packets.State;
import java.util.function.Function;
public class SoundRewriter {
private final BackwardsProtocol protocol;
private final VBSoundMappings soundMappings;
// Can't hold the mappings instance here since it's loaded later
private final IdRewriteFunction idRewriter;
private final Function<String, String> stringIdRewriter;
public SoundRewriter(BackwardsProtocol protocol, VBSoundMappings soundMappings) {
public SoundRewriter(BackwardsProtocol protocol, IdRewriteFunction idRewriter, Function<String, String> stringIdRewriter) {
this.protocol = protocol;
this.soundMappings = soundMappings;
this.idRewriter = idRewriter;
this.stringIdRewriter = stringIdRewriter;
}
public SoundRewriter(BackwardsProtocol protocol, IdRewriteFunction idRewriter) {
this(protocol, idRewriter, null);
}
// The same for entity sound effect
@ -24,7 +33,7 @@ public class SoundRewriter {
map(Type.VAR_INT); // Sound Id
handler(wrapper -> {
int soundId = wrapper.get(Type.VAR_INT, 0);
int mappedId = soundMappings.getNewId(soundId);
int mappedId = idRewriter.rewrite(soundId);
if (mappedId != -1 && soundId != mappedId) {
wrapper.set(Type.VAR_INT, 0, mappedId);
}
@ -40,7 +49,7 @@ public class SoundRewriter {
map(Type.STRING); // Sound identifier
handler(wrapper -> {
String soundId = wrapper.get(Type.STRING, 0);
String mappedId = soundMappings.getNewId(soundId);
String mappedId = stringIdRewriter.apply(soundId);
if (mappedId == null) return;
if (!mappedId.isEmpty()) {
wrapper.set(Type.STRING, 0, mappedId);

View File

@ -27,23 +27,25 @@ import nl.matsv.viabackwards.protocol.protocol1_12_2to1_13.storage.BackwardsBloc
import nl.matsv.viabackwards.protocol.protocol1_12_2to1_13.storage.PlayerPositionStorage1_13;
import nl.matsv.viabackwards.protocol.protocol1_12_2to1_13.storage.TabCompleteStorage;
import us.myles.ViaVersion.api.PacketWrapper;
import us.myles.ViaVersion.api.Via;
import us.myles.ViaVersion.api.data.UserConnection;
import us.myles.ViaVersion.api.platform.providers.ViaProviders;
import us.myles.ViaVersion.api.remapper.PacketRemapper;
import us.myles.ViaVersion.packets.State;
import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.Protocol1_13To1_12_2;
import us.myles.ViaVersion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld;
public class Protocol1_12_2To1_13 extends BackwardsProtocol {
private BlockItemPackets1_13 blockItemPackets;
static {
BackwardsMappings.init();
PaintingMapping.init();
}
@Override
protected void registerPackets() {
executeAsyncAfterLoaded(Protocol1_13To1_12_2.class, () -> {
BackwardsMappings.init();
PaintingMapping.init();
Via.getManager().getProviders().register(BackwardsBlockEntityProvider.class, new BackwardsBlockEntityProvider());
});
(blockItemPackets = new BlockItemPackets1_13(this)).register();
new EntityPackets1_13(this).register();
new PlayerPacket1_13(this).register();
@ -182,11 +184,6 @@ public class Protocol1_12_2To1_13 extends BackwardsProtocol {
}
}
@Override
protected void register(ViaProviders providers) {
providers.register(BackwardsBlockEntityProvider.class, new BackwardsBlockEntityProvider());
}
public PacketRemapper cancel() {
return new PacketRemapper() {
@Override

View File

@ -41,7 +41,7 @@ public class PistonHandler implements BackwardsBlockEntityProvider.BackwardsBloc
addEntries(entry.getKey(), entry.getValue());
}
} else {
JsonObject mappings = MappingDataLoader.loadData("mapping-1.13.json").getAsJsonObject("blocks");
JsonObject mappings = MappingDataLoader.getMappingsCache().get("mapping-1.13.json").getAsJsonObject("blocks");
for (Map.Entry<String, JsonElement> blockState : mappings.entrySet()) {
String key = blockState.getValue().getAsString();
if (!key.contains("piston")) continue;

View File

@ -37,11 +37,11 @@ public class BackwardsMappings {
public static Mappings enchantmentMappings;
public static void init() {
JsonObject mapping1_12 = MappingDataLoader.loadData("mapping-1.12.json");
JsonObject mapping1_13 = MappingDataLoader.loadData("mapping-1.13.json");
ViaBackwards.getPlatform().getLogger().info("Loading 1.13 -> 1.12.2 mappings...");
JsonObject mapping1_12 = MappingDataLoader.getMappingsCache().get("mapping-1.12.json");
JsonObject mapping1_13 = MappingDataLoader.getMappingsCache().get("mapping-1.13.json");
JsonObject mapping1_12_2to1_13 = VBMappingDataLoader.loadFromDataDir("mapping-1.12.2to1.13.json");
ViaBackwards.getPlatform().getLogger().info("Loading 1.13 -> 1.12.2 mappings...");
blockMappings = new BlockMappingsShortArray(mapping1_13.getAsJsonObject("blocks"), mapping1_12.getAsJsonObject("blocks"), mapping1_12_2to1_13.getAsJsonObject("blockstates"));
itemMappings = new VBItemMappings(mapping1_13.getAsJsonObject("items"), mapping1_12.getAsJsonObject("items"), mapping1_12_2to1_13.getAsJsonObject("items"));
soundMappings = new VBSoundMappings(mapping1_13.getAsJsonArray("sounds"), mapping1_12.getAsJsonArray("sounds"), mapping1_12_2to1_13.getAsJsonObject("sounds"));

View File

@ -16,21 +16,19 @@ import us.myles.ViaVersion.api.remapper.PacketHandler;
import us.myles.ViaVersion.api.remapper.PacketRemapper;
import us.myles.ViaVersion.api.type.Type;
import us.myles.ViaVersion.packets.State;
import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.Protocol1_14To1_13_2;
import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.data.MappingData;
import us.myles.ViaVersion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld;
public class Protocol1_13_2To1_14 extends BackwardsProtocol {
private static final Integer[] A = new Integer[0];
private BlockItemPackets1_14 blockItemPackets;
private EntityPackets1_14 entityPackets;
static {
BackwardsMappings.init();
}
@Override
protected void registerPackets() {
executeAsyncAfterLoaded(Protocol1_14To1_13_2.class, BackwardsMappings::init);
blockItemPackets = new BlockItemPackets1_14(this);
blockItemPackets.register();
entityPackets = new EntityPackets1_14(this);

View File

@ -16,11 +16,11 @@ public class BackwardsMappings {
public static VBItemMappings itemMappings;
public static void init() {
JsonObject mapping1_13_2 = MappingDataLoader.loadData("mapping-1.13.2.json");
JsonObject mapping1_14 = MappingDataLoader.loadData("mapping-1.14.json");
ViaBackwards.getPlatform().getLogger().info("Loading 1.14 -> 1.13.2 mappings...");
JsonObject mapping1_13_2 = MappingDataLoader.getMappingsCache().get("mapping-1.13.2.json");
JsonObject mapping1_14 = MappingDataLoader.getMappingsCache().get("mapping-1.14.json");
JsonObject mapping1_13_2to1_14 = VBMappingDataLoader.loadFromDataDir("mapping-1.13.2to1.14.json");
ViaBackwards.getPlatform().getLogger().info("Loading 1.14 -> 1.13.2 mappings...");
blockStateMappings = new VBMappings(mapping1_14.getAsJsonObject("blockstates"), mapping1_13_2.getAsJsonObject("blockstates"), mapping1_13_2to1_14.getAsJsonObject("blockstates"));
blockMappings = new VBMappings(mapping1_14.getAsJsonObject("blocks"), mapping1_13_2.getAsJsonObject("blocks"), mapping1_13_2to1_14.getAsJsonObject("blocks"), false);
itemMappings = new VBItemMappings(mapping1_14.getAsJsonObject("items"), mapping1_13_2.getAsJsonObject("items"), mapping1_13_2to1_14.getAsJsonObject("items"));

View File

@ -20,7 +20,8 @@ public class SoundPackets1_14 extends Rewriter<Protocol1_13_2To1_14> {
@Override
protected void registerPackets() {
SoundRewriter soundRewriter = new SoundRewriter(protocol, BackwardsMappings.soundMappings);
SoundRewriter soundRewriter = new SoundRewriter(protocol,
id -> BackwardsMappings.soundMappings.getNewId(id), stringId -> BackwardsMappings.soundMappings.getNewId(stringId));
soundRewriter.registerSound(0x51, 0x4D);
soundRewriter.registerNamedSound(0x19, 0x1A);

View File

@ -16,6 +16,7 @@ import us.myles.ViaVersion.api.remapper.PacketHandler;
import us.myles.ViaVersion.api.remapper.PacketRemapper;
import us.myles.ViaVersion.api.type.Type;
import us.myles.ViaVersion.packets.State;
import us.myles.ViaVersion.protocols.protocol1_15to1_14_4.Protocol1_15To1_14_4;
import us.myles.ViaVersion.protocols.protocol1_15to1_14_4.data.MappingData;
import us.myles.ViaVersion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld;
@ -25,7 +26,8 @@ public class Protocol1_14_4To1_15 extends BackwardsProtocol {
@Override
protected void registerPackets() {
BackwardsMappings.init();
executeAsyncAfterLoaded(Protocol1_15To1_14_4.class, BackwardsMappings::init);
(blockItemPackets = new BlockItemPackets1_15(this)).register();
new EntityPackets1_15(this).register();
@ -39,7 +41,8 @@ public class Protocol1_14_4To1_15 extends BackwardsProtocol {
translatableRewriter.registerTitle(0x50, 0x4F);
translatableRewriter.registerPing();
SoundRewriter soundRewriter = new SoundRewriter(this, BackwardsMappings.soundMappings);
SoundRewriter soundRewriter = new SoundRewriter(this,
id -> BackwardsMappings.soundMappings.getNewId(id), stringId -> BackwardsMappings.soundMappings.getNewId(stringId));
soundRewriter.registerSound(0x52, 0x51);
soundRewriter.registerSound(0x51, 0x50);
soundRewriter.registerNamedSound(0x1A, 0x19);

View File

@ -16,11 +16,11 @@ public class BackwardsMappings {
public static VBItemMappings itemMappings;
public static void init() {
JsonObject mapping1_14 = MappingDataLoader.loadData("mapping-1.14.json");
JsonObject mapping1_15 = MappingDataLoader.loadData("mapping-1.15.json");
ViaBackwards.getPlatform().getLogger().info("Loading 1.15 -> 1.14.4 mappings...");
JsonObject mapping1_14 = MappingDataLoader.getMappingsCache().get("mapping-1.14.json");
JsonObject mapping1_15 = MappingDataLoader.getMappingsCache().get("mapping-1.15.json");
JsonObject mapping1_14to1_15 = VBMappingDataLoader.loadFromDataDir("mapping-1.14.4to1.15.json");
ViaBackwards.getPlatform().getLogger().info("Loading 1.15 -> 1.14.4 mappings...");
blockStateMappings = new VBMappings(mapping1_15.getAsJsonObject("blockstates"), mapping1_14.getAsJsonObject("blockstates"), mapping1_14to1_15.getAsJsonObject("blockstates"));
blockMappings = new VBMappings(mapping1_15.getAsJsonObject("blocks"), mapping1_14.getAsJsonObject("blocks"), mapping1_14to1_15.getAsJsonObject("blocks"), false);
itemMappings = new VBItemMappings(mapping1_15.getAsJsonObject("items"), mapping1_14.getAsJsonObject("items"), mapping1_14to1_15.getAsJsonObject("items"));

View File

@ -14,6 +14,7 @@ import us.myles.ViaVersion.api.remapper.PacketRemapper;
import us.myles.ViaVersion.api.rewriters.TagRewriter;
import us.myles.ViaVersion.api.type.Type;
import us.myles.ViaVersion.packets.State;
import us.myles.ViaVersion.protocols.protocol1_16to1_15_2.Protocol1_16To1_15_2;
import us.myles.ViaVersion.protocols.protocol1_16to1_15_2.data.MappingData;
import us.myles.ViaVersion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld;
@ -25,7 +26,8 @@ public class Protocol1_15_2To1_16 extends BackwardsProtocol {
@Override
protected void registerPackets() {
BackwardsMappings.init();
executeAsyncAfterLoaded(Protocol1_16To1_15_2.class, BackwardsMappings::init);
(blockItemPackets = new BlockItemPackets1_16(this)).register();
new EntityPackets1_16(this).register();
@ -39,7 +41,8 @@ public class Protocol1_15_2To1_16 extends BackwardsProtocol {
translatableRewriter.registerTitle(0x50, 0x50);
translatableRewriter.registerPing();
SoundRewriter soundRewriter = new SoundRewriter(this, BackwardsMappings.soundMappings);
SoundRewriter soundRewriter = new SoundRewriter(this,
id -> BackwardsMappings.soundMappings.getNewId(id), stringId -> BackwardsMappings.soundMappings.getNewId(stringId));
soundRewriter.registerSound(0x51, 0x51);
soundRewriter.registerSound(0x52, 0x52);
soundRewriter.registerNamedSound(0x1A, 0x1A);
@ -94,7 +97,7 @@ public class Protocol1_15_2To1_16 extends BackwardsProtocol {
});
// Tags
new TagRewriter(this, BackwardsMappings.blockMappings::getNewId, id -> {
new TagRewriter(this, id -> BackwardsMappings.blockMappings.getNewId(id), id -> {
Integer oldId = MappingData.oldToNewItems.inverse().get(id);
return oldId != null ? oldId : -1;
}, Protocol1_15_2To1_16::getNewEntityId).register(0x5C, 0x5C);

View File

@ -21,11 +21,11 @@ public class BackwardsMappings {
public static Map<String, String> attributeMappings = new HashMap<>();
public static void init() {
JsonObject mapping1_15 = MappingDataLoader.loadData("mapping-1.15.json");
JsonObject mapping1_16 = MappingDataLoader.loadData("mapping-1.16.json");
ViaBackwards.getPlatform().getLogger().info("Loading 1.16 -> 1.15.2 mappings...");
JsonObject mapping1_15 = MappingDataLoader.getMappingsCache().get("mapping-1.15.json");
JsonObject mapping1_16 = MappingDataLoader.getMappingsCache().get("mapping-1.16.json");
JsonObject mapping1_15to1_16 = VBMappingDataLoader.loadFromDataDir("mapping-1.15to1.16.json");
ViaBackwards.getPlatform().getLogger().info("Loading 1.16 -> 1.15.2 mappings...");
blockStateMappings = new VBMappings(mapping1_16.getAsJsonObject("blockstates"), mapping1_15.getAsJsonObject("blockstates"), mapping1_15to1_16.getAsJsonObject("blockstates"));
blockMappings = new VBMappings(mapping1_16.getAsJsonObject("blocks"), mapping1_15.getAsJsonObject("blocks"), mapping1_15to1_16.getAsJsonObject("blocks"), false);
itemMappings = new VBItemMappings(mapping1_16.getAsJsonObject("items"), mapping1_15.getAsJsonObject("items"), mapping1_15to1_16.getAsJsonObject("items"));