public override void SetBytes(Stream file, int chunkLength, long offsetPos, bool assemblyMode) { int bytesToRead = chunkLength - BlockTypeLength; int bytesRead = 0; //Loop and create index elements. while (bytesRead < bytesToRead) { byte[] elementTypeBuffer = new byte[ChunkByteLength]; file.Read(elementTypeBuffer, 0, ChunkByteLength); int rawElementTypeValue = BitConverter.ToInt32(elementTypeBuffer, 0); PCFResourceType resourceType = (PCFResourceType)Enum.ToObject(typeof(PCFResourceType), rawElementTypeValue); byte[] byteOffsetBuffer = new byte[ChunkByteLength]; file.Read(byteOffsetBuffer, 0, ChunkByteLength); int rawByteOffsetValue = BitConverter.ToInt32(byteOffsetBuffer, 0); IndexElement element = new IndexElement(resourceType, rawByteOffsetValue); this.indexElements.Add(element); bytesRead += BYTES_PER_ELEMENT; } #if VERBOSE_LOG Debug.Log("Indexblock length: " + bytesToRead); #endif }
public void AddIndex(IndexElement index) { //We cant add to it, if it doesn't exist now...can we. if (this.indexElements == null) { this.indexElements = new List <IndexElement>(); } indexElements.Add(index); }
void SaveToDisk() { //Open file for reading. FileStream file = new FileStream(this.path.FullName, FileMode.Create, FileAccess.Write); FileHeader header = GetHeaderPrototype(); if (this.indexBlock == null) { Debug.LogError("No Index block specified!"); return; } int indexBlockSize = (this.blockData.Count * IndexBlock.BYTES_PER_ELEMENT) + ChunkByteLength + BlockTypeLength; //Calculate number of bytes to offset from start, make room for header and index block. int fileOffset = indexBlockSize + header.GetLength(); file.Seek((long)fileOffset, SeekOrigin.Begin); foreach (KeyValuePair <PCFResourceType, DataBlockBase> pair in this.blockData) { //Add index for each block. IndexElement indexElement = new IndexElement(pair.Key, fileOffset); this.indexBlock.AddIndex(indexElement); //All byte data for a block. byte[] blockData = pair.Value.GetBytes(); //Make sure we dont end up with empty blocks. if (blockData == null || blockData.Length == 0) { blockData = nullElement; } //Cannot use GetLength() here because we need to factor in all bytes including chunklength bytes. int blockLength = blockData.Length; file.Write(blockData, 0, blockLength); //Move file position to next block. fileOffset += blockLength; } header.SetFileLength(fileOffset - header.GetLength()); //Seek back to file header (aka index 0) and write header + index block to disk. file.Seek(0, SeekOrigin.Begin); byte[] headerBytes = header.GetBytes(); byte[] indexBytes = this.indexBlock.GetBytes(); file.Write(headerBytes, 0, headerBytes.Length); file.Write(indexBytes, 0, indexBytes.Length); if (file != null) { //Stop reading pcf file. file.Dispose(); file.Close(); } }