1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-24 22:14:36 +02:00

macOS: fix packet deserialization (#479)

This commit is contained in:
Konstantin Vukolov 2018-08-14 00:48:00 +03:00 committed by Arseniy Shestakov
parent de5f1461ba
commit 96215233bc
9 changed files with 72 additions and 30 deletions

View File

@ -43,6 +43,7 @@
#include <boost/uuid/uuid.hpp> #include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_io.hpp> #include <boost/uuid/uuid_io.hpp>
#include <boost/uuid/uuid_generators.hpp> #include <boost/uuid/uuid_generators.hpp>
#include "../lib/serializer/Cast.h"
template<typename T> class CApplyOnLobby; template<typename T> class CApplyOnLobby;
@ -613,7 +614,7 @@ void CServerHandler::threadHandleConnection()
// Though currently they'll be delivered and might cause crash. // Though currently they'll be delivered and might cause crash.
vstd::clear_pointer(pack); vstd::clear_pointer(pack);
} }
else if(auto lobbyPack = dynamic_cast<CPackForLobby *>(pack)) else if(auto lobbyPack = dynamic_ptr_cast<CPackForLobby>(pack))
{ {
if(applier->getApplier(typeList.getTypeID(pack))->applyOnLobbyHandler(this, pack)) if(applier->getApplier(typeList.getTypeID(pack))->applyOnLobbyHandler(this, pack))
{ {
@ -624,7 +625,7 @@ void CServerHandler::threadHandleConnection()
} }
} }
} }
else if(auto clientPack = dynamic_cast<CPackForClient *>(pack)) else if(auto clientPack = dynamic_ptr_cast<CPackForClient>(pack))
{ {
client->handlePack(clientPack); client->handlePack(clientPack);
} }

View File

@ -267,6 +267,7 @@ set(lib_HEADERS
serializer/JsonDeserializer.h serializer/JsonDeserializer.h
serializer/JsonSerializeFormat.h serializer/JsonSerializeFormat.h
serializer/JsonSerializer.h serializer/JsonSerializer.h
serializer/Cast.h
spells/AbilityCaster.h spells/AbilityCaster.h
spells/AdventureSpellMechanics.h spells/AdventureSpellMechanics.h

View File

@ -99,7 +99,9 @@ public:
else else
{ {
assert(!i->second.empty()); assert(!i->second.empty());
#ifndef __APPLE__
assert(i->second.type() == typeid(VectorizedObjectInfo<T, U>)); assert(i->second.type() == typeid(VectorizedObjectInfo<T, U>));
#endif
VectorizedObjectInfo<T, U> *ret = &(boost::any_cast<VectorizedObjectInfo<T, U>&>(i->second)); VectorizedObjectInfo<T, U> *ret = &(boost::any_cast<VectorizedObjectInfo<T, U>&>(i->second));
return ret; return ret;
} }

62
lib/serializer/Cast.h Normal file
View File

@ -0,0 +1,62 @@
/*
* Cast.h, 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
*
*/
#pragma once
#include <typeinfo>
#include <string>
#include "CTypeList.h"
template<class T, class F>
inline const T * dynamic_ptr_cast(const F * ptr)
{
#ifndef VCMI_APPLE
return dynamic_cast<const T *>(ptr);
#else
if(!strcmp(typeid(*ptr).name(), typeid(T).name()))
{
return static_cast<const T *>(ptr);
}
try
{
auto * sourceTypeInfo = typeList.getTypeInfo(ptr);
auto * targetTypeInfo = &typeid(typename std::remove_const<typename std::remove_pointer<T>::type>::type);
typeList.castRaw((void *)ptr, sourceTypeInfo, targetTypeInfo);
}
catch(...)
{
return nullptr;
}
return static_cast<const T *>(ptr);
#endif
}
template<class T, class F>
inline T * dynamic_ptr_cast(F * ptr)
{
#ifndef VCMI_APPLE
return dynamic_cast<T *>(ptr);
#else
if(!strcmp(typeid(*ptr).name(), typeid(T).name()))
{
return static_cast<T *>(ptr);
}
try
{
auto * sourceTypeInfo = typeList.getTypeInfo(ptr);
auto * targetTypeInfo = &typeid(typename std::remove_const<typename std::remove_pointer<T>::type>::type);
typeList.castRaw((void *)ptr, sourceTypeInfo, targetTypeInfo);
}
catch(...)
{
return nullptr;
}
return static_cast<T *>(ptr);
#endif
}

View File

@ -46,6 +46,7 @@
#include "../lib/registerTypes/RegisterTypes.h" #include "../lib/registerTypes/RegisterTypes.h"
#include "../lib/serializer/CTypeList.h" #include "../lib/serializer/CTypeList.h"
#include "../lib/serializer/Connection.h" #include "../lib/serializer/Connection.h"
#include "../lib/serializer/Cast.h"
#ifndef _MSC_VER #ifndef _MSC_VER
#include <boost/thread/xtime.hpp> #include <boost/thread/xtime.hpp>

View File

@ -12,6 +12,7 @@
#include "CGameHandler.h" #include "CGameHandler.h"
#include "../lib/battle/BattleInfo.h" #include "../lib/battle/BattleInfo.h"
#include "../lib/mapObjects/MiscObjects.h" #include "../lib/mapObjects/MiscObjects.h"
#include "../lib/serializer/Cast.h"
boost::mutex Queries::mx; boost::mutex Queries::mx;

View File

@ -8,7 +8,6 @@
* *
*/ */
#include "StdInc.h" #include "StdInc.h"
#include <boost/asio.hpp> #include <boost/asio.hpp>
#include "../lib/filesystem/Filesystem.h" #include "../lib/filesystem/Filesystem.h"
@ -42,6 +41,7 @@
#include "../lib/CConfigHandler.h" #include "../lib/CConfigHandler.h"
#include "../lib/ScopeGuard.h" #include "../lib/ScopeGuard.h"
#include "../lib/serializer/CMemorySerializer.h" #include "../lib/serializer/CMemorySerializer.h"
#include "../lib/serializer/Cast.h"
#include "../lib/UnlockGuard.h" #include "../lib/UnlockGuard.h"

View File

@ -20,6 +20,7 @@
#include "../lib/serializer/Connection.h" #include "../lib/serializer/Connection.h"
#include "../lib/spells/CSpellHandler.h" #include "../lib/spells/CSpellHandler.h"
#include "../lib/spells/ISpellMechanics.h" #include "../lib/spells/ISpellMechanics.h"
#include "../lib/serializer/Cast.h"
bool CPackForServer::isPlayerOwns(CGameHandler * gh, ObjectInstanceID id) bool CPackForServer::isPlayerOwns(CGameHandler * gh, ObjectInstanceID id)
{ {

View File

@ -18,30 +18,3 @@
#include <boost/random/variate_generator.hpp> #include <boost/random/variate_generator.hpp>
#include <boost/system/system_error.hpp> #include <boost/system/system_error.hpp>
template<class T, class F>
inline const T * dynamic_ptr_cast(const F * ptr)
{
#ifndef __APPLE__
return dynamic_cast<const T*>(ptr);
#else
if (!strcmp(typeid(*ptr).name(), typeid(T).name()))
{
return static_cast<const T*>(ptr);
}
return nullptr;
#endif
}
template<class T, class F>
inline T * dynamic_ptr_cast(F * ptr)
{
#ifndef __APPLE__
return dynamic_cast<T*>(ptr);
#else
if (!strcmp(typeid(*ptr).name(), typeid(T).name()))
{
return static_cast<T*>(ptr);
}
return nullptr;
#endif
}