Search in sources :

Example 31 with Storage

use of net.runelite.cache.fs.Storage in project runelite by runelite.

the class CacheServerTest method testServer.

@Test
public void testServer() throws Exception {
    try (Store store = new Store(folder.newFolder());
        CacheServer server = new CacheServer(store, REVISION)) {
        addInitialFilesToStore(store);
        store.save();
        server.start();
        try (Store store2 = new Store(folder.newFolder());
            CacheClient client = new CacheClient(store2, HOST, REVISION)) {
            client.connect();
            client.handshake().get();
            client.download();
            Index index = store2.findIndex(0);
            Archive archive = index.getArchive(0);
            FileData[] files = archive.getFileData();
            FileData file = files[0];
            assertEquals(7, file.getNameHash());
            Storage storage = store2.getStorage();
            byte[] data = storage.loadArchive(archive);
            data = archive.decompress(data);
            assertArrayEquals("test".getBytes(), data);
            assertEquals(store.getIndexes().get(0).getArchive(0).getCrc(), archive.getCrc());
        }
    }
}
Also used : CacheClient(net.runelite.cache.client.CacheClient) Archive(net.runelite.cache.fs.Archive) Storage(net.runelite.cache.fs.Storage) Store(net.runelite.cache.fs.Store) Index(net.runelite.cache.fs.Index) FileData(net.runelite.cache.index.FileData) Test(org.junit.Test)

Example 32 with Storage

use of net.runelite.cache.fs.Storage in project runelite by runelite.

the class AreaManager method load.

public void load() throws IOException {
    Storage storage = store.getStorage();
    Index index = store.getIndex(IndexType.CONFIGS);
    Archive archive = index.getArchive(ConfigType.AREA.getId());
    byte[] archiveData = storage.loadArchive(archive);
    ArchiveFiles files = archive.getFiles(archiveData);
    for (FSFile file : files.getFiles()) {
        AreaLoader loader = new AreaLoader();
        AreaDefinition area = loader.load(file.getContents(), file.getFileId());
        areas.put(area.id, area);
    }
}
Also used : AreaDefinition(net.runelite.cache.definitions.AreaDefinition) Storage(net.runelite.cache.fs.Storage) Archive(net.runelite.cache.fs.Archive) ArchiveFiles(net.runelite.cache.fs.ArchiveFiles) Index(net.runelite.cache.fs.Index) AreaLoader(net.runelite.cache.definitions.loaders.AreaLoader) FSFile(net.runelite.cache.fs.FSFile)

Example 33 with Storage

use of net.runelite.cache.fs.Storage 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)

Example 34 with Storage

use of net.runelite.cache.fs.Storage in project runelite by runelite.

the class SpriteManager method load.

public void load() throws IOException {
    Storage storage = store.getStorage();
    Index index = store.getIndex(IndexType.SPRITES);
    for (Archive a : index.getArchives()) {
        byte[] contents = a.decompress(storage.loadArchive(a));
        SpriteLoader loader = new SpriteLoader();
        SpriteDefinition[] defs = loader.load(a.getArchiveId(), contents);
        for (SpriteDefinition sprite : defs) {
            sprites.put(sprite.getId(), sprite);
        }
    }
}
Also used : Storage(net.runelite.cache.fs.Storage) Archive(net.runelite.cache.fs.Archive) SpriteDefinition(net.runelite.cache.definitions.SpriteDefinition) Index(net.runelite.cache.fs.Index) SpriteLoader(net.runelite.cache.definitions.loaders.SpriteLoader)

Aggregations

Archive (net.runelite.cache.fs.Archive)34 Storage (net.runelite.cache.fs.Storage)34 Index (net.runelite.cache.fs.Index)33 ArchiveFiles (net.runelite.cache.fs.ArchiveFiles)18 FSFile (net.runelite.cache.fs.FSFile)18 Store (net.runelite.cache.fs.Store)18 File (java.io.File)17 Test (org.junit.Test)17 IOException (java.io.IOException)4 FramemapDefinition (net.runelite.cache.definitions.FramemapDefinition)2 InterfaceDefinition (net.runelite.cache.definitions.InterfaceDefinition)2 InventoryDefinition (net.runelite.cache.definitions.InventoryDefinition)2 LocationsDefinition (net.runelite.cache.definitions.LocationsDefinition)2 MapDefinition (net.runelite.cache.definitions.MapDefinition)2 OverlayDefinition (net.runelite.cache.definitions.OverlayDefinition)2 SpriteDefinition (net.runelite.cache.definitions.SpriteDefinition)2 UnderlayDefinition (net.runelite.cache.definitions.UnderlayDefinition)2 FramemapLoader (net.runelite.cache.definitions.loaders.FramemapLoader)2 InterfaceLoader (net.runelite.cache.definitions.loaders.InterfaceLoader)2 InventoryLoader (net.runelite.cache.definitions.loaders.InventoryLoader)2