Skip to content

Commit

Permalink
cleanup and minor fix
Browse files Browse the repository at this point in the history
the files werent being deleted at the end, I fixed that, and some of the print statements were off.
  • Loading branch information
Devan-Kerman committed Oct 10, 2019
1 parent eeccaf0 commit 27e543e
Showing 1 changed file with 38 additions and 20 deletions.
58 changes: 38 additions & 20 deletions src/main/java/net/epicorp/PatchMC.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

import java.io.*;
import java.nio.file.FileSystemException;
import java.nio.file.Path;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;

public class PatchMC {
Expand All @@ -13,7 +17,7 @@ public static void main(String[] args) throws IOException, InterruptedException
System.out.println("Decompiler: https://github.com/hube12/DecompilerMC (Neil#4879)");
System.out.println("Patch generator: http://gnuwin32.sourceforge.net/packages/diffutils.htm");

if(args.length < 3) {
if (args.length < 3) {
args = new String[3];
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the initial version: ");
Expand All @@ -26,53 +30,66 @@ public static void main(String[] args) throws IOException, InterruptedException
args[2] = scanner.nextLine();
}

System.out.printf("Generating %s from version %s to %s...\n", args[2], args[0], args[1]);

System.out.println("Unpacking decompiler...");
unloadResource("decomp1", "decompile/MCDecompiler.exe", "decompile/lib/cfr-0.146.jar", "decompile/lib/fernflower.jar", "decompile/lib/SpecialSource-1.8.6.jar");
unloadResource("decomp2", "decompile/MCDecompiler.exe", "decompile/lib/cfr-0.146.jar", "decompile/lib/fernflower.jar", "decompile/lib/SpecialSource-1.8.6.jar");


System.out.println("Unpacking decompiler...");
System.out.println("Decompiling...");
Runtime runtime = Runtime.getRuntime();
Process init = decompile(runtime, args[0], new File("decomp1/decompile/MCDecompiler.exe"));
Process targ = decompile(runtime, args[1], new File("decomp2/decompile/MCDecompiler.exe"));

System.out.println("Decompiling...");
System.out.println("Wait several minutes...");
System.out.println("This may take several minutes...");
long start = System.currentTimeMillis();
init.waitFor();
write(init, "INITIAL_DECOMPILE");
targ.waitFor();
write(targ, "TARGET_DECOMPILE");
System.out.printf("Decompiled server jars in %dms!\n", System.currentTimeMillis()-start);

System.out.printf("Decompiled server jars in %dms!\n", System.currentTimeMillis() - start);

System.out.println("Unloading diff...");
unloadResource("temp", "diff/diff.exe", "diff/libiconv2.dll", "diff/libintl3.dll");

System.out.println("Generating patches...");
ProcessBuilder process = genPatches(new File(args[2]), args[0], args[1]);
process.start();

System.out.println("Cleaning up...");
fileList
.stream()
.map(File::toPath)
.sorted(Comparator.reverseOrder())
.map(Path::toFile)
.filter(f -> !f.delete())
.forEach(f -> System.out.printf("Error deleting %s!\n", f));

System.out.println("Done.");
}

public static void write(Process process, String name) {
System.out.printf("====== %s STD_OUT ======\n", name);
new BufferedReader(new InputStreamReader(process.getInputStream())).lines().forEach(System.out::println);
StringBuilder stdout = new StringBuilder();
new BufferedReader(new InputStreamReader(process.getInputStream())).lines().peek(l -> stdout.ensureCapacity(stdout.length() + l.length() + 2)).peek(l -> stdout.append('\t')).peek(stdout::append).peek(l -> stdout.append('\n'));
System.out.println(stdout);
System.out.printf("====== %s STD_OUT ======\n", name);

System.out.printf("====== %s ERR_OUT ======\n", name);
new BufferedReader(new InputStreamReader(process.getErrorStream())).lines().forEach(System.out::println);
StringBuilder errout = new StringBuilder();
new BufferedReader(new InputStreamReader(process.getErrorStream())).lines().peek(l -> errout.ensureCapacity(errout.length() + l.length() + 2)).peek(l -> errout.append('\t')).peek(errout::append).peek(l -> errout.append('\n'));
System.out.println(errout);
System.out.printf("====== %s ERR_OUT ======\n", name);
}


public static ProcessBuilder genPatches(File output, String ver1, String ver2) throws IOException {
return new ProcessBuilder()
.command(new File("temp/diff/diff.exe").getAbsolutePath(),
"-Nur", "\""+new File("decomp1/decompile/src/"+ver1).getAbsolutePath()+"\"",
"\""+new File("decomp2/decompile/src/"+ver2).getAbsolutePath()+"\"")
.redirectOutput(output);
public static ProcessBuilder genPatches(File output, String ver1, String ver2) {
return new ProcessBuilder().command(new File("temp/diff/diff.exe").getAbsolutePath(), "-Nur", "\"" + new File("decomp1/decompile/src/" + ver1).getAbsolutePath() + "\"", "\"" + new File("decomp2/decompile/src/" + ver2).getAbsolutePath() + "\"").redirectOutput(output);
}

public static Process decompile(Runtime runtime, String version, File decompiler) throws IOException {
Process process = runtime.exec(decompiler+"", null, decompiler.getParentFile());
Process process = runtime.exec(decompiler + "", null, decompiler.getParentFile());
PrintWriter stream = new PrintWriter(process.getOutputStream(), true);
stream.println();
stream.println();
Expand All @@ -84,16 +101,17 @@ public static Process decompile(Runtime runtime, String version, File decompiler
}

private static final byte[] BUFFER = new byte[1024];
public static void unloadResource(String dir, String...resources) throws IOException {
private static List<File> fileList = new LinkedList<>();

public static void unloadResource(String dir, String... resources) throws IOException {
File file = new File(dir);
if(!file.mkdirs())
throw new RuntimeException(new FileSystemException("Some shit went down with " + dir));
if (!file.mkdirs()) throw new RuntimeException(new FileSystemException("Some shit went down with " + dir));

for (String resource : resources) {
int read;
File unload = new File(dir, resource);
File parent = unload.getParentFile();
if(!parent.exists()) parent.mkdirs();
if (!parent.exists()) parent.mkdirs();

BufferedInputStream input = new BufferedInputStream(ClassLoader.getSystemResourceAsStream(resource));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(unload));
Expand All @@ -104,6 +122,6 @@ public static void unloadResource(String dir, String...resources) throws IOExcep
input.close();
}

file.deleteOnExit();
fileList.add(file);
}
}

0 comments on commit 27e543e

Please sign in to comment.