diff options
author | Stijn Buys <ingar@osirion.org> | 2011-07-10 19:31:42 +0000 |
---|---|---|
committer | Stijn Buys <ingar@osirion.org> | 2011-07-10 19:31:42 +0000 |
commit | e18fa1d8ad9208a9f5bcfe2d97d36ae3675c80f1 (patch) | |
tree | 427f60f1bd7a4718cc5c96d79d40e9c4c650d5bc /src/core/gameconnection.cc | |
parent | af5404ea593d460d3ae843a23b295e3cfeade5bc (diff) |
Generate a player ID when connecting to a server,
read it from keys.ini if it was previously generated.
Diffstat (limited to 'src/core/gameconnection.cc')
-rw-r--r-- | src/core/gameconnection.cc | 71 |
1 files changed, 50 insertions, 21 deletions
diff --git a/src/core/gameconnection.cc b/src/core/gameconnection.cc index ebd5a49..a62fb4f 100644 --- a/src/core/gameconnection.cc +++ b/src/core/gameconnection.cc @@ -24,7 +24,56 @@ GameConnection::GameConnection(std::string const &connectionstr) connection_instance = this; connection_network = 0; connection_netframe = 0; + + // clear current guid + localplayer()->guid().clear(); + + // read keys.ini + std::string filename(filesystem::homedir()); + filename.append("keys.ini"); + + std::ifstream ifs(filename.c_str()); + if (ifs.is_open()) { + filesystem::IniStream inistr; + + while(inistr.getline(ifs)) { + if (inistr.in_section("client")) { + std::string s; + if (inistr.got_key_string("key", s)) { + aux::to_lowercase(s); + aux::trim(s); + localplayer()->guid().assign(s); + if (!localplayer()->guid().is_valid()) { + con_warn << "keys.ini invalid client key '" << s << "' at line " << inistr.line() << std::endl; + + } + } + } + } + + ifs.close(); + } + // if no valid key was found, generate one + if (!localplayer()->guid().is_valid()) { + localplayer()->guid().generate(); + } + + // write keys.ini + std::ofstream ofs(filename.c_str()); + if (!ofs.is_open()) { + con_warn << "Could not write " << filename << std::endl; + } else { + ofs << "; keys.ini - osirion client identification" << std::endl; + ofs << "; DO NOT EDIT OR DELETE THIS FILE" << std::endl; + ofs << "; If you do, you will not be able to use existing characters in multiplayer games" << std::endl; + ofs << std::endl; + ofs << "[client]" << std::endl; + ofs << "key=" << localplayer()->guid().str() << std::endl; + ofs.close(); + } + + // split hostname into host and port unsigned int port = DEFAULTPORT; std::string host(connectionstr); size_t found = host.find(':'); @@ -41,6 +90,7 @@ GameConnection::GameConnection(std::string const &connectionstr) } + // initiate the network connection connection_network = new NetConnection(); connection_network->connect(host, port); @@ -61,27 +111,6 @@ GameConnection::GameConnection(std::string const &connectionstr) game_players.push_back(localplayer()); set_playerlist_timestamp(timestamp()); - // generate player GUID - // we do it here because offline play doesn't require it - if (!localplayer()->guid().size()) { - - // read keys.ini - - // write keys.ini - std::string filename(filesystem::homedir()); - filename.append("keys.ini"); - - std::ofstream ofs(filename.c_str()); - if (!ofs.is_open()) { - con_warn << "Could not write " << filename << std::endl; - } else { - ofs << "; keys.ini - osirion client identification" << std::endl; - ofs << "; DO NOT EDIT OR DELETE THIS FILE" << std::endl; - ofs << "; If you do you will not be able to use existing characters on a remote server" << std::endl; - ofs.close(); - } - } - set_interactive(true); set_running(true); } |