mirror of
https://github.com/SKCraft/Launcher.git
synced 2025-02-15 01:31:22 +01:00
Change LaunchSupervisor to take a LaunchOptions value object.
This commit is contained in:
parent
3306e67304
commit
cdfb01d9a6
@ -11,6 +11,8 @@ import com.skcraft.launcher.Instance;
|
||||
import com.skcraft.launcher.InstanceList;
|
||||
import com.skcraft.launcher.Launcher;
|
||||
import com.skcraft.launcher.launch.LaunchListener;
|
||||
import com.skcraft.launcher.launch.LaunchOptions;
|
||||
import com.skcraft.launcher.launch.LaunchOptions.UpdatePolicy;
|
||||
import com.skcraft.launcher.swing.*;
|
||||
import com.skcraft.launcher.util.SharedLocale;
|
||||
import com.skcraft.launcher.util.SwingExecutor;
|
||||
@ -349,7 +351,13 @@ public class LauncherFrame extends JFrame {
|
||||
boolean permitUpdate = updateCheck.isSelected();
|
||||
Instance instance = launcher.getInstances().get(instancesTable.getSelectedRow());
|
||||
|
||||
launcher.getLaunchSupervisor().launch(this, instance, permitUpdate, new LaunchListenerImpl(this));
|
||||
LaunchOptions options = new LaunchOptions.Builder()
|
||||
.setInstance(instance)
|
||||
.setListener(new LaunchListenerImpl(this))
|
||||
.setUpdatePolicy(permitUpdate ? UpdatePolicy.UPDATE_IF_SESSION_ONLINE : UpdatePolicy.NO_UPDATE)
|
||||
.setWindow(this)
|
||||
.build();
|
||||
launcher.getLaunchSupervisor().launch(options);
|
||||
}
|
||||
|
||||
private static class LaunchListenerImpl implements LaunchListener {
|
||||
|
@ -0,0 +1,96 @@
|
||||
/*
|
||||
* SK's Minecraft Launcher
|
||||
* Copyright (C) 2010-2014 Albert Pham <http://www.sk89q.com> and contributors
|
||||
* Please see LICENSE.txt for license information.
|
||||
*/
|
||||
|
||||
package com.skcraft.launcher.launch;
|
||||
|
||||
import com.skcraft.launcher.Instance;
|
||||
import com.skcraft.launcher.auth.Session;
|
||||
import lombok.Data;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkNotNull;
|
||||
|
||||
@Data
|
||||
public class LaunchOptions {
|
||||
|
||||
private final Window window;
|
||||
private final Instance instance;
|
||||
private final UpdatePolicy updatePolicy;
|
||||
private final LaunchListener listener;
|
||||
private final Session session;
|
||||
|
||||
@Data
|
||||
public static class Builder {
|
||||
|
||||
private Window window = null;
|
||||
private Instance instance;
|
||||
private UpdatePolicy updatePolicy = UpdatePolicy.UPDATE_IF_SESSION_ONLINE;
|
||||
private LaunchListener listener = new DummyLaunchListener();
|
||||
private Session session;
|
||||
|
||||
public Builder setWindow(Window window) {
|
||||
this.window = window;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setInstance(Instance instance) {
|
||||
this.instance = instance;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setUpdatePolicy(UpdatePolicy updatePolicy) {
|
||||
checkNotNull(updatePolicy, "updatePolicy");
|
||||
this.updatePolicy = updatePolicy;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setListener(LaunchListener listener) {
|
||||
checkNotNull(listener, "listener");
|
||||
this.listener = listener;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setSession(Session session) {
|
||||
this.session = session;
|
||||
return this;
|
||||
}
|
||||
|
||||
public LaunchOptions build() {
|
||||
checkNotNull(instance, "instance");
|
||||
return new LaunchOptions(window, instance, updatePolicy, listener, session);
|
||||
}
|
||||
}
|
||||
|
||||
public enum UpdatePolicy {
|
||||
NO_UPDATE(false),
|
||||
UPDATE_IF_SESSION_ONLINE(true),
|
||||
ALWAYS_UPDATE(true);
|
||||
|
||||
@Getter
|
||||
private final boolean updateEnabled;
|
||||
|
||||
UpdatePolicy(boolean updateEnabled) {
|
||||
this.updateEnabled = updateEnabled;
|
||||
}
|
||||
}
|
||||
|
||||
private static class DummyLaunchListener implements LaunchListener {
|
||||
@Override
|
||||
public void instancesUpdated() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void gameStarted() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void gameClosed() {
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -15,6 +15,7 @@ import com.skcraft.launcher.Launcher;
|
||||
import com.skcraft.launcher.auth.Session;
|
||||
import com.skcraft.launcher.dialog.LoginDialog;
|
||||
import com.skcraft.launcher.dialog.ProgressDialog;
|
||||
import com.skcraft.launcher.launch.LaunchOptions.UpdatePolicy;
|
||||
import com.skcraft.launcher.persistence.Persistence;
|
||||
import com.skcraft.launcher.swing.SwingHelper;
|
||||
import com.skcraft.launcher.update.Updater;
|
||||
@ -42,9 +43,13 @@ public class LaunchSupervisor {
|
||||
this.launcher = launcher;
|
||||
}
|
||||
|
||||
public void launch(final Window window, final Instance instance, boolean permitUpdate, final LaunchListener listener) {
|
||||
public void launch(LaunchOptions options) {
|
||||
final Window window = options.getWindow();
|
||||
final Instance instance = options.getInstance();
|
||||
final LaunchListener listener = options.getListener();
|
||||
|
||||
try {
|
||||
boolean update = permitUpdate && instance.isUpdatePending();
|
||||
boolean update = options.getUpdatePolicy().isUpdateEnabled() && instance.isUpdatePending();
|
||||
|
||||
// Store last access date
|
||||
Date now = new Date();
|
||||
@ -52,9 +57,14 @@ public class LaunchSupervisor {
|
||||
Persistence.commitAndForget(instance);
|
||||
|
||||
// Perform login
|
||||
final Session session = LoginDialog.showLoginRequest(window, launcher);
|
||||
if (session == null) {
|
||||
return;
|
||||
final Session session;
|
||||
if (options.getSession() != null) {
|
||||
session = options.getSession();
|
||||
} else {
|
||||
session = LoginDialog.showLoginRequest(window, launcher);
|
||||
if (session == null) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// If we have to update, we have to update
|
||||
@ -65,7 +75,7 @@ public class LaunchSupervisor {
|
||||
if (update) {
|
||||
// Execute the updater
|
||||
Updater updater = new Updater(launcher, instance);
|
||||
updater.setOnline(session.isOnline());
|
||||
updater.setOnline(options.getUpdatePolicy() == UpdatePolicy.ALWAYS_UPDATE || session.isOnline());
|
||||
ObservableFuture<Instance> future = new ObservableFuture<Instance>(
|
||||
launcher.getExecutor().submit(updater), updater);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user