add file.name(), file.stem() & add 'path' to pack info & add start_coroutine()

This commit is contained in:
MihailRis
2025-01-05 22:44:32 +03:00
parent c51a7e0153
commit 136c35591c
14 changed files with 147 additions and 70 deletions
+10 -7
View File
@@ -15,7 +15,7 @@ namespace fs = std::filesystem;
ContentPack ContentPack::createCore(const EnginePaths& paths) {
return ContentPack {
"core", "Core", ENGINE_VERSION_STRING, "", "", paths.getResourcesFolder(), {}
"core", "Core", ENGINE_VERSION_STRING, "", "", paths.getResourcesFolder(), "res:", {}
};
}
@@ -70,7 +70,7 @@ static void checkContentPackId(const std::string& id, const fs::path& folder) {
}
}
ContentPack ContentPack::read(const fs::path& folder) {
ContentPack ContentPack::read(const std::string& path, const fs::path& folder) {
auto root = files::read_json(folder / fs::path(PACKAGE_FILENAME));
ContentPack pack;
root.at("id").get(pack.id);
@@ -90,6 +90,7 @@ ContentPack ContentPack::read(const fs::path& folder) {
root.at("description").get(pack.description);
root.at("source").get(pack.source);
pack.folder = folder;
pack.path = path;
if (auto found = root.at("dependencies")) {
const auto& dependencies = *found;
@@ -123,17 +124,19 @@ ContentPack ContentPack::read(const fs::path& folder) {
}
void ContentPack::scanFolder(
const fs::path& folder, std::vector<ContentPack>& packs
const std::string& path, const fs::path& folder, std::vector<ContentPack>& packs
) {
if (!fs::is_directory(folder)) {
return;
}
for (const auto& entry : fs::directory_iterator(folder)) {
const fs::path& folder = entry.path();
if (!fs::is_directory(folder)) continue;
if (!is_pack(folder)) continue;
const fs::path& packFolder = entry.path();
if (!fs::is_directory(packFolder)) continue;
if (!is_pack(packFolder)) continue;
try {
packs.push_back(read(folder));
packs.push_back(
read(path + "/" + packFolder.filename().string(), packFolder)
);
} catch (const contentpack_error& err) {
std::cerr << "package.json error at " << err.getFolder().u8string();
std::cerr << ": " << err.what() << std::endl;
+29 -22
View File
@@ -10,18 +10,18 @@
class EnginePaths;
namespace fs = std::filesystem;
class contentpack_error : public std::runtime_error {
std::string packId;
fs::path folder;
std::filesystem::path folder;
public:
contentpack_error(
std::string packId, fs::path folder, const std::string& message
std::string packId,
std::filesystem::path folder,
const std::string& message
);
std::string getPackId() const;
fs::path getFolder() const;
std::filesystem::path getFolder() const;
};
enum class DependencyLevel {
@@ -42,45 +42,52 @@ struct ContentPack {
std::string version = "0.0";
std::string creator = "";
std::string description = "no description";
fs::path folder;
std::filesystem::path folder;
std::string path;
std::vector<DependencyPack> dependencies;
std::string source = "";
fs::path getContentFile() const;
std::filesystem::path getContentFile() const;
static inline const std::string PACKAGE_FILENAME = "package.json";
static inline const std::string CONTENT_FILENAME = "content.json";
static inline const fs::path BLOCKS_FOLDER = "blocks";
static inline const fs::path ITEMS_FOLDER = "items";
static inline const fs::path ENTITIES_FOLDER = "entities";
static inline const fs::path GENERATORS_FOLDER = "generators";
static inline const std::filesystem::path BLOCKS_FOLDER = "blocks";
static inline const std::filesystem::path ITEMS_FOLDER = "items";
static inline const std::filesystem::path ENTITIES_FOLDER = "entities";
static inline const std::filesystem::path GENERATORS_FOLDER = "generators";
static const std::vector<std::string> RESERVED_NAMES;
static bool is_pack(const fs::path& folder);
static ContentPack read(const fs::path& folder);
static void scanFolder(
const fs::path& folder, std::vector<ContentPack>& packs
static bool is_pack(const std::filesystem::path& folder);
static ContentPack read(
const std::string& path, const std::filesystem::path& folder
);
static std::vector<std::string> worldPacksList(const fs::path& folder);
static void scanFolder(
const std::string& path,
const std::filesystem::path& folder,
std::vector<ContentPack>& packs
);
static fs::path findPack(
static std::vector<std::string> worldPacksList(
const std::filesystem::path& folder
);
static std::filesystem::path findPack(
const EnginePaths* paths,
const fs::path& worldDir,
const std::filesystem::path& worldDir,
const std::string& name
);
static ContentPack createCore(const EnginePaths&);
static inline fs::path getFolderFor(ContentType type) {
static inline std::filesystem::path getFolderFor(ContentType type) {
switch (type) {
case ContentType::BLOCK: return ContentPack::BLOCKS_FOLDER;
case ContentType::ITEM: return ContentPack::ITEMS_FOLDER;
case ContentType::ENTITY: return ContentPack::ENTITIES_FOLDER;
case ContentType::GENERATOR: return ContentPack::GENERATORS_FOLDER;
case ContentType::NONE: return fs::u8path("");
default: return fs::u8path("");
case ContentType::NONE: return std::filesystem::u8path("");
default: return std::filesystem::u8path("");
}
}
};
+3 -3
View File
@@ -7,7 +7,7 @@
PacksManager::PacksManager() = default;
void PacksManager::setSources(std::vector<fs::path> sources) {
void PacksManager::setSources(std::vector<std::pair<std::string, fs::path>> sources) {
this->sources = std::move(sources);
}
@@ -15,8 +15,8 @@ void PacksManager::scan() {
packs.clear();
std::vector<ContentPack> packsList;
for (auto& folder : sources) {
ContentPack::scanFolder(folder, packsList);
for (auto& [path, folder] : sources) {
ContentPack::scanFolder(path, folder, packsList);
for (auto& pack : packsList) {
packs.try_emplace(pack.id, pack);
}
+2 -2
View File
@@ -10,12 +10,12 @@ namespace fs = std::filesystem;
class PacksManager {
std::unordered_map<std::string, ContentPack> packs;
std::vector<fs::path> sources;
std::vector<std::pair<std::string, fs::path>> sources;
public:
PacksManager();
/// @brief Set content packs sources (search folders)
void setSources(std::vector<fs::path> sources);
void setSources(std::vector<std::pair<std::string, fs::path>> sources);
/// @brief Scan sources and collect all found packs excluding duplication.
/// Scanning order depends on sources order