Example #1
0
        public void TestSetLength()
        {
            var baseStream    = "hello world , my name is miaomiao".ToStream();
            var segmentStream = new SegmentStream(baseStream, 11);

            segmentStream.SetLength(100);
        }
Example #2
0
        public VbcFrame(Stream stream)
        {
            // TODO this crashes on kknd1 intro briefings!
            /*var length = */ stream.ReadUInt32();
            var flags = stream.ReadUInt16();

            if ((flags & 0x0001) != 0)
            {
                globalMotion = new int2(stream.ReadInt16(), stream.ReadInt16());
            }

            if ((flags & 0x0004) != 0)
            {
                Audio = stream.ReadBytes(stream.ReadInt32() - 4);
            }

            if ((flags & 0x0008) != 0)
            {
                var length = stream.ReadUInt32() - 4;
                video            = new SegmentStream(stream, stream.Position, length);
                stream.Position += length;
            }

            if ((flags & 0x0010) != 0)
            {
                var length = stream.ReadUInt32() - 4;
                colors           = new SegmentStream(stream, stream.Position, length);
                stream.Position += length;
            }

            if ((flags & 0x0020) != 0)
            {
                /*duration = */ stream.ReadUInt16();
            }
        }
Example #3
0
        public void TestReadEnd()
        {
            foobarbaz.Seek(4, SeekOrigin.Begin);
            var segment = new SegmentStream(foobarbaz);

            Assert.AreEqual("bar baz", segment.ToText());
        }
Example #4
0
        public void TestSeekSmallThenStart()
        {
            var segment = new SegmentStream(foobarbaz);

            segment.Seek(-999, SeekOrigin.Begin);
            Assert.AreEqual("foo bar baz", segment.ToText());
        }
Example #5
0
        public void TestSeekEndToRead()
        {
            var segment = new SegmentStream(foobarbaz);

            segment.Seek(999, SeekOrigin.End);
            Assert.AreEqual(string.Empty, segment.ToText());
        }
Example #6
0
        public void TestWrite()
        {
            var segment = new SegmentStream(foobarbaz);
            var data    = encoding.GetBytes("foo");

            segment.Write(data, 0, data.Length);
        }
Example #7
0
        public void TestSeekEnd()
        {
            var segment = new SegmentStream(foobarbaz, 7);

            segment.Seek(-3, SeekOrigin.End);
            Assert.AreEqual("bar", segment.ToText());
        }
Example #8
0
        public MobdRenderFlags(SegmentStream stream, Version version)
        {
            /*Type = */ stream.ReadASCII(4);
            var flags = stream.ReadUInt32();

            if (version == Version.KKND2)
            {
                var paletteOffset = stream.ReadUInt32() - stream.BaseOffset;

                var returnPos = stream.Position;
                stream.Position = paletteOffset;
                stream.ReadUInt32();                 // 00 00 00 80
                stream.ReadUInt32();                 // 00 00 00 80
                stream.ReadUInt32();                 // 00 00 00 80
                var numColors = stream.ReadUInt16();
                Palette = new uint[256];

                for (var i = 0; i < numColors; i++)
                {
                    var color16 = stream.ReadUInt16();                     // aRRRRRGGGGGBBBBB
                    var r       = ((color16 & 0x7c00) >> 7) & 0xff;
                    var g       = ((color16 & 0x03e0) >> 2) & 0xff;
                    var b       = ((color16 & 0x001f) << 3) & 0xff;
                    Palette[i] = (uint)((0xff << 24) | (r << 16) | (g << 8) | b);
                }

                stream.Position = returnPos;
            }

            var imageOffset = stream.ReadUInt32() - stream.BaseOffset;

            stream.Position = imageOffset;
            Image           = new MobdImage(stream, flags, version);
        }
Example #9
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]);
        }
Example #10
0
        public void TestRead()
        {
            var baseStream    = "hello world , my name is miaomiao".ToStream();
            var segmentStream = new SegmentStream(baseStream, 11);

            Assert.AreEqual("hello world", segmentStream.ToText());
        }
