Skip to content

Commit

Permalink
Implement #906
Browse files Browse the repository at this point in the history
Signed-off-by: Ross Allan <rallanpcl@gmail.com>
  • Loading branch information
LunNova committed Jul 17, 2013
1 parent 627ba4a commit 8d95daa
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 5 deletions.
7 changes: 5 additions & 2 deletions src/common/nallar/tickthreading/minecraft/TickThreading.java
Expand Up @@ -135,8 +135,11 @@ public TickThreading() {
"\nTo patch your server, simply run the PATCHME.bat/sh file in your server directory" +
"\n\nAlso, make a full backup of your server if you haven't already!" +
"\n\nFiles checked for patches: " + CollectionsUtil.join(filesToCheck));
Log.flush();
Runtime.getRuntime().halt(1);
MinecraftServer.getServer().initiateShutdown();
if (!System.getProperty("os.name").startsWith("Windows")) {
PatchUtil.startPatch();
}
Runtime.getRuntime().exit(1);
}
ContextAccess.$.getContext(0);
}
Expand Down
47 changes: 44 additions & 3 deletions src/common/nallar/tickthreading/util/PatchUtil.java
@@ -1,7 +1,11 @@
package nallar.tickthreading.util;

import java.io.File;
import java.io.FileDescriptor;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Scanner;
Expand All @@ -20,18 +24,21 @@ public enum PatchUtil {
;
private static boolean written = false;

private static String getClassPath() {
return LocationUtil.locationOf(TickThreading.class).toString() + File.pathSeparator + new File(LocationUtil.getServerDirectory(), "lib/guava-14.0-rc3.jar") + File.pathSeparator + new File(LocationUtil.getServerDirectory(), "lib/asm-all-4.1.jar");
}

public static synchronized void writePatchRunners() throws IOException {
if (written) {
return;
}
written = true;
String java = System.getProperties().getProperty("java.home") + File.separator + "bin" + File.separator + "java";
String CP = LocationUtil.locationOf(TickThreading.class).toString();
String CP = getClassPath();
String MS = CollectionsUtil.join(LocationUtil.getJarLocations());

ZipFile zipFile = new ZipFile(new File(CP));
ZipFile zipFile = new ZipFile(new File(LocationUtil.locationOf(TickThreading.class).toString()));
try {
CP += File.pathSeparator + new File(LocationUtil.getServerDirectory(), "lib/guava-14.0-rc3.jar") + File.pathSeparator + new File(LocationUtil.getServerDirectory(), "lib/asm-all-4.1.jar");
for (ZipEntry zipEntry : new IterableEnumerationWrapper<ZipEntry>((Enumeration<ZipEntry>) zipFile.entries())) {
if (zipEntry.getName().startsWith("patchrun/") && !(!zipEntry.getName().isEmpty() && zipEntry.getName().charAt(zipEntry.getName().length() - 1) == '/')) {
String data = new Scanner(zipFile.getInputStream(zipEntry), "UTF-8").useDelimiter("\\A").next();
Expand Down Expand Up @@ -62,4 +69,38 @@ public static boolean shouldPatch(Iterable<File> files) {
}
return false;
}

@SuppressWarnings ("IOResourceOpenedButNotSafelyClosed")
public static void startPatch() {
String classPath = getClassPath();
String separator = System.getProperty("file.separator");
String path = System.getProperty("java.home")
+ separator + "bin" + separator + "java";
ProcessBuilder processBuilder = new ProcessBuilder(path, "-Dunattend=true", "-cp", classPath, PatchMain.class.getCanonicalName(), "patcher", CollectionsUtil.join(LocationUtil.getJarLocations()));
Process p;
try {
p = processBuilder.start();
} catch (IOException e) {
Log.severe("Failed to start patcher", e);
return;
}
inheritIO(p.getInputStream(), new PrintStream(new FileOutputStream(FileDescriptor.out)));
inheritIO(p.getErrorStream(), new PrintStream(new FileOutputStream(FileDescriptor.err)));
try {
p.waitFor();
} catch (InterruptedException ignored) {
}
}

private static void inheritIO(final InputStream src, final PrintStream dst) {
new Thread(new Runnable() {
@Override
public void run() {
Scanner sc = new Scanner(src);
while (sc.hasNextLine()) {
dst.println(sc.nextLine());
}
}
}).start();
}
}

0 comments on commit 8d95daa

Please sign in to comment.