From 5c810df36f00755ec6c4985d1993c3cadc205bb2 Mon Sep 17 00:00:00 2001 From: Ivan Savenko Date: Sat, 11 Nov 2023 00:39:08 +0200 Subject: [PATCH] Reorganized types registration code --- client/CServerHandler.cpp | 2 +- client/Client.cpp | 6 +- client/windows/CMapOverview.cpp | 2 +- cmake_modules/VCMI_lib.cmake | 17 +- lib/IGameCallback.cpp | 5 +- lib/gameState/CGameState.cpp | 5 +- lib/mapping/CMapInfo.cpp | 2 +- lib/registerTypes/RegisterTypes.cpp | 51 --- lib/registerTypes/RegisterTypes.h | 343 +------------------ lib/registerTypes/RegisterTypesClientPacks.h | 125 +++++++ lib/registerTypes/RegisterTypesLobbyPacks.h | 62 ++++ lib/registerTypes/RegisterTypesMapObjects.h | 137 ++++++++ lib/registerTypes/RegisterTypesServerPacks.h | 56 +++ lib/registerTypes/TypesClientPacks1.cpp | 33 -- lib/registerTypes/TypesClientPacks2.cpp | 37 -- lib/registerTypes/TypesLobbyPacks.cpp | 43 --- lib/registerTypes/TypesMapObjects1.cpp | 34 -- lib/registerTypes/TypesMapObjects2.cpp | 36 -- lib/registerTypes/TypesMapObjects3.cpp | 32 -- lib/registerTypes/TypesServerPacks.cpp | 32 -- lib/serializer/BinaryDeserializer.cpp | 91 +---- lib/serializer/BinaryDeserializer.h | 40 +-- lib/serializer/BinarySerializer.cpp | 62 +--- lib/serializer/BinarySerializer.h | 32 +- lib/serializer/CLoadFile.cpp | 100 ++++++ lib/serializer/CLoadFile.h | 42 +++ lib/serializer/CMemorySerializer.cpp | 4 - lib/serializer/CSaveFile.cpp | 72 ++++ lib/serializer/CSaveFile.h | 42 +++ lib/serializer/Connection.cpp | 5 +- server/CGameHandler.cpp | 10 +- server/CVCMIServer.cpp | 2 +- 32 files changed, 681 insertions(+), 881 deletions(-) delete mode 100644 lib/registerTypes/RegisterTypes.cpp create mode 100644 lib/registerTypes/RegisterTypesClientPacks.h create mode 100644 lib/registerTypes/RegisterTypesLobbyPacks.h create mode 100644 lib/registerTypes/RegisterTypesMapObjects.h create mode 100644 lib/registerTypes/RegisterTypesServerPacks.h delete mode 100644 lib/registerTypes/TypesClientPacks1.cpp delete mode 100644 lib/registerTypes/TypesClientPacks2.cpp delete mode 100644 lib/registerTypes/TypesLobbyPacks.cpp delete mode 100644 lib/registerTypes/TypesMapObjects1.cpp delete mode 100644 lib/registerTypes/TypesMapObjects2.cpp delete mode 100644 lib/registerTypes/TypesMapObjects3.cpp delete mode 100644 lib/registerTypes/TypesServerPacks.cpp create mode 100644 lib/serializer/CLoadFile.cpp create mode 100644 lib/serializer/CLoadFile.h create mode 100644 lib/serializer/CSaveFile.cpp create mode 100644 lib/serializer/CSaveFile.h diff --git a/client/CServerHandler.cpp b/client/CServerHandler.cpp index d0930fe15..361382a17 100644 --- a/client/CServerHandler.cpp +++ b/client/CServerHandler.cpp @@ -47,7 +47,7 @@ #include "../lib/modding/ModIncompatibility.h" #include "../lib/rmg/CMapGenOptions.h" #include "../lib/filesystem/Filesystem.h" -#include "../lib/registerTypes/RegisterTypes.h" +#include "../lib/registerTypes/RegisterTypesLobbyPacks.h" #include "../lib/serializer/Connection.h" #include "../lib/serializer/CMemorySerializer.h" diff --git a/client/Client.cpp b/client/Client.cpp index 280f4daf1..e9238b11e 100644 --- a/client/Client.cpp +++ b/client/Client.cpp @@ -24,6 +24,7 @@ #include "../CCallback.h" #include "../lib/CConfigHandler.h" #include "../lib/gameState/CGameState.h" +#include "../lib/CPlayerState.h" #include "../lib/CThreadHelper.h" #include "../lib/VCMIDirs.h" #include "../lib/UnlockGuard.h" @@ -32,7 +33,7 @@ #include "../lib/mapping/CMapService.h" #include "../lib/pathfinder/CGPathNode.h" #include "../lib/filesystem/Filesystem.h" -#include "../lib/registerTypes/RegisterTypes.h" +#include "../lib/registerTypes/RegisterTypesClientPacks.h" #include "../lib/serializer/Connection.h" #include @@ -137,8 +138,7 @@ CClient::CClient() { waitingRequest.clear(); applier = std::make_shared>(); - registerTypesClientPacks1(*applier); - registerTypesClientPacks2(*applier); + registerTypesClientPacks(*applier); IObjectInterface::cb = this; gs = nullptr; } diff --git a/client/windows/CMapOverview.cpp b/client/windows/CMapOverview.cpp index 335839991..b78d07b84 100644 --- a/client/windows/CMapOverview.cpp +++ b/client/windows/CMapOverview.cpp @@ -39,7 +39,7 @@ #include "../../lib/TerrainHandler.h" #include "../../lib/filesystem/Filesystem.h" -#include "../../lib/serializer/BinaryDeserializer.h" +#include "../../lib/serializer/CLoadFile.h" #include "../../lib/StartInfo.h" #include "../../lib/rmg/CMapGenOptions.h" diff --git a/cmake_modules/VCMI_lib.cmake b/cmake_modules/VCMI_lib.cmake index 3e7663c2b..93bf03483 100644 --- a/cmake_modules/VCMI_lib.cmake +++ b/cmake_modules/VCMI_lib.cmake @@ -133,15 +133,6 @@ macro(add_main_lib TARGET_NAME LIBRARY_TYPE) ${MAIN_LIB_DIR}/pathfinder/PathfindingRules.cpp ${MAIN_LIB_DIR}/pathfinder/TurnInfo.cpp - ${MAIN_LIB_DIR}/registerTypes/RegisterTypes.cpp - ${MAIN_LIB_DIR}/registerTypes/TypesClientPacks1.cpp - ${MAIN_LIB_DIR}/registerTypes/TypesClientPacks2.cpp - ${MAIN_LIB_DIR}/registerTypes/TypesMapObjects1.cpp - ${MAIN_LIB_DIR}/registerTypes/TypesMapObjects2.cpp - ${MAIN_LIB_DIR}/registerTypes/TypesMapObjects3.cpp - ${MAIN_LIB_DIR}/registerTypes/TypesLobbyPacks.cpp - ${MAIN_LIB_DIR}/registerTypes/TypesServerPacks.cpp - ${MAIN_LIB_DIR}/rewardable/Configuration.cpp ${MAIN_LIB_DIR}/rewardable/Info.cpp ${MAIN_LIB_DIR}/rewardable/Interface.cpp @@ -181,8 +172,10 @@ macro(add_main_lib TARGET_NAME LIBRARY_TYPE) ${MAIN_LIB_DIR}/serializer/BinaryDeserializer.cpp ${MAIN_LIB_DIR}/serializer/BinarySerializer.cpp + ${MAIN_LIB_DIR}/serializer/CLoadFile.cpp ${MAIN_LIB_DIR}/serializer/CMemorySerializer.cpp ${MAIN_LIB_DIR}/serializer/Connection.cpp + ${MAIN_LIB_DIR}/serializer/CSaveFile.cpp ${MAIN_LIB_DIR}/serializer/CSerializer.cpp ${MAIN_LIB_DIR}/serializer/CTypeList.cpp ${MAIN_LIB_DIR}/serializer/JsonDeserializer.cpp @@ -505,6 +498,10 @@ macro(add_main_lib TARGET_NAME LIBRARY_TYPE) ${MAIN_LIB_DIR}/pathfinder/TurnInfo.h ${MAIN_LIB_DIR}/registerTypes/RegisterTypes.h + ${MAIN_LIB_DIR}/registerTypes/RegisterTypesClientPacks.h + ${MAIN_LIB_DIR}/registerTypes/RegisterTypesLobbyPacks.h + ${MAIN_LIB_DIR}/registerTypes/RegisterTypesMapObjects.h + ${MAIN_LIB_DIR}/registerTypes/RegisterTypesServerPacks.h ${MAIN_LIB_DIR}/rewardable/Configuration.h ${MAIN_LIB_DIR}/rewardable/Info.h @@ -548,8 +545,10 @@ macro(add_main_lib TARGET_NAME LIBRARY_TYPE) ${MAIN_LIB_DIR}/serializer/BinaryDeserializer.h ${MAIN_LIB_DIR}/serializer/BinarySerializer.h + ${MAIN_LIB_DIR}/serializer/CLoadFile.h ${MAIN_LIB_DIR}/serializer/CMemorySerializer.h ${MAIN_LIB_DIR}/serializer/Connection.h + ${MAIN_LIB_DIR}/serializer/CSaveFile.h ${MAIN_LIB_DIR}/serializer/CSerializer.h ${MAIN_LIB_DIR}/serializer/CTypeList.h ${MAIN_LIB_DIR}/serializer/JsonDeserializer.h diff --git a/lib/IGameCallback.cpp b/lib/IGameCallback.cpp index 76ff89457..e14a5ac1c 100644 --- a/lib/IGameCallback.cpp +++ b/lib/IGameCallback.cpp @@ -21,9 +21,8 @@ #include "bonuses/Propagators.h" #include "bonuses/Updaters.h" -#include "serializer/CSerializer.h" // for SAVEGAME_MAGIC -#include "serializer/BinaryDeserializer.h" -#include "serializer/BinarySerializer.h" +#include "serializer/CLoadFile.h" +#include "serializer/CSaveFile.h" #include "rmg/CMapGenOptions.h" #include "mapObjectConstructors/AObjectTypeHandler.h" #include "mapObjectConstructors/CObjectClassesHandler.h" diff --git a/lib/gameState/CGameState.cpp b/lib/gameState/CGameState.cpp index 7f9fda6df..beb8eeda4 100644 --- a/lib/gameState/CGameState.cpp +++ b/lib/gameState/CGameState.cpp @@ -42,7 +42,7 @@ #include "../modding/IdentifierStorage.h" #include "../pathfinder/CPathfinder.h" #include "../pathfinder/PathfinderOptions.h" -#include "../registerTypes/RegisterTypes.h" +#include "../registerTypes/RegisterTypesClientPacks.h" #include "../rmg/CMapGenerator.h" #include "../serializer/CMemorySerializer.h" #include "../serializer/CTypeList.h" @@ -157,8 +157,7 @@ CGameState::CGameState() gs = this; heroesPool = std::make_unique(); applier = std::make_shared>(); - registerTypesClientPacks1(*applier); - registerTypesClientPacks2(*applier); + registerTypesClientPacks(*applier); globalEffects.setNodeType(CBonusSystemNode::GLOBAL_EFFECTS); } diff --git a/lib/mapping/CMapInfo.cpp b/lib/mapping/CMapInfo.cpp index ede7a16e6..f729cdbcf 100644 --- a/lib/mapping/CMapInfo.cpp +++ b/lib/mapping/CMapInfo.cpp @@ -21,7 +21,7 @@ #include "../campaign/CampaignHandler.h" #include "../filesystem/Filesystem.h" -#include "../serializer/CMemorySerializer.h" +#include "../serializer/CLoadFile.h" #include "../CGeneralTextHandler.h" #include "../rmg/CMapGenOptions.h" #include "../CCreatureHandler.h" diff --git a/lib/registerTypes/RegisterTypes.cpp b/lib/registerTypes/RegisterTypes.cpp deleted file mode 100644 index cc96a27b5..000000000 --- a/lib/registerTypes/RegisterTypes.cpp +++ /dev/null @@ -1,51 +0,0 @@ -/* - * RegisterTypes.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 - * - */ -#include "StdInc.h" -#define INSTANTIATE_REGISTER_TYPES_HERE -#include "RegisterTypes.h" - -#include "../mapping/CMapInfo.h" -#include "../StartInfo.h" -#include "../mapObjects/CObjectHandler.h" -#include "../CCreatureHandler.h" -#include "../spells/CSpellHandler.h" - -#include "../serializer/BinaryDeserializer.h" -#include "../serializer/BinarySerializer.h" -#include "../serializer/CTypeList.h" - -VCMI_LIB_NAMESPACE_BEGIN - -// For reference: peak memory usage by gcc during compilation of register type templates -// registerTypesMapObjects: 1.9 Gb -// registerTypes2: 2.2 Gb -// registerTypesClientPacks1 1.6 Gb -// registerTypesClientPacks2 1.6 Gb -// registerTypesServerPacks: 1.3 Gb -// registerTypes4: 1.3 Gb - - -#define DEFINE_EXTERNAL_METHOD(METHODNAME) \ -extern template DLL_LINKAGE void METHODNAME(BinaryDeserializer & s); \ -extern template DLL_LINKAGE void METHODNAME(BinarySerializer & s); - -//DEFINE_EXTERNAL_METHOD(registerTypesMapObjects) -DEFINE_EXTERNAL_METHOD(registerTypesMapObjects1) -DEFINE_EXTERNAL_METHOD(registerTypesMapObjects2) -DEFINE_EXTERNAL_METHOD(registerTypesClientPacks1) -DEFINE_EXTERNAL_METHOD(registerTypesClientPacks2) -DEFINE_EXTERNAL_METHOD(registerTypesServerPacks) -DEFINE_EXTERNAL_METHOD(registerTypesLobbyPacks) - -template void registerTypes(BinaryDeserializer & s); -template void registerTypes(BinarySerializer & s); -template void registerTypes(CTypeList & s); - -VCMI_LIB_NAMESPACE_END diff --git a/lib/registerTypes/RegisterTypes.h b/lib/registerTypes/RegisterTypes.h index 9736aa285..2c2284452 100644 --- a/lib/registerTypes/RegisterTypes.h +++ b/lib/registerTypes/RegisterTypes.h @@ -9,351 +9,20 @@ */ #pragma once -#include "../networkPacks/PacksForClient.h" -#include "../networkPacks/PacksForClientBattle.h" -#include "../networkPacks/PacksForServer.h" -#include "../networkPacks/PacksForLobby.h" -#include "../networkPacks/SetStackEffect.h" -#include "../mapObjectConstructors/CBankInstanceConstructor.h" -#include "../mapObjects/MapObjects.h" -#include "../mapObjects/CGCreature.h" -#include "../mapObjects/CGTownBuilding.h" -#include "../mapObjects/ObjectTemplate.h" -#include "../battle/BattleInfo.h" -#include "../battle/CObstacleInstance.h" -#include "../bonuses/Limiters.h" -#include "../bonuses/Updaters.h" -#include "../bonuses/Propagators.h" -#include "../CPlayerState.h" -#include "../CStack.h" +#include "RegisterTypesClientPacks.h" +#include "RegisterTypesLobbyPacks.h" +#include "RegisterTypesMapObjects.h" +#include "RegisterTypesServerPacks.h" VCMI_LIB_NAMESPACE_BEGIN -class BinarySerializer; -class BinaryDeserializer; -class CTypeList; - -template -void registerTypesMapObjects1(Serializer &s) -{ - ////////////////////////////////////////////////////////////////////////// - // Adventure map objects - ////////////////////////////////////////////////////////////////////////// - s.template registerType(); - - // Non-armed objects - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - - s.template registerType(); s.template registerType(); s.template registerType(); - - // Armed objects - s.template registerType(); s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); s.template registerType(); - s.template registerType(); -} - -template -void registerTypesMapObjectTypes(Serializer &s) -{ - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - - s.template registerType(); - s.template registerType(); - s.template registerType(); - //new types (other than netpacks) must register here - //order of type registration is critical for loading old savegames -} - -template -void registerTypesMapObjects2(Serializer &s) -{ - //Other object-related - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - - s.template registerType(); - - s.template registerType(); - s.template registerType(); - - //s.template registerType(); - //s.template registerType(); - - //end of objects - - ////////////////////////////////////////////////////////////////////////// - // Bonus system - ////////////////////////////////////////////////////////////////////////// - //s.template registerType(); - s.template registerType(); - - // Limiters - //s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - -// s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - //s.template registerType(); //TODO - //s.template registerType(); - s.template registerType(); - s.template registerType(); - //s.template registerType(); - s.template registerType(); - - //s.template registerType(); - s.template registerType(); -} -template -void registerTypesClientPacks1(Serializer &s) -{ - s.template registerType(); - - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); -} - -template -void registerTypesClientPacks2(Serializer &s) -{ - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - - s.template registerType(); - s.template registerType(); - s.template registerType(); -} - -template -void registerTypesServerPacks(Serializer &s) -{ - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); -} - -template -void registerTypesLobbyPacks(Serializer &s) -{ - s.template registerType(); - s.template registerType(); - s.template registerType(); - - // Any client can sent - s.template registerType(); - s.template registerType(); - s.template registerType(); - // Only host client send - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - // Only server send - s.template registerType(); - s.template registerType(); - - // For client with permissions - s.template registerType(); - // Only for host client - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); - s.template registerType(); -} - template void registerTypes(Serializer &s) { - registerTypesMapObjects1(s); - registerTypesMapObjects2(s); - registerTypesMapObjectTypes(s); - registerTypesClientPacks1(s); - registerTypesClientPacks2(s); + registerTypesMapObjects(s); + registerTypesClientPacks(s); registerTypesServerPacks(s); registerTypesLobbyPacks(s); } -#ifndef INSTANTIATE_REGISTER_TYPES_HERE - -extern template DLL_LINKAGE void registerTypes(BinaryDeserializer & s); -extern template DLL_LINKAGE void registerTypes(BinarySerializer & s); - -#endif - - VCMI_LIB_NAMESPACE_END diff --git a/lib/registerTypes/RegisterTypesClientPacks.h b/lib/registerTypes/RegisterTypesClientPacks.h new file mode 100644 index 000000000..7779c618a --- /dev/null +++ b/lib/registerTypes/RegisterTypesClientPacks.h @@ -0,0 +1,125 @@ +/* + * RegisterTypesClientPacks.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 "../networkPacks/PacksForClient.h" +#include "../networkPacks/PacksForClientBattle.h" +#include "../networkPacks/SetStackEffect.h" + +VCMI_LIB_NAMESPACE_BEGIN + +template +void registerTypesClientPacks(Serializer &s) +{ + s.template registerType(); + + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + + s.template registerType(); + s.template registerType(); + s.template registerType(); +} + +VCMI_LIB_NAMESPACE_END diff --git a/lib/registerTypes/RegisterTypesLobbyPacks.h b/lib/registerTypes/RegisterTypesLobbyPacks.h new file mode 100644 index 000000000..6e20ee244 --- /dev/null +++ b/lib/registerTypes/RegisterTypesLobbyPacks.h @@ -0,0 +1,62 @@ +/* + * RegisterTypesLobbyPacks.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 "../networkPacks/PacksForLobby.h" +#include "../gameState/CGameState.h" +#include "../campaign/CampaignState.h" +#include "../mapping/CMapInfo.h" +#include "../rmg/CMapGenOptions.h" +#include "../gameState/TavernHeroesPool.h" +#include "../gameState/CGameStateCampaign.h" +#include "../mapping/CMap.h" +#include "../TerrainHandler.h" +#include "../RiverHandler.h" +#include "../RoadHandler.h" + +VCMI_LIB_NAMESPACE_BEGIN + +template +void registerTypesLobbyPacks(Serializer &s) +{ + s.template registerType(); + s.template registerType(); + s.template registerType(); + + // Any client can sent + s.template registerType(); + s.template registerType(); + s.template registerType(); + // Only host client send + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + // Only server send + s.template registerType(); + s.template registerType(); + + // For client with permissions + s.template registerType(); + // Only for host client + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); +} + +VCMI_LIB_NAMESPACE_END diff --git a/lib/registerTypes/RegisterTypesMapObjects.h b/lib/registerTypes/RegisterTypesMapObjects.h new file mode 100644 index 000000000..73f87e5e3 --- /dev/null +++ b/lib/registerTypes/RegisterTypesMapObjects.h @@ -0,0 +1,137 @@ +/* + * RegisterTypesMapObjects.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 "../mapObjectConstructors/CBankInstanceConstructor.h" +#include "../mapObjects/MapObjects.h" +#include "../mapObjects/CGCreature.h" +#include "../mapObjects/CGTownBuilding.h" +#include "../mapObjects/ObjectTemplate.h" +#include "../battle/BattleInfo.h" +#include "../battle/CObstacleInstance.h" +#include "../bonuses/Limiters.h" +#include "../bonuses/Updaters.h" +#include "../bonuses/Propagators.h" +#include "../CPlayerState.h" +#include "../CStack.h" +#include "../CHeroHandler.h" + +VCMI_LIB_NAMESPACE_BEGIN + +template +void registerTypesMapObjects(Serializer &s) +{ + ////////////////////////////////////////////////////////////////////////// + // Adventure map objects + ////////////////////////////////////////////////////////////////////////// + s.template registerType(); + + // Non-armed objects + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + + s.template registerType(); s.template registerType(); s.template registerType(); + + // Armed objects + s.template registerType(); s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); s.template registerType(); + s.template registerType(); + + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + + s.template registerType(); + s.template registerType(); + s.template registerType(); + //new types (other than netpacks) must register here + //order of type registration is critical for loading old savegames + + //Other object-related + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + + s.template registerType(); + + s.template registerType(); + s.template registerType(); + + //end of objects + + ////////////////////////////////////////////////////////////////////////// + // Bonus system + ////////////////////////////////////////////////////////////////////////// + //s.template registerType(); + s.template registerType(); + + // Limiters + //s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + +// s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + //s.template registerType(); //TODO + //s.template registerType(); + s.template registerType(); + s.template registerType(); + //s.template registerType(); + s.template registerType(); + + //s.template registerType(); + s.template registerType(); +} + +VCMI_LIB_NAMESPACE_END diff --git a/lib/registerTypes/RegisterTypesServerPacks.h b/lib/registerTypes/RegisterTypesServerPacks.h new file mode 100644 index 000000000..f5eed0156 --- /dev/null +++ b/lib/registerTypes/RegisterTypesServerPacks.h @@ -0,0 +1,56 @@ +/* + * RegisterTypesServerPacks.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 "../networkPacks/PacksForServer.h" + +VCMI_LIB_NAMESPACE_BEGIN + +class BinarySerializer; +class BinaryDeserializer; +class CTypeList; + +template +void registerTypesServerPacks(Serializer &s) +{ + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); + s.template registerType(); +} + +VCMI_LIB_NAMESPACE_END diff --git a/lib/registerTypes/TypesClientPacks1.cpp b/lib/registerTypes/TypesClientPacks1.cpp deleted file mode 100644 index c13b78c83..000000000 --- a/lib/registerTypes/TypesClientPacks1.cpp +++ /dev/null @@ -1,33 +0,0 @@ -/* - * TypesClientPacks1.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 - * - */ -#include "StdInc.h" -#include "RegisterTypes.h" - -#include "../StartInfo.h" -#include "../mapObjects/CObjectHandler.h" -#include "../CCreatureHandler.h" -#include "../VCMI_Lib.h" -#include "../CArtHandler.h" -#include "../CHeroHandler.h" -#include "../spells/CSpellHandler.h" -#include "../CTownHandler.h" - -#include "../serializer/BinaryDeserializer.h" -#include "../serializer/BinarySerializer.h" -#include "../serializer/CTypeList.h" - -VCMI_LIB_NAMESPACE_BEGIN - - -template void registerTypesClientPacks1(BinaryDeserializer & s); -template void registerTypesClientPacks1(BinarySerializer & s); -template void registerTypesClientPacks1(CTypeList & s); - -VCMI_LIB_NAMESPACE_END diff --git a/lib/registerTypes/TypesClientPacks2.cpp b/lib/registerTypes/TypesClientPacks2.cpp deleted file mode 100644 index bf68ca257..000000000 --- a/lib/registerTypes/TypesClientPacks2.cpp +++ /dev/null @@ -1,37 +0,0 @@ -/* - * TypesClientPacks2.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 - * - */ -#include "StdInc.h" -#include "RegisterTypes.h" - -#include "../StartInfo.h" -#include "../CStack.h" -#include "../battle/BattleInfo.h" -#include "../mapObjects/CObjectHandler.h" -#include "../CCreatureHandler.h" -#include "../VCMI_Lib.h" -#include "../CArtHandler.h" -#include "../CHeroHandler.h" -#include "../spells/CSpellHandler.h" -#include "../CTownHandler.h" - -#include "../serializer/BinaryDeserializer.h" -#include "../serializer/BinarySerializer.h" -#include "../serializer/CTypeList.h" - -VCMI_LIB_NAMESPACE_BEGIN - - -template void registerTypesClientPacks2(BinaryDeserializer & s); -template void registerTypesClientPacks2(BinarySerializer & s); -template void registerTypesClientPacks2(CTypeList & s); - - - -VCMI_LIB_NAMESPACE_END diff --git a/lib/registerTypes/TypesLobbyPacks.cpp b/lib/registerTypes/TypesLobbyPacks.cpp deleted file mode 100644 index fe3838d05..000000000 --- a/lib/registerTypes/TypesLobbyPacks.cpp +++ /dev/null @@ -1,43 +0,0 @@ -/* - * TypesLobbyPacks.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 - * - */ -#include "StdInc.h" -#include "RegisterTypes.h" - -#include "../mapping/CMapInfo.h" -#include "../StartInfo.h" -#include "../gameState/CGameState.h" -#include "../gameState/CGameStateCampaign.h" -#include "../gameState/TavernHeroesPool.h" -#include "../mapping/CMap.h" -#include "../mapObjects/CObjectHandler.h" -#include "../CCreatureHandler.h" -#include "../VCMI_Lib.h" -#include "../CArtHandler.h" -#include "../CHeroHandler.h" -#include "../spells/CSpellHandler.h" -#include "../CTownHandler.h" -#include "../RoadHandler.h" -#include "../RiverHandler.h" -#include "../TerrainHandler.h" -#include "../campaign/CampaignState.h" -#include "../rmg/CMapGenOptions.h" - -#include "../serializer/BinaryDeserializer.h" -#include "../serializer/BinarySerializer.h" -#include "../serializer/CTypeList.h" - -VCMI_LIB_NAMESPACE_BEGIN - -template void registerTypesLobbyPacks(BinaryDeserializer & s); -template void registerTypesLobbyPacks(BinarySerializer & s); -template void registerTypesLobbyPacks(CTypeList & s); - - -VCMI_LIB_NAMESPACE_END diff --git a/lib/registerTypes/TypesMapObjects1.cpp b/lib/registerTypes/TypesMapObjects1.cpp deleted file mode 100644 index 1e383a581..000000000 --- a/lib/registerTypes/TypesMapObjects1.cpp +++ /dev/null @@ -1,34 +0,0 @@ -/* - * TypesMapObjects1.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 - * - */ -#include "StdInc.h" -#include "RegisterTypes.h" - -#include "../StartInfo.h" -#include "../mapObjects/CObjectHandler.h" -#include "../CCreatureHandler.h" -#include "../VCMI_Lib.h" -#include "../CArtHandler.h" -#include "../CHeroHandler.h" -#include "../spells/CSpellHandler.h" -#include "../CTownHandler.h" - -#include "../serializer/BinaryDeserializer.h" -#include "../serializer/BinarySerializer.h" -#include "../serializer/CTypeList.h" - -VCMI_LIB_NAMESPACE_BEGIN - -template void registerTypesMapObjects1(BinaryDeserializer & s); -template void registerTypesMapObjects1(BinarySerializer & s); -template void registerTypesMapObjects1(CTypeList & s); - - - -VCMI_LIB_NAMESPACE_END diff --git a/lib/registerTypes/TypesMapObjects2.cpp b/lib/registerTypes/TypesMapObjects2.cpp deleted file mode 100644 index ab7d4f1a1..000000000 --- a/lib/registerTypes/TypesMapObjects2.cpp +++ /dev/null @@ -1,36 +0,0 @@ -/* - * TypesMapObjects2.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 - * - */ -#include "StdInc.h" -#include "RegisterTypes.h" - -#include "../StartInfo.h" -#include "../CStack.h" -#include "../battle/BattleInfo.h" -#include "../mapObjects/CObjectHandler.h" -#include "../CCreatureHandler.h" -#include "../VCMI_Lib.h" -#include "../CArtHandler.h" -#include "../CHeroHandler.h" -#include "../spells/CSpellHandler.h" -#include "../CTownHandler.h" - -#include "../serializer/BinaryDeserializer.h" -#include "../serializer/BinarySerializer.h" -#include "../serializer/CTypeList.h" - -VCMI_LIB_NAMESPACE_BEGIN - - -template void registerTypesMapObjects2(BinaryDeserializer & s); -template void registerTypesMapObjects2(BinarySerializer & s); -template void registerTypesMapObjects2(CTypeList & s); - - -VCMI_LIB_NAMESPACE_END diff --git a/lib/registerTypes/TypesMapObjects3.cpp b/lib/registerTypes/TypesMapObjects3.cpp deleted file mode 100644 index 3833d0dbf..000000000 --- a/lib/registerTypes/TypesMapObjects3.cpp +++ /dev/null @@ -1,32 +0,0 @@ -/* - * TypesMapObjects3.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 - * - */ -#include "StdInc.h" -#include "RegisterTypes.h" - -#include "../StartInfo.h" -#include "../mapObjects/CObjectHandler.h" -#include "../CCreatureHandler.h" -#include "../VCMI_Lib.h" -#include "../CArtHandler.h" -#include "../CHeroHandler.h" -#include "../spells/CSpellHandler.h" -#include "../CTownHandler.h" - -#include "../serializer/BinaryDeserializer.h" -#include "../serializer/BinarySerializer.h" -#include "../serializer/CTypeList.h" - -VCMI_LIB_NAMESPACE_BEGIN - -template void registerTypesMapObjectTypes(BinaryDeserializer & s); -template void registerTypesMapObjectTypes(BinarySerializer & s); -template void registerTypesMapObjectTypes(CTypeList & s); - -VCMI_LIB_NAMESPACE_END diff --git a/lib/registerTypes/TypesServerPacks.cpp b/lib/registerTypes/TypesServerPacks.cpp deleted file mode 100644 index 3d37cbc92..000000000 --- a/lib/registerTypes/TypesServerPacks.cpp +++ /dev/null @@ -1,32 +0,0 @@ -/* - * TypesServerPacks.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 - * - */ -#include "StdInc.h" -#include "RegisterTypes.h" - -#include "../StartInfo.h" -#include "../mapObjects/CObjectHandler.h" -#include "../CCreatureHandler.h" -#include "../VCMI_Lib.h" -#include "../CArtHandler.h" -#include "../CHeroHandler.h" -#include "../spells/CSpellHandler.h" -#include "../CTownHandler.h" - -#include "../serializer/BinaryDeserializer.h" -#include "../serializer/BinarySerializer.h" -#include "../serializer/CTypeList.h" - -VCMI_LIB_NAMESPACE_BEGIN - -template void registerTypesServerPacks(BinaryDeserializer & s); -template void registerTypesServerPacks(BinarySerializer & s); -template void registerTypesServerPacks(CTypeList & s); - -VCMI_LIB_NAMESPACE_END diff --git a/lib/serializer/BinaryDeserializer.cpp b/lib/serializer/BinaryDeserializer.cpp index 95400caf9..0cb95f14a 100644 --- a/lib/serializer/BinaryDeserializer.cpp +++ b/lib/serializer/BinaryDeserializer.cpp @@ -9,97 +9,18 @@ */ #include "StdInc.h" #include "BinaryDeserializer.h" - #include "../registerTypes/RegisterTypes.h" VCMI_LIB_NAMESPACE_BEGIN -extern template void registerTypes(BinaryDeserializer & s); - -CLoadFile::CLoadFile(const boost::filesystem::path & fname, int minimalVersion) - : serializer(this) +BinaryDeserializer::BinaryDeserializer(IBinaryReader * r): CLoaderBase(r) { - registerTypes(serializer); - openNextFile(fname, minimalVersion); -} + saving = false; + fileVersion = 0; + smartPointerSerialization = true; + reverseEndianess = false; -//must be instantiated in .cpp file for access to complete types of all member fields -CLoadFile::~CLoadFile() = default; - -int CLoadFile::read(void * data, unsigned size) -{ - sfile->read(reinterpret_cast(data), size); - return size; -} - -void CLoadFile::openNextFile(const boost::filesystem::path & fname, int minimalVersion) -{ - assert(!serializer.reverseEndianess); - assert(minimalVersion <= SERIALIZATION_VERSION); - - try - { - fName = fname.string(); - sfile = std::make_unique(fname.c_str(), std::ios::in | 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 - char buffer[4]; - sfile->read(buffer, 4); - if(std::memcmp(buffer, "VCMI", 4) != 0) - THROW_FORMAT("Error: not a VCMI file(%s)!", fName); - - serializer & serializer.fileVersion; - if(serializer.fileVersion < minimalVersion) - THROW_FORMAT("Error: too old file format (%s)!", fName); - - if(serializer.fileVersion > SERIALIZATION_VERSION) - { - logGlobal->warn("Warning format version mismatch: found %d when current is %d! (file %s)\n", serializer.fileVersion, SERIALIZATION_VERSION , fName); - - auto * versionptr = reinterpret_cast(&serializer.fileVersion); - std::reverse(versionptr, versionptr + 4); - logGlobal->warn("Version number reversed is %x, checking...", serializer.fileVersion); - - if(serializer.fileVersion == SERIALIZATION_VERSION) - { - logGlobal->warn("%s seems to have different endianness! Entering reversing mode.", fname.string()); - serializer.reverseEndianess = true; - } - else - THROW_FORMAT("Error: too new file format (%s)!", fName); - } - } - catch(...) - { - clear(); //if anything went wrong, we delete file and rethrow - throw; - } -} - -void CLoadFile::reportState(vstd::CLoggerBase * out) -{ - out->debug("CLoadFile"); - if(!!sfile && *sfile) - out->debug("\tOpened %s Position: %d", fName, sfile->tellg()); -} - -void CLoadFile::clear() -{ - sfile = nullptr; - fName.clear(); - serializer.fileVersion = 0; -} - -void CLoadFile::checkMagicBytes(const std::string &text) -{ - std::string loaded = text; - read((void *)loaded.data(), static_cast(text.length())); - if(loaded != text) - throw std::runtime_error("Magic bytes doesn't match!"); + registerTypes(*this); } VCMI_LIB_NAMESPACE_END diff --git a/lib/serializer/BinaryDeserializer.h b/lib/serializer/BinaryDeserializer.h index 9b57ee480..1c4858469 100644 --- a/lib/serializer/BinaryDeserializer.h +++ b/lib/serializer/BinaryDeserializer.h @@ -12,12 +12,9 @@ #include "CSerializer.h" #include "CTypeList.h" #include "../mapObjects/CGHeroInstance.h" -#include "../../Global.h" VCMI_LIB_NAMESPACE_BEGIN -class CStackInstance; - class DLL_LINKAGE CLoaderBase { protected: @@ -115,7 +112,8 @@ class DLL_LINKAGE BinaryDeserializer : public CLoaderBase } }; - template class CPointerLoader : public IPointerLoader + template + class CPointerLoader : public IPointerLoader { public: void * loadPtr(CLoaderBase &ar, ui32 pid) const override //data is pointer to the ACTUAL POINTER @@ -147,13 +145,7 @@ public: bool smartPointerSerialization; bool saving; - BinaryDeserializer(IBinaryReader * r): CLoaderBase(r) - { - saving = false; - fileVersion = 0; - smartPointerSerialization = true; - reverseEndianess = false; - } + BinaryDeserializer(IBinaryReader * r); template BinaryDeserializer & operator&(T & t) @@ -538,30 +530,4 @@ public: } }; -class DLL_LINKAGE CLoadFile : public IBinaryReader -{ -public: - BinaryDeserializer serializer; - - std::string fName; - std::unique_ptr sfile; - - CLoadFile(const boost::filesystem::path & fname, int minimalVersion = SERIALIZATION_VERSION); //throws! - virtual ~CLoadFile(); - int read(void * data, unsigned size) override; //throws! - - void openNextFile(const boost::filesystem::path & fname, int minimalVersion); //throws! - void clear(); - void reportState(vstd::CLoggerBase * out) override; - - void checkMagicBytes(const std::string & text); - - template - CLoadFile & operator>>(T &t) - { - serializer & t; - return * this; - } -}; - VCMI_LIB_NAMESPACE_END diff --git a/lib/serializer/BinarySerializer.cpp b/lib/serializer/BinarySerializer.cpp index 46c99f6de..8ba41babd 100644 --- a/lib/serializer/BinarySerializer.cpp +++ b/lib/serializer/BinarySerializer.cpp @@ -9,69 +9,15 @@ */ #include "StdInc.h" #include "BinarySerializer.h" - #include "../registerTypes/RegisterTypes.h" VCMI_LIB_NAMESPACE_BEGIN -extern template void registerTypes(BinarySerializer & s); - -CSaveFile::CSaveFile(const boost::filesystem::path &fname) - : serializer(this) +BinarySerializer::BinarySerializer(IBinaryWriter * w): CSaverBase(w) { - registerTypes(serializer); - openNextFile(fname); -} - -//must be instantiated in .cpp file for access to complete types of all member fields -CSaveFile::~CSaveFile() = default; - -int CSaveFile::write(const void * data, unsigned size) -{ - sfile->write((char *)data,size); - return size; -} - -void CSaveFile::openNextFile(const boost::filesystem::path &fname) -{ - fName = fname; - try - { - sfile = std::make_unique(fname.c_str(), std::ios::out | 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); - - sfile->write("VCMI",4); //write magic identifier - serializer & SERIALIZATION_VERSION; //write format version - } - catch(...) - { - logGlobal->error("Failed to save to %s", fname.string()); - clear(); - throw; - } -} - -void CSaveFile::reportState(vstd::CLoggerBase * out) -{ - out->debug("CSaveFile"); - if(sfile.get() && *sfile) - { - out->debug("\tOpened %s \tPosition: %d", fName, sfile->tellp()); - } -} - -void CSaveFile::clear() -{ - fName.clear(); - sfile = nullptr; -} - -void CSaveFile::putMagicBytes(const std::string &text) -{ - write(text.c_str(), static_cast(text.length())); + saving=true; + smartPointerSerialization = true; + registerTypes(*this); } VCMI_LIB_NAMESPACE_END diff --git a/lib/serializer/BinarySerializer.h b/lib/serializer/BinarySerializer.h index f65ef07ec..37a054d2d 100644 --- a/lib/serializer/BinarySerializer.h +++ b/lib/serializer/BinarySerializer.h @@ -115,11 +115,7 @@ public: bool smartPointerSerialization; bool saving; - BinarySerializer(IBinaryWriter * w): CSaverBase(w) - { - saving=true; - smartPointerSerialization = true; - } + BinarySerializer(IBinaryWriter * w); template void registerType(const Base * b = nullptr, const Derived * d = nullptr) @@ -404,30 +400,4 @@ public: } }; -class DLL_LINKAGE CSaveFile : public IBinaryWriter -{ -public: - BinarySerializer serializer; - - boost::filesystem::path fName; - std::unique_ptr sfile; - - CSaveFile(const boost::filesystem::path &fname); //throws! - ~CSaveFile(); - int write(const void * data, unsigned size) override; - - void openNextFile(const boost::filesystem::path &fname); //throws! - void clear(); - void reportState(vstd::CLoggerBase * out) override; - - void putMagicBytes(const std::string &text); - - template - CSaveFile & operator<<(const T &t) - { - serializer & t; - return * this; - } -}; - VCMI_LIB_NAMESPACE_END diff --git a/lib/serializer/CLoadFile.cpp b/lib/serializer/CLoadFile.cpp new file mode 100644 index 000000000..cf5ea34d7 --- /dev/null +++ b/lib/serializer/CLoadFile.cpp @@ -0,0 +1,100 @@ +/* + * CLoadFile.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 + * + */ +#include "StdInc.h" +#include "CLoadFile.h" + +VCMI_LIB_NAMESPACE_BEGIN + +CLoadFile::CLoadFile(const boost::filesystem::path & fname, int minimalVersion) + : serializer(this) +{ + openNextFile(fname, minimalVersion); +} + +//must be instantiated in .cpp file for access to complete types of all member fields +CLoadFile::~CLoadFile() = default; + +int CLoadFile::read(void * data, unsigned size) +{ + sfile->read(reinterpret_cast(data), size); + return size; +} + +void CLoadFile::openNextFile(const boost::filesystem::path & fname, int minimalVersion) +{ + assert(!serializer.reverseEndianess); + assert(minimalVersion <= SERIALIZATION_VERSION); + + try + { + fName = fname.string(); + sfile = std::make_unique(fname.c_str(), std::ios::in | 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 + char buffer[4]; + sfile->read(buffer, 4); + if(std::memcmp(buffer, "VCMI", 4) != 0) + THROW_FORMAT("Error: not a VCMI file(%s)!", fName); + + serializer & serializer.fileVersion; + if(serializer.fileVersion < minimalVersion) + THROW_FORMAT("Error: too old file format (%s)!", fName); + + if(serializer.fileVersion > SERIALIZATION_VERSION) + { + logGlobal->warn("Warning format version mismatch: found %d when current is %d! (file %s)\n", serializer.fileVersion, SERIALIZATION_VERSION , fName); + + auto * versionptr = reinterpret_cast(&serializer.fileVersion); + std::reverse(versionptr, versionptr + 4); + logGlobal->warn("Version number reversed is %x, checking...", serializer.fileVersion); + + if(serializer.fileVersion == SERIALIZATION_VERSION) + { + logGlobal->warn("%s seems to have different endianness! Entering reversing mode.", fname.string()); + serializer.reverseEndianess = true; + } + else + THROW_FORMAT("Error: too new file format (%s)!", fName); + } + } + catch(...) + { + clear(); //if anything went wrong, we delete file and rethrow + throw; + } +} + +void CLoadFile::reportState(vstd::CLoggerBase * out) +{ + out->debug("CLoadFile"); + if(!!sfile && *sfile) + out->debug("\tOpened %s Position: %d", fName, sfile->tellg()); +} + +void CLoadFile::clear() +{ + sfile = nullptr; + fName.clear(); + serializer.fileVersion = 0; +} + +void CLoadFile::checkMagicBytes(const std::string &text) +{ + std::string loaded = text; + read((void *)loaded.data(), static_cast(text.length())); + if(loaded != text) + throw std::runtime_error("Magic bytes doesn't match!"); +} + +VCMI_LIB_NAMESPACE_END diff --git a/lib/serializer/CLoadFile.h b/lib/serializer/CLoadFile.h new file mode 100644 index 000000000..8f9079bb9 --- /dev/null +++ b/lib/serializer/CLoadFile.h @@ -0,0 +1,42 @@ +/* + * CLoadFile.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 "BinaryDeserializer.h" + +VCMI_LIB_NAMESPACE_BEGIN + +class DLL_LINKAGE CLoadFile : public IBinaryReader +{ +public: + BinaryDeserializer serializer; + + std::string fName; + std::unique_ptr sfile; + + CLoadFile(const boost::filesystem::path & fname, int minimalVersion = SERIALIZATION_VERSION); //throws! + virtual ~CLoadFile(); + int read(void * data, unsigned size) override; //throws! + + void openNextFile(const boost::filesystem::path & fname, int minimalVersion); //throws! + void clear(); + void reportState(vstd::CLoggerBase * out) override; + + void checkMagicBytes(const std::string & text); + + template + CLoadFile & operator>>(T &t) + { + serializer & t; + return * this; + } +}; + +VCMI_LIB_NAMESPACE_END diff --git a/lib/serializer/CMemorySerializer.cpp b/lib/serializer/CMemorySerializer.cpp index f9d62c619..797c10a6d 100644 --- a/lib/serializer/CMemorySerializer.cpp +++ b/lib/serializer/CMemorySerializer.cpp @@ -10,8 +10,6 @@ #include "StdInc.h" #include "CMemorySerializer.h" -#include "../registerTypes/RegisterTypes.h" - VCMI_LIB_NAMESPACE_BEGIN int CMemorySerializer::read(void * data, unsigned size) @@ -34,8 +32,6 @@ int CMemorySerializer::write(const void * data, unsigned size) CMemorySerializer::CMemorySerializer(): iser(this), oser(this), readPos(0) { - registerTypes(iser); - registerTypes(oser); iser.fileVersion = SERIALIZATION_VERSION; } diff --git a/lib/serializer/CSaveFile.cpp b/lib/serializer/CSaveFile.cpp new file mode 100644 index 000000000..5f377aed6 --- /dev/null +++ b/lib/serializer/CSaveFile.cpp @@ -0,0 +1,72 @@ +/* + * CSaveFile.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 + * + */ +#include "StdInc.h" +#include "CSaveFile.h" + +VCMI_LIB_NAMESPACE_BEGIN + +CSaveFile::CSaveFile(const boost::filesystem::path &fname) + : serializer(this) +{ + openNextFile(fname); +} + +//must be instantiated in .cpp file for access to complete types of all member fields +CSaveFile::~CSaveFile() = default; + +int CSaveFile::write(const void * data, unsigned size) +{ + sfile->write((char *)data,size); + return size; +} + +void CSaveFile::openNextFile(const boost::filesystem::path &fname) +{ + fName = fname; + try + { + sfile = std::make_unique(fname.c_str(), std::ios::out | 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); + + sfile->write("VCMI",4); //write magic identifier + serializer & SERIALIZATION_VERSION; //write format version + } + catch(...) + { + logGlobal->error("Failed to save to %s", fname.string()); + clear(); + throw; + } +} + +void CSaveFile::reportState(vstd::CLoggerBase * out) +{ + out->debug("CSaveFile"); + if(sfile.get() && *sfile) + { + out->debug("\tOpened %s \tPosition: %d", fName, sfile->tellp()); + } +} + +void CSaveFile::clear() +{ + fName.clear(); + sfile = nullptr; +} + +void CSaveFile::putMagicBytes(const std::string &text) +{ + write(text.c_str(), static_cast(text.length())); +} + +VCMI_LIB_NAMESPACE_END diff --git a/lib/serializer/CSaveFile.h b/lib/serializer/CSaveFile.h new file mode 100644 index 000000000..f1b823bf2 --- /dev/null +++ b/lib/serializer/CSaveFile.h @@ -0,0 +1,42 @@ +/* + * CSaveFile.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 "BinarySerializer.h" + +VCMI_LIB_NAMESPACE_BEGIN + +class DLL_LINKAGE CSaveFile : public IBinaryWriter +{ +public: + BinarySerializer serializer; + + boost::filesystem::path fName; + std::unique_ptr sfile; + + CSaveFile(const boost::filesystem::path &fname); //throws! + ~CSaveFile(); + int write(const void * data, unsigned size) override; + + void openNextFile(const boost::filesystem::path &fname); //throws! + void clear(); + void reportState(vstd::CLoggerBase * out) override; + + void putMagicBytes(const std::string &text); + + template + CSaveFile & operator<<(const T &t) + { + serializer & t; + return * this; + } +}; + +VCMI_LIB_NAMESPACE_END diff --git a/lib/serializer/Connection.cpp b/lib/serializer/Connection.cpp index 3029aaf91..a4ad78056 100644 --- a/lib/serializer/Connection.cpp +++ b/lib/serializer/Connection.cpp @@ -10,8 +10,7 @@ #include "StdInc.h" #include "Connection.h" -#include "../registerTypes/RegisterTypes.h" -#include "../mapping/CMapHeader.h" +#include "../networkPacks/NetPacksBase.h" #include @@ -45,8 +44,6 @@ void CConnection::init() enableSmartPointerSerialization(); disableStackSendingByID(); - registerTypes(iser); - registerTypes(oser); #ifndef VCMI_ENDIAN_BIG myEndianess = true; #else diff --git a/server/CGameHandler.cpp b/server/CGameHandler.cpp index 5bf054899..a1f3c25b0 100644 --- a/server/CGameHandler.cpp +++ b/server/CGameHandler.cpp @@ -27,6 +27,7 @@ #include "../lib/CCreatureSet.h" #include "../lib/CGeneralTextHandler.h" #include "../lib/CHeroHandler.h" +#include "../lib/CPlayerState.h" #include "../lib/CSoundBase.h" #include "../lib/CThreadHelper.h" #include "../lib/CTownHandler.h" @@ -47,20 +48,19 @@ #include "../lib/mapping/CMap.h" #include "../lib/mapping/CMapService.h" +#include "../lib/mapObjects/CGMarket.h" #include "../lib/modding/ModIncompatibility.h" #include "../lib/networkPacks/StackLocation.h" #include "../lib/pathfinder/CPathfinder.h" #include "../lib/pathfinder/PathfinderOptions.h" #include "../lib/pathfinder/TurnInfo.h" -#include "../lib/registerTypes/RegisterTypes.h" +#include "../lib/registerTypes/RegisterTypesServerPacks.h" #include "../lib/rmg/CMapGenOptions.h" -#include "../lib/serializer/CTypeList.h" -#include "../lib/serializer/Cast.h" -#include "../lib/serializer/Connection.h" -#include "../lib/serializer/JsonSerializer.h" +#include "../lib/serializer/CSaveFile.h" +#include "../lib/serializer/CLoadFile.h" #include "../lib/spells/CSpellHandler.h" diff --git a/server/CVCMIServer.cpp b/server/CVCMIServer.cpp index 0eba3ed90..e9651f91c 100644 --- a/server/CVCMIServer.cpp +++ b/server/CVCMIServer.cpp @@ -47,7 +47,7 @@ #include "../lib/UnlockGuard.h" // for applier -#include "../lib/registerTypes/RegisterTypes.h" +#include "../lib/registerTypes/RegisterTypesLobbyPacks.h" // UUID generation #include