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
+9 -8
View File
@@ -262,9 +262,9 @@ cmd::CommandsInterpreter* Engine::getCommandsInterpreter() {
PacksManager Engine::createPacksManager(const fs::path& worldFolder) {
PacksManager manager;
manager.setSources({
worldFolder/fs::path("content"),
paths.getUserFilesFolder()/fs::path("content"),
paths.getResourcesFolder()/fs::path("content")
{"world:content", worldFolder.empty() ? worldFolder : worldFolder/fs::path("content")},
{"user:content", paths.getUserFilesFolder()/fs::path("content")},
{"res:content", paths.getResourcesFolder()/fs::path("content")}
});
return manager;
}
@@ -413,11 +413,12 @@ void Engine::loadWorldContent(const fs::path& folder) {
contentPacks.clear();
auto packNames = ContentPack::worldPacksList(folder);
PacksManager manager;
manager.setSources({
folder/fs::path("content"),
paths.getUserFilesFolder()/fs::path("content"),
paths.getResourcesFolder()/fs::path("content")
});
manager.setSources(
{{"world:content",
folder.empty() ? folder : folder / fs::path("content")},
{"user:content", paths.getUserFilesFolder() / fs::path("content")},
{"res:content", paths.getResourcesFolder() / fs::path("content")}}
);
manager.scan();
contentPacks = manager.getAll(manager.assemble(packNames));
paths.setCurrentWorldFolder(folder);
+4 -1
View File
@@ -64,7 +64,7 @@ static int l_pack_get_available(lua::State* L) {
static int l_pack_get_info(
lua::State* L, const ContentPack& pack, const Content* content
) {
lua::createtable(L, 0, 5);
lua::createtable(L, 0, 6);
lua::pushstring(L, pack.id);
lua::setfield(L, "id");
@@ -81,6 +81,9 @@ static int l_pack_get_info(
lua::pushstring(L, pack.version);
lua::setfield(L, "version");
lua::pushstring(L, pack.path);
lua::setfield(L, "path");
if (!engine->isHeadless()) {
auto assets = engine->getAssets();
std::string icon = pack.id + ".icon";
+8 -6
View File
@@ -44,7 +44,6 @@ static void create_libs(State* L, StateType stateType) {
openlib(L, "bjson", bjsonlib);
openlib(L, "block", blocklib);
openlib(L, "byteutil", byteutillib);
openlib(L, "core", corelib);
openlib(L, "file", filelib);
openlib(L, "generation", generationlib);
openlib(L, "item", itemlib);
@@ -52,7 +51,6 @@ static void create_libs(State* L, StateType stateType) {
openlib(L, "mat4", mat4lib);
openlib(L, "pack", packlib);
openlib(L, "quat", quatlib);
openlib(L, "time", timelib);
openlib(L, "toml", tomllib);
openlib(L, "utf8", utf8lib);
openlib(L, "vec2", vec2lib);
@@ -61,16 +59,20 @@ static void create_libs(State* L, StateType stateType) {
if (stateType == StateType::SCRIPT) {
openlib(L, "app", applib);
} else if (stateType == StateType::BASE) {
openlib(L, "__vc_app", applib);
}
if (stateType == StateType::BASE || stateType == StateType::SCRIPT) {
openlib(L, "audio", audiolib);
openlib(L, "console", consolelib);
openlib(L, "core", corelib);
openlib(L, "gui", guilib);
openlib(L, "input", inputlib);
openlib(L, "inventory", inventorylib);
openlib(L, "world", worldlib);
openlib(L, "audio", audiolib);
openlib(L, "console", consolelib);
openlib(L, "player", playerlib);
openlib(L, "network", networklib);
openlib(L, "player", playerlib);
openlib(L, "time", timelib);
openlib(L, "world", worldlib);
openlib(L, "entities", entitylib);
openlib(L, "cameras", cameralib);