#include #include #include #include #include "BlockRegistry.h" namespace BlockRegistry { static Block NULL_BLOCK(0, "null_block"); static std::unordered_map registry; static std::vector idMap = {NULL_BLOCK}; void loadFromFile(const std::string& path) { std::ifstream in; in.open(path); if(in.fail()) { std::cout << "cannot read blocks from file '" << path << "'" << std::endl; return; } std::string line; while(true) { std::getline(in, line, '\n'); if(in.fail()) { break; } u16 id = idMap.size(); std::cout << id << std::endl; idMap.emplace_back(id, line); registry[line] = id; } } const Block& getBlock(const std::string& name) { const std::unordered_map::const_iterator& iter = registry.find(name); if(iter == registry.end()) { return NULL_BLOCK; } return idMap[iter->second]; } const Block& getBlock(u16 id) { if(id >= idMap.size()) { return NULL_BLOCK; } return idMap[id]; } }