Search in sources :

Example 36 with Archive

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

the class CacheServerTest method addInitialFilesToStore.

private void addInitialFilesToStore(Store store) throws FileNotFoundException, IOException {
    Storage storage = store.getStorage();
    Index index = store.addIndex(0);
    Archive archive = index.addArchive(0);
    FileData[] files = new FileData[1];
    archive.setFileData(files);
    FileData file = files[0] = new FileData();
    file.setNameHash(7);
    byte[] data = "test".getBytes();
    Container container = new Container(archive.getCompression(), -1);
    container.compress(data, null);
    byte[] compressedData = container.data;
    storage.saveArchive(archive, compressedData);
}
Also used : Container(net.runelite.cache.fs.Container) Storage(net.runelite.cache.fs.Storage) Archive(net.runelite.cache.fs.Archive) Index(net.runelite.cache.fs.Index) FileData(net.runelite.cache.index.FileData)

Example 37 with Archive

use of net.runelite.cache.fs.Archive 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 38 with Archive

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

the class CacheStorage method save.

@Override
public void save(Store store) throws IOException {
    for (Index index : store.getIndexes()) {
        IndexEntry entry = cacheDao.createIndex(con, cacheEntry, index.getId(), index.getCrc(), index.getRevision());
        for (Archive archive : index.getArchives()) {
            ArchiveEntry archiveEntry = cacheDao.findArchive(con, entry, archive.getArchiveId(), archive.getNameHash(), archive.getCrc(), archive.getRevision());
            if (archiveEntry == null) {
                byte[] hash = archive.getHash();
                archiveEntry = cacheDao.createArchive(con, entry, archive.getArchiveId(), archive.getNameHash(), archive.getCrc(), archive.getRevision(), hash);
                for (FileData file : archive.getFileData()) {
                    cacheDao.associateFileToArchive(con, archiveEntry, file.getId(), file.getNameHash());
                }
            }
            cacheDao.associateArchiveToIndex(con, archiveEntry, entry);
        }
    }
}
Also used : Archive(net.runelite.cache.fs.Archive) IndexEntry(net.runelite.cache.updater.beans.IndexEntry) Index(net.runelite.cache.fs.Index) ArchiveEntry(net.runelite.cache.updater.beans.ArchiveEntry) FileData(net.runelite.cache.index.FileData)

Example 39 with Archive

use of net.runelite.cache.fs.Archive 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 40 with Archive

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

Archive (net.runelite.cache.fs.Archive)41 Index (net.runelite.cache.fs.Index)38 Storage (net.runelite.cache.fs.Storage)34 Store (net.runelite.cache.fs.Store)21 File (java.io.File)19 Test (org.junit.Test)19 ArchiveFiles (net.runelite.cache.fs.ArchiveFiles)18 FSFile (net.runelite.cache.fs.FSFile)18 IOException (java.io.IOException)6 FileData (net.runelite.cache.index.FileData)4 Container (net.runelite.cache.fs.Container)3 BufferedImage (java.awt.image.BufferedImage)2 CacheClient (net.runelite.cache.client.CacheClient)2 FramemapDefinition (net.runelite.cache.definitions.FramemapDefinition)2 InterfaceDefinition (net.runelite.cache.definitions.InterfaceDefinition)2 InventoryDefinition (net.runelite.cache.definitions.InventoryDefinition)2 ItemDefinition (net.runelite.cache.definitions.ItemDefinition)2 LocationsDefinition (net.runelite.cache.definitions.LocationsDefinition)2 MapDefinition (net.runelite.cache.definitions.MapDefinition)2 OverlayDefinition (net.runelite.cache.definitions.OverlayDefinition)2