static AnimationHeader GetAnimationHeader(ByteChunk chunk)
        {
            if (chunk.BytesLeft == 0)
            {
                throw new Exception("Trying to load animation header with no data, chunk size = 0");
            }

            var header = new AnimationHeader();

            header.AnimationType      = chunk.ReadUInt32();
            header.Unknown0_alwaysOne = chunk.ReadUInt32();        // Always 1?
            header.FrameRate          = chunk.ReadSingle();
            var nameLength = chunk.ReadShort();

            header.SkeletonName        = chunk.ReadFixedLength(nameLength);
            header.Unknown1_alwaysZero = chunk.ReadUInt32();        // Always 0? padding?

            if (header.AnimationType == 7)
            {
                header.AnimationTotalPlayTimeInSec = chunk.ReadSingle(); // Play time
            }
            bool isSupportedAnimationFile = header.AnimationType == 5 || header.AnimationType == 6 || header.AnimationType == 7;

            if (!isSupportedAnimationFile)
            {
                throw new Exception($"Unsuported animation format: {header.AnimationType}");
            }

            return(header);
        }
Beispiel #2
0
        // ReSharper disable once UnusedParameter.Local

        private void ValidateLength(ByteChunk chunk)
        {
            if (chunk.Length > MaximumMessageLength)
            {
                throw new ArgumentException("");
            }
        }
        public void BlockCompressorUowShouldWorkAsExpected()
        {
            // Arrange
            var inputDataBytes = new byte[2];
            var inputData      = new ByteChunk {
                Id = 123, Data = inputDataBytes
            };
            var input = MockRepository.GenerateStrictMock <IProducerConsumer <IByteChunk> >();

            input.Expect(t => t.Pop()).Repeat.Once().Return(inputData);
            input.Expect(t => t.Pop()).Repeat.Once().Return(null);

            var compressedBytes = new byte[1];
            var gzip            = MockRepository.GenerateMock <IGzipStream>();

            gzip.Expect(t => t.Compress(inputDataBytes, JobType.Compress)).Repeat.Once().Return(compressedBytes);

            var output = MockRepository.GenerateMock <IProducerConsumer <IByteChunk> >();

            output.Expect(t => t.Push(Arg <IByteChunk> .Matches(d => d.Id == 123 && d.Data == compressedBytes))).Repeat.Once();

            var uow = new BlockCompressorUow(input, output, gzip);

            // Act
            uow.CompressAction(JobType.Compress)();

            // Assert
            input.VerifyAllExpectations();
            output.VerifyAllExpectations();
            gzip.VerifyAllExpectations();
        }
Beispiel #4
0
        public void ShouldInsertPaddingRecordPlusMessageOnBufferWrapWithHeadEqualToTail()
        {
            const int length              = 200;
            var       recordLength        = length + RingBufferDescriptor.HeaderLength;
            var       alignedRecordLength = recordLength.AlignToMultipleOf(RingBufferDescriptor.RecordAlignment);
            var       tail = Capacity - RingBufferDescriptor.HeaderLength;
            var       head = tail;

            _atomicLong.VolatileRead(Head).Returns(head);
            _atomicLong.VolatileRead(Tail).Returns(tail);
            _atomicLong.CompareExchange(Tail, tail + alignedRecordLength + RingBufferDescriptor.RecordAlignment, tail)
            .Returns(tail);

            var chunk = new ByteChunk(null, length);

            Assert.IsTrue(_ringBuffer.Write(MessageTypeId, chunk));

            Received.InOrder(() =>
            {
                // padding first
                _atomicLong.VolatileWrite(new IntPtr(tail),
                                          RingBufferDescriptor.MakeHeader(RingBufferDescriptor.HeaderLength,
                                                                          ManyToOneRingBuffer.PaddingMsgTypeId));

                // message then
                _atomicLong.VolatileWrite(new IntPtr(0), RingBufferDescriptor.MakeHeader(-recordLength, MessageTypeId));
                _buffer.Write(RingBufferDescriptor.EncodedMsgOffset(0), chunk);
                _atomicInt.VolatileWrite(new IntPtr(0), recordLength);
            });
        }
