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

chore: Eradicate C-style casts and further clean up some code #1361

Merged
merged 13 commits into from
Dec 28, 2023
10 changes: 5 additions & 5 deletions dChatServer/ChatPacketHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ void ChatPacketHandler::HandleFriendlistRequest(Packet* packet) {
BitStreamUtils::WriteHeader(bitStream, eConnectionType::CLIENT, eClientMessageType::GET_FRIENDS_LIST_RESPONSE);
bitStream.Write<uint8_t>(0);
bitStream.Write<uint16_t>(1); //Length of packet -- just writing one as it doesn't matter, client skips it.
bitStream.Write((uint16_t)player->friends.size());
bitStream.Write<uint16_t>(player->friends.size());

for (auto& data : player->friends) {
data.Serialize(bitStream);
Expand Down Expand Up @@ -705,7 +705,7 @@ void ChatPacketHandler::SendTeamInviteConfirm(PlayerData* receiver, bool bLeader
bitStream.Write(ucLootFlag);
bitStream.Write(ucNumOfOtherPlayers);
bitStream.Write(ucResponseCode);
bitStream.Write(static_cast<uint32_t>(wsLeaderName.size()));
bitStream.Write<uint32_t>(wsLeaderName.size());
for (const auto character : wsLeaderName) {
bitStream.Write(character);
}
Expand All @@ -730,7 +730,7 @@ void ChatPacketHandler::SendTeamStatus(PlayerData* receiver, LWOOBJID i64LeaderI
bitStream.Write<uint32_t>(0); // BinaryBuffe, no clue what's in here
bitStream.Write(ucLootFlag);
bitStream.Write(ucNumOfOtherPlayers);
bitStream.Write(static_cast<uint32_t>(wsLeaderName.size()));
bitStream.Write<uint32_t>(wsLeaderName.size());
for (const auto character : wsLeaderName) {
bitStream.Write(character);
}
Expand Down Expand Up @@ -771,7 +771,7 @@ void ChatPacketHandler::SendTeamAddPlayer(PlayerData* receiver, bool bIsFreeTria
bitStream.Write(bLocal);
bitStream.Write(bNoLootOnDeath);
bitStream.Write(i64PlayerID);
bitStream.Write(static_cast<uint32_t>(wsPlayerName.size()));
bitStream.Write<uint32_t>(wsPlayerName.size());
for (const auto character : wsPlayerName) {
bitStream.Write(character);
}
Expand Down Expand Up @@ -802,7 +802,7 @@ void ChatPacketHandler::SendTeamRemovePlayer(PlayerData* receiver, bool bDisband
bitStream.Write(bLocal);
bitStream.Write(i64LeaderID);
bitStream.Write(i64PlayerID);
bitStream.Write(static_cast<uint32_t>(wsPlayerName.size()));
bitStream.Write<uint32_t>(wsPlayerName.size());
for (const auto character : wsPlayerName) {
bitStream.Write(character);
}
Expand Down
2 changes: 1 addition & 1 deletion dChatServer/PlayerContainer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ void PlayerContainer::UpdateTeamsOnWorld(TeamData* team, bool deleteTeam) {

if (!deleteTeam) {
bitStream.Write(team->lootFlag);
bitStream.Write(static_cast<char>(team->memberIDs.size()));
bitStream.Write<char>(team->memberIDs.size());
for (const auto memberID : team->memberIDs) {
bitStream.Write(memberID);
}
Expand Down
16 changes: 8 additions & 8 deletions dCommon/AmfSerialize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,17 @@ void RakNet::BitStream::Write<AMFBaseValue&>(AMFBaseValue& value) {
* RakNet writes in the correct byte order - do not reverse this.
*/
void WriteUInt29(RakNet::BitStream* bs, uint32_t v) {
unsigned char b4 = (unsigned char)v;
unsigned char b4 = static_cast<unsigned char>(v);
if (v < 0x00200000) {
b4 = b4 & 0x7F;
if (v > 0x7F) {
unsigned char b3;
v = v >> 7;
b3 = ((unsigned char)(v)) | 0x80;
b3 = static_cast<unsigned char>(v) | 0x80;
if (v > 0x7F) {
unsigned char b2;
v = v >> 7;
b2 = ((unsigned char)(v)) | 0x80;
b2 = static_cast<unsigned char>(v) | 0x80;
bs->Write(b2);
}

Expand All @@ -76,11 +76,11 @@ void WriteUInt29(RakNet::BitStream* bs, uint32_t v) {
unsigned char b3;

v = v >> 8;
b3 = ((unsigned char)(v)) | 0x80;
b3 = static_cast<unsigned char>(v) | 0x80;
v = v >> 7;
b2 = ((unsigned char)(v)) | 0x80;
b2 = static_cast<unsigned char>(v) | 0x80;
v = v >> 7;
b1 = ((unsigned char)(v)) | 0x80;
b1 = static_cast<unsigned char>(v) | 0x80;

bs->Write(b1);
bs->Write(b2);
Expand All @@ -105,8 +105,8 @@ void WriteFlagNumber(RakNet::BitStream* bs, uint32_t v) {
* RakNet writes in the correct byte order - do not reverse this.
*/
void WriteAMFString(RakNet::BitStream* bs, const std::string& str) {
WriteFlagNumber(bs, (uint32_t)str.size());
bs->Write(str.c_str(), (uint32_t)str.size());
WriteFlagNumber(bs, static_cast<uint32_t>(str.size()));
bs->Write(str.c_str(), static_cast<uint32_t>(str.size()));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion dCommon/BrickByBrickFix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ uint32_t BrickByBrickFix::TruncateBrokenBrickByBrickXml() {

if (actualUncompressedSize != -1) {
uint32_t previousSize = completeUncompressedModel.size();
completeUncompressedModel.append((char*)uncompressedChunk.get());
completeUncompressedModel.append(reinterpret_cast<char*>(uncompressedChunk.get()));
completeUncompressedModel.resize(previousSize + actualUncompressedSize);
} else {
LOG("Failed to inflate chunk %i for model %llu. Error: %i", chunkCount, model.id, err);
Expand Down
6 changes: 6 additions & 0 deletions dCommon/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ add_library(dCommon STATIC ${DCOMMON_SOURCES})

target_link_libraries(dCommon bcrypt dDatabase tinyxml2)

# If using gcc, warn on old-style C casts
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
set_source_files_properties(${DCOMMON_SOURCES} PROPERTIES COMPILE_FLAGS "-Wold-style-cast")
endif()

# Handle zlib dependency
if (UNIX)
find_package(ZLIB REQUIRED)
elseif (WIN32)
Expand Down
8 changes: 4 additions & 4 deletions dCommon/Diagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,10 @@ void MakeBacktrace() {
sigact.sa_sigaction = CritErrHdlr;
sigact.sa_flags = SA_RESTART | SA_SIGINFO;

if (sigaction(SIGSEGV, &sigact, (struct sigaction*)nullptr) != 0 ||
sigaction(SIGFPE, &sigact, (struct sigaction*)nullptr) != 0 ||
sigaction(SIGABRT, &sigact, (struct sigaction*)nullptr) != 0 ||
sigaction(SIGILL, &sigact, (struct sigaction*)nullptr) != 0) {
if (sigaction(SIGSEGV, &sigact, static_cast<struct sigaction*>(nullptr)) != 0 ||
jadebenn marked this conversation as resolved.
Show resolved Hide resolved
sigaction(SIGFPE, &sigact, static_cast<struct sigaction*>(nullptr)) != 0 ||
sigaction(SIGABRT, &sigact, static_cast<struct sigaction*>(nullptr)) != 0 ||
sigaction(SIGILL, &sigact, static_cast<struct sigaction*>(nullptr)) != 0) {
fprintf(stderr, "error setting signal handler for %d (%s)\n",
SIGSEGV,
strsignal(SIGSEGV));
Expand Down
2 changes: 1 addition & 1 deletion dCommon/GeneralUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ bool _IsSuffixChar(uint8_t c) {
bool GeneralUtils::_NextUTF8Char(std::string_view& slice, uint32_t& out) {
size_t rem = slice.length();
if (slice.empty()) return false;
const uint8_t* bytes = (const uint8_t*)&slice.front();
const uint8_t* bytes = reinterpret_cast<const uint8_t*>(&slice.front());
if (rem > 0) {
uint8_t first = bytes[0];
if (first < 0x80) { // 1 byte character
Expand Down
22 changes: 11 additions & 11 deletions dCommon/LDFFormat.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,15 @@ class LDFData: public LDFBaseData {

//! Writes the key to the packet
void WriteKey(RakNet::BitStream* packet) {
packet->Write(static_cast<uint8_t>(this->key.length() * sizeof(uint16_t)));
packet->Write<uint8_t>(this->key.length() * sizeof(uint16_t));
for (uint32_t i = 0; i < this->key.length(); ++i) {
packet->Write(static_cast<uint16_t>(this->key[i]));
packet->Write<uint16_t>(this->key[i]);
}
}

//! Writes the value to the packet
void WriteValue(RakNet::BitStream* packet) {
packet->Write(static_cast<uint8_t>(this->GetValueType()));
packet->Write<uint8_t>(this->GetValueType());
packet->Write(this->value);
}

Expand Down Expand Up @@ -179,30 +179,30 @@ template<> inline eLDFType LDFData<std::string>::GetValueType(void) { return LDF
// The specialized version for std::u16string (UTF-16)
template<>
inline void LDFData<std::u16string>::WriteValue(RakNet::BitStream* packet) {
packet->Write(static_cast<uint8_t>(this->GetValueType()));
packet->Write<uint8_t>(this->GetValueType());

packet->Write(static_cast<uint32_t>(this->value.length()));
packet->Write<uint32_t>(this->value.length());
for (uint32_t i = 0; i < this->value.length(); ++i) {
packet->Write(static_cast<uint16_t>(this->value[i]));
packet->Write<uint16_t>(this->value[i]);
}
}

// The specialized version for bool
template<>
inline void LDFData<bool>::WriteValue(RakNet::BitStream* packet) {
packet->Write(static_cast<uint8_t>(this->GetValueType()));
packet->Write<uint8_t>(this->GetValueType());

packet->Write(static_cast<uint8_t>(this->value));
packet->Write<uint8_t>(this->value);
}

// The specialized version for std::string (UTF-8)
template<>
inline void LDFData<std::string>::WriteValue(RakNet::BitStream* packet) {
packet->Write(static_cast<uint8_t>(this->GetValueType()));
packet->Write<uint8_t>(this->GetValueType());

packet->Write(static_cast<uint32_t>(this->value.length()));
packet->Write<uint32_t>(this->value.length());
for (uint32_t i = 0; i < this->value.length(); ++i) {
packet->Write(static_cast<uint8_t>(this->value[i]));
packet->Write<uint8_t>(this->value[i]);
}
}

Expand Down
6 changes: 3 additions & 3 deletions dCommon/MD5.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ void MD5::init() {
// decodes input (unsigned char) into output (uint4). Assumes len is a multiple of 4.
void MD5::decode(uint4 output[], const uint1 input[], size_type len) {
for (unsigned int i = 0, j = 0; j < len; i++, j += 4)
output[i] = ((uint4)input[j]) | (((uint4)input[j + 1]) << 8) |
(((uint4)input[j + 2]) << 16) | (((uint4)input[j + 3]) << 24);
output[i] = static_cast<uint4>(input[j]) | (static_cast<uint4>(input[j + 1]) << 8) |
(static_cast<uint4>(input[j + 2]) << 16) | (static_cast<uint4>(input[j + 3]) << 24);
}

//////////////////////////////
Expand Down Expand Up @@ -278,7 +278,7 @@ void MD5::update(const unsigned char input[], size_type length) {

// for convenience provide a verson with signed char
void MD5::update(const char input[], size_type length) {
update((const unsigned char*)input, length);
update(reinterpret_cast<const unsigned char*>(input), length);
}

//////////////////////////////
Expand Down
34 changes: 17 additions & 17 deletions dCommon/Metrics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ void Metrics::EndMeasurement(MetricVariable variable) {
}

float Metrics::ToMiliseconds(int64_t nanoseconds) {
return (float)nanoseconds / 1e6;
return static_cast<float>(nanoseconds) / 1e6;
}

std::string Metrics::MetricVariableToString(MetricVariable variable) {
Expand Down Expand Up @@ -193,34 +193,34 @@ size_t Metrics::GetPeakRSS() {
/* Windows -------------------------------------------------- */
PROCESS_MEMORY_COUNTERS info;
GetProcessMemoryInfo(GetCurrentProcess(), &info, sizeof(info));
return (size_t)info.PeakWorkingSetSize;
return static_cast<size_t>(info.PeakWorkingSetSize);

#elif (defined(_AIX) || defined(__TOS__AIX__)) || (defined(__sun__) || defined(__sun) || defined(sun) && (defined(__SVR4) || defined(__svr4__)))
/* AIX and Solaris ------------------------------------------ */
struct psinfo psinfo;
int fd = -1;
if ((fd = open("/proc/self/psinfo", O_RDONLY)) == -1)
return (size_t)0L; /* Can't open? */
return static_cast<size_t>(0L); /* Can't open? */
if (read(fd, &psinfo, sizeof(psinfo)) != sizeof(psinfo)) {
close(fd);
return (size_t)0L; /* Can't read? */
return static_cast<size_t>(0L); /* Can't read? */
}
close(fd);
return (size_t)(psinfo.pr_rssize * 1024L);
return static_cast<size_t>(psinfo.pr_rssize * 1024L);

#elif defined(__unix__) || defined(__unix) || defined(unix) || (defined(__APPLE__) && defined(__MACH__))
/* BSD, Linux, and OSX -------------------------------------- */
struct rusage rusage;
getrusage(RUSAGE_SELF, &rusage);
#if defined(__APPLE__) && defined(__MACH__)
return (size_t)rusage.ru_maxrss;
return static_cast<size_t>(rusage.ru_maxrss);
#else
return (size_t)(rusage.ru_maxrss * 1024L);
return static_cast<size_t>(rusage.ru_maxrss * 1024L);
#endif

#else
/* Unknown OS ----------------------------------------------- */
return (size_t)0L; /* Unsupported. */
return static_cast<size_t>(0L); /* Unsupported. */
#endif

}
Expand All @@ -234,33 +234,33 @@ size_t Metrics::GetCurrentRSS() {
/* Windows -------------------------------------------------- */
PROCESS_MEMORY_COUNTERS info;
GetProcessMemoryInfo(GetCurrentProcess(), &info, sizeof(info));
return (size_t)info.WorkingSetSize;
return static_cast<size_t>(info.WorkingSetSize);

#elif defined(__APPLE__) && defined(__MACH__)
/* OSX ------------------------------------------------------ */
struct mach_task_basic_info info;
mach_msg_type_number_t infoCount = MACH_TASK_BASIC_INFO_COUNT;
if (task_info(mach_task_self(), MACH_TASK_BASIC_INFO,
(task_info_t)&info, &infoCount) != KERN_SUCCESS)
return (size_t)0L; /* Can't access? */
return (size_t)info.resident_size;
if (static_cast<task_info>(mach_task_self(), MACH_TASK_BASIC_INFO,
static_cast<task_info_t>(&info), &infoCount) != KERN_SUCCESS)
return static_cast<size_t>(0L); /* Can't access? */
return static_cast<size_t>(info.resident_size);

#elif defined(__linux__) || defined(__linux) || defined(linux) || defined(__gnu_linux__)
/* Linux ---------------------------------------------------- */
long rss = 0L;
FILE* fp = NULL;
if ((fp = fopen("/proc/self/statm", "r")) == NULL)
return (size_t)0L; /* Can't open? */
return static_cast<size_t>(0L); /* Can't open? */
if (fscanf(fp, "%*s%ld", &rss) != 1) {
fclose(fp);
return (size_t)0L; /* Can't read? */
return static_cast<size_t>(0L); /* Can't read? */
}
fclose(fp);
return (size_t)rss * (size_t)sysconf(_SC_PAGESIZE);
return static_cast<size_t>(rss) * static_cast<size_t>(sysconf(_SC_PAGESIZE));

#else
/* AIX, BSD, Solaris, and Unknown OS ------------------------ */
return (size_t)0L; /* Unsupported. */
return static_cast<size_t>(0L); /* Unsupported. */
#endif

}
Expand Down
23 changes: 17 additions & 6 deletions dCommon/NiPoint3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,13 @@ bool NiPoint3::operator!=(const NiPoint3& point) const {
//! Operator for subscripting
float& NiPoint3::operator[](int i) {
float* base = &x;
return (float&)base[i];
return const_cast<float&>(base[i]);
jadebenn marked this conversation as resolved.
Show resolved Hide resolved
}

//! Operator for subscripting
const float& NiPoint3::operator[](int i) const {
const float* base = &x;
return (float&)base[i];
return const_cast<float&>(base[i]);
}

//! Operator for addition of vectors
Expand Down Expand Up @@ -181,7 +181,7 @@ bool NiPoint3::IsWithinAxisAlignedBox(const NiPoint3& minPoint, const NiPoint3&
if (this->y < minPoint.y) return false;
if (this->y > maxPoint.y) return false;

return (this->z < maxPoint.z&& this->z > minPoint.z);
return (this->z < maxPoint.z && this->z > minPoint.z);
}

//! Checks to see if the point (or vector) is within a sphere
Expand Down Expand Up @@ -232,10 +232,21 @@ NiPoint3 NiPoint3::MoveTowards(const NiPoint3& current, const NiPoint3& target,
float dx = target.x - current.x;
float dy = target.y - current.y;
float dz = target.z - current.z;
float lengthSquared = (float)((double)dx * (double)dx + (double)dy * (double)dy + (double)dz * (double)dz);
if ((double)lengthSquared == 0.0 || (double)maxDistanceDelta >= 0.0 && (double)lengthSquared <= (double)maxDistanceDelta * (double)maxDistanceDelta)

float lengthSquared = static_cast<float>(
static_cast<double>(dx) * static_cast<double>(dx) +
static_cast<double>(dy) * static_cast<double>(dy) +
static_cast<double>(dz) * static_cast<double>(dz)
);

if (static_cast<double>(lengthSquared) == 0.0
|| static_cast<double>(maxDistanceDelta) >= 0.0
&& static_cast<double>(lengthSquared)
<= static_cast<double>(maxDistanceDelta) * static_cast<double>(maxDistanceDelta)) {
return target;
float length = (float)std::sqrt((double)lengthSquared);
}

float length = static_cast<float>(std::sqrt(static_cast<double>(lengthSquared)));
jadebenn marked this conversation as resolved.
Show resolved Hide resolved
return NiPoint3(current.x + dx / length * maxDistanceDelta, current.y + dy / length * maxDistanceDelta, current.z + dz / length * maxDistanceDelta);
}

Expand Down
4 changes: 2 additions & 2 deletions dCommon/SHA512.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ void SHA512::transform(const unsigned char* message, unsigned int block_nb) {
uint64 t1, t2;
const unsigned char* sub_block;
int i, j;
for (i = 0; i < (int)block_nb; i++) {
for (i = 0; i < static_cast<int>(block_nb); i++) {
jadebenn marked this conversation as resolved.
Show resolved Hide resolved
sub_block = message + (i << 7);
for (j = 0; j < 16; j++) {
SHA2_PACK64(&sub_block[j << 3], &w[j]);
Expand Down Expand Up @@ -140,7 +140,7 @@ std::string sha512(std::string input) {
memset(digest, 0, SHA512::DIGEST_SIZE);
class SHA512 ctx;
ctx.init();
ctx.update((unsigned char*)input.c_str(), input.length());
ctx.update(reinterpret_cast<unsigned char*>(const_cast<char*>(input.c_str())), input.length());
ctx.final(digest);

char buf[2 * SHA512::DIGEST_SIZE + 1];
Expand Down
Loading