runelite-client: load vanilla client if an update has occured

This commit is contained in:
Adam
2017-05-20 15:42:01 -04:00
parent 39430395ef
commit b8242eeed1
4 changed files with 142 additions and 38 deletions

View File

@@ -0,0 +1,68 @@
/*
* Copyright (c) 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.http.api.updatecheck;
import com.google.gson.JsonParseException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import net.runelite.http.api.RuneliteAPI;
import okhttp3.HttpUrl;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class UpdateCheckClient
{
private static final Logger logger = LoggerFactory.getLogger(UpdateCheckClient.class);
public boolean isOutdated()
{
HttpUrl url = RuneliteAPI.getApiBase().newBuilder()
.addPathSegment("update-check")
.build();
logger.debug("Built URI: {}", url);
Request request = new Request.Builder()
.url(url)
.build();
try (Response response = RuneliteAPI.CLIENT.newCall(request).execute())
{
ResponseBody body = response.body();
InputStream in = body.byteStream();
return RuneliteAPI.GSON.fromJson(new InputStreamReader(in), boolean.class);
}
catch (JsonParseException | IOException ex)
{
logger.debug("Unable to update-check", ex);
return false;
}
}
}

View File

@@ -22,12 +22,12 @@
* (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.client;
import java.applet.Applet;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -35,7 +35,7 @@ public class ClientLoader
{
private static final Logger logger = LoggerFactory.getLogger(ClientLoader.class);
public Applet load() throws MalformedURLException, ClassNotFoundException, IOException, InstantiationException, IllegalAccessException
public Applet loadRunelite() throws ClassNotFoundException, IOException, InstantiationException, IllegalAccessException
{
ConfigLoader config = new ConfigLoader();
@@ -43,6 +43,7 @@ public class ClientLoader
String initialClass = config.getProperty(ConfigLoader.INITIAL_CLASS).replace(".class", "");
// the injected client is a runtime scoped dependency
Class<?> clientClass = this.getClass().getClassLoader().loadClass(initialClass);
Applet rs = (Applet) clientClass.newInstance();
@@ -50,4 +51,31 @@ public class ClientLoader
return rs;
}
public Applet loadVanilla() throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException
{
ConfigLoader config = new ConfigLoader();
config.fetch();
String codebase = config.getProperty(ConfigLoader.CODEBASE);
String initialJar = config.getProperty(ConfigLoader.INITIAL_JAR);
String initialClass = config.getProperty(ConfigLoader.INITIAL_CLASS).replace(".class", "");
URL url = new URL(codebase + initialJar);
// Must set parent classloader to null, or it will pull from
// this class's classloader first
URLClassLoader classloader = new URLClassLoader(new URL[]
{
url
}, null);
Class<?> clientClass = classloader.loadClass(initialClass);
Applet rs = (Applet) clientClass.newInstance();
rs.setStub(new RSStub(config, rs));
return rs;
}
}

View File

@@ -22,47 +22,39 @@
* (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.client;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Map;
import net.runelite.http.api.RuneliteAPI;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class ConfigLoader
{
private static URL configURL;
private static final OkHttpClient CLIENT = RuneliteAPI.CLIENT;
private static final HttpUrl CONFIG_URL = HttpUrl.parse("http://oldschool.runescape.com/jav_config.ws"); // https redirects us to rs3
public static final String CODEBASE = "codebase";
public static final String INITIAL_JAR = "initial_jar";
public static final String INITIAL_CLASS = "initial_class";
public static final String APP_MINWIDTH = "applet_minwidth";
public static final String APP_MINHEIGHT = "applet_minheight";
private final Map<String, String> properties = new HashMap<>(),
appletProperties = new HashMap<>();
static
{
try
{
configURL = new URL("http://oldschool.runescape.com/jav_config.ws"); // https redirects us to rs3
}
catch (MalformedURLException ex)
{
ex.printStackTrace();
}
}
public void fetch() throws IOException
{
URLConnection conn = configURL.openConnection();
try (BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream())))
Request request = new Request.Builder()
.url(CONFIG_URL)
.build();
try (Response response = CLIENT.newCall(request).execute();
BufferedReader in = new BufferedReader(new InputStreamReader(response.body().byteStream())))
{
String str;
@@ -71,7 +63,9 @@ public class ConfigLoader
int idx = str.indexOf('=');
if (idx == -1)
{
continue;
}
String s = str.substring(0, idx);

View File

@@ -32,6 +32,7 @@ import javax.swing.JPanel;
import net.runelite.api.Client;
import net.runelite.client.ClientLoader;
import net.runelite.client.RuneLite;
import net.runelite.http.api.updatecheck.UpdateCheckClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -58,23 +59,36 @@ final class ClientPanel extends JPanel
ClientLoader loader = new ClientLoader();
rs = loader.load();
UpdateCheckClient updateCheck = new UpdateCheckClient();
boolean isOutdated = updateCheck.isOutdated();
if (isOutdated)
{
logger.info("Runelite is outdated - fetching vanilla client");
rs = loader.loadVanilla();
}
else
{
logger.debug("Runelite is up to date");
rs = loader.loadRunelite();
}
rs.setLayout(null);
rs.init();
rs.start();
add(rs, BorderLayout.CENTER);
Client client = null;
try
if (isOutdated)
{
client = new Client((net.runelite.rs.api.Client) rs);
return;
}
catch (Exception ex)
if (!(rs instanceof net.runelite.rs.api.Client))
{
logger.warn("Unable to create client", ex);
logger.error("Injected client does not implement Client!");
System.exit(-1);
}
Client client = new Client((net.runelite.rs.api.Client) rs);
RuneLite.setClient(client);
}