summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorcflip <cflip@cflip.net>2023-08-24 19:57:23 -0600
committercflip <cflip@cflip.net>2023-08-24 19:57:23 -0600
commite884c5e307580cc6a2a1bd3e1293f00e3929679e (patch)
tree0ea4982c6d8c398379f158f2d6737c9982a712db /src
parent1a1e124db2bd5e6d71ecb5fcaba4c26e576d70ea (diff)
Clean up GameUpdater and the files it creates in bin/
Diffstat (limited to 'src')
-rw-r--r--src/net/minecraft/GameUpdater.java296
1 files changed, 92 insertions, 204 deletions
diff --git a/src/net/minecraft/GameUpdater.java b/src/net/minecraft/GameUpdater.java
index 1b02053..d69c2b3 100644
--- a/src/net/minecraft/GameUpdater.java
+++ b/src/net/minecraft/GameUpdater.java
@@ -12,7 +12,6 @@ import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
-import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.HttpURLConnection;
@@ -23,6 +22,7 @@ import java.net.URLConnection;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
+import java.nio.file.StandardCopyOption;
import java.security.AccessControlException;
import java.security.AccessController;
import java.security.CodeSource;
@@ -31,8 +31,6 @@ import java.security.PrivilegedExceptionAction;
import java.security.SecureClassLoader;
import java.util.Enumeration;
import java.util.Vector;
-import java.util.jar.JarOutputStream;
-import java.util.jar.Pack200;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
@@ -48,12 +46,16 @@ public class GameUpdater implements Runnable {
public static final int STATE_INITIALIZE_REAL_APPLET = 8;
public static final int STATE_START_REAL_APPLET = 9;
public static final int STATE_DONE = 10;
+
public int percentage;
public int currentSizeDownload;
public int totalSizeDownload;
public int currentSizeExtract;
public int totalSizeExtract;
- protected URL[] urlList;
+
+ protected URL librariesUrl;
+ protected URL nativesUrl;
+
private static ClassLoader classLoader;
protected Thread loaderThread;
protected Thread animationThread;
@@ -62,9 +64,6 @@ public class GameUpdater implements Runnable {
protected String subtaskMessage = "";
protected int state = 1;
- protected boolean lzmaSupported = false;
-
- protected boolean pack200Supported = false;
protected String[] genericErrorMessage = new String[]{"An error occured while loading the applet.", "Please contact support to resolve this issue.", "<placeholder for error message>"};
protected boolean certificateRefused;
@@ -81,19 +80,6 @@ public class GameUpdater implements Runnable {
public void init() {
this.state = STATE_INIT;
- try {
- Class.forName("LZMA.LzmaInputStream");
- this.lzmaSupported = true;
- } catch (Throwable throwable) {
- }
-
-
- try {
- Pack200.class.getSimpleName();
- this.pack200Supported = true;
- } catch (Throwable throwable) {
- }
-
if (!latestVersion.hasFullInfo()) {
try {
latestVersion.downloadInfo();
@@ -138,27 +124,14 @@ public class GameUpdater implements Runnable {
return "unknown state";
}
-
- protected String trimExtensionByCapabilities(String file) {
- if (!this.pack200Supported) {
- file = file.replaceAll(".pack", "");
- }
-
- if (!this.lzmaSupported) {
- file = file.replaceAll(".lzma", "");
- }
- return file;
- }
-
protected void loadJarURLs() throws Exception {
this.state = STATE_DETERMINING_PACKAGES;
URL path = new URL("http://files.betacraft.uk/launcher/assets/");
- this.urlList = new URL[3];
String osName = System.getProperty("os.name");
- String librariesZip = "libs.zip";
- String nativesZip = null;
+ String librariesZip;
+ String nativesZip;
if (osName.startsWith("Win")) {
librariesZip = "libs-windows.zip";
@@ -174,14 +147,8 @@ public class GameUpdater implements Runnable {
return;
}
- if (nativesZip == null) {
- fatalErrorOccured("no lwjgl natives files found", null);
- } else {
- this.urlList[0] = new URL(path, nativesZip);
- }
-
- this.urlList[1] = new URL(path, librariesZip);
- this.urlList[2] = latestVersion.gameJarUrl;
+ nativesUrl = new URL(path, nativesZip);
+ librariesUrl = new URL(path, librariesZip);
}
@@ -194,14 +161,9 @@ public class GameUpdater implements Runnable {
try {
loadJarURLs();
- String path = AccessController.doPrivileged(new PrivilegedExceptionAction<String>() {
- public String run() throws Exception {
- return Util.getWorkingDirectory() + File.separator + "bin" + File.separator;
- }
- });
+ String path = AccessController.doPrivileged((PrivilegedExceptionAction<String>) () -> Util.getWorkingDirectory() + File.separator + "bin" + File.separator);
File dir = new File(path);
-
if (!dir.exists()) {
dir.mkdirs();
}
@@ -210,13 +172,11 @@ public class GameUpdater implements Runnable {
File versionFile = new File(dir, "version");
boolean cacheAvailable = false;
- if (versionFile.exists() && (
- this.latestVersion.equals("-1") || latestVersion.isSameVersion(versionFile))) {
+ if (versionFile.exists() && (this.latestVersion.equals("-1") || latestVersion.isSameVersion(versionFile))) {
cacheAvailable = true;
this.percentage = 90;
}
-
if (!cacheAvailable) {
downloadJars(path);
extractJars(path);
@@ -262,7 +222,6 @@ public class GameUpdater implements Runnable {
protected PermissionCollection getPermissions(CodeSource codesource) {
PermissionCollection perms = null;
-
try {
Method method = SecureClassLoader.class.getDeclaredMethod("getPermissions", CodeSource.class);
method.setAccessible(true);
@@ -329,107 +288,97 @@ public class GameUpdater implements Runnable {
return appletClass.newInstance();
}
-
- protected void downloadJars(String path) throws Exception {
- this.state = STATE_DOWNLOADING;
-
- int[] fileSizes = new int[this.urlList.length];
-
-
- for (int i = 0; i < this.urlList.length; i++) {
- System.out.println(this.urlList[i]);
- URLConnection urlconnection = this.urlList[i].openConnection();
- urlconnection.setDefaultUseCaches(false);
- if (urlconnection instanceof HttpURLConnection) {
- ((HttpURLConnection) urlconnection).setRequestMethod("HEAD");
- }
- fileSizes[i] = urlconnection.getContentLength();
- this.totalSizeDownload += fileSizes[i];
+ protected int getRemoteFileSize(URL remoteFileUrl) throws IOException {
+ System.out.println(remoteFileUrl);
+ URLConnection urlconnection = remoteFileUrl.openConnection();
+ urlconnection.setDefaultUseCaches(false);
+ if (urlconnection instanceof HttpURLConnection) {
+ ((HttpURLConnection) urlconnection).setRequestMethod("HEAD");
}
+ return urlconnection.getContentLength();
+ }
- int initialPercentage = this.percentage = 10;
-
-
+ protected void downloadRemoteFile(URL remoteFileUrl, String path, String fileName, int expectedSize, int initialPercentage) throws Exception {
byte[] buffer = new byte[65536];
- for (int j = 0; j < this.urlList.length; j++) {
- int unsuccessfulAttempts = 0;
- int maxUnsuccessfulAttempts = 3;
- boolean downloadFile = true;
+ int unsuccessfulAttempts = 0;
+ int maxUnsuccessfulAttempts = 3;
+ boolean downloadFile = true;
+ while (downloadFile) {
+ downloadFile = false;
- while (downloadFile) {
- downloadFile = false;
+ URLConnection urlconnection = remoteFileUrl.openConnection();
+ if (urlconnection instanceof HttpURLConnection) {
+ urlconnection.setRequestProperty("Cache-Control", "no-cache");
+ urlconnection.connect();
+ }
- URLConnection urlconnection = this.urlList[j].openConnection();
+ String currentFile = getFileName(remoteFileUrl);
+ InputStream inputstream = getJarInputStream(currentFile, urlconnection);
+ FileOutputStream fos = new FileOutputStream(path + fileName);
- if (urlconnection instanceof HttpURLConnection) {
- urlconnection.setRequestProperty("Cache-Control", "no-cache");
- urlconnection.connect();
+ long downloadStartTime = System.currentTimeMillis();
+ int downloadedAmount = 0;
+ int fileSize = 0;
+ String downloadSpeedMessage = "";
+ int bufferSize;
+ while ((bufferSize = inputstream.read(buffer, 0, buffer.length)) != -1) {
+ fos.write(buffer, 0, bufferSize);
+ this.currentSizeDownload += bufferSize;
+ fileSize += bufferSize;
+ this.percentage = initialPercentage + this.currentSizeDownload * 45 / this.totalSizeDownload;
+ this.subtaskMessage = "Retrieving: " + currentFile + " " + (this.currentSizeDownload * 100 / this.totalSizeDownload) + "%";
+
+ downloadedAmount += bufferSize;
+
+ long timeLapse = System.currentTimeMillis() - downloadStartTime;
+ if (timeLapse >= 1000L) {
+ float downloadSpeed = downloadedAmount / (float) timeLapse;
+ downloadSpeed = (int) (downloadSpeed * 100.0F) / 100.0F;
+ downloadSpeedMessage = " @ " + downloadSpeed + " KB/sec";
+ downloadedAmount = 0;
+ downloadStartTime += 1000L;
}
- String currentFile = getFileName(this.urlList[j]);
- InputStream inputstream = getJarInputStream(currentFile, urlconnection);
- FileOutputStream fos = new FileOutputStream(path + currentFile);
-
-
- long downloadStartTime = System.currentTimeMillis();
- int downloadedAmount = 0;
- int fileSize = 0;
- String downloadSpeedMessage = "";
- int bufferSize;
- while ((bufferSize = inputstream.read(buffer, 0, buffer.length)) != -1) {
- fos.write(buffer, 0, bufferSize);
- this.currentSizeDownload += bufferSize;
- fileSize += bufferSize;
- this.percentage = initialPercentage + this.currentSizeDownload * 45 / this.totalSizeDownload;
- this.subtaskMessage = "Retrieving: " + currentFile + " " + (this.currentSizeDownload * 100 / this.totalSizeDownload) + "%";
-
- downloadedAmount += bufferSize;
- long timeLapse = System.currentTimeMillis() - downloadStartTime;
-
- if (timeLapse >= 1000L) {
-
- float downloadSpeed = downloadedAmount / (float) timeLapse;
-
- downloadSpeed = (int) (downloadSpeed * 100.0F) / 100.0F;
+ this.subtaskMessage = this.subtaskMessage + downloadSpeedMessage;
+ }
- downloadSpeedMessage = " @ " + downloadSpeed + " KB/sec";
+ inputstream.close();
+ fos.close();
- downloadedAmount = 0;
- downloadStartTime += 1000L;
+ if (urlconnection instanceof HttpURLConnection && fileSize != expectedSize) {
+ if (expectedSize > 0) {
+ unsuccessfulAttempts++;
+ if (unsuccessfulAttempts < maxUnsuccessfulAttempts) {
+ downloadFile = true;
+ this.currentSizeDownload -= fileSize;
+ continue;
}
-
- this.subtaskMessage = this.subtaskMessage + downloadSpeedMessage;
+ throw new Exception("failed to download " + currentFile);
}
+ }
+ }
+ }
- inputstream.close();
- fos.close();
-
+ protected void downloadJars(String path) throws Exception {
+ this.state = STATE_DOWNLOADING;
- if (urlconnection instanceof HttpURLConnection &&
- fileSize != fileSizes[j]) {
- if (fileSizes[j] > 0) {
+ int gameJarSize = getRemoteFileSize(latestVersion.gameJarUrl);
+ int librariesSize = getRemoteFileSize(librariesUrl);
+ int nativesSize = getRemoteFileSize(nativesUrl);
+ totalSizeDownload += gameJarSize + librariesSize + nativesSize;
+ int initialPercentage = this.percentage = 10;
- unsuccessfulAttempts++;
-
- if (unsuccessfulAttempts < maxUnsuccessfulAttempts) {
- downloadFile = true;
- this.currentSizeDownload -= fileSize;
- continue;
- }
- throw new Exception("failed to download " + currentFile);
- }
- }
- }
- }
+ downloadRemoteFile(latestVersion.gameJarUrl, path, "minecraft.jar", gameJarSize, initialPercentage);
+ downloadRemoteFile(librariesUrl, path, getFileName(librariesUrl), librariesSize, initialPercentage);
+ downloadRemoteFile(nativesUrl, path, getFileName(nativesUrl), nativesSize, initialPercentage);
this.subtaskMessage = "";
}
-
protected InputStream getJarInputStream(String currentFile, final URLConnection urlconnection) throws Exception {
final InputStream[] is = new InputStream[1];
@@ -478,52 +427,7 @@ public class GameUpdater implements Runnable {
return is[0];
}
-
- protected void extractLZMA(String in, String out) throws Exception {
- File f = new File(in);
- FileInputStream fileInputHandle = new FileInputStream(f);
-
-
- Class<?> clazz = Class.forName("LZMA.LzmaInputStream");
- Constructor<?> constructor = clazz.getDeclaredConstructor(InputStream.class);
- InputStream inputHandle = (InputStream) constructor.newInstance(new Object[]{fileInputHandle});
-
-
- OutputStream outputHandle = new FileOutputStream(out);
-
- byte[] buffer = new byte[16384];
-
- int ret = inputHandle.read(buffer);
- while (ret >= 1) {
- outputHandle.write(buffer, 0, ret);
- ret = inputHandle.read(buffer);
- }
-
- inputHandle.close();
- outputHandle.close();
-
- outputHandle = null;
- inputHandle = null;
-
-
- f.delete();
- }
-
-
- protected void extractPack(String in, String out) throws Exception {
- File f = new File(in);
- FileOutputStream fostream = new FileOutputStream(out);
- JarOutputStream jostream = new JarOutputStream(fostream);
-
- Pack200.Unpacker unpacker = Pack200.newUnpacker();
- unpacker.unpack(f, jostream);
- jostream.close();
-
-
- f.delete();
- }
-
- protected void extractZIP(String in, String out) throws Exception {
+ protected void extractZIP(String in) throws Exception {
File originalFile = new File(in);
Path targetDir = originalFile.toPath().getParent();
ZipInputStream zipStream = new ZipInputStream(Files.newInputStream(originalFile.toPath()));
@@ -538,46 +442,30 @@ public class GameUpdater implements Runnable {
Files.createDirectories(resolvedPath);
} else {
Files.createDirectories(resolvedPath.getParent());
- Files.copy(zipStream, resolvedPath);
+ Files.copy(zipStream, resolvedPath, StandardCopyOption.REPLACE_EXISTING);
}
}
+
+ originalFile.delete();
}
protected void extractJars(String path) throws Exception {
this.state = STATE_EXTRACTING_PACKAGES;
+ this.percentage = 65;
- float increment = 10.0F / this.urlList.length;
-
- for (int i = 0; i < this.urlList.length; i++) {
- this.percentage = 55 + (int) (increment * (i + 1));
- String filename = getFileName(this.urlList[i]);
-
- if (filename.endsWith(".pack.lzma")) {
- this.subtaskMessage = "Extracting: " + filename + " to " + filename.replaceAll(".lzma", "");
- extractLZMA(path + filename, path + filename.replaceAll(".lzma", ""));
-
- this.subtaskMessage = "Extracting: " + filename.replaceAll(".lzma", "") + " to " + filename.replaceAll(".pack.lzma", "");
- extractPack(path + filename.replaceAll(".lzma", ""), path + filename.replaceAll(".pack.lzma", ""));
- } else if (filename.endsWith(".pack")) {
- this.subtaskMessage = "Extracting: " + filename + " to " + filename.replace(".pack", "");
- extractPack(path + filename, path + filename.replace(".pack", ""));
- } else if (filename.endsWith(".lzma")) {
- this.subtaskMessage = "Extracting: " + filename + " to " + filename.replace(".lzma", "");
- extractLZMA(path + filename, path + filename.replace(".lzma", ""));
- } else if (filename.endsWith(".zip")) {
- this.subtaskMessage = "Extracting: " + filename + " to " + filename.replace(".zip", "");
- // TODO: This should only operate on the libraries zip
- extractZIP(path + filename, path + filename.replace(".zip", ""));
- }
- }
- }
+ // We only need to extract the library jars from the zip archive
+ String filename = getFileName(librariesUrl);
+ this.subtaskMessage = "Extracting: " + filename;
+
+ extractZIP(path + filename);
+ }
protected void extractNatives(String path) throws Exception {
this.state = STATE_EXTRACTING_PACKAGES;
int initialPercentage = this.percentage;
- String nativeJar = getJarName(this.urlList[0]);
+ String nativeJar = getJarName(nativesUrl);
/* TODO: There should still be some kind of validation probably
Certificate[] certificate = Launcher.class.getProtectionDomain().getCodeSource().getCertificates();