contentpack remove feature WIP

This commit is contained in:
MihailRis
2024-02-29 23:47:07 +03:00
parent a1f313bedc
commit 3862dbda66
18 changed files with 541 additions and 433 deletions
+11 -3
View File
@@ -26,7 +26,7 @@ void ContentBuilder::add(ItemDef* def) {
}
void ContentBuilder::add(ContentPackRuntime* pack) {
packs.push_back(std::unique_ptr<ContentPackRuntime>(pack));
packs.emplace(pack->getId(), pack);
}
Block& ContentBuilder::createBlock(std::string id) {
@@ -136,7 +136,7 @@ Content::Content(ContentIndices* indices,
std::unique_ptr<DrawGroups> drawGroups,
std::unordered_map<std::string, Block*> blockDefs,
std::unordered_map<std::string, ItemDef*> itemDefs,
std::vector<std::unique_ptr<ContentPackRuntime>> packs)
std::unordered_map<std::string, std::unique_ptr<ContentPackRuntime>> packs)
: blockDefs(blockDefs),
itemDefs(itemDefs),
indices(indices),
@@ -179,6 +179,14 @@ ItemDef& Content::requireItem(std::string id) const {
return *found->second;
}
const std::vector<std::unique_ptr<ContentPackRuntime>>& Content::getPacks() const {
const ContentPackRuntime* Content::getPackRuntime(std::string id) const {
auto found = packs.find(id);
if (found == packs.end()) {
return nullptr;
}
return found->second.get();
}
const std::unordered_map<std::string, std::unique_ptr<ContentPackRuntime>>& Content::getPacks() const {
return packs;
}
+6 -4
View File
@@ -48,7 +48,7 @@ class ContentBuilder {
std::unordered_map<std::string, ItemDef*> itemDefs;
std::vector<std::string> itemIds;
std::vector<std::unique_ptr<ContentPackRuntime>> packs;
std::unordered_map<std::string, std::unique_ptr<ContentPackRuntime>> packs;
public:
~ContentBuilder();
@@ -108,7 +108,7 @@ class Content {
std::unordered_map<std::string, Block*> blockDefs;
std::unordered_map<std::string, ItemDef*> itemDefs;
std::unique_ptr<ContentIndices> indices;
std::vector<std::unique_ptr<ContentPackRuntime>> packs;
std::unordered_map<std::string, std::unique_ptr<ContentPackRuntime>> packs;
public:
std::unique_ptr<DrawGroups> const drawGroups;
@@ -116,7 +116,7 @@ public:
std::unique_ptr<DrawGroups> drawGroups,
std::unordered_map<std::string, Block*> blockDefs,
std::unordered_map<std::string, ItemDef*> itemDefs,
std::vector<std::unique_ptr<ContentPackRuntime>> packs);
std::unordered_map<std::string, std::unique_ptr<ContentPackRuntime>> packs);
~Content();
inline ContentIndices* getIndices() const {
@@ -129,7 +129,9 @@ public:
ItemDef* findItem(std::string id) const;
ItemDef& requireItem(std::string id) const;
const std::vector<std::unique_ptr<ContentPackRuntime>>& getPacks() const;
const ContentPackRuntime* getPackRuntime(std::string id) const;
const std::unordered_map<std::string, std::unique_ptr<ContentPackRuntime>>& getPacks() const;
};
#endif // CONTENT_CONTENT_H_
+5
View File
@@ -294,6 +294,7 @@ void ContentLoader::load(ContentBuilder& builder) {
auto runtime = new ContentPackRuntime(*pack, scripting::create_pack_environment(*pack));
builder.add(runtime);
env = runtime->getEnvironment()->getId();
ContentPackStats& stats = runtime->getStatsWriteable();
fixPackIndices();
@@ -306,6 +307,7 @@ void ContentLoader::load(ContentBuilder& builder) {
if (!fs::is_regular_file(pack->getContentFile()))
return;
auto root = files::read_json(pack->getContentFile());
auto blocksarr = root->list("blocks");
if (blocksarr) {
@@ -314,6 +316,7 @@ void ContentLoader::load(ContentBuilder& builder) {
std::string full = pack->id+":"+name;
auto& def = builder.createBlock(full);
loadBlock(def, full, name);
stats.totalBlocks++;
if (!def.hidden) {
auto& item = builder.createItem(full+BLOCK_ITEM_SUFFIX);
item.generated = true;
@@ -324,6 +327,7 @@ void ContentLoader::load(ContentBuilder& builder) {
for (uint j = 0; j < 4; j++) {
item.emission[j] = def.emission[j];
}
stats.totalItems++;
}
}
}
@@ -334,6 +338,7 @@ void ContentLoader::load(ContentBuilder& builder) {
std::string name = itemsarr->str(i);
std::string full = pack->id+":"+name;
loadItem(builder.createItem(full), full, name);
stats.totalItems++;
}
}
}
+1 -1
View File
@@ -167,7 +167,7 @@ void ContentPack::readPacks(const EnginePaths* paths,
}
ContentPackRuntime::ContentPackRuntime(
ContentPack info,
ContentPack info,
std::unique_ptr<scripting::Environment> env
) : info(info), env(std::move(env))
{
+19 -1
View File
@@ -77,15 +77,33 @@ struct ContentPack {
);
};
struct ContentPackStats {
size_t totalBlocks;
size_t totalItems;
inline bool hasSavingContent() const {
return totalBlocks + totalItems > 0;
}
};
class ContentPackRuntime {
ContentPack info;
ContentPackStats stats {};
std::unique_ptr<scripting::Environment> env;
public:
ContentPackRuntime(
ContentPack info,
ContentPack info,
std::unique_ptr<scripting::Environment> env
);
inline const ContentPackStats& getStats() const {
return stats;
}
inline ContentPackStats& getStatsWriteable() {
return stats;
}
inline const std::string& getId() {
return info.id;
}