Skip to content

Commit

Permalink
Load, save and optimize NIFs without a root node
Browse files Browse the repository at this point in the history
  • Loading branch information
ousnius committed Feb 25, 2018
1 parent 80a19d7 commit 91aa397
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
6 changes: 6 additions & 0 deletions lib/NIF/BasicTypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,9 @@ void NiHeader::SetBlockOrder(std::vector<std::pair<int, int>>& newIndices) {
return p.first == 0xFFFFFFFF;
}), newIndices.end());

if (newIndices.empty())
return;

for (int i = 0; i < newIndices.size() - 1; i++) {
bool swapped = false;
for (int j = 0; j < newIndices.size() - i - 1; j++) {
Expand Down Expand Up @@ -308,6 +311,9 @@ bool NiHeader::IsBlockReferenced(const int blockId) {
}

void NiHeader::DeleteUnreferencedBlocks(const int rootId, bool* hadDeletions) {
if (rootId == 0xFFFFFFFF)
return;

for (int i = 0; i < numBlocks; i++) {
if (i != rootId && !IsBlockReferenced(i)) {
DeleteBlock(i);
Expand Down
12 changes: 7 additions & 5 deletions lib/NIF/NifFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -810,10 +810,6 @@ OptResultSSE NifFile::OptimizeForSSE(const OptOptionsSSE& options) {
version.SetStream(100);

for (auto &shape : GetShapes()) {
NiNode* parentNode = GetParentNode(shape);
if (!parentNode)
continue;

std::string shapeName = shape->GetName();

auto geomData = hdr.GetBlock<NiGeometryData>(shape->GetDataRef());
Expand Down Expand Up @@ -1051,6 +1047,11 @@ OptResultSSE NifFile::OptimizeForSSE(const OptOptionsSSE& options) {
}

DeleteUnreferencedBlocks();

// For files without a root node, remove the leftover data blocks anyway
hdr.DeleteBlockByType("NiTriStripsData");
hdr.DeleteBlockByType("NiTriShapeData");

return result;
}

Expand Down Expand Up @@ -1178,7 +1179,8 @@ bool NifFile::RenameDuplicateShapes() {
auto nodes = GetChildren<NiNode>();

auto root = GetRootNode();
nodes.push_back(root);
if (root)
nodes.push_back(root);

for (auto &node : nodes) {
int dupCount = 0;
Expand Down

0 comments on commit 91aa397

Please sign in to comment.