summary refs log tree commit diff stats
path: root/src/prototype_manager.cpp
blob: 250aa5940d1d55f7cf4bf453c57b9f3142baad9d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include "prototype_manager.h"
#include <utility>
#include <stdexcept>
#include "xml.h"

PrototypeManager::PrototypeManager(std::string path)
{
  xmlDocPtr doc = xmlParseFile(path.c_str());
  if (doc == nullptr)
  {
    throw std::invalid_argument("Cannot find prototypes file");
  }

  xmlNodePtr top = xmlDocGetRootElement(doc);
  if (top == nullptr)
  {
    throw std::invalid_argument("Error parsing prototypes file");
  }

  if (xmlStrcmp(top->name, reinterpret_cast<const xmlChar*>("entities")))
  {
    throw std::invalid_argument("Error parsing prototypes file");
  }

  xmlChar* key = nullptr;

  for (xmlNodePtr node = top->xmlChildrenNode;
    node != nullptr;
    node = node->next)
  {
    if (!xmlStrcmp(node->name, reinterpret_cast<const xmlChar*>("entity")))
    {
      key = getProp(node, "id");
      std::string prototypeName(reinterpret_cast<char*>(key));
      xmlFree(key);

      key = getProp(node, "sprite");
      std::string spritePath(reinterpret_cast<char*>(key));
      xmlFree(key);

      key = getProp(node, "width");
      int width = atoi(reinterpret_cast<char*>(key));
      xmlFree(key);

      key = getProp(node, "height");
      int height = atoi(reinterpret_cast<char*>(key));
      xmlFree(key);

      key = getProp(node, "action");
      std::string actionStr(reinterpret_cast<char*>(key));
      xmlFree(key);

      Prototype::Action pAction = Prototype::Action::none;
      if (actionStr == "save")
      {
        pAction = Prototype::Action::save;
      }

      prototypes_.emplace(
        std::piecewise_construct,
        std::tie(prototypeName),
        std::tie(
          width,
          height,
          spritePath,
          pAction));
    }
  }

  xmlFreeDoc(doc);
}