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

View File

@ -0,0 +1,7 @@
{
"camera": [
"first-person",
"third-person-front",
"third-person-back"
]
}

View File

@ -1,11 +1,12 @@
#ifndef CONTENT_CONTENT_HPP_
#define CONTENT_CONTENT_HPP_
#include "../typedefs.hpp"
#include "content_fwd.hpp"
#include <string>
#include <vector>
#include <memory>
#include <optional>
#include <stdexcept>
#include <unordered_map>
#include <set>
@ -18,18 +19,12 @@ class Block;
struct BlockMaterial;
struct ItemDef;
struct EntityDef;
class Content;
class ContentPackRuntime;
namespace rigging {
class SkeletonConfig;
}
enum class contenttype {
none, block, item, entity
};
inline const char* contenttype_name(contenttype type) {
constexpr const char* contenttype_name(contenttype type) {
switch (type) {
case contenttype::none: return "none";
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
class Content {
std::unique_ptr<ContentIndices> indices;
@ -122,6 +155,7 @@ public:
ContentUnitDefs<ItemDef> items;
ContentUnitDefs<EntityDef> entities;
std::unique_ptr<DrawGroups> const drawGroups;
ResourceIndicesSet resourceIndices {};
Content(
std::unique_ptr<ContentIndices> indices,

View File

@ -57,6 +57,7 @@ public:
ContentUnitBuilder<Block> blocks {allNames, contenttype::block};
ContentUnitBuilder<ItemDef> items {allNames, contenttype::item};
ContentUnitBuilder<EntityDef> entities {allNames, contenttype::entity};
ResourceIndicesSet resourceIndices {};
~ContentBuilder();

View File

@ -478,9 +478,28 @@ void ContentLoader::load() {
fs::path scriptfile = entry.path();
if (fs::is_regular_file(scriptfile)) {
auto name = pack->id+":"+scriptfile.stem().u8string();
std::cout << name << std::endl;
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));
}
}

View File

@ -1,7 +1,7 @@
#ifndef CONTENT_CONTENT_LOADER_HPP_
#define CONTENT_CONTENT_LOADER_HPP_
#include "../typedefs.hpp"
#include "content_fwd.hpp"
#include <string>
#include <memory>
@ -19,6 +19,7 @@ struct ContentPackStats;
namespace dynamic {
class Map;
class List;
}
class ContentLoader {
@ -36,6 +37,7 @@ class ContentLoader {
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 loadEntity(EntityDef& def, const std::string& name, const fs::path& file);
void loadResources(ResourceType type, dynamic::List* list);
public:
ContentLoader(ContentPack* pack, ContentBuilder& builder);

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_

View File

@ -9,6 +9,7 @@
#include "coders/imageio.hpp"
#include "coders/json.hpp"
#include "coders/toml.hpp"
#include "content/Content.hpp"
#include "content/ContentBuilder.hpp"
#include "content/ContentLoader.hpp"
#include "core_defs.hpp"

View File

@ -5,7 +5,7 @@
#include "typedefs.hpp"
#include "assets/Assets.hpp"
#include "content/Content.hpp"
#include "content/content_fwd.hpp"
#include "content/ContentPack.hpp"
#include "content/PacksManager.hpp"
#include "files/engine_paths.hpp"
@ -48,11 +48,11 @@ class Engine : public util::ObjectsKeeper {
SettingsHandler& settingsHandler;
EnginePaths* paths;
std::unique_ptr<Assets> assets = nullptr;
std::shared_ptr<Screen> screen = nullptr;
std::unique_ptr<Assets> assets;
std::shared_ptr<Screen> screen;
std::vector<ContentPack> contentPacks;
std::unique_ptr<Content> content = nullptr;
std::unique_ptr<ResPaths> resPaths = nullptr;
std::unique_ptr<Content> content;
std::unique_ptr<ResPaths> resPaths;
std::queue<runnable> postRunnables;
std::recursive_mutex postRunnablesMutex;
std::unique_ptr<EngineController> controller;

View File

@ -2,6 +2,7 @@
#include "../delegates.hpp"
#include "../engine.hpp"
#include "../settings.hpp"
#include "../content/Content.hpp"
#include "../graphics/core/Mesh.hpp"
#include "../graphics/ui/elements/CheckBox.hpp"
#include "../graphics/ui/elements/TextBox.hpp"

View File

@ -8,6 +8,7 @@
#include "../../debug/Logger.hpp"
#include "../../engine.hpp"
#include "../../files/files.hpp"
#include "../../content/Content.hpp"
#include "../../graphics/core/DrawContext.hpp"
#include "../../graphics/core/ImageData.hpp"
#include "../../graphics/core/PostProcessing.hpp"

View File

@ -1,6 +1,7 @@
#include "api_lua.hpp"
#include "../../../engine.hpp"
#include "../../../content/Content.hpp"
#include "../../../assets/AssetsLoader.hpp"
#include "../../../files/engine_paths.hpp"
#include "../../../files/WorldFiles.hpp"

View File

@ -2,6 +2,7 @@
#include "lua/lua_engine.hpp"
#include "../../content/Content.hpp"
#include "../../content/ContentPack.hpp"
#include "../../debug/Logger.hpp"
#include "../../engine.hpp"

View File

@ -2,11 +2,12 @@
#define WORLD_WORLDGENERATORS_HPP_
#include "../voxels/WorldGenerator.hpp"
#include "../content/Content.hpp"
#include <map>
#include <vector>
#include <string>
class Content;
typedef WorldGenerator* (*gen_constructor) (const Content*);