Beispiel #5
0
 public ByteSlice(ByteChunk chunk)
 {
     _chunk = chunk.Pointer;
     _offset = 0;
     Count = chunk.Length;
     _buffer = null;
 }
Beispiel #6
0
        public void ShouldWriteToEmptyBuffer()
        {
            const int length              = 8;
            var       recordLength        = length + RingBufferDescriptor.HeaderLength;
            var       alignedRecordLength = recordLength.AlignToMultipleOf(RingBufferDescriptor.RecordAlignment);

            const int headValue = 0;

            _atomicLong.VolatileRead(Arg.Is(Head)).Returns(headValue);
            const int tailValue = 0;

            _atomicLong.VolatileRead(Arg.Is(Tail)).Returns(tailValue);

            _atomicLong.CompareExchange(Arg.Is(Tail), Arg.Is(tailValue), Arg.Is(tailValue + alignedRecordLength))
            .Returns(tailValue);

            var block = stackalloc byte[100];

            var chunk = new ByteChunk(block, length);

            Assert.IsTrue(_ringBuffer.Write(MessageTypeId, chunk));

            Received.InOrder(() =>
            {
                _atomicLong.VolatileWrite(CurrentSlot, RingBufferDescriptor.MakeHeader(-recordLength, MessageTypeId));
                _buffer.Write(RingBufferDescriptor.EncodedMsgOffset(tailValue), chunk);
                _buffer.GetAtomicInt(tailValue);
                _atomicInt.VolatileWrite(CurrentSlot, recordLength);
            });
        }
Beispiel #7
0
        public void ShouldInsertPaddingRecordPlusMessageOnBufferWrap()
        {
            const int length              = 200;
            var       recordLength        = length + RingBufferDescriptor.HeaderLength;
            var       alignedRecordLength = recordLength.AlignToMultipleOf(RingBufferDescriptor.RecordAlignment);
            long      tail = Capacity - RingBufferDescriptor.HeaderLength;
            var       head = tail - RingBufferDescriptor.RecordAlignment * 4;

            _atomicLong.VolatileRead(Head).Returns(head);
            _atomicLong.Read(Tail).Returns(tail);

            var chunk = new ByteChunk(null, length);

            Assert.True(_ringBuffer.Write(MessageTypeId, chunk));

            Received.InOrder(() =>
            {
                // padding first
                _atomicLong.VolatileWrite(new IntPtr(tail),
                                          RingBufferDescriptor.MakeHeader(RingBufferDescriptor.HeaderLength,
                                                                          ManyToOneRingBuffer.PaddingMsgTypeId));

                // then write from the start
                _buffer.Write(RingBufferDescriptor.EncodedMsgOffset(0), chunk);
                _buffer.GetAtomicLong(0);
                _atomicLong.VolatileWrite(new IntPtr(0), RingBufferDescriptor.MakeHeader(recordLength, MessageTypeId));
            });
        }
Beispiel #8
0
 public ByteSlice(ByteChunk chunk)
 {
     _chunk  = chunk.Pointer;
     _offset = 0;
     Count   = chunk.Length;
     _buffer = null;
 }
Beispiel #9
0
        public unsafe void MessageHandler(int messageTypeId, ByteChunk chunk)
        {
            Assert.AreEqual(MessageId, messageTypeId);
            Assert.AreEqual(ChunkLength, chunk.Length);
            var guid = *(Guid *)chunk.Pointer;

            Received.Add(guid);
        }
