Add method to set parsing strictness

This commit is contained in:
Nyall Dawson 2023-08-28 12:56:09 +10:00
parent 02e8b8da1e
commit 8c85d5e387
No known key found for this signature in database
GPG Key ID: D853AA00D2F6AFD0

View File

@ -195,6 +195,11 @@ typedef enum {
OBJECT_TYPE
} Type;
typedef enum {
PERMISSIVE,
STRICT
} ParseStrictness;
static inline int32_t GetComponentSizeInBytes(uint32_t componentType) {
if (componentType == TINYGLTF_COMPONENT_TYPE_BYTE) {
return 1;
@ -1463,6 +1468,11 @@ class TinyGLTF {
bool embedImages, bool embedBuffers,
bool prettyPrint, bool writeBinary);
///
/// Sets the parsing strictness.
///
void SetParseStrictness(ParseStrictness strictness);
///
/// Set callback to use for loading image data
///
@ -1552,6 +1562,8 @@ class TinyGLTF {
size_t bin_size_ = 0;
bool is_binary_ = false;
ParseStrictness strictness_ = ParseStrictness::STRICT;
bool serialize_default_values_ = false; ///< Serialize default values?
bool store_original_json_for_extras_and_extensions_ = false;
@ -2553,6 +2565,10 @@ static bool LoadExternalFile(std::vector<unsigned char> *out, std::string *err,
return true;
}
void TinyGLTF::SetParseStrictness(ParseStrictness strictness) {
strictness_ = strictness;
}
void TinyGLTF::SetImageLoader(LoadImageDataFunction func, void *user_data) {
LoadImageData = func;
load_image_user_data_ = user_data;
@ -5194,13 +5210,14 @@ static bool ParsePbrMetallicRoughness(
static bool ParseMaterial(Material *material, std::string *err, std::string *warn,
const detail::json &o,
bool store_original_json_for_extras_and_extensions) {
bool store_original_json_for_extras_and_extensions,
ParseStrictness strictness) {
ParseStringProperty(&material->name, err, o, "name", /* required */ false);
if (ParseNumberArrayProperty(&material->emissiveFactor, err, o,
"emissiveFactor",
/* required */ false)) {
if (material->emissiveFactor.size() == 4) {
if (strictness==ParseStrictness::PERMISSIVE && material->emissiveFactor.size() == 4) {
if (warn) {
(*warn) +=
"Array length of `emissiveFactor` parameter in "
@ -6207,7 +6224,8 @@ bool TinyGLTF::LoadFromString(Model *model, std::string *err, std::string *warn,
ParseStringProperty(&material.name, err, o, "name", false);
if (!ParseMaterial(&material, err, warn, o,
store_original_json_for_extras_and_extensions_)) {
store_original_json_for_extras_and_extensions_,
strictness_)) {
return false;
}