diff options
Diffstat (limited to 'src/prototype_manager.cpp')
| -rw-r--r-- | src/prototype_manager.cpp | 71 |
1 files changed, 71 insertions, 0 deletions
| diff --git a/src/prototype_manager.cpp b/src/prototype_manager.cpp new file mode 100644 index 0000000..250aa59 --- /dev/null +++ b/src/prototype_manager.cpp | |||
| @@ -0,0 +1,71 @@ | |||
| 1 | #include "prototype_manager.h" | ||
| 2 | #include <utility> | ||
| 3 | #include <stdexcept> | ||
| 4 | #include "xml.h" | ||
| 5 | |||
| 6 | PrototypeManager::PrototypeManager(std::string path) | ||
| 7 | { | ||
| 8 | xmlDocPtr doc = xmlParseFile(path.c_str()); | ||
| 9 | if (doc == nullptr) | ||
| 10 | { | ||
| 11 | throw std::invalid_argument("Cannot find prototypes file"); | ||
| 12 | } | ||
| 13 | |||
| 14 | xmlNodePtr top = xmlDocGetRootElement(doc); | ||
| 15 | if (top == nullptr) | ||
| 16 | { | ||
| 17 | throw std::invalid_argument("Error parsing prototypes file"); | ||
| 18 | } | ||
| 19 | |||
| 20 | if (xmlStrcmp(top->name, reinterpret_cast<const xmlChar*>("entities"))) | ||
| 21 | { | ||
| 22 | throw std::invalid_argument("Error parsing prototypes file"); | ||
| 23 | } | ||
| 24 | |||
| 25 | xmlChar* key = nullptr; | ||
| 26 | |||
| 27 | for (xmlNodePtr node = top->xmlChildrenNode; | ||
| 28 | node != nullptr; | ||
| 29 | node = node->next) | ||
| 30 | { | ||
| 31 | if (!xmlStrcmp(node->name, reinterpret_cast<const xmlChar*>("entity"))) | ||
| 32 | { | ||
| 33 | key = getProp(node, "id"); | ||
| 34 | std::string prototypeName(reinterpret_cast<char*>(key)); | ||
| 35 | xmlFree(key); | ||
| 36 | |||
| 37 | key = getProp(node, "sprite"); | ||
| 38 | std::string spritePath(reinterpret_cast<char*>(key)); | ||
| 39 | xmlFree(key); | ||
| 40 | |||
| 41 | key = getProp(node, "width"); | ||
| 42 | int width = atoi(reinterpret_cast<char*>(key)); | ||
| 43 | xmlFree(key); | ||
| 44 | |||
| 45 | key = getProp(node, "height"); | ||
| 46 | int height = atoi(reinterpret_cast<char*>(key)); | ||
| 47 | xmlFree(key); | ||
| 48 | |||
| 49 | key = getProp(node, "action"); | ||
| 50 | std::string actionStr(reinterpret_cast<char*>(key)); | ||
| 51 | xmlFree(key); | ||
| 52 | |||
| 53 | Prototype::Action pAction = Prototype::Action::none; | ||
| 54 | if (actionStr == "save") | ||
| 55 | { | ||
| 56 | pAction = Prototype::Action::save; | ||
| 57 | } | ||
| 58 | |||
| 59 | prototypes_.emplace( | ||
| 60 | std::piecewise_construct, | ||
| 61 | std::tie(prototypeName), | ||
| 62 | std::tie( | ||
| 63 | width, | ||
| 64 | height, | ||
| 65 | spritePath, | ||
| 66 | pAction)); | ||
| 67 | } | ||
| 68 | } | ||
| 69 | |||
| 70 | xmlFreeDoc(doc); | ||
| 71 | } | ||
