2015-12-14 11:01:16 -08:00
|
|
|
package amidst.gui.profileselect;
|
2014-01-03 21:04:00 -08:00
|
|
|
|
2017-05-31 15:47:12 -07:00
|
|
|
import java.io.IOException;
|
2017-06-01 14:50:10 -07:00
|
|
|
import java.util.Optional;
|
2015-12-21 16:07:45 -08:00
|
|
|
|
2015-11-13 08:53:43 -08:00
|
|
|
import amidst.Application;
|
2015-12-12 14:00:35 -08:00
|
|
|
import amidst.documentation.AmidstThread;
|
|
|
|
import amidst.documentation.CalledOnlyBy;
|
|
|
|
import amidst.documentation.NotThreadSafe;
|
2016-11-20 14:48:53 -08:00
|
|
|
import amidst.logging.AmidstLogger;
|
2016-11-20 17:47:05 -08:00
|
|
|
import amidst.logging.AmidstMessageBox;
|
2018-03-09 23:48:03 -08:00
|
|
|
import amidst.gameengineabstraction.file.IUnresolvedLauncherProfile;
|
2017-06-01 14:50:10 -07:00
|
|
|
import amidst.mojangapi.LauncherProfileRunner;
|
|
|
|
import amidst.mojangapi.RunningLauncherProfile;
|
2017-06-01 11:56:03 -07:00
|
|
|
import amidst.mojangapi.file.LauncherProfile;
|
2017-06-01 13:06:45 -07:00
|
|
|
import amidst.mojangapi.file.VersionListProvider;
|
2015-12-14 16:40:07 -08:00
|
|
|
import amidst.mojangapi.minecraftinterface.local.LocalMinecraftInterfaceCreationException;
|
2017-06-01 12:18:52 -07:00
|
|
|
import amidst.parsing.FormatException;
|
2015-12-07 17:44:43 -08:00
|
|
|
import amidst.threading.WorkerExecutor;
|
2014-01-03 21:04:00 -08:00
|
|
|
|
2015-12-12 14:00:35 -08:00
|
|
|
@NotThreadSafe
|
2015-12-14 11:01:16 -08:00
|
|
|
public class LocalProfileComponent extends ProfileComponent {
|
2015-12-06 17:47:47 -08:00
|
|
|
private final Application application;
|
2015-12-06 20:28:29 -08:00
|
|
|
private final WorkerExecutor workerExecutor;
|
2017-06-01 16:39:48 -07:00
|
|
|
private final VersionListProvider versionListProvider;
|
2017-06-01 14:50:10 -07:00
|
|
|
private final LauncherProfileRunner launcherProfileRunner;
|
2018-03-09 23:48:03 -08:00
|
|
|
private final IUnresolvedLauncherProfile unresolvedProfile;
|
2015-12-06 20:28:29 -08:00
|
|
|
|
2017-06-01 14:50:10 -07:00
|
|
|
private volatile boolean isResolving = false;
|
|
|
|
private volatile boolean failedResolving = false;
|
2015-12-14 06:27:35 -08:00
|
|
|
private volatile boolean isLoading = false;
|
|
|
|
private volatile boolean failedLoading = false;
|
2017-05-31 15:47:12 -07:00
|
|
|
private volatile LauncherProfile resolvedProfile;
|
2015-11-13 08:53:43 -08:00
|
|
|
|
2015-12-12 14:00:35 -08:00
|
|
|
@CalledOnlyBy(AmidstThread.EDT)
|
2016-04-11 05:29:06 -07:00
|
|
|
public LocalProfileComponent(
|
|
|
|
Application application,
|
|
|
|
WorkerExecutor workerExecutor,
|
2017-06-01 16:39:48 -07:00
|
|
|
VersionListProvider versionListProvider,
|
2017-06-01 14:50:10 -07:00
|
|
|
LauncherProfileRunner launcherProfileRunner,
|
2018-03-09 23:48:03 -08:00
|
|
|
IUnresolvedLauncherProfile unresolvedProfile) {
|
2015-11-13 09:16:43 -08:00
|
|
|
this.application = application;
|
2015-12-06 20:28:29 -08:00
|
|
|
this.workerExecutor = workerExecutor;
|
2017-06-01 16:39:48 -07:00
|
|
|
this.versionListProvider = versionListProvider;
|
2017-06-01 14:50:10 -07:00
|
|
|
this.launcherProfileRunner = launcherProfileRunner;
|
2017-05-31 15:47:12 -07:00
|
|
|
this.unresolvedProfile = unresolvedProfile;
|
2015-11-13 09:16:43 -08:00
|
|
|
initComponent();
|
2015-12-06 20:28:29 -08:00
|
|
|
}
|
|
|
|
|
2015-12-12 14:00:35 -08:00
|
|
|
@CalledOnlyBy(AmidstThread.EDT)
|
2017-06-01 16:39:48 -07:00
|
|
|
@Override
|
|
|
|
public void resolveLater() {
|
|
|
|
resolvedProfile = null;
|
2017-06-01 14:50:10 -07:00
|
|
|
isResolving = true;
|
2015-12-14 16:15:53 -08:00
|
|
|
repaintComponent();
|
2017-06-01 14:50:10 -07:00
|
|
|
workerExecutor.run(this::tryResolve, this::resolveFinished);
|
2016-01-15 10:02:38 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
@CalledOnlyBy(AmidstThread.WORKER)
|
2017-06-01 16:39:48 -07:00
|
|
|
private Optional<LauncherProfile> tryResolve() {
|
2016-01-15 10:02:38 -08:00
|
|
|
try {
|
2017-06-02 07:34:31 -07:00
|
|
|
return Optional.of(unresolvedProfile.resolveToVanilla(versionListProvider.getRemoteOrElseLocal()));
|
2017-06-01 12:18:52 -07:00
|
|
|
} catch (FormatException | IOException e) {
|
2016-11-20 18:30:50 -08:00
|
|
|
AmidstLogger.warn(e);
|
2017-06-01 16:39:48 -07:00
|
|
|
return Optional.empty();
|
2016-01-15 10:02:38 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@CalledOnlyBy(AmidstThread.EDT)
|
2017-06-01 16:39:48 -07:00
|
|
|
private void resolveFinished(Optional<LauncherProfile> launcherProfile) {
|
2017-06-01 14:50:10 -07:00
|
|
|
isResolving = false;
|
2017-06-01 16:39:48 -07:00
|
|
|
failedResolving = !launcherProfile.isPresent();
|
|
|
|
resolvedProfile = launcherProfile.orElse(null);
|
2016-01-15 10:02:38 -08:00
|
|
|
repaintComponent();
|
2015-12-06 20:28:29 -08:00
|
|
|
}
|
|
|
|
|
2015-12-12 14:00:35 -08:00
|
|
|
@CalledOnlyBy(AmidstThread.EDT)
|
2014-01-03 21:04:00 -08:00
|
|
|
@Override
|
2015-12-14 06:27:35 -08:00
|
|
|
public void load() {
|
|
|
|
isLoading = true;
|
|
|
|
repaintComponent();
|
2017-06-02 07:34:31 -07:00
|
|
|
displayModdedMinecraftInfoIfNecessary();
|
2016-01-15 10:02:38 -08:00
|
|
|
workerExecutor.run(this::tryLoad, this::loadFinished);
|
|
|
|
}
|
|
|
|
|
2017-06-02 07:34:31 -07:00
|
|
|
@CalledOnlyBy(AmidstThread.EDT)
|
|
|
|
private void displayModdedMinecraftInfoIfNecessary() {
|
|
|
|
if (!resolvedProfile.isVersionListedInProfile()) {
|
|
|
|
String message = "Amidst does not support modded Minecraft versions! Using underlying vanilla Minecraft version "
|
|
|
|
+ resolvedProfile.getVersionId() + " instead.";
|
|
|
|
AmidstLogger.info(message);
|
|
|
|
AmidstMessageBox.displayInfo("Info", message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-15 10:02:38 -08:00
|
|
|
@CalledOnlyBy(AmidstThread.WORKER)
|
2017-06-01 14:50:10 -07:00
|
|
|
private Optional<RunningLauncherProfile> tryLoad() {
|
2016-01-15 10:02:38 -08:00
|
|
|
try {
|
2016-11-20 14:48:53 -08:00
|
|
|
AmidstLogger.info(
|
2017-06-02 04:19:09 -07:00
|
|
|
"using minecraft launcher profile '" + resolvedProfile.getProfileName() + "' with versionId '"
|
2017-06-02 07:34:31 -07:00
|
|
|
+ resolvedProfile.getVersionName() + "'");
|
2017-06-01 14:50:10 -07:00
|
|
|
return Optional.of(launcherProfileRunner.run(resolvedProfile));
|
2016-01-15 10:02:38 -08:00
|
|
|
} catch (LocalMinecraftInterfaceCreationException e) {
|
2016-11-20 18:30:50 -08:00
|
|
|
AmidstLogger.error(e);
|
|
|
|
AmidstMessageBox.displayError("Error", e);
|
2017-06-01 14:50:10 -07:00
|
|
|
return Optional.empty();
|
2016-01-15 10:02:38 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@CalledOnlyBy(AmidstThread.EDT)
|
2017-06-01 14:50:10 -07:00
|
|
|
private void loadFinished(Optional<RunningLauncherProfile> runningLauncherProfile) {
|
2016-01-15 10:02:38 -08:00
|
|
|
isLoading = false;
|
2017-06-01 14:50:10 -07:00
|
|
|
failedLoading = !runningLauncherProfile.isPresent();
|
2016-01-15 10:02:38 -08:00
|
|
|
repaintComponent();
|
2017-06-01 14:50:10 -07:00
|
|
|
if (runningLauncherProfile.isPresent()) {
|
|
|
|
application.displayMainWindow(runningLauncherProfile.get());
|
2016-01-15 10:02:38 -08:00
|
|
|
}
|
2015-11-13 08:53:43 -08:00
|
|
|
}
|
|
|
|
|
2015-12-12 14:00:35 -08:00
|
|
|
@CalledOnlyBy(AmidstThread.EDT)
|
2015-11-13 08:53:43 -08:00
|
|
|
@Override
|
2017-06-01 14:50:10 -07:00
|
|
|
protected boolean isResolving() {
|
|
|
|
return isResolving;
|
2015-12-14 16:15:53 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
@CalledOnlyBy(AmidstThread.EDT)
|
|
|
|
@Override
|
2017-06-01 14:50:10 -07:00
|
|
|
protected boolean failedResolving() {
|
|
|
|
return failedResolving;
|
2015-12-14 06:27:35 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
@CalledOnlyBy(AmidstThread.EDT)
|
|
|
|
@Override
|
2015-12-14 16:15:53 -08:00
|
|
|
protected boolean isLoading() {
|
2015-12-14 06:27:35 -08:00
|
|
|
return isLoading;
|
|
|
|
}
|
|
|
|
|
|
|
|
@CalledOnlyBy(AmidstThread.EDT)
|
|
|
|
@Override
|
2015-12-14 16:15:53 -08:00
|
|
|
protected boolean failedLoading() {
|
2015-12-14 06:27:35 -08:00
|
|
|
return failedLoading;
|
2014-01-03 21:04:00 -08:00
|
|
|
}
|
|
|
|
|
2015-12-12 14:00:35 -08:00
|
|
|
@CalledOnlyBy(AmidstThread.EDT)
|
2014-01-03 21:04:00 -08:00
|
|
|
@Override
|
2015-12-14 16:15:53 -08:00
|
|
|
protected boolean isReadyToLoad() {
|
2017-06-01 16:39:48 -07:00
|
|
|
return resolvedProfile != null;
|
2015-12-14 16:15:53 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
@CalledOnlyBy(AmidstThread.EDT)
|
|
|
|
@Override
|
|
|
|
protected String getProfileName() {
|
2017-05-31 15:47:12 -07:00
|
|
|
return unresolvedProfile.getName();
|
2015-11-13 08:53:43 -08:00
|
|
|
}
|
|
|
|
|
2015-12-12 14:00:35 -08:00
|
|
|
@CalledOnlyBy(AmidstThread.EDT)
|
2015-11-13 08:53:43 -08:00
|
|
|
@Override
|
2015-12-14 16:15:53 -08:00
|
|
|
protected String getVersionName() {
|
2017-06-01 16:39:48 -07:00
|
|
|
if (resolvedProfile != null) {
|
2017-06-02 07:34:31 -07:00
|
|
|
return resolvedProfile.getVersionName();
|
2015-12-10 10:13:17 -08:00
|
|
|
} else {
|
|
|
|
return "";
|
|
|
|
}
|
2014-01-03 21:04:00 -08:00
|
|
|
}
|
|
|
|
}
|