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

@@ -0,0 +1,91 @@
/*
* 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

@@ -0,0 +1,51 @@
/*
* 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

@@ -0,0 +1,76 @@
/*
* 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

@@ -0,0 +1,31 @@
/*
* 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

@@ -0,0 +1,50 @@
/*
* 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;
}