Allow non-saved password user to create wan server

This commit is contained in:
Benau 2018-02-28 15:05:34 +08:00
parent cd5cb5ef6d
commit 18eb0a613a
5 changed files with 79 additions and 23 deletions

View File

@ -887,7 +887,6 @@ int handleCmdLine()
int n;
std::string s;
bool try_login = false;
irr::core::stringw login, password;
if (CommandLine::has("--unit-testing"))
@ -1012,23 +1011,32 @@ int handleCmdLine()
if (CommandLine::has("--login", &s))
{
login = s.c_str();
try_login = true;
} // --login
if (CommandLine::has("--password", &s))
password = s.c_str();
if (try_login)
bool can_wan = false;
if (!login.empty() && !password.empty())
{
irr::core::stringw s;
Online::XMLRequest* request =
PlayerManager::requestSignIn(login, password);
if (request->isSuccess())
while (PlayerManager::getCurrentOnlineState() != PlayerProfile::OS_SIGNED_IN)
{
Log::info("Main", "Logged in from command-line.");
Online::RequestManager::get()->update(0.0f);
StkTime::sleep(1);
}
Log::info("Main", "Logged in from command-line.");
can_wan = true;
delete request;
}
if (!can_wan && CommandLine::has("--login-id", &n) &&
CommandLine::has("--token", &s))
{
NetworkConfig::get()->setCurrentUserId(n);
NetworkConfig::get()->setCurrentUserToken(s);
can_wan = true;
}
// Networking command lines
@ -1080,29 +1088,30 @@ int handleCmdLine()
if (CommandLine::has("--wan-server", &s))
{
// Try to use saved user token if exists
PlayerProfile* player = PlayerManager::getCurrentPlayer();
if (player && player->wasOnlineLastTime() && player->wasOnlineLastTime() &&
player->hasSavedSession())
if (!can_wan && player && player->wasOnlineLastTime() &&
player->wasOnlineLastTime() && player->hasSavedSession())
{
while (true)
while (PlayerManager::getCurrentOnlineState() != PlayerProfile::OS_SIGNED_IN)
{
Online::RequestManager::get()->update(0.0f);
StkTime::sleep(1);
if (PlayerManager::getCurrentOnlineState() == PlayerProfile::OS_SIGNED_IN)
}
can_wan = true;
}
else
{
break;
}
Log::warn("main", "No saved online player session to create a wan server");
}
if (can_wan)
{
NetworkConfig::get()->setServerName(StringUtils::xmlDecode(s));
NetworkConfig::get()->setIsServer(true);
NetworkConfig::get()->setIsWAN();
STKHost::create();
Log::info("main", "Creating a WAN server '%s'.", s.c_str());
}
else
{
Log::warn("main", "No saved online player session to create a wan server");
}
}
else if (CommandLine::has("--lan-server", &s))
{

View File

@ -17,6 +17,7 @@
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "network/network_config.hpp"
#include "online/xml_request.hpp"
NetworkConfig *NetworkConfig::m_network_config = NULL;
bool NetworkConfig::m_disable_lan = false;
@ -41,6 +42,8 @@ NetworkConfig::NetworkConfig()
m_is_public_server = false;
m_max_players = 4;
m_direct_connect = false;
m_cur_user_id = 0;
m_cur_user_token = "";
m_server_name = "";
m_password = "";
m_server_discovery_port = 2757;
@ -106,3 +109,10 @@ std::pair<RaceManager::MinorRaceModeType, bool>
return { RaceManager::MINOR_MODE_NORMAL_RACE, false };
} // getLocalGameMode
// ----------------------------------------------------------------------------
void NetworkConfig::setUserDetails(Online::XMLRequest* r,
const std::string& name)
{
assert(!m_cur_user_token.empty());
} // setUserDetails

View File

@ -28,6 +28,11 @@
#include "irrString.h"
namespace Online
{
class XMLRequest;
}
class NetworkConfig
{
private:
@ -79,6 +84,10 @@ private:
/** If this is a server, the server name. */
irr::core::stringw m_server_name;
/** Used by wan server. */
uint32_t m_cur_user_id;
std::string m_cur_user_token;
NetworkConfig();
public:
@ -198,6 +207,16 @@ public:
// ------------------------------------------------------------------------
bool isDirectConnect() const { return m_direct_connect; }
// ------------------------------------------------------------------------
void setCurrentUserId(uint32_t id) { m_cur_user_id = id ; }
// ------------------------------------------------------------------------
void setCurrentUserToken(const std::string& t) { m_cur_user_token = t; }
// ------------------------------------------------------------------------
uint32_t getCurrentUserId() const { return m_cur_user_id; }
// ------------------------------------------------------------------------
const std::string& getCurrentUserToken() const { return m_cur_user_token; }
// ------------------------------------------------------------------------
void setUserDetails(Online::XMLRequest* r, const std::string& name);
// ------------------------------------------------------------------------
}; // class NetworkConfig

View File

@ -23,6 +23,7 @@
#include "config/user_config.hpp"
#include "guiengine/message_queue.hpp"
#include "guiengine/screen.hpp"
#include "network/network_config.hpp"
#include "online/online_profile.hpp"
#include "online/profile_manager.hpp"
#include "states_screens/main_menu_screen.hpp"
@ -208,9 +209,18 @@ namespace Online
core::stringw username("");
uint32_t userid(0);
#ifdef DEBUG
int token_fetched = input->get("token", &m_token);
int username_fetched = input->get("username", &username);
int userid_fetched = input->get("userid", &userid);
assert(token_fetched && username_fetched && userid_fetched);
#else
input->get("token", &m_token);
input->get("username", &username);
input->get("userid", &userid);
#endif
NetworkConfig::get()->setCurrentUserId(userid);
NetworkConfig::get()->setCurrentUserToken(m_token);
setLastOnlineName(username);
OnlineProfile* profile = new OnlineProfile(userid, username, true);
@ -218,7 +228,6 @@ namespace Online
// existing profile, and then delete profile. Only the returned
// pointer is save to use.
m_profile = ProfileManager::get()->addPersistent(profile);
assert(token_fetched && username_fetched && userid_fetched);
m_online_state = OS_SIGNED_IN;
if(rememberPassword())
{

View File

@ -215,6 +215,15 @@ void CreateServerScreen::createServer()
std::string server_string = NetworkConfig::get()->isWAN() ?
"--public-server --wan-server=" : "--lan-server=";
server_string += StringUtils::xmlEncode(name);
if (NetworkConfig::get()->isWAN())
{
char token[1024];
sprintf(token, " --login-id=%d --token=%s",
NetworkConfig::get()->getCurrentUserId(),
NetworkConfig::get()->getCurrentUserToken().c_str());
server_string += token;
}
char option[1024];
sprintf(option, " --no-graphics --type=%d --difficulty=%d "
"--max-players=%d --network-console --no-console-log "