Ejemplo n.º 1
0
        public PCFFile(IFileHandle path, bool memStream, PCFfileType fileType = PCFfileType.Unknown)
        {
            this.memStream = memStream;
            this.path      = path;
            this.fileType  = fileType;
            this.blockData = new Dictionary <PCFResourceType, DataBlockBase>();

            //This must exist in order for pcf file to work properly.
            this.indexBlock = new IndexBlock();
        }
Ejemplo n.º 2
0
        void LoadDataFromDisk(PCFResourceType loadFlags, bool streamResources, bool assemblyMode)
        {
            Stream file = this.path.GetFileStream(FileMode.Open);

            //Load header first.
            this.fileHeader = LoadHeader(file);

            int bytesRead  = HeaderByteLength;
            int fileLength = (int)file.Length;

            while (bytesRead < fileLength)
            {
                int             chunkLength  = GetNextSegmentLength(file);
                PCFBlockTypes   blockType    = GetNextBlockType(file);
                DataBlockBase   block        = null;
                PCFResourceType resourceType = PCFResourceType.NONE;

                if (blockType == PCFBlockTypes.INDEXBLOCK)
                {
                    block = new IndexBlock();

                    this.blockData.Add(PCFResourceType.INDEX, block);
                }
                else if (blockType == PCFBlockTypes.NODEBLOCK)
                {
                    block = new NodeBlock(loadFlags);

                    this.blockData.Add(PCFResourceType.NODE, block);
                }
                else if (blockType == PCFBlockTypes.RESOURCEBLOCK)
                {
                    //Read 4 bytes to determine resourcetype.
                    file.Read(INT_BUFFER, 0, ChunkByteLength);
                    int rawResourceTypeValue = BitConverter.ToInt32(INT_BUFFER, 0);
                    resourceType = (PCFResourceType)Enum.ToObject(typeof(PCFResourceType), rawResourceTypeValue);

                    //Allows us to mask what blocks to read in from file.
                    if ((resourceType & loadFlags) != 0)
                    {
                        block = new ResourceBlock(resourceType, streamResources);
                        this.blockData.Add(resourceType, block);
                    }
                }
                else
                {
                    Debug.LogError("Unknown block type");
                }

                //Increment file position, make sure to count the chunk size bytes.
                bytesRead += chunkLength + ChunkByteLength;

                if (block != null)
                {
                    if (memStream)
                    {
                        int bytsOffset = ChunkByteLength;
                        if (blockType == PCFBlockTypes.RESOURCEBLOCK)
                        {
                            bytsOffset += ChunkByteLength;
                        }

                        long   fileStreamPos = file.Position;
                        byte[] streamBytes   = new byte[chunkLength - bytsOffset];
                        file.Read(streamBytes, 0, chunkLength - bytsOffset);

                        Stream stream = new MemoryStream(streamBytes);

                        block.SetBytes(stream, chunkLength, fileStreamPos, assemblyMode);
                    }
                    else
                    {
                        //Will internally read from file and increment its position.
                        block.SetBytes(file, chunkLength, 0, assemblyMode);
                    }
                }
                else
                {
                    //Manually seek to next chunk incase SetBytes was never called.
                    file.Seek(bytesRead, SeekOrigin.Begin);
                }
            }

            if (file != null)
            {
                //Stop reading pcf file.
                file.Close();
            }
        }
Ejemplo n.º 3
0
 public void Unload()
 {
     this.blockData.Clear();
     this.blockData  = null;
     this.indexBlock = null;
 }