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);
}
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());
}
}
}
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);
}
}
}
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);
}
}
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);
}
Aggregations