-
Notifications
You must be signed in to change notification settings - Fork 149
Expand file tree
/
Copy pathLoadTask.java
More file actions
146 lines (132 loc) · 4.27 KB
/
LoadTask.java
File metadata and controls
146 lines (132 loc) · 4.27 KB
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
package me.grax.jbytemod.utils.task;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.stream.Stream;
import javax.swing.SwingWorker;
import org.apache.commons.io.IOUtils;
import org.objectweb.asm.tree.ClassNode;
import me.grax.jbytemod.JByteMod;
import me.grax.jbytemod.JarArchive;
import me.grax.jbytemod.ui.PageEndPanel;
import me.grax.jbytemod.utils.ErrorDisplay;
import me.lpk.util.ASMUtils;
public class LoadTask extends SwingWorker<Void, Integer> {
private JarFile input;
private PageEndPanel jpb;
private JByteMod jbm;
private int jarSize; // including directories
private int loaded;
private JarArchive ja;
private long maxMem;
private boolean memoryWarning;
public LoadTask(JByteMod jbm, File input, JarArchive ja) {
try {
this.jarSize = countFiles(this.input = new JarFile(input, false));
JByteMod.LOGGER.log(jarSize + " files to load!");
this.jbm = jbm;
this.jpb = jbm.getPP();
this.ja = ja;
this.maxMem = Runtime.getRuntime().maxMemory();
this.memoryWarning = JByteMod.ops.get("memory_warning").getBoolean();
} catch (IOException e) {
new ErrorDisplay(e);
}
}
@Override
protected Void doInBackground() throws Exception {
publish(0);
this.loadFiles(input);
publish(100);
return null;
}
public int countFiles(final JarFile zipFile) {
final Enumeration<? extends JarEntry> entries = zipFile.entries();
int c = 0;
while (entries.hasMoreElements()) {
entries.nextElement();
++c;
}
return c;
}
/**
* loads both classes and other files at the same time
*/
public void loadFiles(JarFile jar) throws IOException {
long mem = Runtime.getRuntime().totalMemory();
if (mem / (double) maxMem > 0.75) {
JByteMod.LOGGER.warn("Memory usage is high: " + Math.round((mem / (double) maxMem * 100d)) + "%");
}
System.gc();
Map<String, ClassNode> classes = new HashMap<String, ClassNode>();
Map<String, byte[]> otherFiles = new HashMap<String, byte[]>();
Stream<JarEntry> str = jar.stream();
str.forEach(z -> readJar(jar, z, classes, otherFiles));
jar.close();
ja.setClasses(classes);
ja.setOutput(otherFiles);
return;
}
private void readJar(JarFile jar, JarEntry en, Map<String, ClassNode> classes, Map<String, byte[]> otherFiles) {
long ms = System.currentTimeMillis();
publish((int) (((float) loaded++ / (float) jarSize) * 100f));
String name = en.getName();
try (InputStream jis = jar.getInputStream(en)) {
if (name.endsWith(".class") && en.getSize() > 3) {
byte[] bytes = IOUtils.toByteArray(jis);
if (bytes.length < 4)
return;
String cafebabe = String.format("%02X%02X%02X%02X", bytes[0], bytes[1], bytes[2], bytes[3]);
if (cafebabe.toLowerCase().equals("cafebabe")) {
try {
final ClassNode cn = ASMUtils.getNode(bytes);
if (cn != null) { // && (cn.name.equals("java/lang/Object") ? true : cn.superName != null)
cn.innerPath = en.getName();
classes.put(cn.name, cn);
}
} catch (Exception e) {
e.printStackTrace();
JByteMod.LOGGER.err("Failed loading class file " + name);
}
}
} else {
if (!en.isDirectory()) {
byte[] bytes = IOUtils.toByteArray(jis);
otherFiles.put(name, bytes);
} else {
otherFiles.put(name, new byte[0]);
}
}
if (memoryWarning) {
long timeDif = System.currentTimeMillis() - ms;
if (timeDif > 200 && Runtime.getRuntime().totalMemory() / (double) maxMem > 0.95) { // if loading class takes more than 200ms and memory is full stop
JByteMod.LOGGER.logNotification(JByteMod.res.getResource("memory_full"));
publish(100);
this.cancel(true);
return;
}
}
} catch (Exception e) {
e.printStackTrace();
JByteMod.LOGGER.err("Failed loading file");
}
return;
}
@Override
protected void process(List<Integer> chunks) {
int i = chunks.get(chunks.size() - 1);
jpb.setValue(i);
super.process(chunks);
}
@Override
protected void done() {
JByteMod.LOGGER.log("Successfully loaded file!");
jbm.refreshTree();
}
}