CGGuidStruct Utils::GUID_from_str(const std::string& guid_str) {
if (guid_str.empty()) {
throw std::invalid_argument("Empty GUID string");
}
auto split = [](const std::string& str, char delim) {
std::vector<std::string> tokens;
std::stringstream ss(str);
std::string item;
while (std::getline(ss, item, delim)) {
tokens.push_back(item);
}
return tokens;
};
auto guid_parts = split(guid_str, '-');
std::map<std::string, GuidType> guid_type_map = {
{"Pet", GuidType::Pet},
{"Creature", GuidType::Creature},
{"GameObject", GuidType::GameObject},
{"Vehicle", GuidType::Vehicle}
};
CGGuidStruct guid_struct;
uint64_t low = 0, high = 0;
auto guid_type_it = guid_type_map.find(guid_parts[0]);
if (guid_type_it != guid_type_map.end()) {
GuidType guid_type = guid_type_it->second;
low = (static_cast<uint64_t>(guid_type) << 5
| (std::stoull(guid_parts[2]) << 42) | (std::stoull(guid_parts[3]) << 29) | (std::stoull(guid_parts[5]) << 6);
high = (std::stoull(guid_parts[4]) << 40) | std::stoull(guid_parts[6], nullptr, 16);
}
else if (guid_parts[0] == "Player") {
low = (static_cast<uint64_t>(GuidType::Player) << 5
| (std::stoull(guid_parts[1]) << 42);
high = std::stoull(guid_parts[2], nullptr, 16);
}
else {
throw std::runtime_error("无法解析GUID_str: " + guid_str);
}
guid_struct.low = low;
guid_struct.high = high;
return guid_struct;
}