Files
metin2-server/src/server/common/port_item_extra.h
T

35 lines
1.3 KiB
C++

#pragma once
#include <stdint.h>
#include <string>
#include <cstring>
// PORT: supplementary AVA2 pet/rarity state. Classic item packets retain 3 sockets / 7 attributes.
#pragma pack(push,1)
struct PortItemExtra {
int32_t sockets[5];
uint8_t types[8];
int16_t values[8];
char name[25];
};
#pragma pack(pop)
static_assert(sizeof(PortItemExtra)==69,"extended item state");
inline std::string PortItemExtraHex(const PortItemExtra& extra) {
static const char digits[]="0123456789abcdef";
const auto p=reinterpret_cast<const unsigned char*>(&extra);
std::string result(sizeof(extra)*2,'0');
for(unsigned i=0;i<sizeof(extra);++i){result[2*i]=digits[p[i]>>4];result[2*i+1]=digits[p[i]&15];}
return result;
}
inline bool PortItemExtraRead(const char* hex,PortItemExtra& extra) {
PortItemExtra decoded = {};
if(!hex)return false;
if(!*hex){extra=decoded;return true;}
if(std::strlen(hex)!=sizeof(decoded)*2)return false;
auto nibble=[](char c){return c>='0' && c<='9'?c-'0':c>='A' && c<='F'?c-'A'+10:c>='a' && c<='f'?c-'a'+10:-1;};
auto p=reinterpret_cast<unsigned char*>(&decoded);
for(unsigned i=0;i<sizeof(decoded);++i){int a=nibble(hex[2*i]),b=nibble(hex[2*i+1]);if(a<0 || b<0)return false;p[i]=static_cast<unsigned char>((a<<4)|b);}
if(decoded.name[24]!=0)return false;
extra=decoded;return true;
}