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; int n;
std::string s; std::string s;
bool try_login = false;
irr::core::stringw login, password; irr::core::stringw login, password;
if (CommandLine::has("--unit-testing")) if (CommandLine::has("--unit-testing"))
@ -1011,24 +1010,33 @@ int handleCmdLine()
} // --type } // --type
if(CommandLine::has("--login", &s) ) if (CommandLine::has("--login", &s))
{
login = s.c_str(); login = s.c_str();
try_login = true; if (CommandLine::has("--password", &s))
} // --login
if(CommandLine::has("--password", &s))
password = s.c_str(); password = s.c_str();
if (try_login)
bool can_wan = false;
if (!login.empty() && !password.empty())
{ {
irr::core::stringw s; irr::core::stringw s;
Online::XMLRequest* request = Online::XMLRequest* request =
PlayerManager::requestSignIn(login, password); PlayerManager::requestSignIn(login, password);
while (PlayerManager::getCurrentOnlineState() != PlayerProfile::OS_SIGNED_IN)
if (request->isSuccess())
{ {
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 // Networking command lines
@ -1080,29 +1088,30 @@ int handleCmdLine()
if (CommandLine::has("--wan-server", &s)) if (CommandLine::has("--wan-server", &s))
{ {
// Try to use saved user token if exists
PlayerProfile* player = PlayerManager::getCurrentPlayer(); PlayerProfile* player = PlayerManager::getCurrentPlayer();
if (player && player->wasOnlineLastTime() && player->wasOnlineLastTime() && if (!can_wan && player && player->wasOnlineLastTime() &&
player->hasSavedSession()) player->wasOnlineLastTime() && player->hasSavedSession())
{ {
while (true) while (PlayerManager::getCurrentOnlineState() != PlayerProfile::OS_SIGNED_IN)
{ {
Online::RequestManager::get()->update(0.0f); Online::RequestManager::get()->update(0.0f);
StkTime::sleep(1); StkTime::sleep(1);
if (PlayerManager::getCurrentOnlineState() == PlayerProfile::OS_SIGNED_IN)
{
break;
}
} }
can_wan = true;
}
else
{
Log::warn("main", "No saved online player session to create a wan server");
}
if (can_wan)
{
NetworkConfig::get()->setServerName(StringUtils::xmlDecode(s)); NetworkConfig::get()->setServerName(StringUtils::xmlDecode(s));
NetworkConfig::get()->setIsServer(true); NetworkConfig::get()->setIsServer(true);
NetworkConfig::get()->setIsWAN(); NetworkConfig::get()->setIsWAN();
STKHost::create(); STKHost::create();
Log::info("main", "Creating a WAN server '%s'.", s.c_str()); 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)) 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. // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "network/network_config.hpp" #include "network/network_config.hpp"
#include "online/xml_request.hpp"
NetworkConfig *NetworkConfig::m_network_config = NULL; NetworkConfig *NetworkConfig::m_network_config = NULL;
bool NetworkConfig::m_disable_lan = false; bool NetworkConfig::m_disable_lan = false;
@ -41,6 +42,8 @@ NetworkConfig::NetworkConfig()
m_is_public_server = false; m_is_public_server = false;
m_max_players = 4; m_max_players = 4;
m_direct_connect = false; m_direct_connect = false;
m_cur_user_id = 0;
m_cur_user_token = "";
m_server_name = ""; m_server_name = "";
m_password = ""; m_password = "";
m_server_discovery_port = 2757; m_server_discovery_port = 2757;
@ -106,3 +109,10 @@ std::pair<RaceManager::MinorRaceModeType, bool>
return { RaceManager::MINOR_MODE_NORMAL_RACE, false }; return { RaceManager::MINOR_MODE_NORMAL_RACE, false };
} // getLocalGameMode } // 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" #include "irrString.h"
namespace Online
{
class XMLRequest;
}
class NetworkConfig class NetworkConfig
{ {
private: private:
@ -79,6 +84,10 @@ private:
/** If this is a server, the server name. */ /** If this is a server, the server name. */
irr::core::stringw m_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(); NetworkConfig();
public: public:
@ -198,6 +207,16 @@ public:
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
bool isDirectConnect() const { return m_direct_connect; } 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 }; // class NetworkConfig

View File

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

View File

@ -215,6 +215,15 @@ void CreateServerScreen::createServer()
std::string server_string = NetworkConfig::get()->isWAN() ? std::string server_string = NetworkConfig::get()->isWAN() ?
"--public-server --wan-server=" : "--lan-server="; "--public-server --wan-server=" : "--lan-server=";
server_string += StringUtils::xmlEncode(name); 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]; char option[1024];
sprintf(option, " --no-graphics --type=%d --difficulty=%d " sprintf(option, " --no-graphics --type=%d --difficulty=%d "
"--max-players=%d --network-console --no-console-log " "--max-players=%d --network-console --no-console-log "