Ejemplo n.º 1
0
        private static GrfItem Create
        (
            GrfArchive grf,
            string name,
            int compressedLength,
            int compressedLengthAligned,
            int realLength,
            GrfFileFlags flags,
            int position
        )
        {
            GrfItem ret;

            if (IsDirectory(compressedLength, compressedLengthAligned, realLength, flags, position))
            {
                ret = new GrfDirectoryInfo();
            }
            else
            {
                ret = new GrfFileInfo();
            }

            ret.m_Grf  = grf;
            ret.m_Name = FilenameEncoding.Encode(name);
            ret.m_AlignedCompressedLength = compressedLengthAligned;
            ret.m_CompressedLength        = compressedLength;
            ret.m_Length   = realLength;
            ret.m_Flags    = flags;
            ret.m_Position = position;
            ret.m_Hash     = NameHash(ret.m_Name);

            return(ret);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Adds a new <see cref="GrfItem"/> to the <see cref="GrfArchive"/>.
        /// </summary>
        /// <param name="grfname">The filename inside the <see cref="GrfArchive"/>.</param>
        /// <param name="flags">The <see cref="GrfFileFlags"/> for this entry.</param>
        /// <param name="data">The data of the file, or null if the <see cref="GrfItem"/> is a folder.</param>
        /// <exception cref="ObjectDisposedException">
        /// Thrown when the <see cref="GrfArchive"/> is closed.
        /// </exception>
        /// <exception cref="ArgumentNullException">
        /// Thrown when the grfname is null or empty, or when the flags indicate a file and the data is null.
        /// </exception>
        /// <exception cref="AccessViolationException">
        /// Thrown when the <see cref="GrfArchive"/> is read-only.
        /// </exception>
        private void Add(string grfname, GrfFileFlags flags, byte[] data)
        {
            if (m_Grf.IsDisposed)
            {
                throw new ObjectDisposedException(m_Grf.GetType().Name);
            }

            if (m_Grf.IsReadOnly)
            {
                throw new AccessViolationException("The GrfArchive is read-only");
            }

            if (string.IsNullOrEmpty(grfname))
            {
                throw new ArgumentNullException("filename");
            }

            if ((flags & GrfFileFlags.File) == GrfFileFlags.File && data == null)
            {
                throw new ArgumentNullException("data");
            }

            GrfItem add;
            int     i = IndexOf(grfname);

            if ((flags & GrfFileFlags.File) == GrfFileFlags.None || data == null)
            {
                add = new GrfDirectoryInfo(m_Grf, grfname, flags);
            }
            else
            {
                add = new GrfFileInfo(m_Grf, grfname, flags, data);
            }

            if (i >= 0)
            {
                m_Items[i] = add;
            }
            else
            {
                m_Items.Add(add);
            }

            m_Grf.Modified = true;
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Adds a <see cref="GrfItem"/> to the <see cref="GrfArchive"/>.
        /// </summary>
        /// <param name="item">The <see cref="GrfItem"/> to add.</param>
        /// <exception cref="ObjectDisposedException">
        /// Thrown when the <see cref="GrfArchive"/> is closed.
        /// </exception>
        /// <exception cref="AccessViolationException">
        /// Thrown when the <see cref="GrfArchive"/> is read-only.
        /// </exception>
        public void Add(GrfItem item)
        {
            if (m_Grf.IsDisposed)
            {
                throw new ObjectDisposedException(m_Grf.GetType().Name);
            }

            if (m_Grf.IsReadOnly)
            {
                throw new AccessViolationException("The GrfArchive is read-only");
            }

            int i = IndexOf(item.Name);

            if (item is GrfDirectoryInfo)
            {
                GrfDirectoryInfo add = new GrfDirectoryInfo(m_Grf, item.Name, item.Flags);
                if (i < 0)
                {
                    GrfAdd(add);
                }
                else
                {
                    m_Items[i] = add;
                }
            }
            else if (item is GrfFileInfo)
            {
                GrfFileInfo add, file = (GrfFileInfo)item;

                add = new GrfFileInfo(m_Grf, item.Name, item.Flags, file.Data);

                if (i < 0)
                {
                    GrfAdd(add);
                }
                else
                {
                    m_Items[i] = add;
                }
            }
        }
Ejemplo n.º 4
0
        private void FlushFile(int i)
        {
            if (m_Items[i] is GrfDirectoryInfo)
            {
                return;
            }

            GrfFileInfo gfile = (GrfFileInfo)m_Items[i];

            byte[] buf = ZlibStream.CompressBuffer(gfile.Data);
            gfile.CompressedLength        = buf.Length;
            gfile.AlignedCompressedLength = gfile.CompressedLength;

            if ((gfile.Flags & (GrfFileFlags.MixCrypt | GrfFileFlags.Des_0x14)) != GrfFileFlags.None)
            {
                gfile.AlignedCompressedLength += gfile.CompressedLength % 8;
                Array.Resize <byte>(ref buf, gfile.AlignedCompressedLength);
                buf = GrfCrypt.EncryptFileBuffer(buf, gfile.CompressedLength, gfile.Flags);
            }

            int writeOffset = m_Items.FindUnused(gfile.AlignedCompressedLength);

            if (writeOffset == 0)
            {
                // grf_find_unused returned 0 -> append
                FileStream.Seek(0, SeekOrigin.End);
            }
            else
            {
                FileStream.Seek(writeOffset, SeekOrigin.Begin);
            }

            gfile.Position = (int)FileStream.Position;

            FileStream.Write(buf, 0, buf.Length);
        }
Ejemplo n.º 5
0
        private void FlushVer2()
        {
            byte[]      buffer = new byte[m_Items.Count * GRF_ITEM_SIZE];
            IntPtrEx    pName;
            int         i, offset, len;
            GrfFileInfo gfile = null;

            try
            {
                m_Items.SortToPosition();

                using (IntPtrEx pBuffer = new IntPtrEx(buffer))
                {
                    for (i = offset = 0; i < Items.Count; i++)
                    {
                        len = m_Items[i].FullName.Length + 1;
                        using (pName = new IntPtrEx(m_Items[i].FullName))
                        {
                            Marshal.Copy(pName, buffer, offset, len);
                        }

                        offset += len;

                        if (m_Items[i] is GrfFileInfo)
                        {
                            gfile = (GrfFileInfo)m_Items[i];

                            if (m_ForceRepack)
                            {
                                gfile.CompressedLength        = 0;
                                gfile.AlignedCompressedLength = 0;
                                gfile.Position = 0;
                            }

                            if (gfile.CompressedLength == 0 &&
                                gfile.AlignedCompressedLength == 0 &&
                                gfile.Position == 0 &&
                                gfile.Length != 0)
                            {
                                if (!m_AllowCrypt)
                                {
                                    gfile.Flags &= ~(GrfFileFlags.MixCrypt | GrfFileFlags.Des_0x14);
                                }

                                FlushFile(i);
                            }

                            pBuffer.Write <int>(offset, EndianConverter.LittleEndian(gfile.CompressedLength));
                            pBuffer.Write <int>(offset + 4, EndianConverter.LittleEndian(gfile.AlignedCompressedLength));
                            pBuffer.Write <int>(offset + 8, EndianConverter.LittleEndian(gfile.Length));
                            pBuffer.Write <int>(offset + 0xD, EndianConverter.LittleEndian(gfile.Position - GRF_HEADER_FULL_LEN));
                        }
                        else
                        {
                            pBuffer.Write <int>(offset, EndianConverter.LittleEndian(GrfItem.GRFFILE_DIR_SZSMALL));
                            pBuffer.Write <int>(offset + 4, EndianConverter.LittleEndian(GrfItem.GRFFILE_DIR_SZFILE));
                            pBuffer.Write <int>(offset + 8, EndianConverter.LittleEndian(GrfItem.GRFFILE_DIR_SZORIG));
                            pBuffer.Write <int>(offset + 0xD, EndianConverter.LittleEndian(GrfItem.GRFFILE_DIR_OFFSET - GRF_HEADER_FULL_LEN));
                        }

                        pBuffer.Write <byte>(offset + 0xC, (byte)m_Items[i].Flags);

                        offset += 0x11;
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }

            Array.Resize <byte>(ref buffer, offset);
            buffer = ZlibStream.CompressBuffer(buffer);

            m_Items.SortToPosition();

            int writeOffset = m_Items.FindUnused(8 + buffer.Length);

            if (writeOffset == 0)
            {
                FileStream.Seek(0, SeekOrigin.End);
                writeOffset = (int)FileStream.Position;
            }
            else
            {
                FileStream.Seek(writeOffset, SeekOrigin.Begin);
            }

            FileStream.Write(BitConverter.GetBytes(EndianConverter.LittleEndian(buffer.Length)), 0, 4);
            FileStream.Write(BitConverter.GetBytes(EndianConverter.LittleEndian(offset)), 0, 4);
            FileStream.Write(buffer, 0, buffer.Length);

            FileStream.Seek(GRF_HEADER_MID_LEN, SeekOrigin.Begin);

            FileStream.Write(BitConverter.GetBytes(EndianConverter.LittleEndian(writeOffset - GRF_HEADER_FULL_LEN)), 0, 4);
            FileStream.Write(BitConverter.GetBytes(0), 0, 4);
            FileStream.Write(BitConverter.GetBytes(EndianConverter.LittleEndian(i + 7)), 0, 4);
        }
Ejemplo n.º 6
0
        private void FlushVer1()
        {
            byte[]      buffer = new byte[m_Items.Count * GRF_ITEM_SIZE];
            int         i, offset, len, writeOffset;
            GrfFileInfo gfile = null;

            try
            {
                m_Items.SortToPosition();

                using (IntPtrEx pBuffer = new IntPtrEx(buffer))
                {
                    for (i = offset = 0; i < Items.Count; i++)
                    {
                        if (m_Items[i] is GrfFileInfo)
                        {
                            gfile = (GrfFileInfo)m_Items[i];

                            if (m_ForceRepack)
                            {
                                gfile.CompressedLength        = 0;
                                gfile.AlignedCompressedLength = 0;
                                gfile.Position = 0;
                            }

                            if (gfile.CompressedLength == 0 &&
                                gfile.AlignedCompressedLength == 0 &&
                                gfile.Position == 0 &&
                                gfile.Length != 0)
                            {
                                if (gfile.CheckExtension())
                                {
                                    gfile.Flags = (gfile.Flags & ~GrfFileFlags.MixCrypt) | GrfFileFlags.Des_0x14;
                                }
                                else
                                {
                                    gfile.Flags = (gfile.Flags & ~GrfFileFlags.Des_0x14) | GrfFileFlags.MixCrypt;
                                }

                                FlushFile(i);
                            }
                        }

                        len = m_Items[i].FullName.Length + 1;
                        if (m_IntVersion < 0x101)
                        {
                            pBuffer.Write <int>(offset, len);
                            SwapNibbles(pBuffer + offset + 4, m_Items[i].FullName, len);
                            offset += 4 + len;
                        }
                        else if (m_IntVersion < 0x104)
                        {
                            pBuffer.Write <int>(offset, len + 6);
                            offset += 4;
                            SwapNibbles(pBuffer + offset + 6, GrfCrypt.EncryptNameVer1(m_Items[i].Name, len), len);
                            offset += len + 6;
                        }

                        if (m_Items[i] is GrfDirectoryInfo)
                        {
                            pBuffer.Write <int>(offset, EndianConverter.LittleEndian(GrfItem.GRFFILE_DIR_SZSMALL + GrfItem.GRFFILE_DIR_SZORIG + 0x02CB));
                            pBuffer.Write <int>(offset + 4, EndianConverter.LittleEndian(GrfItem.GRFFILE_DIR_SZFILE + 0x92CB));
                            pBuffer.Write <int>(offset + 8, EndianConverter.LittleEndian(GrfItem.GRFFILE_DIR_SZORIG));
                            pBuffer.Write <int>(offset + 0xD, EndianConverter.LittleEndian(GrfItem.GRFFILE_DIR_OFFSET - GRF_HEADER_FULL_LEN));
                        }
                        else
                        {
                            pBuffer.Write <int>(offset, EndianConverter.LittleEndian(gfile.CompressedLength + gfile.Length + 0x02CB));
                            pBuffer.Write <int>(offset + 4, EndianConverter.LittleEndian(gfile.AlignedCompressedLength + 0x92CB));
                            pBuffer.Write <int>(offset + 8, EndianConverter.LittleEndian(gfile.Length));
                            pBuffer.Write <int>(offset + 0xD, EndianConverter.LittleEndian(gfile.Position - GRF_HEADER_FULL_LEN));
                        }

                        pBuffer.Write <byte>(offset + 0xC, (byte)(m_Items[i].Flags & GrfFileFlags.File));

                        offset += 0x11;
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }

            FileStream.Seek(0, SeekOrigin.End);
            writeOffset = (int)FileStream.Position;
            FileStream.Write(buffer, 0, offset);
            FileStream.Seek(GRF_HEADER_MID_LEN, SeekOrigin.End);

            FileStream.Write(BitConverter.GetBytes(EndianConverter.LittleEndian(writeOffset - GRF_HEADER_FULL_LEN)), 0, 4);
            FileStream.Write(BitConverter.GetBytes(0), 0, 4);
            FileStream.Write(BitConverter.GetBytes(EndianConverter.LittleEndian(i + 0 + 7)), 0, 4);
        }