diff --git a/common/network/packet/serial_packet_base.hpp b/common/network/packet/serial_packet_base.hpp index 742c4db..16279c8 100644 --- a/common/network/packet/serial_packet_base.hpp +++ b/common/network/packet/serial_packet_base.hpp @@ -38,9 +38,10 @@ struct SerialPacketBase { IPaddress srcAddress; typedef SerialPacketType Type; + + virtual ~SerialPacketBase(); }; -typedef SerialPacketBase* SerialPacket; -typedef SerialPacketType PacketType; +typedef SerialPacketBase SerialPacket; #endif diff --git a/common/network/serial.cpp b/common/network/serial.cpp deleted file mode 100644 index 6e75c5a..0000000 --- a/common/network/serial.cpp +++ /dev/null @@ -1,473 +0,0 @@ -/* Copyright: (c) Kayne Ruse 2014 - * - * This software is provided 'as-is', without any express or implied - * warranty. In no event will the authors be held liable for any damages - * arising from the use of this software. - * - * Permission is granted to anyone to use this software for any purpose, - * including commercial applications, and to alter it and redistribute it - * freely, subject to the following restrictions: - * - * 1. The origin of this software must not be misrepresented; you must not - * claim that you wrote the original software. If you use this software - * in a product, an acknowledgment in the product documentation would be - * appreciated but is not required. - * - * 2. Altered source versions must be plainly marked as such, and must not be - * misrepresented as being the original software. - * - * 3. This notice may not be removed or altered from any source - * distribution. -*/ -#include "serial.hpp" - -#include "map_allocator.hpp" -#include "statistics.hpp" - -#include - -//------------------------- -//Convenience Macros -//------------------------- - -#define SERIALIZE(buffer, data, size) memcpy(buffer, data, size); buffer += size; -#define DESERIALIZE(buffer, data, size) memcpy(data, buffer, size); buffer += size; - -//------------------------- -//internal serialization functions -//------------------------- - -void serializeType(SerialPacket* packet, char* buffer) { - SERIALIZE(buffer, &packet->meta.type, sizeof(SerialPacket::Type)); -} - -void serializeServer(SerialPacket* packet, char* buffer) { - SERIALIZE(buffer, &packet->meta.type, sizeof(SerialPacket::Type)); - - //server info - SERIALIZE(buffer, &packet->serverInfo.networkVersion, sizeof(int)); - SERIALIZE(buffer, packet->serverInfo.name, PACKET_STRING_SIZE); - SERIALIZE(buffer, &packet->serverInfo.playerCount, sizeof(int)); -} - -void serializeClient(SerialPacket* packet, char* buffer) { - SERIALIZE(buffer, &packet->meta.type, sizeof(SerialPacket::Type)); - - //indexes - SERIALIZE(buffer, &packet->clientInfo.clientIndex, sizeof(int)); - SERIALIZE(buffer, &packet->clientInfo.accountIndex, sizeof(int)); - SERIALIZE(buffer, &packet->clientInfo.characterIndex, sizeof(int)); - - //texts - SERIALIZE(buffer, packet->clientInfo.username, PACKET_STRING_SIZE); - //TODO: password - SERIALIZE(buffer, packet->clientInfo.handle, PACKET_STRING_SIZE); - SERIALIZE(buffer, packet->clientInfo.avatar, PACKET_STRING_SIZE); -} - -void serializeRegionFormat(SerialPacket* packet, char* buffer) { - SERIALIZE(buffer, &packet->meta.type, sizeof(SerialPacket::Type)); - - //format - SERIALIZE(buffer, &packet->regionInfo.mapIndex, sizeof(int)); - SERIALIZE(buffer, &packet->regionInfo.x, sizeof(int)); - SERIALIZE(buffer, &packet->regionInfo.y, sizeof(int)); -} - -void serializeRegionContent(SerialPacket* packet, char* buffer) { - SERIALIZE(buffer, &packet->meta.type, sizeof(SerialPacket::Type)); - - //format - SERIALIZE(buffer, &packet->regionInfo.mapIndex, sizeof(int)); - SERIALIZE(buffer, &packet->regionInfo.x, sizeof(int)); - SERIALIZE(buffer, &packet->regionInfo.y, sizeof(int)); - - //content - for (register int i = 0; i < REGION_WIDTH; i++) { - for (register int j = 0; j < REGION_HEIGHT; j++) { - for (register int k = 0; k < REGION_DEPTH; k++) { - *reinterpret_cast(buffer) = packet->regionInfo.region->GetTile(i, j, k); - buffer += sizeof(Region::type_t); - } - } - } -} - -void serializeCombat(SerialPacket* packet, char* buffer) { - SERIALIZE(buffer, &packet->meta.type, sizeof(SerialPacket::Type)); - - //integers - SERIALIZE(buffer, &packet->combatInfo.combatIndex, sizeof(int)); - SERIALIZE(buffer, &packet->combatInfo.difficulty, sizeof(int)); - - SERIALIZE(buffer, &packet->combatInfo.terrainType, sizeof(CombatData::Terrain)); - - //arrays - SERIALIZE(buffer, &packet->combatInfo.characterArray, COMBAT_MAX_CHARACTERS); - SERIALIZE(buffer, &packet->combatInfo.enemyArray, COMBAT_MAX_ENEMIES); - - //position - SERIALIZE(buffer, &packet->combatInfo.mapIndex, sizeof(int)); - SERIALIZE(buffer, &packet->combatInfo.origin.x, sizeof(double)); - SERIALIZE(buffer, &packet->combatInfo.origin.y, sizeof(double)); - - //TODO: rewards -} - -void serializeStatistics(Statistics* stats, char* buffer) { - //integers - SERIALIZE(buffer, &stats->level, sizeof(int)); - SERIALIZE(buffer, &stats->exp, sizeof(int)); - SERIALIZE(buffer, &stats->maxHP, sizeof(int)); - SERIALIZE(buffer, &stats->health, sizeof(int)); - SERIALIZE(buffer, &stats->maxMP, sizeof(int)); - SERIALIZE(buffer, &stats->mana, sizeof(int)); - SERIALIZE(buffer, &stats->attack, sizeof(int)); - SERIALIZE(buffer, &stats->defence, sizeof(int)); - SERIALIZE(buffer, &stats->intelligence, sizeof(int)); - SERIALIZE(buffer, &stats->resistance, sizeof(int)); - SERIALIZE(buffer, &stats->speed, sizeof(int)); - - //floats - SERIALIZE(buffer, &stats->accuracy, sizeof(float)); - SERIALIZE(buffer, &stats->evasion, sizeof(float)); - SERIALIZE(buffer, &stats->luck, sizeof(float)); -} - -void serializeCharacter(SerialPacket* packet, char* buffer) { - SERIALIZE(buffer, &packet->meta.type, sizeof(SerialPacket::Type)); - - //indexes - SERIALIZE(buffer, &packet->characterInfo.clientIndex, sizeof(int)); - SERIALIZE(buffer, &packet->characterInfo.accountIndex, sizeof(int)); - SERIALIZE(buffer, &packet->characterInfo.characterIndex, sizeof(int)); - - //texts - SERIALIZE(buffer, packet->clientInfo.handle, PACKET_STRING_SIZE); - SERIALIZE(buffer, packet->clientInfo.avatar, PACKET_STRING_SIZE); - - //vectors - SERIALIZE(buffer, &packet->characterInfo.origin.x, sizeof(double)); - SERIALIZE(buffer, &packet->characterInfo.origin.y, sizeof(double)); - SERIALIZE(buffer, &packet->characterInfo.motion.x, sizeof(double)); - SERIALIZE(buffer, &packet->characterInfo.motion.y, sizeof(double)); - - //stats structure - serializeStatistics(&packet->characterInfo.stats, buffer); - buffer += sizeof(Statistics); -} - -void serializeEnemy(SerialPacket* packet, char* buffer) { - SERIALIZE(buffer, &packet->meta.type, sizeof(SerialPacket::Type)); - - //texts - SERIALIZE(buffer, packet->clientInfo.handle, PACKET_STRING_SIZE); - SERIALIZE(buffer, packet->clientInfo.avatar, PACKET_STRING_SIZE); - - //stats structure - serializeStatistics(&packet->characterInfo.stats, buffer); - buffer += sizeof(Statistics); -} - -//------------------------- -//internal deserialization functions -//------------------------- - -void deserializeType(SerialPacket* packet, char* buffer) { - DESERIALIZE(buffer, &packet->meta.type, sizeof(SerialPacket::Type)); -} - -void deserializeServer(SerialPacket* packet, char* buffer) { - DESERIALIZE(buffer, &packet->meta.type, sizeof(SerialPacket::Type)); - - //server info - DESERIALIZE(buffer, &packet->serverInfo.networkVersion, sizeof(int)); - DESERIALIZE(buffer, packet->serverInfo.name, PACKET_STRING_SIZE); - DESERIALIZE(buffer, &packet->serverInfo.playerCount, sizeof(int)); -} - -void deserializeClient(SerialPacket* packet, char* buffer) { - DESERIALIZE(buffer, &packet->meta.type, sizeof(SerialPacket::Type)); - - //indexes - DESERIALIZE(buffer, &packet->clientInfo.clientIndex, sizeof(int)); - DESERIALIZE(buffer, &packet->clientInfo.accountIndex, sizeof(int)); - DESERIALIZE(buffer, &packet->clientInfo.characterIndex, sizeof(int)); - - //texts - DESERIALIZE(buffer, packet->clientInfo.username, PACKET_STRING_SIZE); - //TODO: password - DESERIALIZE(buffer, packet->clientInfo.handle, PACKET_STRING_SIZE); - DESERIALIZE(buffer, packet->clientInfo.avatar, PACKET_STRING_SIZE); -} - -void deserializeRegionFormat(SerialPacket* packet, char* buffer) { - DESERIALIZE(buffer, &packet->meta.type, sizeof(SerialPacket::Type)); - - //format - DESERIALIZE(buffer, &packet->regionInfo.mapIndex, sizeof(int)); - DESERIALIZE(buffer, &packet->regionInfo.x, sizeof(int)); - DESERIALIZE(buffer, &packet->regionInfo.y, sizeof(int)); -} - -void deserializeRegionContent(SerialPacket* packet, char* buffer) { - DESERIALIZE(buffer, &packet->meta.type, sizeof(SerialPacket::Type)); - - //format - DESERIALIZE(buffer, &packet->regionInfo.mapIndex, sizeof(int)); - DESERIALIZE(buffer, &packet->regionInfo.x, sizeof(int)); - DESERIALIZE(buffer, &packet->regionInfo.y, sizeof(int)); - - //an object to work on - BlankAllocator().Create( - &packet->regionInfo.region, - packet->regionInfo.x, - packet->regionInfo.y - ); - - //content - for (register int i = 0; i < REGION_WIDTH; i++) { - for (register int j = 0; j < REGION_HEIGHT; j++) { - for (register int k = 0; k < REGION_DEPTH; k++) { - packet->regionInfo.region->SetTile(i, j, k, *reinterpret_cast(buffer)); - buffer += sizeof(Region::type_t); - } - } - } -} - - -void deserializeCombat(SerialPacket* packet, char* buffer) { - DESERIALIZE(buffer, &packet->meta.type, sizeof(SerialPacket::Type)); - - //integers - DESERIALIZE(buffer, &packet->combatInfo.combatIndex, sizeof(int)); - DESERIALIZE(buffer, &packet->combatInfo.difficulty, sizeof(int)); - - DESERIALIZE(buffer, &packet->combatInfo.terrainType, sizeof(CombatData::Terrain)); - - //arrays - DESERIALIZE(buffer, &packet->combatInfo.characterArray, COMBAT_MAX_CHARACTERS); - DESERIALIZE(buffer, &packet->combatInfo.enemyArray, COMBAT_MAX_ENEMIES); - - //position - DESERIALIZE(buffer, &packet->combatInfo.mapIndex, sizeof(int)); - DESERIALIZE(buffer, &packet->combatInfo.origin.x, sizeof(double)); - DESERIALIZE(buffer, &packet->combatInfo.origin.y, sizeof(double)); - - //TODO: rewards -} - - -void deserializeStatistics(Statistics* stats, char* buffer) { - //integers - DESERIALIZE(buffer, &stats->level, sizeof(int)); - DESERIALIZE(buffer, &stats->exp, sizeof(int)); - DESERIALIZE(buffer, &stats->maxHP, sizeof(int)); - DESERIALIZE(buffer, &stats->health, sizeof(int)); - DESERIALIZE(buffer, &stats->maxMP, sizeof(int)); - DESERIALIZE(buffer, &stats->mana, sizeof(int)); - DESERIALIZE(buffer, &stats->attack, sizeof(int)); - DESERIALIZE(buffer, &stats->defence, sizeof(int)); - DESERIALIZE(buffer, &stats->intelligence, sizeof(int)); - DESERIALIZE(buffer, &stats->resistance, sizeof(int)); - DESERIALIZE(buffer, &stats->speed, sizeof(int)); - - //floats - DESERIALIZE(buffer, &stats->accuracy, sizeof(float)); - DESERIALIZE(buffer, &stats->evasion, sizeof(float)); - DESERIALIZE(buffer, &stats->luck, sizeof(float)); -} - -void deserializeCharacter(SerialPacket* packet, char* buffer) { - DESERIALIZE(buffer, &packet->meta.type, sizeof(SerialPacket::Type)); - - //indexes - DESERIALIZE(buffer, &packet->characterInfo.clientIndex, sizeof(int)); - DESERIALIZE(buffer, &packet->characterInfo.accountIndex, sizeof(int)); - DESERIALIZE(buffer, &packet->characterInfo.characterIndex, sizeof(int)); - - //texts - DESERIALIZE(buffer, packet->clientInfo.handle, PACKET_STRING_SIZE); - DESERIALIZE(buffer, packet->clientInfo.avatar, PACKET_STRING_SIZE); - - //vectors - DESERIALIZE(buffer, &packet->characterInfo.origin.x, sizeof(double)); - DESERIALIZE(buffer, &packet->characterInfo.origin.y, sizeof(double)); - DESERIALIZE(buffer, &packet->characterInfo.motion.x, sizeof(double)); - DESERIALIZE(buffer, &packet->characterInfo.motion.y, sizeof(double)); - - //stats structure - deserializeStatistics(&packet->characterInfo.stats, buffer); - buffer += sizeof(Statistics); -} - -void deserializeEnemy(SerialPacket* packet, char* buffer) { - DESERIALIZE(buffer, &packet->meta.type, sizeof(SerialPacket::Type)); - - //texts - DESERIALIZE(buffer, packet->clientInfo.handle, PACKET_STRING_SIZE); - DESERIALIZE(buffer, packet->clientInfo.avatar, PACKET_STRING_SIZE); - - //stats structure - deserializeStatistics(&packet->characterInfo.stats, buffer); - buffer += sizeof(Statistics); -} - -//------------------------- -//the interface functions -//------------------------- - -void serialize(SerialPacket* packet, void* buffer) { - switch(packet->meta.type) { - //no extra data - case SerialPacket::Type::NONE: - case SerialPacket::Type::PING: - case SerialPacket::Type::PONG: - case SerialPacket::Type::BROADCAST_REQUEST: - - //all rejections - case SerialPacket::Type::BROADCAST_REJECTION: - case SerialPacket::Type::JOIN_REJECTION: - case SerialPacket::Type::REGION_REJECTION: - case SerialPacket::Type::CHARACTER_REJECTION: - case SerialPacket::Type::ENEMY_REJECTION: - case SerialPacket::Type::COMBAT_REJECTION: - - serializeType(packet, reinterpret_cast(buffer)); - break; - - //server info - case SerialPacket::Type::BROADCAST_RESPONSE: - serializeServer(packet, reinterpret_cast(buffer)); - break; - - //client info - case SerialPacket::Type::JOIN_REQUEST: - case SerialPacket::Type::JOIN_RESPONSE: - case SerialPacket::Type::SYNCHRONIZE: - case SerialPacket::Type::DISCONNECT: - case SerialPacket::Type::SHUTDOWN: - serializeClient(packet, reinterpret_cast(buffer)); - break; - - //region info - case SerialPacket::Type::REGION_REQUEST: - serializeRegionFormat(packet, reinterpret_cast(buffer)); - break; - - case SerialPacket::Type::REGION_CONTENT: - serializeRegionContent(packet, reinterpret_cast(buffer)); - break; - - //combat info - case SerialPacket::Type::COMBAT_NEW: - case SerialPacket::Type::COMBAT_DELETE: - case SerialPacket::Type::COMBAT_UPDATE: - - //TODO: is this the best fit? - case SerialPacket::Type::COMBAT_ENTER_REQUEST: - case SerialPacket::Type::COMBAT_ENTER_RESPONSE: - case SerialPacket::Type::COMBAT_EXIT_REQUEST: - case SerialPacket::Type::COMBAT_EXIT_RESPONSE: - - serializeCombat(packet, reinterpret_cast(buffer)); - break; - - //character info - case SerialPacket::Type::CHARACTER_NEW: - case SerialPacket::Type::CHARACTER_DELETE: - case SerialPacket::Type::CHARACTER_UPDATE: - case SerialPacket::Type::CHARACTER_STATS_REQUEST: - case SerialPacket::Type::CHARACTER_STATS_RESPONSE: - serializeCharacter(packet, reinterpret_cast(buffer)); - break; - - //enemy info - case SerialPacket::Type::ENEMY_NEW: - case SerialPacket::Type::ENEMY_DELETE: - case SerialPacket::Type::ENEMY_UPDATE: - case SerialPacket::Type::ENEMY_STATS_REQUEST: - case SerialPacket::Type::ENEMY_STATS_RESPONSE: - serializeEnemy(packet, reinterpret_cast(buffer)); - break; - } -} - -void deserialize(SerialPacket* packet, void* buffer) { - //find the type, so that you can actually deserialize the packet! - deserializeType(packet, reinterpret_cast(buffer)); - switch(packet->meta.type) { - //no extra data - case SerialPacket::Type::NONE: - case SerialPacket::Type::PING: - case SerialPacket::Type::PONG: - case SerialPacket::Type::BROADCAST_REQUEST: - - //all rejections - case SerialPacket::Type::BROADCAST_REJECTION: - case SerialPacket::Type::JOIN_REJECTION: - case SerialPacket::Type::REGION_REJECTION: - case SerialPacket::Type::CHARACTER_REJECTION: - case SerialPacket::Type::ENEMY_REJECTION: - case SerialPacket::Type::COMBAT_REJECTION: - - //NOTHING - break; - - //server info - case SerialPacket::Type::BROADCAST_RESPONSE: - deserializeServer(packet, reinterpret_cast(buffer)); - break; - - //client info - case SerialPacket::Type::JOIN_REQUEST: - case SerialPacket::Type::JOIN_RESPONSE: - case SerialPacket::Type::SYNCHRONIZE: - case SerialPacket::Type::DISCONNECT: - case SerialPacket::Type::SHUTDOWN: - deserializeClient(packet, reinterpret_cast(buffer)); - break; - - //region info - case SerialPacket::Type::REGION_REQUEST: - deserializeRegionFormat(packet, reinterpret_cast(buffer)); - break; - - case SerialPacket::Type::REGION_CONTENT: - deserializeRegionContent(packet, reinterpret_cast(buffer)); - break; - - //combat info - case SerialPacket::Type::COMBAT_NEW: - case SerialPacket::Type::COMBAT_DELETE: - case SerialPacket::Type::COMBAT_UPDATE: - - //TODO: is this the best fit? - case SerialPacket::Type::COMBAT_ENTER_REQUEST: - case SerialPacket::Type::COMBAT_ENTER_RESPONSE: - case SerialPacket::Type::COMBAT_EXIT_REQUEST: - case SerialPacket::Type::COMBAT_EXIT_RESPONSE: - - serializeCombat(packet, reinterpret_cast(buffer)); - break; - - //character info - case SerialPacket::Type::CHARACTER_NEW: - case SerialPacket::Type::CHARACTER_DELETE: - case SerialPacket::Type::CHARACTER_UPDATE: - case SerialPacket::Type::CHARACTER_STATS_REQUEST: - case SerialPacket::Type::CHARACTER_STATS_RESPONSE: - deserializeCharacter(packet, reinterpret_cast(buffer)); - break; - - //enemy info - case SerialPacket::Type::ENEMY_NEW: - case SerialPacket::Type::ENEMY_DELETE: - case SerialPacket::Type::ENEMY_UPDATE: - case SerialPacket::Type::ENEMY_STATS_REQUEST: - case SerialPacket::Type::ENEMY_STATS_RESPONSE: - serializeEnemy(packet, reinterpret_cast(buffer)); - break; - } -} \ No newline at end of file diff --git a/common/network/serial/makefile b/common/network/serial/makefile new file mode 100644 index 0000000..564b228 --- /dev/null +++ b/common/network/serial/makefile @@ -0,0 +1,37 @@ +#config +INCLUDES+=. ../packet ../../gameplay ../../map ../../utilities +LIBS+= +CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES)) + +#source +CXXSRC=$(wildcard *.cpp) + +#objects +OBJDIR=obj +OBJ+=$(addprefix $(OBJDIR)/,$(CXXSRC:.cpp=.o)) + +#output +OUTDIR=../../.. +OUT=$(addprefix $(OUTDIR)/,libcommon.a) + +#targets +all: $(OBJ) $(OUT) + ar -crs $(OUT) $(OBJ) + +$(OBJ): | $(OBJDIR) + +$(OUT): | $(OUTDIR) + +$(OBJDIR): + mkdir $(OBJDIR) + +$(OUTDIR): + mkdir $(OUTDIR) + +$(OBJDIR)/%.o: %.cpp + $(CXX) $(CXXFLAGS) -c -o $@ $< + +clean: + $(RM) *.o *.a *.exe + +rebuild: clean all diff --git a/common/network/serial/serial.cpp b/common/network/serial/serial.cpp new file mode 100644 index 0000000..c73b2bc --- /dev/null +++ b/common/network/serial/serial.cpp @@ -0,0 +1,188 @@ +/* Copyright: (c) Kayne Ruse 2014 + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any source + * distribution. +*/ +#include "serial.hpp" + +#include "serial_util.hpp" + +//simple type functions +void serializeType(SerialPacketBase* packet, void* buffer) { + SERIALIZE(buffer, &packet->type, sizeof(SerialPacketType)); +} + +void deserializeType(SerialPacketBase* packet, void* buffer) { + DESERIALIZE(buffer, &packet->type, sizeof(SerialPacketType)); +} + +//main switch functions +void serializePacket(SerialPacketBase* packet, void* buffer) { + switch(packet->type) { + //no extra data + case SerialPacketType::NONE: + case SerialPacketType::PING: + case SerialPacketType::PONG: + case SerialPacketType::BROADCAST_REQUEST: + + //all rejections + case SerialPacketType::BROADCAST_REJECTION: + case SerialPacketType::JOIN_REJECTION: + case SerialPacketType::REGION_REJECTION: + case SerialPacketType::CHARACTER_REJECTION: + case SerialPacketType::ENEMY_REJECTION: + case SerialPacketType::COMBAT_REJECTION: + + serializeType(packet, buffer); + break; + + //character info + case SerialPacketType::CHARACTER_NEW: + case SerialPacketType::CHARACTER_DELETE: + case SerialPacketType::CHARACTER_UPDATE: + case SerialPacketType::CHARACTER_STATS_REQUEST: + case SerialPacketType::CHARACTER_STATS_RESPONSE: + serializeCharacter(dynamic_cast(packet), buffer); + break; + + //client info + case SerialPacketType::JOIN_REQUEST: + case SerialPacketType::JOIN_RESPONSE: + case SerialPacketType::SYNCHRONIZE: + case SerialPacketType::DISCONNECT: + case SerialPacketType::SHUTDOWN: + serializeClient(dynamic_cast(packet), buffer); + break; + + //combat info + case SerialPacketType::COMBAT_NEW: + case SerialPacketType::COMBAT_DELETE: + case SerialPacketType::COMBAT_UPDATE: + + //TODO: is this the best fit? + case SerialPacketType::COMBAT_ENTER_REQUEST: + case SerialPacketType::COMBAT_ENTER_RESPONSE: + case SerialPacketType::COMBAT_EXIT_REQUEST: + case SerialPacketType::COMBAT_EXIT_RESPONSE: + + serializeCombat(dynamic_cast(packet), buffer); + break; + + //enemy info + case SerialPacketType::ENEMY_NEW: + case SerialPacketType::ENEMY_DELETE: + case SerialPacketType::ENEMY_UPDATE: + case SerialPacketType::ENEMY_STATS_REQUEST: + case SerialPacketType::ENEMY_STATS_RESPONSE: + serializeEnemy(dynamic_cast(packet), buffer); + break; + + //region info + case SerialPacketType::REGION_REQUEST: + serializeRegionFormat(dynamic_cast(packet), buffer); + break; + + case SerialPacketType::REGION_CONTENT: + serializeRegionContent(dynamic_cast(packet), buffer); + break; + + //server info + case SerialPacketType::BROADCAST_RESPONSE: + serializeServer(dynamic_cast(packet), buffer); + break; + } +} + +void deserializePacket(SerialPacketBase* packet, void* buffer) { + //find the type, so that you can actually deserialize the packet! + deserializeType(packet, buffer); + switch(packet->type) { + //no extra data + case SerialPacketType::NONE: + case SerialPacketType::PING: + case SerialPacketType::PONG: + case SerialPacketType::BROADCAST_REQUEST: + + //all rejections + case SerialPacketType::BROADCAST_REJECTION: + case SerialPacketType::JOIN_REJECTION: + case SerialPacketType::REGION_REJECTION: + case SerialPacketType::CHARACTER_REJECTION: + case SerialPacketType::ENEMY_REJECTION: + case SerialPacketType::COMBAT_REJECTION: + + //NOTHING + break; + + //character info + case SerialPacketType::CHARACTER_NEW: + case SerialPacketType::CHARACTER_DELETE: + case SerialPacketType::CHARACTER_UPDATE: + case SerialPacketType::CHARACTER_STATS_REQUEST: + case SerialPacketType::CHARACTER_STATS_RESPONSE: + deserializeCharacter(dynamic_cast(packet), buffer); + break; + + //client info + case SerialPacketType::JOIN_REQUEST: + case SerialPacketType::JOIN_RESPONSE: + case SerialPacketType::SYNCHRONIZE: + case SerialPacketType::DISCONNECT: + case SerialPacketType::SHUTDOWN: + deserializeClient(dynamic_cast(packet), buffer); + break; + + //combat info + case SerialPacketType::COMBAT_NEW: + case SerialPacketType::COMBAT_DELETE: + case SerialPacketType::COMBAT_UPDATE: + + //TODO: is this the best fit? + case SerialPacketType::COMBAT_ENTER_REQUEST: + case SerialPacketType::COMBAT_ENTER_RESPONSE: + case SerialPacketType::COMBAT_EXIT_REQUEST: + case SerialPacketType::COMBAT_EXIT_RESPONSE: + + serializeCombat(dynamic_cast(packet), buffer); + break; + + //enemy info + case SerialPacketType::ENEMY_NEW: + case SerialPacketType::ENEMY_DELETE: + case SerialPacketType::ENEMY_UPDATE: + case SerialPacketType::ENEMY_STATS_REQUEST: + case SerialPacketType::ENEMY_STATS_RESPONSE: + serializeEnemy(dynamic_cast(packet), buffer); + break; + + //region info + case SerialPacketType::REGION_REQUEST: + deserializeRegionFormat(dynamic_cast(packet), buffer); + break; + + case SerialPacketType::REGION_CONTENT: + deserializeRegionContent(dynamic_cast(packet), buffer); + break; + + //server info + case SerialPacketType::BROADCAST_RESPONSE: + deserializeServer(dynamic_cast(packet), buffer); + break; + } +} \ No newline at end of file diff --git a/common/network/serial/serial.hpp b/common/network/serial/serial.hpp new file mode 100644 index 0000000..02d5dba --- /dev/null +++ b/common/network/serial/serial.hpp @@ -0,0 +1,65 @@ +/* Copyright: (c) Kayne Ruse 2014 + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any source + * distribution. +*/ +#ifndef SERIALIZE_HPP_ +#define SERIALIZE_HPP_ + +#include "serial_packet.hpp" + +#include "region.hpp" +#include "statistics.hpp" + +//Primary interface functions +void serializePacket(SerialPacketBase*, void* dest); +void deserializePacket(SerialPacketBase*, void* src); + +void serializeType(SerialPacketBase*, void*); +void deserializeType(SerialPacketBase*, void*); + +//utility functions, exposed +void serializeCharacter(CharacterPacket*, void*); +void serializeClient(ClientPacket*, void*); +void serializeCombat(CombatPacket*, void*); +void serializeEnemy(EnemyPacket*, void*); +void serializeRegionFormat(RegionPacket*, void*); +void serializeRegionContent(RegionPacket*, void*); +void serializeServer(ServerPacket*, void*); +void serializeStatistics(Statistics*, void*); + +void deserializeCharacter(CharacterPacket*, void*); +void deserializeClient(ClientPacket*, void*); +void deserializeCombat(CombatPacket*, void*); +void deserializeEnemy(EnemyPacket*, void*); +void deserializeRegionFormat(RegionPacket*, void*); +void deserializeRegionContent(RegionPacket*, void*); +void deserializeServer(ServerPacket*, void*); +void deserializeStatistics(Statistics*, void*); + +/* DOCS: Keep the PACKET_BUFFER_SIZE up to date + * DOCS: REGION_CONTENT is currently the largest type of packet, read more + * map content: REGION_WIDTH * REGION_HEIGHT * REGION_DEPTH * sizoeof(region::type_t) + * map format: sizeof(int) * 3 + * metadata: sizeof(SerialPacket::Type) +*/ + +constexpr int PACKET_BUFFER_SIZE = REGION_WIDTH * REGION_HEIGHT * REGION_DEPTH * sizeof(Region::type_t) + sizeof(int) * 3 + sizeof(SerialPacketType); + +#endif \ No newline at end of file diff --git a/common/network/serial/serial_character.cpp b/common/network/serial/serial_character.cpp new file mode 100644 index 0000000..033944a --- /dev/null +++ b/common/network/serial/serial_character.cpp @@ -0,0 +1,44 @@ +/* Copyright: (c) Kayne Ruse 2014 + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any source + * distribution. +*/ +#include "serial.hpp" + +#include "serial_util.hpp" + +void serializeCharacter(CharacterPacket* packet, void* buffer) { + SERIALIZE(buffer, &packet->type, sizeof(SerialPacketType)); + + //TODO + + //stats structure + serializeStatistics(&packet->stats, buffer); + buffer = reinterpret_cast(buffer) + sizeof(Statistics); +} + +void deserializeCharacter(CharacterPacket* packet, void* buffer) { + DESERIALIZE(buffer, &packet->type, sizeof(SerialPacketType)); + + //TODO + + //stats structure + deserializeStatistics(&packet->stats, buffer); + buffer = reinterpret_cast(buffer) + sizeof(Statistics); +} diff --git a/common/network/serial/serial_client.cpp b/common/network/serial/serial_client.cpp new file mode 100644 index 0000000..4a818cf --- /dev/null +++ b/common/network/serial/serial_client.cpp @@ -0,0 +1,36 @@ +/* Copyright: (c) Kayne Ruse 2014 + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any source + * distribution. +*/ +#include "serial.hpp" + +#include "serial_util.hpp" + +void serializeClient(ClientPacket* packet, void* buffer) { + SERIALIZE(buffer, &packet->type, sizeof(SerialPacketType)); + + //TODO +} + +void deserializeClient(ClientPacket* packet, void* buffer) { + DESERIALIZE(buffer, &packet->type, sizeof(SerialPacketType)); + + //TODO +} diff --git a/common/network/serial/serial_combat.cpp b/common/network/serial/serial_combat.cpp new file mode 100644 index 0000000..6fbb053 --- /dev/null +++ b/common/network/serial/serial_combat.cpp @@ -0,0 +1,36 @@ +/* Copyright: (c) Kayne Ruse 2014 + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any source + * distribution. +*/ +#include "serial.hpp" + +#include "serial_util.hpp" + +void serializeCombat(CombatPacket* packet, void* buffer) { + SERIALIZE(buffer, &packet->type, sizeof(SerialPacketType)); + + //TODO +} + +void deserializeCombat(CombatPacket* packet, void* buffer) { + DESERIALIZE(buffer, &packet->type, sizeof(SerialPacketType)); + + //TODO +} diff --git a/common/network/serial/serial_enemy.cpp b/common/network/serial/serial_enemy.cpp new file mode 100644 index 0000000..4059ea5 --- /dev/null +++ b/common/network/serial/serial_enemy.cpp @@ -0,0 +1,44 @@ +/* Copyright: (c) Kayne Ruse 2014 + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any source + * distribution. +*/ +#include "serial.hpp" + +#include "serial_util.hpp" + +void serializeEnemy(EnemyPacket* packet, void* buffer) { + SERIALIZE(buffer, &packet->type, sizeof(SerialPacketType)); + + //TODO + + //stats structure + serializeStatistics(&packet->stats, buffer); + buffer = reinterpret_cast(buffer) + sizeof(Statistics); +} + +void deserializeEnemy(EnemyPacket* packet, void* buffer) { + DESERIALIZE(buffer, &packet->type, sizeof(SerialPacketType)); + + //TODO + + //stats structure + deserializeStatistics(&packet->stats, buffer); + buffer = reinterpret_cast(buffer) + sizeof(Statistics); +} diff --git a/common/network/serial/serial_region.cpp b/common/network/serial/serial_region.cpp new file mode 100644 index 0000000..e0860ac --- /dev/null +++ b/common/network/serial/serial_region.cpp @@ -0,0 +1,89 @@ +/* Copyright: (c) Kayne Ruse 2014 + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any source + * distribution. +*/ +#include "serial.hpp" + +#include "serial_util.hpp" + +#include "map_allocator.hpp" + +void serializeRegionFormat(RegionPacket* packet, void* buffer) { + SERIALIZE(buffer, &packet->type, sizeof(SerialPacketType)); + + //format + SERIALIZE(buffer, &packet->roomIndex, sizeof(int)); + SERIALIZE(buffer, &packet->x, sizeof(int)); + SERIALIZE(buffer, &packet->y, sizeof(int)); +} + +void serializeRegionContent(RegionPacket* packet, void* buffer) { + SERIALIZE(buffer, &packet->type, sizeof(SerialPacketType)); + + //format + SERIALIZE(buffer, &packet->roomIndex, sizeof(int)); + SERIALIZE(buffer, &packet->x, sizeof(int)); + SERIALIZE(buffer, &packet->y, sizeof(int)); + + //content + for (register int i = 0; i < REGION_WIDTH; i++) { + for (register int j = 0; j < REGION_HEIGHT; j++) { + for (register int k = 0; k < REGION_DEPTH; k++) { + *reinterpret_cast(buffer) = packet->region->GetTile(i, j, k); + buffer = reinterpret_cast(buffer) + sizeof(Region::type_t); + } + } + } +} + +void deserializeRegionFormat(RegionPacket* packet, void* buffer) { + DESERIALIZE(buffer, &packet->type, sizeof(SerialPacketType)); + + //format + DESERIALIZE(buffer, &packet->roomIndex, sizeof(int)); + DESERIALIZE(buffer, &packet->x, sizeof(int)); + DESERIALIZE(buffer, &packet->y, sizeof(int)); +} + +void deserializeRegionContent(RegionPacket* packet, void* buffer) { + DESERIALIZE(buffer, &packet->type, sizeof(SerialPacketType)); + + //format + DESERIALIZE(buffer, &packet->roomIndex, sizeof(int)); + DESERIALIZE(buffer, &packet->x, sizeof(int)); + DESERIALIZE(buffer, &packet->y, sizeof(int)); + + //an object to work on + BlankAllocator().Create( + &packet->region, + packet->x, + packet->y + ); + + //content + for (register int i = 0; i < REGION_WIDTH; i++) { + for (register int j = 0; j < REGION_HEIGHT; j++) { + for (register int k = 0; k < REGION_DEPTH; k++) { + packet->region->SetTile(i, j, k, *reinterpret_cast(buffer)); + buffer = reinterpret_cast(buffer) + sizeof(Region::type_t); + } + } + } +} \ No newline at end of file diff --git a/common/network/serial/serial_server.cpp b/common/network/serial/serial_server.cpp new file mode 100644 index 0000000..1f94dd4 --- /dev/null +++ b/common/network/serial/serial_server.cpp @@ -0,0 +1,36 @@ +/* Copyright: (c) Kayne Ruse 2014 + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any source + * distribution. +*/ +#include "serial.hpp" + +#include "serial_util.hpp" + +void serializeServer(ServerPacket* packet, void* buffer) { + SERIALIZE(buffer, &packet->type, sizeof(SerialPacketType)); + + //TODO +} + +void deserializeServer(ServerPacket* packet, void* buffer) { + DESERIALIZE(buffer, &packet->type, sizeof(SerialPacketType)); + + //TODO +} diff --git a/common/network/serial/serial_statistics.cpp b/common/network/serial/serial_statistics.cpp new file mode 100644 index 0000000..6a594a8 --- /dev/null +++ b/common/network/serial/serial_statistics.cpp @@ -0,0 +1,65 @@ +/* Copyright: (c) Kayne Ruse 2014 + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any source + * distribution. +*/ +#include "serial.hpp" + +#include "serial_util.hpp" + +void serializeStatistics(Statistics* stats, void* buffer) { + //integers + SERIALIZE(buffer, &stats->level, sizeof(int)); + SERIALIZE(buffer, &stats->exp, sizeof(int)); + SERIALIZE(buffer, &stats->maxHP, sizeof(int)); + SERIALIZE(buffer, &stats->health, sizeof(int)); + SERIALIZE(buffer, &stats->maxMP, sizeof(int)); + SERIALIZE(buffer, &stats->mana, sizeof(int)); + SERIALIZE(buffer, &stats->attack, sizeof(int)); + SERIALIZE(buffer, &stats->defence, sizeof(int)); + SERIALIZE(buffer, &stats->intelligence, sizeof(int)); + SERIALIZE(buffer, &stats->resistance, sizeof(int)); + SERIALIZE(buffer, &stats->speed, sizeof(int)); + + //floats + SERIALIZE(buffer, &stats->accuracy, sizeof(float)); + SERIALIZE(buffer, &stats->evasion, sizeof(float)); + SERIALIZE(buffer, &stats->luck, sizeof(float)); +} + + +void deserializeStatistics(Statistics* stats, void* buffer) { + //integers + DESERIALIZE(buffer, &stats->level, sizeof(int)); + DESERIALIZE(buffer, &stats->exp, sizeof(int)); + DESERIALIZE(buffer, &stats->maxHP, sizeof(int)); + DESERIALIZE(buffer, &stats->health, sizeof(int)); + DESERIALIZE(buffer, &stats->maxMP, sizeof(int)); + DESERIALIZE(buffer, &stats->mana, sizeof(int)); + DESERIALIZE(buffer, &stats->attack, sizeof(int)); + DESERIALIZE(buffer, &stats->defence, sizeof(int)); + DESERIALIZE(buffer, &stats->intelligence, sizeof(int)); + DESERIALIZE(buffer, &stats->resistance, sizeof(int)); + DESERIALIZE(buffer, &stats->speed, sizeof(int)); + + //floats + DESERIALIZE(buffer, &stats->accuracy, sizeof(float)); + DESERIALIZE(buffer, &stats->evasion, sizeof(float)); + DESERIALIZE(buffer, &stats->luck, sizeof(float)); +} diff --git a/common/network/serial.hpp b/common/network/serial/serial_util.hpp similarity index 60% rename from common/network/serial.hpp rename to common/network/serial/serial_util.hpp index 29b9c2d..a0910b8 100644 --- a/common/network/serial.hpp +++ b/common/network/serial/serial_util.hpp @@ -19,20 +19,13 @@ * 3. This notice may not be removed or altered from any source * distribution. */ -#ifndef SERIAL_HPP_ -#define SERIAL_HPP_ +#ifndef SERIALIZEUTIL_HPP_ +#define SERIALIZEUTIL_HPP_ -#include "serial_packet.hpp" +#include -/* NOTE: Keep the PACKET_BUFFER_SIZE up to date - * NOTE: REGION_CONTENT is currently the largest type of packet - * map content: REGION_WIDTH * REGION_HEIGHT * REGION_DEPTH * sizoeof(region::type_t) - * map format: sizeof(int) * 3 - * metadata: sizeof(SerialPacket::Type) -*/ -#define PACKET_BUFFER_SIZE REGION_WIDTH * REGION_HEIGHT * REGION_DEPTH * sizeof(Region::type_t) + sizeof(int) * 3 + sizeof(SerialPacket::Type) - -void serialize(SerialPacket* const, void* dest); -void deserialize(SerialPacket* const, void* src); +//NOTE: The strange assignments here used in order to move the void* parameter +#define SERIALIZE(buffer, data, size) memcpy(buffer, data, size); buffer = reinterpret_cast(buffer) + size; +#define DESERIALIZE(buffer, data, size) memcpy(data, buffer, size); buffer = reinterpret_cast(buffer) + size; #endif