mirror of
				https://github.com/DarkflameUniverse/DarkflameServer.git
				synced 2025-10-31 12:41:55 +00:00 
			
		
		
		
	created FromBitsUnchecked memcpy wrapper
This commit is contained in:
		| @@ -3,6 +3,7 @@ | ||||
| // C++ | ||||
| #include <charconv> | ||||
| #include <cstdint> | ||||
| #include <cstring> | ||||
| #include <ctime> | ||||
| #include <functional> | ||||
| #include <optional> | ||||
| @@ -152,6 +153,26 @@ namespace GeneralUtils { | ||||
| 		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 | ||||
| 	template <typename T> | ||||
| 	concept Enum = std::is_enum_v<T>; | ||||
|   | ||||
| @@ -54,6 +54,7 @@ struct AssetStream : std::istream { | ||||
| 	} | ||||
|  | ||||
| 	operator bool() { | ||||
| 		// NEED TO FIX THIS | ||||
| 		return reinterpret_cast<AssetMemoryBuffer*>(rdbuf())->m_Success; | ||||
| 	} | ||||
| }; | ||||
|   | ||||
| @@ -81,11 +81,10 @@ void NpcAgCourseStarter::OnFireEventServerSide(Entity* self, Entity* sender, std | ||||
| 		scriptedActivityComponent->RemoveActivityPlayerData(sender->GetObjectID()); | ||||
| 	} else if (args == "course_finish") { | ||||
| 		const time_t endTime = std::time(0); | ||||
| 		 | ||||
| 		// Using memcpy since misaligned reads are UB | ||||
| 		time_t startTime{}; | ||||
| 		std::memcpy(&startTime, &data->values[1], sizeof(time_t)); | ||||
| 		const time_t finish = (endTime - startTime); | ||||
|  | ||||
| 		// Using FromBitsUnchecked to create new time_t object since misaligned reads are UB | ||||
| 		const time_t startTime = GeneralUtils::FromBitsUnchecked<time_t>(&data->values[1]); | ||||
| 		const time_t finish = endTime - startTime; | ||||
| 		 | ||||
| 		std::memcpy(&data->values[2], &finish, sizeof(float)); | ||||
|  | ||||
|   | ||||
| @@ -1396,9 +1396,8 @@ void HandlePacket(Packet* packet) { | ||||
| 	} | ||||
|  | ||||
| 	default: | ||||
| 		// Need to use memcpy instead of reinterpret_cast to avoid misaligned reads, which are UB | ||||
| 		auto messageId = MessageType::World::INVALID; | ||||
| 		std::memcpy(&messageId, &packet->data[3], sizeof(MessageType::World)); | ||||
| 		// Need to use FromBitsUnchecked (aka memcpy) instead of reinterpret_cast to avoid misaligned reads, which are UB | ||||
| 		const auto messageId = GeneralUtils::FromBitsUnchecked<MessageType::World>(&packet->data[3]); | ||||
|  | ||||
| 		const std::string_view messageIdString = StringifiedEnum::ToString(messageId); | ||||
| 		LOG("Unknown world packet received: %4i, %s", messageId, messageIdString.data()); | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 jadebenn
					jadebenn