コード例 #1
0
        public void TestReadBufferEnd()
        {
            var segment = new SegmentStream(foobarbaz, 7);
            var actual  = new byte[255];

            Assert.AreEqual(7, segment.Read(actual, 0, 255));
            Assert.AreEqual("foo bar", encoding.GetString(actual, 0, 7));
            Assert.AreEqual(0, segment.Read(actual, 0, 255));
        }
コード例 #2
0
ファイル: SegmentStreamTests.cs プロジェクト: ldvip/Core
        public void TestReadBufferEnd()
        {
            var baseStream    = "hello world , my name is miaomiao".ToStream();
            var segmentStream = new SegmentStream(baseStream, 11);
            var buffer        = new byte[255];

            Assert.AreEqual(11, segmentStream.Read(buffer, 0, 255));
            Assert.AreEqual("hello world", CatLib.Util.Encoding.GetString(buffer, 0, 11));
            Assert.AreEqual(0, segmentStream.Read(buffer, 0, 255));
        }
コード例 #3
0
        public void FullReadTest()
        {
            var pkg1 = new SocketMessage(Regulus.Network.Config.Default.PackageSize + 5);
            var pkg2 = new SocketMessage(Regulus.Network.Config.Default.PackageSize + 4);
            var pkg3 = new SocketMessage(Regulus.Network.Config.Default.PackageSize + 2);

            pkg1.WritePayload(new byte[] { 0, 1, 2, 3, 4 }, 0, 5);
            pkg2.WritePayload(new byte[] { 5, 6, 7, 8 }, 0, 4);
            pkg3.WritePayload(new byte[] { 9, 10 }, 0, 2);

            var stream     = new SegmentStream(new [] { pkg1, pkg2, pkg3 });
            var readBuffer = new byte[15] {
                255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255
            };

            stream.Read(readBuffer, 2, 13);

            Assert.AreEqual(255, readBuffer[0]);
            Assert.AreEqual(255, readBuffer[1]);
            Assert.AreEqual(0, readBuffer[2]);
            Assert.AreEqual(1, readBuffer[3]);
            Assert.AreEqual(2, readBuffer[4]);
            Assert.AreEqual(3, readBuffer[5]);
            Assert.AreEqual(4, readBuffer[6]);
            Assert.AreEqual(5, readBuffer[7]);
            Assert.AreEqual(6, readBuffer[8]);
            Assert.AreEqual(7, readBuffer[9]);
            Assert.AreEqual(8, readBuffer[10]);
            Assert.AreEqual(9, readBuffer[11]);
            Assert.AreEqual(10, readBuffer[12]);
            Assert.AreEqual(255, readBuffer[13]);
            Assert.AreEqual(255, readBuffer[14]);
        }
コード例 #4
0
        public void TestReadBufferMin()
        {
            var segment = new SegmentStream(foobarbaz, 7);
            var buffer  = new byte[3];

            Assert.AreEqual(3, segment.Read(buffer, 0, 3));
            Assert.AreEqual("foo", encoding.GetString(buffer));
        }
コード例 #5
0
        public void TestReadBufferMin()
        {
            var baseStream    = "hello world , my name is miaomiao".ToStream();
            var segmentStream = new SegmentStream(baseStream, 11);
            var buffer        = new byte[5];

            Assert.AreEqual(5, segmentStream.Read(buffer, 0, 5));
            Assert.AreEqual("hello", CatLib.Str.Encoding.GetString(buffer, 0, 5));
        }
