Botcraft 26.3
Loading...
Searching...
No Matches
DataComponents.cpp
Go to the documentation of this file.
1#if PROTOCOL_VERSION > 765 /* > 1.20.4 */
4
30#if PROTOCOL_VERSION < 777 /* < 26.3 */
32#endif
38#if PROTOCOL_VERSION < 774 /* < 1.21.115 */
40#endif
43#if PROTOCOL_VERSION < 770 /* < 1.21.5 */
45#endif
49#if PROTOCOL_VERSION > 766 /* > 1.20.6 */
51#endif
52#if PROTOCOL_VERSION > 767 /* > 1.21.1 */
60#endif
61#if PROTOCOL_VERSION > 769 /* > 1.21.4 */
72#endif
73#if PROTOCOL_VERSION > 772 /* > 1.21.8 */
75#endif
76#if PROTOCOL_VERSION > 773 /* > 1.21.10 */
83#endif
84#if PROTOCOL_VERSION > 775 /* > 26.1.2 */
86#endif
87#if PROTOCOL_VERSION > 776 /* > 26.2 */
93#endif
94
96
97namespace ProtocolCraft
98{
99 namespace Components
100 {
108#if PROTOCOL_VERSION > 769 /* > 1.21.4 */
111#endif
112#if PROTOCOL_VERSION > 765 /* > 1.20.4 */
114#endif
116#if PROTOCOL_VERSION > 769 /* > 1.21.4 */
118#endif
119#if PROTOCOL_VERSION > 766 /* > 1.20.6 */
121#endif
122#if PROTOCOL_VERSION > 773 /* > 1.21.10 */
124#endif
125#if PROTOCOL_VERSION > 769 /* > 1.21.4 */ && PROTOCOL_VERSION < 777 /* < 26.3 */
126 DEFINE_NETWORK_TYPE(AssetInfo);
127 DEFINE_NETWORK_TYPE(MaterialAssetGroup);
128#endif
131#if PROTOCOL_VERSION > 769 /* > 1.21.4 */
134#endif
135#if PROTOCOL_VERSION > 772 /* > 1.21.8 */
138#endif
139#if PROTOCOL_VERSION < 768 /* < 1.21.2 */
140 DEFINE_NETWORK_TYPE(PossibleEffect);
141#endif
142#if PROTOCOL_VERSION > 776 /* > 26.2 */
145#endif
152
178#if PROTOCOL_VERSION < 777 /* < 26.3 */
179 DEFINE_NETWORK_TYPE(DataComponentTypeMapItemColor);
180#endif
186#if PROTOCOL_VERSION < 774 /* < 1.21.11 */
187 DEFINE_NETWORK_TYPE(DataComponentTypeResourceLocation);
188#endif
191#if PROTOCOL_VERSION < 770 /* < 1.21.5 */
192 DEFINE_NETWORK_TYPE(DataComponentTypeUnbreakable);
193#endif
197#if PROTOCOL_VERSION > 766 /* > 1.20.6 */
199#endif
200#if PROTOCOL_VERSION > 767 /* > 1.21.1 */
208#endif
209#if PROTOCOL_VERSION > 769 /* > 1.21.4 */
220#endif
221#if PROTOCOL_VERSION > 772 /* > 1.21.8 */
223#endif
224#if PROTOCOL_VERSION > 773 /* > 1.21.10 */
231#endif
232#if PROTOCOL_VERSION > 775 /* > 26.1.2 */
234#endif
235#if PROTOCOL_VERSION > 776 /* > 26.2 */
241#endif
242
244 {
245 static constexpr std::array<std::string_view, static_cast<size_t>(DataComponentTypes::NUM_DATA_COMPONENT_TYPES)> names = {
246 "custom_data",
247 "max_stack_size",
248 "max_damage",
249 "damage",
250 "unbreakable",
251#if PROTOCOL_VERSION > 773 /* > 1.21.10 */
252 "use_effects",
253#endif
254 "custom_name",
255#if PROTOCOL_VERSION > 773 /* > 1.21.10 */
256 "minimum_attack_charge",
257 "damage_type",
258#endif
259 "item_name",
260#if PROTOCOL_VERSION > 767 /* > 1.21.1 */
261 "item_model",
262#endif
263 "lore",
264 "rarity",
265 "enchantments",
266 "can_place_on",
267 "can_break",
268 "attribute_modifiers",
269 "custom_model_data",
270#if PROTOCOL_VERSION < 770 /* < 1.21.5 */
271 "hide_additional_tooltip",
272 "hide_tooltip",
273#endif
274#if PROTOCOL_VERSION > 769 /* > 1.21.4 */
275 "tooltip_display",
276#endif
277 "repair_cost",
278 "creative_slot_lock",
279 "enchantment_glint_override",
280 "intangible_projectile",
281 "food",
282#if PROTOCOL_VERSION < 768 /* < 1.21.2 */
283 "fire_resistant",
284#endif
285#if PROTOCOL_VERSION > 767 /* > 1.21.1 */
286 "consumable",
287 "use_remainder",
288 "use_cooldown",
289 "damage_resistant",
290#endif
291 "tool",
292#if PROTOCOL_VERSION > 769 /* > 1.21.4 */
293 "weapon",
294#endif
295#if PROTOCOL_VERSION > 773 /* > 1.21.10 */
296 "attack_range",
297#endif
298#if PROTOCOL_VERSION > 767 /* > 1.21.1 */
299 "enchantable",
300 "equippable",
301 "repairable",
302 "glider",
303 "tooltip_style",
304 "death_protection",
305#endif
306#if PROTOCOL_VERSION > 769 /* > 1.21.4 */
307 "blocks_attacks",
308#endif
309#if PROTOCOL_VERSION > 773 /* > 1.21.10 */
310 "piercing_weapon",
311 "kinetic_weapon",
312#endif
313#if PROTOCOL_VERSION > 773 /* > 1.21.10 */ && PROTOCOL_VERSION < 777 /* < 26.3 */
314 "swing_animation",
315#endif
316#if PROTOCOL_VERSION > 776 /* > 26.2 */
317 "attack_animation",
318 "interact_animation",
319#endif
320#if PROTOCOL_VERSION > 774 /* > 1.21.11 */
321 "additional_trade_cost",
322#endif
323#if PROTOCOL_VERSION > 776 /* > 26.2 */
324 "block_transformer",
325 "villager_food",
326#endif
327 "stored_enchantments",
328#if PROTOCOL_VERSION > 774 /* > 1.21.11 */
329 "dye",
330#endif
331 "dyed_color",
332#if PROTOCOL_VERSION < 777 /* < 26.3 */
333 "map_color",
334#endif
335 "map_id",
336 "map_decorations",
337 "map_post_processing",
338 "charged_projectiles",
339 "bundle_contents",
340 "potion_contents",
341#if PROTOCOL_VERSION > 769 /* > 1.21.4 */
342 "potion_duration_scale",
343#endif
344 "suspicious_stew_effects",
345 "writable_book_content",
346 "written_book_content",
347 "trim",
348 "debug_stick_state",
349 "entity_data",
350 "bucket_entity_data",
351 "block_entity_data",
352 "instrument",
353#if PROTOCOL_VERSION > 769 /* > 1.21.4 */
354 "provides_trim_material",
355#endif
356 "ominous_bottle_amplifier",
357#if PROTOCOL_VERSION > 766 /* > 1.20.6 */
358 "jukebox_playable",
359#endif
360#if PROTOCOL_VERSION > 769 /* > 1.21.4 */
361 "provides_banner_patterns",
362#endif
363 "recipes",
364 "lodestone_tracker",
365 "firework_explosion",
366 "fireworks",
367 "profile",
368 "note_block_sound",
369 "banner_patterns",
370 "base_color",
371 "pot_decorations",
372 "container",
373 "block_state",
374 "bees",
375#if PROTOCOL_VERSION > 775 /* > 26.1.2 */
376 "sulfur_cube_content",
377#endif
378 "lock",
379 "container_loot",
380#if PROTOCOL_VERSION > 769 /* > 1.21.4 */
381 "break_sound",
382#if PROTOCOL_VERSION > 776 /* > 26.2 */
383 "compostable",
384 "cooking_fuel",
385 "brewing_fuel",
386 "mob_visibility",
387#endif
388 "villager/variant",
389 "wolf/variant",
390 "wolf/sound_variant",
391 "wolf/collar",
392 "fox/variant",
393 "salmon/size",
394 "parrot/variant",
395 "tropical_fish/pattern",
396 "tropical_fish/base_color",
397 "tropical_fish/pattern_color",
398 "mooshroom/variant",
399 "rabbit/variant",
400 "pig/variant",
401#if PROTOCOL_VERSION > 774 /* > 1.21.11 */
402 "pig/sound_variant",
403#endif
404 "cow/variant",
405#if PROTOCOL_VERSION > 774 /* > 1.21.11 */
406 "cow/sound_variant",
407#endif
408 "chicken/variant",
409#if PROTOCOL_VERSION > 774 /* > 1.21.11 */
410 "chicken/sound_variant",
411#endif
412#if PROTOCOL_VERSION > 773 /* > 1.21.10 */
413 "zombie_nautilus/variant",
414#endif
415 "frog/variant",
416 "horse/variant",
417 "painting/variant",
418 "llama/variant",
419 "axolotl/variant",
420 "cat/variant",
421#if PROTOCOL_VERSION > 774 /* > 1.21.11 */
422 "cat/sound_variant",
423#endif
424 "cat/collar",
425 "sheep/color",
426 "shulker/color",
427#endif
428#if PROTOCOL_VERSION > 776 /* > 26.2 */
429 "provides_pottery_pattern",
430 "sign_text_front",
431 "sign_text_back",
432 "waxed",
433 "cushion/color",
434#endif
435 };
436 if (type <= DataComponentTypes::None || type >= DataComponentTypes::NUM_DATA_COMPONENT_TYPES)
437 {
438 return "";
439 }
440 return names[static_cast<size_t>(type)];
441 }
442
443 std::shared_ptr<DataComponentType> CreateComponentType(const DataComponentTypes type)
444 {
445 if (type <= DataComponentTypes::None || type >= DataComponentTypes::NUM_DATA_COMPONENT_TYPES)
446 {
447 throw std::runtime_error("Unable to create data component with id: " + std::to_string(static_cast<int>(type)) + ".");
448 }
449
450 switch (type)
451 {
454 return std::make_shared<DataComponentTypeAdventureModePredicate>();
456 return std::make_shared<DataComponentTypeArmorTrim>();
458 return std::make_shared<DataComponentTypeBannerPatternLayers>();
460 return std::make_shared<DataComponentTypeBlockItemStateProperties>();
462 return std::make_shared<DataComponentTypeBoolean>();
464 return std::make_shared<DataComponentTypeBundleContents>();
466 return std::make_shared<DataComponentTypeChargedProjectiles>();
469 return std::make_shared<DataComponentTypeComponent>();
471#if PROTOCOL_VERSION < 773 /* < 1.21.9 */
474#endif
475 return std::make_shared<DataComponentTypeCustomData>();
477 return std::make_shared<DataComponentTypeCustomModelData>();
485 // Component with no network serializer specified fallback to NBT
486 return std::make_shared<DataComponentTypeDefault>();
488 return std::make_shared<DataComponentTypeDyeColor>();
490 return std::make_shared<DataComponentTypeDyedItemColor>();
492 return std::make_shared<DataComponentTypeFireworkExplosion>();
494 return std::make_shared<DataComponentTypeFireworks>();
496 return std::make_shared<DataComponentTypeFoodProperties>();
498 return std::make_shared<DataComponentTypeInstrument>();
504#if PROTOCOL_VERSION > 767 /* > 1.21.1 */
506#endif
507 return std::make_shared<DataComponentTypeInteger>();
509 return std::make_shared<DataComponentTypeItemAttributeModifiers>();
511 return std::make_shared<DataComponentTypeItemContainerContents>();
514 return std::make_shared<DataComponentTypeItemEnchantments>();
516 return std::make_shared<DataComponentTypeItemLore>();
517#if PROTOCOL_VERSION > 766 /* > 1.20.6 */
519 return std::make_shared<DataComponentTypeJukeboxPlayable>();
520#endif
522 return std::make_shared<DataComponentTypeListBeehiveBlockEntityOccupant>();
524 return std::make_shared<DataComponentTypeLodestoneTracker>();
526 return std::make_shared<DataComponentTypeMapId>();
527#if PROTOCOL_VERSION < 777 /* < 26.3 */
528 case DataComponentTypes::MapColor:
529 return std::make_shared<DataComponentTypeMapItemColor>();
530#endif
532 return std::make_shared<DataComponentTypeMapPostProcessing>();
534 return std::make_shared<DataComponentTypePotDecorations>();
536 return std::make_shared<DataComponentTypePotionContents>();
538 return std::make_shared<DataComponentTypeRarity>();
540 return std::make_shared<DataComponentTypeResolvableProfile>();
541#if PROTOCOL_VERSION > 767 /* > 1.21.1 */
543 return std::make_shared<DataComponentTypeDamageResistant>();
546#endif
548#if PROTOCOL_VERSION < 774 /* < 1.21.11 */
549 return std::make_shared<DataComponentTypeResourceLocation>();
550#else
551 return std::make_shared<DataComponentTypeIdentifier>();
552#endif
554 return std::make_shared<DataComponentTypeSuspiciousStewEffects>();
556 return std::make_shared<DataComponentTypeTool>();
557#if PROTOCOL_VERSION < 770 /* < 1.21.5 */
559 return std::make_shared<DataComponentTypeUnbreakable>();
560#else
561 // From custom Unbreakable to Unit in 1.21.5
563#endif
564#if PROTOCOL_VERSION < 770 /* < 1.21.5 */
565 case DataComponentTypes::HideAdditionalTooltip:
566 case DataComponentTypes::HideTooltip:
567#endif
569#if PROTOCOL_VERSION < 768 /* < 1.21.2 */
570 case DataComponentTypes::FireResistant:
571#endif
572#if PROTOCOL_VERSION > 767 /* > 1.21.1 */
574#endif
575 return std::make_shared<DataComponentTypeUnit>();
577 return std::make_shared<DataComponentTypeWritableBookContent>();
579 return std::make_shared<DataComponentTypeWrittenBookContent>();
580#if PROTOCOL_VERSION > 767 /* > 1.21.1 */
582 return std::make_shared<DataComponentTypeConsumable>();
584 return std::make_shared<DataComponentTypeDeathProtection>();
586 return std::make_shared<DataComponentTypeEquippable>();
588 return std::make_shared<DataComponentTypeRepairable>();
590 return std::make_shared<DataComponentTypeUseCooldown>();
592 return std::make_shared<DataComponentTypeUseRemainder>();
593#endif
594#if PROTOCOL_VERSION > 769 /* > 1.21.4 */
596 return std::make_shared<DataComponentTypeBlocksAttacks>();
598 return std::make_shared<DataComponentTypeBreakSound>();
600 return std::make_shared<DataComponentTypePaintingVariant>();
602 return std::make_shared<DataComponentTypeFloat>();
604 return std::make_shared<DataComponentTypeProvidesBannerPattern>();
606 return std::make_shared<DataComponentTypeProvidesTrimMaterial>();
608 return std::make_shared<DataComponentTypeTooltipDisplay>();
610 return std::make_shared<DataComponentTypeWeapon>();
611#if PROTOCOL_VERSION < 775 /* < 1.21.11 */
613 return std::make_shared<DataComponentTypeEitherRegistryVarint>();
614#endif
631 return std::make_shared<DataComponentTypeVariantEnum>();
638 return std::make_shared<DataComponentTypeDyeColor>();
639#endif
640#if PROTOCOL_VERSION > 772 /* > 1.21.8 */
643 return std::make_shared<DataComponentTypeTypedEntityData>();
644#endif
645#if PROTOCOL_VERSION > 773 /* > 1.21.10 */
647 return std::make_shared<DataComponentTypeUseEffects>();
649 return std::make_shared<DataComponentTypeFloat>();
651 return std::make_shared<DataComponentTypeAttackRange>();
652#if PROTOCOL_VERSION < 775 /* < 1.21.11 */
655 return std::make_shared<DataComponentTypeEitherRegistryVarint>();
656#endif
658 return std::make_shared<DataComponentTypePiercingWeapon>();
660 return std::make_shared<DataComponentTypeKineticWeapon>();
661#endif
662#if PROTOCOL_VERSION > 773 /* > 1.21.10 */ && PROTOCOL_VERSION < 777 /* < 26.3 */
663 case DataComponentTypes::SwingAnimation:
664 return std::make_shared<DataComponentTypeSwingAnimation>();
665#endif
666#if PROTOCOL_VERSION > 774 /* > 1.21.11 */
668 return std::make_shared<DataComponentTypeDyeColor>();
676 return std::make_shared<DataComponentTypeVariantEnum>();
678 return std::make_shared<DataComponentTypeInteger>();
679#endif
680#if PROTOCOL_VERSION > 775 /* > 26.1.2 */
682 return std::make_shared<DataComponentTypeSulfurCubeContent>();
683#endif
684#if PROTOCOL_VERSION > 776 /* > 26.2 */
687 return std::make_shared<DataComponentTypeSwingAnimation>();
689 return std::make_shared<DataComponentTypeBrewingFuel>();
691 return std::make_shared<DataComponentTypeCompostable>();
693 return std::make_shared<DataComponentTypeCookingFuel>();
696 return std::make_shared<DataComponentTypeVariantEnum>();
698 return std::make_shared<DataComponentTypeMobVisibility>();
701 return std::make_shared<DataComponentTypeSignText>();
703 return std::make_shared<DataComponentTypeVariantEnum>();
705 return std::make_shared<DataComponentTypeUnit>();
707 return std::make_shared<DataComponentTypeDyeColor>();
708#endif
709 default:
710 // Should never happen but will make the compilers happy
711 throw std::runtime_error("Unable to create data component with id: " + std::to_string(static_cast<int>(type)) + ".");
712 }
713 }
714
719
720 const std::map<DataComponentTypes, std::shared_ptr<DataComponentType>>& DataComponentPredicate::GetMap() const
721 {
722 return map;
723 }
724
725 DataComponentPredicate& DataComponentPredicate::SetMap(const std::map<DataComponentTypes, std::shared_ptr<DataComponentType>>& map_)
726 {
727 map = map_;
728 return *this;
729 }
730
732 {
733 // special case, dynamic factory
734 const int map_size = ReadData<VarInt>(iter, length);
735 map.clear();
736 for (int i = 0; i < map_size; ++i)
737 {
738 const DataComponentTypes k = ReadData<DataComponentTypes, VarInt>(iter, length);
739 std::shared_ptr<DataComponentType> v = CreateComponentType(k);
740 if (v != nullptr)
741 {
742 v->Read(iter, length);
743 }
744 map[k] = v;
745 }
746 }
747
749 {
750 // special case, dynamic factory
751 WriteData<VarInt>(static_cast<int>(map.size()), container);
752 for (const auto& [k, v] : map)
753 {
754 WriteData<DataComponentTypes, VarInt>(k, container);
755 if (v != nullptr)
756 {
757 v->Write(container);
758 }
759 }
760 }
761
763 {
764 Json::Value output;
765
766 output["map"] = Json::Array();
767 for (const auto& p : map)
768 {
769 output["map"].push_back({
770 { "name", DataComponentTypesToString(p.first) },
771 { "data", p.second == nullptr ? nullptr : p.second->Serialize() }
772 });
773 }
774
775 return output;
776 }
777
778
779#if PROTOCOL_VERSION > 769 /* > 1.21.4 */
784#endif
785
786
791
792 const std::map<DataComponentTypes, std::shared_ptr<DataComponentType>>& DataComponentPatch::GetMap() const
793 {
794 return map;
795 }
796
797 DataComponentPatch& DataComponentPatch::SetMap(const std::map<DataComponentTypes, std::shared_ptr<DataComponentType>>& map_)
798 {
799 map = map_;
800 return *this;
801 }
802
803 void DataComponentPatch::ReadImpl(ReadIterator& iter, size_t& length)
804 {
805 const int num_data = ReadData<VarInt>(iter, length);
806 const int num_void = ReadData<VarInt>(iter, length);
807
808 map.clear();
809
810 for (int i = 0; i < num_data; ++i)
811 {
812 const DataComponentTypes type = ReadData<DataComponentTypes, VarInt>(iter, length);
813 std::shared_ptr<DataComponentType> data = CreateComponentType(type);
814
815 if (data != nullptr)
816 {
817 data->Read(iter, length);
818 }
819 map.insert({ type, data });
820 }
821
822 for (int i = 0; i < num_void; ++i)
823 {
824 const DataComponentTypes type = ReadData<DataComponentTypes, VarInt>(iter, length);
825 map.insert({ type, nullptr });
826 }
827 }
828
830 {
831 int num_data = 0;
832 for (const auto& p : map)
833 {
834 num_data += p.second != nullptr;
835 }
836 const int num_void = static_cast<int>(map.size()) - num_data;
837
838 WriteData<VarInt>(num_data, container);
839 WriteData<VarInt>(num_void, container);
840
841 for (const auto& p : map)
842 {
843 if (p.second == nullptr)
844 {
845 continue;
846 }
847 WriteData<DataComponentTypes, VarInt>(p.first, container);
848 p.second->Write(container);
849 }
850
851 for (const auto& p : map)
852 {
853 if (p.second != nullptr)
854 {
855 continue;
856 }
857 WriteData<DataComponentTypes, VarInt>(p.first, container);
858 }
859 }
860
862 {
863 Json::Value output;
864
865 output["map"] = Json::Array();
866 for (const auto& p : map)
867 {
868 output["map"].push_back({
869 { "name", DataComponentTypesToString(p.first) },
870 { "data", p.second == nullptr ? nullptr : p.second->Serialize() }
871 });
872 }
873
874 return output;
875 }
876
877#if PROTOCOL_VERSION > 769 /* > 1.21.4 */
879 {
880 for (const auto& [k, v] : patch.GetMap())
881 {
882 if (v == nullptr)
883 {
884 RemovedComponents.push_back(k);
885 }
886 else
887 {
888 // TODO: Fix this, this is *NOT* how the components are serialized to bytes
889 std::vector<unsigned char> serialized;
890 WriteData<DataComponentTypes, VarInt>(k, serialized);
891 v->Write(serialized);
892 // This however should be the right CRC32C hash calculation
893 // CRC32c polynomial, from https://en.wikipedia.org/wiki/Cyclic_redundancy_check#Polynomial_representations
894 // no idea why we need the reversed one but it works
895 constexpr unsigned int polynomial = 0x82F63B78;
896
897 unsigned int crc = 0xFFFFFFFF;
898
899 for (const auto byte : serialized)
900 {
901 crc ^= byte;
902 // TODO: replace slow loop with a faster version with lookup table and cool fancy stuff ?
903 for (int i = 0; i < 8; ++i)
904 {
905 // Trick to replace if branch
906 // if (crc & 1) crc = (crc >> 1) ^ polynomial;
907 // else crc = crc >> 1;
908 // Works because a ^ 0 == a
909 crc = (crc >> 1) ^ (crc & 1) * polynomial;
910 }
911 }
912 AddedComponents[k] = static_cast<int>(~crc);
913 }
914 }
915 }
916
918#endif
919
920#if PROTOCOL_VERSION > 769 /* > 1.21.4 */
925
926 const std::map<DataComponentTypes, std::shared_ptr<DataComponentType>>& LengthPrefixedDataComponentPatch::GetMap() const
927 {
928 return map;
929 }
930
931 LengthPrefixedDataComponentPatch& LengthPrefixedDataComponentPatch::SetMap(const std::map<DataComponentTypes, std::shared_ptr<DataComponentType>>& map_)
932 {
933 map = map_;
934 return *this;
935 }
936
938 {
939 const int num_data = ReadData<VarInt>(iter, length);
940 const int num_void = ReadData<VarInt>(iter, length);
941
942 map.clear();
943
944 for (int i = 0; i < num_data; ++i)
945 {
946 const DataComponentTypes type = ReadData<DataComponentTypes, VarInt>(iter, length);
947 const int data_size = ReadData<VarInt>(iter, length);
948 std::shared_ptr<DataComponentType> data = CreateComponentType(type);
949
950 if (data != nullptr)
951 {
952 data->Read(iter, length);
953 }
954 map.insert({ type, data });
955 }
956
957 for (int i = 0; i < num_void; ++i)
958 {
959 const DataComponentTypes type = ReadData<DataComponentTypes, VarInt>(iter, length);
960 map.insert({ type, nullptr });
961 }
962 }
963
965 {
966 int num_data = 0;
967 for (const auto& p : map)
968 {
969 num_data += p.second != nullptr;
970 }
971 const int num_void = static_cast<int>(map.size()) - num_data;
972
973 WriteData<VarInt>(num_data, container);
974 WriteData<VarInt>(num_void, container);
975
976 for (const auto& p : map)
977 {
978 if (p.second == nullptr)
979 {
980 continue;
981 }
982 WriteData<DataComponentTypes, VarInt>(p.first, container);
983 WriteContainer temp;
984 // Serialize to an intermediate vector to get the size
985 p.second->Write(temp);
986 WriteData<VarInt>(static_cast<int>(temp.size()), container);
987 WriteByteArray(temp, container);
988 }
989
990 for (const auto& p : map)
991 {
992 if (p.second != nullptr)
993 {
994 continue;
995 }
996 WriteData<DataComponentTypes, VarInt>(p.first, container);
997 }
998 }
999
1001 {
1002 Json::Value output;
1003
1004 output["map"] = Json::Array();
1005 for (const auto& p : map)
1006 {
1007 output["map"].push_back({
1008 { "name", DataComponentTypesToString(p.first) },
1009 { "data", p.second == nullptr ? nullptr : p.second->Serialize() }
1010 });
1011 }
1012
1013 return output;
1014 }
1015#endif
1016 }
1017}
1018#endif
#define DEFINE_NETWORK_TYPE(ClassName)
virtual Json::Value SerializeImpl() const override
std::map< DataComponentTypes, std::shared_ptr< DataComponentType > > map
const std::map< DataComponentTypes, std::shared_ptr< DataComponentType > > & GetMap() const
virtual void ReadImpl(ReadIterator &iter, size_t &length) override
DataComponentPatch & SetMap(const std::map< DataComponentTypes, std::shared_ptr< DataComponentType > > &map_)
virtual void WriteImpl(WriteContainer &container) const override
virtual void WriteImpl(WriteContainer &container) const override
std::map< DataComponentTypes, std::shared_ptr< DataComponentType > > map
virtual Json::Value SerializeImpl() const override
virtual void ReadImpl(ReadIterator &iter, size_t &length) override
const std::map< DataComponentTypes, std::shared_ptr< DataComponentType > > & GetMap() const
DataComponentPredicate & SetMap(const std::map< DataComponentTypes, std::shared_ptr< DataComponentType > > &map_)
virtual void ReadImpl(ReadIterator &iter, size_t &length) override
std::map< DataComponentTypes, std::shared_ptr< DataComponentType > > map
const std::map< DataComponentTypes, std::shared_ptr< DataComponentType > > & GetMap() const
virtual void WriteImpl(WriteContainer &container) const override
LengthPrefixedDataComponentPatch & SetMap(const std::map< DataComponentTypes, std::shared_ptr< DataComponentType > > &map_)
Real class declaration, just a derived class of std::vector<Value>
Definition Json.hpp:180
Main class, basically a JsonVariant with extra utility functions it doesn't inherit JsonVariant direc...
Definition Json.hpp:45
void push_back(const Value &value)
Definition Json.cpp:257
std::shared_ptr< DataComponentType > CreateComponentType(const DataComponentTypes type)
std::string_view DataComponentTypesToString(const DataComponentTypes type)
std::vector< unsigned char > WriteContainer
std::vector< unsigned char >::const_iterator ReadIterator
void WriteByteArray(const std::vector< unsigned char > &my_array, WriteContainer &container)