Untitled
#include <vector> #include <string> #include <cstring> #include <optional> #include <enet/enet.h> void sendVariant(ENetPeer* peer, const std::optional<int>& delay = std::nullopt, const std::optional<int>& netID = std::nullopt, const std::vector<std::string>& strings = {}, const std::vector<int>& integers = {}, const std::vector<float>& floats = {}, const std::vector<std::pair<float, float>>& float_pairs = {}, const std::vector<std::tuple<float, float, float>>& float_triplets = {}) { std::vector<uint8_t> packet_data(61, 0); int index = 0; int defaultDelay = delay.value_or(0); int defaultNetID = netID.value_or(-1); int messageType = 0x4, packetType = 0x1, charState = 0x8; std::memcpy(packet_data.data(), &messageType, 4); std::memcpy(packet_data.data() + 4, &packetType, 4); std::memcpy(packet_data.data() + 8, &defaultNetID, 4); std::memcpy(packet_data.data() + 16, &charState, 4); std::memcpy(packet_data.data() + 24, &defaultDelay, 4); for (const auto& str : strings) { packet_data.push_back(index++); packet_data.push_back(0x2); uint16_t str_length = static_cast<uint16_t>(str.size()); const uint8_t* len_ptr = reinterpret_cast<const uint8_t*>(&str_length); packet_data.insert(packet_data.end(), len_ptr, len_ptr + 2); packet_data.insert(packet_data.end(), str.begin(), str.end()); } for (const auto& integer : integers) { packet_data.push_back(index++); packet_data.push_back(0x9); const uint8_t* int_ptr = reinterpret_cast<const uint8_t*>(&integer); packet_data.insert(packet_data.end(), int_ptr, int_ptr + sizeof(int)); } for (const auto& value : floats) { packet_data.push_back(index++); packet_data.push_back(0x1); const uint8_t* float_ptr = reinterpret_cast<const uint8_t*>(&value); packet_data.insert(packet_data.end(), float_ptr, float_ptr + sizeof(float)); } for (const auto& pair : float_pairs) { packet_data.push_back(index++); packet_data.push_back(0x3); const float* pair_values = &pair.first; const uint8_t* pair_ptr = reinterpret_cast<const uint8_t*>(pair_values); packet_data.insert(packet_data.end(), pair_ptr, pair_ptr + 2 * sizeof(float)); } for (const auto& triplet : float_triplets) { packet_data.push_back(index++); packet_data.push_back(0x4); const float* triplet_values = &std::get<0>(triplet); const uint8_t* triplet_ptr = reinterpret_cast<const uint8_t*>(triplet_values); packet_data.insert(packet_data.end(), triplet_ptr, triplet_ptr + 3 * sizeof(float)); } ENetPacket* packet = enet_packet_create(packet_data.data(), packet_data.size(), ENET_PACKET_FLAG_RELIABLE); enet_peer_send(peer, 0, packet); }
Leave a Comment