Check model format version

This commit is contained in:
Epicalert 2021-02-07 22:01:31 +08:00
parent 557111d5c4
commit dd6edb96f3
No known key found for this signature in database
GPG key ID: CAA46F858D0979BD

View file

@ -3,10 +3,14 @@
#include <zip.h> #include <zip.h>
#include <tomlcpp.hpp> //dynamically link tomlcpp if it becomes common in repositories #include <tomlcpp.hpp> //dynamically link tomlcpp if it becomes common in repositories
#include <model.hpp> #include <model.hpp>
#include <config.hpp>
#define BUFFER_SIZE_MODEL_DESC 8192 // 8 KiB #define BUFFER_SIZE_MODEL_DESC 8192 // 8 KiB
#define BUFFER_SIZE_TEXTURE 16777220 // 16 MiB #define BUFFER_SIZE_TEXTURE 16777220 // 16 MiB
#define SUPPORTED_MODEL_MAJOR 0
#define SUPPORTED_MODEL_MINOR 1
void textureFromArchive(zip_t* archive, const char* path, ModelPart* part, size_t slot, std::string triggerName) { void textureFromArchive(zip_t* archive, const char* path, ModelPart* part, size_t slot, std::string triggerName) {
zip_file_t* textureFile = zip_fopen(archive, path, 0); zip_file_t* textureFile = zip_fopen(archive, path, 0);
if (textureFile != NULL) { if (textureFile != NULL) {
@ -42,11 +46,40 @@ Model::Model(const char* path) {
std::cerr << "cannot parse model.toml! " << std::endl << modelDesc.errmsg << std::endl; std::cerr << "cannot parse model.toml! " << std::endl << modelDesc.errmsg << std::endl;
} }
// get name // get format table
auto format = modelDesc.table->getTable("format");
if (!format) {
std::cerr << "Model does not have a format table!" << std::endl;
} else {
// get format version
auto formatMajResult = format->getInt("version_major");
auto formatMinResult = format->getInt("version_minor");
if (formatMajResult.first && formatMinResult.first) {
// check format version
if (formatMajResult.second != SUPPORTED_MODEL_MAJOR ||
formatMinResult.second > SUPPORTED_MODEL_MINOR ) {
std::cerr << "Model format version " <<
formatMajResult.second << "." << formatMinResult.second <<
" is unsupported! This version of " << PROJECT_NAME << " supports model file version " <<
SUPPORTED_MODEL_MAJOR << ".0 to version " <<
SUPPORTED_MODEL_MAJOR << "." << SUPPORTED_MODEL_MINOR << std::endl;
}
} else {
std::cerr << "Model does not define a format version!" << std::endl;
}
}
// get model info table
auto modelInfo = modelDesc.table->getTable("model_info"); auto modelInfo = modelDesc.table->getTable("model_info");
if (!modelInfo) { if (!modelInfo) {
std::cerr << "Model does not have a model_info table!" << std::endl; std::cerr << "Model does not have a model_info table!" << std::endl;
} else { } else {
// get name
auto nameResult = modelInfo->getString("name"); auto nameResult = modelInfo->getString("name");
if (!nameResult.first) { if (!nameResult.first) {
@ -56,7 +89,7 @@ Model::Model(const char* path) {
} }
} }
// parse parts
auto partsDescArray = modelDesc.table->getArray("part"); auto partsDescArray = modelDesc.table->getArray("part");
auto partsVec = *partsDescArray->getTableVector(); auto partsVec = *partsDescArray->getTableVector();