123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- #include <unordered_map>
- #include <vector>
- #include <fstream>
- #include <iostream>
- #include "BlockRegistry.h"
- namespace BlockRegistry
- {
- static Block NULL_BLOCK(0, "null_block");
- static std::unordered_map<std::string, u16> registry;
- static std::vector<Block> 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<std::string, u16>::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];
- }
- }
|