Client.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  1. #include "CMusicHandler.h"
  2. #include "../lib/CCampaignHandler.h"
  3. #include "../CCallback.h"
  4. #include "../CConsoleHandler.h"
  5. #include "CGameInfo.h"
  6. #include "../lib/CGameState.h"
  7. #include "CPlayerInterface.h"
  8. #include "../StartInfo.h"
  9. #include "../lib/BattleState.h"
  10. #include "../lib/CArtHandler.h"
  11. #include "../lib/CDefObjInfoHandler.h"
  12. #include "../lib/CGeneralTextHandler.h"
  13. #include "../lib/CHeroHandler.h"
  14. #include "../lib/CTownHandler.h"
  15. #include "../lib/CObjectHandler.h"
  16. #include "../lib/CBuildingHandler.h"
  17. #include "../lib/CSpellHandler.h"
  18. #include "../lib/Connection.h"
  19. #include "../lib/Interprocess.h"
  20. #include "../lib/NetPacks.h"
  21. #include "../lib/VCMI_Lib.h"
  22. #include "../lib/map.h"
  23. #include "mapHandler.h"
  24. #include "CConfigHandler.h"
  25. #include "Client.h"
  26. #include "GUIBase.h"
  27. #include <boost/bind.hpp>
  28. #include <boost/foreach.hpp>
  29. #include <boost/thread.hpp>
  30. #include <boost/thread/shared_mutex.hpp>
  31. #include <boost/lexical_cast.hpp>
  32. #include <sstream>
  33. #include "CPreGame.h"
  34. #include "CBattleInterface.h"
  35. #include "../CThreadHelper.h"
  36. #include "../lib/ERMScriptModule.h"
  37. #define NOT_LIB
  38. #include "../lib/RegisterTypes.cpp"
  39. extern std::string NAME;
  40. namespace intpr = boost::interprocess;
  41. /*
  42. * Client.cpp, part of VCMI engine
  43. *
  44. * Authors: listed in file AUTHORS in main folder
  45. *
  46. * License: GNU General Public License v2.0 or later
  47. * Full text of license available in license.txt file, in main folder
  48. *
  49. */
  50. template <typename T> class CApplyOnCL;
  51. class CBaseForCLApply
  52. {
  53. public:
  54. virtual void applyOnClAfter(CClient *cl, void *pack) const =0;
  55. virtual void applyOnClBefore(CClient *cl, void *pack) const =0;
  56. virtual ~CBaseForCLApply(){}
  57. template<typename U> static CBaseForCLApply *getApplier(const U * t=NULL)
  58. {
  59. return new CApplyOnCL<U>;
  60. }
  61. };
  62. template <typename T> class CApplyOnCL : public CBaseForCLApply
  63. {
  64. public:
  65. void applyOnClAfter(CClient *cl, void *pack) const
  66. {
  67. T *ptr = static_cast<T*>(pack);
  68. ptr->applyCl(cl);
  69. }
  70. void applyOnClBefore(CClient *cl, void *pack) const
  71. {
  72. T *ptr = static_cast<T*>(pack);
  73. ptr->applyFirstCl(cl);
  74. }
  75. };
  76. static CApplier<CBaseForCLApply> *applier = NULL;
  77. void CClient::init()
  78. {
  79. hotSeat = false;
  80. connectionHandler = NULL;
  81. pathInfo = NULL;
  82. applier = new CApplier<CBaseForCLApply>;
  83. registerTypes2(*applier);
  84. IObjectInterface::cb = this;
  85. serv = NULL;
  86. gs = NULL;
  87. cb = NULL;
  88. terminate = false;
  89. }
  90. CClient::CClient(void)
  91. :waitingRequest(false)
  92. {
  93. init();
  94. }
  95. CClient::CClient(CConnection *con, StartInfo *si)
  96. :waitingRequest(false)
  97. {
  98. init();
  99. newGame(con,si);
  100. }
  101. CClient::~CClient(void)
  102. {
  103. delete pathInfo;
  104. delete applier;
  105. }
  106. void CClient::waitForMoveAndSend(int color)
  107. {
  108. try
  109. {
  110. assert(vstd::contains(battleints, color));
  111. BattleAction ba = battleints[color]->activeStack(gs->curB->getStack(gs->curB->activeStack, false));
  112. *serv << &MakeAction(ba);
  113. return;
  114. }HANDLE_EXCEPTION
  115. tlog1 << "We should not be here!" << std::endl;
  116. }
  117. void CClient::run()
  118. {
  119. setThreadName(-1, "CClient::run");
  120. try
  121. {
  122. CPack *pack = NULL;
  123. while(!terminate)
  124. {
  125. pack = serv->retreivePack(); //get the package from the server
  126. if (terminate)
  127. {
  128. delete pack;
  129. pack = NULL;
  130. break;
  131. }
  132. handlePack(pack);
  133. pack = NULL;
  134. }
  135. }
  136. catch (const std::exception& e)
  137. {
  138. tlog3 << "Lost connection to server, ending listening thread!\n";
  139. tlog1 << e.what() << std::endl;
  140. if(!terminate) //rethrow (-> boom!) only if closing connection was unexpected
  141. {
  142. tlog1 << "Something wrong, lost connection while game is still ongoing...\n";
  143. throw;
  144. }
  145. }
  146. }
  147. void CClient::save(const std::string & fname)
  148. {
  149. if(gs->curB)
  150. {
  151. tlog1 << "Game cannot be saved during battle!\n";
  152. return;
  153. }
  154. *serv << &SaveGame(fname);
  155. }
  156. #include <fstream>
  157. void initVillagesCapitols(Mapa * map)
  158. {
  159. std::ifstream ifs(DATA_DIR "/config/townsDefs.txt");
  160. int ccc;
  161. ifs>>ccc;
  162. for(int i=0;i<ccc*2;i++)
  163. {
  164. const CGDefInfo *n;
  165. if(i<ccc)
  166. {
  167. n = CGI->state->villages[i];
  168. map->defy.push_back(CGI->state->forts[i]);
  169. }
  170. else
  171. n = CGI->state->capitols[i%ccc];
  172. ifs >> const_cast<CGDefInfo*>(n)->name;
  173. if(!n)
  174. tlog1 << "*HUGE* Warning - missing town def for " << i << std::endl;
  175. else
  176. map->defy.push_back(const_cast<CGDefInfo*>(n));
  177. }
  178. }
  179. void CClient::endGame( bool closeConnection /*= true*/ )
  180. {
  181. // Game is ending
  182. // Tell the network thread to reach a stable state
  183. GH.curInt = NULL;
  184. LOCPLINT->terminate_cond.setn(true);
  185. LOCPLINT->pim->lock();
  186. tlog0 << "\n\nEnding current game!" << std::endl;
  187. if(GH.topInt())
  188. GH.topInt()->deactivate();
  189. GH.listInt.clear();
  190. GH.objsToBlit.clear();
  191. GH.statusbar = NULL;
  192. tlog0 << "Removed GUI." << std::endl;
  193. delete CGI->mh;
  194. const_cast<CGameInfo*>(CGI)->mh = NULL;
  195. const_cast<CGameInfo*>(CGI)->state.dellNull();
  196. tlog0 << "Deleted mapHandler and gameState." << std::endl;
  197. CPlayerInterface * oldInt = LOCPLINT;
  198. LOCPLINT = NULL;
  199. oldInt->pim->unlock();
  200. while (!playerint.empty())
  201. {
  202. CGameInterface *pint = playerint.begin()->second;
  203. playerint.erase(playerint.begin());
  204. delete pint;
  205. }
  206. BOOST_FOREACH(CCallback *cb, callbacks)
  207. {
  208. delete cb;
  209. }
  210. tlog0 << "Deleted playerInts." << std::endl;
  211. if(closeConnection)
  212. stopConnection();
  213. tlog0 << "Client stopped." << std::endl;
  214. }
  215. void CClient::loadGame( const std::string & fname )
  216. {
  217. tlog0 <<"\n\nLoading procedure started!\n\n";
  218. CServerHandler sh;
  219. sh.startServer();
  220. timeHandler tmh;
  221. {
  222. char sig[8];
  223. CMapHeader dum;
  224. const_cast<CGameInfo*>(CGI)->mh = new CMapHandler();
  225. StartInfo *si;
  226. CLoadFile lf(fname + ".vlgm1");
  227. lf >> sig >> dum >> si;
  228. tlog0 <<"Reading save signature: "<<tmh.getDif()<<std::endl;
  229. lf >> *VLC;
  230. const_cast<CGameInfo*>(CGI)->setFromLib();
  231. tlog0 <<"Reading handlers: "<<tmh.getDif()<<std::endl;
  232. lf >> gs;
  233. tlog0 <<"Reading gamestate: "<<tmh.getDif()<<std::endl;
  234. const_cast<CGameInfo*>(CGI)->state = gs;
  235. const_cast<CGameInfo*>(CGI)->mh->map = gs->map;
  236. pathInfo = new CPathsInfo(int3(gs->map->width, gs->map->height, gs->map->twoLevel+1));
  237. CGI->mh->init();
  238. initVillagesCapitols(gs->map);
  239. tlog0 <<"Initing maphandler: "<<tmh.getDif()<<std::endl;
  240. }
  241. serv = sh.connectToServer();
  242. serv->addStdVecItems(gs);
  243. tmh.update();
  244. ui8 pom8;
  245. *serv << ui8(3) << ui8(1); //load game; one client
  246. *serv << fname;
  247. *serv >> pom8;
  248. if(pom8)
  249. throw "Server cannot open the savegame!";
  250. else
  251. tlog0 << "Server opened savegame properly.\n";
  252. *serv << ui32(gs->scenarioOps->playerInfos.size()+1); //number of players + neutral
  253. for(std::map<int, PlayerSettings>::iterator it = gs->scenarioOps->playerInfos.begin();
  254. it != gs->scenarioOps->playerInfos.end(); ++it)
  255. {
  256. *serv << ui8(it->first); //players
  257. }
  258. *serv << ui8(255); // neutrals
  259. tlog0 <<"Sent info to server: "<<tmh.getDif()<<std::endl;
  260. {
  261. CLoadFile lf(fname + ".vcgm1");
  262. lf >> *this;
  263. }
  264. }
  265. void CClient::newGame( CConnection *con, StartInfo *si )
  266. {
  267. enum {SINGLE, HOST, GUEST} networkMode = SINGLE;
  268. std::set<ui8> myPlayers;
  269. if (con == NULL)
  270. {
  271. CServerHandler sh;
  272. serv = sh.connectToServer();
  273. }
  274. else
  275. {
  276. serv = con;
  277. networkMode = (con->connectionID == 1) ? HOST : GUEST;
  278. }
  279. for(std::map<int, PlayerSettings>::iterator it =si->playerInfos.begin();
  280. it != si->playerInfos.end(); ++it)
  281. {
  282. if(networkMode == SINGLE //single - one client has all player
  283. || networkMode != SINGLE && serv->connectionID == it->second.human //multi - client has only "its players"
  284. || networkMode == HOST && it->second.human == false) //multi - host has all AI players
  285. {
  286. myPlayers.insert(ui8(it->first)); //add player
  287. }
  288. }
  289. if(networkMode != GUEST)
  290. myPlayers.insert(255); //neutral
  291. timeHandler tmh;
  292. const_cast<CGameInfo*>(CGI)->state = new CGameState();
  293. tlog0 <<"\tGamestate: "<<tmh.getDif()<<std::endl;
  294. CConnection &c(*serv);
  295. ////////////////////////////////////////////////////
  296. if(networkMode == SINGLE)
  297. {
  298. ui8 pom8;
  299. c << ui8(2) << ui8(1); //new game; one client
  300. c << *si;
  301. c >> pom8;
  302. if(pom8)
  303. throw "Server cannot open the map!";
  304. else
  305. tlog0 << "Server opened map properly.\n";
  306. }
  307. c << myPlayers;
  308. ui32 seed, sum;
  309. c >> si >> sum >> seed;
  310. tlog0 <<"\tSending/Getting info to/from the server: "<<tmh.getDif()<<std::endl;
  311. tlog0 << "\tUsing random seed: "<<seed << std::endl;
  312. gs = const_cast<CGameInfo*>(CGI)->state;
  313. gs->scenarioOps = si;
  314. gs->init(si, sum, seed);
  315. tlog0 <<"Initializing GameState (together): "<<tmh.getDif()<<std::endl;
  316. if(gs->map)
  317. {
  318. const_cast<CGameInfo*>(CGI)->mh = new CMapHandler();
  319. CGI->mh->map = gs->map;
  320. tlog0 <<"Creating mapHandler: "<<tmh.getDif()<<std::endl;
  321. CGI->mh->init();
  322. initVillagesCapitols(gs->map);
  323. pathInfo = new CPathsInfo(int3(gs->map->width, gs->map->height, gs->map->twoLevel+1));
  324. tlog0 <<"Initializing mapHandler (together): "<<tmh.getDif()<<std::endl;
  325. }
  326. int humanPlayers = 0;
  327. for(std::map<int, PlayerSettings>::iterator it = gs->scenarioOps->playerInfos.begin();
  328. it != gs->scenarioOps->playerInfos.end(); ++it)//initializing interfaces for players
  329. {
  330. ui8 color = it->first;
  331. gs->currentPlayer = color;
  332. if(!vstd::contains(myPlayers, color))
  333. continue;
  334. if(si->mode != StartInfo::DUEL)
  335. {
  336. CCallback *cb = new CCallback(gs,color,this);
  337. if(!it->second.human)
  338. {
  339. playerint[color] = static_cast<CGameInterface*>(CAIHandler::getNewAI(conf.cc.defaultPlayerAI));
  340. }
  341. else
  342. {
  343. playerint[color] = new CPlayerInterface(color);
  344. humanPlayers++;
  345. }
  346. battleints[color] = playerint[color];
  347. playerint[color]->init(cb);
  348. }
  349. else
  350. {
  351. CBattleCallback * cbc = new CBattleCallback(gs, color, this);
  352. battleints[color] = CAIHandler::getNewBattleAI("StupidAI");
  353. battleints[color]->init(cbc);
  354. }
  355. }
  356. if(si->mode == StartInfo::DUEL)
  357. {
  358. CPlayerInterface *p = new CPlayerInterface(-1);
  359. p->observerInDuelMode = true;
  360. battleints[254] = playerint[254] = p;
  361. GH.curInt = p;
  362. p->init(new CCallback(gs, -1, this));
  363. battleStarted(gs->curB);
  364. }
  365. else
  366. {
  367. loadNeutralBattleAI();
  368. }
  369. serv->addStdVecItems(const_cast<CGameInfo*>(CGI)->state);
  370. hotSeat = (humanPlayers > 1);
  371. CScriptingModule *erm = getERMModule();
  372. privilagedGameEventReceivers.push_back(erm);
  373. privilagedBattleEventReceivers.push_back(erm);
  374. icb = this;
  375. acb = this;
  376. erm->init();
  377. }
  378. template <typename Handler>
  379. void CClient::serialize( Handler &h, const int version )
  380. {
  381. h & hotSeat;
  382. if(h.saving)
  383. {
  384. ui8 players = playerint.size();
  385. h & players;
  386. for(std::map<ui8,CGameInterface *>::iterator i = playerint.begin(); i != playerint.end(); i++)
  387. {
  388. h & i->first & i->second->dllName;
  389. i->second->serialize(h,version);
  390. }
  391. }
  392. else
  393. {
  394. ui8 players;
  395. h & players;
  396. for(int i=0; i < players; i++)
  397. {
  398. std::string dllname;
  399. ui8 pid;
  400. h & pid & dllname;
  401. CGameInterface *nInt = NULL;
  402. if(dllname.length())
  403. {
  404. if(pid == 255)
  405. {
  406. CBattleCallback * cbc = new CBattleCallback(gs, pid, this);
  407. CBattleGameInterface *cbgi = CAIHandler::getNewBattleAI(dllname);
  408. battleints[pid] = cbgi;
  409. cbgi->init(cb);
  410. //TODO? consider serialization
  411. continue;
  412. }
  413. else
  414. nInt = CAIHandler::getNewAI(dllname);
  415. }
  416. else
  417. nInt = new CPlayerInterface(pid);
  418. CCallback *callback = new CCallback(gs,pid,this);
  419. callbacks.insert(callback);
  420. battleints[pid] = playerint[pid] = nInt;
  421. nInt->init(callback);
  422. nInt->serialize(h, version);
  423. }
  424. if(!vstd::contains(battleints, NEUTRAL_PLAYER))
  425. loadNeutralBattleAI();
  426. }
  427. }
  428. void CClient::handlePack( CPack * pack )
  429. {
  430. CBaseForCLApply *apply = applier->apps[typeList.getTypeID(pack)]; //find the applier
  431. if(apply)
  432. {
  433. apply->applyOnClBefore(this,pack);
  434. tlog5 << "\tMade first apply on cl\n";
  435. gs->apply(pack);
  436. tlog5 << "\tApplied on gs\n";
  437. apply->applyOnClAfter(this,pack);
  438. tlog5 << "\tMade second apply on cl\n";
  439. }
  440. else
  441. {
  442. tlog1 << "Message cannot be applied, cannot find applier!\n";
  443. }
  444. delete pack;
  445. }
  446. void CClient::updatePaths()
  447. {
  448. const CGHeroInstance *h = getSelectedHero();
  449. if (h)//if we have selected hero...
  450. gs->calculatePaths(h, *pathInfo);
  451. }
  452. void CClient::finishCampaign( CCampaignState * camp )
  453. {
  454. }
  455. void CClient::proposeNextMission( CCampaignState * camp )
  456. {
  457. GH.pushInt(new CBonusSelection(camp));
  458. GH.curInt = CGP;
  459. }
  460. void CClient::stopConnection()
  461. {
  462. terminate = true;
  463. if (serv) //request closing connection
  464. {
  465. tlog0 << "Connection has been requested to be closed.\n";
  466. boost::unique_lock<boost::mutex>(*serv->wmx);
  467. *serv << &CloseServer();
  468. tlog0 << "Sent closing signal to the server\n";
  469. }
  470. if(connectionHandler)//end connection handler
  471. {
  472. if(connectionHandler->get_id() != boost::this_thread::get_id())
  473. connectionHandler->join();
  474. tlog0 << "Connection handler thread joined" << std::endl;
  475. delete connectionHandler;
  476. connectionHandler = NULL;
  477. }
  478. if (serv) //and delete connection
  479. {
  480. serv->close();
  481. delete serv;
  482. serv = NULL;
  483. tlog3 << "Our socket has been closed." << std::endl;
  484. }
  485. }
  486. void CClient::battleStarted(const BattleInfo * info)
  487. {
  488. CPlayerInterface * att, * def;
  489. if(vstd::contains(playerint, info->sides[0]) && playerint[info->sides[0]]->human)
  490. att = static_cast<CPlayerInterface*>( playerint[info->sides[0]] );
  491. else
  492. att = NULL;
  493. if(vstd::contains(playerint, info->sides[1]) && playerint[info->sides[1]]->human)
  494. def = static_cast<CPlayerInterface*>( playerint[info->sides[1]] );
  495. else
  496. def = NULL;
  497. new CBattleInterface(info->belligerents[0], info->belligerents[1], info->heroes[0], info->heroes[1], Rect((conf.cc.resx - 800)/2, (conf.cc.resy - 600)/2, 800, 600), att, def);
  498. if(vstd::contains(battleints,info->sides[0]))
  499. battleints[info->sides[0]]->battleStart(info->belligerents[0], info->belligerents[1], info->tile, info->heroes[0], info->heroes[1], 0);
  500. if(vstd::contains(battleints,info->sides[1]))
  501. battleints[info->sides[1]]->battleStart(info->belligerents[0], info->belligerents[1], info->tile, info->heroes[0], info->heroes[1], 1);
  502. if(vstd::contains(battleints,254))
  503. battleints[254]->battleStart(info->belligerents[0], info->belligerents[1], info->tile, info->heroes[0], info->heroes[1], 1);
  504. }
  505. void CClient::loadNeutralBattleAI()
  506. {
  507. battleints[255] = CAIHandler::getNewBattleAI(conf.cc.defaultBattleAI);
  508. battleints[255]->init(new CBattleCallback(gs, 255, this));
  509. }
  510. void CClient::commitPackage( CPackForClient *pack )
  511. {
  512. CommitPackage cp;
  513. cp.packToCommit = pack;
  514. *serv << &cp;
  515. }
  516. template void CClient::serialize( CISer<CLoadFile> &h, const int version );
  517. template void CClient::serialize( COSer<CSaveFile> &h, const int version );
  518. void CServerHandler::startServer()
  519. {
  520. th.update();
  521. serverThread = new boost::thread(&CServerHandler::callServer, this); //runs server executable; //TODO: will it work on non-windows platforms?
  522. if(verbose)
  523. tlog0 << "Setting up thread calling server: " << th.getDif() << std::endl;
  524. }
  525. void CServerHandler::waitForServer()
  526. {
  527. if(!serverThread)
  528. startServer();
  529. th.update();
  530. intpr::scoped_lock<intpr::interprocess_mutex> slock(shared->sr->mutex);
  531. while(!shared->sr->ready)
  532. {
  533. shared->sr->cond.wait(slock);
  534. }
  535. if(verbose)
  536. tlog0 << "Waiting for server: " << th.getDif() << std::endl;
  537. }
  538. CConnection * CServerHandler::connectToServer()
  539. {
  540. if(!shared->sr->ready)
  541. waitForServer();
  542. th.update();
  543. CConnection *ret = justConnectToServer(conf.cc.server, port);
  544. if(verbose)
  545. tlog0<<"\tConnecting to the server: "<<th.getDif()<<std::endl;
  546. return ret;
  547. }
  548. CServerHandler::CServerHandler(bool runServer /*= false*/)
  549. {
  550. serverThread = NULL;
  551. shared = NULL;
  552. port = boost::lexical_cast<std::string>(conf.cc.port);
  553. 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
  554. try
  555. {
  556. shared = new SharedMem();
  557. } HANDLE_EXCEPTIONC(tlog1 << "Cannot open interprocess memory: ";)
  558. }
  559. CServerHandler::~CServerHandler()
  560. {
  561. delete shared;
  562. delete serverThread; //detaches, not kills thread
  563. }
  564. void CServerHandler::callServer()
  565. {
  566. setThreadName(-1, "CServerHandler::callServer");
  567. std::string comm = std::string(BIN_DIR PATH_SEPARATOR SERVER_NAME " ") + port + " > server_log.txt";
  568. std::system(comm.c_str());
  569. tlog0 << "Server finished\n";
  570. }
  571. CConnection * CServerHandler::justConnectToServer(const std::string &host, const std::string &port)
  572. {
  573. CConnection *ret = NULL;
  574. while(!ret)
  575. {
  576. try
  577. {
  578. tlog0 << "Establishing connection...\n";
  579. ret = new CConnection( host.size() ? host : conf.cc.server,
  580. port.size() ? port : boost::lexical_cast<std::string>(conf.cc.port),
  581. NAME);
  582. }
  583. catch(...)
  584. {
  585. tlog1 << "\nCannot establish connection! Retrying within 2 seconds" << std::endl;
  586. SDL_Delay(2000);
  587. }
  588. }
  589. return ret;
  590. }