From 91aa397b043aa0f3afe4f5c3360b318acea84387 Mon Sep 17 00:00:00 2001 From: ousnius Date: Sun, 25 Feb 2018 19:42:26 +0100 Subject: [PATCH] Load, save and optimize NIFs without a root node --- lib/NIF/BasicTypes.cpp | 6 ++++++ lib/NIF/NifFile.cpp | 12 +++++++----- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/lib/NIF/BasicTypes.cpp b/lib/NIF/BasicTypes.cpp index 2f1159ab..29bcd812 100644 --- a/lib/NIF/BasicTypes.cpp +++ b/lib/NIF/BasicTypes.cpp @@ -257,6 +257,9 @@ void NiHeader::SetBlockOrder(std::vector>& 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++) { @@ -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); diff --git a/lib/NIF/NifFile.cpp b/lib/NIF/NifFile.cpp index 001c5917..a4394ce3 100644 --- a/lib/NIF/NifFile.cpp +++ b/lib/NIF/NifFile.cpp @@ -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(shape->GetDataRef()); @@ -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; } @@ -1178,7 +1179,8 @@ bool NifFile::RenameDuplicateShapes() { auto nodes = GetChildren(); auto root = GetRootNode(); - nodes.push_back(root); + if (root) + nodes.push_back(root); for (auto &node : nodes) { int dupCount = 0;