Beispiel #1
0
        public IObjectInfo <T> Clone()
        {
            T newObj = null;

            using (var ms = new MemoryStream())
            {
                ObjectInfo <T> newInfo = (ObjectInfo <T>) ObjectInfo <T> .FromTypeIndex(ParentFile, TypeIndex, null);

                newInfo.DataOffset = 0;
                newInfo.ObjectID   = 0;
                using (var writer = new AssetsWriter(ms))
                {
                    Object.Write(writer);
                }
                newInfo.DataSize = (int)ms.Length;
                ms.Seek(0, SeekOrigin.Begin);
                using (var reader = new AssetsReader(ms))
                {
                    newObj = (T)Activator.CreateInstance(typeof(T), newInfo, reader);
                }
                newInfo.DataOffset = -1;
                newInfo.DataSize   = -1;
                newInfo._object    = newObj;
            }
            return((IObjectInfo <T>)newObj.ObjectInfo);
        }
Beispiel #2
0
        public T Clone(AssetsFile toFile = null)
        {
            T newObj = null;

            using (var ms = new MemoryStream())
            {
                int typeIndex = TypeIndex;
                if (toFile != null)
                {
                    var type = ParentFile.Metadata.Types[typeIndex];
                    typeIndex = toFile.GetOrCreateMatchingTypeIndex(type);
                }
                //we do want to get the types from the new file
                ObjectInfo <T> newInfo = (ObjectInfo <T>) ObjectInfo <T> .FromTypeIndex(toFile ?? ParentFile, typeIndex, null);

                newInfo.DataOffset = 0;
                newInfo.ObjectID   = 0;
                using (var writer = new AssetsWriter(ms))
                {
                    Object.Write(writer);
                }
                newInfo.DataSize = (int)ms.Length;
                ms.Seek(0, SeekOrigin.Begin);

                //parent file has to be set to properly to the original file to create pointers
                if (toFile != null)
                {
                    newInfo.ParentFile = ParentFile;
                }
                using (var reader = new AssetsReader(ms))
                {
                    newObj = (T)Activator.CreateInstance(typeof(T), newInfo, reader);
                }
                //set it back so that things moving forward think it's in the new file.
                if (toFile != null)
                {
                    newInfo.ParentFile = toFile;
                }

                newInfo.DataOffset = -1;
                newInfo.DataSize   = -1;
                newInfo._object    = newObj;
            }
            return((T)newObj);
        }