use of net.runelite.cache.downloader.FileResult in project ostracker by OSTracker.
the class RemoteCache method download.
public void download(Store store) throws IOException, ExecutionException, InterruptedException {
FileResult indicesFile = downloadFile(255, 255);
ByteBuf buffer = Unpooled.wrappedBuffer(indicesFile.getContents());
int indexCount = indicesFile.getContents().length / 8;
for (int i = 0; i < indexCount; i++) {
int crc = buffer.readInt();
int revision = buffer.readInt();
Index index = store.findIndex(i);
if (index == null) {
LOGGER.info("Index " + i + " does not exist locally, downloading..");
} else if (index.getRevision() != revision) {
LOGGER.info(MessageFormat.format("Index {0} has the wrong revision (local: {1}, remote: {2})", i, index.getRevision(), revision));
} else {
LOGGER.info("Index " + i + " is up to date");
continue;
}
LOGGER.info("Downloading index " + i);
FileResult indexFile = downloadFile(255, i);
LOGGER.info("Done downloading index " + i);
if (indexFile.getCrc() != crc) {
LOGGER.error("Crc mismatch between downloaded file and expected checksum for index " + i);
continue;
}
Index oldIndex = null;
if (index != null) {
store.removeIndex(index);
oldIndex = index;
}
index = store.addIndex(i);
index.readIndexData(indexFile.getContents());
index.setCrc(crc);
LOGGER.info("Index " + i + " has " + index.getArchives().size() + " archives");
for (Archive archive : index.getArchives()) {
Archive oldArchive = null;
if (oldIndex != null) {
oldArchive = oldIndex.getArchive(archive.getArchiveId());
}
if (oldArchive == null || oldArchive.getRevision() != archive.getRevision()) {
LOGGER.info(MessageFormat.format("Archive {0} in index {1} is out of date, downloading..", archive.getArchiveId(), i));
FileResult archiveFile = downloadFile(i, archive.getArchiveId(), false);
archive.setData(archiveFile.getCompressedData());
} else {
LOGGER.info(MessageFormat.format("Archive {0} in index {1} is up to date", archive.getArchiveId(), i));
if (oldArchive.getData() != null) {
archive.setData(oldArchive.getData());
} else {
archive.loadContents(oldArchive.saveContents());
archive.setCompression(oldArchive.getCompression());
}
}
}
}
}
use of net.runelite.cache.downloader.FileResult in project ostracker by OSTracker.
the class RemoteCache method downloadFile.
private FileResult downloadFile(int indexId, int fileId, boolean shouldDecompress) throws IOException {
ByteBuf buffer = Unpooled.buffer(4);
// Type
buffer.writeByte(indexId == 255 ? 1 : 0);
// Hash
buffer.writeMedium(indexId << 16 | fileId);
writer.write(buffer);
writer.flush();
int remoteIndexId = reader.readUnsignedByte();
if (indexId != remoteIndexId) {
throw new IllegalStateException("Remote index id " + remoteIndexId + " did not match input index id " + indexId);
}
int remoteFileId = reader.readUnsignedShort();
if (fileId != remoteFileId) {
throw new IllegalStateException("Remote file id " + remoteFileId + " did not match input file id " + fileId);
}
FileResult result = downloadCompressedFile(indexId, fileId);
if (shouldDecompress) {
result.decompress(null);
}
return result;
}
Aggregations