Example #1
0
        public int GetStringIndex(string name, bool addnew = false)
        {
            for (var i = 0; i < strings.Count; i++)
            {
                if (strings[i].str == name || (string.IsNullOrEmpty(name) && string.IsNullOrEmpty(strings[i].str)))
                {
                    return(i);
                }
            }

            if (addnew)
            {
                var newstring = new CR2WHeaderString();
                newstring.str = name;
                strings.Add(newstring);

                return(strings.Count - 1);
            }

            return(-1);
        }
Example #2
0
        public void Read(BinaryReader file)
        {
            var filetype = file.ReadBytes(4);

            if (!IDString.SequenceEqual(filetype))
            {
                throw new InvalidFileTypeException("Invalid file type");
            }

            FileVersion = file.ReadUInt32();
            unk2        = file.ReadUInt32();
            unk3        = file.ReadUInt32();

            unk4 = file.ReadUInt32();
            unk5 = file.ReadUInt32();

            cr2wsize   = file.ReadUInt32();
            buffersize = file.ReadUInt32();

            unk6 = file.ReadUInt32();
            unk7 = file.ReadUInt32();

            headers = new List <CR2WHeaderData>();

            for (var i = 0; i < 10; i++)
            {
                var head = new CR2WHeaderData(file);
                headers.Add(head);

                if (head.size > 0 && i > 6)
                {
                    // Unhandled blocks
                    Debugger.Break();
                }
            }

            var string_buffer_start = headers[0].offset;

            // Read Strings
            {
                strings = new List <CR2WHeaderString>();

                var string_start = headers[1].offset;
                var string_count = headers[1].size;

                file.BaseStream.Seek(string_start, SeekOrigin.Begin);

                for (var i = 0; i < string_count; i++)
                {
                    var str = new CR2WHeaderString(file);
                    str.ReadString(file, string_buffer_start);
                    strings.Add(str);
                }
            }

            // Read Handles
            {
                handles = new List <CR2WHeaderHandle>();

                var handles_start = headers[2].offset;
                var handles_count = headers[2].size;

                file.BaseStream.Seek(handles_start, SeekOrigin.Begin);

                for (var i = 0; i < handles_count; i++)
                {
                    var handle = new CR2WHeaderHandle(file);
                    handle.ReadString(file, string_buffer_start);
                    handles.Add(handle);
                }
            }

            // TODO: Figure out where these things belong, possibly block 4 or 7?
            //file.BaseStream.Seek(handles[handles.Count - 1].offset + string_buffer_start, SeekOrigin.Begin);
            //file.ReadCR2WString();
            //unknownstrings = new List<string>();
            //while (file.BaseStream.Position < headers[1].offset)
            //{
            //   unknownstrings.Add(file.ReadCR2WString());
            //}

            // Read Unknown Block 4
            {
                block4 = new List <CR2WHeaderBlock4>();

                var block4_start = headers[3].offset;
                var block4_count = headers[3].size;

                file.BaseStream.Seek(block4_start, SeekOrigin.Begin);

                for (var i = 0; i < block4_count; i++)
                {
                    var block = new CR2WHeaderBlock4(file);
                    block4.Add(block);
                }
            }

            // Read Chunks
            {
                chunks = new List <CR2WChunk>();

                var chunk_start = headers[4].offset;
                var chunk_count = headers[4].size;

                file.BaseStream.Seek(chunk_start, SeekOrigin.Begin);

                for (var i = 0; i < chunk_count; i++)
                {
                    var chunktypeId = file.ReadUInt16();

                    var chunk = new CR2WChunk(this);
                    chunk.typeId = chunktypeId;
                    chunk.Read(file);
                    chunks.Add(chunk);
                }
            }

            // Read Unknown Block 6
            {
                block6 = new List <CR2WHeaderBlock6>();

                var block6_start = headers[5].offset;
                var block6_count = headers[5].size;

                file.BaseStream.Seek(block6_start, SeekOrigin.Begin);

                for (var i = 0; i < block6_count; i++)
                {
                    var block = new CR2WHeaderBlock6(file);
                    block6.Add(block);
                }
            }


            // Read Unknown Block 7
            {
                block7 = new List <CR2WHeaderBlock7>();

                var block7_start = headers[6].offset;
                var block7_count = headers[6].size;

                file.BaseStream.Seek(block7_start, SeekOrigin.Begin);

                for (var i = 0; i < block7_count; i++)
                {
                    var block = new CR2WHeaderBlock7(file);
                    block7.Add(block);
                }
            }

            // Read Chunk buffers
            {
                for (var i = 0; i < chunks.Count; i++)
                {
                    chunks[i].ReadData(file);
                }
            }

            // Read Block7 buffers
            {
                for (var i = 0; i < block7.Count; i++)
                {
                    block7[i].ReadString(file, string_buffer_start);
                    block7[i].ReadData(file);
                }
            }

            file.BaseStream.Seek(cr2wsize, SeekOrigin.Begin);

            var actualbuffersize = (int)(buffersize - cr2wsize);

            if (actualbuffersize > 0)
            {
                bufferdata = new byte[actualbuffersize];
                file.BaseStream.Read(bufferdata, 0, actualbuffersize);
            }
        }