From cc57e03bc45ed13eb13505b5b46c1b0716348a35 Mon Sep 17 00:00:00 2001 From: Persson-dev Date: Thu, 10 Jul 2025 16:34:28 +0200 Subject: [PATCH] splib transition --- include/td/common/Array.h | 31 +++ include/td/game/Game.h | 14 + include/td/game/GameHistory.h | 3 +- include/td/protocol/command/CommandData.h | 22 +- include/td/protocol/command/CommandDeclare.h | 22 -- .../td/protocol/command/CommandDispatcher.h | 17 -- include/td/protocol/command/CommandFactory.h | 19 -- .../td/protocol/command/CommandSerializer.h | 21 -- include/td/protocol/command/CommandVisitor.h | 39 --- include/td/protocol/command/Commands.h | 116 +++----- include/td/protocol/packet/PacketData.h | 3 +- include/td/protocol/packet/PacketDeclare.h | 48 ---- include/td/protocol/packet/PacketDispatcher.h | 17 -- include/td/protocol/packet/PacketFactory.h | 19 -- include/td/protocol/packet/PacketSerializer.h | 21 -- include/td/protocol/packet/PacketVisitor.h | 39 --- include/td/protocol/packet/Packets.h | 124 +++------ src/main.cpp | 11 +- src/td/game/Game.cpp | 1 + src/td/game/GameHistory.cpp | 6 +- src/td/protocol/command/CommandFactory.cpp | 24 -- src/td/protocol/command/CommandSerializer.cpp | 229 --------------- src/td/protocol/command/CommandVisitor.cpp | 11 - src/td/protocol/command/Commands.cpp | 18 -- src/td/protocol/packet/PacketFactory.cpp | 26 -- src/td/protocol/packet/PacketSerializer.cpp | 262 ------------------ src/td/protocol/packet/PacketVisitor.cpp | 11 - src/td/protocol/packet/Packets.cpp | 23 -- xmake.lua | 76 +---- 29 files changed, 146 insertions(+), 1127 deletions(-) create mode 100644 include/td/common/Array.h create mode 100644 include/td/game/Game.h delete mode 100644 include/td/protocol/command/CommandDeclare.h delete mode 100644 include/td/protocol/command/CommandDispatcher.h delete mode 100644 include/td/protocol/command/CommandFactory.h delete mode 100644 include/td/protocol/command/CommandSerializer.h delete mode 100644 include/td/protocol/command/CommandVisitor.h delete mode 100644 include/td/protocol/packet/PacketDeclare.h delete mode 100644 include/td/protocol/packet/PacketDispatcher.h delete mode 100644 include/td/protocol/packet/PacketFactory.h delete mode 100644 include/td/protocol/packet/PacketSerializer.h delete mode 100644 include/td/protocol/packet/PacketVisitor.h create mode 100644 src/td/game/Game.cpp delete mode 100644 src/td/protocol/command/CommandFactory.cpp delete mode 100644 src/td/protocol/command/CommandSerializer.cpp delete mode 100644 src/td/protocol/command/CommandVisitor.cpp delete mode 100644 src/td/protocol/command/Commands.cpp delete mode 100644 src/td/protocol/packet/PacketFactory.cpp delete mode 100644 src/td/protocol/packet/PacketSerializer.cpp delete mode 100644 src/td/protocol/packet/PacketVisitor.cpp delete mode 100644 src/td/protocol/packet/Packets.cpp diff --git a/include/td/common/Array.h b/include/td/common/Array.h new file mode 100644 index 0000000..10acb9b --- /dev/null +++ b/include/td/common/Array.h @@ -0,0 +1,31 @@ +#pragma once + +#include + +namespace td { +template +class Array { + private: + T* m_Data; + + public: + Array() : m_Data(new T[S]) {} + + Array(const std::initializer_list& args) { + std::size_t i = 0; + for(const T& element : args) { + m_Data[i] = element; + i++; + } + } + + T& operator[](std::size_t a_Index) { + return m_Data[a_Index]; + } + + ~Array() { + delete [] m_Data; + } +}; + +} // namespace td diff --git a/include/td/game/Game.h b/include/td/game/Game.h new file mode 100644 index 0000000..0928249 --- /dev/null +++ b/include/td/game/Game.h @@ -0,0 +1,14 @@ +#pragma once + +#include + +namespace td { +namespace game { + +class Game { + private: + GameHistory m_History; +}; + +} // namespace game +} // namespace td diff --git a/include/td/game/GameHistory.h b/include/td/game/GameHistory.h index c5c4f61..73cda9c 100644 --- a/include/td/game/GameHistory.h +++ b/include/td/game/GameHistory.h @@ -1,5 +1,6 @@ #pragma once +#define BOOST_PFR_USE_CPP17 0 #include #include #include @@ -30,7 +31,7 @@ class GameHistory { void FromPacket(td::protocol::pdata::LockSteps&& a_Steps); - td::protocol::packets::LockSteps ToPacket(HistorySizeType a_StartIndex); + td::protocol::packets::LockStepsPacket ToPacket(HistorySizeType a_StartIndex); }; } // namespace game diff --git a/include/td/protocol/command/CommandData.h b/include/td/protocol/command/CommandData.h index f4e0aac..ed3e850 100644 --- a/include/td/protocol/command/CommandData.h +++ b/include/td/protocol/command/CommandData.h @@ -3,40 +3,40 @@ #include #include #include +#include namespace td { namespace protocol { namespace cdata { - struct PlaceTower { - TowerType m_Type : 4; - PlayerID m_Placer : 4; + sp::BitField m_Type; + sp::BitField m_Placer; TowerCoords m_Position; }; struct UpgradeTower { - TowerID m_Tower : 12; - std::uint8_t m_Upgrade : 4; + sp::BitField m_Tower; + sp::BitField m_Upgrade; }; struct SpawnTroop { - EntityType m_Type : 5; - std::uint8_t m_Level : 3; + sp::BitField m_Type; + sp::BitField m_Level; EntityCoords m_Position; PlayerID m_Sender; }; struct UseItem { - ShopItem m_Item : 4; - PlayerID m_User : 4; + sp::BitField m_Item; + sp::BitField m_User; EntityCoords m_Position; }; struct TeamChange { - PlayerID m_Player : 7; - Team m_NewTeam : 1; + sp::BitField m_Player; + sp::BitField m_NewTeam; }; struct PlayerJoin { diff --git a/include/td/protocol/command/CommandDeclare.h b/include/td/protocol/command/CommandDeclare.h deleted file mode 100644 index 76013d7..0000000 --- a/include/td/protocol/command/CommandDeclare.h +++ /dev/null @@ -1,22 +0,0 @@ -#pragma once - - -namespace td { -namespace protocol { - -/** - * \def DeclareAllPacket - * \brief Avoids repetitive operations on commands - */ -#define DeclareAllCommand() \ - DeclareCommand(End) \ - DeclareCommand(PlaceTower) \ - DeclareCommand(PlayerJoin) \ - DeclareCommand(SpawnTroop) \ - DeclareCommand(TeamChange) \ - DeclareCommand(UpgradeTower) \ - DeclareCommand(UseItem) - - -} // namespace protocol -} // namespace td \ No newline at end of file diff --git a/include/td/protocol/command/CommandDispatcher.h b/include/td/protocol/command/CommandDispatcher.h deleted file mode 100644 index 0dc3ef9..0000000 --- a/include/td/protocol/command/CommandDispatcher.h +++ /dev/null @@ -1,17 +0,0 @@ -#pragma once - -/** - * \file CommandDispatcher.h - * \brief File containing the td::protocol::CommandDispatcher class - */ - -#include -#include - -namespace td { -namespace protocol { - -using CommandDispatcher = Dispatcher; - -} // namespace protocol -} // namespace td diff --git a/include/td/protocol/command/CommandFactory.h b/include/td/protocol/command/CommandFactory.h deleted file mode 100644 index 3bae063..0000000 --- a/include/td/protocol/command/CommandFactory.h +++ /dev/null @@ -1,19 +0,0 @@ -#pragma once - -#include -#include - -namespace td { -namespace protocol { -namespace CommandFactory { - -template ::value>::type> -std::shared_ptr CreateCommand() { - return std::make_shared(); -} - -const std::shared_ptr& CreateReadOnlyCommand(CommandType a_Type); - -} // namespace CommandFactory -} // namespace protocol -} // namespace td diff --git a/include/td/protocol/command/CommandSerializer.h b/include/td/protocol/command/CommandSerializer.h deleted file mode 100644 index 181fcb3..0000000 --- a/include/td/protocol/command/CommandSerializer.h +++ /dev/null @@ -1,21 +0,0 @@ -#pragma once - -#include -#include - -namespace td { -namespace protocol { - -class Command; - -using CommandPtr = std::shared_ptr; - -namespace CommandSerializer { - -DataBuffer Serialize(const Command& a_Command); - -std::shared_ptr Deserialize(DataBuffer& a_Data); - -} // namespace CommandSerializer -} // namespace protocol -} // namespace td diff --git a/include/td/protocol/command/CommandVisitor.h b/include/td/protocol/command/CommandVisitor.h deleted file mode 100644 index 987ffd3..0000000 --- a/include/td/protocol/command/CommandVisitor.h +++ /dev/null @@ -1,39 +0,0 @@ -#pragma once - -/** - * \file CommandVisitor.h - * \brief File containing the td::protocol::CommandVisitor class - */ - -#include - -namespace td { -namespace protocol { - -#define DeclareCommand(CommandName, ...) \ - /** This function is called when the packet processed by CommandVisitor::Check is a CommandName */ \ - virtual void Visit(const commands::CommandName&) {} - -/** - * \class CommandVisitor - * \brief This class uses double-dispatch in order to find the real type of a packet - */ -class CommandVisitor : private NonCopyable { - protected: - CommandVisitor() {} - virtual ~CommandVisitor() {} - - public: - /** - * \brief Calls the right CommandVisitor::Visit method corresponding to the real type of the packet - * \param packet the Command to visit - */ - void Check(const Command& packet); - - DeclareAllCommand() -}; - -#undef DeclareCommand - -} // namespace protocol -} // namespace td diff --git a/include/td/protocol/command/Commands.h b/include/td/protocol/command/Commands.h index 4444d21..87f5c4e 100644 --- a/include/td/protocol/command/Commands.h +++ b/include/td/protocol/command/Commands.h @@ -5,107 +5,59 @@ * \brief File containing the definitions of the lockstep commands */ +#include +#include +#include +#include +#include #include #include #include -#include -#include namespace td { namespace protocol { -class CommandVisitor; - /** A Command id is 8 bits wide */ -using CommandID = std::uint8_t; - -#define DeclareCommand(CommandName, ...) /** CommandName */ CommandName, - -/** - * \enum CommandType - * \brief Map a Command to an id - */ -enum class CommandType : CommandID { - - DeclareAllCommand() - - /** The number of Commands */ - COMMAND_COUNT -}; - - -#undef DeclareCommand - - -class Command : private NonCopyable { - public: - /** - * \return The real type of the Command - */ - virtual CommandType GetType() const = 0; - - private: - /** Use a CommandVisitor to make double-dispatch possible */ - virtual void Accept(CommandVisitor& a_Visitor) const = 0; - - friend class CommandVisitor; +enum class CommandID : std::uint8_t { + End = 0, + PlaceTower, + PlayerJoin, + SpawnTroop, + TeamChange, + UpgradeTower, + UseItem, }; +class CommandHandler; +using CommandBase = sp::MessageBase; +template +using CommandMessage = sp::ConcreteMessage; namespace commands { -/** - * \class ConcreteCommand - * \brief A Command associated with an id and holding data - * \tparam PT The Command type - * \tparam Data The structure holding the data of the Command (in td::protocol::data namespace) - */ -template -class ConcreteCommand : public Command { - public: - /** The type of the struct holding the data */ - using CommandDataType = Data; - - /** The structure holding the actual data */ - CommandDataType m_Data; - - /** Construct the Command with data of type CommandDataType */ - ConcreteCommand(const CommandDataType& a_Data = {}); - - constexpr CommandType GetType() const override { - return CT; - }; - - private: - void Accept(CommandVisitor& a_Visitor) const override; -}; - - - - - -// define TD_INSTANCIATE_COMMANDS -// before including this file -// if you want to instantiate templates -#ifdef TD_INSTANCIATE_COMMANDS -#define DeclareCommand(CommandName, ...) \ - using CommandName = ConcreteCommand; \ - template class ConcreteCommand; -#else -#define DeclareCommand(CommandName, ...) /** Defines the CommandName Command */ \ - using CommandName = ConcreteCommand; -#endif - -DeclareAllCommand() - -#undef DeclareCommand +using EndCommand = CommandMessage; +using PlaceTowerCommand = CommandMessage; +using PlayerJoinCommand = CommandMessage; +using SpawnTroopCommand = CommandMessage; +using TeamChangeCommand = CommandMessage; +using UpgradeTowerCommand = CommandMessage; +using UseItemCommand = CommandMessage; } // namespace commands -using LockStep = std::vector>; +using AllCommands = std::tuple; + +class CommandHandler : public sp::MessageHandler {}; + +using CommandDispatcher = sp::MessageDispatcher; + +using CommandFactory = sp::MessageFactory; + +using LockStep = std::vector>; } // namespace protocol } // namespace td diff --git a/include/td/protocol/packet/PacketData.h b/include/td/protocol/packet/PacketData.h index fdcfb62..3bd37aa 100644 --- a/include/td/protocol/packet/PacketData.h +++ b/include/td/protocol/packet/PacketData.h @@ -3,6 +3,7 @@ #include #include #include +#include // Make it dynamic ? #define LOCKSTEP_BUFFER_SIZE 10 @@ -68,7 +69,7 @@ struct BeginGame { struct LockSteps { std::uint16_t m_FirstFrameNumber; - std::array m_LockSteps; + Array m_LockSteps; }; } // namespace pdata diff --git a/include/td/protocol/packet/PacketDeclare.h b/include/td/protocol/packet/PacketDeclare.h deleted file mode 100644 index 1354d6d..0000000 --- a/include/td/protocol/packet/PacketDeclare.h +++ /dev/null @@ -1,48 +0,0 @@ -#pragma once - -/** - * \file PacketDeclare.h - * \brief Holds the definitions of the packets (but not their content) - */ - -namespace td { -namespace protocol { - -/** - * \enum PacketSender - * \brief Indicate who should send a packet - */ -enum class PacketSenderType { - /** Sent by clients and server */ - Both = 1, - /** Sent by clients to the server */ - Client, - /** Sent by server to the clients */ - Server, -}; - -enum class PacketSendType { - Reliable = 1, - Unreliable, - UnreliableOrdered, -}; - - -/** - * \def DeclareAllPacket - * \brief Avoids repetitive operations on packets - */ -#define DeclareAllPacket() \ - DeclarePacket(ChatMessage, Reliable, Both) \ - DeclarePacket(BeginGame, Reliable, Server) \ - DeclarePacket(Disconnect, Reliable, Both) \ - DeclarePacket(KeepAlive, Reliable, Both) \ - DeclarePacket(LockSteps, Unreliable, Both) \ - DeclarePacket(LoggingSuccess, Reliable, Server) \ - DeclarePacket(PlayerJoin, Reliable, Server) \ - DeclarePacket(PlayerLeave, Reliable, Server) \ - DeclarePacket(PlayerLogin, Reliable, Client) \ - - -} // namespace protocol -} // namespace td \ No newline at end of file diff --git a/include/td/protocol/packet/PacketDispatcher.h b/include/td/protocol/packet/PacketDispatcher.h deleted file mode 100644 index d0a5d21..0000000 --- a/include/td/protocol/packet/PacketDispatcher.h +++ /dev/null @@ -1,17 +0,0 @@ -#pragma once - -/** - * \file PacketDispatcher.h - * \brief File containing the td::protocol::PacketDispatcher class - */ - -#include -#include - -namespace td { -namespace protocol { - -using PacketDispatcher = Dispatcher; - -} // namespace protocol -} // namespace td diff --git a/include/td/protocol/packet/PacketFactory.h b/include/td/protocol/packet/PacketFactory.h deleted file mode 100644 index 0f346ad..0000000 --- a/include/td/protocol/packet/PacketFactory.h +++ /dev/null @@ -1,19 +0,0 @@ -#pragma once - -#include -#include - -namespace td { -namespace protocol { -namespace PacketFactory { - -template ::value>::type> -std::unique_ptr CreatePacket() { - return std::make_unique(); -} - -const std::unique_ptr& CreateReadOnlyPacket(PacketType a_Type); - -} // namespace PacketFactory -} // namespace protocol -} // namespace td diff --git a/include/td/protocol/packet/PacketSerializer.h b/include/td/protocol/packet/PacketSerializer.h deleted file mode 100644 index fd71ca2..0000000 --- a/include/td/protocol/packet/PacketSerializer.h +++ /dev/null @@ -1,21 +0,0 @@ -#pragma once - -#include -#include - -namespace td { -namespace protocol { - -class Packet; - -using PacketPtr = std::unique_ptr; - -namespace PacketSerializer { - -DataBuffer Serialize(const Packet& a_Packet); - -std::unique_ptr Deserialize(DataBuffer& a_Data); - -} // namespace PacketSerializer -} // namespace protocol -} // namespace td diff --git a/include/td/protocol/packet/PacketVisitor.h b/include/td/protocol/packet/PacketVisitor.h deleted file mode 100644 index d2a6799..0000000 --- a/include/td/protocol/packet/PacketVisitor.h +++ /dev/null @@ -1,39 +0,0 @@ -#pragma once - -/** - * \file PacketVisitor.h - * \brief File containing the td::protocol::PacketVisitor class - */ - -#include - -namespace td { -namespace protocol { - -#define DeclarePacket(PacketName, ...) \ - /** This function is called when the packet processed by PacketVisitor::Check is a PacketName */ \ - virtual void Visit(const packets::PacketName&) {} - -/** - * \class PacketVisitor - * \brief This class uses double-dispatch in order to find the real type of a packet - */ -class PacketVisitor : private NonCopyable { - protected: - PacketVisitor() {} - virtual ~PacketVisitor() {} - - public: - /** - * \brief Calls the right PacketVisitor::Visit method corresponding to the real type of the packet - * \param packet the Packet to visit - */ - void Check(const Packet& packet); - - DeclareAllPacket() -}; - -#undef DeclarePacket - -} // namespace protocol -} // namespace td diff --git a/include/td/protocol/packet/Packets.h b/include/td/protocol/packet/Packets.h index 24599fe..92822e1 100644 --- a/include/td/protocol/packet/Packets.h +++ b/include/td/protocol/packet/Packets.h @@ -7,107 +7,61 @@ #include #include -#include + +#include +#include +#include +#include +#include namespace td { namespace protocol { -class PacketVisitor; - -/** A Packet id is 8 bits wide */ -using PacketID = std::uint8_t; -using PeerID = std::uint16_t; - -#define DeclarePacket(PacketName, ...) /** PacketName */ PacketName, - -/** - * \enum PacketType - * \brief Map a Packet to an id - */ -enum class PacketType : PacketID { - - DeclareAllPacket() - - /** The number of packets */ - PACKET_COUNT +enum class PacketID : std::uint8_t { + ChatMessage = 0, + BeginGame, + Disconnect, + KeepAlive, + LockSteps, + LoggingSuccess, + PlayerJoin, + PlayerLeave, + PlayerLogin }; +class PacketHandler; -#undef DeclarePacket - - -class Packet : private NonCopyable { - public: - /** - * \return The real type of the packet - */ - virtual PacketType GetType() const = 0; - - /** - * \brief The network peer who sent the packet - */ - PeerID m_Sender; - - private: - /** Use a PacketVisitor to make double-dispatch possible */ - virtual void Accept(PacketVisitor& a_Visitor) const = 0; - - friend class PacketVisitor; -}; - +using PacketBase = sp::MessageBase; +template +using PacketMessage = sp::ConcreteMessage; namespace packets { -/** - * \class ConcretePacket - * \brief A Packet associated with an id and holding data - * \tparam PT The packet type - * \tparam Data The structure holding the data of the packet (in td::protocol::data namespace) - */ -template -class ConcretePacket : public Packet { - public: - /** The type of the struct holding the data */ - using PacketDataType = Data; +using BeginGamePacket = PacketMessage; +using ChatMessagePacket = PacketMessage; +using DisconnectPacket = PacketMessage; +using KeepAlivePacket = PacketMessage; +using LockStepsPacket = PacketMessage; +using LoggingSuccessPacket = PacketMessage; +using PlayerJoinPacket = PacketMessage; +using PlayerLeavePacket = PacketMessage; +using PlayerLoginPacket = PacketMessage; - /** The structure holding the actual data */ - PacketDataType m_Data; - - /** Construct the packet with data of type PacketDataType */ - ConcretePacket(const PacketDataType& a_Data = {}); - - constexpr PacketType GetType() const override { - return PT; - }; - - private: - void Accept(PacketVisitor& a_Visitor) const override; -}; - - - - - -// define TD_INSTANCIATE_PACKETS -// before including this file -// if you want to instantiate templates -#ifdef TD_INSTANCIATE_PACKETS -#define DeclarePacket(PacketName, ...) \ - using PacketName = ConcretePacket; \ - template class ConcretePacket; -#else -#define DeclarePacket(PacketName, ...) /** Defines the PacketName packet */ \ - using PacketName = ConcretePacket; -#endif - -DeclareAllPacket() - -#undef DeclarePacket } // namespace packets +using AllPackets = std::tuple; + +class PacketHandler : public sp::MessageHandler {}; + +using PacketDispatcher = sp::MessageDispatcher; + +using PacketFactory = sp::MessageFactory; + } // namespace protocol } // namespace td diff --git a/src/main.cpp b/src/main.cpp index 9a466a1..80fa251 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,12 +1,9 @@ #include // #include #include -#include -#include -#include -#include +#include -class Test : public td::protocol::CommandVisitor {}; +class Test : public td::protocol::CommandHandler {}; int main(int argc, char** argv) { // Test visitor; @@ -14,8 +11,8 @@ int main(int argc, char** argv) { // visitor.Check(chat); - td::protocol::commands::UpgradeTower com{{1, 2}}; - std::cout << (unsigned)com.GetType() << std::endl; + td::protocol::commands::UpgradeTowerCommand com{1, 2}; + std::cout << (unsigned)com.GetId() << std::endl; td::protocol::CommandDispatcher disptacher; return 0; } diff --git a/src/td/game/Game.cpp b/src/td/game/Game.cpp new file mode 100644 index 0000000..c3677ec --- /dev/null +++ b/src/td/game/Game.cpp @@ -0,0 +1 @@ +#include \ No newline at end of file diff --git a/src/td/game/GameHistory.cpp b/src/td/game/GameHistory.cpp index 2ed127e..f295c77 100644 --- a/src/td/game/GameHistory.cpp +++ b/src/td/game/GameHistory.cpp @@ -32,12 +32,12 @@ void GameHistory::FromPacket(td::protocol::pdata::LockSteps&& a_Steps) { } } -td::protocol::packets::LockSteps GameHistory::ToPacket(HistorySizeType a_StartIndex) { - std::array steps; +td::protocol::packets::LockStepsPacket GameHistory::ToPacket(HistorySizeType a_StartIndex) { + Array steps; for (int i = 0; i < LOCKSTEP_BUFFER_SIZE; i++) { steps[i] = GetLockStep(a_StartIndex + i); } - return {{a_StartIndex, std::move(steps)}}; + return {a_StartIndex, std::move(steps)}; } } // namespace game diff --git a/src/td/protocol/command/CommandFactory.cpp b/src/td/protocol/command/CommandFactory.cpp deleted file mode 100644 index 0dbdc9f..0000000 --- a/src/td/protocol/command/CommandFactory.cpp +++ /dev/null @@ -1,24 +0,0 @@ -#include - -#include -#include -#include - -namespace td { -namespace protocol { -namespace CommandFactory { - -using CommandCreator = std::function()>; - -#define DeclareCommand(CommandName, ...) std::make_shared(), - -static std::array, static_cast(CommandType::COMMAND_COUNT)> Commands = {DeclareAllCommand()}; - -const std::shared_ptr& CreateReadOnlyCommand(CommandType a_Type) { - assert(a_Type < CommandType::COMMAND_COUNT); - return Commands[static_cast(a_Type)]; -} - -} // namespace CommandFactory -} // namespace protocol -} // namespace td diff --git a/src/td/protocol/command/CommandSerializer.cpp b/src/td/protocol/command/CommandSerializer.cpp deleted file mode 100644 index 80b1d3d..0000000 --- a/src/td/protocol/command/CommandSerializer.cpp +++ /dev/null @@ -1,229 +0,0 @@ -#include - -#include -#include - -#include -#include - -namespace td { -namespace protocol { -namespace CommandSerializer { - -#define DeclareCommand(CommandName, ...) \ - void Visit(const commands::CommandName& a_Command) override { \ - const auto& commandData = a_Command.m_Data; \ - SerializeCommandData(commandData); \ - } \ - \ - void SerializeCommandData(const commands::CommandName::CommandDataType& a_Command); - - - - -class Serializer : public CommandVisitor { - private: - DataBuffer& m_Buffer; - - public: - Serializer(DataBuffer& a_Buffer) : m_Buffer(a_Buffer) {} - - void Serialize(const Command& a_Command) { - m_Buffer << static_cast(a_Command.GetType()); - Check(a_Command); - } - - DeclareAllCommand() -}; - -#undef DeclareCommand - - - - - -#define DeclareCommand(CommandName, ...) \ - void Visit(const commands::CommandName& a_Command) override { \ - auto commandPtr = CommandFactory::CreateCommand(); \ - auto& commandData = commandPtr->m_Data; \ - \ - DeserializeCommandData(commandData); \ - \ - m_Command = std::move(commandPtr); \ - } \ - \ - void DeserializeCommandData(commands::CommandName::CommandDataType& a_Command); - - - -class Deserializer : public CommandVisitor { - private: - DataBuffer& m_Buffer; - CommandPtr m_Command; - - public: - Deserializer(DataBuffer& a_Buffer) : m_Buffer(a_Buffer) {} - - bool Deserialize(const CommandPtr& a_Command) { - try { - Check(*a_Command.get()); - } catch (std::exception& e) { - utils::LOGE(utils::Format("[CommandSerializer::Deserializer] %s", e.what())); - return false; - } - return true; - } - - CommandPtr& GetCommand() { - return m_Command; - } - - DeclareAllCommand() -}; - - - - - -DataBuffer Serialize(const Command& a_Command) { - DataBuffer buffer; - - Serializer serializer(buffer); - serializer.Serialize(a_Command); - - return buffer; -} - -std::shared_ptr Deserialize(DataBuffer& a_Data) { - CommandID commandId; - a_Data >> commandId; - - if (commandId >= static_cast(CommandType::COMMAND_COUNT)) - return nullptr; - - CommandType commandType = CommandType(commandId); - - // for double-dispatch - const CommandPtr& emptyCommand = CommandFactory::CreateReadOnlyCommand(commandType); - - Deserializer deserializer(a_Data); - - if (deserializer.Deserialize(emptyCommand)) { - CommandPtr command = std::move(deserializer.GetCommand()); - return command; - } - - return nullptr; -} - - - - - -//--------------------------------------------- -// Command serializer implementation -//---------------------------------------------- - - - - - -void Serializer::SerializeCommandData(const cdata::End& a_Command) {} - -void Deserializer::DeserializeCommandData(cdata::End& a_Command) {} - - - - - -void Serializer::SerializeCommandData(const cdata::PlaceTower& a_Command) { - m_Buffer << static_cast((static_cast(a_Command.m_Type) << 4 | a_Command.m_Placer & 0xF)) - << a_Command.m_Position; -} - -void Deserializer::DeserializeCommandData(cdata::PlaceTower& a_Command) { - std::uint8_t union1; - m_Buffer >> union1 >> a_Command.m_Position; - a_Command.m_Type = td::TowerType(union1 >> 4); - a_Command.m_Placer = union1 & 0xF; -} - - - - - -void Serializer::SerializeCommandData(const cdata::PlayerJoin& a_Command) { - m_Buffer << a_Command.m_ID << a_Command.m_Name; -} - -void Deserializer::DeserializeCommandData(cdata::PlayerJoin& a_Command) { - m_Buffer >> a_Command.m_ID >> a_Command.m_Name; -} - - - - -void Serializer::SerializeCommandData(const cdata::SpawnTroop& a_Command) { - m_Buffer << static_cast(static_cast(a_Command.m_Type) << 3 | a_Command.m_Level & 0x7) - << a_Command.m_Position << a_Command.m_Sender; -} - -void Deserializer::DeserializeCommandData(cdata::SpawnTroop& a_Command) { - std::uint8_t union1; - m_Buffer >> union1 >> a_Command.m_Position >> a_Command.m_Sender; - a_Command.m_Type = td::EntityType(union1 >> 3); - a_Command.m_Level = union1 & 0x7; -} - - - - - -void Serializer::SerializeCommandData(const cdata::TeamChange& a_Command) { - m_Buffer << static_cast(a_Command.m_Player << 1 | static_cast(a_Command.m_NewTeam)); -} - -void Deserializer::DeserializeCommandData(cdata::TeamChange& a_Command) { - std::uint8_t union1; - m_Buffer >> union1; - a_Command.m_Player = union1 >> 1; - a_Command.m_NewTeam = td::Team(union1 & 1); -} - - - - - -void Serializer::SerializeCommandData(const cdata::UpgradeTower& a_Command) { - m_Buffer << static_cast(a_Command.m_Tower << 4 | a_Command.m_Upgrade & 0xF); -} - -void Deserializer::DeserializeCommandData(cdata::UpgradeTower& a_Command) { - std::uint16_t union1; - m_Buffer >> union1; - a_Command.m_Tower = union1 >> 4; - a_Command.m_Upgrade = union1 & 0xF; -} - - - - - -void Serializer::SerializeCommandData(const cdata::UseItem& a_Command) { - m_Buffer << static_cast(static_cast(a_Command.m_Item) << 4 | a_Command.m_User & 0xF) - << a_Command.m_Position; -} - -void Deserializer::DeserializeCommandData(cdata::UseItem& a_Command) { - std::uint8_t union1; - m_Buffer >> union1 >> a_Command.m_Position; - a_Command.m_Item = td::ShopItem(union1 >> 4); - a_Command.m_User = union1 & 0xF; -} - - - - -} // namespace CommandSerializer -} // namespace protocol -} // namespace td diff --git a/src/td/protocol/command/CommandVisitor.cpp b/src/td/protocol/command/CommandVisitor.cpp deleted file mode 100644 index a018309..0000000 --- a/src/td/protocol/command/CommandVisitor.cpp +++ /dev/null @@ -1,11 +0,0 @@ -#include - -namespace td { -namespace protocol { - -void CommandVisitor::Check(const Command& a_Command) { - a_Command.Accept(*this); -} - -} // namespace protocol -} // namespace td diff --git a/src/td/protocol/command/Commands.cpp b/src/td/protocol/command/Commands.cpp deleted file mode 100644 index 09f575e..0000000 --- a/src/td/protocol/command/Commands.cpp +++ /dev/null @@ -1,18 +0,0 @@ -#define TD_INSTANCIATE_COMMANDS -#include - -#include - -namespace td { -namespace protocol { - -template -commands::ConcreteCommand::ConcreteCommand(const CommandDataType& a_Data) : m_Data(a_Data) {} - -template -void commands::ConcreteCommand::Accept(CommandVisitor& a_Visitor) const { - a_Visitor.Visit(*this); -} - -} // namespace protocol -} // namespace td \ No newline at end of file diff --git a/src/td/protocol/packet/PacketFactory.cpp b/src/td/protocol/packet/PacketFactory.cpp deleted file mode 100644 index d1aa219..0000000 --- a/src/td/protocol/packet/PacketFactory.cpp +++ /dev/null @@ -1,26 +0,0 @@ -#include - -#include -#include -#include - -namespace td { -namespace protocol { -namespace PacketFactory { - -using PacketCreator = std::function()>; - -#define DeclarePacket(PacketName, ...) std::make_unique(), - -static std::array, static_cast(PacketType::PACKET_COUNT)> Packets = { - DeclareAllPacket() -}; - -const std::unique_ptr& CreateReadOnlyPacket(PacketType a_Type) { - assert(a_Type < PacketType::PACKET_COUNT); - return Packets[static_cast(a_Type)]; -} - -} // namespace PacketFactory -} // namespace protocol -} // namespace td diff --git a/src/td/protocol/packet/PacketSerializer.cpp b/src/td/protocol/packet/PacketSerializer.cpp deleted file mode 100644 index 0fc785a..0000000 --- a/src/td/protocol/packet/PacketSerializer.cpp +++ /dev/null @@ -1,262 +0,0 @@ -#include - -#include -#include -#include - -#include -#include - -namespace td { -namespace protocol { -namespace PacketSerializer { - -#define DeclarePacket(PacketName, ...) \ - void Visit(const packets::PacketName& a_Packet) override { \ - const auto& packetData = a_Packet.m_Data; \ - SerializePacketData(packetData); \ - } \ - \ - void SerializePacketData(const packets::PacketName::PacketDataType& a_Packet); - - - - -class Serializer : public PacketVisitor { - private: - DataBuffer& m_Buffer; - - public: - Serializer(DataBuffer& a_Buffer) : m_Buffer(a_Buffer) {} - - void Serialize(const Packet& a_Packet) { - m_Buffer << static_cast(a_Packet.GetType()); - Check(a_Packet); - } - - DeclareAllPacket() -}; - -#undef DeclarePacket - - - - - -#define DeclarePacket(PacketName, ...) \ - void Visit(const packets::PacketName& a_Packet) override { \ - auto packetPtr = PacketFactory::CreatePacket(); \ - auto& packetData = packetPtr->m_Data; \ - \ - DeserializePacketData(packetData); \ - \ - m_Packet = std::move(packetPtr); \ - } \ - \ - void DeserializePacketData(packets::PacketName::PacketDataType& a_Packet); - - - -class Deserializer : public PacketVisitor { - private: - DataBuffer& m_Buffer; - PacketPtr m_Packet; - - public: - Deserializer(DataBuffer& a_Buffer) : m_Buffer(a_Buffer) {} - - bool Deserialize(const PacketPtr& a_Packet) { - try { - Check(*a_Packet.get()); - } catch (std::exception& e) { - utils::LOGE(utils::Format("[PacketSerializer::Deserializer] %s", e.what())); - return false; - } - return true; - } - - PacketPtr& GetPacket() { - return m_Packet; - } - - DeclareAllPacket() -}; - - - - - -DataBuffer Serialize(const Packet& a_Packet) { - DataBuffer buffer; - - Serializer serializer(buffer); - serializer.Serialize(a_Packet); - - return buffer; -} - -std::unique_ptr Deserialize(DataBuffer& a_Data) { - PacketID packetId; - a_Data >> packetId; - - if (packetId >= static_cast(PacketType::PACKET_COUNT)) - return nullptr; - - PacketType packetType = PacketType(packetId); - - // for double-dispatch - const PacketPtr& emptyPacket = PacketFactory::CreateReadOnlyPacket(packetType); - - Deserializer deserializer(a_Data); - - if (deserializer.Deserialize(emptyPacket)) { - PacketPtr packet = std::move(deserializer.GetPacket()); - return packet; - } - - return nullptr; -} - - - - - -//--------------------------------------------- -// Packet serializer implementation -//---------------------------------------------- - - - - - -void Serializer::SerializePacketData(const pdata::PlayerLogin& a_Packet) { - m_Buffer << a_Packet.m_PlayerName; -} - -void Deserializer::DeserializePacketData(pdata::PlayerLogin& a_Packet) { - m_Buffer >> a_Packet.m_PlayerName; -} - - - - - -void Serializer::SerializePacketData(const pdata::LoggingSuccess& a_Packet) { - m_Buffer << a_Packet.m_PlayerId; -} - -void Deserializer::DeserializePacketData(pdata::LoggingSuccess& a_Packet) { - m_Buffer >> a_Packet.m_PlayerId; -} - - - - - -void Serializer::SerializePacketData(const pdata::PlayerJoin& a_Packet) { - m_Buffer << a_Packet.m_Player; -} - -void Deserializer::DeserializePacketData(pdata::PlayerJoin& a_Packet) { - m_Buffer >> a_Packet.m_Player; -} - - - - -void Serializer::SerializePacketData(const pdata::PlayerLeave& a_Packet) { - m_Buffer << a_Packet.m_PlayerId; -} - -void Deserializer::DeserializePacketData(pdata::PlayerLeave& a_Packet) { - m_Buffer >> a_Packet.m_PlayerId; -} - - - - - -void Serializer::SerializePacketData(const pdata::KeepAlive& a_Packet) { - m_Buffer << a_Packet.m_KeepAliveId; -} - -void Deserializer::DeserializePacketData(pdata::KeepAlive& a_Packet) { - m_Buffer >> a_Packet.m_KeepAliveId; -} - - - - - -void Serializer::SerializePacketData(const pdata::Disconnect& a_Packet) { - m_Buffer << a_Packet.m_Reason; -} - -void Deserializer::DeserializePacketData(pdata::Disconnect& a_Packet) { - m_Buffer >> a_Packet.m_Reason; -} - - - - - -void Serializer::SerializePacketData(const pdata::ChatMessage& a_Packet) { - m_Buffer << a_Packet.m_Text; -} - -void Deserializer::DeserializePacketData(pdata::ChatMessage& a_Packet) { - m_Buffer >> a_Packet.m_Text; -} - - - - - -void Serializer::SerializePacketData(const pdata::LockSteps& a_Packet) { - m_Buffer << a_Packet.m_FirstFrameNumber; - for (int i = 0; i < LOCKSTEP_BUFFER_SIZE; i++) { - auto& lockStep = a_Packet.m_LockSteps[i]; - m_Buffer << VarInt{lockStep.size()}; - - for (int j = 0; j < lockStep.size(); i++) { - auto command = lockStep[j]; - m_Buffer << CommandSerializer::Serialize(*command); - } - } -} - -void Deserializer::DeserializePacketData(pdata::LockSteps& a_Packet) { - m_Buffer >> a_Packet.m_FirstFrameNumber; - for (int i = 0; i < LOCKSTEP_BUFFER_SIZE; i++) { - VarInt lockStepSize; - m_Buffer >> lockStepSize; - - for (std::size_t j = 0; j < lockStepSize.GetValue(); i++) { - auto command = CommandSerializer::Deserialize(m_Buffer); - if (command) { - a_Packet.m_LockSteps[i].push_back(command); - } else { - throw std::runtime_error("Failed to deserialise command"); - } - } - } -} - - - - - -void Serializer::SerializePacketData(const pdata::BeginGame& a_Packet) { - m_Buffer << a_Packet.m_Map << a_Packet.m_BlueTeam << a_Packet.m_RedTeam << a_Packet.m_Map; -} - -void Deserializer::DeserializePacketData(pdata::BeginGame& a_Packet) { - m_Buffer >> a_Packet.m_Map >> a_Packet.m_BlueTeam >> a_Packet.m_RedTeam >> a_Packet.m_Map; -} - - - - -} // namespace PacketSerializer -} // namespace protocol -} // namespace td diff --git a/src/td/protocol/packet/PacketVisitor.cpp b/src/td/protocol/packet/PacketVisitor.cpp deleted file mode 100644 index 940046c..0000000 --- a/src/td/protocol/packet/PacketVisitor.cpp +++ /dev/null @@ -1,11 +0,0 @@ -#include - -namespace td { -namespace protocol { - -void PacketVisitor::Check(const Packet& a_Packet) { - a_Packet.Accept(*this); -} - -} // namespace protocol -} // namespace td diff --git a/src/td/protocol/packet/Packets.cpp b/src/td/protocol/packet/Packets.cpp deleted file mode 100644 index 1fbc535..0000000 --- a/src/td/protocol/packet/Packets.cpp +++ /dev/null @@ -1,23 +0,0 @@ -#define TD_INSTANCIATE_PACKETS -#include - -#include - -namespace td { -namespace protocol { - -template -packets::ConcretePacket::ConcretePacket(const PacketDataType& a_Data) : m_Data(a_Data) {} - -template -void packets::ConcretePacket::Accept(PacketVisitor& a_Visitor) const { - a_Visitor.Visit(*this); -} - -#define DeclarePacket(PacketName, packetSendType, packetSenderType) \ - static_assert(static_cast(PacketSendType::packetSendType) && static_cast(PacketSenderType::packetSenderType)); - -DeclareAllPacket() - -} // namespace protocol -} // namespace td diff --git a/xmake.lua b/xmake.lua index 7ad71f9..4adb1c4 100644 --- a/xmake.lua +++ b/xmake.lua @@ -1,13 +1,16 @@ add_rules("mode.debug", "mode.release") -add_requires("fpm") +add_repositories("nazara-repo https://github.com/NazaraEngine/xmake-repo.git") +add_repositories("persson-repo https://git.ale-pri.com/Persson-dev/xmake-repo.git") + +add_requires("fpm", "enet6", "nazarautils", "splib 2.0.0") set_languages("c++17") target("Tower-Defense2") add_includedirs("include", {public = true}) set_kind("static") add_files("src/**.cpp") - add_packages("fpm", {public = true}) + add_packages("splib", "fpm", "enet6", "nazarautils", {public = true}) @@ -25,72 +28,3 @@ for _, file in ipairs(os.files("test/**.cpp")) do add_tests("compile_and_run") end --- --- If you want to known more usage about xmake, please see https://xmake.io --- --- ## FAQ --- --- You can enter the project directory firstly before building project. --- --- $ cd projectdir --- --- 1. How to build project? --- --- $ xmake --- --- 2. How to configure project? --- --- $ xmake f -p [macosx|linux|iphoneos ..] -a [x86_64|i386|arm64 ..] -m [debug|release] --- --- 3. Where is the build output directory? --- --- The default output directory is `./build` and you can configure the output directory. --- --- $ xmake f -o outputdir --- $ xmake --- --- 4. How to run and debug target after building project? --- --- $ xmake run [targetname] --- $ xmake run -d [targetname] --- --- 5. How to install target to the system directory or other output directory? --- --- $ xmake install --- $ xmake install -o installdir --- --- 6. Add some frequently-used compilation flags in xmake.lua --- --- @code --- -- add debug and release modes --- add_rules("mode.debug", "mode.release") --- --- -- add macro definition --- add_defines("NDEBUG", "_GNU_SOURCE=1") --- --- -- set warning all as error --- set_warnings("all", "error") --- --- -- set language: c99, c++11 --- set_languages("c99", "c++11") --- --- -- set optimization: none, faster, fastest, smallest --- set_optimize("fastest") --- --- -- add include search directories --- add_includedirs("/usr/include", "/usr/local/include") --- --- -- add link libraries and search directories --- add_links("tbox") --- add_linkdirs("/usr/local/lib", "/usr/lib") --- --- -- add system link libraries --- add_syslinks("z", "pthread") --- --- -- add compilation and link flags --- add_cxflags("-stdnolib", "-fno-strict-aliasing") --- add_ldflags("-L/usr/local/lib", "-lpthread", {force = true}) --- --- @endcode --- -