Skip to content

Commit

Permalink
fix: debug with addDouble
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] authored and dudantas committed Sep 26, 2024
1 parent fad17e5 commit 6d308f6
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 10 deletions.
16 changes: 7 additions & 9 deletions src/server/network/message/networkmessage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,20 +113,18 @@ void NetworkMessage::addString(const std::string &value, const std::source_locat

void NetworkMessage::addDouble(double value, uint8_t precision /*= 2*/) {
addByte(precision);

// Scale the double value by the specified precision factor and cast it to int64_t
auto scaledValue = static_cast<int64_t>(value * std::pow(SCALING_BASE, precision));
// Add the scaled value to the buffer
add<int64_t>(scaledValue);
add<uint32_t>((value * std::pow(static_cast<float>(SCALING_BASE), precision)) + std::numeric_limits<int32_t>::max());
}

double NetworkMessage::getDouble() {
// Retrieve the precision byte from the buffer
uint8_t precision = getByte();
// Retrieve the scaled int64_t value from the buffer
int64_t scaledValue = get<int64_t>();
// Retrieve the scaled uint32_t value from the buffer
uint32_t scaledValue = get<uint32_t>();

Check warning on line 123 in src/server/network/message/networkmessage.cpp

View workflow job for this annotation

GitHub Actions / Qodana for C/C++

use-auto

use auto when initializing with a template cast to avoid duplicating the type name
// Convert the scaled value back to double using the precision factor
return static_cast<double>(scaledValue) / std::pow(SCALING_BASE, precision);
double adjustedValue = static_cast<double>(scaledValue) - std::numeric_limits<int32_t>::max();

Check warning on line 125 in src/server/network/message/networkmessage.cpp

View workflow job for this annotation

GitHub Actions / Qodana for C/C++

misra-cpp2008-5-0-5

MISRA 5-0-5: There shall be no implicit floating-integral conversions
// Convert back to the original double value using the precision factor
return adjustedValue / std::pow(static_cast<double>(SCALING_BASE), precision);
}

void NetworkMessage::addByte(uint8_t value, std::source_location location /*= std::source_location::current()*/) {
Expand Down Expand Up @@ -180,7 +178,7 @@ void NetworkMessage::addPosition(const Position &pos) {
addByte(pos.z);
}

void NetworkMessage::append(const NetworkMessage& other) {
void NetworkMessage::append(const NetworkMessage &other) {
size_t otherLength = other.getLength();
size_t otherStartPos = NetworkMessage::INITIAL_BUFFER_POSITION; // Always start copying from the initial buffer position

Expand Down
2 changes: 1 addition & 1 deletion src/server/network/message/networkmessage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ class NetworkMessage {
return size <= (info.length - (info.position - INITIAL_BUFFER_POSITION));
}

void append(const NetworkMessage& other);
void append(const NetworkMessage &other);

protected:
struct NetworkMessageInfo {
Expand Down

0 comments on commit 6d308f6

Please sign in to comment.