コード例 #6
0
        public void BatchesReadTest()
        {
            SocketMessage pkg1 = new SocketMessage(Regulus.Network.Config.Default.PackageSize + 5);
            SocketMessage pkg2 = new SocketMessage(Regulus.Network.Config.Default.PackageSize + 4);
            SocketMessage pkg3 = new SocketMessage(Regulus.Network.Config.Default.PackageSize + 2);

            pkg1.WritePayload(new byte[] { 0, 1, 2, 3, 4 }, 0, 5);
            pkg2.WritePayload(new byte[] { 5, 6, 7, 8 }, 0, 4);
            pkg3.WritePayload(new byte[] { 9, 10 }, 0, 2);

            SegmentStream stream = new SegmentStream(new[] { pkg1, pkg2, pkg3 });

            byte[] readBuffer = new byte[8] {
                255, 255, 255, 255, 255, 255, 255, 255
            };
            stream.Read(readBuffer, 2, 6);

            Assert.Equal(255, readBuffer[0]);
            Assert.Equal(255, readBuffer[1]);
            Assert.Equal(0, readBuffer[2]);
            Assert.Equal(1, readBuffer[3]);
            Assert.Equal(2, readBuffer[4]);
            Assert.Equal(3, readBuffer[5]);
            Assert.Equal(4, readBuffer[6]);
            Assert.Equal(5, readBuffer[7]);

            readBuffer = new byte[8] {
                255, 255, 255, 255, 255, 255, 255, 255
            };
            int readCount = stream.Read(readBuffer, 0, 6);

            Assert.Equal(5, readCount);

            Assert.Equal(6, readBuffer[0]);
            Assert.Equal(7, readBuffer[1]);
            Assert.Equal(8, readBuffer[2]);
            Assert.Equal(9, readBuffer[3]);
            Assert.Equal(10, readBuffer[4]);
            Assert.Equal(255, readBuffer[5]);
            Assert.Equal(255, readBuffer[6]);
            Assert.Equal(255, readBuffer[7]);
        }
コード例 #7
0
ファイル: Mapd.cs プロジェクト: jkalmar/OpenKrush
        public Mapd(SegmentStream stream)
        {
            // This is damn ugly, but it seems MAPD uses offsets from lvl start.
            var basePosition = ((SegmentStream)stream.BaseStream).BaseStream.Position - 8;

            var test = stream.ReadInt32();

            stream.Position += test * 4;
            var generation = stream.ReadInt32() == 256 ? Generation.Gen1 : Generation.Gen2;

            stream.Position -= (test + 2) * 4;

            if (generation == Generation.Gen2)
            {
                stream.ReadInt32(); // Unk
            }
            var layerOffsets = new int[stream.ReadInt32()];

            Layers = new MapdLayer[layerOffsets.Length];

            for (var i = 0; i < layerOffsets.Length; i++)
            {
                layerOffsets[i] = stream.ReadInt32();
            }

            var palette = new byte[stream.ReadInt32() * 4];

            if (generation == Generation.Gen2)
            {
                for (var i = 0; i < palette.Length;)
                {
                    var color16 = stream.ReadUInt16(); // aRRRRRGGGGGBBBBB
                    palette[i++] = (byte)(((color16 & 0x7c00) >> 7) & 0xff);
                    palette[i++] = (byte)(((color16 & 0x03e0) >> 2) & 0xff);
                    palette[i++] = (byte)(((color16 & 0x001f) << 3) & 0xff);
                    palette[i++] = 0xff;
                }
            }
            else
            {
                stream.Read(palette);

                for (var i = 0; i < palette.Length / 4; i++)
                {
                    palette[i * 4 + 3] = 0xff;
                }
            }

            for (var i = 0; i < Layers.Length; i++)
            {
                stream.Position = layerOffsets[i] - basePosition;

                var type = new string(stream.ReadASCII(4).Reverse().ToArray());

                if (type != "SCRL")
                {
                    throw new Exception("Unknown type.");
                }

                var tileWidth  = stream.ReadInt32();
                var tileHeight = stream.ReadInt32();
                var tilesX     = stream.ReadInt32();
                var tilesY     = stream.ReadInt32();

                if (generation == Generation.Gen2)
                {
                    stream.ReadInt32(); // Unk
                    stream.ReadInt32(); // Unk
                    stream.ReadInt32(); // Unk
                }

                var tilePixels = new Dictionary <int, byte[]>();
                var tiles      = new List <int>();

                for (var y = 0; y < tilesY; y++)
                {
                    for (var x = 0; x < tilesX; x++)
                    {
                        var tile = stream.ReadInt32();

                        if (generation == Generation.Gen2)
                        {
                            tile -= tile % 4;
                        }

                        tiles.Add(tile);

                        if (tile != 0 && !tilePixels.ContainsKey(tile))
                        {
                            tilePixels.Add(tile, new byte[tileWidth * tileHeight * 4]);
                        }
                    }
                }

                foreach (var(offset, pixels) in tilePixels)
                {
                    stream.Position = offset - basePosition;

                    if (generation == Generation.Gen1)
                    {
                        stream.ReadInt32(); // Unk
                    }
                    for (var y = 0; y < tileHeight; y++)
                    {
                        for (var x = 0; x < tileWidth; x++)
                        {
                            var index = stream.ReadByte();

                            if (index == 0 && i != 0)
                            {
                                continue;
                            }

                            Array.Copy(palette, index * 4, pixels, (y * tileWidth + x) * 4, 4);
                        }
                    }
                }

                var layer = new MapdLayer(tilesX * tileWidth, tilesY * tileHeight);
                Layers[i] = layer;

                for (var y = 0; y < tilesY; y++)
                {
                    for (var x = 0; x < tilesX; x++)
                    {
                        var tile = tiles[y * tilesX + x];

                        if (tile == 0)
                        {
                            continue;
                        }

                        var pixels = tilePixels[tile];
                        var offset = (y * tileHeight * tilesX + x) * tileWidth;

                        for (var row = 0; row < tileHeight; row++)
                        {
                            Array.Copy(pixels, row * tileWidth * 4, layer.Pixels, (offset + row * layer.Width) * 4, tileWidth * 4);
                        }
                    }
                }
            }
        }
