blob: b12acfc8cbe84c6c0f18ec29f8ab3ae02f370169 (
plain)
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
|
/*
* Q2DataDialog.java
* Copyright (C) 2003
*/
package jake2.qcommon;
import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.util.Enumeration;
import java.util.Vector;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
public class Q2DataTool {
static final String home = System.getProperty("user.home");
static final String sep = System.getProperty("file.separator");
static final String dataDir = home + sep + "Jake2";
static final String baseq2Dir = dataDir + sep + "baseq2";
private Vector<String> mirrorNames = new Vector<String>();
private Vector<String> mirrorLinks = new Vector<String>();
private byte[] buf = new byte[8192];
public void testQ2Data() {
initMirrors();
for(int i=0; !isAvail() && i<mirrorNames.size(); i++) {
try {
install(i);
} catch (Exception e) {
e.printStackTrace();
}
}
}
void destroy() {
}
void setStatus(String text) {
System.err.println(text);
System.err.println();
}
boolean isAvail() {
Cvar.Set("cddir", baseq2Dir);
FS.setCDDir();
return null != FS.LoadFile("pics/colormap.pcx");
}
void initMirrors() {
InputStream in = getClass().getResourceAsStream("/mirrors");
BufferedReader r = new BufferedReader(new InputStreamReader(in));
try {
while (true) {
String name = r.readLine();
String value = r.readLine();
if (name == null || value == null) break;
mirrorNames.add(name);
mirrorLinks.add(value);
}
} catch (Exception e) {}
finally {
try {
r.close();
} catch (Exception e1) {}
try {
in.close();
} catch (Exception e1) {}
}
}
void install(int mirrorIdx) {
final String mirrorName = mirrorNames.get(mirrorIdx);
final String mirror = mirrorLinks.get(mirrorIdx);
InputStream in = null;
OutputStream out = null;
File outFile = null;
setStatus("downloading from "+mirrorName+": <"+mirror+">");
File dir = null;
try {
dir = new File(dataDir);
dir.mkdirs();
}
catch (Exception e) {}
try {
if (!dir.isDirectory() || !dir.canWrite()) {
setStatus("can't write to " + dataDir);
return;
}
}
catch (Exception e) {
setStatus(e.getMessage());
return;
}
try {
URL url = new URL(mirror);
URLConnection conn = url.openConnection();
// int length = conn.getContentLength();
in = conn.getInputStream();
outFile = File.createTempFile("Jake2Data", ".zip");
outFile.deleteOnExit();
out = new FileOutputStream(outFile);
copyStream(in, out);
} catch (Exception e) {
setStatus(e.getMessage());
return;
} finally {
try {
in.close();
} catch (Exception e) {}
try {
out.close();
} catch (Exception e) {}
}
try {
installData(outFile.getCanonicalPath());
} catch (Exception e) {
setStatus(e.getMessage());
return;
}
try {
if (outFile != null) outFile.delete();
} catch (Exception e) {}
setStatus("installation successful from "+mirrorName+": <"+mirror+">");
}
void installData(String filename) throws Exception {
InputStream in = null;
OutputStream out = null;
try {
ZipFile f = new ZipFile(filename);
Enumeration<? extends ZipEntry> e = f.entries();
while (e.hasMoreElements()) {
ZipEntry entry = (ZipEntry)e.nextElement();
String name = entry.getName();
int i;
if ((i = name.indexOf("/baseq2")) > -1 && name.indexOf(".dll") == -1) {
name = dataDir + name.substring(i);
File outFile = new File(name);
if (entry.isDirectory()) {
outFile.mkdirs();
} else {
setStatus("installing " + outFile.getName());
outFile.getParentFile().mkdirs();
out = new FileOutputStream(outFile);
in = f.getInputStream(entry);
copyStream(in, out);
}
}
}
} catch (Exception e) {
throw e;
} finally {
try {in.close();} catch (Exception e1) {}
try {out.close();} catch (Exception e1) {}
}
}
void copyStream(InputStream in, OutputStream out) throws Exception {
try {
// int c = 0;
int l;
while ((l = in.read(buf)) > 0) {
out.write(buf, 0, l);
// c += l;
}
} catch (Exception e) {
throw e;
} finally {
try {
in.close();
} catch (Exception e) {}
try {
out.close();
} catch (Exception e) {}
}
}
}
|