void CClient::newGame( CConnection *con, StartInfo *si ) { enum {SINGLE, HOST, GUEST} networkMode = SINGLE; if (con == nullptr) { CServerHandler sh; serv = sh.connectToServer(); } else { serv = con; networkMode = (con->connectionID == 1) ? HOST : GUEST; } CConnection &c = *serv; //////////////////////////////////////////////////// logNetwork->infoStream() <<"\tWill send info to server..."; CStopWatch tmh; if(networkMode == SINGLE) { ui8 pom8; c << ui8(2) << ui8(1); //new game; one client c << *si; c >> pom8; if(pom8) throw std::runtime_error("Server cannot open the map!"); }
void CClient::loadGame(const std::string & fname, const bool server, const std::vector<int>& humanplayerindices, const int loadNumPlayers, int player_, const std::string & ipaddr, const std::string & port) { PlayerColor player(player_); //intentional shadowing logNetwork->infoStream() <<"Loading procedure started!"; CServerHandler sh; if(server) sh.startServer(); else serv = sh.justConnectToServer(ipaddr,port=="" ? "3030" : port); CStopWatch tmh; unique_ptr<CLoadFile> loader; try { std::string clientSaveName = *CResourceHandler::get("local")->getResourceName(ResourceID(fname, EResType::CLIENT_SAVEGAME)); std::string controlServerSaveName; if (CResourceHandler::get("local")->existsResource(ResourceID(fname, EResType::SERVER_SAVEGAME))) { controlServerSaveName = *CResourceHandler::get("local")->getResourceName(ResourceID(fname, EResType::SERVER_SAVEGAME)); } else// create entry for server savegame. Triggered if save was made after launch and not yet present in res handler { controlServerSaveName = clientSaveName.substr(0, clientSaveName.find_last_of(".")) + ".vsgm1"; CResourceHandler::get("local")->createResource(controlServerSaveName, true); } if(clientSaveName.empty()) throw std::runtime_error("Cannot open client part of " + fname); if(controlServerSaveName.empty()) throw std::runtime_error("Cannot open server part of " + fname); { CLoadIntegrityValidator checkingLoader(clientSaveName, controlServerSaveName, minSupportedVersion); loadCommonState(checkingLoader); loader = checkingLoader.decay(); } logNetwork->infoStream() << "Loaded common part of save " << tmh.getDiff(); const_cast<CGameInfo*>(CGI)->mh = new CMapHandler(); const_cast<CGameInfo*>(CGI)->mh->map = gs->map; pathInfo = make_unique<CPathsInfo>(getMapSize()); CGI->mh->init(); logNetwork->infoStream() <<"Initing maphandler: "<<tmh.getDiff(); } catch(std::exception &e) { logGlobal->errorStream() << "Cannot load game " << fname << ". Error: " << e.what(); throw; //obviously we cannot continue here } /* if(!server) player = PlayerColor(player_); */ std::set<PlayerColor> clientPlayers; if(server) serv = sh.connectToServer(); //*loader >> *this; if(server) { tmh.update(); ui8 pom8; *serv << ui8(3) << ui8(loadNumPlayers); //load game; one client if single-player *serv << fname; *serv >> pom8; if(pom8) throw std::runtime_error("Server cannot open the savegame!"); else logNetwork->infoStream() << "Server opened savegame properly."; } if(server) { for(auto & elem : gs->scenarioOps->playerInfos) if(!std::count(humanplayerindices.begin(),humanplayerindices.end(),elem.first.getNum()) || elem.first==player) { clientPlayers.insert(elem.first); } clientPlayers.insert(PlayerColor::NEUTRAL); } else { clientPlayers.insert(player); } std::cout << "CLIENTPLAYERS:\n"; for(auto x : clientPlayers) std::cout << x << std::endl; std::cout << "ENDCLIENTPLAYERS\n"; serialize(loader->serializer,0,clientPlayers); *serv << ui32(clientPlayers.size()); for(auto & elem : clientPlayers) *serv << ui8(elem.getNum()); serv->addStdVecItems(gs); /*why is this here?*/ //*loader >> *this; logNetwork->infoStream() << "Loaded client part of save " << tmh.getDiff(); logNetwork->infoStream() <<"Sent info to server: "<<tmh.getDiff(); //*serv << clientPlayers; serv->enableStackSendingByID(); serv->disableSmartPointerSerialization(); // logGlobal->traceStream() << "Objects:"; // for(int i = 0; i < gs->map->objects.size(); i++) // { // auto o = gs->map->objects[i]; // if(o) // logGlobal->traceStream() << boost::format("\tindex=%5d, id=%5d; address=%5d, pos=%s, name=%s") % i % o->id % (int)o.get() % o->pos % o->getHoverText(); // else // logGlobal->traceStream() << boost::format("\tindex=%5d --- nullptr") % i; // } }
void CClient::loadGame( const std::string & fname ) { logNetwork->infoStream() <<"Loading procedure started!"; CServerHandler sh; sh.startServer(); CStopWatch tmh; try { std::string clientSaveName = CResourceHandler::get()->getResourceName(ResourceID(fname, EResType::CLIENT_SAVEGAME)); std::string controlServerSaveName; if (CResourceHandler::get()->existsResource(ResourceID(fname, EResType::SERVER_SAVEGAME))) { controlServerSaveName = CResourceHandler::get()->getResourceName(ResourceID(fname, EResType::SERVER_SAVEGAME)); } else// create entry for server savegame. Triggered if save was made after launch and not yet present in res handler { controlServerSaveName = clientSaveName.substr(0, clientSaveName.find_last_of(".")) + ".vsgm1"; CResourceHandler::get()->createResource(controlServerSaveName, true); } if(clientSaveName.empty()) throw std::runtime_error("Cannot open client part of " + fname); if(controlServerSaveName.empty()) throw std::runtime_error("Cannot open server part of " + fname); unique_ptr<CLoadFile> loader; { CLoadIntegrityValidator checkingLoader(clientSaveName, controlServerSaveName); loadCommonState(checkingLoader); loader = checkingLoader.decay(); } logNetwork->infoStream() << "Loaded common part of save " << tmh.getDiff(); const_cast<CGameInfo*>(CGI)->mh = new CMapHandler(); const_cast<CGameInfo*>(CGI)->mh->map = gs->map; pathInfo = make_unique<CPathsInfo>(getMapSize()); CGI->mh->init(); logNetwork->infoStream() <<"Initing maphandler: "<<tmh.getDiff(); *loader >> *this; logNetwork->infoStream() << "Loaded client part of save " << tmh.getDiff(); } catch(std::exception &e) { logGlobal->errorStream() << "Cannot load game " << fname << ". Error: " << e.what(); throw; //obviously we cannot continue here } serv = sh.connectToServer(); serv->addStdVecItems(gs); tmh.update(); ui8 pom8; *serv << ui8(3) << ui8(1); //load game; one client *serv << fname; *serv >> pom8; if(pom8) throw std::runtime_error("Server cannot open the savegame!"); else logNetwork->infoStream() << "Server opened savegame properly."; *serv << ui32(gs->scenarioOps->playerInfos.size()+1); //number of players + neutral for(auto it = gs->scenarioOps->playerInfos.begin(); it != gs->scenarioOps->playerInfos.end(); ++it) { *serv << ui8(it->first.getNum()); //players } *serv << ui8(PlayerColor::NEUTRAL.getNum()); logNetwork->infoStream() <<"Sent info to server: "<<tmh.getDiff(); serv->enableStackSendingByID(); serv->disableSmartPointerSerialization(); }