Search in sources :

Example 1 with ArchiveData

use of net.runelite.cache.index.ArchiveData in project runelite by runelite.

the class Index method toIndexData.

public IndexData toIndexData() {
    IndexData data = new IndexData();
    data.setProtocol(protocol);
    data.setRevision(revision);
    data.setNamed(named);
    ArchiveData[] archiveDatas = new ArchiveData[archives.size()];
    data.setArchives(archiveDatas);
    int idx = 0;
    for (Archive archive : archives) {
        ArchiveData ad = archiveDatas[idx++] = new ArchiveData();
        ad.setId(archive.getArchiveId());
        ad.setNameHash(archive.getNameHash());
        ad.setCrc(archive.getCrc());
        ad.setRevision(archive.getRevision());
        FileData[] files = archive.getFileData();
        ad.setFiles(files);
    }
    return data;
}
Also used : IndexData(net.runelite.cache.index.IndexData) ArchiveData(net.runelite.cache.index.ArchiveData) FileData(net.runelite.cache.index.FileData)

Example 2 with ArchiveData

use of net.runelite.cache.index.ArchiveData in project runelite by runelite.

the class DiskStorage method loadIndex.

private void loadIndex(Index index) throws IOException {
    logger.trace("Loading index {}", index.getId());
    byte[] indexData = readIndex(index.getId());
    Container res = Container.decompress(indexData, null);
    byte[] data = res.data;
    IndexData id = new IndexData();
    id.load(data);
    index.setProtocol(id.getProtocol());
    index.setRevision(id.getRevision());
    index.setNamed(id.isNamed());
    for (ArchiveData ad : id.getArchives()) {
        Archive archive = index.addArchive(ad.getId());
        archive.setNameHash(ad.getNameHash());
        archive.setCrc(ad.getCrc());
        archive.setRevision(ad.getRevision());
        archive.setFileData(ad.getFiles());
        assert ad.getFiles().length > 0;
    }
    index.setCrc(res.crc);
    index.setCompression(res.compression);
    assert res.revision == -1;
}
Also used : Container(net.runelite.cache.fs.Container) Archive(net.runelite.cache.fs.Archive) IndexData(net.runelite.cache.index.IndexData) ArchiveData(net.runelite.cache.index.ArchiveData)

Example 3 with ArchiveData

use of net.runelite.cache.index.ArchiveData in project runelite by runelite.

the class CacheClient method download.