コード例 #8
0
        /// <summary>
        /// Copy the contents of a stored file into an opened stream
        /// </summary>
        /// <param name="entry">Entry information of file to extract</param>
        /// <param name="_stream">Stream to store the uncompressed data</param>
        /// <returns>True if success, false if not.</returns>
        /// <remarks>Unique compression methods are Store and Deflate</remarks>
        public bool ExtractFile(ZipEntry entry, Stream stream)
        {
            if (!stream.CanWrite)
            {
                throw new InvalidOperationException("Stream cannot be written");
            }

            // check signature
            byte[] signature = new byte[4];
            this.zipFileStream.Seek(entry.HeaderOffset, SeekOrigin.Begin);
            this.zipFileStream.Read(signature, 0, 4);
            if (BitConverter.ToUInt32(signature, 0) != 0x04034b50)
            {
                return(false);
            }

            // Prepare streams
            stream.SetLength(entry.FileSize);
            var outStream = new CrcStream(stream);

            this.zipFileStream.Seek(entry.FileOffset, SeekOrigin.Begin);

            // Select input stream for inflating or just reading
            Stream inStream = new SegmentStream(this.zipFileStream, entry.CompressedSize);

            if (entry.Method == Compression.Deflate)
            {
                using (var intPutStream = new Ionic.Zlib.DeflateStream(inStream, Ionic.Zlib.CompressionMode.Decompress))
                {
                    intPutStream.FlushMode = FlushType.Full;
                    int    buffSize = 4096;
                    byte[] buff     = new byte[buffSize];
                    int    size     = 0;
                    do
                    {
                        size = intPutStream.Read(buff, 0, buffSize);
                        if (size > 0)
                        {
                            outStream.Write(buff, 0, size);
                        }
                    } while (size > 0);
                }
                //inStream = new DeflateStream(inStream, CompressionMode.Decompress, true);
            }

            // Decompress
            if (entry.Method == Compression.LZMA)
            {
                var decoder = new SevenZip.Compression.LZMA.Decoder();

                const int PropsLength = 5;
                var       buffer      = new byte[PropsLength];

                inStream.Read(buffer, 0, sizeof(int));
                if (BitConverter.ToInt32(buffer, 0) != 0x00051409)
                {
                    throw new Exception("Invalid LZMA stream signature");
                }

                if (inStream.Read(buffer, 0, PropsLength) != PropsLength)
                {
                    throw new Exception("Invalid LZMA properties length");
                }
                decoder.SetDecoderProperties(buffer);

                decoder.Code(inStream, outStream, entry.CompressedSize, entry.FileSize, null);
            }
            else
            {
                inStream.CopyTo(outStream);
            }

            stream.Flush();

            //if (entry.Method == Compression.Deflate)
            inStream.Dispose();
            if (entry.Crc32 != outStream.WriteCrc)
            {
                throw new Exception("Uncompressed file CRC mismatch");
            }
            return(true);
        }