diff --git a/cache/src/main/java/net/runelite/cache/client/CacheClient.java b/cache/src/main/java/net/runelite/cache/client/CacheClient.java index cc474f891b..07d453be5d 100644 --- a/cache/src/main/java/net/runelite/cache/client/CacheClient.java +++ b/cache/src/main/java/net/runelite/cache/client/CacheClient.java @@ -40,6 +40,8 @@ import io.netty.channel.socket.nio.NioSocketChannel; import java.io.FileNotFoundException; import java.io.IOException; import java.util.ArrayDeque; +import java.util.ArrayList; +import java.util.List; import java.util.Queue; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; @@ -59,8 +61,6 @@ public class CacheClient implements AutoCloseable private static final String HOST = "oldschool1.runescape.com"; private static final int PORT = 43594; - private static final int CLIENT_REVISION = 142; - private static final int MAX_REQUESTS = 19; // too many and the server closes the conncetion private final Store store; // store cache will be written to @@ -75,9 +75,9 @@ public class CacheClient implements AutoCloseable private CompletableFuture handshakeFuture; private final Queue requests = new ArrayDeque<>(); - public CacheClient(Store store) + public CacheClient(Store store, int clientRevision) { - this(store, HOST, CLIENT_REVISION); + this(store, HOST, clientRevision); } public CacheClient(Store store, String host, int clientRevision) @@ -173,21 +173,35 @@ public class CacheClient implements AutoCloseable return state; } - public void download() throws InterruptedException, ExecutionException, FileNotFoundException, IOException + public List requestIndexes() throws IOException { - Stopwatch stopwatch = Stopwatch.createStarted(); - - FileResult result = requestFile(255, 255, true).get(); + FileResult result = requestFile(255, 255, true).join(); result.decompress(null); ByteBuf buffer = Unpooled.wrappedBuffer(result.getContents()); - int indexCount = result.getContents().length / 8; + List indexInfo = new ArrayList<>(); for (int i = 0; i < indexCount; ++i) { int crc = buffer.readInt(); int revision = buffer.readInt(); + indexInfo.add(new IndexInfo(i, crc, revision)); + } + + return indexInfo; + } + + public void download() throws InterruptedException, ExecutionException, FileNotFoundException, IOException + { + Stopwatch stopwatch = Stopwatch.createStarted(); + + List indexes = requestIndexes(); + for (IndexInfo indexInfo : indexes) + { + int i = indexInfo.getId(); + int crc = indexInfo.getCrc(); + int revision = indexInfo.getRevision(); Index index = store.findIndex(i); @@ -306,7 +320,7 @@ public class CacheClient implements AutoCloseable logger.info("Download completed in {}", stopwatch); } - public synchronized CompletableFuture requestFile(int index, int fileId, boolean flush) + private synchronized CompletableFuture requestFile(int index, int fileId, boolean flush) { if (state != ClientState.CONNECTED) { diff --git a/cache/src/main/java/net/runelite/cache/client/IndexInfo.java b/cache/src/main/java/net/runelite/cache/client/IndexInfo.java new file mode 100644 index 0000000000..b6f088e84b --- /dev/null +++ b/cache/src/main/java/net/runelite/cache/client/IndexInfo.java @@ -0,0 +1,101 @@ +/* + * Copyright (c) 2016-2017, Adam + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package net.runelite.cache.client; + +public class IndexInfo +{ + private final int id; + private final int crc; + private final int revision; + + public IndexInfo(int id, int crc, int revision) + { + this.id = id; + this.crc = crc; + this.revision = revision; + } + + @Override + public String toString() + { + return "IndexInfo{" + "id=" + id + ", crc=" + crc + ", revision=" + revision + '}'; + } + + @Override + public int hashCode() + { + int hash = 5; + hash = 71 * hash + this.id; + hash = 71 * hash + this.crc; + hash = 71 * hash + this.revision; + return hash; + } + + @Override + public boolean equals(Object obj) + { + if (this == obj) + { + return true; + } + if (obj == null) + { + return false; + } + if (getClass() != obj.getClass()) + { + return false; + } + final IndexInfo other = (IndexInfo) obj; + if (this.id != other.id) + { + return false; + } + if (this.crc != other.crc) + { + return false; + } + if (this.revision != other.revision) + { + return false; + } + return true; + } + + public int getId() + { + return id; + } + + public int getCrc() + { + return crc; + } + + public int getRevision() + { + return revision; + } +} diff --git a/cache/src/test/java/net/runelite/cache/CacheProperties.java b/cache/src/test/java/net/runelite/cache/CacheProperties.java new file mode 100644 index 0000000000..50da8759dc --- /dev/null +++ b/cache/src/test/java/net/runelite/cache/CacheProperties.java @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2016-2017, Adam + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package net.runelite.cache; + +import java.io.IOException; +import java.io.InputStream; +import java.util.Properties; + +public class CacheProperties +{ + private static Properties getProperies() throws IOException + { + Properties properties = new Properties(); + InputStream resourceAsStream = StoreLocation.class.getResourceAsStream("/cache.properties"); + properties.load(resourceAsStream); + return properties; + } + + public static int getRsVersion() throws IOException + { + return Integer.parseInt(getProperies().getProperty("rs.version")); + } + + public static int getCacheVersion() throws IOException + { + return Integer.parseInt(getProperies().getProperty("cache.version")); + } +} diff --git a/cache/src/test/java/net/runelite/cache/StoreLocation.java b/cache/src/test/java/net/runelite/cache/StoreLocation.java index ecda43fda3..c751a3f19c 100644 --- a/cache/src/test/java/net/runelite/cache/StoreLocation.java +++ b/cache/src/test/java/net/runelite/cache/StoreLocation.java @@ -62,17 +62,9 @@ public class StoreLocation } } - private static Properties getProperies() throws IOException - { - Properties properties = new Properties(); - InputStream resourceAsStream = StoreLocation.class.getResourceAsStream("/cache.properties"); - properties.load(resourceAsStream); - return properties; - } - private static File setupCacheDir() throws IOException { - File file = new File(System.getProperty("java.io.tmpdir"), "cache-" + getProperies().getProperty("cache.version")); + File file = new File(System.getProperty("java.io.tmpdir"), "cache-" + CacheProperties.getCacheVersion()); if (file.exists()) { diff --git a/cache/src/test/java/net/runelite/cache/client/CacheClientTest.java b/cache/src/test/java/net/runelite/cache/client/CacheClientTest.java index fc60965415..1e74efa9d5 100644 --- a/cache/src/test/java/net/runelite/cache/client/CacheClientTest.java +++ b/cache/src/test/java/net/runelite/cache/client/CacheClientTest.java @@ -26,6 +26,7 @@ package net.runelite.cache.client; import java.io.File; import java.util.concurrent.CompletableFuture; +import net.runelite.cache.CacheProperties; import net.runelite.cache.fs.Store; import org.junit.Assert; import org.junit.Before; @@ -53,7 +54,7 @@ public class CacheClientTest { store.load(); - CacheClient c = new CacheClient(store); + CacheClient c = new CacheClient(store, CacheProperties.getRsVersion()); c.connect(); CompletableFuture handshake = c.handshake(); @@ -78,7 +79,7 @@ public class CacheClientTest { store.loadTree(new File("C:\\rs\\runescape-data\\cache")); - CacheClient c = new CacheClient(store); + CacheClient c = new CacheClient(store, CacheProperties.getRsVersion()); c.connect(); CompletableFuture handshake = c.handshake(); diff --git a/cache/src/test/resources/cache.properties b/cache/src/test/resources/cache.properties index 6b22f10e57..03e58d141f 100644 --- a/cache/src/test/resources/cache.properties +++ b/cache/src/test/resources/cache.properties @@ -1,2 +1,3 @@ +rs.version=${rs.version} cache.version=${cache.version}