add packs resources (WIP)

This commit is contained in:
MihailRis
2024-07-11 10:32:05 +03:00
parent 30dd853044
commit ca8652ffab
13 changed files with 103 additions and 16 deletions
+7
View File
@@ -0,0 +1,7 @@
{
"camera": [
"first-person",
"third-person-front",
"third-person-back"
]
}
+42 -8
View File
@@ -1,11 +1,12 @@
#ifndef CONTENT_CONTENT_HPP_ #ifndef CONTENT_CONTENT_HPP_
#define CONTENT_CONTENT_HPP_ #define CONTENT_CONTENT_HPP_
#include "../typedefs.hpp" #include "content_fwd.hpp"
#include <string> #include <string>
#include <vector> #include <vector>
#include <memory> #include <memory>
#include <optional>
#include <stdexcept> #include <stdexcept>
#include <unordered_map> #include <unordered_map>
#include <set> #include <set>
@@ -18,18 +19,12 @@ class Block;
struct BlockMaterial; struct BlockMaterial;
struct ItemDef; struct ItemDef;
struct EntityDef; struct EntityDef;
class Content;
class ContentPackRuntime;
namespace rigging { namespace rigging {
class SkeletonConfig; class SkeletonConfig;
} }
enum class contenttype { constexpr const char* contenttype_name(contenttype type) {
none, block, item, entity
};
inline const char* contenttype_name(contenttype type) {
switch (type) { switch (type) {
case contenttype::none: return "none"; case contenttype::none: return "none";
case contenttype::block: return "block"; case contenttype::block: return "block";
@@ -111,6 +106,44 @@ public:
} }
}; };
class ResourceIndices {
std::vector<std::string> names;
std::unordered_map<std::string, size_t> indices;
public:
ResourceIndices() {}
static constexpr size_t MISSING = -1;
void add(std::string name) {
indices[name] = names.size();
names.push_back(name);
}
size_t indexOf(const std::string& name) const {
const auto& found = indices.find(name);
if (found == indices.end()) {
return found->second;
}
return MISSING;
}
};
constexpr const char* to_string(ResourceType type) {
switch (type) {
case ResourceType::CAMERA: return "camera";
default: return "unknown";
}
}
inline std::optional<ResourceType> ResourceType_from(std::string_view str) {
if (str == "camera") {
return ResourceType::CAMERA;
}
return std::nullopt;
}
using ResourceIndicesSet = ResourceIndices[static_cast<size_t>(ResourceType::LAST)+1];
/// @brief Content is a definitions repository /// @brief Content is a definitions repository
class Content { class Content {
std::unique_ptr<ContentIndices> indices; std::unique_ptr<ContentIndices> indices;
@@ -122,6 +155,7 @@ public:
ContentUnitDefs<ItemDef> items; ContentUnitDefs<ItemDef> items;
ContentUnitDefs<EntityDef> entities; ContentUnitDefs<EntityDef> entities;
std::unique_ptr<DrawGroups> const drawGroups; std::unique_ptr<DrawGroups> const drawGroups;
ResourceIndicesSet resourceIndices {};
Content( Content(
std::unique_ptr<ContentIndices> indices, std::unique_ptr<ContentIndices> indices,
+1
View File
@@ -57,6 +57,7 @@ public:
ContentUnitBuilder<Block> blocks {allNames, contenttype::block}; ContentUnitBuilder<Block> blocks {allNames, contenttype::block};
ContentUnitBuilder<ItemDef> items {allNames, contenttype::item}; ContentUnitBuilder<ItemDef> items {allNames, contenttype::item};
ContentUnitBuilder<EntityDef> entities {allNames, contenttype::entity}; ContentUnitBuilder<EntityDef> entities {allNames, contenttype::entity};
ResourceIndicesSet resourceIndices {};
~ContentBuilder(); ~ContentBuilder();
+20 -1
View File
@@ -478,9 +478,28 @@ void ContentLoader::load() {
fs::path scriptfile = entry.path(); fs::path scriptfile = entry.path();
if (fs::is_regular_file(scriptfile)) { if (fs::is_regular_file(scriptfile)) {
auto name = pack->id+":"+scriptfile.stem().u8string(); auto name = pack->id+":"+scriptfile.stem().u8string();
std::cout << name << std::endl;
scripting::load_entity_component(name, scriptfile); scripting::load_entity_component(name, scriptfile);
} }
} }
} }
fs::path resourcesFile = folder / fs::u8path("resources.json");
if (fs::exists(resourcesFile)) {
auto resRoot = files::read_json(resourcesFile);
for (const auto& [key, _] : resRoot->values) {
if (auto resType = ResourceType_from(key)) {
if (auto arr = resRoot->list(key)) {
loadResources(*resType, arr.get());
}
} else {
logger.warning() << "unknown resource type: " << key;
}
}
}
}
void ContentLoader::loadResources(ResourceType type, dynamic::List* list) {
for (size_t i = 0; i < list->size(); i++) {
builder.resourceIndices[static_cast<size_t>(type)].add(list->str(i));
}
} }
+3 -1
View File
@@ -1,7 +1,7 @@
#ifndef CONTENT_CONTENT_LOADER_HPP_ #ifndef CONTENT_CONTENT_LOADER_HPP_
#define CONTENT_CONTENT_LOADER_HPP_ #define CONTENT_CONTENT_LOADER_HPP_
#include "../typedefs.hpp" #include "content_fwd.hpp"
#include <string> #include <string>
#include <memory> #include <memory>
@@ -19,6 +19,7 @@ struct ContentPackStats;
namespace dynamic { namespace dynamic {
class Map; class Map;
class List;
} }
class ContentLoader { class ContentLoader {
@@ -36,6 +37,7 @@ class ContentLoader {
static void loadBlock(Block& def, const std::string& name, const fs::path& file); static void loadBlock(Block& def, const std::string& name, const fs::path& file);
static void loadItem(ItemDef& def, const std::string& name, const fs::path& file); static void loadItem(ItemDef& def, const std::string& name, const fs::path& file);
static void loadEntity(EntityDef& def, const std::string& name, const fs::path& file); static void loadEntity(EntityDef& def, const std::string& name, const fs::path& file);
void loadResources(ResourceType type, dynamic::List* list);
public: public:
ContentLoader(ContentPack* pack, ContentBuilder& builder); ContentLoader(ContentPack* pack, ContentBuilder& builder);
+18
View File
@@ -0,0 +1,18 @@
#ifndef CONTENT_CONTENT_FWD_HPP_
#define CONTENT_CONTENT_FWD_HPP_
#include "../typedefs.hpp"
class Content;
class ContentPackRuntime;
enum class contenttype {
none, block, item, entity
};
enum class ResourceType : size_t {
CAMERA,
LAST=CAMERA
};
#endif // CONTENT_CONTENT_FWD_HPP_
+1
View File
@@ -9,6 +9,7 @@
#include "coders/imageio.hpp" #include "coders/imageio.hpp"
#include "coders/json.hpp" #include "coders/json.hpp"
#include "coders/toml.hpp" #include "coders/toml.hpp"
#include "content/Content.hpp"
#include "content/ContentBuilder.hpp" #include "content/ContentBuilder.hpp"
#include "content/ContentLoader.hpp" #include "content/ContentLoader.hpp"
#include "core_defs.hpp" #include "core_defs.hpp"
+5 -5
View File
@@ -5,7 +5,7 @@
#include "typedefs.hpp" #include "typedefs.hpp"
#include "assets/Assets.hpp" #include "assets/Assets.hpp"
#include "content/Content.hpp" #include "content/content_fwd.hpp"
#include "content/ContentPack.hpp" #include "content/ContentPack.hpp"
#include "content/PacksManager.hpp" #include "content/PacksManager.hpp"
#include "files/engine_paths.hpp" #include "files/engine_paths.hpp"
@@ -48,11 +48,11 @@ class Engine : public util::ObjectsKeeper {
SettingsHandler& settingsHandler; SettingsHandler& settingsHandler;
EnginePaths* paths; EnginePaths* paths;
std::unique_ptr<Assets> assets = nullptr; std::unique_ptr<Assets> assets;
std::shared_ptr<Screen> screen = nullptr; std::shared_ptr<Screen> screen;
std::vector<ContentPack> contentPacks; std::vector<ContentPack> contentPacks;
std::unique_ptr<Content> content = nullptr; std::unique_ptr<Content> content;
std::unique_ptr<ResPaths> resPaths = nullptr; std::unique_ptr<ResPaths> resPaths;
std::queue<runnable> postRunnables; std::queue<runnable> postRunnables;
std::recursive_mutex postRunnablesMutex; std::recursive_mutex postRunnablesMutex;
std::unique_ptr<EngineController> controller; std::unique_ptr<EngineController> controller;
+1
View File
@@ -2,6 +2,7 @@
#include "../delegates.hpp" #include "../delegates.hpp"
#include "../engine.hpp" #include "../engine.hpp"
#include "../settings.hpp" #include "../settings.hpp"
#include "../content/Content.hpp"
#include "../graphics/core/Mesh.hpp" #include "../graphics/core/Mesh.hpp"
#include "../graphics/ui/elements/CheckBox.hpp" #include "../graphics/ui/elements/CheckBox.hpp"
#include "../graphics/ui/elements/TextBox.hpp" #include "../graphics/ui/elements/TextBox.hpp"
+1
View File
@@ -8,6 +8,7 @@
#include "../../debug/Logger.hpp" #include "../../debug/Logger.hpp"
#include "../../engine.hpp" #include "../../engine.hpp"
#include "../../files/files.hpp" #include "../../files/files.hpp"
#include "../../content/Content.hpp"
#include "../../graphics/core/DrawContext.hpp" #include "../../graphics/core/DrawContext.hpp"
#include "../../graphics/core/ImageData.hpp" #include "../../graphics/core/ImageData.hpp"
#include "../../graphics/core/PostProcessing.hpp" #include "../../graphics/core/PostProcessing.hpp"
+1
View File
@@ -1,6 +1,7 @@
#include "api_lua.hpp" #include "api_lua.hpp"
#include "../../../engine.hpp" #include "../../../engine.hpp"
#include "../../../content/Content.hpp"
#include "../../../assets/AssetsLoader.hpp" #include "../../../assets/AssetsLoader.hpp"
#include "../../../files/engine_paths.hpp" #include "../../../files/engine_paths.hpp"
#include "../../../files/WorldFiles.hpp" #include "../../../files/WorldFiles.hpp"
+1
View File
@@ -2,6 +2,7 @@
#include "lua/lua_engine.hpp" #include "lua/lua_engine.hpp"
#include "../../content/Content.hpp"
#include "../../content/ContentPack.hpp" #include "../../content/ContentPack.hpp"
#include "../../debug/Logger.hpp" #include "../../debug/Logger.hpp"
#include "../../engine.hpp" #include "../../engine.hpp"
+2 -1
View File
@@ -2,11 +2,12 @@
#define WORLD_WORLDGENERATORS_HPP_ #define WORLD_WORLDGENERATORS_HPP_
#include "../voxels/WorldGenerator.hpp" #include "../voxels/WorldGenerator.hpp"
#include "../content/Content.hpp"
#include <map> #include <map>
#include <vector> #include <vector>
#include <string> #include <string>
class Content;
typedef WorldGenerator* (*gen_constructor) (const Content*); typedef WorldGenerator* (*gen_constructor) (const Content*);