Fix resource pack loading

This commit is contained in:
rtm516 2020-06-03 01:07:15 +01:00
parent ddd21aef25
commit f93b07491e
2 changed files with 21 additions and 12 deletions

View file

@ -86,30 +86,37 @@ public class UpstreamPacketHandler extends LoggingPacketHandler {
case SEND_PACKS: case SEND_PACKS:
for(String id : packet.getPackIds()) { for(String id : packet.getPackIds()) {
ResourcePackDataInfoPacket data = new ResourcePackDataInfoPacket(); ResourcePackDataInfoPacket data = new ResourcePackDataInfoPacket();
ResourcePack pack = ResourcePack.PACKS.get(id.split("_")[0]); String[] packID = id.split("_");
ResourcePack pack = ResourcePack.PACKS.get(packID[0]);
ResourcePackManifest.Header header = pack.getManifest().getHeader(); ResourcePackManifest.Header header = pack.getManifest().getHeader();
data.setPackId(header.getUuid()); data.setPackId(header.getUuid());
data.setChunkCount(pack.getFile().length()/ResourcePack.CHUNK_SIZE); int chunkCount = (int) Math.ceil((int) pack.getFile().length() / (double) ResourcePack.CHUNK_SIZE);
data.setChunkCount(chunkCount);
//data.setChunkCount(pack.getFile().length()/ResourcePack.CHUNK_SIZE);
data.setCompressedPackSize(pack.getFile().length()); data.setCompressedPackSize(pack.getFile().length());
data.setMaxChunkSize(ResourcePack.CHUNK_SIZE); data.setMaxChunkSize(ResourcePack.CHUNK_SIZE);
data.setHash(pack.getSha256()); data.setHash(pack.getSha256());
data.setPackVersion(packID[1]);
data.setPremium(false);
data.setType(ResourcePackDataInfoPacket.Type.RESOURCE);
session.getUpstream().sendPacket(data); session.sendUpstreamPacket(data);
} }
break; break;
case HAVE_ALL_PACKS: case HAVE_ALL_PACKS:
ResourcePackStackPacket stackPacket = new ResourcePackStackPacket(); ResourcePackStackPacket stackPacket = new ResourcePackStackPacket();
stackPacket.setExperimental(false); stackPacket.setExperimental(false);
stackPacket.setForcedToAccept(true); stackPacket.setForcedToAccept(true);
stackPacket.setGameVersion(GeyserConnector.BEDROCK_PACKET_CODEC.getMinecraftVersion()); stackPacket.setGameVersion(GeyserConnector.BEDROCK_PACKET_CODEC.getMinecraftVersion());
for(ResourcePack pack : ResourcePack.PACKS.values()) { for(ResourcePack pack : ResourcePack.PACKS.values()) {
ResourcePackManifest.Header header = pack.getManifest().getHeader(); ResourcePackManifest.Header header = pack.getManifest().getHeader();
String version = header.getVersion()[0] + "." + header.getVersion()[1] + "." + header.getVersion()[2]; String version = header.getVersion()[0] + "." + header.getVersion()[1] + "." + header.getVersion()[2];
stackPacket.getResourcePacks().add(new ResourcePackStackPacket.Entry(header.getUuid().toString(), version, "")); stackPacket.getResourcePacks().add(new ResourcePackStackPacket.Entry(header.getUuid().toString(), version, ""));
} }
session.sendUpstreamPacket(stackPacket); session.sendUpstreamPacket(stackPacket);
break; break;
@ -172,24 +179,26 @@ public class UpstreamPacketHandler extends LoggingPacketHandler {
@Override @Override
public boolean handle(ResourcePackChunkRequestPacket packet) { public boolean handle(ResourcePackChunkRequestPacket packet) {
ResourcePackChunkDataPacket data = new ResourcePackChunkDataPacket(); ResourcePackChunkDataPacket data = new ResourcePackChunkDataPacket();
ResourcePack pack = ResourcePack.PACKS.get(data.getPackId().toString()); ResourcePack pack = ResourcePack.PACKS.get(packet.getPackId().toString());
data.setChunkIndex(packet.getChunkIndex()); data.setChunkIndex(packet.getChunkIndex());
data.setProgress(packet.getChunkIndex()*ResourcePack.CHUNK_SIZE); data.setProgress(packet.getChunkIndex() * ResourcePack.CHUNK_SIZE);
data.setPackVersion(packet.getPackVersion()); data.setPackVersion(packet.getPackVersion());
data.setPackId(packet.getPackId()); data.setPackId(packet.getPackId());
byte[] packData = new byte[(int) MathUtils.constrain(pack.getFile().length(), 0, ResourcePack.CHUNK_SIZE)];
int offset = packet.getChunkIndex() * ResourcePack.CHUNK_SIZE;
byte[] packData = new byte[(int) MathUtils.constrain(pack.getFile().length() - offset, 0, ResourcePack.CHUNK_SIZE)];
try (InputStream inputStream = new FileInputStream(pack.getFile())) { try (InputStream inputStream = new FileInputStream(pack.getFile())) {
int offset = packet.getChunkIndex()*ResourcePack.CHUNK_SIZE; inputStream.skip(offset);
inputStream.read(packData, 0, packData.length);
inputStream.read(packData, offset, packData.length);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
data.setData(packData); data.setData(packData);
session.getUpstream().sendPacket(data); session.sendUpstreamPacket(data);
return true; return true;
} }
} }

View file

@ -15,7 +15,7 @@ import java.util.zip.ZipFile;
public class ResourcePack { public class ResourcePack {
public static final Map<String, ResourcePack> PACKS = new HashMap<>(); public static final Map<String, ResourcePack> PACKS = new HashMap<>();
public static final NativeCode<VoxelwindHash> HASH = new NativeCode<>("native-hash", JavaHash.class, NativeHash.class); public static final NativeCode<VoxelwindHash> HASH = new NativeCode<>("native-hash", JavaHash.class, NativeHash.class);
public static final int CHUNK_SIZE = 1048576; public static final int CHUNK_SIZE = 102400;
private byte[] sha256; private byte[] sha256;
private File file; private File file;