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
This commit is contained in:
rdb 2023-10-22 11:59:30 +02:00
parent b4e8cf6958
commit 0121e74aa4

View File

@ -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);
}
/**