cache: split index request from download, remove hardcoded client rev

This commit is contained in:
Adam
2017-09-03 16:21:23 -04:00
parent 1305c37951
commit 0bfdf1d39a
6 changed files with 180 additions and 21 deletions

View File

@@ -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<Integer> handshakeFuture;
private final Queue<PendingFileRequest> 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<IndexInfo> 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> 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<IndexInfo> 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<FileResult> requestFile(int index, int fileId, boolean flush)
private synchronized CompletableFuture<FileResult> requestFile(int index, int fileId, boolean flush)
{
if (state != ClientState.CONNECTED)
{

View File

@@ -0,0 +1,101 @@
/*
* Copyright (c) 2016-2017, Adam <Adam@sigterm.info>
* 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;
}
}

View File

@@ -0,0 +1,50 @@
/*
* Copyright (c) 2016-2017, Adam <Adam@sigterm.info>
* 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"));
}
}

View File

@@ -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())
{

View File

@@ -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<Integer> 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<Integer> handshake = c.handshake();

View File

@@ -1,2 +1,3 @@
rs.version=${rs.version}
cache.version=${cache.version}