From 0121e74aa48fda10300c5809667ade67e86c5db0 Mon Sep 17 00:00:00 2001 From: rdb Date: Sun, 22 Oct 2023 11:59:30 +0200 Subject: [PATCH] assimp: Add assimp-disable-extensions config var (for #1537) This can be used to disable certain extensions from being loaded via Assimp Also makes the `get_additional_extensions()` method a bit more efficient by having it avoid string concatenation, using a temporary buffer instead --- pandatool/src/assimp/loaderFileTypeAssimp.cxx | 41 +++++++++++++++---- 1 file changed, 34 insertions(+), 7 deletions(-) diff --git a/pandatool/src/assimp/loaderFileTypeAssimp.cxx b/pandatool/src/assimp/loaderFileTypeAssimp.cxx index bd2eb19d53..2b82e34e2a 100644 --- a/pandatool/src/assimp/loaderFileTypeAssimp.cxx +++ b/pandatool/src/assimp/loaderFileTypeAssimp.cxx @@ -57,22 +57,49 @@ get_extension() const { */ string LoaderFileTypeAssimp:: get_additional_extensions() const { + // This may be called at static init time, so ensure it is constructed now. + static ConfigVariableString assimp_disable_extensions + ("assimp-disable-extensions", "", + PRC_DESC("A list of extensions (without preceding dot) that should not be " + "loaded via the Assimp loader, even if Assimp supports these " + "formats. It is useful to set this for eg. gltf and glb files " + "to prevent them from being accidentally loaded via the Assimp " + "plug-in instead of via a superior plug-in like panda3d-gltf.")); + + bool has_disabled_exts = !assimp_disable_extensions.empty(); + aiString aexts; aiGetExtensionList(&aexts); + char *buffer = (char *)alloca(aexts.length + 2); + char *p = buffer; + // The format is like: *.mdc;*.mdl;*.mesh.xml;*.mot - std::string ext; char *sub = strtok(aexts.data, ";"); while (sub != nullptr) { - ext += sub + 2; - sub = strtok(nullptr, ";"); - - if (sub != nullptr) { - ext += ' '; + bool enabled = true; + if (has_disabled_exts) { + for (size_t i = 0; i < assimp_disable_extensions.get_num_words(); ++i) { + std::string disabled_ext = assimp_disable_extensions.get_word(i); + if (strcmp(sub + 2, disabled_ext.c_str()) == 0) { + enabled = false; + break; + } + } } + if (enabled) { + *(p++) = ' '; + size_t len = strlen(sub + 2); + memcpy(p, sub + 2, len); + p += len; + } + + sub = strtok(nullptr, ";"); } - return ext; + // Strip first space + ++buffer; + return std::string(buffer, p - buffer); } /**