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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
|
package NET.worlds.network;
import NET.worlds.console.Gamma;
import NET.worlds.console.Main;
import NET.worlds.console.MainCallback;
import NET.worlds.console.MainTerminalCallback;
import NET.worlds.core.IniFile;
import NET.worlds.core.Std;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OptionalDataException;
import java.io.Serializable;
import java.util.Collection;
import java.util.Date;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Vector;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
public class Cache implements MainCallback, MainTerminalCallback, Serializable {
private static final long serialVersionUID = -7557149391688293661L;
private static final long CACHE_VERSION = 0L;
private static String CACHE_DIR = Gamma.earlyURLUnalias("home:cachedir/").replace('/', '\\');
static final int CACHE_MIN_CHANGE = IniFile.gamma().getIniInt("NetCacheMinChange", 50);
static final long CACHE_MAX_DELAY = IniFile.gamma().getIniInt("NetCacheMaxDelay", 5) * 1000L * 60L;
private Date lastCacheSave = new Date();
static transient Cache cache = initLoad();
private transient Hashtable<Object, CacheEntry> table = new Hashtable<Object, CacheEntry>();
private transient CacheEntry terminator = new CacheEntry();
transient long totalBytes;
transient int hasChanged;
private int nextAvailable = 1;
public static void ClearCustomAvatars() {
Collection<CacheEntry> c = cache.table.values();
Iterator<CacheEntry> it = c.iterator();
Vector<CacheEntry> tmp = new Vector<CacheEntry>();
while (it.hasNext()) {
CacheEntry ce = it.next();
if (ce != null && ce.url.toString().contains("custom")) {
tmp.add(ce);
}
}
for (int i = 0; i < tmp.size(); i++) {
removeEntry(tmp.get(i));
}
}
static Cache initLoad() {
System.out.println("Initializing cache...");
Cache c = new Cache();
FileInputStream in = null;
ObjectInputStream s = null;
try {
System.out.println("Restoring cache...");
File fi = new File(CACHE_DIR + "cache.index");
if (!fi.exists()) {
fi = new File(CACHE_DIR + "cache.index.new");
if (!fi.exists()) {
fi = new File(CACHE_DIR + "cache.index.old");
}
}
in = new FileInputStream(fi);
s = new ObjectInputStream(in);
long version = s.readLong();
if (version != 0L) {
throw new Exception("Wrong version of cache.index");
}
c = (Cache)s.readObject();
c.terminator = new CacheEntry();
c.table = new Hashtable<Object, CacheEntry>();
Object obj;
while ((obj = s.readObject()) != null && obj instanceof CacheEntry) {
if (((CacheEntry)obj).url != null && ((CacheEntry)obj).localName != null && ((CacheEntry)obj).state >= 7) {
c.add((CacheEntry)obj);
}
}
s.close();
s = null;
in.close();
in = null;
System.out.println("Marking cache as closed...");
} catch (OptionalDataException var22) {
} catch (Exception var23) {
System.out.println(var23);
System.out.println("Flushing cache index.");
File findex = new File(CACHE_DIR + "cache.index");
findex.mkdirs();
findex.delete();
} finally {
if (s != null) {
try {
s.close();
} catch (Exception var21) {
}
ObjectInputStream var26 = null;
}
if (in != null) {
try {
in.close();
} catch (Exception var20) {
}
FileInputStream var25 = null;
}
}
String[] names = new File(CACHE_DIR).list();
Hashtable<String, String> files = new Hashtable<String, String>();
if (names != null) {
for (int i = 0; i < names.length; i++) {
files.put((CACHE_DIR + names[i]).toUpperCase(), "");
}
files.remove((CACHE_DIR + "cache.index").toUpperCase());
}
c.totalBytes = 0L;
c.hasChanged = 0;
CacheEntry p = c.terminator.next;
while (p != c.terminator) {
CacheEntry e = p;
p = p.next;
String name = e.localName.toUpperCase();
boolean isFile = files.get(name) != null;
files.remove(name);
c.totalBytes = c.totalBytes + e.bytes;
if (!e.done() || !isFile) {
c.remove(e);
if (isFile) {
new File(name).delete();
}
}
}
Enumeration<String> e = files.keys();
while (e.hasMoreElements()) {
new File(e.nextElement()).delete();
}
File f = new File("./avatars.zip");
if (f.exists()) {
cache = c;
InjectZipFile(f, "rel:avatar:");
}
c.lastCacheSave = new Date();
Main.register(c);
return c;
}
public static CacheFile getFile(URL url, boolean forceRecheck) {
return cache.getAFile(url, forceRecheck);
}
public static CacheFile getFile(URL url) {
return cache.getAFile(url, false);
}
public static CacheFile getFile(String url) {
return cache.getAFile(URL.make(url), false);
}
public static CacheEntry getEntry(URL url) {
return cache.get(url);
}
public static void removeEntry(CacheEntry ce) {
cache.remove(ce);
}
public static void InjectZipFile(File zipFile, String urlPrefix) {
long timeStamp = zipFile.lastModified();
try {
ZipFile zf = new ZipFile(zipFile);
System.out.println("Adding " + zf.size() + " entries from " + zipFile);
Enumeration<?> e = zf.entries();
while (e.hasMoreElements()) {
ZipEntry ze = (ZipEntry)e.nextElement();
InputStream is = zf.getInputStream(ze);
URL remoteName = URL.make(urlPrefix + ze.getName());
CacheEntry ce = cache.get(remoteName);
if (ce == null) {
String localName = assignLocalName(remoteName);
FileOutputStream fos = new FileOutputStream(localName);
byte[] buffer = new byte[4096];
while (true) {
try {
int bytesRead = is.read(buffer);
if (bytesRead == -1) {
break;
}
fos.write(buffer, 0, bytesRead);
} catch (IOException var14) {
break;
}
}
fos.close();
ce = new CacheEntry();
ce.localName = new String(localName);
ce.url = remoteName;
ce.state = 4;
ce.remoteTime = timeStamp;
ce.checkTime = new Date();
cache.add(ce);
}
is.close();
}
zf.close();
} catch (Exception var15) {
System.out.println("Error processing cache zip file: " + var15);
}
}
private synchronized CacheFile getAFile(URL url, boolean forceRecheck) {
CacheEntry e = null;
if (url.isRemote()) {
e = cache.get(url);
if (e == null) {
e = new CacheEntry(url);
cache.add(e);
} else if (forceRecheck) {
e.forceRecheck();
}
}
return new CacheFile(url, e);
}
private Cache() {
}
@Override
public void mainCallback() {
}
public synchronized void resyncIndex() {
if (this.hasChanged >= CACHE_MIN_CHANGE || this.lastCacheSave.before(new Date(new Date().getTime() - CACHE_MAX_DELAY))) {
this.saveIndex();
System.gc();
}
}
public void saveIndex() {
try {
System.out.println("Marking cache as open...");
File fin = new File(CACHE_DIR + "cache.index.new");
FileOutputStream o = new FileOutputStream(fin);
ObjectOutputStream s = new ObjectOutputStream(o);
s.writeLong(0L);
s.writeObject(this);
for (CacheEntry e = this.terminator.next; e != this.terminator; e = e.next) {
if (e.url != null && e.localName != null) {
s.writeObject(e);
}
}
s.writeInt(0);
s.flush();
s.close();
o.close();
this.lastCacheSave = new Date();
this.hasChanged = 0;
System.out.println("Marking cache as closed...");
File fi = new File(CACHE_DIR + "cache.index");
File fio = new File(CACHE_DIR + "cache.index.old");
fi.renameTo(fio);
fin.renameTo(fi);
fio.delete();
} catch (Exception var6) {
System.out.println("Error writing cache index: " + var6);
}
}
@Override
public void terminalCallback() {
Main.unregister(this);
this.saveIndex();
}
static String assignLocalName(URL url) {
String remoteName = url.unalias();
String ext = ".temp";
int lastDot = remoteName.lastIndexOf(46);
if (lastDot > remoteName.lastIndexOf(47) && remoteName.indexOf("?", lastDot) < 0 && remoteName.indexOf("#", lastDot) < 0) {
ext = remoteName.substring(lastDot);
}
return url.endsWith(".gr2") ? CACHE_DIR + url.getBase() : CACHE_DIR + Integer.toString(cache.nextAvailable++, 36) + ext;
}
public synchronized void add(CacheEntry e) {
this.table.put(e.url, e);
CacheEntry p = this.terminator.prev;
p.next = e;
e.prev = p;
e.next = this.terminator;
this.terminator.prev = e;
this.hasChanged++;
}
public int numEntries() {
return this.table.size();
}
public synchronized CacheEntry get(URL url) {
CacheEntry e = this.table.get(url);
if (e != null) {
this.markUsed(e);
}
return e;
}
private void markUsed(CacheEntry e) {
if (e != null && e.next != this.terminator) {
e.prev.next = e.next;
e.next.prev = e.prev;
this.terminator.prev.next = e;
e.prev = this.terminator.prev;
e.next = this.terminator;
this.terminator.prev = e;
}
}
public void remove(CacheEntry e) {
this.totalBytes = this.totalBytes - e.bytes;
e.prev.next = e.next;
e.next.prev = e.prev;
e.prev = null;
e.next = null;
this.table.remove(e.url);
this.hasChanged++;
new File(e.localName.toUpperCase()).delete();
}
private CacheEntry freeLRU() {
for (CacheEntry e = this.terminator.next; e != this.terminator; e = e.next) {
if (!e.inUse()) {
this.remove(e);
return e;
}
}
return null;
}
public synchronized void makeSpaceFor(int incomingSize) {
while (cache.numEntries() > CacheEntry.CACHE_MAX_ENTRIES && this.freeLRU() != null) {
}
while ((this.totalBytes + 2L * incomingSize) / 1024L > Std.GetDiskFreeSpace() && Std.GetDiskFreeSpace() > -1L) {
CacheEntry freed = this.freeLRU();
if (freed == null) {
break;
}
}
}
}
|