Inceptum/launcher/src/main/java/io/gitlab/jfronny/inceptum/launcher/api/account/AccountManager.java

118 lines
4.0 KiB
Java
Raw Normal View History

2022-09-04 21:21:24 +02:00
package io.gitlab.jfronny.inceptum.launcher.api.account;
2021-10-29 22:50:42 +02:00
2022-09-04 21:21:24 +02:00
import io.gitlab.jfronny.commons.io.JFiles;
2022-09-18 20:11:06 +02:00
import io.gitlab.jfronny.commons.serialize.gson.api.GsonHolder;
2022-04-28 23:13:37 +02:00
import io.gitlab.jfronny.gson.reflect.TypeToken;
2022-09-04 21:21:24 +02:00
import io.gitlab.jfronny.inceptum.launcher.LauncherEnv;
import io.gitlab.jfronny.inceptum.common.*;
2021-10-29 22:50:42 +02:00
import java.io.IOException;
import java.lang.reflect.Type;
import java.nio.file.Files;
import java.nio.file.Path;
2021-10-29 22:50:42 +02:00
import java.util.ArrayList;
import java.util.List;
public class AccountManager {
private static final Type abstractAccountListType = new TypeToken<List<MicrosoftAccount>>() {}.getType();
private static MicrosoftAccount SELECTED_ACCOUNT;
private static final List<MicrosoftAccount> ACCOUNTS = new ArrayList<>();
2021-11-24 19:53:06 +01:00
public static final AuthInfo NULL_AUTH = new AuthInfo("Joe", "2536abce90e8476a871679918164abc5", "99abe417230342cb8e9e2168ab46297a", "legacy");
2021-10-29 22:50:42 +02:00
public static AuthInfo getSelectedAccount() {
2021-12-01 16:12:47 +01:00
if (SELECTED_ACCOUNT == null) return NULL_AUTH;
2022-09-04 21:21:24 +02:00
else if (SELECTED_ACCOUNT.ensureAccessTokenValid()) return new AuthInfo(SELECTED_ACCOUNT);
2021-10-30 19:26:59 +02:00
else {
Utils.LOGGER.error("Couldn't login properly, using offline mode");
2021-10-30 19:26:59 +02:00
return NULL_AUTH;
}
2021-10-29 22:50:42 +02:00
}
2021-11-02 13:43:18 +01:00
public static boolean accountMissing() {
2021-12-01 16:12:47 +01:00
return ACCOUNTS.isEmpty();
2021-11-02 13:43:18 +01:00
}
2021-10-29 22:50:42 +02:00
public static List<MicrosoftAccount> getAccounts() {
return List.copyOf(ACCOUNTS);
}
2021-12-01 16:12:47 +01:00
public static int getSelectedIndex() {
if (SELECTED_ACCOUNT == null) return ACCOUNTS.size();
return ACCOUNTS.indexOf(SELECTED_ACCOUNT);
}
2021-10-29 22:50:42 +02:00
public static void saveAccounts() {
try {
2022-09-04 21:21:24 +02:00
JFiles.writeObject(MetaHolder.ACCOUNTS_PATH, ACCOUNTS);
2021-10-29 22:50:42 +02:00
} catch (IOException e) {
Utils.LOGGER.error("Could not save accounts", e);
2021-10-29 22:50:42 +02:00
}
}
public static void loadAccounts() {
Utils.LOGGER.info("Loading accounts");
if (Files.exists(MetaHolder.ACCOUNTS_PATH)) {
2021-10-29 22:50:42 +02:00
try {
2022-09-04 21:21:24 +02:00
ACCOUNTS.addAll(JFiles.readObject(MetaHolder.ACCOUNTS_PATH, abstractAccountListType));
2021-10-29 22:50:42 +02:00
} catch (IOException e) {
Utils.LOGGER.error("Could not load accounts", e);
2021-10-29 22:50:42 +02:00
}
}
for (MicrosoftAccount account : ACCOUNTS) {
2021-10-30 19:26:59 +02:00
account.refreshAccessToken();
if (account.accountId.equalsIgnoreCase(InceptumConfig.lastAccount)) {
2021-10-29 22:50:42 +02:00
SELECTED_ACCOUNT = account;
}
}
Utils.LOGGER.info("Finished loading accounts");
2021-10-29 22:50:42 +02:00
}
public static void addAccount(MicrosoftAccount account) {
ACCOUNTS.add(account);
if (ACCOUNTS.size() > 1) {
2022-09-04 21:21:24 +02:00
LauncherEnv.showOkCancel("Account added successfully. Switch to it now?", "Success", () -> switchAccount(account), R::nop);
} else switchAccount(account);
2021-10-29 22:50:42 +02:00
saveAccounts();
}
public static void removeAccount(MicrosoftAccount account) {
if (SELECTED_ACCOUNT == account) {
if (ACCOUNTS.size() == 1)
switchAccount(null);
else
switchAccount(ACCOUNTS.get(0));
}
ACCOUNTS.remove(account);
saveAccounts();
}
public static void switchAccount(MicrosoftAccount account) {
if (account == null) {
Utils.LOGGER.info("Logging out");
2021-10-29 22:50:42 +02:00
SELECTED_ACCOUNT = null;
InceptumConfig.lastAccount = null;
2021-10-29 22:50:42 +02:00
} else {
Utils.LOGGER.info("Changed account to " + account);
2021-10-29 22:50:42 +02:00
SELECTED_ACCOUNT = account;
InceptumConfig.lastAccount = account.accountId;
2021-10-29 22:50:42 +02:00
}
InceptumConfig.saveConfig();
2021-10-29 22:50:42 +02:00
}
public static Object getAccountByName(String username) {
for (MicrosoftAccount account : ACCOUNTS) {
if (account.accountId.equalsIgnoreCase(username))
return account;
}
return null;
}
public static boolean isAccountByName(String username) {
for (MicrosoftAccount account : ACCOUNTS) {
if (account.accountId.equalsIgnoreCase(username))
return true;
}
return false;
}
}