Example #11
0
        // a bit dirty. only used by WorldClientCollection
        public void Send(SegmentStream stream)
        {
            if (Socket == null || !Connected)
            {
                stream.Dispose();
                return;
            }

            var args = ObjectPoolMgr.ObtainObject <PoolableSocketArgs>();

            try
            {
                args.Completed += OnSendCompleted;
                args.SetBuffer(stream.Segment.Buffer.Array, stream.Segment.Offset, (int)(stream.Position));
                args.UserToken = stream;

                if (!Socket.SendAsync(args))
                {
                    args.Completed -= OnSendCompleted;
                    stream.Dispose();
                    ObjectPoolMgr.ReleaseObject(args);
                }
            }
            catch
            {
                args.Dispose();
                stream.Dispose();
                // args could be disposed if an error occured
                throw;
            }
        }
Example #12
0
        public void TestNotSupportSeekOrigin()
        {
            var baseStream    = "hello world , my name is miaomiao".ToStream();
            var segmentStream = new SegmentStream(baseStream, 11);

            segmentStream.Seek(0, (SeekOrigin)100);
        }
Example #13
0
        public Mobd(SegmentStream stream)
        {
            // This is damn ugly, but it seems MOBD uses offsets from lvl start.
            var fileOffset      = (uint)stream.BaseOffset;
            var firstFrameStart = stream.Length;

            var animationOffsets     = new List <uint>();
            var rotationalAnimations = new List <MobdAnimation>();
            var simpleAnimations     = new List <MobdAnimation>();

            while (stream.Position < firstFrameStart)
            {
                var value = stream.ReadInt32();

                if (value == 0 || (value - fileOffset < stream.Position && value >= fileOffset))
                {
                    stream.Position -= 4;
                    break;
                }

                animationOffsets.Add((uint)(stream.Position - 4));

                while (true)
                {
                    value = stream.ReadInt32();

                    if (value == -1 || value == 0)
                    {
                        break;
                    }

                    firstFrameStart = Math.Min(firstFrameStart, value - fileOffset);
                }
            }

            while (stream.Position < firstFrameStart)
            {
                var value = stream.ReadUInt32();

                if (value == 0)
                {
                    continue;
                }

                animationOffsets.Remove(value - fileOffset);
                var returnPosition = stream.Position;
                stream.Position = value - fileOffset;
                rotationalAnimations.Add(new MobdAnimation(stream));
                stream.Position = returnPosition;
            }

            foreach (var animationOffset in animationOffsets)
            {
                stream.Position = animationOffset;
                simpleAnimations.Add(new MobdAnimation(stream));
            }

            RotationalAnimations = rotationalAnimations.ToArray();
            SimpleAnimations     = simpleAnimations.ToArray();
        }
Example #14
0
        public MobdFrame(SegmentStream stream, Version version)
        {
            OriginX = stream.ReadUInt32();
            OriginY = stream.ReadUInt32();
            /*Unk1 =*/ stream.ReadUInt32();
            var renderFlagsOffset = stream.ReadUInt32();

            /*var boxListOffset =*/ stream.ReadUInt32();             // we do not read boxes (2 points)
            /*Unk2 =*/ stream.ReadUInt32();
            var pointListOffset = stream.ReadUInt32();

            if (pointListOffset > 0)
            {
                var points = new List <MobdPoint>();
                stream.Position = pointListOffset - stream.BaseOffset;

                while (true)
                {
                    var boxId = stream.ReadUInt32();

                    if (boxId == 0xffffffff)
                    {
                        break;
                    }

                    stream.Position -= 4;
                    points.Add(new MobdPoint(stream));
                }

                Points = points.ToArray();
            }

            stream.Position = renderFlagsOffset - stream.BaseOffset;
            RenderFlags     = new MobdRenderFlags(stream, version);
        }
Example #15
0
        public void TestGetLength()
        {
            var baseStream    = "hello world , my name is miaomiao".ToStream();
            var segmentStream = new SegmentStream(baseStream, 11);

            Assert.AreEqual(11, segmentStream.Length);
        }
Example #16
0
        public override IList <ArraySegment <byte> > GetByteSegments(T obj, ByteSegmentLocators.IByteSegmentLocator nextSegmentLocator, int segmentMaxSize)
        {
            var strm = new SegmentStream(size => nextSegmentLocator.GetNextSegment(size > segmentMaxSize ? segmentMaxSize : size));

            _serializer.WriteObject(obj, strm);
            return(strm.GetSegments());
        }
