|
@@ -1,24 +1,57 @@
|
|
|
-#include "BlockRegistry.h"
|
|
|
+#include <unordered_map>
|
|
|
+#include <vector>
|
|
|
+#include <fstream>
|
|
|
+#include <iostream>
|
|
|
|
|
|
-Block BlockRegistry::NULL_BLOCK(0);
|
|
|
+#include "BlockRegistry.h"
|
|
|
|
|
|
-BlockRegistry::BlockRegistry() : idCounter(1)
|
|
|
+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& BlockRegistry::getBlock(const std::string& name) const
|
|
|
-{
|
|
|
- const std::unordered_map<std::string, Block>::const_iterator& iter = registry.find(name);
|
|
|
- if(iter == registry.end())
|
|
|
+ const Block& getBlock(const std::string& name)
|
|
|
{
|
|
|
- return NULL_BLOCK;
|
|
|
- }
|
|
|
- return iter->second;
|
|
|
-}
|
|
|
+ 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& BlockRegistry::getBlock(BlockId id) const
|
|
|
-{
|
|
|
- return NULL_BLOCK;
|
|
|
+ const Block& getBlock(u16 id)
|
|
|
+ {
|
|
|
+ if(id >= idMap.size())
|
|
|
+ {
|
|
|
+ return NULL_BLOCK;
|
|
|
+ }
|
|
|
+ return idMap[id];
|
|
|
+ }
|
|
|
}
|
|
|
|