diff --git a/src/content/ContentPack.cpp b/src/content/ContentPack.cpp index 522b818f..021f083e 100644 --- a/src/content/ContentPack.cpp +++ b/src/content/ContentPack.cpp @@ -1,3 +1,4 @@ +#define VC_ENABLE_REFLECTION #include "ContentPack.hpp" #include @@ -146,7 +147,7 @@ ContentPack ContentPack::read(const io::path& folder) { std::uint8_t op_size = 0; // Two symbol operators - if (op == ">=" || op == "=>" || op == "<=" || op == "=<") { + if (op == ">=" || op == "<=") { op_size = 2; depVerOperator = op; } @@ -169,7 +170,16 @@ ContentPack ContentPack::read(const io::path& folder) { } } - pack.dependencies.push_back({level, depName, depVer, depVerOperator}); + VersionOperator versionOperator; + if (VersionOperatorMeta.getItem(depVerOperator, versionOperator)) { + pack.dependencies.push_back( + {level, depName, depVer, versionOperator} + ); + } else { + throw contentpack_error( + pack.id, folder, "invalid version operator" + ); + } } } diff --git a/src/content/ContentPack.hpp b/src/content/ContentPack.hpp index 1aa3a9ea..bcc91ecb 100644 --- a/src/content/ContentPack.hpp +++ b/src/content/ContentPack.hpp @@ -1,14 +1,15 @@ #pragma once +#include "typedefs.hpp" +#include "content_fwd.hpp" +#include "io/io.hpp" +#include "util/EnumMetadata.hpp" + #include #include #include #include -#include "typedefs.hpp" -#include "content_fwd.hpp" -#include "io/io.hpp" - class EnginePaths; class contentpack_error : public std::runtime_error { @@ -25,11 +26,19 @@ public: io::path getFolder() const; }; -enum class DependencyVersionOperator { +enum class VersionOperator { EQUAL, GREATHER, LESS, GREATHER_OR_EQUAL, LESS_OR_EQUAL }; +VC_ENUM_METADATA(VersionOperator) + {"=", VersionOperator::EQUAL}, + {">", VersionOperator::GREATHER}, + {"<", VersionOperator::LESS}, + {">=", VersionOperator::GREATHER_OR_EQUAL}, + {"<=", VersionOperator::LESS_OR_EQUAL}, +VC_ENUM_END + enum class DependencyLevel { REQUIRED, // dependency must be installed OPTIONAL, // dependency will be installed if found @@ -41,7 +50,7 @@ struct DependencyPack { DependencyLevel level; std::string id; std::string version; - std::string op; + VersionOperator op; }; struct ContentPackStats { diff --git a/src/content/ContentPackVersion.cpp b/src/content/ContentPackVersion.cpp index 7a3d8801..7f32a291 100644 --- a/src/content/ContentPackVersion.cpp +++ b/src/content/ContentPackVersion.cpp @@ -26,26 +26,11 @@ Version::Version(const std::string& version) { if (parts.size() > 2) patch = parts[2]; } -DependencyVersionOperator Version::string_to_operator(const std::string& op) { - if (op == "=") - return DependencyVersionOperator::EQUAL; - else if (op == ">") - return DependencyVersionOperator::GREATHER; - else if (op == "<") - return DependencyVersionOperator::LESS; - else if (op == ">=" || op == "=>") - return DependencyVersionOperator::GREATHER_OR_EQUAL; - else if (op == "<=" || op == "=<") - return DependencyVersionOperator::LESS_OR_EQUAL; - else - return DependencyVersionOperator::EQUAL; -} - bool isNumber(const std::string& s) { return !s.empty() && std::all_of(s.begin(), s.end(), ::is_digit); } -bool Version::matches_pattern(const std::string& version) { +bool Version::matchesPattern(const std::string& version) { for (char c : version) { if (!isdigit(c) && c != '.') { return false; diff --git a/src/content/ContentPackVersion.hpp b/src/content/ContentPackVersion.hpp index e922ddd3..54deaf3c 100644 --- a/src/content/ContentPackVersion.hpp +++ b/src/content/ContentPackVersion.hpp @@ -33,25 +33,22 @@ public: return !(*this > other); } - bool process_operator(const std::string& op, const Version& other) const { - auto dep_op = Version::string_to_operator(op); - - switch (dep_op) { - case DependencyVersionOperator::EQUAL: + bool processOperator(VersionOperator op, const Version& other) const { + switch (op) { + case VersionOperator::EQUAL: return *this == other; - case DependencyVersionOperator::GREATHER: + case VersionOperator::GREATHER: return *this > other; - case DependencyVersionOperator::LESS: + case VersionOperator::LESS: return *this < other; - case DependencyVersionOperator::LESS_OR_EQUAL: + case VersionOperator::LESS_OR_EQUAL: return *this <= other; - case DependencyVersionOperator::GREATHER_OR_EQUAL: + case VersionOperator::GREATHER_OR_EQUAL: return *this >= other; default: return false; } } - static DependencyVersionOperator string_to_operator(const std::string& op); - static bool matches_pattern(const std::string& version); + static bool matchesPattern(const std::string& version); }; diff --git a/src/content/PacksManager.cpp b/src/content/PacksManager.cpp index 2d9759cb..3cbde0f2 100644 --- a/src/content/PacksManager.cpp +++ b/src/content/PacksManager.cpp @@ -1,3 +1,4 @@ +#define VC_ENABLE_REFLECTION #include "PacksManager.hpp" #include @@ -109,9 +110,9 @@ static bool resolve_dependencies( auto dep_pack = found -> second; - if (Version::matches_pattern(dep.version) && Version::matches_pattern(dep_pack.version) + if (Version::matchesPattern(dep.version) && Version::matchesPattern(dep_pack.version) && Version(dep_pack.version) - .process_operator(dep.op, Version(dep.version)) + .processOperator(dep.op, Version(dep.version)) ) { // dependency pack version meets the required one continue; @@ -120,7 +121,11 @@ static bool resolve_dependencies( continue; } else { throw contentpack_error( - dep.id, io::path(), "does not meet required version '" + dep.op + dep.version +"' of '" + pack->id + "'" + dep.id, + io::path(), + "does not meet required version '" + + VersionOperatorMeta.getNameString(dep.op) + dep.version + + "' of '" + pack->id + "'" ); } diff --git a/src/logic/scripting/lua/libs/libpack.cpp b/src/logic/scripting/lua/libs/libpack.cpp index f5f952b7..77eed4f0 100644 --- a/src/logic/scripting/lua/libs/libpack.cpp +++ b/src/logic/scripting/lua/libs/libpack.cpp @@ -1,8 +1,4 @@ -#include -#include -#include -#include -#include +#define VC_ENABLE_REFLECTION #include "assets/AssetsLoader.hpp" #include "content/Content.hpp" @@ -19,6 +15,12 @@ #include "world/World.hpp" #include "api_lua.hpp" +#include +#include +#include +#include +#include + using namespace scripting; static int l_pack_get_folder(lua::State* L) { @@ -114,13 +116,14 @@ static int l_pack_get_info( default: throw std::runtime_error(""); } + auto opString = VersionOperatorMeta.getNameString(dpack.op); lua::pushfstring( L, "%s%s@%s%s", prefix.c_str(), dpack.id.c_str(), - dpack.op != "=" ? dpack.op.c_str() : "", + (dpack.op == VersionOperator::EQUAL ? "" : opString).c_str(), dpack.version.c_str() ); lua::rawseti(L, i + 1);