mirror of
https://github.com/DarkflameUniverse/DarkflameServer.git
synced 2024-11-25 23:17:28 +00:00
created FromBitsUnchecked memcpy wrapper
This commit is contained in:
parent
6fa719c679
commit
66fa3ff4ba
@ -3,6 +3,7 @@
|
|||||||
// C++
|
// C++
|
||||||
#include <charconv>
|
#include <charconv>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
|
#include <cstring>
|
||||||
#include <ctime>
|
#include <ctime>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <optional>
|
#include <optional>
|
||||||
@ -152,6 +153,26 @@ namespace GeneralUtils {
|
|||||||
char_pointer_hash
|
char_pointer_hash
|
||||||
>;
|
>;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A convenience wrapper around std::memcpy that creates a new object
|
||||||
|
* from the value representation of the provided bytes.Use when
|
||||||
|
* std::bit_cast is not applicable.
|
||||||
|
* @warning All restrictions of std::memcpy still apply. Accessing
|
||||||
|
* outside of the source object is undefined behavior.
|
||||||
|
* @param from The source of the value representation
|
||||||
|
* @returns A new object with the given value representation
|
||||||
|
*/
|
||||||
|
template <typename To, typename From>
|
||||||
|
[[nodiscard]]
|
||||||
|
inline To FromBitsUnchecked(const From* from) noexcept
|
||||||
|
requires (std::is_trivially_copyable_v<To>
|
||||||
|
&& std::is_trivially_copyable_v<From>)
|
||||||
|
{
|
||||||
|
To to{};
|
||||||
|
std::memcpy(&to, from, sizeof(To));
|
||||||
|
return to;
|
||||||
|
}
|
||||||
|
|
||||||
// Concept constraining to enum types
|
// Concept constraining to enum types
|
||||||
template <typename T>
|
template <typename T>
|
||||||
concept Enum = std::is_enum_v<T>;
|
concept Enum = std::is_enum_v<T>;
|
||||||
|
@ -54,6 +54,7 @@ struct AssetStream : std::istream {
|
|||||||
}
|
}
|
||||||
|
|
||||||
operator bool() {
|
operator bool() {
|
||||||
|
// NEED TO FIX THIS
|
||||||
return reinterpret_cast<AssetMemoryBuffer*>(rdbuf())->m_Success;
|
return reinterpret_cast<AssetMemoryBuffer*>(rdbuf())->m_Success;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -82,10 +82,9 @@ void NpcAgCourseStarter::OnFireEventServerSide(Entity* self, Entity* sender, std
|
|||||||
} else if (args == "course_finish") {
|
} else if (args == "course_finish") {
|
||||||
const time_t endTime = std::time(0);
|
const time_t endTime = std::time(0);
|
||||||
|
|
||||||
// Using memcpy since misaligned reads are UB
|
// Using FromBitsUnchecked to create new time_t object since misaligned reads are UB
|
||||||
time_t startTime{};
|
const time_t startTime = GeneralUtils::FromBitsUnchecked<time_t>(&data->values[1]);
|
||||||
std::memcpy(&startTime, &data->values[1], sizeof(time_t));
|
const time_t finish = endTime - startTime;
|
||||||
const time_t finish = (endTime - startTime);
|
|
||||||
|
|
||||||
std::memcpy(&data->values[2], &finish, sizeof(float));
|
std::memcpy(&data->values[2], &finish, sizeof(float));
|
||||||
|
|
||||||
|
@ -1396,9 +1396,8 @@ void HandlePacket(Packet* packet) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
default:
|
default:
|
||||||
// Need to use memcpy instead of reinterpret_cast to avoid misaligned reads, which are UB
|
// Need to use FromBitsUnchecked (aka memcpy) instead of reinterpret_cast to avoid misaligned reads, which are UB
|
||||||
auto messageId = MessageType::World::INVALID;
|
const auto messageId = GeneralUtils::FromBitsUnchecked<MessageType::World>(&packet->data[3]);
|
||||||
std::memcpy(&messageId, &packet->data[3], sizeof(MessageType::World));
|
|
||||||
|
|
||||||
const std::string_view messageIdString = StringifiedEnum::ToString(messageId);
|
const std::string_view messageIdString = StringifiedEnum::ToString(messageId);
|
||||||
LOG("Unknown world packet received: %4i, %s", messageId, messageIdString.data());
|
LOG("Unknown world packet received: %4i, %s", messageId, messageIdString.data());
|
||||||
|
Loading…
Reference in New Issue
Block a user