Exemplo n.º 1
0
        //internals
        private bool _ParseDAT()
        {
            fs = new FileStream(this.In_FilePath, FileMode.Open, FileAccess.Read);
            br = new BinaryReader(fs);

            //read header first
            d_header.readIn(br, endian);

            //Seek to TOC
            br.BaseStream.Seek(d_header.dTOC_ptr, SeekOrigin.Begin);

            //Read TOC entries
            for (int i = 0; i < d_header.dTOC_entryCount; i++)
            {
                TOC_ENTRY curEntry = new TOC_ENTRY();
                curEntry.readIn(br, endian);
                if (curEntry.toc_entryPtr == 0xFFFFFFFF)
                {
                    continue;
                }
                d_tocEntryList.Add(curEntry);
            }

            //Seek back to 0
            br.BaseStream.Seek(0, SeekOrigin.Begin);

            //try gather data for extract
            for (int j = 0; j < d_tocEntryList.Count; j++)
            {
                TOC_ENTRY curEntry = d_tocEntryList[j];
                TOC_ENTRY nxtEntry = null;
                if ((j + 1 < d_tocEntryList.Count))
                {
                    nxtEntry = d_tocEntryList[j + 1];
                }
                else
                {
                    nxtEntry = new TOC_ENTRY((uint32)d_header.dTOC_ptr);
                }


                br.BaseStream.Seek(curEntry.toc_entryPtr, SeekOrigin.Begin);
                CHUNK curChunk = new CHUNK();

                List <subCHUNK> subchunks = new List <subCHUNK>();
                while (br.BaseStream.Position < nxtEntry.toc_entryPtr - 16)
                {
                    subCHUNK cur = new subCHUNK();
                    cur.readIn(br, endian);
                    subchunks.Add(cur);

                    //br.BaseStream.Position += IO.PaddingAlign(br.BaseStream.Position, 16);
                }

                if (subchunks.Count == 0)
                {
                    continue;
                }

                curChunk._offset = subchunks[0]._offset;

                List <byte[]> decoded_chunks = new List <byte[]>();
                foreach (subCHUNK s in subchunks)
                {
                    if (s.cdata == null)
                    {
                        continue;
                    }
                    decoded_chunks.Add(s.cdata);
                    //add sizes
                    curChunk.zsize += s.zsize;
                    curChunk.size  += s.size;
                }

                curChunk.cdata = decoded_chunks.SelectMany(x => x).ToArray();

                //write out file with .datchunk extension
                string out_filepath = Path.Combine(this.Out_FolderPath, "entry_" + j + ".datchunk");
                using (FileStream fw = new FileStream(out_filepath, FileMode.Create, FileAccess.Write))
                    using (BinaryWriter bw = new BinaryWriter(fw))
                    {
                        bw.Write(curChunk.cdata);
                        bw.Flush();
                    }

                decoded_chunks.Clear();
            }

            //Seek back to 0
            br.BaseStream.Seek(0, SeekOrigin.Begin);


            return(true);
        }
Exemplo n.º 2
0
        private static void ParseChunks(BinaryReader reader, List <CHUNK> chunks)
        {
            while (reader.BaseStream.Position < reader.BaseStream.Length)
            {
                int block       = reader.ReadInt32();
                int contentLen  = reader.ReadInt32();
                int childrenLen = reader.ReadInt32();
                switch (block)
                {
                case VOX_:
                    Console.WriteLine("should not have encountered \"VOX\" here!");
                    break;

                case PACK:
                    PACKBlock pack = new PACKBlock();
                    pack.id          = block;
                    pack.contentLen  = contentLen;
                    pack.childrenLen = childrenLen;
                    pack.numModels   = reader.ReadInt32();
                    chunks.Add(pack);
                    break;

                case SIZE:
                    SIZEBlock size = new SIZEBlock();
                    size.id          = block;
                    size.contentLen  = contentLen;
                    size.childrenLen = childrenLen;
                    size.sizeX       = reader.ReadInt32();
                    size.sizeY       = reader.ReadInt32();
                    size.sizeZ       = reader.ReadInt32();
                    chunks.Add(size);
                    break;

                case XYZI:
                    XYZIBlock xyzi = new XYZIBlock();
                    xyzi.id          = block;
                    xyzi.contentLen  = contentLen;
                    xyzi.childrenLen = childrenLen;
                    xyzi.numVoxels   = reader.ReadInt32();
                    xyzi.voxels      = new XYZIBlock.XYZIB[xyzi.numVoxels];
                    for (int i = 0; i < xyzi.numVoxels; i++)
                    {
                        xyzi.voxels[i]   = new XYZIBlock.XYZIB();
                        xyzi.voxels[i].x = reader.ReadByte();
                        xyzi.voxels[i].y = reader.ReadByte();
                        xyzi.voxels[i].z = reader.ReadByte();
                        xyzi.voxels[i].m = reader.ReadByte();
                    }
                    chunks.Add(xyzi);
                    break;

                case RGBA:
                    RGBABlock rgba = new RGBABlock();
                    rgba.id          = block;
                    rgba.contentLen  = contentLen;
                    rgba.childrenLen = childrenLen;
                    rgba.colors      = new RGBABlock.COLORB[256];
                    for (int i = 0; i < 256; i++)
                    {
                        rgba.colors[i]   = new RGBABlock.COLORB();
                        rgba.colors[i].r = reader.ReadByte();
                        rgba.colors[i].g = reader.ReadByte();
                        rgba.colors[i].b = reader.ReadByte();
                        rgba.colors[i].a = reader.ReadByte();
                    }
                    chunks.Add(rgba);
                    break;

                default:
                    CHUNK genericChunk = new CHUNK();
                    genericChunk.id          = block;
                    genericChunk.contentLen  = contentLen;
                    genericChunk.childrenLen = childrenLen;
                    chunks.Add(genericChunk);
                    break;
                }
            }
        }