Example #17
0
        public VbcFrame(Stream stream)
        {
            // TODO this crashes on kknd1 intro briefings! This is because in addition to audio and video, they context a text chunk!
            stream.ReadUInt32();             // Length
            var flags = stream.ReadUInt16();

            if ((flags & 0x0001) != 0)
            {
                globalMotion = new int2(stream.ReadInt16(), stream.ReadInt16());
            }

            if ((flags & 0x0004) != 0)
            {
                Audio = stream.ReadBytes(stream.ReadInt32() - 4);
            }

            if ((flags & 0x0008) != 0)
            {
                var length = stream.ReadUInt32() - 4;
                video            = new SegmentStream(stream, stream.Position, length);
                stream.Position += length;
            }

            if ((flags & 0x0010) != 0)
            {
                var length = stream.ReadUInt32() - 4;
                colors           = new SegmentStream(stream, stream.Position, length);
                stream.Position += length;
            }

            if ((flags & 0x0020) != 0)
            {
                stream.ReadUInt16();                 // Duration
            }
        }
Example #18
0
        public void TestWrite()
        {
            var baseStream    = "hello world , my name is miaomiao".ToStream();
            var segmentStream = new SegmentStream(baseStream, 11);
            var buffer        = CatLib.Util.Encoding.GetBytes("hello world");

            segmentStream.Write(buffer, 0, buffer.Length);
        }
Example #19
0
        public void TestSeekSmallThenStart()
        {
            var baseStream    = "hello world , my name is miaomiao".ToStream();
            var segmentStream = new SegmentStream(baseStream, 11);

            segmentStream.Seek(-100, SeekOrigin.Begin);
            Assert.AreEqual("hello world", segmentStream.ToText());
        }
Example #20
0
        public void TestSeekEndToRead()
        {
            var baseStream    = "hello world , my name is miaomiao".ToStream();
            var segmentStream = new SegmentStream(baseStream, 11);

            segmentStream.Seek(100, SeekOrigin.End);
            Assert.AreEqual(string.Empty, segmentStream.ToText());
        }
Example #21
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));
        }
Example #22
0
        public void TestSeekCurrent()
        {
            foobarbaz.Seek(2, SeekOrigin.Begin);
            var segment = new SegmentStream(foobarbaz, 5);

            segment.Seek(2, SeekOrigin.Current);
            Assert.AreEqual("bar", segment.ToText());
        }
Example #23
0
 VocFormat(VocFormat cloneFrom)
 {
     SampleRate   = cloneFrom.SampleRate;
     stream       = SegmentStream.CreateWithoutOwningStream(cloneFrom.stream, 0, (int)cloneFrom.stream.Length);
     blocks       = cloneFrom.blocks;
     totalSamples = cloneFrom.totalSamples;
     Rewind();
 }
Example #24
0
        public Stream GetContent(PackageEntry entry)
        {
            Stream parentStream;
            var    offset = dataStart + entry.Offset + SegmentStream.GetOverallNestedOffset(s, out parentStream);
            var    path   = ((FileStream)parentStream).Name;

            return(new SegmentStream(File.OpenRead(path), offset, entry.Length));
        }
Example #25
0
        public void TestReadBuffer()
        {
            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));
        }
Example #26
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.Util.Encoding.GetString(buffer, 0, 5));
        }
Example #27
0
            public Stream GetStream(string filename)
            {
                if (!index.TryGetValue(filename, out var entry))
                {
                    return(null);
                }

                return(SegmentStream.CreateWithoutOwningStream(stream, entry.Offset, (int)entry.Length));
            }
Example #28
0
        public void TestReadEnd()
        {
            var baseStream = "hello world , my name is miaomiao".ToStream();

            baseStream.Seek(14, SeekOrigin.Begin);
            var segmentStream = new SegmentStream(baseStream);

            Assert.AreEqual("my name is miaomiao", segmentStream.ToText());
        }
Example #29
0
        public void TestSeekEnd()
        {
            var baseStream    = "hello world , my name is miaomiao".ToStream();
            var segmentStream = new SegmentStream(baseStream, 11);

            segmentStream.Seek(-5, SeekOrigin.End);

            Assert.AreEqual("world", segmentStream.ToText());
        }
Example #30
0
        public void TestSeekBegin()
        {
            var baseStream    = "hello world , my name is miaomiao".ToStream();
            var segmentStream = new SegmentStream(baseStream, 11);

            segmentStream.ToText(null, false);
            segmentStream.Seek(0, SeekOrigin.Begin);
            Assert.AreEqual("hello world", segmentStream.ToText());
        }