public void download() throws IOException {
    Stopwatch stopwatch = Stopwatch.createStarted();
    List<IndexInfo> indexes = requestIndexes();
    for (IndexInfo indexInfo : indexes) {
        int i = indexInfo.getId();
        int crc = indexInfo.getCrc();
        int revision = indexInfo.getRevision();
        Index index = store.findIndex(i);
        if (index == null) {
            logger.info("Index {} does not exist, creating", i);
        } else if (index.getRevision() != revision) {
            if (revision < index.getRevision()) {
                logger.warn("Index {} revision is going BACKWARDS! (our revision {}, their revision {})", index.getId(), index.getRevision(), revision);
            } else {
                logger.info("Index {} has the wrong revision (our revision {}, their revision {})", index.getId(), index.getRevision(), revision);
            }
        } else if (index.getCrc() != crc) {
            logger.warn("Index {} CRC has changed! (our crc {}, their crc {})", index.getCrc(), index.getCrc(), crc);
        } else {
            // despite the index being up to date, not everything
            // can be downloaded, eg. for tracks.
            logger.info("Index {} is up to date", index.getId());
        }
        logger.info("Downloading index {}", i);
        FileResult indexFileResult = requestFile(255, i, true).join();
        indexFileResult.decompress(null);
        logger.info("Downloaded index {}", i);
        if (indexFileResult.getCrc() != crc) {
            logger.warn("Corrupted download for index {}", i);
            continue;
        }
        IndexData indexData = new IndexData();
        indexData.load(indexFileResult.getContents());
        if (index == null) {
            index = store.addIndex(i);
        }
        // update index settings
        index.setProtocol(indexData.getProtocol());
        index.setNamed(indexData.isNamed());
        index.setCrc(crc);
        index.setRevision(revision);
        logger.info("Index {} has {} archives", i, indexData.getArchives().length);
        for (ArchiveData ad : indexData.getArchives()) {
            Archive existing = index.getArchive(ad.getId());
            if (existing != null && existing.getRevision() == ad.getRevision() && existing.getCrc() == ad.getCrc() && existing.getNameHash() == ad.getNameHash()) {
                logger.debug("Archive {}/{} in index {} is up to date", ad.getId(), indexData.getArchives().length, index.getId());
                continue;
            }
            if (existing == null) {
                logger.info("Archive {}/{} in index {} is out of date, downloading", ad.getId(), indexData.getArchives().length, index.getId());
            } else if (ad.getRevision() < existing.getRevision()) {
                logger.warn("Archive {}/{} in index {} revision is going BACKWARDS! (our revision {}, their revision {})", ad.getId(), indexData.getArchives().length, index.getId(), existing.getRevision(), ad.getRevision());
            } else {
                logger.info("Archive {}/{} in index {} is out of date, downloading. " + "revision: ours: {} theirs: {}, crc: ours: {} theirs {}, name: ours {} theirs {}", ad.getId(), indexData.getArchives().length, index.getId(), existing.getRevision(), ad.getRevision(), existing.getCrc(), ad.getCrc(), existing.getNameHash(), ad.getNameHash());
            }
            final Archive archive = existing == null ? index.addArchive(ad.getId()) : existing;
            archive.setRevision(ad.getRevision());
            archive.setCrc(ad.getCrc());
            archive.setNameHash(ad.getNameHash());
            // Add files
            archive.setFileData(ad.getFiles());
            CompletableFuture<FileResult> future = requestFile(index.getId(), ad.getId(), false);
            future.handle((fr, ex) -> {
                byte[] data = fr.getCompressedData();
                Crc32 crc32 = new Crc32();
                crc32.update(data, 0, data.length);
                int hash = crc32.getHash();
                if (hash != archive.getCrc()) {
                    logger.warn("crc mismatch on downloaded archive {}/{}: {} != {}", archive.getIndex().getId(), archive.getArchiveId(), hash, archive.getCrc());
                    throw new RuntimeException("crc mismatch");
                }
                if (watcher != null) {
                    watcher.downloadComplete(archive, data);
                } else {
                    try {
                        Storage storage = store.getStorage();
                        storage.saveArchive(archive, data);
                    } catch (IOException ex1) {
                        logger.warn("unable to save archive data", ex1);
                    }
                }
                return null;
            });
        }
    }
    // flush any pending requests
    channel.flush();
    while (!requests.isEmpty()) {
        // wait for pending requests
        synchronized (this) {
            try {
                wait();
            } catch (InterruptedException ex) {
                logger.warn(null, ex);
            }
        }
    }
    stopwatch.stop();
    logger.info("Download completed in {}", stopwatch);
}
Also used : Archive(net.runelite.cache.fs.Archive) Stopwatch(com.google.common.base.Stopwatch) Index(net.runelite.cache.fs.Index) IOException(java.io.IOException) Storage(net.runelite.cache.fs.Storage) IndexData(net.runelite.cache.index.IndexData) Crc32(net.runelite.cache.util.Crc32) ArchiveData(net.runelite.cache.index.ArchiveData)

Aggregations

ArchiveData (net.runelite.cache.index.ArchiveData)3 IndexData (net.runelite.cache.index.IndexData)3 Archive (net.runelite.cache.fs.Archive)2 Stopwatch (com.google.common.base.Stopwatch)1 IOException (java.io.IOException)1 Container (net.runelite.cache.fs.Container)1 Index (net.runelite.cache.fs.Index)1 Storage (net.runelite.cache.fs.Storage)1 FileData (net.runelite.cache.index.FileData)1 Crc32 (net.runelite.cache.util.Crc32)1