Beispiel #10
0
            public UnknownIntArrayTable(ByteChunk data)
            {
                var count = data.ReadInt32();

                Data = new List <int>(count);
                for (int i = 0; i < count; i++)
                {
                    Data.Add(data.ReadInt32());
                }
            }
            public StringArrayTable(ByteChunk data)
            {
                var count = data.ReadInt32();

                Values = new List <string>(count);
                for (int i = 0; i < count; i++)
                {
                    Values.Add(data.ReadString());
                }
            }
 public AnimationBin(string filename, ByteChunk data)
 {
     FileName              = filename;
     TableVersion          = data.ReadInt32();
     RowCount              = data.ReadInt32();
     AnimationTableEntries = new List <AnimationBinEntry>(RowCount);
     for (int i = 0; i < RowCount; i++)
     {
         AnimationTableEntries.Add(new AnimationBinEntry(data));
     }
 }
        List <AnimationDataFile> FindAllSubFiles(ByteChunk data)
        {
            var toalFileCount = data.ReadInt32();
            var fileList      = new List <AnimationDataFile>(toalFileCount);

            for (int i = 0; i < toalFileCount; i++)
            {
                var file = new AnimationDataFile(data);
                fileList.Add(file);
                data.Index += file.Size;
            }
            return(fileList);
        }
        List <AnimationBin> GetAnimationBins(List <AnimationDataFile> animationDataFile, ByteChunk data)
        {
            var output        = new List <AnimationBin>();
            var animationBins = animationDataFile.Where(x => x.Name.Contains("tables.bin")).ToList();

            foreach (var animBin in animationBins)
            {
                var byteChunk = new ByteChunk(data.Buffer, animBin.StartOffset);
                output.Add(new AnimationBin(animBin.Name, byteChunk));
            }

            return(output);
        }
        public AnimationBinEntry(ByteChunk data)
        {
            Name         = data.ReadString();
            SkeletonName = data.ReadString();
            MountName    = data.ReadString();
            var count = data.ReadInt32();

            for (int i = 0; i < count; i++)
            {
                FragmentReferences.Add(new FragmentReference(data));
            }
            Unknown = data.ReadShort();
        }
Beispiel #16
0
        private void AddChunk()
        {
            ByteChunk newChunk = new ByteChunk(ChunkSize);

            if (WriteChunk != null)
            {
                WriteChunk.Next = newChunk;
            }
            else
            {
                ReadChunk = WriteChunk = newChunk;
            }
        }
Beispiel #17
0
        public void MessageHandler(int messageTypeId, ByteChunk chunk)
        {
            var hash   = HashMessageId(messageTypeId);
            var actors = _messageMap[hash];

            for (var i = 0; i < _count; i++)
            {
                // invoke only if the mask matches the actors, to limit the overhead of calling unneeded actors
                if ((GetActorMask(i) & actors) != 0)
                {
                    _readers[i].MessageHandlerImpl(messageTypeId, chunk);
                }
            }
        }
Beispiel #18
0
        public unsafe bool Write <TMessage>(ref Envelope envelope, ref TMessage message, IRingBuffer bufferToWrite)
            where TMessage : struct
        {
            if (typeof(TMessage) != typeof(Guid))
            {
                throw new ArgumentException("Guids only!");
            }

            var guid = (Guid)(object)message;

            var ch1 = new ByteChunk((byte *)&guid, 16);

            return(bufferToWrite.Write(MessageId, ch1));
        }
        public void FileWriterUowShouldWorkAsExpected()
        {
            // Arrange
            const string fileName = "fileName";

            var input1DataBytes = new byte[1];
            var input1Data      = new ByteChunk {
                Id = 0, Data = input1DataBytes
            };
            var input1 = MockRepository.GenerateMock <IProducerConsumer <IByteChunk> >();

            input1.Expect(t => t.Peek()).Repeat.Once().Return(input1Data);
            input1.Expect(t => t.Pop()).Repeat.Once().Return(input1Data);
            var input2DataBytes = new byte[1];
            var input2Data      = new ByteChunk {
                Id = 1, Data = input2DataBytes
            };
            var input2 = MockRepository.GenerateMock <IProducerConsumer <IByteChunk> >();

            input2.Expect(t => t.Peek()).Repeat.Once().Return(input2Data);
            input2.Expect(t => t.Pop()).Repeat.Once().Return(input2Data);

            var finishPc     = MockRepository.GenerateMock <IProducerConsumer <IByteChunk> >();
            var finishPcData = new ByteChunk {
                Id = 2, Data = null
            };

            finishPc.Expect(t => t.Peek()).Repeat.Once().Return(finishPcData);

            var io       = MockRepository.GenerateMock <IIo>();
            var outputMs = new MemoryStream();

            io.Expect(t => t.FileOpenWrite(fileName)).Repeat.Once().Return(outputMs);

            var uow = new FileWriterUow(io,
                                        new List <IProducerConsumer <IByteChunk> > {
                input1, input2
            },
                                        finishPc);

            // Act
            uow.WriteFileAction(fileName)();

            // Assert
            input1.VerifyAllExpectations();
            input2.VerifyAllExpectations();
            io.VerifyAllExpectations();
            finishPc.VerifyAllExpectations();
        }
 public AnimationFragment(string fileName, ByteChunk data = null)
 {
     FileName = fileName;
     if (data != null)
     {
         Skeletons = new StringArrayTable(data);
         MinSlotId = data.ReadInt32();
         MaxSlotId = data.ReadInt32();
         var numFragItems = data.ReadInt32();
         for (int i = 0; i < numFragItems; i++)
         {
             Fragments.Add(new AnimationFragmentEntry(data));
         }
     }
 }
Beispiel #21
0
 public MatchedAnimationTableEntry(ByteChunk data)
 {
     AttackTable         = new UnknownIntArrayTable(data);
     AttackUnknown0      = data.ReadInt32();
     AttackUnknown1      = data.ReadInt32();
     AttackUnknown2      = data.ReadInt32();
     AttackAnimation     = data.ReadString();
     MountAnimation      = data.ReadString();
     DefenceTable        = new UnknownIntArrayTable(data);
     DefenceUnknown0     = data.ReadInt32();
     DefenceUnknown1     = data.ReadInt32();
     DefenceUnknown2     = data.ReadInt32();
     DefenceAnimation    = data.ReadString();
     Unknown_alwaysEmpty = data.ReadString();
 }
Beispiel #22
0
 public void Enqueue(byte b)
 {
     if (WriteChunk == null)
     {
         AddChunk();
     }
     if (WritePointer >= ChunkSize)
     {
         AddChunk();
         WritePointer = 0;
         WriteChunk   = WriteChunk.Next;
     }
     m_Length++;
     WriteChunk.Bytes[WritePointer++] = b;
 }
Beispiel #23
0
        public void AHandlerTests()
        {
            var counter = Substitute.For <IStructSizeCounter>();

            counter.GetSize(typeof(Envelope)).Returns(4);
            var aHandler = new AHandler();
            var reader   = new MessageReader(aHandler, counter, GetABId);
            var bytes    = stackalloc byte[10];
            var chunk    = new ByteChunk(bytes, 10);

            Assert.AreEqual(0, aHandler.Counter);
            reader.MessageHandlerImpl(ATypeId, chunk);
            Assert.AreEqual(1, aHandler.Counter);
            reader.MessageHandlerImpl(BTypeId, chunk);
            Assert.AreEqual(1, aHandler.Counter);
        }
Beispiel #24
0
 public Action CompressAction(JobType jobType)
 {
     return(new Action(() =>
     {
         var inputTask = _input.Pop();
         while (inputTask != null)
         {
             var outputTask = new ByteChunk
             {
                 Id = inputTask.Id,
                 Data = _gzipStream.Compress(inputTask.Data, jobType)
             };
             _output.Push(outputTask);
             inputTask = _input.Pop();
         }
     }));
 }
Beispiel #25
0
        public AnimationFragmentEntry(ByteChunk data)
        {
            _id   = data.ReadInt32();
            _slot = data.ReadInt32();

            Slot = AnimationSlotTypeHelper.GetFromId(_slot);

            AnimationFile     = data.ReadString();
            MetaDataFile      = data.ReadString();
            SoundMetaDataFile = data.ReadString();
            Skeleton          = data.ReadString();
            Blend             = data.ReadSingle();
            Weight            = data.ReadSingle();
            Unknown0          = data.ReadInt32();
            Unknown1          = data.ReadInt32();
            Unknown3          = data.ReadString();
            Unknown4          = data.ReadBool();
        }
Beispiel #26
0
 public byte Dequeue()
 {
     if (ReadPointer >= ChunkSize)
     {
         ReadChunk   = ReadChunk.Next;
         ReadPointer = 0;
         if (ReadChunk == null)
         {
             WriteChunk   = null;
             WritePointer = 0;
         }
     }
     if (m_Length == 0)
     {
         throw new Exception("ByteQueue is empty");
     }
     m_Length--;
     return(ReadChunk.Bytes[ReadPointer++]);
 }
Beispiel #27
0
        public int IndexOf(byte searchByte)
        {
            ByteChunk currentChunk = ReadChunk;
            int       currentPointer = ReadPointer, pos = 0;

            for (int i = m_Length; i > 0; i--, pos++)
            {
                if (currentPointer >= ChunkSize)
                {
                    currentChunk   = currentChunk.Next;
                    currentPointer = 0;
                }
                if (currentChunk.Bytes[currentPointer++] == searchByte)
                {
                    return(pos);
                }
            }
            return(-1);
        }
        private bool TryWriteToSegment(int messagetypeid, ByteChunk chunk, Segment *segment,
                                       int neededBytes)
        {
            var firstAvailablePosition = (long *)segment->Buffer;

            if (*firstAvailablePosition + neededBytes <= _segmentLength)
            {
                var buffer = segment->Buffer + *firstAvailablePosition;
                var header = (Header *)buffer;
                header->MessageTypeId = messagetypeid;
                header->ChunkLength   = chunk.Length;

                Native.MemcpyUnmanaged(buffer + sizeof(Header), chunk.Pointer, chunk.Length);
                *firstAvailablePosition += neededBytes;

                return(true);
            }

            return(false);
        }
Beispiel #29
0
        protected override void Transform()
        {
            while (true && !_cancelled)
            {
                var chunk = _transformationQueue.Dequeue();

                if (chunk == null)
                {
                    break;
                }

                using (MemoryStream stream = new MemoryStream())
                {
                    using (GZipStream gzip = new GZipStream(stream, CompressionMode.Compress))
                    {
                        gzip.Write(chunk.Bytes, 0, chunk.Bytes.Length);
                    }

                    byte[] buffer = stream.ToArray();

                    if (chunk.Bytes.Length + 8 < buffer.Length)
                    {
                        byte[] newBuffer = new byte[chunk.Bytes.Length + 8];

                        BitConverter.GetBytes(newBuffer.Length).CopyTo(newBuffer, 4);

                        chunk.Bytes.CopyTo(newBuffer, _chunkHeaderSize);

                        _writeQueue.Enqueue(new ByteChunk(newBuffer, chunk.ChunkOrder));
                    }
                    else
                    {
                        BitConverter.GetBytes(buffer.Length).CopyTo(buffer, _chunkSizeBytesCount);

                        var result = new ByteChunk(buffer, chunk.ChunkOrder);

                        _writeQueue.Enqueue(result);
                    }
                }
            }
        }
Beispiel #30
0
        public void ShouldRejectWriteWhenInsufficientSpace()
        {
            const int  length = 200;
            const long head   = 0;
            var        tail   = head +
                                (Capacity -
                                 (length - RingBufferDescriptor.RecordAlignment).AlignToMultipleOf(
                                     RingBufferDescriptor.RecordAlignment));

            _atomicLong.VolatileRead(Head).Returns(head);
            _atomicLong.VolatileRead(Tail).Returns(tail);

            var chunk = new ByteChunk(null, length);

            Assert.False(_ringBuffer.Write(MessageTypeId, chunk));

            _atomicInt.ReceivedWithAnyArgs(0);
            _atomicLong.DidNotReceiveWithAnyArgs().CompareExchange(IntPtr.Zero, 0, 0);
            _atomicLong.DidNotReceiveWithAnyArgs().VolatileWrite(IntPtr.Zero, 0);
            _buffer.DidNotReceiveWithAnyArgs().Write(0, new ByteChunk(null, 0));
        }
        bool IRingBuffer.Write(int messagetypeid, ByteChunk chunk)
        {
            var neededBytes = GetNeededBytes(chunk.Length);
            var tail        = _currentHead->Tail;

            // ReSharper disable once ConvertIfStatementToNullCoalescingExpression
            if (tail == null)
            {
                tail = _currentHead;
            }

            if (TryWriteToSegment(messagetypeid, chunk, tail, neededBytes))
            {
                return(true);
            }

            // no write, pop new segment
            tail->Next = PopNew();

            return(TryWriteToSegment(messagetypeid, chunk, tail->Next, neededBytes));
        }