Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static std::string read_contents(const std::string& filename) {
- std::ifstream in(filename);
- std::stringstream sstr;
- sstr << in.rdbuf();
- return sstr.str();
- }
- static size_t nnpos(const std::string& buf, size_t pos) {
- if (pos == std::string::npos) {
- return buf.length();
- }
- return pos;
- }
- static std::string_view next_token(const std::string& buf, size_t& pos) {
- auto start = nnpos(buf, pos);
- auto limit = nnpos(buf, buf.find_first_of("\r\n", start));
- auto end = min(limit, nnpos(buf, buf.find_first_of("\t", start)));
- pos = buf.find_first_not_of("\t", end);
- return std::string_view(buf.data() + start, end - start);
- }
- static void for_each_line(const std::string& buf, std::function<void(const std::string& buf, size_t pos)> fn) {
- for (size_t pos = 0; pos <= buf.length(); pos = nnpos(buf, buf.find_first_of('\n', pos)) + 1) {
- if (pos + 1 >= buf.length() || (buf[pos] == '/' && buf[pos + 1] == '/')) {
- continue;
- }
- fn(buf, pos);
- }
- };
- static std::unordered_map<std::string_view, std::string_view> parse_spec_item(const std::string& spec_item) {
- std::unordered_map<std::string_view, std::string_view> item_id_to_loc;
- for_each_line(spec_item, [&](const std::string& buf, size_t pos) {
- next_token(buf, pos);
- auto item_id = next_token(buf, pos);
- auto item_id_loc = next_token(buf, pos);
- item_id_to_loc[item_id] = item_id_loc;
- });
- return item_id_to_loc;
- }
- static std::unordered_map<std::string_view, std::string_view> parse_prop_item_txt_txt(const std::string& loc_item) {
- std::unordered_map<std::string_view, std::string_view> loc_to_str;
- for_each_line(loc_item, [&](const std::string& buf, size_t pos) {
- auto loc_id = next_token(buf, pos);
- auto loc_str = next_token(buf, pos);
- loc_to_str[loc_id] = loc_str;
- });
- return loc_to_str;
- }
- static std::unordered_map<std::string_view, std::string_view> parse_define_item(const std::string& define_item) {
- std::unordered_map<std::string_view, std::string_view> item_id_to_val;
- for_each_line(define_item, [&](const std::string& buf, size_t pos) {
- next_token(buf, pos);
- auto define_id = next_token(buf, pos);
- auto define_val = next_token(buf, pos);
- if (define_id.empty() || define_id == "#endif" || define_id == "#ifndef" || define_val == "__DEFINE_ITEM") {
- return;
- }
- item_id_to_val[define_id] = define_val;
- });
- return item_id_to_val;
- }
- static std::unordered_map<std::string, int> populate_item_database() {
- std::unordered_map<std::string, int> item_name_to_id;
- if (!std::filesystem::exists("data/Spec_Item.txt") ||
- !std::filesystem::exists("data/propItem.txt.txt") ||
- !std::filesystem::exists("data/defineItem.h")) {
- return item_name_to_id;
- }
- std::string spec_item = read_contents("data/Spec_Item.txt");
- std::string prop_item_txt_txt = read_contents("data/propItem.txt.txt");
- std::string define_item = read_contents("data/defineItem.h");
- std::unordered_map<std::string_view, std::string_view> item_id_to_val = parse_define_item(define_item);
- std::unordered_map<std::string_view, std::string_view> item_id_to_loc = parse_spec_item(spec_item);
- std::unordered_map<std::string_view, std::string_view> loc_to_str = parse_prop_item_txt_txt(prop_item_txt_txt);
- for (auto& [define_id, define_val] : item_id_to_val) {
- try {
- item_name_to_id[std::string(loc_to_str[item_id_to_loc[define_id]])] = std::stoi(std::string(define_val));
- } catch (...) {}
- }
- return item_name_to_id;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement