Reconstructed too many codes
This commit is contained in:
@@ -46,9 +46,9 @@ public class ContentGetAndShowTask extends HTTPGetTask implements Event<String>
|
||||
|
||||
@Override
|
||||
public boolean call(Object sender, String value) {
|
||||
LogWindow.instance.clean();
|
||||
LogWindow.instance.log(value);
|
||||
LogWindow.instance.setVisible(true);
|
||||
LogWindow.INSTANCE.clean();
|
||||
LogWindow.INSTANCE.log(value);
|
||||
LogWindow.INSTANCE.setVisible(true);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,11 +17,11 @@
|
||||
*/
|
||||
package org.jackhuang.hellominecraft.utils;
|
||||
|
||||
import org.jackhuang.hellominecraft.utils.functions.Consumer;
|
||||
import org.jackhuang.hellominecraft.utils.functions.Predicate;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
import rx.Observable;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -29,17 +29,9 @@ import java.util.Iterator;
|
||||
*/
|
||||
public final class CollectionUtils {
|
||||
|
||||
public static <T> void forEach(Collection<T> coll, Consumer<T> p) {
|
||||
for (T t : coll)
|
||||
p.accept(t);
|
||||
}
|
||||
|
||||
public static <T> ArrayList<T> map(Collection<T> coll, Predicate<T> p) {
|
||||
ArrayList<T> newColl = new ArrayList<>();
|
||||
forEach(coll, t -> {
|
||||
if (p.apply(t))
|
||||
newColl.add(t);
|
||||
});
|
||||
Observable.from(coll).filter(p).subscribe(newColl::add);
|
||||
return newColl;
|
||||
}
|
||||
|
||||
|
||||
@@ -29,6 +29,8 @@ import java.net.URL;
|
||||
import java.util.Map;
|
||||
import org.jackhuang.hellominecraft.HMCLog;
|
||||
import org.jackhuang.hellominecraft.utils.system.IOUtils;
|
||||
import rx.Observable;
|
||||
import rx.subscriptions.Subscriptions;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -156,4 +158,15 @@ public final class NetUtils {
|
||||
throw new IllegalArgumentException("Could not concatenate given URL with GET arguments!", ex);
|
||||
}
|
||||
}
|
||||
|
||||
public static Observable<String> getRx(String url) {
|
||||
return Observable.create(t1 -> {
|
||||
try {
|
||||
t1.onNext(get(url));
|
||||
} catch(Exception e) {
|
||||
t1.onError(e);
|
||||
}
|
||||
return Subscriptions.empty();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,6 +21,7 @@ import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import org.jackhuang.hellominecraft.C;
|
||||
import org.jackhuang.hellominecraft.HMCLog;
|
||||
|
||||
/**
|
||||
@@ -40,6 +41,14 @@ public class Java {
|
||||
return name;
|
||||
}
|
||||
|
||||
public String getLocalizedName() {
|
||||
if (name.equals("Default"))
|
||||
return C.i18n("settings.default");
|
||||
if (name.equals("Custom"))
|
||||
return C.i18n("settings.custom");
|
||||
return name;
|
||||
}
|
||||
|
||||
public String getHome() {
|
||||
return home;
|
||||
}
|
||||
|
||||
@@ -17,7 +17,14 @@
|
||||
*/
|
||||
package org.jackhuang.hellominecraft.version;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipFile;
|
||||
import org.jackhuang.hellominecraft.C;
|
||||
import org.jackhuang.hellominecraft.HMCLog;
|
||||
import org.jackhuang.hellominecraft.utils.ArrayUtils;
|
||||
import org.jackhuang.hellominecraft.utils.NetUtils;
|
||||
|
||||
/**
|
||||
* @author huangyuhui
|
||||
@@ -59,4 +66,127 @@ public class MinecraftVersionRequest {
|
||||
}
|
||||
return text;
|
||||
}
|
||||
|
||||
private static int lessThan32(byte[] b, int x) {
|
||||
for (; x < b.length; x++)
|
||||
if (b[x] < 32)
|
||||
return x;
|
||||
return -1;
|
||||
}
|
||||
|
||||
private static MinecraftVersionRequest getVersionOfOldMinecraft(ZipFile file, ZipEntry entry) throws IOException {
|
||||
MinecraftVersionRequest r = new MinecraftVersionRequest();
|
||||
byte[] tmp = NetUtils.getBytesFromStream(file.getInputStream(entry));
|
||||
|
||||
byte[] bytes = "Minecraft Minecraft ".getBytes("ASCII");
|
||||
int j;
|
||||
if ((j = ArrayUtils.matchArray(tmp, bytes)) < 0) {
|
||||
r.type = MinecraftVersionRequest.UNKOWN;
|
||||
return r;
|
||||
}
|
||||
int i = j + bytes.length;
|
||||
|
||||
if ((j = lessThan32(tmp, i)) < 0) {
|
||||
r.type = MinecraftVersionRequest.UNKOWN;
|
||||
return r;
|
||||
}
|
||||
String ver = new String(tmp, i, j - i, "ASCII");
|
||||
r.version = ver;
|
||||
|
||||
r.type = file.getEntry("META-INF/MANIFEST.MF") == null
|
||||
? MinecraftVersionRequest.MODIFIED : MinecraftVersionRequest.OK;
|
||||
return r;
|
||||
}
|
||||
|
||||
private static MinecraftVersionRequest getVersionOfNewMinecraft(ZipFile file, ZipEntry entry) throws IOException {
|
||||
MinecraftVersionRequest r = new MinecraftVersionRequest();
|
||||
byte[] tmp = NetUtils.getBytesFromStream(file.getInputStream(entry));
|
||||
|
||||
byte[] str = "-server.txt".getBytes("ASCII");
|
||||
int j = ArrayUtils.matchArray(tmp, str);
|
||||
if (j < 0) {
|
||||
r.type = MinecraftVersionRequest.UNKOWN;
|
||||
return r;
|
||||
}
|
||||
int i = j + str.length;
|
||||
i += 11;
|
||||
j = lessThan32(tmp, i);
|
||||
if (j < 0) {
|
||||
r.type = MinecraftVersionRequest.UNKOWN;
|
||||
return r;
|
||||
}
|
||||
r.version = new String(tmp, i, j - i, "ASCII");
|
||||
|
||||
char ch = r.version.charAt(0);
|
||||
// 1.8.1+
|
||||
if (ch < '0' || ch > '9') {
|
||||
str = "Can't keep up! Did the system time change, or is the server overloaded?".getBytes("ASCII");
|
||||
j = ArrayUtils.matchArray(tmp, str);
|
||||
if (j < 0) {
|
||||
r.type = MinecraftVersionRequest.UNKOWN;
|
||||
return r;
|
||||
}
|
||||
i = -1;
|
||||
while (j > 0) {
|
||||
if (tmp[j] >= 48 && tmp[j] <= 57) {
|
||||
i = j;
|
||||
break;
|
||||
}
|
||||
j--;
|
||||
}
|
||||
if (i == -1) {
|
||||
r.type = MinecraftVersionRequest.UNKOWN;
|
||||
return r;
|
||||
}
|
||||
int k = i;
|
||||
while (tmp[k] >= 48 && tmp[k] <= 57 || tmp[k] == 46)
|
||||
k--;
|
||||
k++;
|
||||
r.version = new String(tmp, k, i - k + 1);
|
||||
}
|
||||
r.type = file.getEntry("META-INF/MANIFEST.MF") == null
|
||||
? MinecraftVersionRequest.MODIFIED : MinecraftVersionRequest.OK;
|
||||
return r;
|
||||
}
|
||||
|
||||
public static MinecraftVersionRequest minecraftVersion(File file) {
|
||||
MinecraftVersionRequest r = new MinecraftVersionRequest();
|
||||
if (!file.exists()) {
|
||||
r.type = MinecraftVersionRequest.NOT_FOUND;
|
||||
return r;
|
||||
}
|
||||
if (!file.isFile()) {
|
||||
r.type = MinecraftVersionRequest.NOT_FILE;
|
||||
return r;
|
||||
}
|
||||
if (!file.canRead()) {
|
||||
r.type = MinecraftVersionRequest.UNREADABLE;
|
||||
return r;
|
||||
}
|
||||
ZipFile localZipFile = null;
|
||||
try {
|
||||
localZipFile = new ZipFile(file);
|
||||
ZipEntry minecraft = localZipFile
|
||||
.getEntry("net/minecraft/client/Minecraft.class");
|
||||
if (minecraft != null)
|
||||
return getVersionOfOldMinecraft(localZipFile, minecraft);
|
||||
ZipEntry main = localZipFile.getEntry("net/minecraft/client/main/Main.class");
|
||||
ZipEntry minecraftserver = localZipFile.getEntry("net/minecraft/server/MinecraftServer.class");
|
||||
if ((main != null) && (minecraftserver != null))
|
||||
return getVersionOfNewMinecraft(localZipFile, minecraftserver);
|
||||
r.type = MinecraftVersionRequest.INVALID;
|
||||
return r;
|
||||
} catch (IOException localException) {
|
||||
HMCLog.warn("Zip file is invalid", localException);
|
||||
r.type = MinecraftVersionRequest.INVALID_JAR;
|
||||
return r;
|
||||
} finally {
|
||||
if (localZipFile != null)
|
||||
try {
|
||||
localZipFile.close();
|
||||
} catch (IOException ex) {
|
||||
HMCLog.warn("Failed to close zip file", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -57,7 +57,7 @@ public class LogWindow extends javax.swing.JFrame {
|
||||
System.setErr(new LauncherPrintStream(err));
|
||||
}
|
||||
|
||||
public static LogWindow instance = new LogWindow();
|
||||
public static final LogWindow INSTANCE = new LogWindow();
|
||||
|
||||
/**
|
||||
* This method is called from within the constructor to initialize the form.
|
||||
|
||||
Reference in New Issue
Block a user