Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New packet format. #103

Draft
wants to merge 2 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
994 changes: 9 additions & 985 deletions .editorconfig

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ out/
packages/
vcpkg_installed/
CMakeSettings.json
!server/include/Packet/Out/
52 changes: 52 additions & 0 deletions server/include/Packet/In/LevelWarp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#ifndef LEVELWARP_H
#define LEVELWARP_H

#include <chrono>
#include <cstdint>
#include <string>
#include <vector>

#include "Packet/PacketBase.h"
#include "IEnums.h"


struct LevelWarp : public PacketBase<LevelWarp>
{
//time_t modificationTime = 0;
float x = 0.0f;
float y = 0.0f;
CString level;

public:
static const uint8_t ID = PLI_LEVELWARP;

static LevelWarp deserialize(CString& buffer) noexcept
{
LevelWarp packet;

/*
uint8_t packetType = buffer.readGUChar();
if (packetType == PLI_LEVELWARPMOD)
modificationTime = (time_t)buffer.readGUInt5();
*/

packet.x = (float)(buffer.readGChar() / 2.0f);
packet.y = (float)(buffer.readGChar() / 2.0f);
packet.level = buffer.readString("");

return packet;
}

static LevelWarp deserialize(CString&& buffer) noexcept
{
return deserialize(buffer);
}

[[nodiscard]]
CString serialize(int clientVersion = CLVER_2_17) const noexcept
{
return {};
}
};

#endif // LEVELWARP_H
46 changes: 46 additions & 0 deletions server/include/Packet/Out/LevelBoard.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#ifndef LEVELBOARD_H
#define LEVELBOARD_H

#include <chrono>
#include <cstdint>
#include <string>
#include <vector>

#include "Packet/PacketBase.h"
#include "IEnums.h"

#include "Level/LevelBoardChange.h"


struct LevelBoard : public PacketBase<LevelBoard>
{
const LevelBoardChange* levelBoardChange = nullptr;

public:
static const uint8_t ID = PLO_LEVELBOARD;

static LevelBoard deserialize(CString& buffer) noexcept
{
return {};
}

static LevelBoard deserialize(CString&& buffer) noexcept
{
return {};
}

[[nodiscard]]
CString serialize(int clientVersion = CLVER_2_17) const noexcept
{
CString packet;

packet.writeGChar(ID);

if (levelBoardChange != nullptr)
packet.write(levelBoardChange->getBoardStr());

return packet;
}
};

#endif // LEVELBOARD_H
42 changes: 42 additions & 0 deletions server/include/Packet/PacketBase.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#ifndef PACKETBASE_H
#define PACKETBASE_H

#include <cstdint>
#include <vector>
#include <concepts>

#include "IUtil.h"
#include "CString.h"


template <typename T>
concept IsPacket = requires(T t)
{
{ T::ID } -> std::same_as<const uint8_t>;
{ T::deserialize(std::declval<CString&&>()) } -> std::same_as<T>;
{ t.serialize() } -> std::same_as<CString>;
};

template <class T>
struct PacketBase
{
//static const uint8_t ID = T::ID;

static T deserialize(CString& buffer) noexcept
{
return T::deserialize(std::forward(buffer));
}

static T deserialize(CString&& buffer) noexcept
{
return T::deserialize(std::forward(buffer));
}

[[nodiscard]]
CString serialize(int clientVersion = CLVER_2_17) const noexcept
{
return static_cast<const T*>(this)->serialize(clientVersion);
}
};

#endif // PACKETBASE_H
8 changes: 8 additions & 0 deletions server/include/Player.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
#include <unordered_set>
#include <vector>

#include "Packet/PacketBase.h"

#ifdef V8NPCSERVER
#include "ScriptBindings.h"
#endif
Expand Down Expand Up @@ -135,6 +137,12 @@ class Player : public Account, public CSocketStub, public std::enable_shared_fro
bool sendFile(const CString& pFile);
bool sendFile(const CString& pPath, const CString& pFile);

template <IsPacket T>
void sendPacket(PacketBase<T>&& packet, bool appendNL = true)
{
sendPacket(packet.serialize(m_versionId), appendNL);
}

// Type of player
bool isAdminIp();
bool isStaff();
Expand Down
14 changes: 14 additions & 0 deletions server/src/Level/Level.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#include <set>
#include <tiletypes.h>

#include "Packet/Out/LevelBoard.h"

/*
Global Variables
*/
Expand Down Expand Up @@ -169,6 +171,17 @@ CString Level::getLayerPacket(int layer)

CString Level::getBoardChangesPacket(time_t time)
{
CString retVal;
for (const auto& change : m_boardChanges)
{
if (change.getModTime() >= time)
{
LevelBoard boardChange{ .levelBoardChange = &change };
retVal << boardChange.serialize();
}
}
return retVal;
/*
CString retVal;
retVal >> (char)PLO_LEVELBOARD;
for (const auto& change: m_boardChanges)
Expand All @@ -177,6 +190,7 @@ CString Level::getBoardChangesPacket(time_t time)
retVal << change.getBoardStr();
}
return retVal;
*/
}

CString Level::getBoardChangesPacket2(time_t time)
Expand Down
7 changes: 7 additions & 0 deletions server/src/Player/Player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#include "Weapon.h"
#include "utilities/stringutils.h"

#include "Packet/In/LevelWarp.h"

/*
Logs
*/
Expand Down Expand Up @@ -2453,12 +2455,17 @@ bool Player::msgPLI_LEVELWARP(CString& pPacket)
{
time_t modTime = 0;

LevelWarp lw = LevelWarp::deserialize(pPacket);
warp(lw.level, lw.x, lw.y, modTime);

/*
if (pPacket[0] - 32 == PLI_LEVELWARPMOD)
modTime = (time_t)pPacket.readGUInt5();

float loc[2] = { (float)(pPacket.readGChar() / 2.0f), (float)(pPacket.readGChar() / 2.0f) };
CString newLevel = pPacket.readString("");
warp(newLevel, loc[0], loc[1], modTime);
*/

return true;
}
Expand Down