Fixed not showing prompt when no version here
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* Hello Minecraft! Launcher.
|
||||
* Copyright (C) 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 3 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. If not, see {http://www.gnu.org/licenses/}.
|
||||
*/
|
||||
package org.jackhuang.hellominecraft.api;
|
||||
|
||||
import java.util.EventObject;
|
||||
import java.util.HashMap;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author huang
|
||||
*/
|
||||
public class EventBus {
|
||||
|
||||
HashMap<Class, EventHandler> events = new HashMap<>();
|
||||
|
||||
public EventBus() {
|
||||
}
|
||||
|
||||
public <T extends EventObject> EventHandler<T> channel(Class<T> classOfT) {
|
||||
if (!events.containsKey(classOfT))
|
||||
events.put(classOfT, new EventHandler<>());
|
||||
return events.get(classOfT);
|
||||
}
|
||||
|
||||
public void fireChannel(EventObject obj) {
|
||||
channel((Class<EventObject>) obj.getClass()).fire(obj);
|
||||
}
|
||||
|
||||
public boolean fireChannelResulted(ResultedEvent obj) {
|
||||
return channel((Class<ResultedEvent>) obj.getClass()).fireResulted(obj);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -15,9 +15,10 @@
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see {http://www.gnu.org/licenses/}.
|
||||
*/
|
||||
package org.jackhuang.hellominecraft.util;
|
||||
package org.jackhuang.hellominecraft.api;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.EventObject;
|
||||
import org.jackhuang.hellominecraft.util.func.Consumer;
|
||||
|
||||
/**
|
||||
@@ -25,18 +26,11 @@ import org.jackhuang.hellominecraft.util.func.Consumer;
|
||||
* @author huangyuhui
|
||||
* @param <T> EventArgs
|
||||
*/
|
||||
public class EventHandler<T> {
|
||||
public class EventHandler<T extends EventObject> {
|
||||
|
||||
ArrayList<Object> events = new ArrayList<>();
|
||||
Object sender;
|
||||
|
||||
public EventHandler(Object sender) {
|
||||
this.sender = sender;
|
||||
}
|
||||
|
||||
public void register(Event<T> t) {
|
||||
if (!events.contains(t))
|
||||
events.add(t);
|
||||
public EventHandler() {
|
||||
}
|
||||
|
||||
public void register(Consumer<T> t) {
|
||||
@@ -44,20 +38,40 @@ public class EventHandler<T> {
|
||||
events.add(t);
|
||||
}
|
||||
|
||||
public void registerFirst(Consumer<T> t) {
|
||||
if (!events.contains(t))
|
||||
events.add(0, t);
|
||||
}
|
||||
|
||||
public void register(Runnable t) {
|
||||
if (!events.contains(t))
|
||||
events.add(t);
|
||||
}
|
||||
|
||||
public void registerFirst(Runnable t) {
|
||||
if (!events.contains(t))
|
||||
events.add(0, t);
|
||||
}
|
||||
|
||||
public boolean execute(T x) {
|
||||
public void fire(T x) {
|
||||
for (Object t : events)
|
||||
if (t instanceof Consumer) {
|
||||
((Consumer) t).accept(x);
|
||||
} else if (t instanceof Runnable)
|
||||
((Runnable) t).run();
|
||||
}
|
||||
|
||||
public boolean fireResulted(T x) {
|
||||
if (!(x instanceof ResultedEvent))
|
||||
throw new IllegalArgumentException("x should be ResultedEvent");
|
||||
ResultedEvent event = (ResultedEvent) x;
|
||||
boolean flag = true;
|
||||
for (Object t : events)
|
||||
if (t instanceof Event) {
|
||||
if (!((Event<T>) t).call(sender, x))
|
||||
if (t instanceof Consumer) {
|
||||
((Consumer) t).accept(x);
|
||||
if (!event.result())
|
||||
flag = false;
|
||||
} else if (t instanceof Consumer)
|
||||
((Consumer<T>) t).accept(x);
|
||||
else if (t instanceof Runnable)
|
||||
} else if (t instanceof Runnable)
|
||||
((Runnable) t).run();
|
||||
return flag;
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
/*
|
||||
* Hello Minecraft!.
|
||||
* Hello Minecraft! Launcher.
|
||||
* Copyright (C) 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 3 of the License, or
|
||||
@@ -15,14 +15,16 @@
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see {http://www.gnu.org/licenses/}.
|
||||
*/
|
||||
package org.jackhuang.hellominecraft.util;
|
||||
package org.jackhuang.hellominecraft.api;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author huangyuhui
|
||||
* @param <T> EventArgs
|
||||
* @author huang
|
||||
*/
|
||||
public interface Event<T> {
|
||||
public class HMCLAPI {
|
||||
|
||||
boolean call(Object sender, T t);
|
||||
/**
|
||||
* Events.
|
||||
*/
|
||||
public static final EventBus EVENT_BUS = new EventBus();
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* Hello Minecraft! Launcher.
|
||||
* Copyright (C) 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 3 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. If not, see {http://www.gnu.org/licenses/}.
|
||||
*/
|
||||
package org.jackhuang.hellominecraft.api;
|
||||
|
||||
import java.util.EventObject;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author huang
|
||||
*/
|
||||
public class PropertyChangedEvent<T> extends EventObject {
|
||||
|
||||
String propertyName;
|
||||
T oldValue, newValue;
|
||||
|
||||
public PropertyChangedEvent(Object source, String propertyName, T oldValue, T newValue) {
|
||||
super(source);
|
||||
this.propertyName = propertyName;
|
||||
this.oldValue = oldValue;
|
||||
this.newValue = newValue;
|
||||
}
|
||||
|
||||
public String getPropertyName() {
|
||||
return propertyName;
|
||||
}
|
||||
|
||||
public T getNewValue() {
|
||||
return newValue;
|
||||
}
|
||||
|
||||
public T getOldValue() {
|
||||
return oldValue;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* Hello Minecraft! Launcher.
|
||||
* Copyright (C) 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 3 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. If not, see {http://www.gnu.org/licenses/}.
|
||||
*/
|
||||
package org.jackhuang.hellominecraft.api;
|
||||
|
||||
import java.util.EventObject;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author huang
|
||||
*/
|
||||
public class ResultedEvent extends EventObject {
|
||||
protected boolean result = true;
|
||||
|
||||
public ResultedEvent(Object sender) {
|
||||
super(sender);
|
||||
}
|
||||
|
||||
public boolean result() {
|
||||
return result;
|
||||
}
|
||||
|
||||
public void failed() {
|
||||
setResult(false);
|
||||
}
|
||||
|
||||
public void setResult(boolean canceled) {
|
||||
this.result = canceled;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* Hello Minecraft! Launcher.
|
||||
* Copyright (C) 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 3 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. If not, see {http://www.gnu.org/licenses/}.
|
||||
*/
|
||||
package org.jackhuang.hellominecraft.api;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author huang
|
||||
*/
|
||||
public class ResultedSimpleEvent<T> extends ResultedEvent {
|
||||
|
||||
T value;
|
||||
|
||||
public ResultedSimpleEvent(Object sender, T t) {
|
||||
super(sender);
|
||||
value = t;
|
||||
}
|
||||
|
||||
public T getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public void setValue(T value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* Hello Minecraft! Launcher.
|
||||
* Copyright (C) 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 3 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. If not, see {http://www.gnu.org/licenses/}.
|
||||
*/
|
||||
package org.jackhuang.hellominecraft.api;
|
||||
|
||||
import java.util.EventObject;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author huang
|
||||
*/
|
||||
public class SimpleEvent<T> extends EventObject {
|
||||
private T value;
|
||||
|
||||
public SimpleEvent(Object source, T value) {
|
||||
super(source);
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public T getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public void setValue(T value) {
|
||||
this.value = value;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* Hello Minecraft! Launcher.
|
||||
* Copyright (C) 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 3 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. If not, see {http://www.gnu.org/licenses/}.
|
||||
*/
|
||||
package org.jackhuang.hellominecraft.api.event;
|
||||
|
||||
import org.jackhuang.hellominecraft.api.ResultedSimpleEvent;
|
||||
import org.jackhuang.hellominecraft.util.VersionNumber;
|
||||
|
||||
/**
|
||||
* This event gets fired when we found that user's HMCL is out of date.
|
||||
* <br>
|
||||
* This event is {@link org.jackhuang.hellominecraft.api.ResultedEvent}
|
||||
* If this event is failed, HMCL will not ask user to upgrade the application.
|
||||
* <br>
|
||||
* This event is fired on the {@link org.jackhuang.hellominecraft.api.HMCLAPI#EVENT_BUS}
|
||||
* @param source {@link org.jackhuang.hellominecraft.util.UpdateChecker}
|
||||
* @param VersionNumber newest version
|
||||
* @author huang
|
||||
*/
|
||||
public class OutOfDateEvent extends ResultedSimpleEvent<VersionNumber> {
|
||||
|
||||
public OutOfDateEvent(Object source, VersionNumber value) {
|
||||
super(source, value);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Hello Minecraft! Launcher.
|
||||
* Copyright (C) 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 3 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. If not, see {http://www.gnu.org/licenses/}.
|
||||
*/
|
||||
package org.jackhuang.hellominecraft.api.event.process;
|
||||
|
||||
import org.jackhuang.hellominecraft.api.SimpleEvent;
|
||||
import org.jackhuang.hellominecraft.util.sys.JavaProcess;
|
||||
|
||||
/**
|
||||
* This event gets fired when we launch the JVM and it got crashed.
|
||||
* <br>
|
||||
* This event is fired on the {@link org.jackhuang.hellominecraft.api.HMCLAPI#EVENT_BUS}
|
||||
* @param source {@link org.jackhuang.hellominecraft.util.sys.JavaProcessMonitor}
|
||||
* @param JavaProcess the crashed process.
|
||||
* @author huangyuhui
|
||||
*/
|
||||
public class JVMLaunchFailedEvent extends SimpleEvent<JavaProcess> {
|
||||
|
||||
public JVMLaunchFailedEvent(Object source, JavaProcess value) {
|
||||
super(source, value);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Hello Minecraft! Launcher.
|
||||
* Copyright (C) 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 3 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. If not, see {http://www.gnu.org/licenses/}.
|
||||
*/
|
||||
package org.jackhuang.hellominecraft.api.event.process;
|
||||
|
||||
import org.jackhuang.hellominecraft.api.SimpleEvent;
|
||||
import org.jackhuang.hellominecraft.util.sys.JavaProcess;
|
||||
|
||||
/**
|
||||
* This event gets fired when a JavaProcess exited abnormally and the exit code is not zero.
|
||||
* <br>
|
||||
* This event is fired on the {@link org.jackhuang.hellominecraft.api.HMCLAPI#EVENT_BUS}
|
||||
* @param source {@link org.jackhuang.hellominecraft.util.sys.JavaProcessMonitor}
|
||||
* @param JavaProcess The process that exited abnormally.
|
||||
* @author huangyuhui
|
||||
*/
|
||||
public class JavaProcessExitedAbnormallyEvent extends SimpleEvent<JavaProcess> {
|
||||
|
||||
public JavaProcessExitedAbnormallyEvent(Object source, JavaProcess value) {
|
||||
super(source, value);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Hello Minecraft! Launcher.
|
||||
* Copyright (C) 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 3 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. If not, see {http://www.gnu.org/licenses/}.
|
||||
*/
|
||||
package org.jackhuang.hellominecraft.api.event.process;
|
||||
|
||||
import org.jackhuang.hellominecraft.api.SimpleEvent;
|
||||
import org.jackhuang.hellominecraft.util.sys.JavaProcess;
|
||||
|
||||
/**
|
||||
* This event gets fired when a JavaProcess is starting.
|
||||
* <br>
|
||||
* This event is fired on the {@link org.jackhuang.hellominecraft.api.HMCLAPI#EVENT_BUS}
|
||||
* @param source {@link org.jackhuang.hellominecraft.util.sys.JavaProcessMonitor}
|
||||
* @param JavaProcess the starting JavaProcess.
|
||||
* @author huang
|
||||
*/
|
||||
public class JavaProcessStartingEvent extends SimpleEvent<JavaProcess> {
|
||||
|
||||
public JavaProcessStartingEvent(Object source, JavaProcess value) {
|
||||
super(source, value);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Hello Minecraft! Launcher.
|
||||
* Copyright (C) 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 3 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. If not, see {http://www.gnu.org/licenses/}.
|
||||
*/
|
||||
package org.jackhuang.hellominecraft.api.event.process;
|
||||
|
||||
import org.jackhuang.hellominecraft.api.SimpleEvent;
|
||||
import org.jackhuang.hellominecraft.util.sys.JavaProcess;
|
||||
|
||||
/**
|
||||
* This event gets fired when minecraft process exited successfully and the exit code is 0.
|
||||
* <br>
|
||||
* This event is fired on the {@link org.jackhuang.hellominecraft.api.HMCLAPI#EVENT_BUS}
|
||||
* @param source {@link org.jackhuang.hellominecraft.util.sys.JavaProcessMonitor}
|
||||
* @param JavaProcess minecraft process
|
||||
* @author huangyuhui
|
||||
*/
|
||||
public class JavaProcessStoppedEvent extends SimpleEvent<JavaProcess> {
|
||||
|
||||
public JavaProcessStoppedEvent(Object source, JavaProcess value) {
|
||||
super(source, value);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -17,11 +17,15 @@
|
||||
*/
|
||||
package org.jackhuang.hellominecraft.util;
|
||||
|
||||
import org.jackhuang.hellominecraft.api.EventHandler;
|
||||
import org.jackhuang.hellominecraft.util.net.NetUtils;
|
||||
import com.google.gson.JsonSyntaxException;
|
||||
import java.io.IOException;
|
||||
import org.jackhuang.hellominecraft.util.log.HMCLog;
|
||||
import java.util.Map;
|
||||
import org.jackhuang.hellominecraft.api.HMCLAPI;
|
||||
import org.jackhuang.hellominecraft.api.SimpleEvent;
|
||||
import org.jackhuang.hellominecraft.api.event.OutOfDateEvent;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -89,11 +93,12 @@ public final class UpdateChecker implements IUpdateChecker {
|
||||
};
|
||||
}
|
||||
|
||||
public final EventHandler<VersionNumber> outOfDateEvent = new EventHandler<>(this);
|
||||
public final EventHandler<SimpleEvent<VersionNumber>> upgrade = new EventHandler<>();
|
||||
|
||||
@Override
|
||||
public void checkOutdate() {
|
||||
if (outOfDate)
|
||||
outOfDateEvent.execute(getNewVersion());
|
||||
if (HMCLAPI.EVENT_BUS.fireChannelResulted(new OutOfDateEvent(this, getNewVersion())))
|
||||
upgrade.fire(new SimpleEvent<>(this, getNewVersion()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,11 +17,13 @@
|
||||
*/
|
||||
package org.jackhuang.hellominecraft.util.func;
|
||||
|
||||
import java.util.EventListener;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author huangyuhui
|
||||
*/
|
||||
public interface Consumer<T> {
|
||||
public interface Consumer<T> extends EventListener {
|
||||
|
||||
void accept(T t);
|
||||
}
|
||||
|
||||
@@ -27,7 +27,8 @@ import java.nio.charset.Charset;
|
||||
import org.jackhuang.hellominecraft.util.C;
|
||||
import org.jackhuang.hellominecraft.util.log.HMCLog;
|
||||
import org.jackhuang.hellominecraft.util.task.comm.PreviousResult;
|
||||
import org.jackhuang.hellominecraft.util.EventHandler;
|
||||
import org.jackhuang.hellominecraft.api.EventHandler;
|
||||
import org.jackhuang.hellominecraft.api.SimpleEvent;
|
||||
import org.jackhuang.hellominecraft.util.code.Charsets;
|
||||
import org.jackhuang.hellominecraft.util.task.Task;
|
||||
|
||||
@@ -39,7 +40,7 @@ public class HTTPGetTask extends Task implements PreviousResult<String> {
|
||||
|
||||
String url, result;
|
||||
Charset encoding;
|
||||
EventHandler<String> doneEvent = new EventHandler<>(this);
|
||||
EventHandler<SimpleEvent<String>> doneEvent = new EventHandler<>();
|
||||
boolean shouldContinue = true;
|
||||
|
||||
public HTTPGetTask(String url) {
|
||||
@@ -81,7 +82,7 @@ public class HTTPGetTask extends Task implements PreviousResult<String> {
|
||||
return;
|
||||
}
|
||||
result = baos.toString(encoding.name());
|
||||
doneEvent.execute(result);
|
||||
doneEvent.fire(new SimpleEvent<>(this, result));
|
||||
return;
|
||||
} catch (IOException ex) {
|
||||
t = new IOException("Failed to get " + url, ex);
|
||||
|
||||
@@ -20,7 +20,12 @@ package org.jackhuang.hellominecraft.util.sys;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import org.jackhuang.hellominecraft.util.CollectionUtils;
|
||||
import org.jackhuang.hellominecraft.util.EventHandler;
|
||||
import org.jackhuang.hellominecraft.api.EventHandler;
|
||||
import org.jackhuang.hellominecraft.api.HMCLAPI;
|
||||
import org.jackhuang.hellominecraft.api.event.process.JVMLaunchFailedEvent;
|
||||
import org.jackhuang.hellominecraft.api.event.process.JavaProcessExitedAbnormallyEvent;
|
||||
import org.jackhuang.hellominecraft.api.event.process.JavaProcessStartingEvent;
|
||||
import org.jackhuang.hellominecraft.api.event.process.JavaProcessStoppedEvent;
|
||||
import org.jackhuang.hellominecraft.util.StrUtils;
|
||||
import org.jackhuang.hellominecraft.util.log.HMCLog;
|
||||
import org.jackhuang.hellominecraft.util.log.Level;
|
||||
@@ -32,20 +37,6 @@ import org.jackhuang.hellominecraft.util.log.Level;
|
||||
public class JavaProcessMonitor {
|
||||
|
||||
private final HashSet<Thread> al = new HashSet<>();
|
||||
/**
|
||||
* this event will be executed only if the application returned 0.
|
||||
*/
|
||||
public final EventHandler<JavaProcess> stoppedEvent = new EventHandler<>(this);
|
||||
/**
|
||||
* When the monitored application exited with exit code not zero, this event
|
||||
* will be executed. Event args is the exit code.
|
||||
*/
|
||||
public final EventHandler<Integer> applicationExitedAbnormallyEvent = new EventHandler<>(this);
|
||||
/**
|
||||
* When jvm crashed, this event will be executed. Event args is the exit
|
||||
* code.
|
||||
*/
|
||||
public final EventHandler<Integer> jvmLaunchFailedEvent = new EventHandler<>(this);
|
||||
private final JavaProcess p;
|
||||
|
||||
public JavaProcessMonitor(JavaProcess p) {
|
||||
@@ -55,24 +46,35 @@ public class JavaProcessMonitor {
|
||||
public JavaProcess getJavaProcess() {
|
||||
return p;
|
||||
}
|
||||
|
||||
private Object tag;
|
||||
|
||||
public Object getTag() {
|
||||
return tag;
|
||||
}
|
||||
|
||||
public void setTag(Object tag) {
|
||||
this.tag = tag;
|
||||
}
|
||||
|
||||
|
||||
public void start() {
|
||||
HMCLAPI.EVENT_BUS.fireChannel(new JavaProcessStartingEvent(this, p));
|
||||
ProcessThread a = new ProcessThread(p);
|
||||
a.stopEvent.register((sender, t) -> {
|
||||
HMCLog.log("Process exit code: " + t.getExitCode());
|
||||
if (t.getExitCode() != 0 || StrUtils.containsOne(t.getStdOutLines(),
|
||||
a.stopEvent.register(event -> {
|
||||
HMCLog.log("Process exit code: " + p.getExitCode());
|
||||
if (p.getExitCode() != 0 || StrUtils.containsOne(p.getStdOutLines(),
|
||||
Arrays.asList("Unable to launch"),
|
||||
x -> Level.guessLevel(x, Level.INFO).lessOrEqual(Level.ERROR)))
|
||||
applicationExitedAbnormallyEvent.execute(t.getExitCode());
|
||||
if (t.getExitCode() != 0 && StrUtils.containsOne(t.getStdOutLines(),
|
||||
HMCLAPI.EVENT_BUS.fireChannel(new JavaProcessExitedAbnormallyEvent(JavaProcessMonitor.this, p));
|
||||
if (p.getExitCode() != 0 && StrUtils.containsOne(p.getStdOutLines(),
|
||||
Arrays.asList("Could not create the Java Virtual Machine.",
|
||||
"Error occurred during initialization of VM",
|
||||
"A fatal exception has occurred. Program will exit.",
|
||||
"Unable to launch"),
|
||||
x -> Level.guessLevel(x, Level.INFO).lessOrEqual(Level.ERROR)))
|
||||
jvmLaunchFailedEvent.execute(t.getExitCode());
|
||||
processThreadStopped((ProcessThread) sender, false);
|
||||
return true;
|
||||
HMCLAPI.EVENT_BUS.fireChannel(new JVMLaunchFailedEvent(JavaProcessMonitor.this, p));
|
||||
processThreadStopped((ProcessThread) event.getSource(), false);
|
||||
});
|
||||
a.start();
|
||||
al.add(a);
|
||||
@@ -84,7 +86,7 @@ public class JavaProcessMonitor {
|
||||
for (Thread a : al)
|
||||
a.interrupt();
|
||||
al.clear();
|
||||
stoppedEvent.execute(p);
|
||||
HMCLAPI.EVENT_BUS.fireChannel(new JavaProcessStoppedEvent(this, p));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,7 +22,8 @@ import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import org.jackhuang.hellominecraft.util.log.HMCLog;
|
||||
import org.jackhuang.hellominecraft.util.EventHandler;
|
||||
import org.jackhuang.hellominecraft.api.EventHandler;
|
||||
import org.jackhuang.hellominecraft.api.SimpleEvent;
|
||||
import org.jackhuang.hellominecraft.util.code.Charsets;
|
||||
|
||||
/**
|
||||
@@ -33,8 +34,8 @@ public class ProcessThread extends Thread {
|
||||
|
||||
JavaProcess p;
|
||||
|
||||
public final EventHandler<String> printlnEvent = new EventHandler<>(this);
|
||||
public final EventHandler<JavaProcess> stopEvent = new EventHandler<>(this);
|
||||
public final EventHandler<SimpleEvent<String>> printlnEvent = new EventHandler<>();
|
||||
public final EventHandler<SimpleEvent<JavaProcess>> stopEvent = new EventHandler<>();
|
||||
|
||||
public ProcessThread(JavaProcess process) {
|
||||
p = process;
|
||||
@@ -55,18 +56,18 @@ public class ProcessThread extends Thread {
|
||||
String line;
|
||||
while (p.isRunning())
|
||||
while ((line = br.readLine()) != null) {
|
||||
printlnEvent.execute(line);
|
||||
System.out.println("Minecraft: " + line);
|
||||
printlnEvent.fire(new SimpleEvent<>(this, line));
|
||||
System.out.println("MC: " + line);
|
||||
p.getStdOutLines().add(line);
|
||||
}
|
||||
while ((line = br.readLine()) != null) {
|
||||
printlnEvent.execute(line);
|
||||
System.out.println("Minecraft: " + line);
|
||||
printlnEvent.fire(new SimpleEvent<>(this, line));
|
||||
System.out.println("MC: " + line);
|
||||
p.getStdOutLines().add(line);
|
||||
}
|
||||
if (p.getProcessManager() != null)
|
||||
p.getProcessManager().onProcessStopped(p);
|
||||
stopEvent.execute(p);
|
||||
stopEvent.fire(new SimpleEvent<>(this, p));
|
||||
} catch (IOException e) {
|
||||
HMCLog.err("An error occured when reading process stdout/stderr.", e);
|
||||
} finally {
|
||||
|
||||
@@ -20,6 +20,7 @@ package org.jackhuang.hellominecraft.util.task;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.EventObject;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedList;
|
||||
@@ -29,7 +30,7 @@ import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import org.jackhuang.hellominecraft.util.EventHandler;
|
||||
import org.jackhuang.hellominecraft.api.EventHandler;
|
||||
import org.jackhuang.hellominecraft.util.log.HMCLog;
|
||||
|
||||
/**
|
||||
@@ -39,7 +40,7 @@ import org.jackhuang.hellominecraft.util.log.HMCLog;
|
||||
public class TaskList extends Thread {
|
||||
|
||||
List<Task> taskQueue = Collections.synchronizedList(new LinkedList<>());
|
||||
public final EventHandler<Object> doneEvent = new EventHandler<>(this);
|
||||
public final EventHandler<EventObject> doneEvent = new EventHandler<>();
|
||||
ArrayList<DoingDoneListener<Task>> taskListener = new ArrayList<>();
|
||||
|
||||
int totTask;
|
||||
@@ -159,7 +160,7 @@ public class TaskList extends Thread {
|
||||
executeTask(taskQueue.remove(0));
|
||||
if (shouldContinue) {
|
||||
HMCLog.log("Tasks are successfully finished.");
|
||||
doneEvent.execute(null);
|
||||
doneEvent.fire(new EventObject(this));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
128
HMCUtils/src/main/java/org/jackhuang/hellominecraft/util/ui/Page.java
Executable file
128
HMCUtils/src/main/java/org/jackhuang/hellominecraft/util/ui/Page.java
Executable file
@@ -0,0 +1,128 @@
|
||||
/*
|
||||
* Hello Minecraft! Launcher.
|
||||
* Copyright (C) 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 3 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. If not, see {http://www.gnu.org/licenses/}.
|
||||
*/
|
||||
package org.jackhuang.hellominecraft.util.ui;
|
||||
|
||||
import java.awt.AlphaComposite;
|
||||
import java.awt.Graphics;
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.image.BufferedImage;
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.SwingUtilities;
|
||||
import javax.swing.Timer;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author huangyuhui
|
||||
*/
|
||||
public class Page extends JPanel implements Selectable {
|
||||
|
||||
boolean selected = false;
|
||||
|
||||
@Override
|
||||
public boolean isSelected() {
|
||||
return selected;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSelect() {
|
||||
if (!selected)
|
||||
animate();
|
||||
selected = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLeave() {
|
||||
selected = false;
|
||||
}
|
||||
|
||||
boolean created = false;
|
||||
|
||||
@Override
|
||||
public void onCreate() {
|
||||
created = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isCreated() {
|
||||
return created;
|
||||
}
|
||||
|
||||
// -------------------
|
||||
// Animation
|
||||
// -------------------
|
||||
private static final int ANIMATION_LENGTH = 10;
|
||||
|
||||
public Page() {
|
||||
timer = new Timer(1, (e) -> {
|
||||
SwingUtilities.invokeLater(() -> {
|
||||
Page.this.repaint();
|
||||
offsetX += 0.15;
|
||||
if (offsetX >= ANIMATION_LENGTH) {
|
||||
timer.stop();
|
||||
Page.this.repaint();
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
BufferedImage cache = null;
|
||||
|
||||
@Override
|
||||
public void paint(Graphics g) {
|
||||
if (!(g instanceof Graphics2D)) {
|
||||
super.paint(g);
|
||||
return;
|
||||
}
|
||||
double pgs = 1 - Math.sin(Math.PI / 2 / ANIMATION_LENGTH * offsetX);
|
||||
if (Math.abs(ANIMATION_LENGTH - offsetX) < 0.1) {
|
||||
super.paint(g);
|
||||
return;
|
||||
}
|
||||
|
||||
if (offsetX == 0) {
|
||||
cache = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB);
|
||||
Graphics2D g2d = cache.createGraphics();
|
||||
if (isOpaque()) {
|
||||
g2d.setColor(getBackground());
|
||||
g2d.fillRect(0, 0, getWidth(), getHeight());
|
||||
}
|
||||
super.paint(g2d);
|
||||
g2d.dispose();
|
||||
}
|
||||
if (pgs > 1)
|
||||
pgs = 1;
|
||||
if (pgs < 0)
|
||||
pgs = 0;
|
||||
Graphics2D gg = (Graphics2D) g;
|
||||
gg.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, (float) (1 - pgs)));
|
||||
g.drawImage(cache, (int) (pgs * 50), 0, this);
|
||||
}
|
||||
|
||||
double offsetX = ANIMATION_LENGTH;
|
||||
|
||||
Timer timer;
|
||||
|
||||
protected boolean animationEnabled = true;
|
||||
|
||||
public void animate() {
|
||||
if (animationEnabled) {
|
||||
offsetX = 0;
|
||||
timer.start();
|
||||
}
|
||||
}
|
||||
}
|
||||
35
HMCUtils/src/main/java/org/jackhuang/hellominecraft/util/ui/Selectable.java
Executable file
35
HMCUtils/src/main/java/org/jackhuang/hellominecraft/util/ui/Selectable.java
Executable file
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
* Hello Minecraft! Launcher.
|
||||
* Copyright (C) 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 3 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. If not, see {http://www.gnu.org/licenses/}.
|
||||
*/
|
||||
package org.jackhuang.hellominecraft.util.ui;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author huangyuhui
|
||||
*/
|
||||
public interface Selectable {
|
||||
|
||||
void onCreate();
|
||||
|
||||
boolean isCreated();
|
||||
|
||||
void onSelect();
|
||||
|
||||
boolean isSelected();
|
||||
|
||||
void onLeave();
|
||||
}
|
||||
Reference in New Issue
Block a user