49#ifdef PROTOCOLCRAFT_DETAILED_PARSING
51 typename Internal::OffsetType<StorageType>::type* start_offset =
nullptr,
52 typename Internal::OffsetType<StorageType>::type* end_offset =
nullptr
57 if constexpr (std::is_arithmetic_v<SerializationType>)
59 if (length <
sizeof(SerializationType))
61 throw std::runtime_error(
"Not enough input in ReadData");
63#ifdef PROTOCOLCRAFT_DETAILED_PARSING
64 if (start_offset !=
nullptr)
66 *start_offset = length;
69 SerializationType output;
70 std::memcpy(&output, &(*iter),
sizeof(SerializationType));
71 length -=
sizeof(SerializationType);
72 iter +=
sizeof(SerializationType);
73#ifdef PROTOCOLCRAFT_DETAILED_PARSING
74 if (end_offset !=
nullptr)
80 if constexpr (
sizeof(SerializationType) > 1)
86 constexpr int num = 1;
87 if (*(
char*)&num == 1)
95 return static_cast<StorageType
>(output);
98 else if constexpr (Internal::IsVarType<SerializationType>)
100#ifdef PROTOCOLCRAFT_DETAILED_PARSING
101 if (start_offset !=
nullptr)
103 *start_offset = length;
107 typename SerializationType::underlying_type result = 0;
113 if (num_read >= length)
115 throw std::runtime_error(
"Not enough input in ReadData<VarType>");
117 if (num_read * 7 >
sizeof(
typename SerializationType::underlying_type) * 8)
119 throw std::runtime_error(
"VarType is too big in ReadData<VarType>");
122 read = *(iter + num_read);
124 typename SerializationType::underlying_type value =
static_cast<typename SerializationType::underlying_type
>(read & 127);
125 result |= (value << (7 * num_read));
128 }
while ((read & 128) != 0);
132#ifdef PROTOCOLCRAFT_DETAILED_PARSING
133 if (end_offset !=
nullptr)
135 *end_offset = length;
139 return static_cast<StorageType
>(result);
142 else if constexpr (std::is_same_v<SerializationType, std::string> && std::is_same_v<StorageType, std::string>)
144#ifdef PROTOCOLCRAFT_DETAILED_PARSING
145 if (start_offset !=
nullptr)
147 *start_offset = length;
150 const size_t size = ReadData<size_t, VarInt>(iter, length);
153 throw std::runtime_error(
"Not enough input in ReadData<std::string>");
158#ifdef PROTOCOLCRAFT_DETAILED_PARSING
159 if (end_offset !=
nullptr)
161 *end_offset = length;
165 return std::string(iter - size, iter);
168 else if constexpr (std::is_base_of_v<NetworkType, SerializationType> && std::is_base_of_v<NetworkType, StorageType>)
170#ifdef PROTOCOLCRAFT_DETAILED_PARSING
171 if (start_offset !=
nullptr)
173 *start_offset = length;
176 SerializationType output;
177 output.Read(iter, length);
178#ifdef PROTOCOLCRAFT_DETAILED_PARSING
179 if (end_offset !=
nullptr)
181 *end_offset = length;
185 return static_cast<StorageType
>(output);
189 Internal::IsVector<SerializationType> ||
190 Internal::IsArray<SerializationType> ||
191 Internal::IsGenericVector<SerializationType>)
194 StorageType output{};
195#ifdef PROTOCOLCRAFT_DETAILED_PARSING
196 if (start_offset !=
nullptr)
198 start_offset->first = length;
202 if constexpr (Internal::IsVector<SerializationType>)
204 static_assert(Internal::IsVector<StorageType>,
"StorageType should be a std::vector");
205 N = ReadData<size_t, VarInt>(iter, length);
207#ifdef PROTOCOLCRAFT_DETAILED_PARSING
208 if (start_offset !=
nullptr)
210 start_offset->second.resize(N);
212 if (end_offset !=
nullptr)
214 end_offset->second.resize(N);
219 else if constexpr (Internal::IsArray<SerializationType>)
221 static_assert(Internal::IsArray<StorageType>,
"StorageType should be a std::array");
222 N = std::size(output);
228 if constexpr (SerializationType::size > 0)
230 static_assert(Internal::IsArray<StorageType>,
"StorageType should be a std::array");
231 N = SerializationType::size;
234 else if constexpr (std::is_same_v<typename SerializationType::size_type, void>)
236 static_assert(std::is_same_v<StorageType, std::vector<unsigned char>>,
"Only std::vector<unsigned char> is supported for \"all remaining data\"");
239#ifdef PROTOCOLCRAFT_DETAILED_PARSING
240 if (start_offset !=
nullptr)
242 start_offset->second.resize(N);
244 if (end_offset !=
nullptr)
246 end_offset->second.resize(N);
253 static_assert(Internal::IsVector<StorageType>,
"StorageType should be a std::vector");
254 N = ReadData<size_t, typename SerializationType::size_type>(iter, length);
256#ifdef PROTOCOLCRAFT_DETAILED_PARSING
257 if (start_offset !=
nullptr)
259 start_offset->second.resize(N);
261 if (end_offset !=
nullptr)
263 end_offset->second.resize(N);
270 if constexpr (
sizeof(
typename SerializationType::value_type) == 1 &&
271 !std::is_same_v<typename SerializationType::value_type, bool> &&
272 std::is_same_v<typename SerializationType::value_type, typename StorageType::value_type> &&
273 !std::is_base_of_v<NetworkType, typename SerializationType::value_type>)
277 throw std::runtime_error(
"Not enough input to read vector data");
279#ifdef PROTOCOLCRAFT_DETAILED_PARSING
280 for (
size_t i = 0; i < N; ++i)
282 if (start_offset !=
nullptr)
284 start_offset->second[i] = length - i;
286 if (end_offset !=
nullptr)
288 end_offset->second[i] = length - i + 1;
292 std::memcpy(output.data(), &(*iter), N);
299 for (
size_t i = 0; i < N; ++i)
301 output[i] = ReadData<typename StorageType::value_type, typename SerializationType::value_type>(iter, length
302#ifdef PROTOCOLCRAFT_DETAILED_PARSING
304 start_offset !=
nullptr ? &start_offset->second[i] :
nullptr,
305 end_offset !=
nullptr ? &end_offset->second[i] :
nullptr
310#ifdef PROTOCOLCRAFT_DETAILED_PARSING
311 if (end_offset !=
nullptr)
313 end_offset->first = length;
319 else if constexpr (Internal::IsOptional<StorageType> && Internal::IsOptional<SerializationType>)
323 const bool has_value = ReadData<bool, bool>(iter, length);
326 output = ReadData<typename StorageType::value_type, typename SerializationType::value_type>(iter, length
327#ifdef PROTOCOLCRAFT_DETAILED_PARSING
338 else if constexpr (Internal::IsPair<StorageType> && Internal::IsPair<SerializationType>)
342#ifdef PROTOCOLCRAFT_DETAILED_PARSING
343 if (start_offset !=
nullptr)
345 start_offset->first = length;
348 output.first = ReadData<typename StorageType::first_type, typename SerializationType::first_type>(iter, length
349#ifdef PROTOCOLCRAFT_DETAILED_PARSING
351 start_offset !=
nullptr ? &start_offset->second.first :
nullptr,
352 end_offset !=
nullptr ? &end_offset->second.first :
nullptr
355 output.second = ReadData<typename StorageType::second_type, typename SerializationType::second_type>(iter, length
356#ifdef PROTOCOLCRAFT_DETAILED_PARSING
358 start_offset !=
nullptr ? &start_offset->second.second :
nullptr,
359 end_offset !=
nullptr ? &end_offset->second.second :
nullptr
363#ifdef PROTOCOLCRAFT_DETAILED_PARSING
364 if (end_offset !=
nullptr)
366 end_offset->first = length;
372 else if constexpr (Internal::IsMap<StorageType> && Internal::IsMap<SerializationType>)
374#ifdef PROTOCOLCRAFT_DETAILED_PARSING
375 if (start_offset !=
nullptr)
377 start_offset->first = length;
380 const int output_length = ReadData<int, VarInt>(iter, length);
383 for (
int i = 0; i < output_length; ++i)
385 const typename StorageType::key_type key = ReadData<typename StorageType::key_type, typename SerializationType::key_type>(iter, length);
386 const typename StorageType::mapped_type val = ReadData<typename StorageType::mapped_type, typename SerializationType::mapped_type>(iter, length
387#ifdef PROTOCOLCRAFT_DETAILED_PARSING
389 start_offset !=
nullptr ? &start_offset->second[key] :
nullptr,
390 end_offset !=
nullptr ? &end_offset->second[key] :
nullptr
393 output.insert(std::make_pair(key, val));
396#ifdef PROTOCOLCRAFT_DETAILED_PARSING
397 if (end_offset !=
nullptr)
399 end_offset->first = length;
405 else if constexpr (Internal::IsBitset<StorageType> && std::is_same_v<SerializationType, StorageType>)
408 const size_t N = output.size();
409#ifdef PROTOCOLCRAFT_DETAILED_PARSING
410 if (start_offset !=
nullptr)
412 *start_offset = length;
415 const std::vector<unsigned char> bytes =
ReadByteArray(iter, length, N / 8 + (N % 8 != 0));
416 for (
size_t i = 0; i < N; ++i)
418 output.set(i, (bytes[i / 8] >> (i % 8)) & 0x01);
420#ifdef PROTOCOLCRAFT_DETAILED_PARSING
421 if (end_offset !=
nullptr)
423 *end_offset = length;
431 static_assert(Internal::dependant_false<SerializationType>,
"Types not supported in ReadData");
454 void WriteData(
typename std::conditional_t<std::is_arithmetic_v<StorageType> || std::is_enum_v<StorageType>, StorageType,
const StorageType&> value,
WriteContainer& container)
457 if constexpr (std::is_arithmetic_v<SerializationType>)
459 SerializationType val =
static_cast<SerializationType
>(value);
462 if constexpr (
sizeof(SerializationType) > 1)
468 constexpr int num = 1;
469 if (*(
char*)&num == 1)
479 reinterpret_cast<unsigned char*
>(&val),
480 reinterpret_cast<unsigned char*
>(&val) +
sizeof(SerializationType)
484 else if constexpr (Internal::IsVarType<SerializationType>)
486 std::make_unsigned_t<typename SerializationType::underlying_type> val =
static_cast<std::make_unsigned_t<typename SerializationType::underlying_type>
>(value);
488 unsigned char temp =
static_cast<unsigned char>(val & 127);
494 container.push_back(temp);
498 else if constexpr (std::is_same_v<SerializationType, std::string> && std::is_same_v<StorageType, std::string>)
500 WriteData<int, VarInt>(
static_cast<int>(value.size()), container);
501 container.insert(container.end(), value.begin(), value.end());
504 else if constexpr (std::is_base_of_v<NetworkType, SerializationType> && std::is_base_of_v<NetworkType, StorageType>)
507 static_cast<SerializationType
>(value).Write(container);
511 Internal::IsVector<SerializationType> ||
512 Internal::IsArray<SerializationType> ||
513 Internal::IsGenericVector<SerializationType>)
516 if constexpr (Internal::IsVector<SerializationType>)
518 static_assert(Internal::IsVector<StorageType>,
"StorageType should be a std::vector");
519 WriteData<int, VarInt>(
static_cast<int>(value.size()), container);
522 else if constexpr (Internal::IsArray<SerializationType>)
524 static_assert(Internal::IsArray<StorageType>,
"StorageType should be a std::array");
530 if constexpr (SerializationType::size > 0)
532 static_assert(Internal::IsArray<StorageType>,
"StorageType should be a std::array");
535 else if constexpr (std::is_same_v<typename SerializationType::size_type, void>)
537 static_assert(std::is_same_v<StorageType, std::vector<unsigned char>>,
"Only std::vector<unsigned char> is supported for \"all remaining data\"");
542 static_assert(Internal::IsVector<StorageType>,
"StorageType should be a std::vector");
543 WriteData<int, typename SerializationType::size_type>(
static_cast<int>(value.size()), container);
548 if constexpr (
sizeof(
typename SerializationType::value_type) == 1 &&
549 !std::is_same_v<typename SerializationType::value_type, bool> &&
550 std::is_same_v<typename StorageType::value_type, typename SerializationType::value_type> &&
551 !std::is_base_of_v<NetworkType, typename SerializationType::value_type>)
553 container.insert(container.end(), value.begin(), value.end());
557 for (
const auto& e : value)
559 WriteData<typename StorageType::value_type, typename SerializationType::value_type>(e, container);
564 else if constexpr (Internal::IsOptional<StorageType> && Internal::IsOptional<SerializationType>)
566 WriteData<bool, bool>(value.has_value(), container);
567 if (value.has_value())
569 WriteData<typename StorageType::value_type, typename SerializationType::value_type>(value.value(), container);
573 else if constexpr (Internal::IsPair<StorageType> && Internal::IsPair<SerializationType>)
575 WriteData<typename StorageType::first_type, typename SerializationType::first_type>(value.first, container);
576 WriteData<typename StorageType::second_type, typename SerializationType::second_type>(value.second, container);
579 else if constexpr (Internal::IsMap<StorageType> && Internal::IsMap<SerializationType>)
581 WriteData<int, VarInt>(
static_cast<int>(value.size()), container);
582 for (
const auto& p : value)
584 WriteData<typename StorageType::key_type, typename SerializationType::key_type>(p.first, container);
585 WriteData<typename StorageType::mapped_type, typename SerializationType::mapped_type>(p.second, container);
589 else if constexpr (Internal::IsBitset<StorageType> && Internal::IsBitset<SerializationType>)
591 const size_t N = value.size();
592 std::vector<unsigned char> bytes(N / 8 + (N % 8 != 0), 0);
593 for (
size_t i = 0; i < N; ++i)
597 bytes[i / 8] |= 1 << (i % 8);
604 static_assert(Internal::dependant_false<SerializationType>,
"Types not supported in WriteData");