Пример #1
0
 public ZtrFileTextUnpacker(Stream input, ZtrFileEntry[] output, ZtrFileHeaderLineInfo[] offsets, FFXIIITextEncoding encoding)
 {
     _input = input;
     _output = output;
     _offsets = offsets;
     _encoding = encoding;
 }
Пример #2
0
        public void Pack()
        {
            byte blockIndex = 0;
            List<int> blockOffsets = new List<int>(256) {0};
            ZtrFileHeaderLineInfo[] infos = new ZtrFileHeaderLineInfo[_input.Length];

            //int offset = 0;
            //for (int i = 0; i < _input.Length; i++)
            //{
            //    String value = _input[i].Value;
            //    ZtrFileHeaderLineInfo info = new ZtrFileHeaderLineInfo();
            //    info.Block = blockIndex;
            //    info.BlockOffset = offset;
            //}

            //int index = 0, blockNumber = 0;
            //using (MemoryStream io = new MemoryStream(32768))
            //{
            //    byte[] readBuff = new byte[4096];
            //    while (compressedSize > 0)
            //    {
            //        int offset = 0;
            //        ZtrFileEncoding tagsEncoding = ZtrFileEncoding.ReadFromStream(_output);
            //        compressedSize = compressedSize - tagsEncoding.BlockSize - 4;
            //        long blockOffset = _output.Position;
            //        while (offset < 4096 && compressedSize > 0)
            //        {
            //            while (index < _offsets.Length && _offsets[index].Block == blockNumber && _output.Position - blockOffset == _offsets[index].PackedOffset)
            //            {
            //                _offsets[index].UnpackedOffset = (int)(io.Position + offset + _offsets[index].BlockOffset);
            //                if (index > 0)
            //                    _offsets[index - 1].UnpackedLength = _offsets[index].UnpackedOffset - _offsets[index - 1].UnpackedOffset;
            //                index++;
            //            }

            //            int value = _output.ReadByte();
            //            byte[] replace = tagsEncoding.Encoding[value];

            //            Array.Copy(replace, 0, readBuff, offset, replace.Length);

            //            offset += replace.Length;
            //            compressedSize--;
            //        }

            //        io.Write(readBuff, 0, offset);
            //        blockNumber++;
            //    }

            //    _offsets[index - 1].UnpackedLength = (int)(io.Position - _offsets[index - 1].UnpackedOffset);
            //    for (int i = 0; i < _offsets.Length; i++)
            //    {
            //        io.SetPosition(_offsets[i].UnpackedOffset);
            //        byte[] buff = io.EnsureRead(_offsets[i].UnpackedLength);
            //        _input[i].Value = _encoding.GetString(buff, 0, buff.Length - 2);
            //    }
            //}
        }
Пример #3
0
        public unsafe void Pack(ZtrFileHeader header)
        {
            _blockOffset = 0;
            _blockNumber = 0;
            _innerIndex  = 0;
            _innerCount  = 0;

            List <int> blockOffsets = new List <int>(256)
            {
                0
            };

            ZtrFileHeaderLineInfo[] lines = new ZtrFileHeaderLineInfo[_input.Length];
            ushort[,] innerOffsets = new ushort[_input.Length, 2];

            ushort writeIndex = 0;

            byte[] writeBuff = new byte[4096];
            byte[] codeBuff  = new byte[64 * 1024];

            fixed(byte *writeBuffPtr = &writeBuff[0])
            fixed(byte *codeBuffPtr = &codeBuff[0])
            {
                for (int e = 0; e < _input.Length; e++)
                {
                    _innerCount++;
                    innerOffsets[e, 0] = writeIndex;

                    ZtrFileEntry entry = _input[e];
                    int          count = _encoding.GetBytes(entry.Value, 0, entry.Value.Length, codeBuff, 0);
                    codeBuff[count++] = 0;
                    codeBuff[count++] = 0;

                    lines[e] = new ZtrFileHeaderLineInfo
                    {
                        Block       = _blockNumber,
                        BlockOffset = 0 // See below: lines[i].BlockOffset = checked ((byte)innerOffsets[i, 1]);
                    };

                    for (int b = 0; b < count; b++)
                    {
                        writeBuffPtr[writeIndex++] = codeBuffPtr[b];
                        if (writeIndex == writeBuff.Length)
                        {
                            WriteBlock(writeBuff, ref writeIndex, innerOffsets, blockOffsets);
                        }
                    }
                }
            }

            if (writeIndex > 0)
            {
                WriteBlock(writeBuff, ref writeIndex, innerOffsets, blockOffsets);
            }

            for (int i = 0; i < innerOffsets.Length / 2; i++)
            {
                lines[i].PackedOffset = innerOffsets[i, 0];
                lines[i].BlockOffset  = checked ((byte)innerOffsets[i, 1]);
            }

            if (blockOffsets.Count > _blockNumber)
            {
                _blockNumber++;
            }

            header.TextBlocksCount = _blockNumber;
            header.TextBlockTable  = blockOffsets.ToArray();
            header.TextLinesTable  = lines;
        }