Skip to content

Improve Load&Save using Apache Ant #78

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -199,5 +199,11 @@
<artifactId>commons-cli</artifactId>
<version>1.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.ant/ant -->
<dependency>
<groupId>org.apache.ant</groupId>
<artifactId>ant</artifactId>
<version>1.10.5</version>
</dependency>
</dependencies>
</project>
</project>
228 changes: 120 additions & 108 deletions src/main/java/me/grax/jbytemod/utils/task/LoadTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import javax.swing.SwingWorker;

import org.apache.commons.io.IOUtils;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.objectweb.asm.tree.ClassNode;

import me.grax.jbytemod.JByteMod;
Expand All @@ -24,120 +26,130 @@

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;
private ZipFile 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);
}
}
public LoadTask(JByteMod jbm, File input, JarArchive ja) {
try {
this.jarSize = countFiles(this.input = new ZipFile(input, "UTF-8"));
JByteMod.LOGGER.log(jarSize + " files to load!");
this.jbm = jbm;
this.jpb = jbm.getPP();
this.ja = ja;
// clean old cache
// ja.setClasses(null);
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;
}
@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;
}
public int countFiles(final ZipFile zipFile) {
final Enumeration<ZipEntry> entries = zipFile.getEntries();
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[]>();
/**
* loads both classes and other files at the same time
*/
public void loadFiles(ZipFile 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;
}
final Enumeration<ZipEntry> entries = jar.getEntries();
while (entries.hasMoreElements()) {
readJar(jar, entries.nextElement(), 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")) {
byte[] bytes = IOUtils.toByteArray(jis);
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)
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;
}
private void readJar(ZipFile jar, ZipEntry zipEntry, Map<String, ClassNode> classes,
Map<String, byte[]> otherFiles) {
long ms = System.currentTimeMillis();
publish((int) (((float) loaded++ / (float) jarSize) * 100f));
String name = zipEntry.getName();
try (InputStream jis = jar.getInputStream(zipEntry)) {
if (name.endsWith(".class") && !zipEntry.isDirectory()) {
synchronized (classes) {
byte[] bytes = IOUtils.toByteArray(jis);
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)
classes.put(cn.name, cn);
}
} catch (Exception e) {
e.printStackTrace();
JByteMod.LOGGER.err("Failed loading class file " + name);
}
}
}
} else {
synchronized (otherFiles) {
if (!zipEntry.isDirectory()) {
byte[] bytes = IOUtils.toByteArray(jis);
otherFiles.put(name, bytes);
} else {
otherFiles.put(name, new byte[0]);
}
}
}

@Override
protected void process(List<Integer> chunks) {
int i = chunks.get(chunks.size() - 1);
jpb.setValue(i);
super.process(chunks);
}
if (memoryWarning) {
long timeDif = System.currentTimeMillis() - ms;
if (timeDif > 60 * 3 * 1000 && Runtime.getRuntime().totalMemory() / (double) maxMem > 0.95) { // if
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 done() {
JByteMod.LOGGER.log("Successfully loaded file!");
jbm.refreshTree();
}
@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();
}
}
Loading