2011-12-14 00:23:17 +03:00
|
|
|
#include "StdInc.h"
|
2008-07-03 18:03:32 +03:00
|
|
|
#include "Connection.h"
|
2009-10-06 03:32:33 +03:00
|
|
|
|
2009-04-15 02:29:26 +03:00
|
|
|
#ifndef _MSC_VER
|
2013-01-03 15:19:20 +03:00
|
|
|
#include "RegisterTypes.h"
|
2009-04-15 02:29:26 +03:00
|
|
|
#endif
|
2009-04-14 13:46:42 +03:00
|
|
|
|
2009-10-06 03:32:33 +03:00
|
|
|
//for smart objs serialization over net
|
2013-01-03 15:19:20 +03:00
|
|
|
#include "Mapping/CMapInfo.h"
|
2011-12-14 00:23:17 +03:00
|
|
|
#include "StartInfo.h"
|
2011-02-06 19:26:27 +02:00
|
|
|
#include "BattleState.h"
|
2009-10-06 03:32:33 +03:00
|
|
|
#include "CGameState.h"
|
2013-01-03 15:19:20 +03:00
|
|
|
#include "Mapping/CMap.h"
|
2012-08-10 16:07:53 +03:00
|
|
|
#include "CModHandler.h"
|
2010-12-20 23:22:53 +02:00
|
|
|
#include "CObjectHandler.h"
|
|
|
|
#include "CCreatureHandler.h"
|
2010-05-02 21:20:26 +03:00
|
|
|
#include "VCMI_Lib.h"
|
2010-12-20 23:22:53 +02:00
|
|
|
#include "CArtHandler.h"
|
|
|
|
#include "CHeroHandler.h"
|
2011-02-20 11:24:53 +02:00
|
|
|
#include "CSpellHandler.h"
|
2010-12-20 23:22:53 +02:00
|
|
|
#include "CTownHandler.h"
|
2013-01-03 15:19:20 +03:00
|
|
|
#include "Mapping/CCampaignHandler.h"
|
2010-09-03 21:42:54 +03:00
|
|
|
#include "NetPacks.h"
|
2012-11-20 20:53:45 +03:00
|
|
|
#include "CDefObjInfoHandler.h"
|
2009-10-06 03:32:33 +03:00
|
|
|
|
2011-12-14 00:23:17 +03:00
|
|
|
#include <boost/asio.hpp>
|
2009-10-06 03:32:33 +03:00
|
|
|
|
2009-04-15 17:03:31 +03:00
|
|
|
/*
|
|
|
|
* Connection.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
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2008-07-03 18:03:32 +03:00
|
|
|
using namespace boost;
|
|
|
|
using namespace boost::asio::ip;
|
2011-12-14 00:23:17 +03:00
|
|
|
template<typename Serializer> DLL_LINKAGE void registerTypes(Serializer &s); //defined elsewhere and explicitly instantiated for used serializers
|
2008-07-03 18:03:32 +03:00
|
|
|
|
2009-03-07 00:11:17 +02:00
|
|
|
CTypeList typeList;
|
|
|
|
|
2008-07-03 18:03:32 +03:00
|
|
|
#define LOG(a) \
|
|
|
|
if(logging)\
|
|
|
|
out << a
|
|
|
|
#if defined(__hppa__) || \
|
|
|
|
defined(__m68k__) || defined(mc68000) || defined(_M_M68K) || \
|
|
|
|
(defined(__MIPS__) && defined(__MISPEB__)) || \
|
|
|
|
defined(__ppc__) || defined(__POWERPC__) || defined(_M_PPC) || \
|
|
|
|
defined(__sparc__)
|
|
|
|
#define BIG_ENDIAN
|
|
|
|
#else
|
|
|
|
#define LIL_ENDIAN
|
|
|
|
#endif
|
|
|
|
|
2008-07-09 20:22:28 +03:00
|
|
|
void CConnection::init()
|
2008-07-03 18:03:32 +03:00
|
|
|
{
|
2012-09-25 21:00:55 +03:00
|
|
|
enableSmartPointerSerializatoin();
|
|
|
|
disableStackSendingByID();
|
2009-03-07 17:54:12 +02:00
|
|
|
registerTypes(static_cast<CISer<CConnection>&>(*this));
|
|
|
|
registerTypes(static_cast<COSer<CConnection>&>(*this));
|
2008-07-03 18:03:32 +03:00
|
|
|
#ifdef LIL_ENDIAN
|
|
|
|
myEndianess = true;
|
|
|
|
#else
|
|
|
|
myEndianess = false;
|
|
|
|
#endif
|
2008-07-09 20:22:28 +03:00
|
|
|
connected = true;
|
|
|
|
std::string pom;
|
|
|
|
//we got connection
|
|
|
|
(*this) << std::string("Aiya!\n") << name << myEndianess; //identify ourselves
|
|
|
|
(*this) >> pom >> pom >> contactEndianess;
|
2009-01-11 00:08:18 +02:00
|
|
|
tlog0 << "Established connection with "<<pom<<std::endl;
|
2008-07-26 16:57:32 +03:00
|
|
|
wmx = new boost::mutex;
|
|
|
|
rmx = new boost::mutex;
|
2010-10-24 14:35:14 +03:00
|
|
|
|
|
|
|
handler = NULL;
|
|
|
|
receivedStop = sendStop = false;
|
|
|
|
static int cid = 1;
|
|
|
|
connectionID = cid++;
|
2008-07-09 20:22:28 +03:00
|
|
|
}
|
|
|
|
|
2009-01-11 00:08:18 +02:00
|
|
|
CConnection::CConnection(std::string host, std::string port, std::string Name)
|
|
|
|
:io_service(new asio::io_service), name(Name)
|
2008-07-09 20:22:28 +03:00
|
|
|
{
|
2008-09-10 00:10:24 +03:00
|
|
|
int i;
|
2008-08-04 12:05:52 +03:00
|
|
|
boost::system::error_code error = asio::error::host_not_found;
|
2008-07-03 18:03:32 +03:00
|
|
|
socket = new tcp::socket(*io_service);
|
|
|
|
tcp::resolver resolver(*io_service);
|
2008-09-07 06:38:37 +03:00
|
|
|
tcp::resolver::iterator end, pom, endpoint_iterator = resolver.resolve(tcp::resolver::query(host,port),error);
|
|
|
|
if(error)
|
|
|
|
{
|
2008-09-19 11:16:19 +03:00
|
|
|
tlog1 << "Problem with resolving: " << std::endl << error <<std::endl;
|
2008-09-07 06:38:37 +03:00
|
|
|
goto connerror1;
|
|
|
|
}
|
|
|
|
pom = endpoint_iterator;
|
|
|
|
if(pom != end)
|
2008-09-19 11:16:19 +03:00
|
|
|
tlog0<<"Found endpoints:" << std::endl;
|
2008-09-07 06:38:37 +03:00
|
|
|
else
|
|
|
|
{
|
2008-09-19 11:16:19 +03:00
|
|
|
tlog1 << "Critical problem: No endpoints found!" << std::endl;
|
2008-09-07 06:38:37 +03:00
|
|
|
goto connerror1;
|
|
|
|
}
|
2008-09-10 00:10:24 +03:00
|
|
|
i=0;
|
2008-09-07 06:38:37 +03:00
|
|
|
while(pom != end)
|
|
|
|
{
|
2008-09-19 11:16:19 +03:00
|
|
|
tlog0 << "\t" << i << ": " << (boost::asio::ip::tcp::endpoint&)*pom << std::endl;
|
2008-09-07 06:38:37 +03:00
|
|
|
pom++;
|
|
|
|
}
|
2008-09-09 10:05:02 +03:00
|
|
|
i=0;
|
2008-09-07 06:38:37 +03:00
|
|
|
while(endpoint_iterator != end)
|
|
|
|
{
|
2008-09-19 11:16:19 +03:00
|
|
|
tlog0 << "Trying connection to " << (boost::asio::ip::tcp::endpoint&)*endpoint_iterator << " (" << i++ << ")" << std::endl;
|
2008-09-07 06:38:37 +03:00
|
|
|
socket->connect(*endpoint_iterator, error);
|
|
|
|
if(!error)
|
|
|
|
{
|
|
|
|
init();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-09-19 11:16:19 +03:00
|
|
|
tlog1 << "Problem with connecting: " << std::endl << error << std::endl;
|
2008-09-07 06:38:37 +03:00
|
|
|
}
|
2008-09-09 10:05:02 +03:00
|
|
|
endpoint_iterator++;
|
2008-09-07 06:38:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//we shouldn't be here - error handling
|
|
|
|
connerror1:
|
2008-09-19 11:16:19 +03:00
|
|
|
tlog1 << "Something went wrong... checking for error info" << std::endl;
|
2008-09-07 06:38:37 +03:00
|
|
|
if(error)
|
2008-09-19 15:09:15 +03:00
|
|
|
tlog1 << error <<std::endl;
|
2008-09-07 06:38:37 +03:00
|
|
|
else
|
2008-09-19 11:16:19 +03:00
|
|
|
tlog1 << "No error info. " << std::endl;
|
2008-09-07 06:38:37 +03:00
|
|
|
delete io_service;
|
2008-09-09 10:05:02 +03:00
|
|
|
//delete socket;
|
2012-04-22 10:32:45 +03:00
|
|
|
throw std::runtime_error("Can't establish connection :(");
|
2008-07-03 18:03:32 +03:00
|
|
|
}
|
2010-05-15 05:26:49 +03:00
|
|
|
CConnection::CConnection(TSocket * Socket, std::string Name )
|
2011-08-05 02:49:32 +03:00
|
|
|
:socket(Socket),io_service(&Socket->get_io_service()), name(Name)//, send(this), rec(this)
|
2008-07-03 18:03:32 +03:00
|
|
|
{
|
2008-07-09 20:22:28 +03:00
|
|
|
init();
|
|
|
|
}
|
2010-05-15 05:26:49 +03:00
|
|
|
CConnection::CConnection(TAcceptor * acceptor, boost::asio::io_service *Io_service, std::string Name)
|
2009-01-11 00:08:18 +02:00
|
|
|
: name(Name)//, send(this), rec(this)
|
2008-07-09 20:22:28 +03:00
|
|
|
{
|
2008-08-04 12:05:52 +03:00
|
|
|
boost::system::error_code error = asio::error::host_not_found;
|
2008-07-09 20:22:28 +03:00
|
|
|
socket = new tcp::socket(*io_service);
|
|
|
|
acceptor->accept(*socket,error);
|
2008-09-09 10:05:02 +03:00
|
|
|
if (error)
|
|
|
|
{
|
2008-09-19 15:09:15 +03:00
|
|
|
tlog1 << "Error on accepting: " << std::endl << error << std::endl;
|
2008-09-09 10:05:02 +03:00
|
|
|
delete socket;
|
2012-04-22 10:32:45 +03:00
|
|
|
throw std::runtime_error("Can't establish connection :(");
|
2008-09-09 10:05:02 +03:00
|
|
|
}
|
2008-07-09 20:22:28 +03:00
|
|
|
init();
|
2008-07-03 18:03:32 +03:00
|
|
|
}
|
|
|
|
int CConnection::write(const void * data, unsigned size)
|
|
|
|
{
|
2008-09-17 13:18:22 +03:00
|
|
|
//LOG("Sending " << size << " byte(s) of data" <<std::endl);
|
2010-01-29 22:52:45 +02:00
|
|
|
try
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
ret = asio::write(*socket,asio::const_buffers_1(asio::const_buffer(data,size)));
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
catch(...)
|
|
|
|
{
|
|
|
|
//connection has been lost
|
|
|
|
connected = false;
|
|
|
|
throw;
|
|
|
|
}
|
2008-07-03 18:03:32 +03:00
|
|
|
}
|
|
|
|
int CConnection::read(void * data, unsigned size)
|
|
|
|
{
|
2008-09-17 13:18:22 +03:00
|
|
|
//LOG("Receiving " << size << " byte(s) of data" <<std::endl);
|
2010-01-29 22:52:45 +02:00
|
|
|
try
|
|
|
|
{
|
|
|
|
int ret = asio::read(*socket,asio::mutable_buffers_1(asio::mutable_buffer(data,size)));
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
catch(...)
|
|
|
|
{
|
|
|
|
//connection has been lost
|
|
|
|
connected = false;
|
|
|
|
throw;
|
|
|
|
}
|
2008-07-03 18:03:32 +03:00
|
|
|
}
|
|
|
|
CConnection::~CConnection(void)
|
|
|
|
{
|
2010-10-24 14:35:14 +03:00
|
|
|
if(handler)
|
|
|
|
handler->join();
|
|
|
|
|
|
|
|
delete handler;
|
|
|
|
|
2008-08-25 13:25:16 +03:00
|
|
|
close();
|
2010-01-29 22:52:45 +02:00
|
|
|
delete io_service;
|
|
|
|
delete wmx;
|
|
|
|
delete rmx;
|
2008-07-03 18:03:32 +03:00
|
|
|
}
|
2008-08-04 12:05:52 +03:00
|
|
|
|
2009-03-16 00:57:04 +02:00
|
|
|
template<class T>
|
|
|
|
CConnection & CConnection::operator&(const T &t) {
|
2012-11-20 20:53:45 +03:00
|
|
|
throw std::exception();
|
2009-03-16 00:57:04 +02:00
|
|
|
//XXX this is temporaly ? solution to fix gcc (4.3.3, other?) compilation
|
|
|
|
// problem for more details contact t0@czlug.icis.pcz.pl or impono@gmail.com
|
|
|
|
// do not remove this exception it shoudnt be called
|
2012-11-20 20:53:45 +03:00
|
|
|
return *this;
|
2009-03-16 00:57:04 +02:00
|
|
|
}
|
|
|
|
|
2008-08-25 13:25:16 +03:00
|
|
|
void CConnection::close()
|
|
|
|
{
|
|
|
|
if(socket)
|
|
|
|
{
|
|
|
|
socket->close();
|
|
|
|
delete socket;
|
|
|
|
socket = NULL;
|
|
|
|
}
|
|
|
|
}
|
2008-11-16 03:06:15 +02:00
|
|
|
|
2010-01-29 22:52:45 +02:00
|
|
|
bool CConnection::isOpen() const
|
|
|
|
{
|
|
|
|
return socket && connected;
|
|
|
|
}
|
|
|
|
|
2010-07-14 05:53:21 +03:00
|
|
|
void CConnection::reportState(CLogger &out)
|
|
|
|
{
|
|
|
|
out << "CConnection\n";
|
|
|
|
if(socket && socket->is_open())
|
|
|
|
{
|
|
|
|
out << "\tWe have an open and valid socket\n";
|
|
|
|
out << "\t" << socket->available() <<" bytes awaiting\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-03 21:42:54 +03:00
|
|
|
CPack * CConnection::retreivePack()
|
|
|
|
{
|
|
|
|
CPack *ret = NULL;
|
|
|
|
boost::unique_lock<boost::mutex> lock(*rmx);
|
|
|
|
tlog5 << "Listening... ";
|
|
|
|
*this >> ret;
|
|
|
|
tlog5 << "\treceived server message of type " << typeid(*ret).name() << std::endl;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-03-26 01:46:14 +03:00
|
|
|
void CConnection::sendPackToServer(const CPack &pack, ui8 player, ui32 requestID)
|
2011-08-25 18:24:37 +03:00
|
|
|
{
|
|
|
|
boost::unique_lock<boost::mutex> lock(*wmx);
|
2012-01-03 04:55:26 +03:00
|
|
|
tlog5 << "Sending to server a pack of type " << typeid(pack).name() << std::endl;
|
2012-03-26 01:46:14 +03:00
|
|
|
*this << player << requestID << &pack; //packs has to be sent as polymorphic pointers!
|
2011-08-25 18:24:37 +03:00
|
|
|
}
|
|
|
|
|
2012-09-25 21:00:55 +03:00
|
|
|
void CConnection::disableStackSendingByID()
|
|
|
|
{
|
|
|
|
CISer<CConnection>::sendStackInstanceByIds = false;
|
|
|
|
COSer<CConnection>::sendStackInstanceByIds = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CConnection::enableStackSendingByID()
|
|
|
|
{
|
|
|
|
CISer<CConnection>::sendStackInstanceByIds = true;
|
|
|
|
COSer<CConnection>::sendStackInstanceByIds = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CConnection::disableSmartPointerSerialization()
|
|
|
|
{
|
|
|
|
CISer<CConnection>::smartPointerSerialization = false;
|
|
|
|
COSer<CConnection>::smartPointerSerialization = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CConnection::enableSmartPointerSerializatoin()
|
|
|
|
{
|
|
|
|
CISer<CConnection>::smartPointerSerialization = true;
|
|
|
|
COSer<CConnection>::smartPointerSerialization = true;
|
|
|
|
}
|
|
|
|
|
2008-11-16 03:06:15 +02:00
|
|
|
CSaveFile::CSaveFile( const std::string &fname )
|
2008-08-04 12:05:52 +03:00
|
|
|
{
|
2009-03-07 00:11:17 +02:00
|
|
|
registerTypes(*this);
|
2010-02-26 13:18:09 +02:00
|
|
|
openNextFile(fname);
|
|
|
|
}
|
|
|
|
|
|
|
|
CSaveFile::~CSaveFile()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
int CSaveFile::write( const void * data, unsigned size )
|
|
|
|
{
|
|
|
|
sfile->write((char *)data,size);
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSaveFile::openNextFile(const std::string &fname)
|
|
|
|
{
|
2010-07-14 05:53:21 +03:00
|
|
|
fName = fname;
|
2012-06-09 22:58:17 +03:00
|
|
|
try
|
2009-06-02 01:31:11 +03:00
|
|
|
{
|
2012-06-09 22:58:17 +03:00
|
|
|
sfile = make_unique<std::ofstream>(fname.c_str(), std::ios::binary);
|
|
|
|
sfile->exceptions(std::ifstream::failbit | std::ifstream::badbit); //we throw a lot anyway
|
|
|
|
|
|
|
|
if(!(*sfile))
|
|
|
|
THROW_FORMAT("Error: cannot open to write %s!", fname);
|
|
|
|
|
2009-06-02 01:31:11 +03:00
|
|
|
sfile->write("VCMI",4); //write magic identifier
|
|
|
|
*this << version; //write format version
|
|
|
|
}
|
2012-06-09 22:58:17 +03:00
|
|
|
catch(...)
|
|
|
|
{
|
|
|
|
tlog1 << "Failed to save to " << fname << std::endl;
|
|
|
|
clear();
|
|
|
|
throw;
|
|
|
|
}
|
2008-08-04 12:05:52 +03:00
|
|
|
}
|
|
|
|
|
2010-07-14 05:53:21 +03:00
|
|
|
void CSaveFile::reportState(CLogger &out)
|
|
|
|
{
|
|
|
|
out << "CSaveFile" << std::endl;
|
2012-04-08 04:15:18 +03:00
|
|
|
if(sfile.get() && *sfile)
|
2010-07-14 05:53:21 +03:00
|
|
|
{
|
|
|
|
out << "\tOpened " << fName << "\n\tPosition: " << sfile->tellp() << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-09 22:58:17 +03:00
|
|
|
void CSaveFile::clear()
|
|
|
|
{
|
|
|
|
fName.clear();
|
|
|
|
sfile = nullptr;
|
|
|
|
}
|
|
|
|
|
2010-10-24 14:35:14 +03:00
|
|
|
CLoadFile::CLoadFile(const std::string &fname, int minimalVersion /*= version*/)
|
2010-02-26 13:18:09 +02:00
|
|
|
{
|
|
|
|
registerTypes(*this);
|
2010-10-24 14:35:14 +03:00
|
|
|
openNextFile(fname, minimalVersion);
|
2010-02-26 13:18:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CLoadFile::~CLoadFile()
|
2008-08-04 12:05:52 +03:00
|
|
|
{
|
|
|
|
}
|
2008-11-16 03:06:15 +02:00
|
|
|
|
2010-02-26 13:18:09 +02:00
|
|
|
int CLoadFile::read( const void * data, unsigned size )
|
2008-11-16 03:06:15 +02:00
|
|
|
{
|
2012-06-08 03:12:31 +03:00
|
|
|
sfile->read((char *)data,size);
|
2008-11-16 03:06:15 +02:00
|
|
|
return size;
|
2009-01-06 20:42:20 +02:00
|
|
|
}
|
|
|
|
|
2010-10-24 14:35:14 +03:00
|
|
|
void CLoadFile::openNextFile(const std::string &fname, int minimalVersion)
|
2010-02-26 13:18:09 +02:00
|
|
|
{
|
2012-06-08 03:12:31 +03:00
|
|
|
assert(!reverseEndianess);
|
|
|
|
assert(minimalVersion <= version);
|
|
|
|
|
|
|
|
try
|
2009-06-02 01:31:11 +03:00
|
|
|
{
|
2012-06-08 03:12:31 +03:00
|
|
|
fName = fname;
|
|
|
|
sfile = make_unique<std::ifstream>(fname, std::ios::binary);
|
|
|
|
sfile->exceptions(std::ifstream::failbit | std::ifstream::badbit); //we throw a lot anyway
|
|
|
|
|
|
|
|
if(!(*sfile))
|
|
|
|
THROW_FORMAT("Error: cannot open to read %s!", fname);
|
|
|
|
|
|
|
|
//we can read
|
2009-06-02 01:31:11 +03:00
|
|
|
char buffer[4];
|
|
|
|
sfile->read(buffer, 4);
|
|
|
|
if(std::memcmp(buffer,"VCMI",4))
|
2012-06-08 03:12:31 +03:00
|
|
|
THROW_FORMAT("Error: not a VCMI file(%s)!", fname);
|
2009-06-02 01:31:11 +03:00
|
|
|
|
2012-06-08 03:12:31 +03:00
|
|
|
*this >> fileVersion;
|
|
|
|
if(fileVersion < minimalVersion)
|
|
|
|
THROW_FORMAT("Error: too old file format (%s)!", fname);
|
|
|
|
|
|
|
|
if(fileVersion > version)
|
2012-04-17 15:46:21 +03:00
|
|
|
{
|
2012-06-08 03:12:31 +03:00
|
|
|
tlog3 << boost::format("Warning format version mismatch: found %d when current is %d! (file %s)\n") % fileVersion % version % fname;
|
|
|
|
|
|
|
|
auto versionptr = (char*)&fileVersion;
|
2012-06-01 18:59:26 +03:00
|
|
|
std::reverse(versionptr, versionptr + 4);
|
2012-06-08 03:12:31 +03:00
|
|
|
tlog3 << "Version number reversed is " << fileVersion << ", checking...\n";
|
|
|
|
|
|
|
|
if(fileVersion == version)
|
2012-06-01 18:59:26 +03:00
|
|
|
{
|
2012-06-08 03:12:31 +03:00
|
|
|
tlog3 << fname << " seems to have different endianess! Entering reversing mode.\n";
|
2012-06-01 18:59:26 +03:00
|
|
|
reverseEndianess = true;
|
|
|
|
}
|
|
|
|
else
|
2012-06-08 03:12:31 +03:00
|
|
|
THROW_FORMAT("Error: too new file format (%s)!", fname);
|
2009-06-02 01:31:11 +03:00
|
|
|
}
|
|
|
|
}
|
2012-06-08 03:12:31 +03:00
|
|
|
catch(...)
|
|
|
|
{
|
|
|
|
clear(); //if anything went wrong, we delete file and rethrow
|
|
|
|
throw;
|
|
|
|
}
|
2009-01-06 20:42:20 +02:00
|
|
|
}
|
|
|
|
|
2010-07-14 05:53:21 +03:00
|
|
|
void CLoadFile::reportState(CLogger &out)
|
|
|
|
{
|
|
|
|
out << "CLoadFile" << std::endl;
|
2012-04-08 04:15:18 +03:00
|
|
|
if(!!sfile && *sfile)
|
2010-07-14 05:53:21 +03:00
|
|
|
{
|
|
|
|
out << "\tOpened " << fName << "\n\tPosition: " << sfile->tellg() << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-08 03:12:31 +03:00
|
|
|
void CLoadFile::clear()
|
|
|
|
{
|
|
|
|
sfile = nullptr;
|
|
|
|
fName.clear();
|
|
|
|
fileVersion = 0;
|
|
|
|
}
|
|
|
|
|
2009-03-07 00:11:17 +02:00
|
|
|
CTypeList::CTypeList()
|
|
|
|
{
|
2009-03-12 01:25:59 +02:00
|
|
|
registerTypes(*this);
|
2009-03-07 00:11:17 +02:00
|
|
|
}
|
|
|
|
|
2009-03-14 20:19:07 +02:00
|
|
|
ui16 CTypeList::registerType( const std::type_info *type )
|
2009-03-07 00:11:17 +02:00
|
|
|
{
|
2009-03-07 17:54:12 +02:00
|
|
|
TTypeMap::const_iterator i = types.find(type);
|
2009-03-07 00:11:17 +02:00
|
|
|
if(i != types.end())
|
2009-03-07 17:54:12 +02:00
|
|
|
return i->second; //type found, return ID
|
2009-03-07 00:11:17 +02:00
|
|
|
|
2009-03-07 17:54:12 +02:00
|
|
|
//type not found - add it to the list and return given ID
|
2009-03-07 00:11:17 +02:00
|
|
|
ui16 id = types.size() + 1;
|
2009-03-07 17:54:12 +02:00
|
|
|
types.insert(std::make_pair(type,id));
|
2009-03-07 00:11:17 +02:00
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
2009-03-14 20:19:07 +02:00
|
|
|
ui16 CTypeList::getTypeID( const std::type_info *type )
|
2009-03-07 00:11:17 +02:00
|
|
|
{
|
2009-03-07 17:54:12 +02:00
|
|
|
TTypeMap::const_iterator i = types.find(type);
|
|
|
|
if(i != types.end())
|
|
|
|
return i->second;
|
2009-03-07 00:11:17 +02:00
|
|
|
else
|
|
|
|
return 0;
|
2009-10-04 05:02:45 +03:00
|
|
|
}
|
2010-06-26 19:02:10 +03:00
|
|
|
|
2010-10-24 14:35:14 +03:00
|
|
|
std::ostream & operator<<(std::ostream &str, const CConnection &cpc)
|
|
|
|
{
|
|
|
|
return str << "Connection with " << cpc.name << " (ID: " << cpc.connectionID << /*", " << (cpc.host ? "host" : "guest") <<*/ ")";
|
|
|
|
}
|
|
|
|
|
2010-06-26 19:02:10 +03:00
|
|
|
CSerializer::~CSerializer()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
CSerializer::CSerializer()
|
|
|
|
{
|
|
|
|
smartVectorMembersSerialization = false;
|
2012-04-14 05:20:22 +03:00
|
|
|
sendStackInstanceByIds = false;
|
2010-06-26 19:02:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CSerializer::addStdVecItems(CGameState *gs, LibClasses *lib)
|
|
|
|
{
|
|
|
|
registerVectoredType(&gs->map->objects, &CGObjectInstance::id);
|
2010-11-10 02:06:25 +02:00
|
|
|
registerVectoredType(&lib->heroh->heroes, &CHero::ID);
|
2010-06-26 19:02:10 +03:00
|
|
|
registerVectoredType(&lib->creh->creatures, &CCreature::idNumber);
|
|
|
|
registerVectoredType(&lib->arth->artifacts, &CArtifact::id);
|
2010-12-17 20:47:07 +02:00
|
|
|
registerVectoredType(&gs->map->artInstances, &CArtifactInstance::id);
|
2012-07-08 11:42:03 +03:00
|
|
|
registerVectoredType(&gs->map->quests, &CQuest::qid);
|
2010-06-26 19:02:10 +03:00
|
|
|
smartVectorMembersSerialization = true;
|
2010-08-21 03:37:19 +03:00
|
|
|
}
|