Esempio n. 1
0
        public void EventStreamerTest()
        {
            Debug.Log($"sizeof {nameof(Data11)} is {UnsafeUtility.SizeOf<Data11>()}");
            Debug.Log($"sizeof {nameof(Data11_Explicit)} is {UnsafeUtility.SizeOf<Data11_Explicit>()}");
            Debug.Log($"sizeof {nameof(OddData)} is {UnsafeUtility.SizeOf<OddData>()}");

            var registry = new EventDataTypes.EventTypeRegistry(Allocator.TempJob);
            var tid      = registry.NextUndefinedTypeID();

            Assert.AreEqual(0, tid);
            tid = registry.NextUndefinedTypeID();
            Assert.AreEqual(0, tid);
            var info = registry.RegisterEventType(tid)
                       .RegisterNextDataType <float>()
                       .RegisterNextDataType <float>()
                       .RegisterNextDataType <float>()
                       .RegisterNextDataType <float>()
                       .RegisterNextDataType <int>()
                       .RegisterNextDataType <int>()
                       .RegisterNextDataType <int>()
                       .RegisterNextDataType <int>();


            var streamer = new EventStreamer(Allocator.TempJob);

            //Write events---------------------------------------------------------------------------------------------------------------------------
            var w        = streamer.AsWriter();
            var evtHdrIn = new EventHeader(0);

            evtHdrIn.SetLocalDataAt <float>(0, 1.5f);
            evtHdrIn.SetLocalDataAt <float>(4, 2.5f);
            evtHdrIn.SetLocalDataAt <float>(8, 3.5f);
            evtHdrIn.SetLocalDataAt <float>(12, 4.5f);

            var handle = w.BeginBatch();

            //event 0
            handle.WriteHeader(evtHdrIn, 16).WriteExternalData <int>(3).WriteExternalData <int>(4).WriteExternalData <int>(5).WriteExternalData <int>(6);


            var buffer = handle.CreateEventBuffer();

            //event 1
            buffer.NewEvent(1)
            .AddData <int>(0).AddData <int>(1).AddData <int>(2).AddData <int>(3).AddData <int>(4).AddData <int>(5)
            .AddData <int>(6).AddData <int>(7).AddData <int>(8).AddData <int>(9).AddData <int>(10).AddData <int>(11)
            .Write();

            //event 2
            buffer.NewEvent(2)
            .AddData <OddData>(new OddData()
            {
                Data0 = 11, Data1 = 24, Data2 = 3
            })
            .AddData <OddData>(new OddData()
            {
                Data0 = 12, Data1 = 25, Data2 = 2
            })
            .AddData <OddData>(new OddData()
            {
                Data0 = 13, Data1 = 26, Data2 = 1
            })
            .Write();

            //event 3
            buffer.NewEvent(3).AddData <int>(0).Write();

            //event 4
            buffer.NewEvent(4).AddData <int>(-1).StartExternalData().AddData <int>(1).AddData <int>(2).AddData <int>(3).Write();

            //event 5
            buffer.NewEvent(5).AddData <SomeEventData>
            (
                new SomeEventData()
            {
                st = new SourceTargetPair()
                {
                    Source = new Entity()
                    {
                        Index = 10, Version = 11
                    },
                    Target = new Entity()
                    {
                        Index = 20, Version = 21
                    },
                },
                Damage     = 99,
                LargeData0 = 50,
                LargeData1 = 50,
                LargeData2 = 50,
                LargeData3 = 50,
            }
            ).Write();


            buffer.Dispose();
            //event 6,7
            handle.WriteEvent(6, 10);
            handle.WriteEvent(7, (ulong)32767, (ulong)65535);

            handle.EndBatch();

            //Read events---------------------------------------------------------------------------------------------------------------------------


            streamer.CollectElementsToRead(out var batch2Read, out var elementCount, Allocator.TempJob).Complete();
            Debug.Log($"Stream has {elementCount.Value} Items in {batch2Read.Length} batches, start Reading");

            var r                 = streamer.AsReader();
            var eventsOut         = new NativeArray <GenericEvent>(elementCount.Value, Allocator.Temp, NativeArrayOptions.UninitializedMemory);
            int totalExternalSize = 0;

            for (int b = 0, lenB = batch2Read.Length; b < lenB; b++)
            {
                var rh = r.BeginBatch(batch2Read[b]);
                for (int i = 0, lenE = rh.ElementCount; i < lenE; i++)
                {
                    var e = rh.ReadEvent();
                    totalExternalSize += e.SizeInfo.ExternalDataByteSize;
                    eventsOut[i]       = e;
                }
                rh.EndBatch();
            }
            elementCount.Dispose();
            batch2Read.Dispose();

            Debug.Log($"Stream has {streamer.CalculateEventCount()} Items in {streamer.CalculateBlockCount()} Blocks, after Reading");


            //Move Data from stream to buffer --------------------------------------------------------------------------------------------------------

            var externalCache = new UnsafeAppendBuffer(totalExternalSize, Unity.Jobs.LowLevel.Unsafe.JobsUtility.CacheLineSize, Allocator.TempJob);
            var checkSum      = 0;

            for (int i = 0, len = eventsOut.Length; i < len; i++)
            {
                var e = eventsOut[i];
                checkSum    += e.MoveExternalDataTo(ref externalCache);
                eventsOut[i] = e;
            }
            Assert.AreEqual(totalExternalSize, checkSum);

            //Release stream  memory ---------------------------------------
            streamer.Dispose();

            int     intData   = default;
            float   floatData = default;
            OddData oddData   = default;

            var evtOut = eventsOut[0];

            LogEventInfo(evtOut, 0);
            Assert.AreEqual(0, evtOut.TypeID);

            Debug.Log((floatData = info.GetData <float>(0, evtOut)).ToString());
            Assert.AreEqual(1.5f, floatData);
            Debug.Log((floatData = info.GetData <float>(1, evtOut)).ToString());
            Assert.AreEqual(2.5f, floatData);
            Debug.Log((floatData = info.GetData <float>(2, evtOut)).ToString());
            Assert.AreEqual(3.5f, floatData);
            Debug.Log((floatData = info.GetData <float>(3, evtOut)).ToString());
            Assert.AreEqual(4.5f, floatData);

            Debug.Log((intData = info.GetData <int>(4, evtOut)).ToString());
            Assert.AreEqual(3, intData);
            Debug.Log((intData = info.GetData <int>(5, evtOut)).ToString());
            Assert.AreEqual(4, intData);
            Debug.Log((intData = info.GetData <int>(6, evtOut)).ToString());
            Assert.AreEqual(5, intData);
            Debug.Log((intData = info.GetData <int>(7, evtOut)).ToString());
            Assert.AreEqual(6, intData);

            evtOut = eventsOut[1];
            LogEventInfo(evtOut, 1);
            Assert.AreEqual(1, evtOut.TypeID);
            for (int i = 0; i < 12; i++)
            {
                Debug.Log((intData = evtOut.GetDataAt <int>(i * 4)).ToString());
                Assert.AreEqual(i, intData);
            }
            Debug.Log((intData = evtOut.LocalDataAt <int>(12)).ToString());
            Assert.AreEqual(3, intData);
            Debug.Log((intData = evtOut.ExternalDataAt <int>(0)).ToString());
            Assert.AreEqual(4, intData);

            evtOut = eventsOut[2];
            LogEventInfo(evtOut, 2);
            Assert.AreEqual(2, evtOut.TypeID);
            var oddSize = UnsafeUtility.SizeOf <OddData>();

            for (int i = 0; i < 3; i++)
            {
                oddData = evtOut.GetDataAt <OddData>(i * oddSize);
                Debug.Log(oddData.Data0.ToString());
            }
            // Debug.Log((oddData = evtOut.LocalDataAt<OddData>(0)).Data0.ToString());
            // Debug.Log((oddData = evtOut.ExternalDataAt<OddData>(0)).Data0.ToString());

            evtOut = eventsOut[3];
            LogEventInfo(evtOut, 3);
            Assert.AreEqual(3, evtOut.TypeID);

            evtOut = eventsOut[4];
            LogEventInfo(evtOut, 4);
            Assert.AreEqual(4, evtOut.TypeID);
            Debug.Log((intData = evtOut.LocalDataAt <int>(0)).ToString());
            Assert.AreEqual(-1, intData);
            Debug.Log((intData = evtOut.ExternalDataAt <int>(0)).ToString());
            Assert.AreEqual(1, intData);
            Debug.Log((intData = evtOut.ExternalDataAt <int>(4)).ToString());
            Assert.AreEqual(2, intData);
            Debug.Log((intData = evtOut.ExternalDataAt <int>(8)).ToString());
            Assert.AreEqual(3, intData);

            evtOut = eventsOut[5];
            LogEventInfo(evtOut, 5);
            var sd = evtOut.ExternalDataAs <SomeEventData>();

            Debug.Log($"some data: source[{sd.st.Source.Entity.Index}:{sd.st.Source.Entity.Version}] target[{sd.st.Target.Entity.Index}:{sd.st.Target.Entity.Version}] Damage{sd.Damage}");
            Debug.Log($"some data: LargeData0[{sd.LargeData0}] LargeData1[{sd.LargeData1}] LargeData2[{sd.LargeData2}] LargeData3[{sd.LargeData3}]");

            evtOut = eventsOut[6];
            LogEventInfo(evtOut, 6);
            Debug.Log($"Local:{evtOut.LocalDataAs<int>()}");
            Assert.AreEqual(10, evtOut.LocalDataAs <int>());


            evtOut = eventsOut[7];
            LogEventInfo(evtOut, 7);
            Debug.Log($"Local:{evtOut.LocalDataAs<ulong>()} External:{evtOut.ExternalDataAs<ulong>()}");
            Assert.AreEqual(32767, evtOut.LocalDataAs <ulong>());
            Assert.AreEqual(65535, evtOut.ExternalDataAs <ulong>());

            // wh.WriteEvent(6, 10);
            // wh.WriteEvent(7, (ulong)32767, (ulong)65535);


            if (streamer.IsCreated)
            {
                streamer.Dispose();
            }
            if (eventsOut.IsCreated)
            {
                eventsOut.Dispose();
            }
            if (externalCache.IsCreated)
            {
                externalCache.Dispose();
            }
            if (registry.IsCreated)
            {
                registry.Dispose();
            }
        }