package oldschool.runescape.com;
import java.applet.Applet;
import java.applet.AppletContext;
import java.applet.AppletStub;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
/**
* A loader for the oldschool version of RuneScape
*
* @author Tyluur<itstyluur@gmail.com>
* @author Netty
*/
public final class RS2Loader extends ClassLoader implements AppletStub {
/** The URL of the oldschool client. */
private static URL oldscapeURL;
/** The instance of the frame. */
private static JFrame frame = new JFrame("RuneScape07");
/** The map of the parameters provided by the client. */
private static HashMap<String, String> params = new HashMap<String, String>();
/** The map of the client classes */
private static Hashtable<String, byte[]> classes = new Hashtable<String, byte[]>();
/** The instance of the ProgressBar class.*/
private static ProgressBar progress;
private static JTabbedPane tabbedPane;
private static JPanel contentPane;
/**
* Starts up the class.
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
oldscapeURL = new URL("http://oldschool1.runescape.com/");
parseParams();
loadClasses();
loadClient();
}
private static void parseParams() throws IOException {
System.out.println("Parsing parameters...");
try (BufferedReader br = new BufferedReader(new InputStreamReader(oldscapeURL.openStream()))) {
String line;
while ((line = br.readLine()) != null) {
int index;
if ((index = line.indexOf("archive=") + 8) > 8) {
params.put("initial_jar", line.substring(index, line.length() - 4));
} else if ((index = line.indexOf("param name=\"") + 12) > 12) {
int end = line.indexOf('"', index);
String key = line.substring(index, end);
String value = line.substring(end + 9, line.length() - 5);
params.put(key, value);
}
}
}
}
private static void loadClasses() throws IOException {
URL url = new URL(oldscapeURL, params.get("initial_jar"));
File f = new File(System.getProperty("user.home") + "/.jagex_cache_32/gamepack.jar");
if (f.exists()) {
url = f.toURI().toURL();
System.out.println("Reading gamepack...");
} else {
System.out.println("Downloading gamepack...");
}
URLConnection con = url.openConnection();
InputStream in = con.getInputStream();
ByteArrayOutputStream out = new ByteArrayOutputStream(con.getContentLength());
progress = new ProgressBar(0, con.getContentLength());
byte[] buffer = new byte[4096];
int read;
while ((read = in.read(buffer)) != -1) {
out.write(buffer, 0, read);
progress.updateProgress(out.size());
}
in.close();
if (!f.exists()) {
FileOutputStream fos = new FileOutputStream(f);
fos.write(out.toByteArray());
fos.close();
}
System.out.println("Unzipping gamepack...");
ZipInputStream zis = new ZipInputStream(new ByteArrayInputStream(out.toByteArray()));
ZipEntry ze;
while ((ze = zis.getNextEntry()) != null) {
if (ze.getName().endsWith(".class")) {
out.reset();
while ((read = zis.read(buffer)) != -1) {
out.write(buffer, 0, read);
}
classes.put(ze.getName().replace(".class", ""), out.toByteArray());
}
}
}
private static void loadClient() throws Exception {
System.out.println("Loading client...");
RS2Loader loader = new RS2Loader();
Applet a = null;
try {
a = (Applet) loader.loadClass("client").newInstance();
a.setPreferredSize(new Dimension(765, 503));
a.setStub(loader);
a.init();
a.start();
} catch (Exception e) {
new File(System.getProperty("user.home") + "/gamepack.jar").delete();
loadClient();
e.printStackTrace();
JOptionPane.showMessageDialog(null, "Error loading client - delete gamepack.jar in: " + System.getProperty("user.home") + "/.jagex_cache_32 and reload this applet.");
}
JMenuBar menuBar = new JMenuBar();
JMenu file = new JMenu("File");
JMenu tools = new JMenu("Tools");
JMenuItem exit = new JMenuItem("Exit");
JMenuItem calc = new JMenuItem("Combat Calculator");
calc.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Calculator.main();
}
});
exit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Game client exited.");
System.exit(1);
}
});
file.add(exit);
tools.add(calc);
menuBar.add(file);
menuBar.add(tools);
if (!new File(System.getProperty("user.home") + "/.jagex_cache_32/icon.png").exists())
saveImage("http://images2.wikia.nocookie.net/__cb20111129224838/runescape/images/8/8e/Training_sword.png", System.getProperty("user.home") + "/.jagex_cache_32/icon.png");
getFrame().setIconImage(new ImageIcon(System.getProperty("user.home") + "/.jagex_cache_32/icon.png").getImage());
getFrame().setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getFrame().setContentPane(a);
getFrame().setResizable(false);
getFrame().setJMenuBar(menuBar);
getFrame().pack();
getFrame().setLocationRelativeTo(null);
tabbedPane = new JTabbedPane(JTabbedPane.TOP);
tabbedPane.setBounds(801, 0, 326, 597);
contentPane = new JPanel();
getFrame().setContentPane(contentPane);
contentPane.setLayout(null);
tabbedPane = new JTabbedPane(JTabbedPane.TOP);
tabbedPane.setBounds(801, 0, 326, 597);
contentPane.add(tabbedPane);
JPanel panelTools = new JPanel();
panelTools.setLayout(null);
tabbedPane.addTab("Tools", null, panelTools, null);
getFrame().setVisible(true);
}
public static void saveImage(String imageUrl, String destinationFile) throws IOException {
URL url = new URL(imageUrl);
InputStream is = url.openStream();
OutputStream os = new FileOutputStream(destinationFile);
byte[] b = new byte[2048];
int length;
while ((length = is.read(b)) != -1) {
os.write(b, 0, length);
}
is.close();
os.close();
}
@Override
public Class<?> loadClass(String name) throws ClassNotFoundException {
byte[] clazz = classes.get(name);
if (clazz != null) {
return defineClass(name, clazz, 0, clazz.length);
}
return super.loadClass(name);
}
@Override
public boolean isActive() {
return false;
}
@Override
public URL getDocumentBase() {
return oldscapeURL;
}
@Override
public URL getCodeBase() {
return oldscapeURL;
}
@Override
public String getParameter(String name) {
return params.get(name);
}
@Override
public AppletContext getAppletContext() {
return null;
}
@Override
public void appletResize(int width, int height) {
}
/**
* @return the frame
*/
public static JFrame getFrame() {
return frame;
}
/**
* @param frame the frame to set
*/
public static void setFrame(JFrame frame) {
RS2Loader.frame = frame;
}
}