#include "../hch/CCampaignHandler.h" #include "../CCallback.h" #include "../CConsoleHandler.h" #include "CGameInfo.h" #include "../lib/CGameState.h" #include "CPlayerInterface.h" #include "../StartInfo.h" #include "../hch/CArtHandler.h" #include "../hch/CDefObjInfoHandler.h" #include "../hch/CGeneralTextHandler.h" #include "../hch/CHeroHandler.h" #include "../hch/CTownHandler.h" #include "../hch/CObjectHandler.h" #include "../hch/CBuildingHandler.h" #include "../hch/CSpellHandler.h" #include "../lib/Connection.h" #include "../lib/Interprocess.h" #include "../lib/NetPacks.h" #include "../lib/VCMI_Lib.h" #include "../lib/map.h" #include "mapHandler.h" #include "CConfigHandler.h" #include "Client.h" #include "GUIBase.h" #include #include #include #include #include #undef DLL_EXPORT #define DLL_EXPORT #include "../lib/RegisterTypes.cpp" extern std::string NAME; namespace intpr = boost::interprocess; /* * Client.cpp, part of VCMI engine * * Authors: listed in file AUTHORS in main folder * * License: GNU General Public License v2.0 or later * Full text of license available in license.txt file, in main folder * */ class CBaseForCLApply { public: virtual void applyOnClAfter(CClient *cl, void *pack) const =0; virtual void applyOnClBefore(CClient *cl, void *pack) const =0; virtual ~CBaseForCLApply(){} }; template class CApplyOnCL : public CBaseForCLApply { public: void applyOnClAfter(CClient *cl, void *pack) const { T *ptr = static_cast(pack); ptr->applyCl(cl); } void applyOnClBefore(CClient *cl, void *pack) const { T *ptr = static_cast(pack); ptr->applyFirstCl(cl); } }; class CCLApplier { public: std::map apps; CCLApplier() { registerTypes2(*this); } ~CCLApplier() { std::map::iterator iter; for(iter = apps.begin(); iter != apps.end(); iter++) delete iter->second; } template void registerType(const T * t=NULL) { ui16 ID = typeList.registerType(t); apps[ID] = new CApplyOnCL; } } *applier = NULL; void CClient::init() { hotSeat = false; connectionHandler = NULL; pathInfo = NULL; applier = new CCLApplier; IObjectInterface::cb = this; serv = NULL; gs = NULL; cb = NULL; terminate = false; boost::interprocess::shared_memory_object::remove("vcmi_memory"); //if the application has previously crashed, the memory may not have been removed. to avoid problems - try to destroy it try { shared = new SharedMem(); } HANDLE_EXCEPTIONC(tlog1 << "Cannot open interprocess memory: ";) } CClient::CClient(void) :waitingRequest(false) { init(); } CClient::CClient(CConnection *con, StartInfo *si) :waitingRequest(false) { init(); newGame(con,si); } CClient::~CClient(void) { delete pathInfo; delete applier; delete shared; } void CClient::waitForMoveAndSend(int color) { try { BattleAction ba = playerint[color]->activeStack(gs->curB->activeStack); *serv << &MakeAction(ba); return; }HANDLE_EXCEPTION tlog1 << "We should not be here!" << std::endl; } void CClient::run() { try { CPack *pack = NULL; while(!terminate) { pack = retreivePack(); //get the package from the server if (terminate) { delete pack; pack = NULL; break; } handlePack(pack); pack = NULL; } } catch (const std::exception& e) { tlog3 << "Lost connection to server, ending listening thread!\n"; tlog1 << e.what() << std::endl; if(!terminate) //rethrow (-> boom!) only if closing connection was unexpected { tlog1 << "Something wrong, lost connection while game is still ongoing...\n"; throw; } } } void CClient::stop() { // Game is ending // Tell the network thread to reach a stable state terminate = true; GH.curInt = NULL; LOCPLINT->terminate_cond.setn(true); LOCPLINT->pim->lock(); endGame(); tlog0 << "Client stopped." << std::endl; } void CClient::save(const std::string & fname) { if(gs->curB) { tlog1 << "Game cannot be saved during battle!\n"; return; } *serv << &SaveGame(fname); } #include void initVillagesCapitols(Mapa * map) { std::ifstream ifs(DATA_DIR "/config/townsDefs.txt"); int ccc; ifs>>ccc; for(int i=0;istate->villages[i]; map->defy.push_back(CGI->state->forts[i]); } else n = CGI->state->capitols[i%ccc]; ifs >> n->name; if(!n) tlog1 << "*HUGE* Warning - missing town def for " << i << std::endl; else map->defy.push_back(n); } } void CClient::endGame() { tlog0 << "\n\nEnding current game!" << std::endl; if(GH.topInt()) GH.topInt()->deactivate(); GH.listInt.clear(); GH.objsToBlit.clear(); GH.statusbar = NULL; tlog0 << "Removed GUI." << std::endl; delete CGI->mh; CGI->mh = NULL; delete CGI->state; CGI->state = NULL; tlog0 << "Deleted mapHandler and gameState." << std::endl; LOCPLINT = NULL; while (!playerint.empty()) { delete playerint.begin()->second; playerint.erase(playerint.begin()); } BOOST_FOREACH(CCallback *cb, callbacks) { delete cb; } tlog0 << "Deleted playerInts." << std::endl; if (serv) { tlog0 << "Connection has been requested to be closed.\n"; boost::unique_lock(*serv->wmx); *serv << &CloseServer(); tlog0 << "Sent closing signal to the server\n"; serv->close(); delete serv; serv = NULL; tlog3 << "Our socket has been closed." << std::endl; } connectionHandler->join(); tlog0 << "Connection handler thread joined" << std::endl; delete connectionHandler; connectionHandler = NULL; } void CClient::loadGame( const std::string & fname ) { tlog0 <<"\n\nLoading procedure started!\n\n"; timeHandler tmh; char portc[10]; SDL_itoa(conf.cc.port,portc,10); runServer(portc); //create new server tlog0 <<"Restarting server: "<mh = new CMapHandler(); StartInfo *si; CLoadFile lf(fname + ".vlgm1"); lf >> sig >> dum >> si; tlog0 <<"Reading save signature: "<> *VLC; CGI->setFromLib(); tlog0 <<"Reading handlers: "<> gs; tlog0 <<"Reading gamestate: "<state = gs; CGI->mh->map = gs->map; pathInfo = new CPathsInfo(int3(gs->map->width, gs->map->height, gs->map->twoLevel+1)); CGI->mh->init(); initVillagesCapitols(gs->map); tlog0 <<"Initing maphandler: "<addStdVecItems(gs); tlog0 <<"Setting up connection: "<> pom8; if(pom8) throw "Server cannot open the savegame!"; else tlog0 << "Server opened savegame properly.\n"; *serv << ui8(gs->scenarioOps->playerInfos.size()+1); //number of players + neutral for(std::map::iterator it = gs->scenarioOps->playerInfos.begin(); it != gs->scenarioOps->playerInfos.end(); ++it) { *serv << ui8(it->first); //players } *serv << ui8(255); // neutrals tlog0 <<"Sent info to server: "<> *this; } } int CClient::getCurrentPlayer() { return gs->currentPlayer; } int CClient::getSelectedHero() { if(const CGHeroInstance *selHero = IGameCallback::getSelectedHero(getCurrentPlayer())) return selHero->id; else return -1; } void CClient::newGame( CConnection *con, StartInfo *si ) { if (con == NULL) { timeHandler pomtime; char portc[10]; SDL_itoa(conf.cc.port,portc,10); CClient::runServer(portc); tlog0<<"Preparing shared memory and starting server: "<state = new CGameState(); tlog0 <<"\tGamestate: "<> pom8; if(pom8) throw "Server cannot open the map!"; else tlog0 << "Server opened map properly.\n"; c << ui8(si->playerInfos.size()+1); //number of players + neutral for(std::map::iterator it =si->playerInfos.begin(); it != si->playerInfos.end(); ++it) { c << ui8(it->first); //players } c << ui8(255); // neutrals ui32 seed, sum; delete si; c >> si >> sum >> seed; tlog0 <<"\tSending/Getting info to/from the server: "<state; gs->scenarioOps = si; gs->init(si, sum, seed); CGI->mh = new CMapHandler(); tlog0 <<"Initializing GameState (together): "<mh->map = gs->map; tlog0 <<"Creating mapHandler: "<mh->init(); initVillagesCapitols(gs->map); pathInfo = new CPathsInfo(int3(gs->map->width, gs->map->height, gs->map->twoLevel+1)); tlog0 <<"Initializing mapHandler (together): "<::iterator it = gs->scenarioOps->playerInfos.begin(); it != gs->scenarioOps->playerInfos.end(); ++it)//initializing interfaces for players { ui8 color = it->first; CCallback *cb = new CCallback(gs,color,this); if(!it->second.human) { playerint[color] = static_cast(CAIHandler::getNewAI(cb,conf.cc.defaultAI)); } else { playerint[color] = new CPlayerInterface(color); humanPlayers++; } gs->currentPlayer = color; playerint[color]->init(cb); } serv->addStdVecItems(CGI->state); hotSeat = (humanPlayers > 1); playerint[255] = CAIHandler::getNewAI(cb,conf.cc.defaultAI); playerint[255]->init(new CCallback(gs,255,this)); } void CClient::runServer(const char * portc) { static std::string comm = std::string(BIN_DIR PATH_SEPARATOR SERVER_NAME " ") + portc + " > server_log.txt"; //needs to be static, if not - will be probably destroyed before new thread reads it boost::thread servthr(boost::bind(system,comm.c_str())); //runs server executable; //TODO: will it work on non-windows platforms? } void CClient::waitForServer() { intpr::scoped_lock slock(shared->sr->mutex); while(!shared->sr->ready) { shared->sr->cond.wait(slock); } } template void CClient::serialize( Handler &h, const int version ) { h & hotSeat; if(h.saving) { ui8 players = playerint.size(); h & players; for(std::map::iterator i = playerint.begin(); i != playerint.end(); i++) { h & i->first & i->second->dllName; i->second->serialize(h,version); } } else { ui8 players; h & players; for(int i=0; i < players; i++) { std::string dllname; ui8 pid; h & pid & dllname; CCallback *callback = new CCallback(gs,pid,this); callbacks.insert(callback); CGameInterface *nInt = NULL; if(dllname.length()) nInt = CAIHandler::getNewAI(callback,dllname); else nInt = new CPlayerInterface(pid); playerint[pid] = nInt; nInt->init(callback); nInt->serialize(h, version); } } } CPack * CClient::retreivePack() { CPack *ret = NULL; boost::unique_lock lock(*serv->rmx); tlog5 << "Listening... "; *serv >> ret; tlog5 << "\treceived server message of type " << typeid(*ret).name() << std::endl; return ret; } void CClient::handlePack( CPack * pack ) { CBaseForCLApply *apply = applier->apps[typeList.getTypeID(pack)]; //find the applier if(apply) { apply->applyOnClBefore(this,pack); tlog5 << "\tMade first apply on cl\n"; gs->apply(pack); tlog5 << "\tApplied on gs\n"; apply->applyOnClAfter(this,pack); tlog5 << "\tMade second apply on cl\n"; } else { tlog1 << "Message cannot be applied, cannot find applier!\n"; } delete pack; } void CClient::updatePaths() { const CGHeroInstance *h = getHero(getSelectedHero()); if (h)//if we have selected hero... gs->calculatePaths(h, *pathInfo); } template void CClient::serialize( CISer &h, const int version ); template void CClient::serialize( COSer &h, const int version );