-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSimpleClient.java
240 lines (205 loc) · 7.89 KB
/
SimpleClient.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*
* ISC License
*
* Copyright (c) 2017-2019, Hunter WB <hunterwb.com>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
import javax.swing.JFrame;
import javax.swing.WindowConstants;
import java.applet.Applet;
import java.applet.AppletContext;
import java.applet.AppletStub;
import java.applet.AudioClip;
import java.awt.Desktop;
import java.awt.Dimension;
import java.awt.Image;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.net.URLConnection;
import java.net.URLStreamHandler;
import java.nio.charset.StandardCharsets;
import java.security.InvalidParameterException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.jar.JarEntry;
import java.util.jar.JarInputStream;
@SuppressWarnings("deprecation")
public final class SimpleClient implements AppletStub, AppletContext {
public static void main(String[] args) throws Exception {
System.setProperty("sun.awt.noerasebackground", "true"); // fixes resize flickering
SimpleClient c = load();
Applet applet = c.loadApplet();
JFrame frame = new JFrame(c.title());
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.add(applet);
frame.pack();
frame.setMinimumSize(frame.getSize());
frame.setPreferredSize(frame.getSize());
frame.setLocationRelativeTo(null);
frame.setVisible(true);
applet.init();
applet.start();
}
private final Map<String, String> properties;
private final Map<String, String> parameters;
private SimpleClient(
Map<String, String> properties,
Map<String, String> parameters
) {
this.properties = properties;
this.parameters = parameters;
}
public static SimpleClient load() throws IOException {
Map<String, String> properties = new HashMap<>();
Map<String, String> parameters = new HashMap<>();
URL url = new URL("http://oldschool.runescape.com/jav_config.ws");
try (BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream(), StandardCharsets.ISO_8859_1))) {
String line;
while ((line = reader.readLine()) != null) {
String[] split1 = line.split("=", 2);
switch (split1[0]) {
case "param":
String[] split2 = split1[1].split("=", 2);
parameters.put(split2[0], split2[1]);
break;
case "msg":
// ignore
break;
default:
properties.put(split1[0], split1[1]);
}
}
}
return new SimpleClient(properties, parameters);
}
public Applet loadApplet() throws Exception {
Applet applet = (Applet) classLoader(gamepackUrl()).loadClass(initialClass()).getDeclaredConstructor().newInstance();
applet.setStub(this);
applet.setMaximumSize(appletMaxSize());
applet.setMinimumSize(appletMinSize());
applet.setPreferredSize(applet.getMinimumSize());
return applet;
}
public String title() {
return properties.get("title");
}
private Dimension appletMinSize() {
return new Dimension(
Integer.parseInt(properties.get("applet_minwidth")),
Integer.parseInt(properties.get("applet_minheight"))
);
}
private Dimension appletMaxSize() {
return new Dimension(
Integer.parseInt(properties.get("applet_maxwidth")),
Integer.parseInt(properties.get("applet_maxheight"))
);
}
private URL gamepackUrl() throws MalformedURLException {
return new URL(properties.get("codebase") + properties.get("initial_jar"));
}
private String initialClass() {
String fileName = properties.get("initial_class");
return fileName.substring(0, fileName.length() - 6);
}
@Override public URL getCodeBase() {
try {
return new URL(properties.get("codebase"));
} catch (MalformedURLException e) {
throw new InvalidParameterException();
}
}
@Override public URL getDocumentBase() {
return getCodeBase();
}
@Override public boolean isActive() {
return true;
}
@Override public String getParameter(String name) {
return parameters.get(name);
}
@Override public void showDocument(URL url) {
try {
Desktop.getDesktop().browse(url.toURI());
} catch (Exception e) {
e.printStackTrace();
}
}
@Override public void showDocument(URL url, String target) {
showDocument(url);
}
@Override public AppletContext getAppletContext() {
return this;
}
@Override public void appletResize(int width, int height) {}
@Override public AudioClip getAudioClip(URL url) {
throw new UnsupportedOperationException();
}
@Override public Image getImage(URL url) {
throw new UnsupportedOperationException();
}
@Override public Applet getApplet(String name) {
throw new UnsupportedOperationException();
}
@Override public Enumeration<Applet> getApplets() {
throw new UnsupportedOperationException();
}
@Override public void showStatus(String status) {
throw new UnsupportedOperationException();
}
@Override public void setStream(String key, InputStream stream) {
throw new UnsupportedOperationException();
}
@Override public InputStream getStream(String key) {
throw new UnsupportedOperationException();
}
@Override public Iterator<String> getStreamKeys() {
throw new UnsupportedOperationException();
}
private static ClassLoader classLoader(URL jarUrl) throws IOException {
Map<String, byte[]> files = new HashMap<>();
try (JarInputStream jar = new JarInputStream(new BufferedInputStream(jarUrl.openStream()))) {
JarEntry entry;
while ((entry = jar.getNextJarEntry()) != null) {
files.put('/' + entry.getName(), jar.readAllBytes());
}
}
URL url = new URL("x-buffer", null, -1, "/", new URLStreamHandler() {
@Override protected URLConnection openConnection(URL u) throws FileNotFoundException {
byte[] data = files.get(u.getFile());
if (data == null) throw new FileNotFoundException(u.getFile());
return new URLConnection(u) {
@Override public void connect() {}
@Override public long getContentLengthLong() {
return data.length;
}
@Override public InputStream getInputStream() {
return new ByteArrayInputStream(data);
}
};
}
});
return new URLClassLoader(new URL[]{url});
}
}