diff --git a/launcher/src/main/java/com/skcraft/launcher/Launcher.java b/launcher/src/main/java/com/skcraft/launcher/Launcher.java index 8ca6638..10ca1df 100644 --- a/launcher/src/main/java/com/skcraft/launcher/Launcher.java +++ b/launcher/src/main/java/com/skcraft/launcher/Launcher.java @@ -12,8 +12,8 @@ import com.google.common.base.Supplier; import com.google.common.util.concurrent.ListeningExecutorService; import com.google.common.util.concurrent.MoreExecutors; +import com.skcraft.launcher.auth.AccountList; import com.skcraft.launcher.auth.LoginService; -import com.skcraft.launcher.auth.NewAccountList; import com.skcraft.launcher.auth.UserType; import com.skcraft.launcher.auth.YggdrasilLoginService; import com.skcraft.launcher.launch.LaunchSupervisor; @@ -64,7 +64,7 @@ @Getter private final Properties properties; @Getter private final InstanceList instances; @Getter private final Configuration config; - @Getter private final NewAccountList accounts; + @Getter private final AccountList accounts; @Getter private final AssetsRoot assets; @Getter private final LaunchSupervisor launchSupervisor = new LaunchSupervisor(this); @Getter private final UpdateManager updateManager = new UpdateManager(this); @@ -97,7 +97,7 @@ this.instances = new InstanceList(this); this.assets = new AssetsRoot(new File(baseDir, "assets")); this.config = Persistence.load(new File(configDir, "config.json"), Configuration.class); - this.accounts = Persistence.load(new File(configDir, "accounts.dat"), NewAccountList.class); + this.accounts = Persistence.load(new File(configDir, "accounts.dat"), AccountList.class); setDefaultConfig(); diff --git a/launcher/src/main/java/com/skcraft/launcher/auth/Account.java b/launcher/src/main/java/com/skcraft/launcher/auth/Account.java deleted file mode 100644 index e99bade..0000000 --- a/launcher/src/main/java/com/skcraft/launcher/auth/Account.java +++ /dev/null @@ -1,91 +0,0 @@ -/* - * SK's Minecraft Launcher - * Copyright (C) 2010-2014 Albert Pham and contributors - * Please see LICENSE.txt for license information. - */ - -package com.skcraft.launcher.auth; - -import com.fasterxml.jackson.annotation.JsonIgnoreProperties; -import com.google.common.base.Strings; -import lombok.Data; -import lombok.NonNull; - -import java.util.Date; - -/** - * A user account that can be stored and loaded. - */ -@Data -@JsonIgnoreProperties(ignoreUnknown = true) -public class Account implements Comparable { - - private String id; - private String password; - private Date lastUsed; - - /** - * Create a new account. - */ - public Account() { - } - - /** - * Create a new account with the given ID. - * - * @param id the ID - */ - public Account(String id) { - setId(id); - } - - /** - * Set the account's stored password, that may be stored to disk. - * - * @param password the password - */ - public void setPassword(String password) { - if (password != null && password.isEmpty()) { - password = null; - } - this.password = Strings.emptyToNull(password); - } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - - Account account = (Account) o; - - if (!id.equalsIgnoreCase(account.id)) return false; - - return true; - } - - @Override - public int hashCode() { - return id.toLowerCase().hashCode(); - } - - @Override - public int compareTo(@NonNull Account o) { - Date otherDate = o.getLastUsed(); - - if (otherDate == null && lastUsed == null) { - return 0; - } else if (otherDate == null) { - return -1; - } else if (lastUsed == null) { - return 1; - } else { - return -lastUsed.compareTo(otherDate); - } - } - - @Override - public String toString() { - return getId(); - } - -} diff --git a/launcher/src/main/java/com/skcraft/launcher/auth/AccountList.java b/launcher/src/main/java/com/skcraft/launcher/auth/AccountList.java index a86356d..0595577 100644 --- a/launcher/src/main/java/com/skcraft/launcher/auth/AccountList.java +++ b/launcher/src/main/java/com/skcraft/launcher/auth/AccountList.java @@ -1,134 +1,78 @@ -/* - * SK's Minecraft Launcher - * Copyright (C) 2010-2014 Albert Pham and contributors - * Please see LICENSE.txt for license information. - */ - package com.skcraft.launcher.auth; -import com.fasterxml.jackson.annotation.JsonAutoDetect; +import com.beust.jcommander.internal.Lists; import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; -import com.fasterxml.jackson.annotation.JsonProperty; +import com.skcraft.launcher.dialog.component.ListListenerReducer; import com.skcraft.launcher.persistence.Scrambled; import lombok.Getter; -import lombok.NonNull; +import lombok.Setter; +import lombok.ToString; +import org.apache.commons.lang.RandomStringUtils; import javax.swing.*; -import java.util.ArrayList; -import java.util.Collections; -import java.util.Iterator; +import javax.swing.event.ListDataEvent; +import javax.swing.event.ListDataListener; import java.util.List; /** - * A list of accounts that can be stored to disk. + * Persisted account list */ -@Scrambled("ACCOUNT_LIST") +@Scrambled("ACCOUNT_LIST_NOT_SECURITY!") +@Getter +@Setter +@ToString @JsonIgnoreProperties(ignoreUnknown = true) -@JsonAutoDetect( - getterVisibility = JsonAutoDetect.Visibility.NONE, - setterVisibility = JsonAutoDetect.Visibility.NONE, - fieldVisibility = JsonAutoDetect.Visibility.NONE) -public class AccountList extends AbstractListModel implements ComboBoxModel { +public class AccountList implements ListModel { + private List accounts = Lists.newArrayList(); + private String clientId = RandomStringUtils.randomAlphanumeric(24); - @JsonProperty - @Getter - private List accounts = new ArrayList(); - private transient Account selected; + @JsonIgnore private final ListListenerReducer listeners = new ListListenerReducer(); - /** - * Add a new account. - * - *

If there is already an existing account with the same ID, then the - * new account will not be added.

- * - * @param account the account to add - */ - public synchronized void add(@NonNull Account account) { - if (!accounts.contains(account)) { - accounts.add(account); - Collections.sort(accounts); - fireContentsChanged(this, 0, accounts.size()); - } - } + public synchronized void add(SavedSession session) { + accounts.add(session); - /** - * Remove an account. - * - * @param account the account - */ - public synchronized void remove(@NonNull Account account) { - Iterator it = accounts.iterator(); - while (it.hasNext()) { - Account other = it.next(); - if (other.equals(account)) { - it.remove(); - fireContentsChanged(this, 0, accounts.size() + 1); - break; - } - } - } + int index = accounts.size() - 1; + listeners.intervalAdded(new ListDataEvent(this, ListDataEvent.INTERVAL_ADDED, index, index)); + } - /** - * Set the list of accounts. - * - * @param accounts the list of accounts - */ - public synchronized void setAccounts(@NonNull List accounts) { - this.accounts = accounts; - Collections.sort(accounts); - } + public synchronized void remove(SavedSession session) { + int index = accounts.indexOf(session); - @Override - @JsonIgnore - public synchronized int getSize() { - return accounts.size(); - } + if (index > -1) { + accounts.remove(index); + listeners.intervalRemoved(new ListDataEvent(this, ListDataEvent.INTERVAL_REMOVED, index, index)); + } + } - @Override - public synchronized Account getElementAt(int index) { - try { - return accounts.get(index); - } catch (IndexOutOfBoundsException e) { - return null; - } - } + public synchronized void update(SavedSession newSavedSession) { + int index = accounts.indexOf(newSavedSession); - @Override - public void setSelectedItem(Object item) { - if (item == null) { - selected = null; - return; - } + if (index > -1) { + accounts.set(index, newSavedSession); + listeners.contentsChanged(new ListDataEvent(this, ListDataEvent.CONTENTS_CHANGED, index, index)); + } else { + this.add(newSavedSession); + } + } - if (item instanceof Account) { - this.selected = (Account) item; - } else { - String id = String.valueOf(item).trim(); - Account account = new Account(id); - for (Account test : accounts) { - if (test.equals(account)) { - account = test; - break; - } - } - selected = account; - } + @Override + public int getSize() { + return accounts.size(); + } - if (selected.getId() == null || selected.getId().isEmpty()) { - selected = null; - } - } + @Override + public SavedSession getElementAt(int index) { + return accounts.get(index); + } - @Override - @JsonIgnore - public Account getSelectedItem() { - return selected; - } + @Override + public void addListDataListener(ListDataListener l) { + listeners.addListDataListener(l); + } - public synchronized void forgetPasswords() { - for (Account account : accounts) { - account.setPassword(null); - } - } + @Override + public void removeListDataListener(ListDataListener l) { + listeners.removeListDataListener(l); + } } diff --git a/launcher/src/main/java/com/skcraft/launcher/auth/NewAccountList.java b/launcher/src/main/java/com/skcraft/launcher/auth/NewAccountList.java deleted file mode 100644 index 6081252..0000000 --- a/launcher/src/main/java/com/skcraft/launcher/auth/NewAccountList.java +++ /dev/null @@ -1,78 +0,0 @@ -package com.skcraft.launcher.auth; - -import com.beust.jcommander.internal.Lists; -import com.fasterxml.jackson.annotation.JsonIgnore; -import com.fasterxml.jackson.annotation.JsonIgnoreProperties; -import com.skcraft.launcher.dialog.component.ListListenerReducer; -import com.skcraft.launcher.persistence.Scrambled; -import lombok.Getter; -import lombok.Setter; -import lombok.ToString; -import org.apache.commons.lang.RandomStringUtils; - -import javax.swing.*; -import javax.swing.event.ListDataEvent; -import javax.swing.event.ListDataListener; -import java.util.List; - -/** - * Persisted account list - */ -@Scrambled("ACCOUNT_LIST_NOT_SECURITY!") -@Getter -@Setter -@ToString -@JsonIgnoreProperties(ignoreUnknown = true) -public class NewAccountList implements ListModel { - private List accounts = Lists.newArrayList(); - private String clientId = RandomStringUtils.randomAlphanumeric(24); - - @JsonIgnore private final ListListenerReducer listeners = new ListListenerReducer(); - - public synchronized void add(SavedSession session) { - accounts.add(session); - - int index = accounts.size() - 1; - listeners.intervalAdded(new ListDataEvent(this, ListDataEvent.INTERVAL_ADDED, index, index)); - } - - public synchronized void remove(SavedSession session) { - int index = accounts.indexOf(session); - - if (index > -1) { - accounts.remove(index); - listeners.intervalRemoved(new ListDataEvent(this, ListDataEvent.INTERVAL_REMOVED, index, index)); - } - } - - public synchronized void update(SavedSession newSavedSession) { - int index = accounts.indexOf(newSavedSession); - - if (index > -1) { - accounts.set(index, newSavedSession); - listeners.contentsChanged(new ListDataEvent(this, ListDataEvent.CONTENTS_CHANGED, index, index)); - } else { - this.add(newSavedSession); - } - } - - @Override - public int getSize() { - return accounts.size(); - } - - @Override - public SavedSession getElementAt(int index) { - return accounts.get(index); - } - - @Override - public void addListDataListener(ListDataListener l) { - listeners.addListDataListener(l); - } - - @Override - public void removeListDataListener(ListDataListener l) { - listeners.removeListDataListener(l); - } -} diff --git a/launcher/src/main/java/com/skcraft/launcher/dialog/LoginDialog.java b/launcher/src/main/java/com/skcraft/launcher/dialog/LoginDialog.java index 1fc0709..fdc2c49 100644 --- a/launcher/src/main/java/com/skcraft/launcher/dialog/LoginDialog.java +++ b/launcher/src/main/java/com/skcraft/launcher/dialog/LoginDialog.java @@ -12,7 +12,6 @@ import com.skcraft.concurrency.ProgressObservable; import com.skcraft.launcher.Configuration; import com.skcraft.launcher.Launcher; -import com.skcraft.launcher.auth.Account; import com.skcraft.launcher.auth.AuthenticationException; import com.skcraft.launcher.auth.Session; import com.skcraft.launcher.auth.YggdrasilLoginService; @@ -22,13 +21,13 @@ import com.skcraft.launcher.util.SwingExecutor; import lombok.Getter; import lombok.NonNull; +import lombok.RequiredArgsConstructor; import javax.swing.*; import java.awt.*; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.io.IOException; -import java.util.Date; import java.util.concurrent.Callable; /** @@ -112,18 +111,15 @@ if (password == null || password.isEmpty()) { SwingHelper.showErrorDialog(this, SharedLocale.tr("login.noPasswordError"), SharedLocale.tr("login.noPasswordTitle")); } else { - Account account = new Account(usernameText.getText()); - account.setLastUsed(new Date()); - - attemptLogin(account, password); + attemptLogin(usernameText.getText(), password); } } else { SwingHelper.showErrorDialog(this, SharedLocale.tr("login.noLoginError"), SharedLocale.tr("login.noLoginTitle")); } } - private void attemptLogin(Account account, String password) { - LoginCallable callable = new LoginCallable(account, password); + private void attemptLogin(String username, String password) { + LoginCallable callable = new LoginCallable(username, password); ObservableFuture future = new ObservableFuture( launcher.getExecutor().submit(callable), callable); @@ -153,19 +149,15 @@ return dialog.getSession(); } - private class LoginCallable implements Callable,ProgressObservable { - private final Account account; + @RequiredArgsConstructor + private class LoginCallable implements Callable, ProgressObservable { + private final String username; private final String password; - private LoginCallable(Account account, String password) { - this.account = account; - this.password = password; - } - @Override public Session call() throws AuthenticationException, IOException, InterruptedException { YggdrasilLoginService service = launcher.getYggdrasil(); - Session identity = service.login(launcher.getProperties().getProperty("agentName"), account.getId(), password); + Session identity = service.login(launcher.getProperties().getProperty("agentName"), username, password); // The presence of the identity (profile in Mojang terms) corresponds to whether the account // owns the game, so we need to check that