From 5265382d9d1bcd4ec88dbf4e980f254bc6a0d7c1 Mon Sep 17 00:00:00 2001 From: Nemirtingas Date: Tue, 25 Jun 2019 15:04:09 +0200 Subject: [PATCH] Added Json item loader Added Json item loader --- dll/item_db_loader.cpp | 57 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 dll/item_db_loader.cpp diff --git a/dll/item_db_loader.cpp b/dll/item_db_loader.cpp new file mode 100644 index 0000000..9817be5 --- /dev/null +++ b/dll/item_db_loader.cpp @@ -0,0 +1,57 @@ +/* Copyright (C) 2019 Nemirtingas (Maxime P) + This file is part of the Goldberg Emulator + + The Goldberg Emulator is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 3 of the License, or (at your option) any later version. + + The Goldberg Emulator is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the Goldberg Emulator; if not, see + . */ +#include "item_db_loader.h" + +#include +#include "json.hpp" + +std::map> read_items_db(std::string const& items_db) +{ + std::map> items; + + std::ifstream items_file(items_db); + items_file.seekg(0, std::ios::end); + size_t size = items_file.tellg(); + std::string buffer(size, '\0'); + items_file.seekg(0); + items_file.read(&buffer[0], size); + items_file.close(); + + try + { + std::map> tmp; + nlohmann::json json = nlohmann::json::parse(buffer); + + for (auto& i : json.items()) + { + SteamItemDef_t key = std::stoi((*i).key()); + nlohmann::json& value = (*i).value(); + for( auto& j : value.items() ) + { + tmp[key][(*j).key()] = (*j).value(); + } + } + + items.swap(tmp); + } + catch (std::exception& e) + { + PRINT_DEBUG("Error while parsing json: %s", e.what()); + } + + return items; +} \ No newline at end of file