refactor Content
This commit is contained in:
+50
-39
@@ -43,61 +43,79 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
template<class T>
|
||||
class ContentUnitIndices {
|
||||
std::vector<T*> defs;
|
||||
public:
|
||||
ContentUnitIndices(std::vector<T*> defs) : defs(std::move(defs)) {}
|
||||
|
||||
inline T* get(blockid_t id) const {
|
||||
if (id >= defs.size()) {
|
||||
return nullptr;
|
||||
}
|
||||
return defs[id];
|
||||
}
|
||||
|
||||
inline size_t count() const {
|
||||
return defs.size();
|
||||
}
|
||||
|
||||
inline const T* const* getDefs() const {
|
||||
return defs.data();
|
||||
}
|
||||
};
|
||||
|
||||
/// @brief Runtime defs cache: indices
|
||||
class ContentIndices {
|
||||
std::vector<Block*> blockDefs;
|
||||
std::vector<ItemDef*> itemDefs;
|
||||
public:
|
||||
ContentUnitIndices<Block> blocks;
|
||||
ContentUnitIndices<ItemDef> items;
|
||||
|
||||
ContentIndices(
|
||||
std::vector<Block*> blockDefs,
|
||||
std::vector<ItemDef*> itemDefs
|
||||
);
|
||||
};
|
||||
|
||||
inline Block* getBlockDef(blockid_t id) const {
|
||||
if (id >= blockDefs.size())
|
||||
template<class T>
|
||||
class ContentUnitDefs {
|
||||
std::unordered_map<std::string, std::unique_ptr<T>> defs;
|
||||
public:
|
||||
ContentUnitDefs(std::unordered_map<std::string, std::unique_ptr<T>> defs)
|
||||
: defs(std::move(defs)) {
|
||||
}
|
||||
|
||||
T* find(const std::string& id) const {
|
||||
const auto& found = defs.find(id);
|
||||
if (found == defs.end()) {
|
||||
return nullptr;
|
||||
return blockDefs[id];
|
||||
}
|
||||
return found->second.get();
|
||||
}
|
||||
|
||||
inline ItemDef* getItemDef(itemid_t id) const {
|
||||
if (id >= itemDefs.size())
|
||||
return nullptr;
|
||||
return itemDefs[id];
|
||||
}
|
||||
|
||||
inline size_t countBlockDefs() const {
|
||||
return blockDefs.size();
|
||||
}
|
||||
|
||||
inline size_t countItemDefs() const {
|
||||
return itemDefs.size();
|
||||
}
|
||||
|
||||
// use this for critical spots to prevent range check overhead
|
||||
const Block* const* getBlockDefs() const {
|
||||
return blockDefs.data();
|
||||
}
|
||||
|
||||
const ItemDef* const* getItemDefs() const {
|
||||
return itemDefs.data();
|
||||
T& require(const std::string& id) const {
|
||||
const auto& found = defs.find(id);
|
||||
if (found == defs.end()) {
|
||||
throw std::runtime_error("missing content unit "+id);
|
||||
}
|
||||
return *found->second;
|
||||
}
|
||||
};
|
||||
|
||||
/* Content is a definitions repository */
|
||||
/// @brief Content is a definitions repository
|
||||
class Content {
|
||||
std::unordered_map<std::string, std::unique_ptr<Block>> blockDefs;
|
||||
std::unordered_map<std::string, std::unique_ptr<ItemDef>> itemDefs;
|
||||
std::unique_ptr<ContentIndices> indices;
|
||||
std::unordered_map<std::string, std::unique_ptr<ContentPackRuntime>> packs;
|
||||
std::unordered_map<std::string, std::unique_ptr<BlockMaterial>> blockMaterials;
|
||||
public:
|
||||
ContentUnitDefs<Block> blocks;
|
||||
ContentUnitDefs<ItemDef> items;
|
||||
std::unique_ptr<DrawGroups> const drawGroups;
|
||||
|
||||
Content(
|
||||
std::unique_ptr<ContentIndices> indices,
|
||||
std::unique_ptr<DrawGroups> drawGroups,
|
||||
std::unordered_map<std::string, std::unique_ptr<Block>> blockDefs,
|
||||
std::unordered_map<std::string, std::unique_ptr<ItemDef>> itemDefs,
|
||||
ContentUnitDefs<Block> blocks,
|
||||
ContentUnitDefs<ItemDef> items,
|
||||
std::unordered_map<std::string, std::unique_ptr<ContentPackRuntime>> packs,
|
||||
std::unordered_map<std::string, std::unique_ptr<BlockMaterial>> blockMaterials
|
||||
);
|
||||
@@ -106,15 +124,8 @@ public:
|
||||
inline ContentIndices* getIndices() const {
|
||||
return indices.get();
|
||||
}
|
||||
|
||||
Block* findBlock(const std::string& id) const;
|
||||
Block& requireBlock(const std::string& id) const;
|
||||
|
||||
ItemDef* findItem(const std::string& id) const;
|
||||
ItemDef& requireItem(const std::string& id) const;
|
||||
|
||||
const BlockMaterial* findBlockMaterial(const std::string& id) const;
|
||||
|
||||
const ContentPackRuntime* getPackRuntime(const std::string& id) const;
|
||||
|
||||
const std::unordered_map<std::string, std::unique_ptr<BlockMaterial>>& getBlockMaterials() const;
|
||||
|
||||
Reference in New Issue
Block a user