From 1e928f302028a869ce6a147d8717e48c6407a260 Mon Sep 17 00:00:00 2001
From: Amorilia <amorilia@users.sourceforge.net>
Date: Sat, 24 Sep 2011 17:01:37 +0100
Subject: [PATCH] Use single root for nif in WriteNifTree and
 ResolveMissingLinkStack, to make interface consistent with the rest of
 niflib.

---
 include/niflib.h                 |  8 ++++----
 src/niflib.cpp                   | 20 +++++++++-----------
 test/missing_link_stack_test.cpp |  8 ++------
 3 files changed, 15 insertions(+), 21 deletions(-)

diff --git a/include/niflib.h b/include/niflib.h
index 114ff827..db1eab65 100644
--- a/include/niflib.h
+++ b/include/niflib.h
@@ -108,10 +108,10 @@ enum ExportOptions {
 NIFLIB_API unsigned int GetNifVersion( string const & file_name );
 
 /*!
- * Return the missing link stack with objects replaced from nif trees at specified roots.
+ * Return the missing link stack with objects replaced from nif trees at specified root.
  */
 NIFLIB_API list<Ref<NiObject> > ResolveMissingLinkStack(
-	list<Ref<NiObject> > const & roots,
+	NiObject *root,
 	const list<NiObject *> & missing_link_stack);
 
 /*!
@@ -165,12 +165,12 @@ NIFLIB_API Ref<NiObject> ReadNifTree( istream & in, NifInfo * info = NULL );
 /*!
  * Creates a new NIF file of the given file name by crawling through the data tree starting with the root objects given, and keeps track of links that cannot been written.
  * \param[in] out The output stream to write the NIF data to.
- * \param[in] roots The root objects to start from when writing out the NIF file.  All decedents of these blocks will be written to the file in tree-descending order.
+ * \param[in] root The root object to start from when writing out the NIF file.  All decedents of this block will be written to the file in tree-descending order.
  * \param[in] missing_link_stack stack of links which are referred to but which are not inside the tree rooted by roots.
  * \param[in] info A NifInfo structure that contains information such as the version of the NIF file to create.
  * \sa ReadNifList, WriteNifTree
  */
-NIFLIB_API void WriteNifTree( ostream & out, list<Ref<NiObject> > const & roots, list<NiObject *> & missing_link_stack, const NifInfo & info = NifInfo() );
+NIFLIB_API void WriteNifTree( ostream & out, NiObject *root, list<NiObject *> & missing_link_stack, const NifInfo & info = NifInfo() );
 
 /*!
  * Creates a new NIF file of the given file name by crawling through the data tree starting with the root object given.
diff --git a/src/niflib.cpp b/src/niflib.cpp
index 6d889bc1..b9c233c0 100644
--- a/src/niflib.cpp
+++ b/src/niflib.cpp
@@ -407,7 +407,7 @@ vector<NiObjectRef> ReadNifList( istream & in, list<NiObjectRef> & missing_link_
 	return obj_list;
 }
 
-NiObjectRef _ResolveMissingLinkStackHelper(NiObjectRef root, NiObject *obj) {
+NiObjectRef _ResolveMissingLinkStackHelper(NiObject *root, NiObject *obj) {
 	// search by name
 	NiNodeRef rootnode = DynamicCast<NiNode>(root);
 	NiNodeRef objnode = DynamicCast<NiNode>(obj);
@@ -428,20 +428,12 @@ NiObjectRef _ResolveMissingLinkStackHelper(NiObjectRef root, NiObject *obj) {
 }
 
 list<NiObjectRef> ResolveMissingLinkStack(
-	list<NiObjectRef> const & roots,
+	NiObject *root,
 	const list<NiObject *> & missing_link_stack)
 {
 	list<NiObjectRef> result;
 	for (list<NiObject *>::const_iterator obj = missing_link_stack.begin(); obj != missing_link_stack.end(); ++obj) {
-		NiObjectRef resolved;
-		if (*obj != NULL) {
-			for (list<NiObjectRef>::const_iterator root = roots.begin(); root != roots.end(); ++root) {
-				resolved = _ResolveMissingLinkStackHelper(*root, *obj);
-				if (resolved != NULL)
-					break;
-			}
-		}
-		result.push_back(resolved);
+		result.push_back(_ResolveMissingLinkStackHelper(root, *obj));
 	}
 	return result;
 }
@@ -592,6 +584,12 @@ void WriteNifTree( ostream & out, list<NiObjectRef> const & roots, list<NiObject
 	out << hdrInfo(NULL);
 }
 
+void WriteNifTree( ostream & out, NiObject *root, list<NiObject *> & missing_link_stack, const NifInfo & info) {
+	list<NiObjectRef> roots;
+	roots.push_back(root);
+	WriteNifTree( out, roots, missing_link_stack, info );
+}
+
 void WriteNifTree( ostream & out, list<NiObjectRef> const & roots, const NifInfo & info) {
 	list<NiObject *> missing_link_stack;
 	WriteNifTree( out, roots, missing_link_stack, info );
diff --git a/test/missing_link_stack_test.cpp b/test/missing_link_stack_test.cpp
index 59abb879..9d0b42e8 100644
--- a/test/missing_link_stack_test.cpp
+++ b/test/missing_link_stack_test.cpp
@@ -43,9 +43,7 @@ BOOST_AUTO_TEST_CASE(missing_link_stack_simple_test)
   }
   // write
   list<NiObject *> missing_link_stack;
-  list<NiObjectRef> roots;
-  roots.push_back(StaticCast<NiObject>(shape));
-  BOOST_CHECK_NO_THROW(WriteNifTree(ss, roots, missing_link_stack, NifInfo(VER_20_0_0_5)));
+  BOOST_CHECK_NO_THROW(WriteNifTree(ss, shape, missing_link_stack, NifInfo(VER_20_0_0_5)));
   bool has_root = false;
   bool has_bone = false;
   // check that root and bone are in the missing link stack
@@ -63,9 +61,7 @@ BOOST_AUTO_TEST_CASE(missing_link_stack_simple_test)
   BOOST_CHECK_EQUAL(has_root, true);
   BOOST_CHECK_EQUAL(has_bone, true);
   // read it again
-  roots.clear();
-  roots.push_back(StaticCast<NiObject>(root));
-  list<NiObjectRef> resolved_link_stack = ResolveMissingLinkStack(roots, missing_link_stack);
+  list<NiObjectRef> resolved_link_stack = ResolveMissingLinkStack(root, missing_link_stack);
   ss.seekg(0);
   NifInfo info;
   NiObjectRef new_root;
-- 
GitLab