From bc88828ba5c89f1ff54c95d8dd840e3b3d01f99f Mon Sep 17 00:00:00 2001
From: Shon Ferguson <shonferg@users.sourceforge.net>
Date: Sat, 24 Jun 2006 16:08:21 +0000
Subject: [PATCH] Created CloneNifTree function.

---
 niflib.cpp | 11 +++++++++++
 niflib.h   | 11 ++++++++++-
 2 files changed, 21 insertions(+), 1 deletion(-)

diff --git a/niflib.cpp b/niflib.cpp
index bea78575..9ea75996 100644
--- a/niflib.cpp
+++ b/niflib.cpp
@@ -786,4 +786,15 @@ unsigned int GetVersion(string version){
    return outver;
 }
 
+Ref<NiObject> CloneNifTree( Ref<NiObject> const & root, unsigned int version, unsigned int user_version ) {
+	//Create a string stream to temporarily hold the state-save of this tree
+	stringstream tmp;
+
+	//Write the existing tree into the stringstream
+	WriteNifTree( tmp, root, version, user_version );
+
+	//Read the data back out of the stringstream, returning the new tree
+	return ReadNifTree( tmp );
+}
+
 } // namespace NifLib
\ No newline at end of file
diff --git a/niflib.h b/niflib.h
index 9639e0f3..86409a7d 100644
--- a/niflib.h
+++ b/niflib.h
@@ -199,7 +199,6 @@ NIFLIB_API void WriteNifTree( string const & file_name, Ref<NiObject> const & ro
  */
 NIFLIB_API void WriteNifTree( ostream & stream, Ref<NiObject> const & root, unsigned int version = VER_4_0_0_2, unsigned int user_version = 0 );
 
-//TODO:  This was written by Amorilia.  Figure out how to fix it.
 /*!
  * Writes a bunch of files given a base file name, and a pointer to the root block of the Nif file tree.
  * \param file_name The desired file name for the base NIF file. This name serves as the basis for the names of any Kf files and Kfm files as well.  The path is relative to the working directory unless a full path is specified.
@@ -210,6 +209,16 @@ NIFLIB_API void WriteNifTree( ostream & stream, Ref<NiObject> const & root, unsi
  */
 NIFLIB_API void WriteFileGroup( string const & file_name, Ref<NiObject> const & root, unsigned int version, ExportOptions export_files, NifGame kf_type );
 
+/*!
+ * Creates a clone of an entire tree of objects.
+ * \param root The root block to start from when cloning the NIF data.  All referenced objects will be included in the new tree.
+ * \param version The version of the NIF format to use when writing a file.  Default is version 4.0.0.2.
+ * \param user_version The user version of the NIF format to use when writing a file.  Default is user version 0.
+ * \return The root of the new cloned tree.
+ */
+NIFLIB_API Ref<NiObject> CloneNifTree( Ref<NiObject> const & root, unsigned int version = VER_4_0_0_2, unsigned int user_version = 0 );
+
+
 //TODO:  Figure out how to fix this to work with the new system
 /*!
  * Merges two Nif trees into one.  For standard Nif files, any blocks with the same name are merged.  For Kf files, blocks are attatched to those that match the name specified in the KF root block.  The data stored in a NIF file varies from version to version.  Usually you are safe with the default option (the highest availiable version) but you may need to use an earlier version if you need to clone an obsolete piece of information.
-- 
GitLab