minor refactor

This commit is contained in:
MihailRis 2025-02-26 02:13:14 +03:00
parent fd59aa4f65
commit fba1ba6732
2 changed files with 28 additions and 22 deletions

View file

@ -7,12 +7,14 @@
#include "coders/json.hpp" #include "coders/json.hpp"
#include "constants.hpp" #include "constants.hpp"
#include "data/dv.hpp" #include "data/dv.hpp"
#include "debug/Logger.hpp"
#include "io/engine_paths.hpp" #include "io/engine_paths.hpp"
#include "io/io.hpp" #include "io/io.hpp"
namespace fs = std::filesystem; namespace fs = std::filesystem;
static debug::Logger logger("content-pack");
ContentPack ContentPack::createCore(const EnginePaths& paths) { ContentPack ContentPack::createCore(const EnginePaths& paths) {
return ContentPack { return ContentPack {
"core", "Core", ENGINE_VERSION_STRING, "", "", "res:", "res:", {} "core", "Core", ENGINE_VERSION_STRING, "", "", "res:", "res:", {}
@ -20,7 +22,9 @@ ContentPack ContentPack::createCore(const EnginePaths& paths) {
} }
const std::vector<std::string> ContentPack::RESERVED_NAMES = { const std::vector<std::string> ContentPack::RESERVED_NAMES = {
"res", "abs", "local", "core", "user", "world", "none", "null"}; "res", "abs", "local", "core", "user", "world", "none", "null", "export",
"config"
};
contentpack_error::contentpack_error( contentpack_error::contentpack_error(
std::string packId, io::path folder, const std::string& message std::string packId, io::path folder, const std::string& message
@ -45,17 +49,17 @@ bool ContentPack::is_pack(const io::path& folder) {
return io::is_regular_file(folder / PACKAGE_FILENAME); return io::is_regular_file(folder / PACKAGE_FILENAME);
} }
static void checkContentPackId(const std::string& id, const io::path& folder) { static void check_pack_id(const std::string& id, const io::path& folder) {
if (id.length() < 2 || id.length() > 24) if (id.length() < 2 || id.length() > 24)
throw contentpack_error( throw contentpack_error(
id, folder, "content-pack id length is out of range [2, 24]" id, folder, "content-pack id length is out of range [2, 24]"
); );
if (isdigit(id[0])) if (std::isdigit(id[0]))
throw contentpack_error( throw contentpack_error(
id, folder, "content-pack id must not start with a digit" id, folder, "content-pack id must not start with a digit"
); );
for (char c : id) { for (char c : id) {
if (!isalnum(c) && c != '_') { if (!std::isalnum(c) && c != '_') {
throw contentpack_error( throw contentpack_error(
id, folder, "illegal character in content-pack id" id, folder, "illegal character in content-pack id"
); );
@ -113,34 +117,36 @@ ContentPack ContentPack::read(const std::string& path, const io::path& folder) {
pack.dependencies.push_back({level, depName}); pack.dependencies.push_back({level, depName});
} }
} }
if (pack.id == "none") {
if (pack.id == "none")
throw contentpack_error( throw contentpack_error(
pack.id, folder, "content-pack id is not specified" pack.id, folder, "content-pack id is not specified"
); );
checkContentPackId(pack.id, folder); }
check_pack_id(pack.id, folder);
return pack; return pack;
} }
void ContentPack::scanFolder( void ContentPack::scanFolder(
const std::string& path, const io::path& folder, std::vector<ContentPack>& packs const std::string& path,
const io::path& folder,
std::vector<ContentPack>& packs
) { ) {
if (!io::is_directory(folder)) { if (!io::is_directory(folder)) {
return; return;
} }
for (const auto& packFolder : io::directory_iterator(folder)) { for (const auto& packFolder : io::directory_iterator(folder)) {
if (!io::is_directory(packFolder)) continue; if (!io::is_directory(packFolder) || !is_pack(packFolder)) {
if (!is_pack(packFolder)) continue; continue;
}
try { try {
packs.push_back( packs.push_back(
read(path + "/" + packFolder.name(), packFolder) read(path + "/" + packFolder.name(), packFolder)
); );
} catch (const contentpack_error& err) { } catch (const contentpack_error& err) {
std::cerr << "package.json error at " << err.getFolder().string(); logger.error() << "package.json error at "
std::cerr << ": " << err.what() << std::endl; << err.getFolder().string() << ": " << err.what();
} catch (const std::runtime_error& err) { } catch (const std::runtime_error& err) {
std::cerr << err.what() << std::endl; logger.error() << err.what();
} }
} }
} }

View file

@ -80,7 +80,7 @@ struct ContentPack {
static ContentPack createCore(const EnginePaths&); static ContentPack createCore(const EnginePaths&);
static inline io::path getFolderFor(ContentType type) { static io::path getFolderFor(ContentType type) {
switch (type) { switch (type) {
case ContentType::BLOCK: return ContentPack::BLOCKS_FOLDER; case ContentType::BLOCK: return ContentPack::BLOCKS_FOLDER;
case ContentType::ITEM: return ContentPack::ITEMS_FOLDER; case ContentType::ITEM: return ContentPack::ITEMS_FOLDER;
@ -97,7 +97,7 @@ struct ContentPackStats {
size_t totalItems; size_t totalItems;
size_t totalEntities; size_t totalEntities;
inline bool hasSavingContent() const { bool hasSavingContent() const {
return totalBlocks + totalItems + totalEntities > 0; return totalBlocks + totalItems + totalEntities > 0;
} }
}; };
@ -125,23 +125,23 @@ public:
ContentPackRuntime(ContentPack info, scriptenv env); ContentPackRuntime(ContentPack info, scriptenv env);
~ContentPackRuntime(); ~ContentPackRuntime();
inline const ContentPackStats& getStats() const { const ContentPackStats& getStats() const {
return stats; return stats;
} }
inline ContentPackStats& getStatsWriteable() { ContentPackStats& getStatsWriteable() {
return stats; return stats;
} }
inline const std::string& getId() { const std::string& getId() const {
return info.id; return info.id;
} }
inline const ContentPack& getInfo() const { const ContentPack& getInfo() const {
return info; return info;
} }
inline scriptenv getEnvironment() const { scriptenv getEnvironment() const {
return env; return env;
} }
}; };