예제 #1
0
        public bool Write(Cache cache, CacheItemNode node, DataBuffer original)
        {
            try
            {
                DataBuffer index_data = cache.SubArchives[node.SubArchive].ExtractFile("index.dat");
                original.Location   = 0;
                index_data.Location = original.ReadShort() + 4;
                byte pcount = index_data.ReadByte();
                if (pcount >= Palette.Length)
                {
                    index_data.Location -= 5;
                }
                else                 //move to back
                {
                    index_data.Location = index_data.Buffer.Length;
                    byte[] temp = new byte[index_data.Buffer.Length + 12 + Palette.Length * 3];
                    Array.Copy(index_data.Buffer, temp, index_data.Buffer.Length);
                    index_data.Buffer = temp;
                }
                IndexLocation = index_data.Location;
                index_data.WriteShort(WholeWidth);
                index_data.WriteShort(WholeHeight);
                index_data.WriteByte((byte)Palette.Length);
                for (int i = 0; i < Palette.Length - 1; i++)
                {
                    index_data.WriteByte(Palette[i].R);
                    index_data.WriteByte(Palette[i].G);
                    index_data.WriteByte(Palette[i].B);
                }
                index_data.WriteByte((byte)XOffset);
                index_data.WriteByte((byte)YOffset);
                index_data.WriteShort(Width);
                index_data.WriteShort(Height);

                index_data.WriteByte(0);                 //x, y (1 is y, x)
                byte[] data = new byte[Width * Height + 2];
                data[0] = (byte)(IndexLocation >> 8);
                data[1] = (byte)IndexLocation;
                for (int i = 0; i < Width * Height; i++)
                {
                    //todo: optimize
                    Color c = Pixels[i];
                    for (int k = 0; k < Palette.Length; k++)
                    {
                        if (Palette[k].R == c.R && Palette[k].G == c.G && Palette[k].B == c.B && Palette[k].A == c.A)
                        {
                            if (c.A == 0)
                            {
                                data[i + 2] = 0;
                            }
                            else
                            {
                                data[i + 2] = (byte)(k + 1);
                            }
                        }
                    }
                }

                cache.SubArchives[node.SubArchive].WriteFile("index.dat", index_data.Buffer);
                cache.SubArchives[node.SubArchive].WriteFile(node.FileIndex, data);
                cache.SubArchives[node.SubArchive].RewriteArchive();
                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }
예제 #2
0
        public bool RewriteArchive()
        {
            DataBuffer data_buffer = new DataBuffer(new byte[8192]);

            for (int i = 0; i < FileCount; i++)
            {
                if (FileBuffers[i] == null)
                {
                    ExtractFile(i);
                }
                PackFile(ref data_buffer, i, FileBuffers[i], !ArchiveDecompressed);
            }

            DataBuffer header = new DataBuffer(new byte[2 + FileCount * 10]);

            header.WriteShort(FileCount);
            for (int i = 0; i < FileCount; i++)
            {
                header.WriteInteger(FileNames[i]);
                header.Write3Bytes(UncompressedFileSizes[i]);
                header.Write3Bytes(CompressedFileSizes[i]);
                //pos += CompressedFileSizes[i];
            }


            if (ArchiveDecompressed)
            {
                DataBuffer d = new DataBuffer(new byte[data_buffer.Buffer.Length + header.Buffer.Length]);
                //d.Write3Bytes(data_buffer.Buffer.Length);
                //d.Write3Bytes(0);
                d.Write(header.Buffer, 0, header.Location);
                d.Write(data_buffer.Buffer, 0, data_buffer.Location);
                MemoryStream      ms = new MemoryStream();
                BZip2OutputStream os = new BZip2OutputStream(ms, 1);
                os.Write(d.Buffer, 0, d.Location);
                os.Close();

                byte[]     c     = ms.GetBuffer();
                DataBuffer final = new DataBuffer(new byte[os.BytesWritten + 6]);
                final.Write3Bytes(d.Buffer.Length);
                final.Write3Bytes(os.BytesWritten);
                final.Write(c, 0, os.BytesWritten);
                MainBuffer = final.Buffer;
            }
            else
            {
                DataBuffer final = new DataBuffer(new byte[data_buffer.Buffer.Length + header.Buffer.Length + 100000]);
                final.Write3Bytes(data_buffer.Buffer.Length);
                if (ArchiveDecompressed)
                {
                    final.Write3Bytes(0);
                }
                else
                {
                    final.Write3Bytes(data_buffer.Buffer.Length);
                }
                final.Write(header.Buffer, 0, header.Location);
                final.Write(data_buffer.Buffer, 0, data_buffer.Location);
                MainBuffer = final.Buffer;
            }
            LoadBuffer(MainBuffer);

            return(true);
        }