Пример #1
0
        public override void SetBytes(Stream file, int chunkLength, long offsetPos, bool assemblyMode)
        {
            //Factor in the bytes we have already read elsewhere.
            int bytesToRead = chunkLength - (BlockTypeLength + ResourceTypeLength);
            int bytesRead   = 0;

            //Number of resources in this block.
            file.Read(CHUNK_BYTE_BUFFER, 0, ChunkByteLength);
            this.objectCount = BitConverter.ToInt32(CHUNK_BYTE_BUFFER, 0);

            //Preallocate size of collection to avoid resize calls (memory optimization)
            this.resourceDatabase = new Dictionary <UInt32, AssetResource>(this.objectCount);

            bytesRead += ChunkByteLength;

            //Loop and create index elements.
            while (bytesRead < bytesToRead)
            {
                file.Read(CHUNK_BYTE_BUFFER, 0, ChunkByteLength);
                int resourceChunkLength = BitConverter.ToInt32(CHUNK_BYTE_BUFFER, 0);

                int bytesLeftToRead = resourceChunkLength;

                file.Read(CHUNK_BYTE_BUFFER, 0, ChunkByteLength);
                UInt32 referenceID = BitConverter.ToUInt32(CHUNK_BYTE_BUFFER, 0);

                bytesLeftToRead -= ChunkByteLength;

                file.Read(SIMPLE_BYTE_BUFFER, 0, SimpleByteLength);
                bool streamed = BitConverter.ToBoolean(SIMPLE_BYTE_BUFFER, 0);

                bytesLeftToRead -= SimpleByteLength;

                file.Read(CHUNK_BYTE_BUFFER, 0, ChunkByteLength);
                int metaDataType = BitConverter.ToInt32(CHUNK_BYTE_BUFFER, 0);

                bytesLeftToRead -= ChunkByteLength;

                file.Read(CHUNK_BYTE_BUFFER, 0, ChunkByteLength);
                int metadataLength = BitConverter.ToInt32(CHUNK_BYTE_BUFFER, 0);

                bytesLeftToRead -= ChunkByteLength;

                //Optmization, some nodes lack these fields.
                byte[] metaData = null;
                if (metadataLength > 0)
                {
                    metaData = new byte[metadataLength];
                    file.Read(metaData, 0, metadataLength);

                    bytesLeftToRead -= metadataLength;
                }

                byte[] dataBuffer = null;

                //The are used for streamed nodes.
                UInt32 streamPosition = 0;
                UInt32 streamLength   = 0;

                //Optmization, some nodes lack these fields.
                if (bytesLeftToRead != 0)
                {
                    streamPosition = (UInt32)(file.Position + offsetPos);
                    streamLength   = (UInt32)bytesLeftToRead;

                    if (assemblyMode)
                    {
                        streamPosition = (UInt32)(file.Position + offsetPos);
                        streamLength   = (UInt32)bytesLeftToRead;
                        dataBuffer     = new byte[bytesLeftToRead];

                        file.Read(dataBuffer, 0, bytesLeftToRead);
                    }
                    else
                    {
                        //Streamed nodes dont load the byte data into memory, only save the location of where it is in the file.
                        //We also allow the caller to stream all data so that we can create the resources without putting pressure om memory.
                        if (streamed || this.streamResources)
                        {
                            file.Seek(bytesLeftToRead, SeekOrigin.Current);
                        }
                        else
                        {
                            dataBuffer = new byte[bytesLeftToRead];
                            file.Read(dataBuffer, 0, bytesLeftToRead);
                        }
                    }
                }

                AssetResource resource = new AssetResource(streamed, streamPosition, streamLength);
                resource.Deserialize(referenceID, metaDataType, metaData, dataBuffer, assemblyMode);

                //Use hashmap indexing, faster to lookup.
                this.resourceDatabase.Add(referenceID, resource);

                bytesRead += resourceChunkLength + ChunkByteLength;
            }

            #if VERBOSE_LOG
            Debug.Log("Resourceblock length: " + chunkLength);
            #endif
        }