From 6468af6eb72e6f1108f6f675bb5d7e8be950fca7 Mon Sep 17 00:00:00 2001 From: Martin Michelsen Date: Mon, 19 Jun 2023 15:32:27 -0700 Subject: [PATCH] fix $item with names that are also specials --- src/ItemData.cc | 63 +++++++++++++++++++++++++++++-------------------- src/ItemData.hh | 2 +- 2 files changed, 38 insertions(+), 27 deletions(-) diff --git a/src/ItemData.cc b/src/ItemData.cc index 77f24c8d..07a17096 100644 --- a/src/ItemData.cc +++ b/src/ItemData.cc @@ -1409,7 +1409,7 @@ const unordered_map name_info_for_primary_identifier({ {0x031903, "Team Points 10000"}, }); -ItemData::ItemData(const string& orig_description) { +ItemData::ItemData(const string& orig_description, bool skip_special) { this->data1d.clear(0); this->id = 0xFFFFFFFF; this->data2d = 0; @@ -1444,16 +1444,18 @@ ItemData::ItemData(const string& orig_description) { } uint8_t weapon_special = 0; - for (const auto& it : name_for_weapon_special) { - if (!it.second) { - continue; - } - string prefix = tolower(it.second); - prefix += ' '; - if (starts_with(desc, prefix)) { - weapon_special = it.first; - desc = desc.substr(prefix.size()); - break; + if (!skip_special) { + for (const auto& it : name_for_weapon_special) { + if (!it.second) { + continue; + } + string prefix = tolower(it.second); + prefix += ' '; + if (starts_with(desc, prefix)) { + weapon_special = it.first; + desc = desc.substr(prefix.size()); + break; + } } } @@ -1865,20 +1867,29 @@ ItemData item_for_string(const string& desc) { try { return ItemData(desc); } catch (const exception&) { - string data = parse_data_string(desc); - if (data.size() < 2) { - throw runtime_error("item code too short"); - } - if (data.size() > 16) { - throw runtime_error("item code too long"); - } - ItemData ret; - if (data.size() <= 12) { - memcpy(ret.data1.data(), data.data(), data.size()); - } else { - memcpy(ret.data1.data(), data.data(), 12); - memcpy(ret.data2.data(), data.data() + 12, data.size() - 12); - } - return ret; } + try { + return ItemData(desc, true); + } catch (const exception&) { + } + + string data = parse_data_string(desc); + if (data.size() < 2) { + throw runtime_error("item code too short"); + } + if (data[0] > 4) { + throw runtime_error("invalid item class"); + } + if (data.size() > 16) { + throw runtime_error("item code too long"); + } + + ItemData ret; + if (data.size() <= 12) { + memcpy(ret.data1.data(), data.data(), data.size()); + } else { + memcpy(ret.data1.data(), data.data(), 12); + memcpy(ret.data2.data(), data.data() + 12, data.size() - 12); + } + return ret; } diff --git a/src/ItemData.hh b/src/ItemData.hh index 6abd7f76..6bdf3257 100644 --- a/src/ItemData.hh +++ b/src/ItemData.hh @@ -96,7 +96,7 @@ struct ItemData { // 0x14 bytes } __attribute__((packed)); ItemData(); - explicit ItemData(const std::string& orig_description); + explicit ItemData(const std::string& orig_description, bool skip_special = false); ItemData(const ItemData& other); ItemData& operator=(const ItemData& other);