Reconstruct the codes.
This commit is contained in:
@@ -25,34 +25,35 @@ import java.util.ResourceBundle;
|
||||
* @author huangyuhui
|
||||
*/
|
||||
public final class C {
|
||||
|
||||
public static final Gson gsonPrettyPrinting = new GsonBuilder().setPrettyPrinting().create();
|
||||
public static final Gson gson = new Gson();
|
||||
|
||||
|
||||
public static final ResourceBundle I18N = ResourceBundle.getBundle("org/jackhuang/hellominecraft/launcher/I18N");
|
||||
|
||||
|
||||
//http://repo1.maven.org/maven2
|
||||
|
||||
public static final String URL_PUBLISH = "http://www.mcbbs.net/thread-142335-1-1.html";
|
||||
public static final String URL_TIEBA = "http://tieba.baidu.com/f?kw=hellominecraftlauncher";
|
||||
public static final String URL_GITHUB = "https://github.com/huanghongxun/HMCL/issues";
|
||||
public static final String URL_MINECRAFTFORUM = "http://www.minecraftforum.net/forums/mapping-and-modding/minecraft-tools/1265720-hello-minecraft-launcher-1-9-3-mc-1-7-4-auto";
|
||||
|
||||
|
||||
public static final String FILE_MINECRAFT_VERSIONS = "versions";
|
||||
|
||||
|
||||
public static final String LINE_SEPARATOR = System.getProperty("line.separator");
|
||||
|
||||
|
||||
public static final String URL_FORGE_LIST = "http://files.minecraftforge.net/maven/net/minecraftforge/forge/json";
|
||||
public static final String URL_LITELOADER_LIST = "http://dl.liteloader.com/versions/versions.json";
|
||||
|
||||
private C(){}
|
||||
|
||||
|
||||
private C() {
|
||||
}
|
||||
|
||||
public static String i18n(String a, Object... format) {
|
||||
try {
|
||||
return String.format(C.I18N.getString(a), format);
|
||||
} catch(Exception e) {
|
||||
} catch (Exception e) {
|
||||
HMCLog.warn("Failed to read localization lang: " + a, e);
|
||||
return a;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -18,33 +18,32 @@ package org.jackhuang.hellominecraft;
|
||||
|
||||
import org.jackhuang.hellominecraft.logging.logger.Logger;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @author huangyuhui
|
||||
*/
|
||||
public class HMCLog {
|
||||
|
||||
|
||||
public static Logger logger = new Logger("HMC");
|
||||
|
||||
|
||||
public static void log(String message) {
|
||||
logger.info(message);
|
||||
}
|
||||
|
||||
|
||||
public static void warn(String message) {
|
||||
logger.warn(message);
|
||||
}
|
||||
|
||||
|
||||
public static void warn(String msg, Throwable t) {
|
||||
logger.warn(msg, t);
|
||||
}
|
||||
|
||||
|
||||
public static void err(String msg) {
|
||||
logger.error(msg);
|
||||
}
|
||||
|
||||
|
||||
public static void err(String msg, Throwable t) {
|
||||
logger.error(msg, t);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -21,19 +21,25 @@ package org.jackhuang.hellominecraft.tasks;
|
||||
* @author huangyuhui
|
||||
*/
|
||||
public interface DoingDoneListener<K> {
|
||||
|
||||
/**
|
||||
* Task done.
|
||||
* @param k
|
||||
*
|
||||
* @param k
|
||||
*/
|
||||
void onDone(K k);
|
||||
|
||||
/**
|
||||
* Before task executing.
|
||||
* @param k
|
||||
*
|
||||
* @param k
|
||||
*/
|
||||
void onDoing(K k);
|
||||
|
||||
/**
|
||||
* Task failed.
|
||||
* @param k
|
||||
*
|
||||
* @param k
|
||||
*/
|
||||
void onFailed(K k);
|
||||
}
|
||||
|
||||
@@ -24,6 +24,7 @@ import java.util.HashSet;
|
||||
* @author huangyuhui
|
||||
*/
|
||||
public class ParallelTask extends Task {
|
||||
|
||||
Collection<Task> dependsTask = new HashSet<>();
|
||||
|
||||
@Override
|
||||
@@ -40,9 +41,9 @@ public class ParallelTask extends Task {
|
||||
public Collection<Task> getDependTasks() {
|
||||
return dependsTask;
|
||||
}
|
||||
|
||||
|
||||
public void addDependsTask(Task t) {
|
||||
dependsTask.add(t);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -21,8 +21,9 @@ package org.jackhuang.hellominecraft.tasks;
|
||||
* @author huangyuhui
|
||||
*/
|
||||
public abstract class ProgressProvider {
|
||||
|
||||
protected ProgressProviderListener ppl;
|
||||
|
||||
|
||||
public ProgressProvider setProgressProviderListener(ProgressProviderListener p) {
|
||||
ppl = p;
|
||||
return this;
|
||||
|
||||
@@ -21,7 +21,10 @@ package org.jackhuang.hellominecraft.tasks;
|
||||
* @author huangyuhui
|
||||
*/
|
||||
public interface ProgressProviderListener {
|
||||
|
||||
void setProgress(int prog, int max);
|
||||
|
||||
void setStatus(String sta);
|
||||
|
||||
void onProgressProviderDone();
|
||||
}
|
||||
|
||||
@@ -23,28 +23,36 @@ import java.util.Collection;
|
||||
* @author huangyuhui
|
||||
*/
|
||||
public abstract class Task extends ProgressProvider {
|
||||
|
||||
|
||||
/**
|
||||
* Run in a new thread(packed in TaskList).
|
||||
*
|
||||
* @return is task finished sucessfully.
|
||||
*/
|
||||
public abstract boolean executeTask();
|
||||
|
||||
|
||||
/**
|
||||
* if this func returns false, TaskList will force abort the thread.
|
||||
* run in main thread.
|
||||
* if this func returns false, TaskList will force abort the thread. run in
|
||||
* main thread.
|
||||
*
|
||||
* @return is aborted.
|
||||
*/
|
||||
public boolean abort() { return false; }
|
||||
|
||||
public Throwable getFailReason() { return failReason; }
|
||||
public boolean abort() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public Throwable getFailReason() {
|
||||
return failReason;
|
||||
}
|
||||
protected Throwable failReason = null;
|
||||
|
||||
protected void setFailReason(Throwable s) {
|
||||
failReason = s;
|
||||
}
|
||||
|
||||
|
||||
protected String tag;
|
||||
protected boolean parallelExecuting;
|
||||
|
||||
public Task setTag(String tag) {
|
||||
this.tag = tag;
|
||||
return this;
|
||||
@@ -57,9 +65,14 @@ public abstract class Task extends ProgressProvider {
|
||||
public void setParallelExecuting(boolean parallelExecuting) {
|
||||
this.parallelExecuting = parallelExecuting;
|
||||
}
|
||||
|
||||
|
||||
public abstract String getInfo();
|
||||
|
||||
public Collection<Task> getDependTasks() { return null; }
|
||||
public Collection<Task> getAfterTasks() { return null; }
|
||||
|
||||
public Collection<Task> getDependTasks() {
|
||||
return null;
|
||||
}
|
||||
|
||||
public Collection<Task> getAfterTasks() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,14 +23,14 @@ package org.jackhuang.hellominecraft.tasks;
|
||||
public abstract class TaskInfo extends Task {
|
||||
|
||||
String info;
|
||||
|
||||
|
||||
public TaskInfo(String info) {
|
||||
this.info = info;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String getInfo() {
|
||||
return info;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -87,9 +87,8 @@ public class TaskList extends Thread {
|
||||
static final Set<Task> taskPool = Collections.synchronizedSet(new HashSet<Task>());
|
||||
|
||||
private void processTasks(Collection<Task> c) {
|
||||
if (c == null) {
|
||||
if (c == null)
|
||||
return;
|
||||
}
|
||||
this.totTask += c.size();
|
||||
Set<InvokeThread> runningThread = Collections.synchronizedSet(new HashSet<InvokeThread>());
|
||||
for (Task t2 : c) {
|
||||
@@ -99,46 +98,41 @@ public class TaskList extends Thread {
|
||||
runningThread.add(thread);
|
||||
thread.start();
|
||||
}
|
||||
while (!runningThread.isEmpty()) {
|
||||
while (!runningThread.isEmpty())
|
||||
try {
|
||||
if(this.isInterrupted()) return;
|
||||
if (this.isInterrupted()) return;
|
||||
Thread.sleep(1);
|
||||
} catch (InterruptedException ex) {
|
||||
HMCLog.warn("Failed to sleep task thread", ex);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
private void executeTask(Task t) {
|
||||
if (!shouldContinue || t == null) {
|
||||
if (!shouldContinue || t == null)
|
||||
return;
|
||||
}
|
||||
processTasks(t.getDependTasks());
|
||||
|
||||
HMCLog.log("Executing task: " + t.getInfo());
|
||||
for (DoingDoneListener<Task> d : taskListener) {
|
||||
for (DoingDoneListener<Task> d : taskListener)
|
||||
d.onDoing(t);
|
||||
}
|
||||
|
||||
if (t.executeTask()) {
|
||||
HMCLog.log("Task finished: " + t.getInfo());
|
||||
for (DoingDoneListener<Task> d : taskListener) {
|
||||
for (DoingDoneListener<Task> d : taskListener)
|
||||
d.onDone(t);
|
||||
}
|
||||
processTasks(t.getAfterTasks());
|
||||
} else {
|
||||
HMCLog.err("Task failed: " + t.getInfo(), t.getFailReason());
|
||||
for (DoingDoneListener<Task> d : taskListener) {
|
||||
for (DoingDoneListener<Task> d : taskListener)
|
||||
d.onFailed(t);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
Thread.currentThread().setName("TaskList");
|
||||
|
||||
|
||||
threadPool.clear();
|
||||
for (Task taskQueue1 : taskQueue)
|
||||
executeTask(taskQueue1);
|
||||
@@ -153,10 +147,10 @@ public class TaskList extends Thread {
|
||||
|
||||
public void abort() {
|
||||
shouldContinue = false;
|
||||
while(!threadPool.isEmpty())
|
||||
synchronized(threadPool) {
|
||||
while (!threadPool.isEmpty())
|
||||
synchronized (threadPool) {
|
||||
InvokeThread it = threadPool.iterator().next();
|
||||
if(!it.task.abort()) it.interrupt();
|
||||
if (!it.task.abort()) it.interrupt();
|
||||
threadPool.remove(it);
|
||||
}
|
||||
this.interrupt();
|
||||
|
||||
@@ -21,7 +21,9 @@ package org.jackhuang.hellominecraft.tasks;
|
||||
* @author huangyuhui
|
||||
*/
|
||||
public class TaskRunnable extends TaskInfo {
|
||||
|
||||
private final Runnable r;
|
||||
|
||||
public TaskRunnable(String info, Runnable r) {
|
||||
super(info);
|
||||
this.r = r;
|
||||
@@ -32,10 +34,10 @@ public class TaskRunnable extends TaskInfo {
|
||||
try {
|
||||
r.run();
|
||||
return true;
|
||||
} catch(Throwable t) {
|
||||
} catch (Throwable t) {
|
||||
setFailReason(t);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -170,7 +170,7 @@ public class TaskWindow extends javax.swing.JDialog
|
||||
}// </editor-fold>//GEN-END:initComponents
|
||||
|
||||
private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
|
||||
if(MessageBox.Show(C.i18n("operation.confirm_stop"), MessageBox.YES_NO_OPTION) == MessageBox.YES_OPTION)
|
||||
if (MessageBox.Show(C.i18n("operation.confirm_stop"), MessageBox.YES_NO_OPTION) == MessageBox.YES_OPTION)
|
||||
this.dispose();
|
||||
}//GEN-LAST:event_btnCancelActionPerformed
|
||||
|
||||
@@ -230,7 +230,7 @@ public class TaskWindow extends javax.swing.JDialog
|
||||
|
||||
@Override
|
||||
public void onFailed(Task task) {
|
||||
failReasons.add(task.getInfo() + ": " + (task.getFailReason() == null ? "No exception" : task.getFailReason().getLocalizedMessage()));
|
||||
failReasons.add(task.getInfo() + ": " + (null == task.getFailReason() ? "No exception" : task.getFailReason().getLocalizedMessage()));
|
||||
pgsTotal.setMaximum(taskList.taskCount());
|
||||
pgsTotal.setValue(pgsTotal.getValue() + 1);
|
||||
SwingUtils.replaceLast(lstDownload, task.getFailReason());
|
||||
|
||||
@@ -21,16 +21,17 @@ package org.jackhuang.hellominecraft.tasks.communication;
|
||||
* @author huangyuhui
|
||||
* @param <T> the type of result.
|
||||
*/
|
||||
public class DefaultPreviousResult<T> implements PreviousResult<T>{
|
||||
public class DefaultPreviousResult<T> implements PreviousResult<T> {
|
||||
|
||||
T a;
|
||||
|
||||
public DefaultPreviousResult(T a) {
|
||||
this.a = a;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public T getResult() {
|
||||
return a;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -27,89 +27,79 @@ import java.util.Map;
|
||||
public class ArrayUtils {
|
||||
|
||||
public static <T> boolean isEmpty(T[] array) {
|
||||
return array == null || array.length <= 0;
|
||||
return array == null || array.length <= 0;
|
||||
}
|
||||
|
||||
public static <T> boolean isNotEmpty(T[] array) {
|
||||
return !isEmpty(array);
|
||||
return !isEmpty(array);
|
||||
}
|
||||
|
||||
public static <T> boolean contains(T[] array, T objectToFind) {
|
||||
return indexOf(array, objectToFind) != -1;
|
||||
return indexOf(array, objectToFind) != -1;
|
||||
}
|
||||
|
||||
public static <T> int indexOf(T[] array, T valueToFind) {
|
||||
return indexOf(array, valueToFind, 0);
|
||||
return indexOf(array, valueToFind, 0);
|
||||
}
|
||||
|
||||
public static <T> int indexOf(T[] array, T valueToFind, int startIndex) {
|
||||
if (array == null) {
|
||||
return -1;
|
||||
}
|
||||
if (startIndex < 0) {
|
||||
startIndex = 0;
|
||||
}
|
||||
for (int i = startIndex; i < array.length; i++) {
|
||||
if (valueToFind.equals(array[i])) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
if (array == null)
|
||||
return -1;
|
||||
if (startIndex < 0)
|
||||
startIndex = 0;
|
||||
for (int i = startIndex; i < array.length; i++)
|
||||
if (valueToFind.equals(array[i]))
|
||||
return i;
|
||||
return -1;
|
||||
}
|
||||
|
||||
public static <T> int lastIndexOf(T[] array, T valueToFind) {
|
||||
return lastIndexOf(array, valueToFind, 2147483647);
|
||||
return lastIndexOf(array, valueToFind, 2147483647);
|
||||
}
|
||||
|
||||
public static <T> int lastIndexOf(T[] array, T valueToFind, int startIndex) {
|
||||
if (array == null) {
|
||||
return -1;
|
||||
}
|
||||
if (startIndex < 0) {
|
||||
return -1;
|
||||
}
|
||||
if (startIndex >= array.length) {
|
||||
startIndex = array.length - 1;
|
||||
}
|
||||
for (int i = startIndex; i >= 0; i--) {
|
||||
if (valueToFind.equals(array[i])) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
if (array == null)
|
||||
return -1;
|
||||
if (startIndex < 0)
|
||||
return -1;
|
||||
if (startIndex >= array.length)
|
||||
startIndex = array.length - 1;
|
||||
for (int i = startIndex; i >= 0; i--)
|
||||
if (valueToFind.equals(array[i]))
|
||||
return i;
|
||||
return -1;
|
||||
}
|
||||
|
||||
public static ArrayList merge(List a, List b) {
|
||||
ArrayList al = new ArrayList(a.size() + b.size());
|
||||
al.addAll(a); al.addAll(b);
|
||||
return al;
|
||||
ArrayList al = new ArrayList(a.size() + b.size());
|
||||
al.addAll(a);
|
||||
al.addAll(b);
|
||||
return al;
|
||||
}
|
||||
|
||||
|
||||
public static <K> K getEnd(K[] k) {
|
||||
if(k == null) return null;
|
||||
else return k[k.length-1];
|
||||
if (k == null) return null;
|
||||
else return k[k.length - 1];
|
||||
}
|
||||
|
||||
|
||||
public static List tryGetMapWithList(Map map, String key) {
|
||||
List l = (List)map.get(key);
|
||||
if(l == null)
|
||||
map.put(key, l = new ArrayList());
|
||||
return l;
|
||||
List l = (List) map.get(key);
|
||||
if (l == null)
|
||||
map.put(key, l = new ArrayList());
|
||||
return l;
|
||||
}
|
||||
|
||||
public static <T> int matchArray(T[] a, T[] b) {
|
||||
for (int i = 0; i < a.length - b.length; i++) {
|
||||
int j = 1;
|
||||
for (int k = 0; k < b.length; k++) {
|
||||
if (b[k].equals(a[(i + k)])) {
|
||||
if (b[k].equals(a[(i + k)]))
|
||||
continue;
|
||||
}
|
||||
j = 0;
|
||||
break;
|
||||
}
|
||||
if (j != 0) {
|
||||
if (j != 0)
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
@@ -118,15 +108,13 @@ public class ArrayUtils {
|
||||
for (int i = 0; i < a.length - b.length; i++) {
|
||||
int j = 1;
|
||||
for (int k = 0; k < b.length; k++) {
|
||||
if (b[k] == a[(i + k)]) {
|
||||
if (b[k] == a[(i + k)])
|
||||
continue;
|
||||
}
|
||||
j = 0;
|
||||
break;
|
||||
}
|
||||
if (j != 0) {
|
||||
if (j != 0)
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -27,16 +27,19 @@ import java.util.Iterator;
|
||||
* @author huangyuhui
|
||||
*/
|
||||
public final class CollectionUtils {
|
||||
|
||||
public static <T> void forEach(Collection<T> coll, Consumer<T> p) {
|
||||
for(T t : coll) p.accept(t);
|
||||
for (T t : coll) p.accept(t);
|
||||
}
|
||||
|
||||
|
||||
public static <T> Collection<T> sortOut(Collection<T> coll, Predicate<T> p) {
|
||||
ArrayList<T> newColl = new ArrayList<>();
|
||||
forEach(coll, t -> { if(p.apply(t)) newColl.add(t); });
|
||||
forEach(coll, t -> {
|
||||
if (p.apply(t)) newColl.add(t);
|
||||
});
|
||||
return newColl;
|
||||
}
|
||||
|
||||
|
||||
public static <T> boolean removeIf(Collection<T> coll, Predicate<T> p) {
|
||||
boolean removed = false;
|
||||
final Iterator<T> each = coll.iterator();
|
||||
|
||||
@@ -30,73 +30,60 @@ public class DoubleOutputStream extends OutputStream {
|
||||
private boolean c = true;
|
||||
|
||||
public DoubleOutputStream(OutputStream paramOutputStream1, OutputStream paramOutputStream2) {
|
||||
this(paramOutputStream1, paramOutputStream2, true);
|
||||
this(paramOutputStream1, paramOutputStream2, true);
|
||||
}
|
||||
|
||||
private DoubleOutputStream(OutputStream paramOutputStream1, OutputStream paramOutputStream2, boolean paramBoolean) {
|
||||
this.a = paramOutputStream1;
|
||||
this.b = paramOutputStream2;
|
||||
this.c = true;
|
||||
this.a = paramOutputStream1;
|
||||
this.b = paramOutputStream2;
|
||||
this.c = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void write(byte[] arr, int off, int len) throws IOException {
|
||||
if (this.a != null) {
|
||||
this.a.write(arr, off, len);
|
||||
}
|
||||
if (this.b != null) {
|
||||
this.b.write(arr, off, len);
|
||||
}
|
||||
if (this.c) {
|
||||
flush();
|
||||
}
|
||||
if (this.a != null)
|
||||
this.a.write(arr, off, len);
|
||||
if (this.b != null)
|
||||
this.b.write(arr, off, len);
|
||||
if (this.c)
|
||||
flush();
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void write(byte[] paramArrayOfByte) throws IOException {
|
||||
if (this.a != null) {
|
||||
this.a.write(paramArrayOfByte);
|
||||
}
|
||||
if (this.b != null) {
|
||||
this.b.write(paramArrayOfByte);
|
||||
}
|
||||
if (this.c) {
|
||||
flush();
|
||||
}
|
||||
if (this.a != null)
|
||||
this.a.write(paramArrayOfByte);
|
||||
if (this.b != null)
|
||||
this.b.write(paramArrayOfByte);
|
||||
if (this.c)
|
||||
flush();
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void write(int paramInt) throws IOException {
|
||||
if (this.a != null) {
|
||||
this.a.write(paramInt);
|
||||
}
|
||||
if (this.b != null) {
|
||||
this.b.write(paramInt);
|
||||
}
|
||||
if (this.c) {
|
||||
flush();
|
||||
}
|
||||
if (this.a != null)
|
||||
this.a.write(paramInt);
|
||||
if (this.b != null)
|
||||
this.b.write(paramInt);
|
||||
if (this.c)
|
||||
flush();
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void close() throws IOException {
|
||||
flush();
|
||||
flush();
|
||||
|
||||
if (this.a != null) {
|
||||
this.a.close();
|
||||
}
|
||||
if (this.b != null) {
|
||||
this.b.close();
|
||||
}
|
||||
if (this.a != null)
|
||||
this.a.close();
|
||||
if (this.b != null)
|
||||
this.b.close();
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void flush() throws IOException {
|
||||
if (this.a != null) {
|
||||
this.a.flush();
|
||||
}
|
||||
if (this.b != null) {
|
||||
this.b.flush();
|
||||
}
|
||||
if (this.a != null)
|
||||
this.a.flush();
|
||||
if (this.b != null)
|
||||
this.b.flush();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,5 +22,6 @@ package org.jackhuang.hellominecraft.utils;
|
||||
* @param <T> EventArgs
|
||||
*/
|
||||
public interface Event<T> {
|
||||
|
||||
boolean call(Object sender, T t);
|
||||
}
|
||||
|
||||
@@ -24,27 +24,28 @@ import java.util.HashSet;
|
||||
* @param <T> EventArgs
|
||||
*/
|
||||
public class EventHandler<T> {
|
||||
|
||||
HashSet<Event<T>> handlers;
|
||||
Object sender;
|
||||
|
||||
|
||||
public EventHandler(Object sender) {
|
||||
handlers = new HashSet<>();
|
||||
this.sender = sender;
|
||||
}
|
||||
|
||||
|
||||
public void register(Event<T> t) {
|
||||
handlers.add(t);
|
||||
}
|
||||
|
||||
|
||||
public void unregister(Event<T> t) {
|
||||
handlers.remove(t);
|
||||
}
|
||||
|
||||
|
||||
public boolean execute(T x) {
|
||||
boolean flag = true;
|
||||
for(Event<T> t : handlers)
|
||||
if(!t.call(sender, x)) flag = false;
|
||||
for (Event<T> t : handlers)
|
||||
if (!t.call(sender, x)) flag = false;
|
||||
return flag;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -30,19 +30,18 @@ public class LauncherPrintStream extends PrintStream {
|
||||
private final ArrayList<Consumer<String>> printListeners = new ArrayList<>();
|
||||
|
||||
public LauncherPrintStream(OutputStream paramOutputStream) {
|
||||
super(paramOutputStream);
|
||||
super(paramOutputStream);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void println(String paramString) {
|
||||
super.println(paramString);
|
||||
super.println(paramString);
|
||||
|
||||
for (Consumer<String> a1 : printListeners) {
|
||||
a1.accept(paramString);
|
||||
}
|
||||
for (Consumer<String> a1 : printListeners)
|
||||
a1.accept(paramString);
|
||||
}
|
||||
|
||||
public final void addPrintListener(Consumer<String> paraml) {
|
||||
this.printListeners.add(paraml);
|
||||
this.printListeners.add(paraml);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,16 +38,16 @@ public class MathUtils {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static int parseMemory(String s, int def) {
|
||||
try {
|
||||
return Integer.parseInt(s);
|
||||
} catch (Exception e) {
|
||||
int a = parseInt(s.substring(0, s.length() - 1), def);
|
||||
if(s.endsWith("g")) return a * 1024;
|
||||
else if(s.endsWith("k")) return a / 1024;
|
||||
if (s.endsWith("g")) return a * 1024;
|
||||
else if (s.endsWith("k")) return a / 1024;
|
||||
else return a;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -34,40 +34,38 @@ import org.jackhuang.hellominecraft.HMCLog;
|
||||
* @author huang
|
||||
*/
|
||||
public final class NetUtils {
|
||||
|
||||
|
||||
public static byte[] getBytesFromStream(InputStream is) throws IOException {
|
||||
ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream();
|
||||
byte[] arrayOfByte1 = new byte[1024];
|
||||
int i;
|
||||
while ((i = is.read(arrayOfByte1)) >= 0) {
|
||||
while ((i = is.read(arrayOfByte1)) >= 0)
|
||||
localByteArrayOutputStream.write(arrayOfByte1, 0, i);
|
||||
}
|
||||
is.close();
|
||||
return localByteArrayOutputStream.toByteArray();
|
||||
}
|
||||
|
||||
|
||||
public static String getStreamContent(InputStream is) throws IOException {
|
||||
return getStreamContent(is, DEFAULT_CHARSET);
|
||||
}
|
||||
|
||||
|
||||
public static String getStreamContent(InputStream is, String encoding)
|
||||
throws IOException {
|
||||
throws IOException {
|
||||
String result;
|
||||
try (BufferedReader br = new BufferedReader(new InputStreamReader(is, encoding))) {
|
||||
result = "";
|
||||
String line;
|
||||
while ((line = br.readLine()) != null) {
|
||||
while ((line = br.readLine()) != null)
|
||||
result += line + "\n";
|
||||
}
|
||||
}
|
||||
if(result.length() < 1) return "";
|
||||
else return result.substring(0, result.length() - 1);
|
||||
if (result.length() < 1) return "";
|
||||
else return result.substring(0, result.length() - 1);
|
||||
}
|
||||
|
||||
|
||||
public static String doGet(String url, String encoding) throws IOException {
|
||||
return getStreamContent(new URL(url).openConnection().getInputStream());
|
||||
}
|
||||
|
||||
|
||||
public static String doGet(String url) throws IOException {
|
||||
return doGet(url, DEFAULT_CHARSET);
|
||||
}
|
||||
@@ -85,16 +83,15 @@ public final class NetUtils {
|
||||
public static String sendGetRequest(String endpoint,
|
||||
String requestParameters) {
|
||||
String result = null;
|
||||
if (endpoint.startsWith("http://")) {
|
||||
if (endpoint.startsWith("http://"))
|
||||
// Send a GET request to the servlet
|
||||
try {
|
||||
// Construct data
|
||||
StringBuilder data = new StringBuilder();
|
||||
// Send data
|
||||
String urlStr = endpoint;
|
||||
if (requestParameters != null && requestParameters.length() > 0) {
|
||||
if (requestParameters != null && requestParameters.length() > 0)
|
||||
urlStr += "?" + requestParameters;
|
||||
}
|
||||
URL url = new URL(urlStr);
|
||||
URLConnection conn = url.openConnection();
|
||||
|
||||
@@ -110,7 +107,6 @@ public final class NetUtils {
|
||||
} catch (Exception e) {
|
||||
HMCLog.warn("Failed to send get request.", e);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -164,7 +160,7 @@ public final class NetUtils {
|
||||
}
|
||||
private static final String METHOD_POST = "POST";
|
||||
private static final String DEFAULT_CHARSET = "UTF-8";
|
||||
|
||||
|
||||
public static URL constantURL(String url) {
|
||||
try {
|
||||
return new URL(url);
|
||||
@@ -173,15 +169,14 @@ public final class NetUtils {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static URL concatenateURL(URL url, String query) {
|
||||
try {
|
||||
if ((url.getQuery() != null) && (url.getQuery().length() > 0)) {
|
||||
return new URL(url.getProtocol(), url.getHost(), url.getPort(), new StringBuilder().append(url.getFile()).append("&").append(query).toString());
|
||||
}
|
||||
return new URL(url.getProtocol(), url.getHost(), url.getPort(), new StringBuilder().append(url.getFile()).append("?").append(query).toString());
|
||||
} catch (MalformedURLException ex) {
|
||||
throw new IllegalArgumentException("Could not concatenate given URL with GET arguments!", ex);
|
||||
}
|
||||
try {
|
||||
if ((url.getQuery() != null) && (url.getQuery().length() > 0))
|
||||
return new URL(url.getProtocol(), url.getHost(), url.getPort(), new StringBuilder().append(url.getFile()).append("&").append(query).toString());
|
||||
return new URL(url.getProtocol(), url.getHost(), url.getPort(), new StringBuilder().append(url.getFile()).append("?").append(query).toString());
|
||||
} catch (MalformedURLException ex) {
|
||||
throw new IllegalArgumentException("Could not concatenate given URL with GET arguments!", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,6 +25,7 @@ import java.util.Map;
|
||||
* @param <V> V Type
|
||||
*/
|
||||
public class Pair<K, V> implements Map.Entry<K, V> {
|
||||
|
||||
public K key;
|
||||
public V value;
|
||||
|
||||
@@ -49,5 +50,5 @@ public class Pair<K, V> implements Map.Entry<K, V> {
|
||||
this.value = value;
|
||||
return t;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -1,39 +0,0 @@
|
||||
/*
|
||||
* Copyright 2013 huangyuhui <huanghongxun2008@126.com>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program.
|
||||
*/
|
||||
package org.jackhuang.hellominecraft.utils;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author huangyuhui
|
||||
*/
|
||||
public class ReflectUtils {
|
||||
|
||||
public static Set<? extends Class<?>> getClasses(Class c) {
|
||||
HashSet set = new HashSet();
|
||||
set.addAll(Arrays.asList(c.getInterfaces()));
|
||||
while(c != Object.class) {
|
||||
set.add(c);
|
||||
c = c.getSuperclass();
|
||||
}
|
||||
return set;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -32,6 +32,14 @@ import org.jackhuang.hellominecraft.HMCLog;
|
||||
*/
|
||||
public class SwingUtils {
|
||||
|
||||
/**
|
||||
* Make DefaultTableModel by overriding getColumnClass and isCellEditable of DefaultTableModel.
|
||||
*
|
||||
* @param titleA The title of each column.
|
||||
* @param typesA The type of each column value.
|
||||
* @param canEditA Is column editable?
|
||||
* @return
|
||||
*/
|
||||
public static DefaultTableModel makeDefaultTableModel(String[] titleA, final Class[] typesA, final boolean[] canEditA) {
|
||||
return new javax.swing.table.DefaultTableModel(
|
||||
new Object[][]{},
|
||||
@@ -51,6 +59,10 @@ public class SwingUtils {
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Open URL by java.awt.Desktop
|
||||
* @param link
|
||||
*/
|
||||
public static void openLink(URI link) {
|
||||
try {
|
||||
java.awt.Desktop.getDesktop().browse(link);
|
||||
@@ -58,40 +70,61 @@ public class SwingUtils {
|
||||
HMCLog.warn("Failed to open link: " + link, e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Move the cursor to the end of TextArea.
|
||||
* @param tf the TextArea
|
||||
*/
|
||||
public static void moveEnd(JTextArea tf) {
|
||||
int position = tf.getText().length();
|
||||
tf.setCaretPosition(position);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Move the cursor to the end of ScrollPane.
|
||||
* @param pane the ScrollPane
|
||||
*/
|
||||
public static void moveEnd(JScrollPane pane) {
|
||||
JScrollBar bar = pane.getVerticalScrollBar();
|
||||
bar.setValue(bar.getMaximum());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the DefaultListModel from JList.
|
||||
* @param list
|
||||
* @return Forcely Type casted to DefaultListModel
|
||||
*/
|
||||
public static DefaultListModel getDefaultListModel(JList list) {
|
||||
return (DefaultListModel)list.getModel();
|
||||
return (DefaultListModel) list.getModel();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Append new element to JList
|
||||
* @param list the JList
|
||||
* @param element the Element
|
||||
*/
|
||||
public static void appendLast(JList list, Object element) {
|
||||
getDefaultListModel(list).addElement(element);
|
||||
}
|
||||
|
||||
|
||||
public static void replaceLast(JList list, Object element) {
|
||||
DefaultListModel model = getDefaultListModel(list);
|
||||
model.set(model.getSize()-1, element);
|
||||
model.set(model.getSize() - 1, element);
|
||||
}
|
||||
|
||||
|
||||
public static void clear(JList list) {
|
||||
list.setModel(new DefaultListModel());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Clear the JTable
|
||||
* @param table JTable with DefaultTableModel.
|
||||
*/
|
||||
public static void clearDefaultTable(JTable table) {
|
||||
DefaultTableModel model = (DefaultTableModel)table.getModel();
|
||||
while(model.getRowCount() > 0) {
|
||||
DefaultTableModel model = (DefaultTableModel) table.getModel();
|
||||
while (model.getRowCount() > 0)
|
||||
model.removeRow(0);
|
||||
}
|
||||
table.updateUI();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -26,6 +26,7 @@ import org.jackhuang.hellominecraft.HMCLog;
|
||||
* @author huangyuhui
|
||||
*/
|
||||
public final class UpdateChecker extends Thread {
|
||||
|
||||
public static boolean OUT_DATED = false;
|
||||
public VersionNumber base;
|
||||
public String type;
|
||||
@@ -33,46 +34,43 @@ public final class UpdateChecker extends Thread {
|
||||
public NonConsumer dl;
|
||||
|
||||
public UpdateChecker(VersionNumber base, String type, boolean continueUpdate, NonConsumer dl) {
|
||||
super("UpdateChecker");
|
||||
super("UpdateChecker");
|
||||
this.base = base;
|
||||
this.type = type;
|
||||
this.continueUpdate = continueUpdate;
|
||||
this.dl = dl;
|
||||
}
|
||||
|
||||
|
||||
VersionNumber value;
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
|
||||
String url = "http://huangyuhui.duapp.com/info.php?type=" + type, version;
|
||||
try {
|
||||
version = NetUtils.doGet(url);
|
||||
} catch (Exception e) {
|
||||
String url = "http://huangyuhui.duapp.com/info.php?type=" + type, version;
|
||||
try {
|
||||
version = NetUtils.doGet(url);
|
||||
} catch (Exception e) {
|
||||
HMCLog.warn("Failed to get update url.", e);
|
||||
return;
|
||||
}
|
||||
value = VersionNumber.check(version);
|
||||
if (!continueUpdate) {
|
||||
return;
|
||||
}
|
||||
return;
|
||||
}
|
||||
value = VersionNumber.check(version);
|
||||
if (!continueUpdate)
|
||||
return;
|
||||
process(false);
|
||||
}
|
||||
|
||||
|
||||
public void process(boolean showMessage) {
|
||||
if (value == null) {
|
||||
HMCLog.warn("Failed to check update...");
|
||||
if(showMessage) {
|
||||
if (value == null) {
|
||||
HMCLog.warn("Failed to check update...");
|
||||
if (showMessage)
|
||||
MessageBox.Show(C.i18n("update.failed"));
|
||||
}
|
||||
} else {
|
||||
if (VersionNumber.isOlder(base, value)) {
|
||||
} else
|
||||
if (VersionNumber.isOlder(base, value)) {
|
||||
OUT_DATED = true;
|
||||
dl.onDone();
|
||||
}
|
||||
}
|
||||
dl.onDone();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public VersionNumber getNewVersion() {
|
||||
return value;
|
||||
}
|
||||
|
||||
@@ -44,8 +44,9 @@ import org.jackhuang.hellominecraft.HMCLog;
|
||||
* @author huangyuhui
|
||||
*/
|
||||
public final class Utils {
|
||||
|
||||
|
||||
private static final GsonBuilder gsonBuilder = new GsonBuilder().setPrettyPrinting();
|
||||
|
||||
public static GsonBuilder getDefaultGsonBuilder() {
|
||||
return gsonBuilder;
|
||||
}
|
||||
@@ -53,13 +54,12 @@ public final class Utils {
|
||||
public static String[] getURL() {
|
||||
URL[] urls = ((URLClassLoader) Utils.class.getClassLoader()).getURLs();
|
||||
String[] urlStrings = new String[urls.length];
|
||||
for (int i = 0; i < urlStrings.length; i++) {
|
||||
for (int i = 0; i < urlStrings.length; i++)
|
||||
try {
|
||||
urlStrings[i] = URLDecoder.decode(urls[i].getPath(), "UTF-8");
|
||||
} catch (UnsupportedEncodingException ex) {
|
||||
HMCLog.warn("Unsupported UTF-8 encoding", ex);
|
||||
}
|
||||
}
|
||||
return urlStrings;
|
||||
}
|
||||
|
||||
@@ -79,31 +79,31 @@ public final class Utils {
|
||||
throw new IOException("Failed to get field handle to set library path");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static int getSuggestedMemorySize() {
|
||||
try {
|
||||
OperatingSystemMXBean osmb = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
|
||||
int memory = (int)(osmb.getTotalPhysicalMemorySize() / 1024 / 1024) / 4;
|
||||
memory = Math.round((float)memory/128.0f)*128;
|
||||
return memory;
|
||||
} catch(Throwable t) {
|
||||
HMCLog.warn("Failed to get total memory size, use 1024MB.", t);
|
||||
return 1024;
|
||||
}
|
||||
try {
|
||||
OperatingSystemMXBean osmb = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
|
||||
int memory = (int) (osmb.getTotalPhysicalMemorySize() / 1024 / 1024) / 4;
|
||||
memory = Math.round((float) memory / 128.0f) * 128;
|
||||
return memory;
|
||||
} catch (Throwable t) {
|
||||
HMCLog.warn("Failed to get total memory size, use 1024MB.", t);
|
||||
return 1024;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static void setClipborad(String text) {
|
||||
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(new StringSelection(text), null);
|
||||
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(new StringSelection(text), null);
|
||||
}
|
||||
|
||||
|
||||
public static boolean openLink(String url) {
|
||||
try {
|
||||
Desktop.getDesktop().browse(new URI(url));
|
||||
return true;
|
||||
} catch (URISyntaxException | IOException ex) {
|
||||
HMCLog.warn("Failed to open link:" + url, ex);
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
Desktop.getDesktop().browse(new URI(url));
|
||||
return true;
|
||||
} catch (URISyntaxException | IOException ex) {
|
||||
HMCLog.warn("Failed to open link:" + url, ex);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static void openFolder(File f) {
|
||||
@@ -112,66 +112,66 @@ public final class Utils {
|
||||
java.awt.Desktop.getDesktop().open(f);
|
||||
} catch (Exception ex) {
|
||||
MessageBox.Show(C.i18n("message.cannot_open_explorer") + ex.getMessage());
|
||||
HMCLog.warn("Failed to open folder:" + f, ex);
|
||||
HMCLog.warn("Failed to open folder:" + f, ex);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static ImageIcon scaleImage(ImageIcon i, int x, int y) {
|
||||
return new ImageIcon(i.getImage().getScaledInstance(x, y, Image.SCALE_SMOOTH));
|
||||
}
|
||||
|
||||
|
||||
public static ImageIcon searchBackgroundImage(ImageIcon background, String bgpath, int width, int height) {
|
||||
Random r = new Random();
|
||||
boolean loaded = false;
|
||||
|
||||
|
||||
// bgpath
|
||||
if (StrUtils.isNotBlank(bgpath) && !loaded) {
|
||||
String[] backgroundPath = bgpath.split(";");
|
||||
if(backgroundPath.length > 0) {
|
||||
if (StrUtils.isNotBlank(bgpath) && !loaded) {
|
||||
String[] backgroundPath = bgpath.split(";");
|
||||
if (backgroundPath.length > 0) {
|
||||
int index = r.nextInt(backgroundPath.length);
|
||||
background = new ImageIcon(Toolkit.getDefaultToolkit().getImage(backgroundPath[index]).getScaledInstance(width, height, Image.SCALE_DEFAULT));
|
||||
HMCLog.log("Prepared background image in bgpath.");
|
||||
loaded = true;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// bgskin
|
||||
if (!loaded) {
|
||||
File backgroundImageFile = new File("bg");
|
||||
if (backgroundImageFile.exists() && backgroundImageFile.isDirectory()) {
|
||||
String[] backgroundPath = backgroundImageFile.list();
|
||||
if(backgroundPath.length > 0) {
|
||||
File backgroundImageFile = new File("bg");
|
||||
if (backgroundImageFile.exists() && backgroundImageFile.isDirectory()) {
|
||||
String[] backgroundPath = backgroundImageFile.list();
|
||||
if (backgroundPath.length > 0) {
|
||||
int index = r.nextInt(backgroundPath.length);
|
||||
background = new ImageIcon(Toolkit.getDefaultToolkit().getImage("bg" + File.separator + backgroundPath[index]).getScaledInstance(width, height, Image.SCALE_DEFAULT));
|
||||
HMCLog.log("Prepared background image in bgskin folder.");
|
||||
loaded = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// background.png
|
||||
if (!loaded) {
|
||||
File backgroundImageFile = new File("background.png");
|
||||
if (backgroundImageFile.exists()) {
|
||||
loaded = true;
|
||||
background = new ImageIcon(Toolkit.getDefaultToolkit().getImage(backgroundImageFile.getAbsolutePath()).getScaledInstance(width, height, Image.SCALE_DEFAULT));
|
||||
background = new ImageIcon(Toolkit.getDefaultToolkit().getImage(backgroundImageFile.getAbsolutePath()).getScaledInstance(width, height, Image.SCALE_DEFAULT));
|
||||
HMCLog.log("Prepared background image in background.png.");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// background.jpg
|
||||
if (!loaded) {
|
||||
File backgroundImageFile = new File("background.jpg");
|
||||
if (backgroundImageFile.exists()) {
|
||||
loaded = true;
|
||||
background = new ImageIcon(Toolkit.getDefaultToolkit().getImage(backgroundImageFile.getAbsolutePath()).getScaledInstance(width, height, Image.SCALE_DEFAULT));
|
||||
//loaded = true;
|
||||
background = new ImageIcon(Toolkit.getDefaultToolkit().getImage(backgroundImageFile.getAbsolutePath()).getScaledInstance(width, height, Image.SCALE_DEFAULT));
|
||||
HMCLog.log("Prepared background image in background.jpg.");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return background;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* In order to fight against the permission manager.
|
||||
*/
|
||||
@@ -181,7 +181,7 @@ public final class Utils {
|
||||
Method exit = z.getDeclaredMethod("exit", int.class);
|
||||
exit.setAccessible(true);
|
||||
exit.invoke(z, 0);
|
||||
} catch(ClassNotFoundException | NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
|
||||
} catch (ClassNotFoundException | NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
|
||||
MessageBox.Show(C.i18n("launcher.exit_failed"));
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
@@ -27,16 +27,16 @@ public final class VersionNumber implements Comparable<VersionNumber> {
|
||||
public byte firstVer, secondVer, thirdVer;
|
||||
|
||||
public VersionNumber(byte a, byte b, byte c) {
|
||||
firstVer = a; secondVer = b; thirdVer = c;
|
||||
firstVer = a;
|
||||
secondVer = b;
|
||||
thirdVer = c;
|
||||
}
|
||||
|
||||
public static VersionNumber check(String data) {
|
||||
while (!data.isEmpty() && ((data.charAt(0) < '0' || data.charAt(0) > '9') && data.charAt(0) != '.')) {
|
||||
while (!data.isEmpty() && ((data.charAt(0) < '0' || data.charAt(0) > '9') && data.charAt(0) != '.'))
|
||||
data = data.substring(1);
|
||||
}
|
||||
if (data.isEmpty()) {
|
||||
if (data.isEmpty())
|
||||
return null;
|
||||
}
|
||||
VersionNumber ur;
|
||||
String[] ver = data.split("\\.");
|
||||
if (ver.length == 3) {
|
||||
@@ -55,24 +55,21 @@ public final class VersionNumber implements Comparable<VersionNumber> {
|
||||
}
|
||||
|
||||
public static boolean isOlder(VersionNumber a, VersionNumber b) {
|
||||
if (a.firstVer < b.firstVer) {
|
||||
if (a.firstVer < b.firstVer)
|
||||
return true;
|
||||
} else if (a.firstVer == b.firstVer) {
|
||||
if (a.secondVer < b.secondVer) {
|
||||
else if (a.firstVer == b.firstVer)
|
||||
if (a.secondVer < b.secondVer)
|
||||
return true;
|
||||
} else if (a.secondVer == b.secondVer) {
|
||||
if (a.thirdVer < b.thirdVer) {
|
||||
else if (a.secondVer == b.secondVer)
|
||||
if (a.thirdVer < b.thirdVer)
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(VersionNumber o) {
|
||||
if(isOlder(this, o)) return -1;
|
||||
else if(isOlder(o, this)) return 1;
|
||||
if (isOlder(this, o)) return -1;
|
||||
else if (isOlder(o, this)) return 1;
|
||||
else return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -16,7 +16,6 @@
|
||||
*/
|
||||
package org.jackhuang.hellominecraft.utils.code;
|
||||
|
||||
import org.jackhuang.hellominecraft.utils.code.Hex;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.security.MessageDigest;
|
||||
|
||||
@@ -20,5 +20,6 @@ package org.jackhuang.hellominecraft.utils.functions;
|
||||
* @author huangyuhui
|
||||
*/
|
||||
public interface BiConsumer<V, V2> {
|
||||
|
||||
void onDone(V value, V2 value2);
|
||||
}
|
||||
|
||||
@@ -21,5 +21,6 @@ package org.jackhuang.hellominecraft.utils.functions;
|
||||
* @author huangyuhui
|
||||
*/
|
||||
public interface BiFunction<A, B, C> {
|
||||
|
||||
C apply(A a, B b);
|
||||
}
|
||||
|
||||
@@ -21,5 +21,6 @@ package org.jackhuang.hellominecraft.utils.functions;
|
||||
* @author huangyuhui
|
||||
*/
|
||||
public interface Consumer<T> {
|
||||
|
||||
void accept(T t);
|
||||
}
|
||||
|
||||
@@ -21,5 +21,6 @@ package org.jackhuang.hellominecraft.utils.functions;
|
||||
* @author huangyuhui
|
||||
*/
|
||||
public interface Function<T, R> {
|
||||
|
||||
R apply(T t);
|
||||
}
|
||||
|
||||
@@ -21,5 +21,6 @@ package org.jackhuang.hellominecraft.utils.functions;
|
||||
* @author huangyuhui
|
||||
*/
|
||||
public interface NonConsumer {
|
||||
|
||||
void onDone();
|
||||
}
|
||||
|
||||
@@ -21,5 +21,6 @@ package org.jackhuang.hellominecraft.utils.functions;
|
||||
* @author huangyuhui
|
||||
*/
|
||||
public interface NonFunction<T> {
|
||||
|
||||
T onDone();
|
||||
}
|
||||
|
||||
@@ -21,5 +21,6 @@ package org.jackhuang.hellominecraft.utils.functions;
|
||||
* @author huangyuhui
|
||||
*/
|
||||
public interface Predicate<T> {
|
||||
|
||||
boolean apply(T t);
|
||||
}
|
||||
|
||||
@@ -21,5 +21,6 @@ package org.jackhuang.hellominecraft.utils.functions;
|
||||
* @author huangyuhui
|
||||
*/
|
||||
public interface TriConsumer<V1, V2, V3> {
|
||||
|
||||
void onDone(V1 v1, V2 v2, V3 v3);
|
||||
}
|
||||
|
||||
@@ -52,11 +52,10 @@ public class Compressor {
|
||||
BufferedOutputStream bos = new BufferedOutputStream(os);
|
||||
try (ZipOutputStream zos = new ZipOutputStream(bos)) {
|
||||
String basePath;
|
||||
if (sourceDir.isDirectory()) {
|
||||
if (sourceDir.isDirectory())
|
||||
basePath = sourceDir.getPath();
|
||||
} else {//直接压缩单个文件时,取父目录
|
||||
else//直接压缩单个文件时,取父目录
|
||||
basePath = sourceDir.getParent();
|
||||
}
|
||||
zipFile(sourceDir, basePath, zos);
|
||||
zos.closeEntry();
|
||||
}
|
||||
@@ -72,22 +71,21 @@ public class Compressor {
|
||||
private static void zipFile(File source, String basePath,
|
||||
ZipOutputStream zos) throws IOException {
|
||||
File[] files;
|
||||
if (source.isDirectory()) {
|
||||
if (source.isDirectory())
|
||||
files = source.listFiles();
|
||||
} else {
|
||||
else {
|
||||
files = new File[1];
|
||||
files[0] = source;
|
||||
}
|
||||
String pathName;//存相对路径(相对于待压缩的根目录)
|
||||
byte[] buf = new byte[1024];
|
||||
int length;
|
||||
for (File file : files) {
|
||||
for (File file : files)
|
||||
if (file.isDirectory()) {
|
||||
pathName = file.getPath().substring(basePath.length() + 1)
|
||||
+ "/";
|
||||
if (file.getName().toLowerCase().contains("meta-inf")) {
|
||||
if (file.getName().toLowerCase().contains("meta-inf"))
|
||||
continue;
|
||||
}
|
||||
zos.putNextEntry(new ZipEntry(pathName));
|
||||
zipFile(file, basePath, zos);
|
||||
} else {
|
||||
@@ -95,18 +93,16 @@ public class Compressor {
|
||||
try (InputStream is = new FileInputStream(file)) {
|
||||
BufferedInputStream bis = new BufferedInputStream(is);
|
||||
zos.putNextEntry(new ZipEntry(pathName));
|
||||
while ((length = bis.read(buf)) > 0) {
|
||||
while ((length = bis.read(buf)) > 0)
|
||||
zos.write(buf, 0, length);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void unzip(String zipFileName, String extPlace) throws IOException {
|
||||
unzip(new File(zipFileName), new File(extPlace));
|
||||
}
|
||||
|
||||
|
||||
public static void unzip(File zipFileName, File extPlace) throws IOException {
|
||||
unzip(zipFileName, extPlace, new String[0]);
|
||||
}
|
||||
@@ -129,7 +125,7 @@ public class Compressor {
|
||||
while (e.hasMoreElements()) {
|
||||
ZipEntry zipEnt = (ZipEntry) e.nextElement();
|
||||
gbkPath = zipEnt.getName();
|
||||
if(StrUtils.startsWithOne(without, gbkPath)) continue;
|
||||
if (StrUtils.startsWithOne(without, gbkPath)) continue;
|
||||
if (zipEnt.isDirectory()) {
|
||||
strtemp = strPath + File.separator + gbkPath;
|
||||
File dir = new File(strtemp);
|
||||
@@ -142,15 +138,13 @@ public class Compressor {
|
||||
strtemp = strPath + File.separator + gbkPath;
|
||||
//建目录
|
||||
String strsubdir = gbkPath;
|
||||
for (int i = 0; i < strsubdir.length(); i++) {
|
||||
for (int i = 0; i < strsubdir.length(); i++)
|
||||
if (strsubdir.substring(i, i + 1).equalsIgnoreCase("/")) {
|
||||
String temp = strPath + File.separator + strsubdir.substring(0, i);
|
||||
File subdir = new File(temp);
|
||||
if (!subdir.exists()) {
|
||||
if (!subdir.exists())
|
||||
subdir.mkdir();
|
||||
}
|
||||
}
|
||||
}
|
||||
try (FileOutputStream fos = new FileOutputStream(strtemp); BufferedOutputStream bos = new BufferedOutputStream(fos)) {
|
||||
int c;
|
||||
while ((c = bis.read()) != -1)
|
||||
|
||||
@@ -36,197 +36,177 @@ import org.jackhuang.hellominecraft.utils.NetUtils;
|
||||
public class FileUtils {
|
||||
|
||||
public static void deleteDirectory(File directory)
|
||||
throws IOException {
|
||||
if (!directory.exists()) {
|
||||
return;
|
||||
}
|
||||
throws IOException {
|
||||
if (!directory.exists())
|
||||
return;
|
||||
|
||||
if (!isSymlink(directory)) {
|
||||
cleanDirectory(directory);
|
||||
}
|
||||
if (!isSymlink(directory))
|
||||
cleanDirectory(directory);
|
||||
|
||||
if (!directory.delete()) {
|
||||
String message = "Unable to delete directory " + directory + ".";
|
||||
if (!directory.delete()) {
|
||||
String message = "Unable to delete directory " + directory + ".";
|
||||
|
||||
throw new IOException(message);
|
||||
}
|
||||
throw new IOException(message);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static boolean deleteDirectoryQuietly(File directory) {
|
||||
try {
|
||||
deleteDirectory(directory);
|
||||
return true;
|
||||
} catch(Exception e) {
|
||||
} catch (Exception e) {
|
||||
HMCLog.err("Failed to delete directory " + directory, e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static boolean cleanDirectoryQuietly(File directory) {
|
||||
try {
|
||||
cleanDirectory(directory);
|
||||
return true;
|
||||
} catch(Exception e) {
|
||||
} catch (Exception e) {
|
||||
HMCLog.err("Failed to clean directory " + directory, e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static void cleanDirectory(File directory)
|
||||
throws IOException {
|
||||
if (!directory.exists()) {
|
||||
throws IOException {
|
||||
if (!directory.exists()) {
|
||||
//String message = directory + " does not exist";
|
||||
//throw new IllegalArgumentException(message);
|
||||
//throw new IllegalArgumentException(message);
|
||||
directory.mkdirs();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (!directory.isDirectory()) {
|
||||
String message = directory + " is not a directory";
|
||||
throw new IllegalArgumentException(message);
|
||||
}
|
||||
if (!directory.isDirectory()) {
|
||||
String message = directory + " is not a directory";
|
||||
throw new IllegalArgumentException(message);
|
||||
}
|
||||
|
||||
File[] files = directory.listFiles();
|
||||
if (files == null) {
|
||||
throw new IOException("Failed to list contents of " + directory);
|
||||
}
|
||||
File[] files = directory.listFiles();
|
||||
if (files == null)
|
||||
throw new IOException("Failed to list contents of " + directory);
|
||||
|
||||
IOException exception = null;
|
||||
for (File file : files) {
|
||||
try {
|
||||
forceDelete(file);
|
||||
} catch (IOException ioe) {
|
||||
exception = ioe;
|
||||
}
|
||||
}
|
||||
IOException exception = null;
|
||||
for (File file : files)
|
||||
try {
|
||||
forceDelete(file);
|
||||
} catch (IOException ioe) {
|
||||
exception = ioe;
|
||||
}
|
||||
|
||||
if (null != exception) {
|
||||
throw exception;
|
||||
}
|
||||
if (null != exception)
|
||||
throw exception;
|
||||
}
|
||||
|
||||
public static void forceDelete(File file)
|
||||
throws IOException {
|
||||
if (file.isDirectory()) {
|
||||
deleteDirectory(file);
|
||||
} else {
|
||||
boolean filePresent = file.exists();
|
||||
if (!file.delete()) {
|
||||
if (!filePresent) {
|
||||
throw new FileNotFoundException("File does not exist: " + file);
|
||||
}
|
||||
String message = "Unable to delete file: " + file;
|
||||
throws IOException {
|
||||
if (file.isDirectory())
|
||||
deleteDirectory(file);
|
||||
else {
|
||||
boolean filePresent = file.exists();
|
||||
if (!file.delete()) {
|
||||
if (!filePresent)
|
||||
throw new FileNotFoundException("File does not exist: " + file);
|
||||
String message = "Unable to delete file: " + file;
|
||||
|
||||
throw new IOException(message);
|
||||
}
|
||||
}
|
||||
throw new IOException(message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean isSymlink(File file)
|
||||
throws IOException {
|
||||
if (file == null) {
|
||||
throw new NullPointerException("File must not be null");
|
||||
}
|
||||
if (File.separatorChar == '\\') {
|
||||
return false;
|
||||
}
|
||||
File fileInCanonicalDir;
|
||||
if (file.getParent() == null) {
|
||||
fileInCanonicalDir = file;
|
||||
} else {
|
||||
File canonicalDir = file.getParentFile().getCanonicalFile();
|
||||
fileInCanonicalDir = new File(canonicalDir, file.getName());
|
||||
}
|
||||
throws IOException {
|
||||
if (file == null)
|
||||
throw new NullPointerException("File must not be null");
|
||||
if (File.separatorChar == '\\')
|
||||
return false;
|
||||
File fileInCanonicalDir;
|
||||
if (file.getParent() == null)
|
||||
fileInCanonicalDir = file;
|
||||
else {
|
||||
File canonicalDir = file.getParentFile().getCanonicalFile();
|
||||
fileInCanonicalDir = new File(canonicalDir, file.getName());
|
||||
}
|
||||
|
||||
return !fileInCanonicalDir.getCanonicalFile().equals(fileInCanonicalDir.getAbsoluteFile());
|
||||
return !fileInCanonicalDir.getCanonicalFile().equals(fileInCanonicalDir.getAbsoluteFile());
|
||||
}
|
||||
|
||||
public static void copyDirectory(File srcDir, File destDir)
|
||||
throws IOException {
|
||||
copyDirectory(srcDir, destDir, true);
|
||||
throws IOException {
|
||||
copyDirectory(srcDir, destDir, true);
|
||||
}
|
||||
|
||||
public static void copyDirectory(File srcDir, File destDir, boolean preserveFileDate)
|
||||
throws IOException {
|
||||
copyDirectory(srcDir, destDir, null, preserveFileDate);
|
||||
throws IOException {
|
||||
copyDirectory(srcDir, destDir, null, preserveFileDate);
|
||||
}
|
||||
|
||||
public static void copyDirectory(File srcDir, File destDir, FileFilter filter)
|
||||
throws IOException {
|
||||
copyDirectory(srcDir, destDir, filter, true);
|
||||
throws IOException {
|
||||
copyDirectory(srcDir, destDir, filter, true);
|
||||
}
|
||||
|
||||
public static void copyDirectory(File srcDir, File destDir, FileFilter filter, boolean preserveFileDate)
|
||||
throws IOException {
|
||||
if (srcDir == null) {
|
||||
throw new NullPointerException("Source must not be null");
|
||||
}
|
||||
if (destDir == null) {
|
||||
throw new NullPointerException("Destination must not be null");
|
||||
}
|
||||
if (!srcDir.exists()) {
|
||||
throw new FileNotFoundException("Source '" + srcDir + "' does not exist");
|
||||
}
|
||||
if (!srcDir.isDirectory()) {
|
||||
throw new IOException("Source '" + srcDir + "' exists but is not a directory");
|
||||
}
|
||||
if (srcDir.getCanonicalPath().equals(destDir.getCanonicalPath())) {
|
||||
throw new IOException("Source '" + srcDir + "' and destination '" + destDir + "' are the same");
|
||||
}
|
||||
throws IOException {
|
||||
if (srcDir == null)
|
||||
throw new NullPointerException("Source must not be null");
|
||||
if (destDir == null)
|
||||
throw new NullPointerException("Destination must not be null");
|
||||
if (!srcDir.exists())
|
||||
throw new FileNotFoundException("Source '" + srcDir + "' does not exist");
|
||||
if (!srcDir.isDirectory())
|
||||
throw new IOException("Source '" + srcDir + "' exists but is not a directory");
|
||||
if (srcDir.getCanonicalPath().equals(destDir.getCanonicalPath()))
|
||||
throw new IOException("Source '" + srcDir + "' and destination '" + destDir + "' are the same");
|
||||
|
||||
List exclusionList = null;
|
||||
if (destDir.getCanonicalPath().startsWith(srcDir.getCanonicalPath())) {
|
||||
File[] srcFiles = filter == null ? srcDir.listFiles() : srcDir.listFiles(filter);
|
||||
if ((srcFiles != null) && (srcFiles.length > 0)) {
|
||||
exclusionList = new ArrayList(srcFiles.length);
|
||||
for (File srcFile : srcFiles) {
|
||||
File copiedFile = new File(destDir, srcFile.getName());
|
||||
exclusionList.add(copiedFile.getCanonicalPath());
|
||||
}
|
||||
}
|
||||
}
|
||||
doCopyDirectory(srcDir, destDir, filter, preserveFileDate, exclusionList);
|
||||
List exclusionList = null;
|
||||
if (destDir.getCanonicalPath().startsWith(srcDir.getCanonicalPath())) {
|
||||
File[] srcFiles = filter == null ? srcDir.listFiles() : srcDir.listFiles(filter);
|
||||
if ((srcFiles != null) && (srcFiles.length > 0)) {
|
||||
exclusionList = new ArrayList(srcFiles.length);
|
||||
for (File srcFile : srcFiles) {
|
||||
File copiedFile = new File(destDir, srcFile.getName());
|
||||
exclusionList.add(copiedFile.getCanonicalPath());
|
||||
}
|
||||
}
|
||||
}
|
||||
doCopyDirectory(srcDir, destDir, filter, preserveFileDate, exclusionList);
|
||||
}
|
||||
|
||||
private static void doCopyDirectory(File srcDir, File destDir, FileFilter filter, boolean preserveFileDate, List<String> exclusionList)
|
||||
throws IOException {
|
||||
File[] srcFiles = filter == null ? srcDir.listFiles() : srcDir.listFiles(filter);
|
||||
if (srcFiles == null) {
|
||||
throw new IOException("Failed to list contents of " + srcDir);
|
||||
}
|
||||
if (destDir.exists()) {
|
||||
if (!destDir.isDirectory()) {
|
||||
throw new IOException("Destination '" + destDir + "' exists but is not a directory");
|
||||
}
|
||||
} else if ((!destDir.mkdirs()) && (!destDir.isDirectory())) {
|
||||
throw new IOException("Destination '" + destDir + "' directory cannot be created");
|
||||
}
|
||||
throws IOException {
|
||||
File[] srcFiles = filter == null ? srcDir.listFiles() : srcDir.listFiles(filter);
|
||||
if (srcFiles == null)
|
||||
throw new IOException("Failed to list contents of " + srcDir);
|
||||
if (destDir.exists()) {
|
||||
if (!destDir.isDirectory())
|
||||
throw new IOException("Destination '" + destDir + "' exists but is not a directory");
|
||||
} else if ((!destDir.mkdirs()) && (!destDir.isDirectory()))
|
||||
throw new IOException("Destination '" + destDir + "' directory cannot be created");
|
||||
|
||||
if (!destDir.canWrite()) {
|
||||
throw new IOException("Destination '" + destDir + "' cannot be written to");
|
||||
}
|
||||
for (File srcFile : srcFiles) {
|
||||
File dstFile = new File(destDir, srcFile.getName());
|
||||
if ((exclusionList == null) || (!exclusionList.contains(srcFile.getCanonicalPath()))) {
|
||||
if (srcFile.isDirectory()) {
|
||||
doCopyDirectory(srcFile, dstFile, filter, preserveFileDate, exclusionList);
|
||||
} else {
|
||||
doCopyFile(srcFile, dstFile, preserveFileDate);
|
||||
}
|
||||
}
|
||||
if (!destDir.canWrite())
|
||||
throw new IOException("Destination '" + destDir + "' cannot be written to");
|
||||
for (File srcFile : srcFiles) {
|
||||
File dstFile = new File(destDir, srcFile.getName());
|
||||
if ((exclusionList == null) || (!exclusionList.contains(srcFile.getCanonicalPath())))
|
||||
if (srcFile.isDirectory())
|
||||
doCopyDirectory(srcFile, dstFile, filter, preserveFileDate, exclusionList);
|
||||
else
|
||||
doCopyFile(srcFile, dstFile, preserveFileDate);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
if (preserveFileDate) {
|
||||
destDir.setLastModified(srcDir.lastModified());
|
||||
}
|
||||
if (preserveFileDate)
|
||||
destDir.setLastModified(srcDir.lastModified());
|
||||
}
|
||||
|
||||
public static String readFileToString(File file)
|
||||
throws IOException {
|
||||
return NetUtils.getStreamContent(IOUtils.openInputStream(file));
|
||||
throws IOException {
|
||||
return NetUtils.getStreamContent(IOUtils.openInputStream(file));
|
||||
}
|
||||
|
||||
|
||||
public static String readFileToStringQuietly(File file) {
|
||||
try {
|
||||
return NetUtils.getStreamContent(IOUtils.openInputStream(file));
|
||||
@@ -237,239 +217,216 @@ public class FileUtils {
|
||||
}
|
||||
|
||||
public static String readFileToString(File file, String charset)
|
||||
throws IOException {
|
||||
return NetUtils.getStreamContent(IOUtils.openInputStream(file), charset);
|
||||
throws IOException {
|
||||
return NetUtils.getStreamContent(IOUtils.openInputStream(file), charset);
|
||||
}
|
||||
|
||||
|
||||
public static String readFileToStringIgnoreFileNotFound(File file) throws IOException {
|
||||
try {
|
||||
return NetUtils.getStreamContent(IOUtils.openInputStream(file));
|
||||
} catch (FileNotFoundException ex) {
|
||||
return "";
|
||||
}
|
||||
try {
|
||||
return NetUtils.getStreamContent(IOUtils.openInputStream(file));
|
||||
} catch (FileNotFoundException ex) {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
public static void copyFile(File srcFile, File destFile)
|
||||
throws IOException {
|
||||
copyFile(srcFile, destFile, true);
|
||||
throws IOException {
|
||||
copyFile(srcFile, destFile, true);
|
||||
}
|
||||
|
||||
public static void copyFile(File srcFile, File destFile, boolean preserveFileDate)
|
||||
throws IOException {
|
||||
if (srcFile == null) {
|
||||
throw new NullPointerException("Source must not be null");
|
||||
}
|
||||
if (destFile == null) {
|
||||
throw new NullPointerException("Destination must not be null");
|
||||
}
|
||||
if (!srcFile.exists()) {
|
||||
throw new FileNotFoundException("Source '" + srcFile + "' does not exist");
|
||||
}
|
||||
if (srcFile.isDirectory()) {
|
||||
throw new IOException("Source '" + srcFile + "' exists but is a directory");
|
||||
}
|
||||
if (srcFile.getCanonicalPath().equals(destFile.getCanonicalPath())) {
|
||||
throw new IOException("Source '" + srcFile + "' and destination '" + destFile + "' are the same");
|
||||
}
|
||||
File parentFile = destFile.getParentFile();
|
||||
if ((parentFile != null)
|
||||
&& (!parentFile.mkdirs()) && (!parentFile.isDirectory())) {
|
||||
throw new IOException("Destination '" + parentFile + "' directory cannot be created");
|
||||
}
|
||||
throws IOException {
|
||||
if (srcFile == null)
|
||||
throw new NullPointerException("Source must not be null");
|
||||
if (destFile == null)
|
||||
throw new NullPointerException("Destination must not be null");
|
||||
if (!srcFile.exists())
|
||||
throw new FileNotFoundException("Source '" + srcFile + "' does not exist");
|
||||
if (srcFile.isDirectory())
|
||||
throw new IOException("Source '" + srcFile + "' exists but is a directory");
|
||||
if (srcFile.getCanonicalPath().equals(destFile.getCanonicalPath()))
|
||||
throw new IOException("Source '" + srcFile + "' and destination '" + destFile + "' are the same");
|
||||
File parentFile = destFile.getParentFile();
|
||||
if ((parentFile != null)
|
||||
&& (!parentFile.mkdirs()) && (!parentFile.isDirectory()))
|
||||
throw new IOException("Destination '" + parentFile + "' directory cannot be created");
|
||||
|
||||
if ((destFile.exists()) && (!destFile.canWrite())) {
|
||||
throw new IOException("Destination '" + destFile + "' exists but is read-only");
|
||||
}
|
||||
doCopyFile(srcFile, destFile, preserveFileDate);
|
||||
if ((destFile.exists()) && (!destFile.canWrite()))
|
||||
throw new IOException("Destination '" + destFile + "' exists but is read-only");
|
||||
doCopyFile(srcFile, destFile, preserveFileDate);
|
||||
}
|
||||
|
||||
private static void doCopyFile(File srcFile, File destFile, boolean preserveFileDate)
|
||||
throws IOException {
|
||||
if ((destFile.exists()) && (destFile.isDirectory())) {
|
||||
throw new IOException("Destination '" + destFile + "' exists but is a directory");
|
||||
}
|
||||
throws IOException {
|
||||
if ((destFile.exists()) && (destFile.isDirectory()))
|
||||
throw new IOException("Destination '" + destFile + "' exists but is a directory");
|
||||
|
||||
FileInputStream fis = null;
|
||||
FileOutputStream fos = null;
|
||||
FileChannel input = null;
|
||||
FileChannel output = null;
|
||||
try {
|
||||
fis = new FileInputStream(srcFile);
|
||||
fos = new FileOutputStream(destFile);
|
||||
input = fis.getChannel();
|
||||
output = fos.getChannel();
|
||||
long size = input.size();
|
||||
long pos = 0L;
|
||||
long count;
|
||||
while (pos < size) {
|
||||
count = size - pos > 31457280L ? 31457280L : size - pos;
|
||||
pos += output.transferFrom(input, pos, count);
|
||||
}
|
||||
} finally {
|
||||
IOUtils.closeQuietly(output);
|
||||
IOUtils.closeQuietly(fos);
|
||||
IOUtils.closeQuietly(input);
|
||||
IOUtils.closeQuietly(fis);
|
||||
}
|
||||
FileInputStream fis = null;
|
||||
FileOutputStream fos = null;
|
||||
FileChannel input = null;
|
||||
FileChannel output = null;
|
||||
try {
|
||||
fis = new FileInputStream(srcFile);
|
||||
fos = new FileOutputStream(destFile);
|
||||
input = fis.getChannel();
|
||||
output = fos.getChannel();
|
||||
long size = input.size();
|
||||
long pos = 0L;
|
||||
long count;
|
||||
while (pos < size) {
|
||||
count = size - pos > 31457280L ? 31457280L : size - pos;
|
||||
pos += output.transferFrom(input, pos, count);
|
||||
}
|
||||
} finally {
|
||||
IOUtils.closeQuietly(output);
|
||||
IOUtils.closeQuietly(fos);
|
||||
IOUtils.closeQuietly(input);
|
||||
IOUtils.closeQuietly(fis);
|
||||
}
|
||||
|
||||
if (srcFile.length() != destFile.length()) {
|
||||
throw new IOException("Failed to copy full contents from '" + srcFile + "' to '" + destFile + "'");
|
||||
}
|
||||
if (srcFile.length() != destFile.length())
|
||||
throw new IOException("Failed to copy full contents from '" + srcFile + "' to '" + destFile + "'");
|
||||
|
||||
if (preserveFileDate) {
|
||||
destFile.setLastModified(srcFile.lastModified());
|
||||
}
|
||||
if (preserveFileDate)
|
||||
destFile.setLastModified(srcFile.lastModified());
|
||||
}
|
||||
|
||||
public static int indexOfLastSeparator(String filename) {
|
||||
if (filename == null) {
|
||||
return -1;
|
||||
}
|
||||
int lastUnixPos = filename.lastIndexOf(47);
|
||||
int lastWindowsPos = filename.lastIndexOf(92);
|
||||
return Math.max(lastUnixPos, lastWindowsPos);
|
||||
if (filename == null)
|
||||
return -1;
|
||||
int lastUnixPos = filename.lastIndexOf(47);
|
||||
int lastWindowsPos = filename.lastIndexOf(92);
|
||||
return Math.max(lastUnixPos, lastWindowsPos);
|
||||
}
|
||||
|
||||
public static int indexOfExtension(String filename) {
|
||||
if (filename == null) {
|
||||
return -1;
|
||||
}
|
||||
int extensionPos = filename.lastIndexOf(46);
|
||||
int lastSeparator = indexOfLastSeparator(filename);
|
||||
return lastSeparator > extensionPos ? -1 : extensionPos;
|
||||
if (filename == null)
|
||||
return -1;
|
||||
int extensionPos = filename.lastIndexOf(46);
|
||||
int lastSeparator = indexOfLastSeparator(filename);
|
||||
return lastSeparator > extensionPos ? -1 : extensionPos;
|
||||
}
|
||||
|
||||
public static String getName(String filename) {
|
||||
if (filename == null) {
|
||||
return null;
|
||||
}
|
||||
int index = indexOfLastSeparator(filename);
|
||||
return filename.substring(index + 1);
|
||||
if (filename == null)
|
||||
return null;
|
||||
int index = indexOfLastSeparator(filename);
|
||||
return filename.substring(index + 1);
|
||||
}
|
||||
|
||||
public static String getBaseName(String filename) {
|
||||
return removeExtension(getName(filename));
|
||||
return removeExtension(getName(filename));
|
||||
}
|
||||
|
||||
public static String getExtension(String filename) {
|
||||
if (filename == null) {
|
||||
return null;
|
||||
}
|
||||
int index = indexOfExtension(filename);
|
||||
if (index == -1) {
|
||||
return "";
|
||||
}
|
||||
return filename.substring(index + 1);
|
||||
if (filename == null)
|
||||
return null;
|
||||
int index = indexOfExtension(filename);
|
||||
if (index == -1)
|
||||
return "";
|
||||
return filename.substring(index + 1);
|
||||
}
|
||||
|
||||
public static String removeExtension(String filename) {
|
||||
if (filename == null) {
|
||||
return null;
|
||||
}
|
||||
int index = indexOfExtension(filename);
|
||||
if (index == -1) {
|
||||
return filename;
|
||||
}
|
||||
return filename.substring(0, index);
|
||||
if (filename == null)
|
||||
return null;
|
||||
int index = indexOfExtension(filename);
|
||||
if (index == -1)
|
||||
return filename;
|
||||
return filename.substring(0, index);
|
||||
}
|
||||
|
||||
|
||||
public static void writeQuietly(File file, CharSequence data) {
|
||||
try {
|
||||
write(file, data);
|
||||
} catch(IOException e) {
|
||||
} catch (IOException e) {
|
||||
HMCLog.warn("Failed to write data to file: " + file, e);
|
||||
}
|
||||
}
|
||||
|
||||
public static void write(File file, CharSequence data)
|
||||
throws IOException {
|
||||
write(file, data, "UTF-8", false);
|
||||
throws IOException {
|
||||
write(file, data, "UTF-8", false);
|
||||
}
|
||||
|
||||
public static void write(File file, CharSequence data, boolean append)
|
||||
throws IOException {
|
||||
write(file, data, "UTF-8", append);
|
||||
throws IOException {
|
||||
write(file, data, "UTF-8", append);
|
||||
}
|
||||
|
||||
public static void write(File file, CharSequence data, String encoding)
|
||||
throws IOException {
|
||||
write(file, data, encoding, false);
|
||||
throws IOException {
|
||||
write(file, data, encoding, false);
|
||||
}
|
||||
|
||||
public static void write(File file, CharSequence data, String encoding, boolean append)
|
||||
throws IOException {
|
||||
String str = data == null ? null : data.toString();
|
||||
writeStringToFile(file, str, encoding, append);
|
||||
throws IOException {
|
||||
String str = data == null ? null : data.toString();
|
||||
writeStringToFile(file, str, encoding, append);
|
||||
}
|
||||
|
||||
public static void writeStringToFile(File file, String data)
|
||||
throws IOException {
|
||||
writeStringToFile(file, data, "UTF-8", false);
|
||||
throws IOException {
|
||||
writeStringToFile(file, data, "UTF-8", false);
|
||||
}
|
||||
|
||||
public static void writeStringToFile(File file, String data, String encoding)
|
||||
throws IOException {
|
||||
writeStringToFile(file, data, encoding, false);
|
||||
throws IOException {
|
||||
writeStringToFile(file, data, encoding, false);
|
||||
}
|
||||
|
||||
public static void writeStringToFile(File file, String data, String encoding, boolean append)
|
||||
throws IOException {
|
||||
OutputStream out = null;
|
||||
try {
|
||||
out = openOutputStream(file, append);
|
||||
IOUtils.write(data, out, encoding);
|
||||
out.close();
|
||||
} finally {
|
||||
IOUtils.closeQuietly(out);
|
||||
}
|
||||
throws IOException {
|
||||
OutputStream out = null;
|
||||
try {
|
||||
out = openOutputStream(file, append);
|
||||
IOUtils.write(data, out, encoding);
|
||||
out.close();
|
||||
} finally {
|
||||
IOUtils.closeQuietly(out);
|
||||
}
|
||||
}
|
||||
|
||||
public static FileInputStream openInputStream(File file)
|
||||
throws IOException {
|
||||
if (file.exists()) {
|
||||
if (file.isDirectory()) {
|
||||
throw new IOException("File '" + file + "' exists but is a directory");
|
||||
}
|
||||
if (!file.canRead()) {
|
||||
throw new IOException("File '" + file + "' cannot be read");
|
||||
}
|
||||
} else {
|
||||
throw new FileNotFoundException("File '" + file + "' does not exist");
|
||||
}
|
||||
return new FileInputStream(file);
|
||||
throws IOException {
|
||||
if (file.exists()) {
|
||||
if (file.isDirectory())
|
||||
throw new IOException("File '" + file + "' exists but is a directory");
|
||||
if (!file.canRead())
|
||||
throw new IOException("File '" + file + "' cannot be read");
|
||||
} else
|
||||
throw new FileNotFoundException("File '" + file + "' does not exist");
|
||||
return new FileInputStream(file);
|
||||
}
|
||||
|
||||
public static FileOutputStream openOutputStream(File file)
|
||||
throws IOException {
|
||||
return openOutputStream(file, false);
|
||||
throws IOException {
|
||||
return openOutputStream(file, false);
|
||||
}
|
||||
|
||||
public static FileOutputStream openOutputStream(File file, boolean append)
|
||||
throws IOException {
|
||||
if (file.exists()) {
|
||||
if (file.isDirectory()) {
|
||||
throw new IOException("File '" + file + "' exists but is a directory");
|
||||
}
|
||||
if (!file.canWrite()) {
|
||||
throw new IOException("File '" + file + "' cannot be written to");
|
||||
}
|
||||
} else {
|
||||
File parent = file.getParentFile();
|
||||
if ((parent != null)
|
||||
&& (!parent.mkdirs()) && (!parent.isDirectory())) {
|
||||
throw new IOException("Directory '" + parent + "' could not be created");
|
||||
}
|
||||
throws IOException {
|
||||
if (file.exists()) {
|
||||
if (file.isDirectory())
|
||||
throw new IOException("File '" + file + "' exists but is a directory");
|
||||
if (!file.canWrite())
|
||||
throw new IOException("File '" + file + "' cannot be written to");
|
||||
} else {
|
||||
File parent = file.getParentFile();
|
||||
if ((parent != null)
|
||||
&& (!parent.mkdirs()) && (!parent.isDirectory()))
|
||||
throw new IOException("Directory '" + parent + "' could not be created");
|
||||
file.createNewFile();
|
||||
}
|
||||
}
|
||||
|
||||
return new FileOutputStream(file, append);
|
||||
return new FileOutputStream(file, append);
|
||||
}
|
||||
|
||||
|
||||
public static File[] searchSuffix(File dir, String suffix) {
|
||||
ArrayList<File> al = new ArrayList();
|
||||
File[] files = dir.listFiles();
|
||||
for(File f : files)
|
||||
if(f.getName().endsWith(suffix)) al.add(f);
|
||||
for (File f : files)
|
||||
if (f.getName().endsWith(suffix)) al.add(f);
|
||||
return al.toArray(new File[0]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,7 +16,6 @@
|
||||
*/
|
||||
package org.jackhuang.hellominecraft.utils.system;
|
||||
|
||||
import org.jackhuang.hellominecraft.utils.system.OS;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.Closeable;
|
||||
import java.io.File;
|
||||
@@ -43,14 +42,12 @@ import org.jackhuang.hellominecraft.HMCLog;
|
||||
public class IOUtils {
|
||||
|
||||
public static String addSeparator(String path) {
|
||||
if (path == null || path.trim().length() == 0) {
|
||||
if (path == null || path.trim().length() == 0)
|
||||
return "";
|
||||
}
|
||||
if (isSeparator(path.charAt(path.length() - 1))) {
|
||||
if (isSeparator(path.charAt(path.length() - 1)))
|
||||
return path;
|
||||
} else {
|
||||
else
|
||||
return path + File.separatorChar;
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean isSeparator(char ch) {
|
||||
@@ -60,21 +57,18 @@ public class IOUtils {
|
||||
public static String removeLastSeparator(String dir) {
|
||||
String t = dir.trim();
|
||||
char ch = t.charAt(t.length() - 1);
|
||||
if (isSeparator(ch)) {
|
||||
if (isSeparator(ch))
|
||||
return t.substring(0, t.length() - 1);
|
||||
}
|
||||
return t;
|
||||
}
|
||||
|
||||
public static String extractLastDirectory(String dir) {
|
||||
String t = removeLastSeparator(dir);
|
||||
int i = t.length() - 1;
|
||||
while (i >= 0 && !isSeparator(dir.charAt(i))) {
|
||||
while (i >= 0 && !isSeparator(dir.charAt(i)))
|
||||
i--;
|
||||
}
|
||||
if (i < 0) {
|
||||
if (i < 0)
|
||||
return t;
|
||||
}
|
||||
return t.substring(i + 1, (t.length() - i) + (i + 1) - 1);
|
||||
}
|
||||
|
||||
@@ -83,11 +77,9 @@ public class IOUtils {
|
||||
if (f.isDirectory()) {
|
||||
File[] f1 = f.listFiles();
|
||||
int len = f1.length;
|
||||
for (int i = 0; i < len; i++) {
|
||||
if (f1[i].isFile()) {
|
||||
for (int i = 0; i < len; i++)
|
||||
if (f1[i].isFile())
|
||||
arr.add(f1[i].getName());
|
||||
}
|
||||
}
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
@@ -97,11 +89,9 @@ public class IOUtils {
|
||||
if (f.isDirectory()) {
|
||||
File[] f1 = f.listFiles();
|
||||
int len = f1.length;
|
||||
for (int i = 0; i < len; i++) {
|
||||
if (f1[i].isFile()) {
|
||||
for (int i = 0; i < len; i++)
|
||||
if (f1[i].isFile())
|
||||
arr.add(addSeparator(f.getAbsolutePath()) + f1[i].getName());
|
||||
}
|
||||
}
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
@@ -111,11 +101,9 @@ public class IOUtils {
|
||||
if (f.isDirectory()) {
|
||||
File[] f1 = f.listFiles();
|
||||
int len = f1.length;
|
||||
for (int i = 0; i < len; i++) {
|
||||
if (f1[i].isDirectory()) {
|
||||
for (int i = 0; i < len; i++)
|
||||
if (f1[i].isDirectory())
|
||||
arr.add(f1[i].getName());
|
||||
}
|
||||
}
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
@@ -152,9 +140,8 @@ public class IOUtils {
|
||||
for (int i = 0; i < macs.length; i++) {
|
||||
mac = Integer.toHexString(macs[i] & 0xFF);
|
||||
|
||||
if (mac.length() == 1) {
|
||||
if (mac.length() == 1)
|
||||
mac = '0' + mac;
|
||||
}
|
||||
|
||||
sb.append(mac).append("-");
|
||||
}
|
||||
@@ -179,11 +166,10 @@ public class IOUtils {
|
||||
public static String getJavaDir() {
|
||||
String path = System.getProperty("java.home") + File.separatorChar + "bin" + File.separatorChar;
|
||||
path = addSeparator(path);
|
||||
if (OS.os() == OS.WINDOWS && new File(path + "javaw.exe").isFile()) {
|
||||
if (OS.os() == OS.WINDOWS && new File(path + "javaw.exe").isFile())
|
||||
return path + "javaw.exe";
|
||||
} else {
|
||||
else
|
||||
return path + "java";
|
||||
}
|
||||
}
|
||||
|
||||
public static byte[] readFully(InputStream stream) throws IOException {
|
||||
@@ -192,9 +178,8 @@ public class IOUtils {
|
||||
int len;
|
||||
do {
|
||||
len = stream.read(data);
|
||||
if (len <= 0) {
|
||||
if (len <= 0)
|
||||
continue;
|
||||
}
|
||||
entryBuffer.write(data, 0, len);
|
||||
} while (len != -1);
|
||||
|
||||
@@ -219,39 +204,33 @@ public class IOUtils {
|
||||
|
||||
public static void closeQuietly(Closeable closeable) {
|
||||
try {
|
||||
if (closeable != null) {
|
||||
if (closeable != null)
|
||||
closeable.close();
|
||||
}
|
||||
} catch (IOException ioe) {
|
||||
}
|
||||
}
|
||||
|
||||
public static void write(byte[] data, OutputStream output)
|
||||
throws IOException {
|
||||
if (data != null) {
|
||||
if (data != null)
|
||||
output.write(data);
|
||||
}
|
||||
}
|
||||
|
||||
public static void write(String data, OutputStream output, String encoding)
|
||||
throws IOException {
|
||||
if (data != null) {
|
||||
if (data != null)
|
||||
output.write(data.getBytes(encoding));
|
||||
}
|
||||
}
|
||||
|
||||
public static FileInputStream openInputStream(File file)
|
||||
throws IOException {
|
||||
if (file.exists()) {
|
||||
if (file.isDirectory()) {
|
||||
if (file.isDirectory())
|
||||
throw new IOException("File '" + file + "' exists but is a directory");
|
||||
}
|
||||
if (!file.canRead()) {
|
||||
if (!file.canRead())
|
||||
throw new IOException("File '" + file + "' cannot be read");
|
||||
}
|
||||
} else {
|
||||
} else
|
||||
throw new FileNotFoundException("File '" + file + "' does not exist");
|
||||
}
|
||||
return new FileInputStream(file);
|
||||
}
|
||||
|
||||
|
||||
@@ -16,7 +16,6 @@
|
||||
*/
|
||||
package org.jackhuang.hellominecraft.utils.system;
|
||||
|
||||
import org.jackhuang.hellominecraft.utils.system.ProcessManager;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
@@ -35,7 +34,7 @@ public class JavaProcess {
|
||||
public JavaProcess(List<String> commands, Process process, ProcessManager pm) {
|
||||
this.commands = commands;
|
||||
this.process = process;
|
||||
if(pm != null) pm.registerProcess(this);
|
||||
if (pm != null) pm.registerProcess(this);
|
||||
}
|
||||
|
||||
public JavaProcess(String[] commands, Process process, ProcessManager pm) {
|
||||
@@ -89,4 +88,4 @@ public class JavaProcess {
|
||||
public void stop() {
|
||||
this.process.destroy();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,7 +45,7 @@ public final class JdkVersion {
|
||||
public String getLocation() {
|
||||
return location;
|
||||
}
|
||||
|
||||
|
||||
public int getParsedVersion() {
|
||||
return parseVersion(getVersion());
|
||||
}
|
||||
@@ -109,18 +109,14 @@ public final class JdkVersion {
|
||||
// version String should look like "1.4.2_10"
|
||||
majorJavaVersion = parseVersion(javaVersion);
|
||||
}
|
||||
|
||||
|
||||
private static int parseVersion(String javaVersion) {
|
||||
if(StrUtils.isBlank(javaVersion)) return UNKOWN;
|
||||
if (StrUtils.isBlank(javaVersion)) return UNKOWN;
|
||||
int a = UNKOWN;
|
||||
if (javaVersion.contains("1.9."))
|
||||
a = JAVA_19;
|
||||
else if (javaVersion.contains("1.8."))
|
||||
a = JAVA_18;
|
||||
else if (javaVersion.contains("1.7."))
|
||||
a = JAVA_17;
|
||||
else if (javaVersion.contains("1.6."))
|
||||
a = JAVA_16;
|
||||
if (javaVersion.contains("1.9.")) a = JAVA_19;
|
||||
else if (javaVersion.contains("1.8.")) a = JAVA_18;
|
||||
else if (javaVersion.contains("1.7.")) a = JAVA_17;
|
||||
else if (javaVersion.contains("1.6.")) a = JAVA_16;
|
||||
return a;
|
||||
}
|
||||
|
||||
|
||||
@@ -16,38 +16,38 @@
|
||||
*/
|
||||
package org.jackhuang.hellominecraft.utils.system;
|
||||
|
||||
import javax.swing.JOptionPane;
|
||||
import javax.swing.JOptionPane;
|
||||
import org.jackhuang.hellominecraft.C;
|
||||
|
||||
/**
|
||||
* @author huangyuhui
|
||||
*/
|
||||
public class MessageBox
|
||||
{
|
||||
|
||||
/**
|
||||
* @author huangyuhui
|
||||
*/
|
||||
public class MessageBox {
|
||||
|
||||
private static String Title = C.i18n("message.info");
|
||||
/**
|
||||
* Buttons: OK
|
||||
*/
|
||||
public static final int DEFAULT_OPTION = -1;
|
||||
public static final int DEFAULT_OPTION = -1;
|
||||
/**
|
||||
* Buttons: Yes No
|
||||
*/
|
||||
public static final int YES_NO_OPTION = 10;
|
||||
public static final int YES_NO_OPTION = 10;
|
||||
/**
|
||||
* Buttons: Yes No Cancel
|
||||
*/
|
||||
public static final int YES_NO_CANCEL_OPTION =11;
|
||||
public static final int YES_NO_CANCEL_OPTION = 11;
|
||||
/**
|
||||
* Buttons: OK Cancel
|
||||
*/
|
||||
public static final int OK_CANCEL_OPTION = 12;
|
||||
public static final int OK_CANCEL_OPTION = 12;
|
||||
/**
|
||||
* User Operation: Yes
|
||||
*/
|
||||
public static final int YES_OPTION = 0;
|
||||
/**
|
||||
* User Operation: No
|
||||
*/
|
||||
*/
|
||||
public static final int NO_OPTION = 1;
|
||||
/**
|
||||
* User Operation: Cancel
|
||||
@@ -67,7 +67,7 @@ public class MessageBox
|
||||
public static final int ERROR_MESSAGE = 0;
|
||||
/**
|
||||
* Message Box Type: Info
|
||||
*/
|
||||
*/
|
||||
public static final int INFORMATION_MESSAGE = 1;
|
||||
/**
|
||||
* Message Box Type: Warning
|
||||
@@ -81,46 +81,45 @@ public class MessageBox
|
||||
* Message Box Type: Plain
|
||||
*/
|
||||
public static final int PLAIN_MESSAGE = -1;
|
||||
|
||||
|
||||
/**
|
||||
* Show MsgBox with title and options
|
||||
*
|
||||
* @param Msg The Message
|
||||
* @param Title The title of MsgBox.
|
||||
* @param Option The type of MsgBox.
|
||||
* @return user operation.
|
||||
*/
|
||||
public static int Show(String Msg, String Title, int Option)
|
||||
{
|
||||
switch(Option)
|
||||
{
|
||||
case YES_NO_OPTION:
|
||||
case YES_NO_CANCEL_OPTION:
|
||||
case OK_CANCEL_OPTION:
|
||||
return JOptionPane.showConfirmDialog(null, Msg, Title, Option - 10);
|
||||
default:
|
||||
JOptionPane.showMessageDialog(null, Msg, Title, Option);
|
||||
}
|
||||
return 0;
|
||||
public static int Show(String Msg, String Title, int Option) {
|
||||
switch (Option) {
|
||||
case YES_NO_OPTION:
|
||||
case YES_NO_CANCEL_OPTION:
|
||||
case OK_CANCEL_OPTION:
|
||||
return JOptionPane.showConfirmDialog(null, Msg, Title, Option - 10);
|
||||
default:
|
||||
JOptionPane.showMessageDialog(null, Msg, Title, Option);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Show MsgBox with options
|
||||
*
|
||||
* @param Msg The Message
|
||||
* @param Option The type of MsgBox.
|
||||
* @return User Operation
|
||||
*/
|
||||
public static int Show(String Msg, int Option)
|
||||
{
|
||||
return Show(Msg, Title, Option);
|
||||
}
|
||||
|
||||
public static int Show(String Msg, int Option) {
|
||||
return Show(Msg, Title, Option);
|
||||
}
|
||||
|
||||
/**
|
||||
* Show Default MsgBox
|
||||
*
|
||||
* @param Msg The Message
|
||||
* @return User Operation
|
||||
*/
|
||||
public static int Show(String Msg)
|
||||
{
|
||||
return Show(Msg, Title, INFORMATION_MESSAGE);
|
||||
}
|
||||
}
|
||||
public static int Show(String Msg) {
|
||||
return Show(Msg, Title, INFORMATION_MESSAGE);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,12 +24,12 @@ import org.jackhuang.hellominecraft.HMCLog;
|
||||
* @author huangyuhui
|
||||
*/
|
||||
public enum OS {
|
||||
|
||||
|
||||
LINUX('/'),
|
||||
WINDOWS('\\'),
|
||||
OSX('/'),
|
||||
UNKOWN('/');
|
||||
|
||||
|
||||
public final char fileSeparator;
|
||||
|
||||
private OS(char fileSeparator) {
|
||||
@@ -39,27 +39,21 @@ public enum OS {
|
||||
public static OS os() {
|
||||
String str;
|
||||
if ((str = System.getProperty("os.name").toLowerCase())
|
||||
.contains("win")) {
|
||||
.contains("win"))
|
||||
return OS.WINDOWS;
|
||||
}
|
||||
if (str.contains("mac")) {
|
||||
if (str.contains("mac"))
|
||||
return OS.OSX;
|
||||
}
|
||||
if (str.contains("solaris")) {
|
||||
if (str.contains("solaris"))
|
||||
return OS.LINUX;
|
||||
}
|
||||
if (str.contains("sunos")) {
|
||||
if (str.contains("sunos"))
|
||||
return OS.LINUX;
|
||||
}
|
||||
if (str.contains("linux")) {
|
||||
if (str.contains("linux"))
|
||||
return OS.LINUX;
|
||||
}
|
||||
if (str.contains("unix")) {
|
||||
if (str.contains("unix"))
|
||||
return OS.LINUX;
|
||||
}
|
||||
return OS.UNKOWN;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return Free Physical Memory Size (Byte)
|
||||
*/
|
||||
@@ -67,10 +61,10 @@ public enum OS {
|
||||
try {
|
||||
OperatingSystemMXBean o = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
|
||||
return o.getTotalPhysicalMemorySize();
|
||||
} catch(Throwable t) {
|
||||
} catch (Throwable t) {
|
||||
HMCLog.warn("Failed to get total physical memory size");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -48,8 +48,8 @@ public enum Platform {
|
||||
};
|
||||
|
||||
public abstract String getBit();
|
||||
|
||||
|
||||
public static Platform getPlatform() {
|
||||
return System.getProperty("os.arch").contains("64") ? BIT_64 : BIT_32;
|
||||
return System.getProperty("os.arch").contains("64") ? BIT_64 : BIT_32;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,18 +25,17 @@ import java.util.HashSet;
|
||||
public class ProcessManager {
|
||||
|
||||
private static final HashSet<JavaProcess> gameProcesses = new HashSet();
|
||||
|
||||
|
||||
public void registerProcess(JavaProcess jp) {
|
||||
gameProcesses.add(jp);
|
||||
}
|
||||
|
||||
|
||||
public void stopAllProcesses() {
|
||||
for(JavaProcess jp : gameProcesses) {
|
||||
for (JavaProcess jp : gameProcesses)
|
||||
jp.stop();
|
||||
}
|
||||
gameProcesses.clear();
|
||||
}
|
||||
|
||||
|
||||
public void onProcessStopped(JavaProcess p) {
|
||||
gameProcesses.remove(p);
|
||||
}
|
||||
|
||||
@@ -31,7 +31,7 @@ public class ProcessThread extends Thread {
|
||||
|
||||
JavaProcess p;
|
||||
boolean readError = false, enableReading = true;
|
||||
|
||||
|
||||
public final EventHandler<String> printlnEvent = new EventHandler<>(this);
|
||||
public final EventHandler<JavaProcess> stopEvent = new EventHandler<>(this);
|
||||
|
||||
@@ -49,22 +49,20 @@ public class ProcessThread extends Thread {
|
||||
public void run() {
|
||||
InputStream in = null;
|
||||
BufferedReader br = null;
|
||||
if (enableReading) {
|
||||
if (enableReading)
|
||||
in = readError ? p.getRawProcess().getErrorStream() : p.getRawProcess().getInputStream();
|
||||
}
|
||||
try {
|
||||
if (enableReading) {
|
||||
if (enableReading)
|
||||
try {
|
||||
br = new BufferedReader(new InputStreamReader(in, System.getProperty("sun.jnu.encoding", "UTF-8")));
|
||||
} catch (UnsupportedEncodingException ex) {
|
||||
HMCLog.warn("Unsupported encoding: " + System.getProperty("sun.jnu.encoding", "UTF-8"), ex);
|
||||
br = new BufferedReader(new InputStreamReader(in));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
String line;
|
||||
while (p.isRunning()) {
|
||||
if (enableReading) {
|
||||
while (p.isRunning())
|
||||
if (enableReading)
|
||||
while ((line = br.readLine()) != null) {
|
||||
printlnEvent.execute(line);
|
||||
if (readError) {
|
||||
@@ -75,14 +73,12 @@ public class ProcessThread extends Thread {
|
||||
p.getStdOutLines().add(line);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
else
|
||||
try {
|
||||
Thread.sleep(1);
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
if (enableReading) {
|
||||
if (enableReading)
|
||||
while ((line = br.readLine()) != null) {
|
||||
printlnEvent.execute(line);
|
||||
if (readError) {
|
||||
@@ -93,7 +89,6 @@ public class ProcessThread extends Thread {
|
||||
p.getStdOutLines().add(line);
|
||||
}
|
||||
}
|
||||
}
|
||||
stopEvent.execute(p);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
|
||||
@@ -24,12 +24,12 @@ import org.jackhuang.hellominecraft.C;
|
||||
* @author huangyuhui
|
||||
*/
|
||||
public class MinecraftRemoteVersions {
|
||||
|
||||
|
||||
public ArrayList<MinecraftRemoteVersion> versions;
|
||||
public MinecraftRemoteLatestVersion latest;
|
||||
|
||||
|
||||
public static MinecraftRemoteVersions fromJson(String s) {
|
||||
return C.gson.fromJson(s, MinecraftRemoteVersions.class);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -22,11 +22,12 @@ import org.jackhuang.hellominecraft.C;
|
||||
* @author huangyuhui
|
||||
*/
|
||||
public class MinecraftVersionRequest {
|
||||
|
||||
public static final int Unkown = 0, Invaild = 1, InvaildJar = 2,
|
||||
Modified = 3, OK = 4, NotFound = 5, NotReadable = 6, NotAFile = 7;
|
||||
public int type;
|
||||
public String version;
|
||||
|
||||
|
||||
public static String getResponse(MinecraftVersionRequest minecraftVersion) {
|
||||
String text = "";
|
||||
switch (minecraftVersion.type) {
|
||||
|
||||
@@ -30,7 +30,7 @@ import org.jackhuang.hellominecraft.utils.Utils;
|
||||
* @author huangyuhui
|
||||
*/
|
||||
public class LogWindow extends javax.swing.JFrame {
|
||||
|
||||
|
||||
boolean movingEnd;
|
||||
NonFunction<Boolean> listener;
|
||||
NonConsumer terminateGameListener;
|
||||
@@ -40,18 +40,18 @@ public class LogWindow extends javax.swing.JFrame {
|
||||
*/
|
||||
public LogWindow() {
|
||||
initComponents();
|
||||
|
||||
|
||||
movingEnd = true;
|
||||
|
||||
setLocationRelativeTo(null);
|
||||
|
||||
TextComponentOutputStream tc = new TextComponentOutputStream(txtLog);
|
||||
DoubleOutputStream out = new DoubleOutputStream(tc, System.out);
|
||||
System.setOut(new LauncherPrintStream(out));
|
||||
DoubleOutputStream err = new DoubleOutputStream(tc, System.err);
|
||||
System.setErr(new LauncherPrintStream(err));
|
||||
|
||||
TextComponentOutputStream tc = new TextComponentOutputStream(txtLog);
|
||||
DoubleOutputStream out = new DoubleOutputStream(tc, System.out);
|
||||
System.setOut(new LauncherPrintStream(out));
|
||||
DoubleOutputStream err = new DoubleOutputStream(tc, System.err);
|
||||
System.setErr(new LauncherPrintStream(err));
|
||||
}
|
||||
|
||||
|
||||
public static LogWindow instance = new LogWindow();
|
||||
|
||||
/**
|
||||
@@ -206,15 +206,15 @@ public class LogWindow extends javax.swing.JFrame {
|
||||
}//GEN-LAST:event_btnClearActionPerformed
|
||||
|
||||
private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
|
||||
if(listener != null && listener.onDone()) Utils.shutdownForcely();
|
||||
if (listener != null && listener.onDone()) Utils.shutdownForcely();
|
||||
}//GEN-LAST:event_formWindowClosed
|
||||
|
||||
private void btnCopyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCopyActionPerformed
|
||||
Utils.setClipborad(this.txtLog.getText());
|
||||
Utils.setClipborad(this.txtLog.getText());
|
||||
}//GEN-LAST:event_btnCopyActionPerformed
|
||||
|
||||
private void btnMCBBSActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnMCBBSActionPerformed
|
||||
Utils.openLink(C.URL_PUBLISH);
|
||||
Utils.openLink(C.URL_PUBLISH);
|
||||
}//GEN-LAST:event_btnMCBBSActionPerformed
|
||||
|
||||
private void btnTieBaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnTieBaActionPerformed
|
||||
@@ -226,7 +226,7 @@ public class LogWindow extends javax.swing.JFrame {
|
||||
}//GEN-LAST:event_btnMCFActionPerformed
|
||||
|
||||
private void btnTerminateGameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnTerminateGameActionPerformed
|
||||
if(terminateGameListener != null)
|
||||
if (terminateGameListener != null)
|
||||
terminateGameListener.onDone();
|
||||
}//GEN-LAST:event_btnTerminateGameActionPerformed
|
||||
|
||||
@@ -238,49 +238,49 @@ public class LogWindow extends javax.swing.JFrame {
|
||||
String text = txtLog.getText();
|
||||
text += status + System.getProperty("line.separator");
|
||||
txtLog.setText(text);
|
||||
|
||||
if(movingEnd) {
|
||||
|
||||
if (movingEnd) {
|
||||
int position = text.length();
|
||||
txtLog.setCaretPosition(position);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void log(String status, Throwable t) {
|
||||
log(status);
|
||||
log(StrUtils.getStackTrace(t));
|
||||
log(status);
|
||||
log(StrUtils.getStackTrace(t));
|
||||
}
|
||||
|
||||
|
||||
public void setExit(NonFunction<Boolean> exit) {
|
||||
this.listener = exit;
|
||||
this.listener = exit;
|
||||
}
|
||||
|
||||
|
||||
public void setTerminateGame(NonConsumer l) {
|
||||
this.terminateGameListener = l;
|
||||
this.terminateGameListener = l;
|
||||
}
|
||||
|
||||
|
||||
public void clean() {
|
||||
txtLog.setText("");
|
||||
}
|
||||
|
||||
|
||||
public boolean getMovingEnd() {
|
||||
return movingEnd;
|
||||
}
|
||||
|
||||
|
||||
public void setMovingEnd(boolean b) {
|
||||
movingEnd = b;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setVisible(boolean b) {
|
||||
lblCrash.setVisible(false);
|
||||
btnMCBBS.setVisible(false);
|
||||
btnTieBa.setVisible(false);
|
||||
btnMCF.setVisible(false);
|
||||
super.setVisible(b);
|
||||
lblCrash.setVisible(false);
|
||||
btnMCBBS.setVisible(false);
|
||||
btnTieBa.setVisible(false);
|
||||
btnMCF.setVisible(false);
|
||||
super.setVisible(b);
|
||||
}
|
||||
|
||||
|
||||
public void showAsCrashWindow(boolean out_date) {
|
||||
if(out_date) {
|
||||
if (out_date) {
|
||||
lblCrash.setVisible(false);
|
||||
btnMCBBS.setVisible(false);
|
||||
btnTieBa.setVisible(false);
|
||||
@@ -293,10 +293,10 @@ public class LogWindow extends javax.swing.JFrame {
|
||||
btnMCF.setVisible(true);
|
||||
lblCrash.setText(C.i18n("ui.label.crashing"));
|
||||
}
|
||||
|
||||
super.setVisible(true);
|
||||
|
||||
super.setVisible(true);
|
||||
}
|
||||
|
||||
|
||||
// Variables declaration - do not modify//GEN-BEGIN:variables
|
||||
private javax.swing.JButton btnClear;
|
||||
private javax.swing.JButton btnClose;
|
||||
|
||||
@@ -18,9 +18,11 @@ package org.jackhuang.hellominecraft.views;
|
||||
|
||||
/**
|
||||
* The frame given to choose things.
|
||||
*
|
||||
* @author huangyuhui
|
||||
*/
|
||||
public class Selector extends javax.swing.JDialog {
|
||||
|
||||
String[] selList;
|
||||
String msg;
|
||||
/**
|
||||
@@ -39,12 +41,12 @@ public class Selector extends javax.swing.JDialog {
|
||||
initComponents();
|
||||
|
||||
setLocationRelativeTo(null);
|
||||
|
||||
|
||||
this.selList = selList;
|
||||
this.sel = failedToSel;
|
||||
this.msg = msg;
|
||||
jLabel1.setText(msg);
|
||||
for(String s : selList)
|
||||
for (String s : selList)
|
||||
jComboBox1.addItem(s);
|
||||
}
|
||||
|
||||
|
||||
@@ -46,9 +46,8 @@ public class TintablePanel extends JPanel {
|
||||
}
|
||||
|
||||
public void setOverIcon(ImageIcon image) {
|
||||
if (this.overIcon != null) {
|
||||
if (this.overIcon != null)
|
||||
remove(this.overIcon);
|
||||
}
|
||||
|
||||
this.overIcon = new JLabel(image);
|
||||
this.overIcon.setVisible(false);
|
||||
|
||||
Reference in New Issue
Block a user