Skip to content

Commit

Permalink
assimp: Add assimp-disable-extensions config var (for #1537)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
rdb committed Oct 22, 2023
1 parent b4e8cf6 commit 0121e74
Showing 1 changed file with 34 additions and 7 deletions.
41 changes: 34 additions & 7 deletions pandatool/src/assimp/loaderFileTypeAssimp.cxx
Expand Up @@ -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);
}

/**
Expand Down

0 comments on commit 0121e74

Please sign in to comment.