each authenticator has its own username now

This commit is contained in:
huangyuhui
2016-01-02 10:26:48 +08:00
parent b82243a9c0
commit a62848d4fa
18 changed files with 87 additions and 49 deletions

View File

@@ -48,13 +48,17 @@ public class DefaultPlugin implements IPlugin {
public void onRegisterAuthenticators(Consumer<IAuthenticator> apply) {
String clientToken = Settings.getInstance().getClientToken();
OFFLINE_LOGIN = new OfflineAuthenticator(clientToken);
OFFLINE_LOGIN.onLoadSettings(Settings.getInstance().getAuthenticatorConfig(OFFLINE_LOGIN.id()));
YGGDRASIL_LOGIN = new YggdrasilAuthenticator(clientToken);
YGGDRASIL_LOGIN.onLoadSettings(Settings.getInstance().getYggdrasilConfig());
YGGDRASIL_LOGIN.onLoadSettings(Settings.getInstance().getAuthenticatorConfig(YGGDRASIL_LOGIN.id()));
SKINME_LOGIN = new SkinmeAuthenticator(clientToken);
SKINME_LOGIN.onLoadSettings(Settings.getInstance().getAuthenticatorConfig(SKINME_LOGIN.id()));
Runtime.getRuntime().addShutdownHook(new Thread(()
-> Settings.getInstance().setYggdrasilConfig(YGGDRASIL_LOGIN.onSaveSettings())
));
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
Settings.getInstance().setAuthenticatorConfig(OFFLINE_LOGIN.id(), OFFLINE_LOGIN.onSaveSettings());
Settings.getInstance().setAuthenticatorConfig(YGGDRASIL_LOGIN.id(), YGGDRASIL_LOGIN.onSaveSettings());
Settings.getInstance().setAuthenticatorConfig(SKINME_LOGIN.id(), SKINME_LOGIN.onSaveSettings());
}));
apply.accept(OFFLINE_LOGIN);
apply.accept(YGGDRASIL_LOGIN);
apply.accept(SKINME_LOGIN);
@@ -62,7 +66,7 @@ public class DefaultPlugin implements IPlugin {
@Override
public void showUI() {
MainFrame.showMainFrame(Settings.isFirstLoading());
MainFrame.showMainFrame();
}
@Override

View File

@@ -36,6 +36,7 @@ import org.jackhuang.hellominecraft.launcher.version.MinecraftLibrary;
import org.jackhuang.hellominecraft.tasks.TaskWindow;
import org.jackhuang.hellominecraft.utils.system.FileUtils;
import org.jackhuang.hellominecraft.utils.MessageBox;
import org.jackhuang.hellominecraft.utils.StrUtils;
/**
*
@@ -67,7 +68,7 @@ public class MinecraftLoader extends AbstractMinecraftLoader {
res.add((v.isCanceledWrapper() ? "" : "-mainClass=") + mainClass);
String arg = v.getSelectedMinecraftVersion().minecraftArguments;
String[] splitted = org.jackhuang.hellominecraft.utils.StrUtils.tokenize(arg);
String[] splitted = StrUtils.tokenize(arg);
if (!checkAssetsExist())
if (MessageBox.Show(C.i18n("assets.no_assets"), MessageBox.YES_NO_OPTION) == MessageBox.YES_OPTION) {

View File

@@ -1,91 +0,0 @@
/*
* Hello Minecraft! Launcher.
* Copyright (C) 2013 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.hellominecraft.launcher.servers;
import org.jackhuang.hellominecraft.launcher.api.IPlugin;
import org.jackhuang.hellominecraft.launcher.launch.IMinecraftProvider;
import org.jackhuang.hellominecraft.launcher.servers.mfcraft.CheckModsMinecraftProvider;
import org.jackhuang.hellominecraft.launcher.servers.mfcraft.MFCraftAuthenticator;
import org.jackhuang.hellominecraft.launcher.servers.mfcraft.Servers;
import org.jackhuang.hellominecraft.launcher.settings.Profile;
import org.jackhuang.hellominecraft.launcher.settings.Settings;
import org.jackhuang.hellominecraft.launcher.utils.auth.AuthenticationException;
import org.jackhuang.hellominecraft.launcher.utils.auth.IAuthenticator;
import org.jackhuang.hellominecraft.launcher.utils.auth.UserProfileProvider;
import org.jackhuang.hellominecraft.launcher.utils.auth.YggdrasilAuthenticator;
import org.jackhuang.hellominecraft.launcher.version.ServerInfo;
import org.jackhuang.hellominecraft.launcher.views.MainFrame;
import org.jackhuang.hellominecraft.launcher.views.ServerListView;
import org.jackhuang.hellominecraft.utils.functions.Consumer;
import org.jackhuang.hellominecraft.views.Selector;
/**
*
* @author huangyuhui
*/
public class ServerPlugin implements IPlugin {
protected static YggdrasilAuthenticator YGGDRASIL_LOGIN;
protected static MFCraftAuthenticator MFCRAFT_LOGIN;
@Override
public IMinecraftProvider provideMinecraftProvider(Profile profile) {
return new CheckModsMinecraftProvider(profile);
}
@Override
public void onRegisterAuthenticators(Consumer<IAuthenticator> apply) {
String clientToken = Settings.getInstance().getClientToken();
MFCRAFT_LOGIN = new MFCraftAuthenticator(clientToken);
YGGDRASIL_LOGIN = new YggdrasilAuthenticator(clientToken);
YGGDRASIL_LOGIN.onLoadSettings(Settings.getInstance().getYggdrasilConfig());
apply.accept(MFCRAFT_LOGIN);
apply.accept(YGGDRASIL_LOGIN);
}
@Override
public void showUI() {
MainFrame.showMainFrame(Settings.isFirstLoading());
}
public static ServerInfo lastServerInfo;
@Override
public void onProcessingLoginResult(UserProfileProvider result) throws AuthenticationException {
Servers s = Servers.getInstance();
String[] sel = new String[s.areas.size()];
for (int i = 0; i < sel.length; i++)
sel[i] = s.areas.get(i).name;
Selector selector = new Selector(null, sel, "选择你要登录的服务器大区");
int ind = selector.getChoice();
for (ServerInfo si : s.areas.get(ind).servers)
si.downloadIcon();
ServerListView slv = new ServerListView(s.areas.get(ind).servers.toArray(new ServerInfo[0]));
int c = slv.getChoice();
if (c == -1)
throw new AuthenticationException("未选择服务器");
lastServerInfo = s.areas.get(ind).servers.get(ind);
result.setServer(lastServerInfo);
}
@Override
public void onInitializingProfile(Profile p) {
p.initialize(1);
}
}

View File

@@ -1,51 +0,0 @@
/*
* Hello Minecraft! Launcher.
* Copyright (C) 2013 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.hellominecraft.launcher.servers.mfcraft;
import java.io.IOException;
import org.jackhuang.hellominecraft.HMCLog;
import org.jackhuang.hellominecraft.launcher.servers.ServerPlugin;
import org.jackhuang.hellominecraft.launcher.settings.Profile;
import org.jackhuang.hellominecraft.launcher.version.MinecraftModService;
import org.jackhuang.hellominecraft.launcher.version.MinecraftVersionManager;
import org.jackhuang.hellominecraft.utils.ArrayUtils;
/**
*
* @author huangyuhui
*/
public class CheckModsMinecraftProvider extends MinecraftVersionManager {
public CheckModsMinecraftProvider(Profile p) {
super(p);
}
@Override
public boolean onLaunch() {
try {
super.onLaunch();
String[] md5s = ((MinecraftModService) getModService()).checkMd5s();
String[] md5 = ServerPlugin.lastServerInfo.md5;
return ArrayUtils.equals(md5s, md5);
} catch (IOException ex) {
HMCLog.err("Failed to check md5");
return false;
}
}
}

View File

@@ -1,76 +0,0 @@
/*
* Hello Minecraft! Launcher.
* Copyright (C) 2013 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.hellominecraft.launcher.servers.mfcraft;
import com.google.gson.JsonSyntaxException;
import java.io.IOException;
import org.jackhuang.hellominecraft.C;
import org.jackhuang.hellominecraft.launcher.utils.auth.AuthenticationException;
import org.jackhuang.hellominecraft.launcher.utils.auth.IAuthenticator;
import org.jackhuang.hellominecraft.launcher.utils.auth.LoginInfo;
import static org.jackhuang.hellominecraft.launcher.utils.auth.OfflineAuthenticator.getUUIDFromUserName;
import org.jackhuang.hellominecraft.launcher.utils.auth.UserProfileProvider;
import org.jackhuang.hellominecraft.launcher.version.ServerInfo;
import org.jackhuang.hellominecraft.utils.NetUtils;
/**
*
* @author huangyuhui
*/
public class MFCraftAuthenticator extends IAuthenticator {
public MFCraftAuthenticator(String clientToken) {
super(clientToken);
}
@Override
public UserProfileProvider login(LoginInfo info) throws AuthenticationException {
try {
UserProfileProvider result = new UserProfileProvider();
String url = String.format("http://zh.mfcraft.cn/index.php?c=user_public&a=clientlogin&user=%s&pass=%s", info.username, info.password);
String response = NetUtils.get(url);
if (response.contains("error"))
throw new AuthenticationException(C.i18n("login.wrong_password"));
result.setUserName(info.username);
String uuid = getUUIDFromUserName(info.username);
result.setSession(uuid);
result.setUserId(uuid);
result.setAccessToken(uuid);
result.setUserType("Legacy");
return result;
} catch (IOException | JsonSyntaxException ex) {
throw new AuthenticationException(C.i18n("login.failed.connect_authentication_server"), ex);
}
}
@Override
public String getName() {
return "MFCraft";
}
@Override
public UserProfileProvider loginBySettings() throws AuthenticationException {
return null;
}
@Override
public void logout() {
}
}

View File

@@ -1,31 +0,0 @@
/*
* Hello Minecraft! Launcher.
* Copyright (C) 2013 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.hellominecraft.launcher.servers.mfcraft;
import java.util.ArrayList;
import org.jackhuang.hellominecraft.launcher.version.ServerInfo;
/**
*
* @author huangyuhui
*/
public class ServerArea {
public String name;
public ArrayList<ServerInfo> servers;
}

View File

@@ -1,50 +0,0 @@
/*
* Hello Minecraft! Launcher.
* Copyright (C) 2013 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.hellominecraft.launcher.servers.mfcraft;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jackhuang.hellominecraft.C;
import org.jackhuang.hellominecraft.HMCLog;
import org.jackhuang.hellominecraft.utils.NetUtils;
/**
*
* @author huangyuhui
*/
public class Servers {
public static Servers instance;
public static Servers getInstance() {
if (instance == null) {
String servers = null;
try {
servers = NetUtils.get("http://zh.mfcraft.cn/View/servers.php");
instance = C.gson.fromJson(servers, Servers.class);
} catch (IOException ex) {
HMCLog.err("Failed to get servers");
}
}
return instance;
}
public ArrayList<ServerArea> areas;
}

View File

@@ -20,6 +20,7 @@ package org.jackhuang.hellominecraft.launcher.settings;
import org.jackhuang.hellominecraft.launcher.utils.download.DownloadType;
import com.google.gson.annotations.SerializedName;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
@@ -40,8 +41,6 @@ public final class Config {
private String last;
@SerializedName("bgpath")
private String bgpath;
@SerializedName("username")
private String username;
@SerializedName("clientToken")
private final String clientToken;
private String proxyHost, proxyPort, proxyUserName, proxyPassword;
@@ -116,15 +115,6 @@ public final class Config {
Settings.save();
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
Settings.save();
}
public String getClientToken() {
return clientToken;
}
@@ -163,12 +153,12 @@ public final class Config {
return configurations;
}
public Map getYggdrasilConfig() {
return yggdrasil;
public Map getAuthenticatorConfig(String authId) {
return auth.get(authId);
}
public void setYggdrasilConfig(Map yggdrasil) {
this.yggdrasil = yggdrasil;
public void setAuthenticatorConfig(String authId, Map map) {
auth.put(authId, map);
Settings.save();
}
@@ -178,17 +168,17 @@ public final class Config {
private int downloadtype;
@SerializedName("configurations")
private TreeMap<String, Profile> configurations;
@SerializedName("yggdrasil")
private Map yggdrasil;
@SerializedName("auth")
private Map<String, Map> auth;
public Config() {
clientToken = UUID.randomUUID().toString();
username = "";
logintype = downloadtype = 0;
enableShadow = false;
enableAnimation = true;
theme = 4;
decorated = OS.os() == OS.LINUX;
auth = new HashMap<>();
}
public DownloadType getDownloadSource() {

View File

@@ -32,7 +32,6 @@ import org.jackhuang.hellominecraft.utils.CollectionUtils;
import org.jackhuang.hellominecraft.utils.system.FileUtils;
import org.jackhuang.hellominecraft.utils.system.IOUtils;
import org.jackhuang.hellominecraft.utils.MessageBox;
import org.jackhuang.hellominecraft.utils.StrUtils;
import org.jackhuang.hellominecraft.utils.UpdateChecker;
import org.jackhuang.hellominecraft.utils.VersionNumber;
import org.jackhuang.hellominecraft.utils.system.Java;
@@ -48,7 +47,6 @@ public final class Settings {
public static final File SETTINGS_FILE = new File(IOUtils.currentDir(), "hmcl.json");
private static boolean isFirstLoading;
private static final Config SETTINGS;
public static final UpdateChecker UPDATE_CHECKER = new UpdateChecker(new VersionNumber(Main.VERSION_FIRST, Main.VERSION_SECOND, Main.VERSION_THIRD),
"hmcl");
@@ -58,10 +56,6 @@ public final class Settings {
return SETTINGS;
}
public static boolean isFirstLoading() {
return isFirstLoading;
}
static {
SETTINGS = initSettings();
if (!getProfiles().containsKey(DEFAULT_PROFILE))
@@ -82,7 +76,7 @@ public final class Settings {
private static Config initSettings() {
Config c = new Config();
if (SETTINGS_FILE.exists()) {
if (SETTINGS_FILE.exists())
try {
String str = FileUtils.readFileToString(SETTINGS_FILE);
if (str == null || str.trim().equals(""))
@@ -100,11 +94,8 @@ public final class Settings {
System.exit(1);
}
}
isFirstLoading = StrUtils.isBlank(c.getUsername());
} else {
else
HMCLog.log("No settings file here, may be first loading.");
isFirstLoading = true;
}
return c;
}

View File

@@ -79,6 +79,11 @@ public final class BestLogin extends IAuthenticator {
}
}
@Override
public String id() {
return "best";
}
@Override
public String getName() {
return "BestLogin";

View File

@@ -18,7 +18,9 @@
package org.jackhuang.hellominecraft.launcher.utils.auth;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.jackhuang.hellominecraft.launcher.api.PluginManager;
/**
@@ -34,7 +36,7 @@ public abstract class IAuthenticator {
PluginManager.NOW_PLUGIN.onRegisterAuthenticators(LOGINS::add);
}
protected String clientToken;
protected String clientToken, username;
public IAuthenticator(String clientToken) {
this.clientToken = clientToken;
@@ -52,6 +54,8 @@ public abstract class IAuthenticator {
*/
public abstract UserProfileProvider login(LoginInfo info) throws AuthenticationException;
public abstract String id();
/**
*
* @return the name of login method.
@@ -78,4 +82,24 @@ public abstract class IAuthenticator {
public abstract UserProfileProvider loginBySettings() throws AuthenticationException;
public abstract void logout();
public Map onSaveSettings() {
HashMap m = new HashMap();
m.put("IAuthenticator_UserName", username);
return m;
}
public void onLoadSettings(Map m) {
if (m == null)
return;
username = (String) m.get("IAuthenticator_UserName");
}
public String getUsername() {
return username;
}
public void setUsername(String s) {
username = s;
}
}

View File

@@ -49,6 +49,11 @@ public final class OfflineAuthenticator extends IAuthenticator {
return DigestUtils.md5Hex(str);
}
@Override
public String id() {
return "offline";
}
@Override
public String getName() {
return C.i18n("login.methods.offline");

View File

@@ -104,6 +104,11 @@ public final class SkinmeAuthenticator extends IAuthenticator {
}
}
@Override
public String id() {
return "skinme";
}
@Override
public String getName() {
return "Skinme";

View File

@@ -92,16 +92,26 @@ public final class YggdrasilAuthenticator extends IAuthenticator {
return ua.isLoggedIn();
}
@Override
public String id() {
return "yggdrasil";
}
@Override
public String getName() {
return C.i18n("login.methods.yggdrasil");
}
@Override
public Map onSaveSettings() {
return ua.saveForStorage();
Map m = ua.saveForStorage();
m.putAll(super.onSaveSettings());
return m;
}
@Override
public void onLoadSettings(Map settings) {
super.onLoadSettings(settings);
if (settings == null)
return;
ua.loadFromStorage(settings);

View File

@@ -1241,7 +1241,7 @@ public final class GameSettingsPanel extends AnimatedPanel implements DropTarget
// <editor-fold defaultstate="collapsed" desc="Mods">
private boolean reloadingMods = false;
private void reloadMods() {
private synchronized void reloadMods() {
if (reloadingMods)
return;
reloadingMods = true;
@@ -1343,6 +1343,6 @@ public final class GameSettingsPanel extends AnimatedPanel implements DropTarget
private javax.swing.JTextField txtWidth;
// End of variables declaration//GEN-END:variables
private javax.swing.JPanel pnlGameDownloads;
private final javax.swing.JPanel pnlGameDownloads;
// </editor-fold>
}

View File

@@ -47,6 +47,7 @@ import org.jackhuang.hellominecraft.launcher.settings.Settings;
import org.jackhuang.hellominecraft.launcher.utils.auth.IAuthenticator;
import org.jackhuang.hellominecraft.lookandfeel.GraphicsUtils;
import org.jackhuang.hellominecraft.lookandfeel.Theme;
import org.jackhuang.hellominecraft.utils.StrUtils;
import org.jackhuang.hellominecraft.utils.Utils;
import org.jackhuang.hellominecraft.views.DropShadowBorder;
import org.jackhuang.hellominecraft.views.TintablePanel;
@@ -388,10 +389,10 @@ public final class MainFrame extends DraggableFrame {
windowTitle.setForeground(Color.white);
}
public static void showMainFrame(boolean firstLoad) {
if (firstLoad)
SwingUtilities.invokeLater(() -> MainFrame.INSTANCE.showMessage(C.i18n("ui.message.first_load")));
public static void showMainFrame() {
IAuthenticator l = Settings.getInstance().getAuthenticator();
if (StrUtils.isBlank(l.getUsername()))
SwingUtilities.invokeLater(() -> MainFrame.INSTANCE.showMessage(C.i18n("ui.message.first_load")));
if (l.hasPassword() && !l.isLoggedIn())
SwingUtilities.invokeLater(() -> MainFrame.INSTANCE.showMessage(C.i18n("ui.message.enter_password")));
INSTANCE.setVisible(true);

View File

@@ -297,7 +297,8 @@ public class MainPagePanel extends AnimatedPanel implements Event<String> {
}//GEN-LAST:event_txtPlayerNameFocusGained
private void txtPlayerNameFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtPlayerNameFocusLost
Settings.getInstance().setUsername(txtPlayerName.getText());
IAuthenticator l = Settings.getInstance().getAuthenticator();
l.setUsername(txtPlayerName.getText());
}//GEN-LAST:event_txtPlayerNameFocusLost
private void cboLoginModeItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cboLoginModeItemStateChanged
@@ -350,8 +351,8 @@ public class MainPagePanel extends AnimatedPanel implements Event<String> {
private void txtPlayerNameKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtPlayerNameKeyPressed
if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
Settings.getInstance().setUsername(txtPlayerName.getText());
IAuthenticator l = Settings.getInstance().getAuthenticator();
l.setUsername(txtPlayerName.getText());
if (!l.hasPassword())
runGame();
else if (!l.isLoggedIn())
@@ -400,7 +401,7 @@ public class MainPagePanel extends AnimatedPanel implements Event<String> {
return;
}
final IAuthenticator l = IAuthenticator.LOGINS.get(index);
final LoginInfo li = new LoginInfo(Settings.getInstance().getUsername(), l.isLoggedIn() || !l.hasPassword() ? null : new String(txtPassword.getPassword()));
final LoginInfo li = new LoginInfo(l.getUsername(), l.isLoggedIn() || !l.hasPassword() ? null : new String(txtPassword.getPassword()));
new Thread() {
@Override
public void run() {
@@ -458,9 +459,10 @@ public class MainPagePanel extends AnimatedPanel implements Event<String> {
cl.last(pnlPassword);
else
cl.first(pnlPassword);
String username = Settings.getInstance().getUsername();
if (StrUtils.isNotBlank(username))
txtPlayerName.setText(username);
String username = l.getUsername();
if (username == null)
username = "";
txtPlayerName.setText(username);
}
void loadFromSettings() {