Update Readme.md for HMCLCore introduction

This commit is contained in:
huangyuhui
2018-01-16 19:34:53 +08:00
parent 4b39c046a7
commit b90d6b7b8c
90 changed files with 4192 additions and 4615 deletions

View File

@@ -183,9 +183,9 @@ public abstract class Task {
return new TaskExecutor(this);
}
public final TaskExecutor executor(TaskListener taskListener) {
public final TaskExecutor executor(Function<TaskExecutor, TaskListener> taskListener) {
TaskExecutor executor = new TaskExecutor(this);
executor.setTaskListener(taskListener);
executor.setTaskListener(taskListener.apply(executor));
return executor;
}
@@ -204,13 +204,21 @@ public abstract class Task {
}
public final TaskExecutor subscribe(Scheduler scheduler, ExceptionalConsumer<AutoTypingMap<String>, ?> closure) {
return subscribe(of(closure, scheduler));
return subscribe(of(scheduler, closure));
}
public final TaskExecutor subscribe(Scheduler scheduler, ExceptionalRunnable<?> closure) {
return subscribe(of(scheduler, i -> closure.run()));
}
public final TaskExecutor subscribe(ExceptionalConsumer<AutoTypingMap<String>, ?> closure) {
return subscribe(of(closure));
}
public final TaskExecutor subscribe(ExceptionalRunnable<?> closure) {
return subscribe(of(closure));
}
public final Task then(Task b) {
return then(s -> b);
}
@@ -237,13 +245,17 @@ public abstract class Task {
}
public static Task of(ExceptionalConsumer<AutoTypingMap<String>, ?> closure) {
return of(closure, Schedulers.defaultScheduler());
return of(Schedulers.defaultScheduler(), closure);
}
public static Task of(ExceptionalConsumer<AutoTypingMap<String>, ?> closure, Scheduler scheduler) {
public static Task of(Scheduler scheduler, ExceptionalConsumer<AutoTypingMap<String>, ?> closure) {
return new SimpleTask(closure, scheduler);
}
public static Task of(Scheduler scheduler, ExceptionalRunnable<?> closure) {
return new SimpleTask(i -> closure.run(), scheduler);
}
public static <V> TaskResult<V> ofResult(String id, Callable<V> callable) {
return new TaskCallable<>(id, callable);
}

View File

@@ -19,6 +19,7 @@ package org.jackhuang.hmcl.util;
import java.util.Collection;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
/**
@@ -38,6 +39,10 @@ public final class AutoTypingMap<K> {
return (V) impl.get(key);
}
public <V> Optional<V> getOptional(K key) {
return Optional.ofNullable(get(key));
}
public void set(K key, Object value) {
if (value != null)
impl.put(key, value);

View File

@@ -71,12 +71,12 @@ public final class Lang {
}
}
public static <T, R, E extends Exception> Function<T, R> hideException(ExceptionalFunction<T, R, E> function) {
public static <T, R, E extends Exception> Function<T, R> hideFunction(ExceptionalFunction<T, R, E> function) {
return r -> invoke(function, r);
}
public static <T, R, E extends Exception> Function<T, R> liftException(ExceptionalFunction<T, R, E> function) throws E {
return hideException(function);
public static <T, R, E extends Exception> Function<T, R> liftFunction(ExceptionalFunction<T, R, E> function) throws E {
return hideFunction(function);
}
/**
@@ -107,6 +107,33 @@ public final class Lang {
return hideException(supplier);
}
/**
* This method will call a method without checked exceptions
* by treating the compiler.
*
* If this method throws a checked exception,
* it will still abort the application because of the exception.
*
* @param <T> type of result.
* @param consumer your method.
* @return the result of the method to invoke.
*/
public static <T, E extends Exception> void invokeConsumer(ExceptionalConsumer<T, E> consumer, T t) {
try {
consumer.accept(t);
} catch (Exception e) {
throwable(e);
}
}
public static <T, E extends Exception> Consumer<T> hideConsumer(ExceptionalConsumer<T, E> consumer) {
return it -> invokeConsumer(consumer, it);
}
public static <T, E extends Exception> Consumer<T> liftConsumer(ExceptionalConsumer<T, E> consumer) throws E {
return hideConsumer(consumer);
}
public static <E extends Exception> boolean test(ExceptionalSupplier<Boolean, E> r) {
try {
return r.get();
@@ -248,17 +275,25 @@ public final class Lang {
return () -> asIterator(enumeration);
}
public static int parseInt(String string, int defaultValue) {
public static int parseInt(Object string, int defaultValue) {
try {
return Integer.parseInt(string);
return Integer.parseInt(string.toString());
} catch (NumberFormatException e) {
return defaultValue;
}
}
public static Integer toIntOrNull(String string) {
public static Integer toIntOrNull(Object string) {
try {
return Integer.parseInt(string);
return Integer.parseInt(string.toString());
} catch (NumberFormatException e) {
return null;
}
}
public static Double toDoubleOrNull(Object string) {
try {
return Double.parseDouble(string.toString());
} catch (NumberFormatException e) {
return null;
}
@@ -268,4 +303,9 @@ public final class Lang {
for (T a : t) if (a != null) return a;
return null;
}
public static <T> T apply(T t, Consumer<T> consumer) {
consumer.accept(t);
return t;
}
}

View File

@@ -1,152 +0,0 @@
/*
* Hello Minecraft! Launcher.
* Copyright (C) 2017 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.hmcl
import org.jackhuang.hmcl.auth.OfflineAccount
import org.jackhuang.hmcl.download.DefaultDependencyManager
import org.jackhuang.hmcl.download.liteloader.LiteLoaderVersionList
import org.jackhuang.hmcl.download.BMCLAPIDownloadProvider
import org.jackhuang.hmcl.download.MojangDownloadProvider
import org.jackhuang.hmcl.game.DefaultGameRepository
import org.jackhuang.hmcl.launch.DefaultLauncher
import org.jackhuang.hmcl.game.LaunchOptions
import org.jackhuang.hmcl.game.minecraftVersion
import org.jackhuang.hmcl.launch.ProcessListener
import org.jackhuang.hmcl.util.makeCommand
import org.jackhuang.hmcl.task.Task
import org.jackhuang.hmcl.task.TaskListener
import org.jackhuang.hmcl.util.Log4jLevel
import java.io.File
import java.net.InetSocketAddress
import java.net.Proxy
class Test {
val ss = Proxy(Proxy.Type.SOCKS, InetSocketAddress("127.0.0.1", 1080))
val repository = DefaultGameRepository(File(".minecraft").absoluteFile)
val dependency = DefaultDependencyManager(
repository = repository,
downloadProvider = MojangDownloadProvider,
proxy = ss)
init {
repository.refreshVersions()
}
fun launch() {
val launcher = DefaultLauncher(
repository = repository,
versionId = "test",
account = OfflineAccount.fromUsername("player007").logIn(),
options = LaunchOptions(gameDir = repository.baseDirectory),
listener = object : ProcessListener {
override fun onLog(log: String, level: Log4jLevel) {
println(log)
}
override fun onExit(exitCode: Int, exitType: ProcessListener.ExitType) {
println("Process exited then exit code $exitCode")
}
},
isDaemon = false
)
println(makeCommand(launcher.rawCommandLine))
launcher.launch()
try {
Thread.sleep(Long.MAX_VALUE)
} catch (e: InterruptedException) {
return
}
}
fun downloadNewVersion() {
val thread = Thread.currentThread()
dependency.gameBuilder()
.name("test")
.gameVersion("1.12")
.version("forge", "14.21.1.2426")
.version("liteloader", "1.12-SNAPSHOT-4")
.version("optifine", "HD_U_C4")
.buildAsync().executor().apply {
taskListener = taskListener(thread)
}.start()
try {
Thread.sleep(Long.MAX_VALUE)
} catch (e: InterruptedException) {
return
}
}
fun completeGame() {
val thread = Thread.currentThread()
val version = repository.getVersion("test").resolve(repository)
dependency.checkGameCompletionAsync(version).executor().apply {
taskListener = taskListener(thread)
}.start()
try {
Thread.sleep(Long.MAX_VALUE)
} catch (e: InterruptedException) {
return
}
}
fun installForge() {
val thread = Thread.currentThread()
val version = repository.getVersion("test").resolve(repository)
val minecraftVersion = minecraftVersion(repository.getVersionJar(version)) ?: ""
// optifine HD_U_C4
// forge 14.21.1.2426
// liteloader 1.12-SNAPSHOT-4
dependency.installLibraryAsync(minecraftVersion, version, "liteloader", "1.12-SNAPSHOT-4").executor().apply {
taskListener = taskListener(thread)
}.start()
try {
Thread.sleep(Long.MAX_VALUE)
} catch (e: InterruptedException) {
return
}
}
fun refreshAsync() {
val thread = Thread.currentThread()
LiteLoaderVersionList.refreshAsync(BMCLAPIDownloadProvider).executor().apply {
taskListener = taskListener(thread)
}.start()
try {
Thread.sleep(Long.MAX_VALUE)
} catch (e: InterruptedException) {
return
}
}
fun taskListener(thread: Thread) = object : TaskListener {
override fun onReady(task: Task) {
}
override fun onFinished(task: Task) {
}
override fun onFailed(task: Task, throwable: Throwable) {
}
override fun onTerminate() {
thread.interrupt()
}
}
}