public void Data()
        {
            var counter = 0;
            var file    = new LogFileWriter("test3.zip", "LogFileWriterTestsData");
            var source  = new MemoryPool("test", MemoryAddress.StaticAbsolute, 0x123456, 0, 0x1234);

            source.Add(new MemoryFieldFunc <int>("testInt", (pool) => counter++)); // these are always logged
            source.Add(new MemoryFieldFunc <string>("test", (pool) => "test"));

            file.Subscribe(source);

            // Fill up  a data file
            for (int i = 0; i < 1441792; i++) // 38.5MiB
            {
                file.Update(i);               // +28 bytes
            }
            // We should have logged 2 data files by now.
            Assert.True(File.Exists("LogFileWriterTestsData/test/Data.bin"));

            file.Save();

            ZipStorer z = ZipStorer.Open("test3.zip", FileAccess.Read);

            var files = z.ReadCentralDir();

            Assert.AreEqual(3, files.Count);
            Assert.True(files.Any(x => x.FilenameInZip == "test/Data.bin"));
            Assert.True(files.Where(x => x.FilenameInZip == "test/Data.bin").FirstOrDefault().FileSize ==
                        1441792 * ((2 + 4 + 4 + 2) + (2 + 4 + 4 + 4 + 2)));
            Assert.True(files.Any(x => x.FilenameInZip == "test/Structure.xml"));
            Assert.True(files.Any(x => x.FilenameInZip == "test/Time.bin"));
            Assert.True(files.Where(x => x.FilenameInZip == "test/Time.bin").FirstOrDefault().FileSize == 1441792 * 8);
        }
Exemple #2
0
        public void DataIsWrittenIn16MBChunks()
        {
            int dataWrites = 0;
            int timeWrites = 0;
            GlobalEvents.Hook<LogFileWriteAction>((x) =>
                                                      {
                                                          Assert.AreEqual(null, x.File);
                                                          Assert.AreEqual("test", x.Group);
                                                          // Count the write actions););
                                                          if (x.FileType == LogFileType.Data)
                                                              dataWrites++;
                                                          if (x.FileType == LogFileType.Time)
                                                              timeWrites++;
                                                      }, false);

            int counter = 0;
            var source = new MemoryPool("test", MemoryAddress.StaticAbsolute, 0x123456, 0, 0x1234);

            source.Add(new MemoryFieldFunc<int>("testInt", (pool) => counter++));
            source.Add(new MemoryFieldFunc<string>("test", (pool) => "test"));

            var group = new LogGroup(null, "test", source);

            // Fill up  a data file
            for (int i = 0; i < 1441792; i++) // 28 * 1441792 = 38.5MiB
                group.Update(i); // +28 bytes

            Assert.AreEqual(38, dataWrites); // 38*1MiB
            Assert.AreEqual(10, timeWrites);

            group.Close();

            Assert.AreEqual(39, dataWrites); // last 0.5MiB
            Assert.AreEqual(11, timeWrites);
        }
        public void DataWithInfrequentFields()
        {
            int i = 0;

            var counter    = 0;
            var counter2   = 0;
            var checkCalls = 0;
            var file       = new LogFileWriter("test5.zip", "LogFileWriterTestsCreateDataWithInfrequentFields");
            var source     = new MemoryPool("test", MemoryAddress.StaticAbsolute, 0x123456, 0, 0x1234);

            source.Add(new MemoryFieldFunc <int>("testInt", (pool) => counter++)); // these are always logged
            source.Add(new MemoryFieldFunc <string>("test", (pool) => "test"));

            var source2 = new MemoryPool("Henk", MemoryAddress.StaticAbsolute, 0x123456, 0, 0x1234);

            source2.Add(new MemoryFieldCustomFunc <int>("testInt", (pool) => counter2++, () => (i % 20 == 0))); // log every 20 samples.
            source2.Add(new MemoryFieldCustomFunc <string>("test", (pool) =>
            {
                byte[] data = new byte[4];
                for (int j = 0; j < 4; j++)
                {
                    data[j] = ((byte)(0x41 + counter2 % 26));
                }
                return(Encoding.ASCII.GetString(data));
            }, () => (i % 20 == 5)));

            file.Subscribe(source);
            file.Subscribe(source2);

            // Fill up next 1/2 of data file
            for (i = 0; i < 224695; i++) // 33MiB
            {
                file.Update(i);          // +24 bytes
            }
            // We should have logged 2 data files by now.
            Assert.True(File.Exists("LogFileWriterTestsCreateDataWithInfrequentFields/test/Data.bin"));
            Assert.False(File.Exists("LogFileWriterTestsCreateDataWithInfrequentFields/Henk/Data.bin"));

            file.Save();

            ZipStorer z = ZipStorer.Open("test5.zip", FileAccess.Read);

            var files = z.ReadCentralDir();

            Assert.AreEqual(6, files.Count);
            Assert.True(files.Any(x => x.FilenameInZip == "test/Data.bin"));
            Assert.True(files.Any(x => x.FilenameInZip == "test/Structure.xml"));
            Assert.True(files.Any(x => x.FilenameInZip == "test/Time.bin"));

            Assert.True(files.Any(x => x.FilenameInZip == "Henk/Data.bin"));
            Assert.True(files.Any(x => x.FilenameInZip == "Henk/Structure.xml"));
            Assert.True(files.Any(x => x.FilenameInZip == "Henk/Time.bin"));
        }
Exemple #4
0
        public void FieldStaticAbsoluteSignature()
        {
            InitTest();

            var plrMemoryPtr = new MemoryFieldSignature <int>("Player", MemoryAddress.StaticAbsolute,
                                                              "A0XXXXXXXX8B0D????????F6D81BC0", new[] { 0 }, 0x6000);

            drvPool.Add(plrMemoryPtr);

            memory.Scanner.Enable();
            memory.Refresh();
            memory.Scanner.Disable();

            Assert.True(plrMemoryPtr.HasChanged());

            int preRescanCount = actionLogbook.Count;

            Assert.Greater(actionLogbook.Count, 1);
            Assert.AreEqual(0x7154C0, actionLogbook[actionLogbook.Count - 3].Address); // pool itself
            Assert.AreEqual(0x6000, actionLogbook[actionLogbook.Count - 3].Size);
            Assert.AreEqual(0x71528C, actionLogbook[actionLogbook.Count - 2].Address); // our sig ptr scan
            Assert.AreEqual(0x4, actionLogbook[actionLogbook.Count - 2].Size);
            Assert.AreEqual(0x7154C0, actionLogbook[actionLogbook.Count - 1].Address); // our sig
            Assert.AreEqual(0x6000, actionLogbook[actionLogbook.Count - 1].Size);

            Assert.AreEqual(0x7154C0, drvPool.ReadAs <int>("Player"));
            Assert.AreEqual(0x7154C0, plrMemoryPtr.Address);

            Assert.AreEqual(actionLogbook.Count, preRescanCount);

            // It will only refresh the contents of the pointer
            memory.Refresh();

            Assert.False(plrMemoryPtr.HasChanged());

            int postRescanCount = actionLogbook.Count;

            Assert.AreEqual(2, postRescanCount - preRescanCount);

            Assert.AreEqual(0x7154C0, actionLogbook[actionLogbook.Count - 5].Address); // pool itself
            Assert.AreEqual(0x6000, actionLogbook[actionLogbook.Count - 5].Size);
            Assert.AreEqual(0x71528C, actionLogbook[actionLogbook.Count - 4].Address); // our sig ptr scan
            Assert.AreEqual(0x4, actionLogbook[actionLogbook.Count - 4].Size);
            Assert.AreEqual(0x7154C0, actionLogbook[actionLogbook.Count - 3].Address); // our sig
            Assert.AreEqual(0x6000, actionLogbook[actionLogbook.Count - 3].Size);

            Assert.AreEqual(0x7154C0, actionLogbook[actionLogbook.Count - 2].Address); // pool itself
            Assert.AreEqual(0x6000, actionLogbook[actionLogbook.Count - 2].Size);
            Assert.AreEqual(0x7154C0, actionLogbook[actionLogbook.Count - 1].Address); // our result from sig
            Assert.AreEqual(0x6000, actionLogbook[actionLogbook.Count - 1].Size);
        }
Exemple #5
0
        void AsyncSendComplete(object?sender, SocketAsyncEventArgs args)
        {
            _socketEventArgsPool.Add(args);

            lock (_asyncCompleteLock)
            {
                int newCount = Interlocked.Decrement(ref _waitingSendCount);
                if (newCount == 0)
                {
                    _onBulkAsyncFinished?.Invoke();
                    _onBulkAsyncFinished = null;
                }
            }
        }
Exemple #6
0
        void AsyncSendComplete(object?sender, SocketAsyncEventArgs args)
        {
            _socketEventArgsPool.Add(args);

            lock (_asyncCompleteLock)
            {
                var token    = (BulkRequestToken)args.UserToken !;
                int newCount = token.DecrementWaitingCount();
                if (newCount == 0)
                {
                    token.Finish();
                }
            }
        }
        public void Groups()
        {
            var file   = new LogFileWriter("test2.zip", "LogFileWriterTestsGroups");
            var source = new MemoryPool("test", MemoryAddress.StaticAbsolute, 0x123456, 0, 0x1234);

            source.Add(new MemoryFieldFunc <int>("testInt", (pool) => 123));
            source.Add(new MemoryFieldFunc <string>("test", (pool) => "test"));

            file.Subscribe(source);

            Assert.AreEqual(1, file.Groups.Count());
            Assert.AreEqual(source.Name, file.Groups.FirstOrDefault().Name);
            Assert.AreEqual(true, file.Groups.FirstOrDefault().Subscribed);
            Assert.AreEqual(2, file.Groups.FirstOrDefault().Fields.Count());
            Assert.AreEqual(true, file.Groups.FirstOrDefault().Fields.Any(x => x.Name == "testInt"));
            Assert.AreEqual(true, file.Groups.FirstOrDefault().Fields.Any(x => x.Name == "test"));

            Assert.AreEqual(source, file.Groups.FirstOrDefault().DataSource);

            file.Subscribe(source);

            Assert.AreEqual(1, file.Groups.Count());

            file.Unsubscribe(source);

            Assert.AreEqual(1, file.Groups.Count()); // but it's become inactive instead;
            Assert.AreEqual(false, file.Groups.FirstOrDefault().Subscribed);


            source.Add(new MemoryFieldFunc <string>("test2", (pool) => "test"));

            file.Subscribe(source);

            Assert.AreEqual(1, file.Groups.Count());
            Assert.AreEqual(true, file.Groups.FirstOrDefault().Subscribed);
            Assert.AreEqual(3, file.Groups.FirstOrDefault().Fields.Count());

            file.Save();

            ZipStorer z = ZipStorer.Open("test2.zip", FileAccess.Read);

            var files = z.ReadCentralDir();

            Assert.AreEqual(3, files.Count);
            Assert.True(files.Any(x => x.FilenameInZip == "test/Data.bin"));
            Assert.True(files.Any(x => x.FilenameInZip == "test/Structure.xml"));
            Assert.True(files.Any(x => x.FilenameInZip == "test/Time.bin"));
        }
        public void DataWithResubscriptions()
        {
            var counter = 0;
            var file    = new LogFileWriter("test4.zip", "LogFileWriterTestsDataWithResubscriptions");
            var source  = new MemoryPool("test", MemoryAddress.StaticAbsolute, 0x123456, 0, 0x1234);

            source.Add(new MemoryFieldFunc <int>("testInt", (pool) => counter++)); // these are always logged
            source.Add(new MemoryFieldFunc <string>("test", (pool) => "test"));

            file.Subscribe(source);

            // Fill up next 1/2 of data file
            int i = 0;

            for (i = 0; i < 1441792 / 2; i++) // 33MiB
            {
                file.Update(i);               // +24 bytes
            }
            file.Unsubscribe(source);

            file.Update(i++); // does nothing now, but the data thing does miss 1 time .

            file.Subscribe(source);

            // Fill up next 1/2 of a data file
            for (; i < 1441792; i++) // 33MiB
            {
                file.Update(i);      // +24 bytes
            }
            Thread.Sleep(500);

            // We should have logged 2 data files by now.
            Assert.True(File.Exists("LogFileWriterTestsDataWithResubscriptions/test/Data.bin"));

            file.Save();

            ZipStorer z = ZipStorer.Open("test4.zip", FileAccess.Read);

            var files = z.ReadCentralDir();

            Assert.AreEqual(3, files.Count);
            Assert.True(files.Any(x => x.FilenameInZip == "test/Data.bin"));
            Assert.True(files.Where(x => x.FilenameInZip == "test/Data.bin").FirstOrDefault().FileSize == (1441792 - 1) * 28);
            Assert.True(files.Any(x => x.FilenameInZip == "test/Structure.xml"));
            Assert.True(files.Any(x => x.FilenameInZip == "test/Time.bin"));
            Assert.True(files.Where(x => x.FilenameInZip == "test/Time.bin").FirstOrDefault().FileSize == 1441792 * 8 - 8);
        }
Exemple #9
0
        public void Create()
        {
            int counter = 0;
            var source  = new MemoryPool("test", MemoryAddress.StaticAbsolute, 0x123456, 0, 0x1234);

            source.Add(new MemoryFieldFunc <int>("testInt", (pool) => counter++));
            source.Add(new MemoryFieldFunc <string>("test", (pool) => "test"));

            var group = new LogGroup(null, "test", source);

            Assert.AreEqual("test", group.Name);
            Assert.AreEqual(2, group.Fields.Count());
            Assert.AreEqual("testInt", group.Fields.FirstOrDefault().Name);
            Assert.AreEqual("test", group.Fields.Skip(1).FirstOrDefault().Name);

            Assert.True(group.Subscribed);
        }
Exemple #10
0
        public void Create()
        {
            int counter = 0;
            var source = new MemoryPool("test", MemoryAddress.StaticAbsolute, 0x123456, 0, 0x1234);

            source.Add(new MemoryFieldFunc<int>("testInt", (pool) => counter++));
            source.Add(new MemoryFieldFunc<string>("test", (pool) => "test"));

            var group = new LogGroup(null, "test", source);

            Assert.AreEqual("test", group.Name);
            Assert.AreEqual(2, group.Fields.Count());
            Assert.AreEqual("testInt", group.Fields.FirstOrDefault().Name);
            Assert.AreEqual("test", group.Fields.Skip(1).FirstOrDefault().Name);

            Assert.True(group.Subscribed);
        }
Exemple #11
0
        public void RecordTestData()
        {
            float[] rpmWave;
            float[] speedWave;
            GetWaves(out rpmWave, out speedWave);
            int index = 0;

            var provider = new MemoryProvider(null);
            var fakePool = new MemoryPool("Driver", MemoryAddress.StaticAbsolute, 0, 0);

            fakePool.Add(new MemoryFieldFunc <float>("Speed", (pool) => speedWave[index], true));
            fakePool.Add(new MemoryFieldFunc <float>("RPM", (pool) => rpmWave[index++], true));
            fakePool.Add(new MemoryFieldConstant <bool>("IsAI", false));
            fakePool.Add(new MemoryFieldConstant <bool>("IsPlayer", true));
            provider.Add(fakePool);

            var fakeDriver  = new TelemetryDriver(fakePool);
            var fakeDrivers = new List <TelemetryDriver>(new[] { fakeDriver });

            TelemetryLogger logger = new TelemetryLogger("testSim", new TelemetryLoggerConfiguration(true, true, true, true));

            logger.SetTemporaryLocations("TelemetryLoggerTests_Tmp.zip", "TelemetryLoggerTests_TmpDir");
            logger.SetDatasource(provider);
            //logger.SetAnnotater( new TelemetryArchive());
            GlobalEvents.Fire(new SessionStarted(), true);
            GlobalEvents.Fire(new DriversAdded(null, fakeDrivers), true);

            for (int i = 0; i < 1024; i++)
            {
                logger.Update(i * 25);
            }

            GlobalEvents.Fire(new SessionStopped(), true);
            Thread.Sleep(2500);

            ZipStorer checkFile = ZipStorer.Open("TelemetryLoggerTests_Tmp.zip", FileAccess.Read);

            var files = checkFile.ReadCentralDir();

            Assert.AreEqual(3, files.Count);
            Assert.AreEqual(1024 * 2 * 12 + 2 * 8, files.Where(x => x.FilenameInZip.Contains("Data.bin")).FirstOrDefault().FileSize);
            Assert.AreEqual(1024 * 8, files.Where(x => x.FilenameInZip.Contains("Time.bin")).FirstOrDefault().FileSize);
        }
Exemple #12
0
        public void DataIsWrittenIn16MBChunks()
        {
            int dataWrites = 0;
            int timeWrites = 0;

            GlobalEvents.Hook <LogFileWriteAction>((x) =>
            {
                Assert.AreEqual(null, x.File);
                Assert.AreEqual("test", x.Group);
                // Count the write actions););
                if (x.FileType == LogFileType.Data)
                {
                    dataWrites++;
                }
                if (x.FileType == LogFileType.Time)
                {
                    timeWrites++;
                }
            }, false);

            int counter = 0;
            var source  = new MemoryPool("test", MemoryAddress.StaticAbsolute, 0x123456, 0, 0x1234);

            source.Add(new MemoryFieldFunc <int>("testInt", (pool) => counter++));
            source.Add(new MemoryFieldFunc <string>("test", (pool) => "test"));

            var group = new LogGroup(null, "test", source);

            // Fill up  a data file
            for (int i = 0; i < 1441792; i++) // 28 * 1441792 = 38.5MiB
            {
                group.Update(i);              // +28 bytes
            }
            Assert.AreEqual(38, dataWrites);  // 38*1MiB
            Assert.AreEqual(10, timeWrites);

            group.Close();

            Assert.AreEqual(39, dataWrites); // last 0.5MiB
            Assert.AreEqual(11, timeWrites);
        }
        private void AdvancedMiner()
        {
            var reader = new MemoryReader();

            reader.Open(ActiveProcess, true);
            miner = new MemoryProvider(reader);

            var scanner = new MemorySignatureScanner(reader);

            scanner.Enable();
            var staticAddr   = scanner.Scan <int>(MemoryRegionType.READWRITE, "75E98B0D????????5F5E");
            var staticOffset = scanner.Scan <byte>(MemoryRegionType.READWRITE, "578B7E??8D04973BF8742F");
            var ptr1Offset   = 0;
            var spdOffset    = scanner.Scan <byte>(MemoryRegionType.READWRITE, "DEC9D947??DECADEC1D955FC");
            var cxOffset     = scanner.Scan <byte>(MemoryRegionType.READWRITE, "F30F5C4E??F30F59C0F30F59");
            var cyOffset     = cxOffset + 4; // scanner.Scan<byte>(MemoryRegionType.READWRITE, "5F8B0A890E8B4A??894EXX8B4AXX894EXX");
            var czOffset     = cxOffset + 8; // scanner.Scan<byte>(MemoryRegionType.READWRITE, "8B4A08??894EXXD9420CD95E0C");

            scanner.Disable();

            var carsPool = new MemoryPool("Cars", MemoryAddress.StaticAbsolute, staticAddr, new int[] { 0, staticOffset }, 64 * 4);

            miner.Add(carsPool);

            for (int k = 0; k < 64; k++)
            {
                var carPool = new MemoryPool("Car " + k, MemoryAddress.Dynamic, carsPool, k * 4, 512);
                carPool.Add(new MemoryField <float>("Speed", MemoryAddress.Dynamic, carPool, spdOffset, 4));
                carPool.Add(new MemoryField <float>("CoordinateX", MemoryAddress.Dynamic, carPool, cxOffset, 4));
                carPool.Add(new MemoryField <float>("CoordinateY", MemoryAddress.Dynamic, carPool, cyOffset, 4));
                carPool.Add(new MemoryField <float>("CoordinateZ", MemoryAddress.Dynamic, carPool, czOffset, 4));

                miner.Add(carPool);

                Cars.Add(new Ets2Car {
                    ID = k
                });
            }
        }
Exemple #14
0
        public void Basic()
        {
            InitTest();

            var testField = new MemoryFieldConstant <bool>("Test", false);

            drvPool.Add(testField);

            memory.Refresh();

            Assert.AreEqual("Test", testField.Name);
            Assert.AreEqual(0, testField.Address);
            Assert.AreEqual(0, testField.Size);
            Assert.AreEqual(0, testField.Offset);

            Assert.True(testField.IsConstant);
            Assert.False(testField.IsDynamic);
            Assert.False(testField.IsStatic);

            Assert.True(testField.HasChanged());
            Assert.False(testField.HasChanged());
        }
Exemple #15
0
        /// <summary>
        /// Allocate a block of memory based upon a best fit policy.
        /// </summary>
        /// <param name="iBlockSizeRequest"></param>
        /// <returns></returns>
        public MemoryBlock Allocate(int iBlockSizeRequest)
        {
            try
            {
                IEnumerable <KeyValuePair <int, List <int> > > availableMemoryBlocks =
                    from entry in MemoryPool
                    where (entry.Key) >= iBlockSizeRequest
                    orderby entry.Key ascending
                    select entry;
                if (0 == availableMemoryBlocks.Count())
                {
                    return(new MemoryBlock());
                }
                KeyValuePair <int, List <int> > smallestBlocks = availableMemoryBlocks.First();
                int iBlockSize    = smallestBlocks.Key;
                int iBlockAddress = smallestBlocks.Value.First();
                smallestBlocks.Value.RemoveAt(0);

                if (0 == smallestBlocks.Value.Count())
                {
                    MemoryPool.Remove(iBlockSize);
                }

                if (iBlockSize > iBlockSizeRequest)
                {
                    int iBlockRemainderSize    = iBlockSize - iBlockSizeRequest;
                    int iBlockRemainderAddress = iBlockAddress + iBlockSizeRequest;
                    if (MemoryPool.ContainsKey(iBlockRemainderSize))
                    {
                        Debug.Assert(!MemoryPool[iBlockRemainderSize].Contains(iBlockRemainderAddress));
                        MemoryPool[iBlockRemainderSize].Add(iBlockRemainderAddress);
                    }
                    else
                    {
                        MemoryPool.Add(iBlockRemainderSize, new List <int>()
                        {
                            iBlockRemainderAddress
                        });
                    }
                }
                return(new MemoryBlock()
                {
                    BlockAddress = iBlockAddress, BlockSize = iBlockSizeRequest
                });
            }
            catch (Exception)
            {
                throw;
            }
        }
Exemple #16
0
        public void CounterField()
        {
            InitTest();

            int iCounter = 0;

            var fieldCounter = new MemoryFieldFunc <int>("Counter", (pool) =>
            {
                iCounter++;
                return(iCounter);
            });

            drvPool.Add(fieldCounter);
            memory.Refresh();

            Assert.AreEqual(1, drvPool.ReadAs <int>("Counter"));
            Assert.AreEqual(2, drvPool.ReadAs <int>("Counter"));
            Assert.AreEqual(3, drvPool.ReadAs <int>("Counter"));
            Assert.AreEqual(4, drvPool.ReadAs <int>("Counter"));
            Assert.AreEqual(5, drvPool.ReadAs <int>("Counter"));
            Assert.AreEqual(6, drvPool.ReadAs <int>("Counter"));
            Assert.AreEqual(7, drvPool.ReadAs <int>("Counter"));
        }
Exemple #17
0
        public void SendCallEnded(ushort groupId, Span <IPEndPoint> endPoints, CachedEncryptionKey keyInfo)
        {
            var buffer = _sendBufferPool.Get();

            // Packet Type
            buffer[0] = (byte)RopuPacketType.CallEnded;
            // Group ID (uint16)
            buffer.WriteUshort(groupId, 1);

            BulkSendEncrypted(buffer.AsSpan(0, 3), groupId, endPoints, keyInfo);

            _sendBufferPool.Add(buffer);
        }
Exemple #18
0
        bool TryAddTransactionToMemoryPool(ByteString id, byte[] data)
        {
            // Data should not exceed the maximum size.
            if (data.Length > MaximumTransactionSize)
            {
                throw new ArgumentException();
            }

            // Integrity check.
            var computedId = Hash.ComputeDoubleSHA256(data);

            if (!ByteString.CopyFrom(computedId).Equals(id))
            {
                throw new ArgumentException();
            }

            if (MemoryPool.ContainsKey(id))
            {
                return(false);
            }

            var tx = BlockchainUtil.DeserializeTransaction(data);

            // Ignore the coinbase transactions.
            if (tx.InEntries.Count == 0)
            {
                return(false);
            }

            lock (MemoryPool)
            {
                if (MemoryPool.ContainsKey(id))
                {
                    return(false);
                }
                MemoryPool.Add(id, tx);
            }

            return(true);
        }
Exemple #19
0
        public void RecordTestData()
        {
            float[] rpmWave;
            float[] speedWave;
            GetWaves(out rpmWave, out speedWave);
            int index = 0;

            var provider = new MemoryProvider(null);
            var fakePool = new MemoryPool("Driver", MemoryAddress.StaticAbsolute, 0, 0);
            fakePool.Add(new MemoryFieldFunc<float>("Speed", (pool) => speedWave[index], true));
            fakePool.Add(new MemoryFieldFunc<float>("RPM", (pool) => rpmWave[index++], true));
            fakePool.Add(new MemoryFieldConstant<bool>("IsAI", false));
            fakePool.Add(new MemoryFieldConstant<bool>("IsPlayer", true));
            provider.Add(fakePool);

            var fakeDriver = new TelemetryDriver(fakePool);
            var fakeDrivers = new List<TelemetryDriver>(new[] {fakeDriver});

            TelemetryLogger logger = new TelemetryLogger("testSim", new TelemetryLoggerConfiguration(true, true, true, true));
            logger.SetDatasource(provider);
            logger.SetAnnotater( new TelemetryArchive());
            GlobalEvents.Fire(new SessionStarted(), true);
            GlobalEvents.Fire(new DriversAdded(null, fakeDrivers), true);

            for(int i = 0 ; i  <1024;i++)
                logger.Update(i*25);

            GlobalEvents.Fire(new SessionStopped(), true);
            Thread.Sleep(500);

            ZipStorer checkFile = ZipStorer.Open("tmp.zip", FileAccess.Read);

            var files = checkFile.ReadCentralDir();
            Assert.AreEqual(3, files.Count);
            Assert.AreEqual(1024 * 2 * 12 + 2 * 8, files.Where(x => x.FilenameInZip.Contains("Data.bin")).FirstOrDefault().FileSize);
            Assert.AreEqual(1024*8, files.Where(x => x.FilenameInZip.Contains("Time.bin")).FirstOrDefault().FileSize);
        }
Exemple #20
0
        async Task HandleBody(InventoryMessage message, int peerId)
        {
            // Data should not exceed the maximum size.
            var data = message.Data;

            if (data.Length > MaximumBlockSize)
            {
                throw new ArgumentException();
            }

            // ハッシュ値正しい?
            var id = message.IsBlock ?
                     BlockchainUtil.ComputeBlockId(data) :
                     Hash.ComputeDoubleSHA256(data);

            if (!ByteString.CopyFrom(id).Equals(message.ObjectId))
            {
                return;
            }

            if (message.IsBlock)
            {
                // ミューテックス
                lock (Blocks)
                {
                    if (Blocks.ContainsKey(message.ObjectId))
                    {
                        return;
                    }
                    // ハッシュテーブルに追加
                    Blocks.Add(message.ObjectId, data);
                }

                var block = BlockchainUtil.DeserializeBlock(data);
                // 前のブロックも知らなかったら前のももらう
                var prevId = block.PreviousHash;
                if (!Blocks.ContainsKey(prevId))
                {
                    await ConnectionManager.SendAsync(new InventoryMessage
                    {
                        Type     = Request,
                        IsBlock  = true,
                        ObjectId = prevId,
                    }, peerId);
                }
                Executor.ProcessBlock(block);
            }
            else
            {
                if (MemoryPool.ContainsKey(message.ObjectId))
                {
                    return;
                }

                var tx = BlockchainUtil.DeserializeTransaction(data);

                // Ignore the coinbase transactions.
                if (tx.InEntries.Count == 0)
                {
                    return;
                }

                lock (MemoryPool)
                {
                    if (MemoryPool.ContainsKey(message.ObjectId))
                    {
                        return;
                    }
                    MemoryPool.Add(message.ObjectId, tx);
                }
            }

            message.Type = Advertise;
            message.Data = null;
            // 他の人に教えてあげる
            await ConnectionManager.BroadcastAsync(message, peerId);
        }
Exemple #21
0
        public void LapTests()
        {
            var lapsRecorded = new List <Lap>();

            GlobalEvents.Hook <TelemetryLapComplete>(lap => lapsRecorded.Add(lap.Lap), true);

            float[] rpmWave;
            float[] speedWave;
            GetWaves(out rpmWave, out speedWave);
            int index = 0;

            var provider = new MemoryProvider(null);
            var fakePool = new MemoryPool("Driver", MemoryAddress.StaticAbsolute, 0, 0);

            fakePool.Add(new MemoryFieldFunc <float>("Speed", (pool) => speedWave[index], true));
            fakePool.Add(new MemoryFieldFunc <float>("RPM", (pool) => rpmWave[index++], true));
            fakePool.Add(new MemoryFieldFunc <float>("Laps", (pool) =>
            {
                if (index <= 100)
                {
                    return(0);
                }
                if (index > 100 && index <= 225)
                {
                    return(1);
                }
                if (index > 225 && index <= 325)
                {
                    return(2);
                }
                if (index > 325 && index <= 500)
                {
                    return(3);
                }
                if (index > 500 && index <= 575)
                {
                    return(4);
                }
                if (index > 575 && index <= 1024)
                {
                    return(5);
                }

                return(-1);
            }, true));
            fakePool.Add(new MemoryFieldConstant <bool>("IsAI", false));
            fakePool.Add(new MemoryFieldConstant <bool>("IsPlayer", true));
            provider.Add(fakePool);

            var fakeDriver  = new TelemetryDriver(fakePool);
            var fakeDrivers = new List <TelemetryDriver>(new[] { fakeDriver });

            var logger = new TelemetryLogger("testSim", new TelemetryLoggerConfiguration(true, true, true, true));

            logger.SetDatasource(provider);
            //logger.SetAnnotater(new TelemetryArchive());
            GlobalEvents.Fire(new SessionStarted(), true);
            GlobalEvents.Fire(new DriversAdded(null, fakeDrivers), true);

            for (int i = 0; i < 1024; i++)
            {
                logger.Update(i * 25);
            }

            GlobalEvents.Fire(new SessionStopped(), true);
            Thread.Sleep(500);

            Assert.AreEqual(6, lapsRecorded.Count);
            Assert.AreEqual(0, lapsRecorded[0].LapNumber);
            Assert.AreEqual(1, lapsRecorded[1].LapNumber);
            Assert.AreEqual(2, lapsRecorded[2].LapNumber);
            Assert.AreEqual(3, lapsRecorded[3].LapNumber);
            Assert.AreEqual(4, lapsRecorded[4].LapNumber);
            Assert.AreEqual(5, lapsRecorded[5].LapNumber);

            Assert.AreEqual(100, lapsRecorded[0].Total);
            Assert.AreEqual(125, lapsRecorded[1].Total);
            Assert.AreEqual(100, lapsRecorded[2].Total);
            Assert.AreEqual(175, lapsRecorded[3].Total);
            Assert.AreEqual(75, lapsRecorded[4].Total);
            Assert.AreEqual(-1, lapsRecorded[5].Total);
        }
        public void LapTests()
        {
            var lapsRecorded = new List<Lap>();

            GlobalEvents.Hook<TelemetryLapComplete>(lap => lapsRecorded.Add(lap.Lap),true);

            float[] rpmWave;
            float[] speedWave;
            GetWaves(out rpmWave, out speedWave);
            int index = 0;

            var provider = new MemoryProvider(null);
            var fakePool = new MemoryPool("Driver", MemoryAddress.StaticAbsolute, 0, 0);
            fakePool.Add(new MemoryFieldFunc<float>("Speed", (pool) => speedWave[index], true));
            fakePool.Add(new MemoryFieldFunc<float>("RPM", (pool) => rpmWave[index++], true));
            fakePool.Add(new MemoryFieldFunc<float>("Laps", (pool) =>
                                                                {
                                                                    if(index <= 100)
                                                                        return 0;
                                                                    if (index > 100 && index <= 225)
                                                                        return 1;
                                                                    if (index > 225 && index <= 325)
                                                                        return 2;
                                                                    if (index > 325 && index <= 500)
                                                                        return 3;
                                                                    if (index > 500 && index <= 575)
                                                                        return 4;
                                                                    if (index > 575 && index <= 1024)
                                                                        return 5;

                                                                        return -1;
                                                                }, true));
            fakePool.Add(new MemoryFieldConstant<bool>("IsAI", false));
            fakePool.Add(new MemoryFieldConstant<bool>("IsPlayer", true));
            provider.Add(fakePool);

            var fakeDriver = new TelemetryDriver(fakePool);
            var fakeDrivers = new List<TelemetryDriver>(new[] { fakeDriver });

            var logger = new TelemetryLogger("testSim", new TelemetryLoggerConfiguration(true, true, true, true));
            logger.SetDatasource(provider);
            //logger.SetAnnotater(new TelemetryArchive());
            GlobalEvents.Fire(new SessionStarted(), true);
            GlobalEvents.Fire(new DriversAdded(null, fakeDrivers), true);

            for (int i = 0; i < 1024; i++)
                logger.Update(i * 25);

            GlobalEvents.Fire(new SessionStopped(), true);
            Thread.Sleep(500);

            Assert.AreEqual(6, lapsRecorded.Count);
            Assert.AreEqual(0, lapsRecorded[0].LapNumber);
            Assert.AreEqual(1, lapsRecorded[1].LapNumber);
            Assert.AreEqual(2, lapsRecorded[2].LapNumber);
            Assert.AreEqual(3, lapsRecorded[3].LapNumber);
            Assert.AreEqual(4, lapsRecorded[4].LapNumber);
            Assert.AreEqual(5, lapsRecorded[5].LapNumber);

            Assert.AreEqual(100, lapsRecorded[0].Total);
            Assert.AreEqual(125, lapsRecorded[1].Total);
            Assert.AreEqual(100, lapsRecorded[2].Total);
            Assert.AreEqual(175, lapsRecorded[3].Total);
            Assert.AreEqual(75, lapsRecorded[4].Total);
            Assert.AreEqual(-1, lapsRecorded[5].Total);
        }
        async ValueTask <bool> AwaitRequest <H>(
            ushort requestId, H handler, byte[] buffer, IPEndPoint endPoint,
            ManualResetEvent manualResetEvent, int length) where H : class
        {
            var userId  = UserId;
            var keyInfo = _keysClient.GetMyKeyInfo();

            if (userId == null)
            {
                Console.Error.WriteLine($"Could not send packet because userId is null");
                return(false);
            }
            if (keyInfo == null)
            {
                Console.Error.WriteLine($"Could not send packet because keyInfo is null");
                return(false);
            }

            _requests[requestId] = handler;

            var packet = _sendBufferPool.Get();

            try
            {
                int packetLength = _packetEncryption.CreateEncryptedPacket(buffer.AsSpan(0, length), packet, false, userId.Value, keyInfo);

                _socket.SendTo(packet, 0, packetLength, SocketFlags.None, endPoint);
                bool acknowledged = await AwaitResetEvent(manualResetEvent);

                _sendBufferPool.Add(packet);

                _requests[requestId] = null;
                return(acknowledged);
            }
            catch (Exception exception)
            {
                Console.Error.WriteLine(exception);
                throw exception;
            }
        }
Exemple #24
0
        /// <summary>
        /// Free memory back into memory buffer.
        /// </summary>
        /// <param name="m"></param>
        public void Free(MemoryBlock m)
        {
            if (null == m || m.IsNull())
            {
                return;
            }
            try
            {
                int iBlockAddressStart = m.BlockAddress;
                int iBlockAddressEnd   = m.BlockAddress + m.BlockSize;
                int iMergeBlockSize    = m.BlockSize;
                int iMergeBlockAddress = m.BlockAddress;
                // If Block Address is already available, then ignore
                foreach (KeyValuePair <int, List <int> > kvp in MemoryPool)
                {
                    if (kvp.Value.Contains(m.BlockAddress))
                    {
                        return;
                    }
                }
                // Merge with existing block with start of address
                bool fMergeStart             = false;
                int  iMergeStartBlockSize    = 0;
                int  iMergeStartBlockAddress = 0;
                foreach (KeyValuePair <int, List <int> > kvp in MemoryPool)
                {
                    int iBlockSize = kvp.Key;
                    foreach (int iBlockAddress in kvp.Value)
                    {
                        if (iBlockAddress > iBlockAddressStart)
                        {
                            continue;
                        }
                        if (iBlockSize + iBlockAddress == iBlockAddressStart)
                        {
                            fMergeStart             = true;
                            iMergeStartBlockSize    = iBlockSize;
                            iMergeStartBlockAddress = iBlockAddress;
                            MemoryPool[iBlockSize].Remove(iBlockAddress);
                            if (0 == MemoryPool[iBlockSize].Count())
                            {
                                MemoryPool.Remove(iBlockSize);
                            }
                            break;
                        }
                    }
                    if (fMergeStart)
                    {
                        break;
                    }
                }

                // Merge existing block with the end of the block
                if (fMergeStart)
                {
                    iMergeBlockSize   += iMergeStartBlockSize;
                    iMergeBlockAddress = iMergeStartBlockAddress;
                }

                bool fMergeEnd             = false;
                int  iMergeEndBlockSize    = 0;
                int  iMergeEndBlockAddress = 0;
                IEnumerable <KeyValuePair <int, List <int> > > availableMemoryBlocksWithAddressEnd =
                    from entry in MemoryPool
                    where (entry.Value.Contains(iBlockAddressEnd))
                    select entry;

                if (0 < availableMemoryBlocksWithAddressEnd.Count())
                {
                    fMergeEnd = true;
                    KeyValuePair <int, List <int> > kvp = availableMemoryBlocksWithAddressEnd.First();
                    iMergeEndBlockSize    = kvp.Key;
                    iMergeEndBlockAddress = iBlockAddressEnd;
                    Debug.Assert(MemoryPool[iMergeEndBlockSize].Contains(iMergeEndBlockAddress));
                    MemoryPool[iMergeEndBlockSize].Remove(iMergeEndBlockAddress);
                    if (0 == MemoryPool[iMergeEndBlockSize].Count)
                    {
                        MemoryPool.Remove(iMergeEndBlockSize);
                    }
                }

                if (fMergeEnd)
                {
                    iMergeBlockSize += iMergeEndBlockSize;
                }

                if (MemoryPool.Keys.Contains(iMergeBlockSize))
                {
                    MemoryPool[iMergeBlockSize].Add(iMergeBlockAddress);
                }
                else
                {
                    MemoryPool.Add(iMergeBlockSize, new List <int>()
                    {
                        iMergeBlockAddress
                    });
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
        public void FieldDynamic()
        {
            InitTest();

            var FieldFuel  = new MemoryField <float>("FuelCapacity", MemoryAddress.Dynamic, 0, 0x3160, 4);
            var FieldIndex = new MemoryField <int>("Index", MemoryAddress.Dynamic, 0, 0x8, 4); // +0x8 = driver index

            drvPool.Add(FieldIndex);
            drvPool.Add(FieldFuel);

            memory.Refresh();

            Assert.True(FieldIndex.HasChanged());
            Assert.True(FieldFuel.HasChanged());

            // only 1 read from pool:
            Assert.AreEqual(1, actionLogbook.Count);
            Assert.AreEqual(0x7154C0, actionLogbook[0].Address);
            Assert.AreEqual(0x6000, actionLogbook[0].Size);

            // And read some values in different types:
            Assert.AreEqual(0, drvPool.ReadAs <int>("Index"));
            Assert.AreEqual(0, drvPool.ReadAs <uint>("Index"));
            Assert.AreEqual(0.0f, drvPool.ReadAs <float>("Index"));

            Assert.AreEqual(100.0f, drvPool.ReadAs <float>("FuelCapacity"));
            Assert.AreEqual(100, drvPool.ReadAs <int>("FuelCapacity"));
            Assert.AreEqual(100.0, drvPool.ReadAs <double>("FuelCapacity"));

            memory.Refresh();

            Assert.False(FieldIndex.HasChanged());
            Assert.False(FieldFuel.HasChanged());

            Assert.AreEqual(2, actionLogbook.Count);
            Assert.AreEqual(0x7154C0, actionLogbook[0].Address);
            Assert.AreEqual(0x6000, actionLogbook[0].Size);
            Assert.AreEqual(0x7154C0, actionLogbook[1].Address);
            Assert.AreEqual(0x6000, actionLogbook[1].Size);
        }
        public void CreateDriver(MemoryPool pool, bool isPlayer)
        {
            if (isPlayer)
            {
                //pool.Add(new MemoryFieldLazy<float>("", MemoryAddress.Static, 0, 4));
                // I can now add tyre temperatures, pressure, brake info etc.
                pool.Add(new MemoryFieldConstant<bool>("IsPlayer", true));
                pool.Add(new MemoryFieldLazy<double>("EngineLifetime", MemoryAddress.Static, 0x006DC0AC, 8));
                pool.Add(new MemoryFieldLazy<double>("EngineOil", MemoryAddress.Static, 0x006DC044, 8, Conversions.Kelvin2Celsius));
                pool.Add(new MemoryFieldLazy<double>("EngineWater", MemoryAddress.Static, 0x006DC084, 8, Conversions.Kelvin2Celsius));
                pool.Add(new MemoryFieldLazy<byte>("EngineMode", MemoryAddress.Static, 0x006DBF70, 1));
                pool.Add(new MemoryFieldLazy<byte>("EngineTorque", MemoryAddress.Static, 0x006DC224, 1));

                pool.Add(new MemoryFieldLazy<int>("FuelStop1", MemoryAddress.Static, 0x006E1EAC, 4));
                pool.Add(new MemoryFieldLazy<int>("FuelStop1", MemoryAddress.Static, 0x006E1EEC, 4));
                pool.Add(new MemoryFieldLazy<int>("FuelStop1", MemoryAddress.Static, 0x006E1F2C, 4));

                pool.Add(new MemoryFieldLazy<double>("RideheightLF", MemoryAddress.Static, 0x006DB778, 8));
                pool.Add(new MemoryFieldLazy<double>("RideheightRF", MemoryAddress.Static, 0x006DB780, 8));
                pool.Add(new MemoryFieldLazy<double>("RideheightLR", MemoryAddress.Static, 0x006DB788, 8));
                pool.Add(new MemoryFieldLazy<double>("RideheightRR", MemoryAddress.Static, 0x006DB790, 8));

                pool.Add(new MemoryFieldLazy<string>("TyreCompoundFront", MemoryAddress.Static, 0x006E1177, 16));
                pool.Add(new MemoryFieldLazy<string>("TyreCompoundRear", MemoryAddress.Static, 0x006E11B7, 16));

                pool.Add(new MemoryFieldLazy<double>("TyrePressureLF", MemoryAddress.Static, 0x006D9F5C, 8));
                pool.Add(new MemoryFieldLazy<double>("TyrePressureRF", MemoryAddress.Static, 0x006DA55C, 8));
                pool.Add(new MemoryFieldLazy<double>("TyrePressureLR", MemoryAddress.Static, 0x006DAB5C, 8));
                pool.Add(new MemoryFieldLazy<double>("TyrePressureRR", MemoryAddress.Static, 0x006DB15C, 8));

                pool.Add(new MemoryFieldLazy<double>("TyreSpeedLF", MemoryAddress.Static, 0x006D9C04, 8));
                pool.Add(new MemoryFieldLazy<double>("TyreSpeedRF", MemoryAddress.Static, 0x006DA204, 8));
                pool.Add(new MemoryFieldLazy<double>("TyreSpeedLR", MemoryAddress.Static, 0x006DA804, 8));
                pool.Add(new MemoryFieldLazy<double>("TyreSpeedRR", MemoryAddress.Static, 0x006DAE04, 8));

                pool.Add(new MemoryFieldLazy<double>("TyreTemperatureInsideLF", MemoryAddress.Static, 0x006D9F44, 8));
                pool.Add(new MemoryFieldLazy<double>("TyreTemperatureInsideRF", MemoryAddress.Static, 0x006DA534, 8));
                pool.Add(new MemoryFieldLazy<double>("TyreTemperatureInsideLR", MemoryAddress.Static, 0x006DAB44, 8));
                pool.Add(new MemoryFieldLazy<double>("TyreTemperatureInsideRR", MemoryAddress.Static, 0x006DB134, 8));

                pool.Add(new MemoryFieldLazy<double>("TyreTemperatureMiddleLF", MemoryAddress.Static, 0x006D9F3C, 8));
                pool.Add(new MemoryFieldLazy<double>("TyreTemperatureMiddleRF", MemoryAddress.Static, 0x006DA53C, 8));
                pool.Add(new MemoryFieldLazy<double>("TyreTemperatureMiddleLR", MemoryAddress.Static, 0x006DAB3C, 8));
                pool.Add(new MemoryFieldLazy<double>("TyreTemperatureMiddleRR", MemoryAddress.Static, 0x006DB13C, 8));

                pool.Add(new MemoryFieldLazy<double>("TyreTemperatureOutsideLF", MemoryAddress.Static, 0x006D9F34, 8));
                pool.Add(new MemoryFieldLazy<double>("TyreTemperatureOutsideRF", MemoryAddress.Static, 0x006DA544, 8));
                pool.Add(new MemoryFieldLazy<double>("TyreTemperatureOutsideLR", MemoryAddress.Static, 0x006DAB34, 8));
                pool.Add(new MemoryFieldLazy<double>("TyreTemperatureOutsideRR", MemoryAddress.Static, 0x006DB144, 8));

                pool.Add(new MemoryFieldLazy<int>("AerodyanmicFrontwingSetting", MemoryAddress.Static, 0x006E182C, 4));
                pool.Add(new MemoryFieldLazy<int>("AerodyanmicRearwingSetting", MemoryAddress.Static, 0x006E186C, 4));
                pool.Add(new MemoryFieldLazy<int>("AerodyanmicRadiatorSetting", MemoryAddress.Static, 0x006E18AC, 4));
                pool.Add(new MemoryFieldLazy<int>("AerodyanmicBrakeductSetting", MemoryAddress.Static, 0x006E18EC, 4));

                pool.Add(new MemoryFieldLazy<double>("BrakeTemperatureLF", MemoryAddress.Static, 0x006DA0E0, 8));
                pool.Add(new MemoryFieldLazy<double>("BrakeTemperatureRF", MemoryAddress.Static, 0x006DA6E0, 8));
                pool.Add(new MemoryFieldLazy<double>("BrakeTemperatureLR", MemoryAddress.Static, 0x006DACE0, 8));
                pool.Add(new MemoryFieldLazy<double>("BrakeTemperatureRR", MemoryAddress.Static, 0x006DB2E0, 8));

                pool.Add(new MemoryFieldLazy<double>("BrakeThicknessLF", MemoryAddress.Static, 0x006DA110, 8));
                pool.Add(new MemoryFieldLazy<double>("BrakeThicknessRF", MemoryAddress.Static, 0x006DA710, 8));
                pool.Add(new MemoryFieldLazy<double>("BrakeThicknessLR", MemoryAddress.Static, 0x006DAD10, 8));
                pool.Add(new MemoryFieldLazy<double>("BrakeThicknessRR", MemoryAddress.Static, 0x006DB310, 8));

                pool.Add(new MemoryFieldLazy<double>("InputClutch", MemoryAddress.Static, 0x006D9744, 8));
                pool.Add(new MemoryFieldLazy<double>("InputSteering", MemoryAddress.Static, 0x006D972C, 8));

                pool.Add(new MemoryFieldLazy<byte>("HelpsABS", MemoryAddress.Static, 0x006D9786, 1));
                pool.Add(new MemoryFieldLazy<byte>("HelpsTC", MemoryAddress.Static, 0x006D977E, 1));
                pool.Add(new MemoryFieldLazy<byte>("HelpsSteer", MemoryAddress.Static, 0x006D9785, 1));

                pool.Add(new MemoryFieldLazy<bool>("HelpsLock", MemoryAddress.Static, 0x006D9784, 1));
                pool.Add(new MemoryFieldLazy<bool>("HelpsSpin", MemoryAddress.Static, 0x006D9787, 1));
                pool.Add(new MemoryFieldLazy<bool>("HelpsStability", MemoryAddress.Static, 0x006D9780, 1));
                pool.Add(new MemoryFieldLazy<bool>("HelpsClutch", MemoryAddress.Static, 0x006D9785, 1));
                //pool.Add(new MemoryFieldLazy<byte>("HelpsShift", MemoryAddress.Static, 0x006D9782, 1));

            }
            else
                pool.Add(new MemoryFieldConstant<bool>("IsPlayer", false));
        }
        public void Initialize(MemoryProvider provider)
        {
            MemoryPool simulator = new MemoryPool("Simulator", MemoryAddress.Static, 0, 0);
            simulator.Add(new MemoryFieldLazy<int>("CarPlayer", MemoryAddress.Static, 0x31528c, 4));
            simulator.Add(new MemoryFieldLazy<int[]>("Drivers", MemoryAddress.Static, 0x315298, 0x19C));

            simulator.Add(new MemoryFieldLazy<string>("LocationGame", MemoryAddress.Static, 0x6EB320, 0, 256));

            MemoryPool session = new MemoryPool("Session", MemoryAddress.Static, 0, 0);
            session.Add(new MemoryFieldLazy<int>("Cars", MemoryAddress.Static, 0x315290, 4));
            session.Add(new MemoryFieldLazy<float>("Time", MemoryAddress.Static, 0x60022C, 4));
            session.Add(new MemoryFieldLazy<float>("Clock", MemoryAddress.Static, 0x6E2CD8, 4));

            session.Add(new MemoryFieldLazy<string>("LocationTrack", MemoryAddress.Static, 0x309D28, 0, 256));

            session.Add(new MemoryFieldLazy<bool>("IsOffline", MemoryAddress.Static, 0x315444, 1, (x) => !x));
            session.Add(new MemoryFieldLazy<bool>("IsActive", MemoryAddress.Static, 0x30FEE4, 1));
            session.Add(new MemoryFieldLazy<bool>("IsReplay", MemoryAddress.Static, 0x315444, 1));
            session.Add(new MemoryFieldFunc<bool>("IsLoading", (pool) => !pool.ReadAs<bool>("IsActive") && pool.ReadAs<int>("Cars") > 0 && pool.ReadAs<string>("LocationTrack").Length != 0 ));

            MemoryPool templateDriver = new MemoryPool("DriverTemplate", MemoryAddress.StaticAbsolute, 0, 0x5F48); // base, 0x5F48 size
            templateDriver.Add(new MemoryFieldConstant<bool>("IsActive", true));

            templateDriver.Add(new MemoryFieldLazy<int>("Index", MemoryAddress.Dynamic, 0, 0x8, 32));

            templateDriver.Add(new MemoryFieldLazy<string>("Name", MemoryAddress.Dynamic, 0, 0x5B08, 32));
            templateDriver.Add(new MemoryFieldLazy<string>("CarTeam", MemoryAddress.Dynamic, 0, 0x5C22, 64));
            templateDriver.Add(new MemoryFieldLazy<string>("CarModel", MemoryAddress.Dynamic, 0, 0x5C62, 64));
            templateDriver.Add(new MemoryFieldLazy<string>("CarClasses", MemoryAddress.Dynamic, 0, 0x39BC, 64));

            templateDriver.Add(new MemoryFieldLazy<float>("Meter", MemoryAddress.Dynamic, 0, 0x3D04, 4));
            templateDriver.Add(new MemoryFieldLazy<float>("Speed", MemoryAddress.Dynamic, 0, 0x57C0, 4));
            templateDriver.Add(new MemoryFieldLazy<float>("RPM", MemoryAddress.Dynamic, 0, 0x317C, 4));
            templateDriver.Add(new MemoryFieldLazy<float>("RPMMax", MemoryAddress.Dynamic, 0, 0x3180, 4));
            templateDriver.Add(new MemoryFieldLazy<int>("Gear", MemoryAddress.Dynamic, 0, 0x321C, 1));

            templateDriver.Add(new MemoryFieldLazy<float>("Mass", MemoryAddress.Dynamic, 0, 0x28DC, 4));
            templateDriver.Add(new MemoryFieldLazy<float>("Fuel", MemoryAddress.Dynamic, 0, 0x315C, 4));
            templateDriver.Add(new MemoryFieldLazy<float>("FuelCapacity", MemoryAddress.Dynamic, 0, 0x3160, 4));

            templateDriver.Add(new MemoryFieldLazy<float>("TyreWearLF", MemoryAddress.Dynamic, 0, 0x2A34, 4));
            templateDriver.Add(new MemoryFieldLazy<float>("TyreWearRF", MemoryAddress.Dynamic, 0, 0x2C1C, 4));
            templateDriver.Add(new MemoryFieldLazy<float>("TyreWearLR", MemoryAddress.Dynamic, 0, 0x2E04, 4));
            templateDriver.Add(new MemoryFieldLazy<float>("TyreWearRR", MemoryAddress.Dynamic, 0, 0x2FEC, 4));

            templateDriver.Add(new MemoryFieldLazy<float>("InputThrottle", MemoryAddress.Dynamic, 0, 0x2938, 4));
            templateDriver.Add(new MemoryFieldLazy<float>("InputBrake", MemoryAddress.Dynamic, 0, 0x2940, 4));

            templateDriver.Add(new MemoryFieldLazy<float>("CoordinateX", MemoryAddress.Dynamic, 0, 0x289C, 4));
            templateDriver.Add(new MemoryFieldLazy<float>("CoordinateY", MemoryAddress.Dynamic, 0, 0x28A4, 4));
            templateDriver.Add(new MemoryFieldLazy<float>("CoordinateZ", MemoryAddress.Dynamic, 0, 0x28A0, 4));

            templateDriver.Add(new MemoryFieldLazy<float>("CoordinateX-Replay", MemoryAddress.Dynamic, 0, 0x10, 4));
            templateDriver.Add(new MemoryFieldLazy<float>("CoordinateY-Replay", MemoryAddress.Dynamic, 0, 0x18, 4));
            templateDriver.Add(new MemoryFieldLazy<float>("CoordinateZ-Replay", MemoryAddress.Dynamic, 0, 0x14, 4));

            templateDriver.Add(new MemoryFieldLazy<float>("RotationX", MemoryAddress.Dynamic, 0, 0x40, 4));
            templateDriver.Add(new MemoryFieldLazy<float>("RotationY", MemoryAddress.Dynamic, 0, 0x48, 4));
            templateDriver.Add(new MemoryFieldLazy<float>("RotationZ", MemoryAddress.Dynamic, 0, 0x44, 4));
            templateDriver.Add(new MemoryFieldFunc<float>("Yaw", (pool) => (float)Math.Atan2(pool.ReadAs<float>("RotationX"), pool.ReadAs<float>("RotationY")), true));
            templateDriver.Add(new MemoryFieldLazy<float>("AccelerationX", MemoryAddress.Dynamic, 0, 0x57C8, 4));

            templateDriver.Add(new MemoryFieldLazy<int>("Pitstops", MemoryAddress.Dynamic, 0, 0x3D2C, 4));
            templateDriver.Add(new MemoryFieldLazy<int>("Position", MemoryAddress.Dynamic, 0, 0x3D20, 4));
            templateDriver.Add(new MemoryFieldLazy<int>("Laps", MemoryAddress.Dynamic, 0, 0x3CF8, 1));

            templateDriver.Add(new MemoryFieldLazy<bool>("IsAI", MemoryAddress.Dynamic, 0, 0x59C8, 1));
            templateDriver.Add(new MemoryFieldLazy<bool>("IsRetired", MemoryAddress.Dynamic, 0, 0x4160, 1));
            templateDriver.Add(new MemoryFieldLazy<bool>("IsLimiter", MemoryAddress.Dynamic, 0, 0x17B1, 1));
            templateDriver.Add(new MemoryFieldLazy<bool>("IsPits", MemoryAddress.Dynamic, 0, 0x27A8, 1));
            templateDriver.Add(new MemoryFieldLazy<bool>("IsDriving", MemoryAddress.Dynamic, 0, 0x3CBF, 1));
            templateDriver.Add(new MemoryFieldLazy<byte>("IsDisqualified", MemoryAddress.Dynamic, 0x3D24, 1,
                                                         (v) => (byte)((v == 3) ? 1 : 0)));

            templateDriver.Add(new MemoryFieldLazy<bool>("FlagYellow", MemoryAddress.Dynamic, 0, 0x104, 1, (x) => !x));
            templateDriver.Add(new MemoryFieldLazy<bool>("FlagBlue", MemoryAddress.Dynamic, 0, 0x3E39, 1));
            templateDriver.Add(new MemoryFieldLazy<bool>("FlagBlack", MemoryAddress.Dynamic, 0, 0x3D24, 1));
            templateDriver.Add(new MemoryFieldLazy<bool>("Ignition", MemoryAddress.Dynamic, 0, 0xAA, 1));

            var laps = new MemoryPool("Laps", MemoryAddress.Dynamic, templateDriver, 0x3D90, 6 * 4 * 200);
            // 200 laps, 6 floats each.
            templateDriver.Add(laps);

            provider.Add(simulator);
            provider.Add(session);
            provider.Add(templateDriver);

            provider.Refresh();
            templateDriver.SetTemplate(true);

            DriverPositionOffset = templateDriver.Fields["Position"].Offset;
            NamePositionOffset = templateDriver.Fields["Name"].Offset;
        }
        public void Initialize(MemoryProvider provider)
        {
            MemoryPool simulator = new MemoryPool("Simulator", MemoryAddress.Static, 0, 0);

            simulator.Add(new MemoryFieldLazy <int>("CarPlayer", MemoryAddress.Static, 0x31528c, 4));
            simulator.Add(new MemoryFieldLazy <int[]>("Drivers", MemoryAddress.Static, 0x315298, 0x19C));

            simulator.Add(new MemoryFieldLazy <string>("LocationGame", MemoryAddress.Static, 0x6EB320, 0, 256));

            MemoryPool session = new MemoryPool("Session", MemoryAddress.Static, 0, 0);

            session.Add(new MemoryFieldLazy <int>("Cars", MemoryAddress.Static, 0x315290, 4));
            session.Add(new MemoryFieldLazy <float>("Time", MemoryAddress.Static, 0x60022C, 4));
            session.Add(new MemoryFieldLazy <float>("Clock", MemoryAddress.Static, 0x6E2CD8, 4));

            session.Add(new MemoryFieldLazy <string>("LocationTrack", MemoryAddress.Static, 0x309D28, 0, 256));

            session.Add(new MemoryFieldLazy <bool>("IsOffline", MemoryAddress.Static, 0x315444, 1, (x) => !x));
            session.Add(new MemoryFieldLazy <bool>("IsActive", MemoryAddress.Static, 0x30FEE4, 1));
            session.Add(new MemoryFieldLazy <bool>("IsReplay", MemoryAddress.Static, 0x315444, 1));
            session.Add(new MemoryFieldFunc <bool>("IsLoading", (pool) => !pool.ReadAs <bool>("IsActive") && pool.ReadAs <int>("Cars") > 0 && pool.ReadAs <string>("LocationTrack").Length != 0));

            MemoryPool templateDriver = new MemoryPool("DriverTemplate", MemoryAddress.StaticAbsolute, 0, 0x5F48); // base, 0x5F48 size

            templateDriver.Add(new MemoryFieldConstant <bool>("IsActive", true));

            templateDriver.Add(new MemoryFieldLazy <int>("Index", MemoryAddress.Dynamic, 0, 0x8, 32));

            templateDriver.Add(new MemoryFieldLazy <string>("Name", MemoryAddress.Dynamic, 0, 0x5B08, 32));
            templateDriver.Add(new MemoryFieldLazy <string>("CarTeam", MemoryAddress.Dynamic, 0, 0x5C22, 64));
            templateDriver.Add(new MemoryFieldLazy <string>("CarModel", MemoryAddress.Dynamic, 0, 0x5C62, 64));
            templateDriver.Add(new MemoryFieldLazy <string>("CarClasses", MemoryAddress.Dynamic, 0, 0x39BC, 64));

            templateDriver.Add(new MemoryFieldLazy <float>("Meter", MemoryAddress.Dynamic, 0, 0x3D04, 4));
            templateDriver.Add(new MemoryFieldLazy <float>("Speed", MemoryAddress.Dynamic, 0, 0x57C0, 4));
            templateDriver.Add(new MemoryFieldLazy <float>("RPM", MemoryAddress.Dynamic, 0, 0x317C, 4));
            templateDriver.Add(new MemoryFieldLazy <float>("RPMMax", MemoryAddress.Dynamic, 0, 0x3180, 4));
            templateDriver.Add(new MemoryFieldLazy <int>("Gear", MemoryAddress.Dynamic, 0, 0x321C, 1));

            templateDriver.Add(new MemoryFieldLazy <float>("Mass", MemoryAddress.Dynamic, 0, 0x28DC, 4));
            templateDriver.Add(new MemoryFieldLazy <float>("Fuel", MemoryAddress.Dynamic, 0, 0x315C, 4));
            templateDriver.Add(new MemoryFieldLazy <float>("FuelCapacity", MemoryAddress.Dynamic, 0, 0x3160, 4));

            templateDriver.Add(new MemoryFieldLazy <float>("TyreWearLF", MemoryAddress.Dynamic, 0, 0x2A34, 4));
            templateDriver.Add(new MemoryFieldLazy <float>("TyreWearRF", MemoryAddress.Dynamic, 0, 0x2C1C, 4));
            templateDriver.Add(new MemoryFieldLazy <float>("TyreWearLR", MemoryAddress.Dynamic, 0, 0x2E04, 4));
            templateDriver.Add(new MemoryFieldLazy <float>("TyreWearRR", MemoryAddress.Dynamic, 0, 0x2FEC, 4));

            templateDriver.Add(new MemoryFieldLazy <float>("InputThrottle", MemoryAddress.Dynamic, 0, 0x2938, 4));
            templateDriver.Add(new MemoryFieldLazy <float>("InputBrake", MemoryAddress.Dynamic, 0, 0x2940, 4));

            templateDriver.Add(new MemoryFieldLazy <float>("CoordinateX", MemoryAddress.Dynamic, 0, 0x289C, 4));
            templateDriver.Add(new MemoryFieldLazy <float>("CoordinateY", MemoryAddress.Dynamic, 0, 0x28A4, 4));
            templateDriver.Add(new MemoryFieldLazy <float>("CoordinateZ", MemoryAddress.Dynamic, 0, 0x28A0, 4));

            templateDriver.Add(new MemoryFieldLazy <float>("CoordinateX-Replay", MemoryAddress.Dynamic, 0, 0x10, 4));
            templateDriver.Add(new MemoryFieldLazy <float>("CoordinateY-Replay", MemoryAddress.Dynamic, 0, 0x18, 4));
            templateDriver.Add(new MemoryFieldLazy <float>("CoordinateZ-Replay", MemoryAddress.Dynamic, 0, 0x14, 4));

            templateDriver.Add(new MemoryFieldLazy <float>("RotationX", MemoryAddress.Dynamic, 0, 0x40, 4));
            templateDriver.Add(new MemoryFieldLazy <float>("RotationY", MemoryAddress.Dynamic, 0, 0x48, 4));
            templateDriver.Add(new MemoryFieldLazy <float>("RotationZ", MemoryAddress.Dynamic, 0, 0x44, 4));
            templateDriver.Add(new MemoryFieldFunc <float>("Yaw", (pool) => (float)Math.Atan2(pool.ReadAs <float>("RotationX"), pool.ReadAs <float>("RotationY")), true));
            templateDriver.Add(new MemoryFieldLazy <float>("AccelerationX", MemoryAddress.Dynamic, 0, 0x57C8, 4));

            templateDriver.Add(new MemoryFieldLazy <int>("Pitstops", MemoryAddress.Dynamic, 0, 0x3D2C, 4));
            templateDriver.Add(new MemoryFieldLazy <int>("Position", MemoryAddress.Dynamic, 0, 0x3D20, 4));
            templateDriver.Add(new MemoryFieldLazy <int>("Laps", MemoryAddress.Dynamic, 0, 0x3CF8, 1));

            templateDriver.Add(new MemoryFieldLazy <bool>("IsAI", MemoryAddress.Dynamic, 0, 0x59C8, 1));
            templateDriver.Add(new MemoryFieldLazy <bool>("IsRetired", MemoryAddress.Dynamic, 0, 0x4160, 1));
            templateDriver.Add(new MemoryFieldLazy <bool>("IsLimiter", MemoryAddress.Dynamic, 0, 0x17B1, 1));
            templateDriver.Add(new MemoryFieldLazy <bool>("IsPits", MemoryAddress.Dynamic, 0, 0x27A8, 1));
            templateDriver.Add(new MemoryFieldLazy <bool>("IsDriving", MemoryAddress.Dynamic, 0, 0x3CBF, 1));
            templateDriver.Add(new MemoryFieldLazy <byte>("IsDisqualified", MemoryAddress.Dynamic, 0x3D24, 1,
                                                          (v) => (byte)((v == 3) ? 1 : 0)));

            templateDriver.Add(new MemoryFieldLazy <bool>("FlagYellow", MemoryAddress.Dynamic, 0, 0x104, 1, (x) => !x));
            templateDriver.Add(new MemoryFieldLazy <bool>("FlagBlue", MemoryAddress.Dynamic, 0, 0x3E39, 1));
            templateDriver.Add(new MemoryFieldLazy <bool>("FlagBlack", MemoryAddress.Dynamic, 0, 0x3D24, 1));
            templateDriver.Add(new MemoryFieldLazy <bool>("Ignition", MemoryAddress.Dynamic, 0, 0xAA, 1));

            var laps = new MemoryPool("Laps", MemoryAddress.Dynamic, templateDriver, 0x3D90, 6 * 4 * 200);

            // 200 laps, 6 floats each.
            templateDriver.Add(laps);


            provider.Add(simulator);
            provider.Add(session);
            provider.Add(templateDriver);

            provider.Refresh();
            templateDriver.SetTemplate(true);

            DriverPositionOffset = templateDriver.Fields["Position"].Offset;
            NamePositionOffset   = templateDriver.Fields["Name"].Offset;
        }
        public void CreateDriver(MemoryPool pool, bool isPlayer)
        {
            if (isPlayer)
            {
                //pool.Add(new MemoryFieldLazy<float>("", MemoryAddress.Static, 0, 4));
                // I can now add tyre temperatures, pressure, brake info etc.
                pool.Add(new MemoryFieldConstant <bool>("IsPlayer", true));
                pool.Add(new MemoryFieldLazy <double>("EngineLifetime", MemoryAddress.Static, 0x006DC0AC, 8));
                pool.Add(new MemoryFieldLazy <double>("EngineOil", MemoryAddress.Static, 0x006DC044, 8, Conversions.Kelvin2Celsius));
                pool.Add(new MemoryFieldLazy <double>("EngineWater", MemoryAddress.Static, 0x006DC084, 8, Conversions.Kelvin2Celsius));
                pool.Add(new MemoryFieldLazy <byte>("EngineMode", MemoryAddress.Static, 0x006DBF70, 1));
                pool.Add(new MemoryFieldLazy <byte>("EngineTorque", MemoryAddress.Static, 0x006DC224, 1));

                pool.Add(new MemoryFieldLazy <int>("FuelStop1", MemoryAddress.Static, 0x006E1EAC, 4));
                pool.Add(new MemoryFieldLazy <int>("FuelStop1", MemoryAddress.Static, 0x006E1EEC, 4));
                pool.Add(new MemoryFieldLazy <int>("FuelStop1", MemoryAddress.Static, 0x006E1F2C, 4));

                pool.Add(new MemoryFieldLazy <double>("RideheightLF", MemoryAddress.Static, 0x006DB778, 8));
                pool.Add(new MemoryFieldLazy <double>("RideheightRF", MemoryAddress.Static, 0x006DB780, 8));
                pool.Add(new MemoryFieldLazy <double>("RideheightLR", MemoryAddress.Static, 0x006DB788, 8));
                pool.Add(new MemoryFieldLazy <double>("RideheightRR", MemoryAddress.Static, 0x006DB790, 8));

                pool.Add(new MemoryFieldLazy <string>("TyreCompoundFront", MemoryAddress.Static, 0x006E1177, 16));
                pool.Add(new MemoryFieldLazy <string>("TyreCompoundRear", MemoryAddress.Static, 0x006E11B7, 16));

                pool.Add(new MemoryFieldLazy <double>("TyrePressureLF", MemoryAddress.Static, 0x006D9F5C, 8));
                pool.Add(new MemoryFieldLazy <double>("TyrePressureRF", MemoryAddress.Static, 0x006DA55C, 8));
                pool.Add(new MemoryFieldLazy <double>("TyrePressureLR", MemoryAddress.Static, 0x006DAB5C, 8));
                pool.Add(new MemoryFieldLazy <double>("TyrePressureRR", MemoryAddress.Static, 0x006DB15C, 8));

                pool.Add(new MemoryFieldLazy <double>("TyreSpeedLF", MemoryAddress.Static, 0x006D9C04, 8));
                pool.Add(new MemoryFieldLazy <double>("TyreSpeedRF", MemoryAddress.Static, 0x006DA204, 8));
                pool.Add(new MemoryFieldLazy <double>("TyreSpeedLR", MemoryAddress.Static, 0x006DA804, 8));
                pool.Add(new MemoryFieldLazy <double>("TyreSpeedRR", MemoryAddress.Static, 0x006DAE04, 8));

                pool.Add(new MemoryFieldLazy <double>("TyreTemperatureInsideLF", MemoryAddress.Static, 0x006D9F44, 8));
                pool.Add(new MemoryFieldLazy <double>("TyreTemperatureInsideRF", MemoryAddress.Static, 0x006DA534, 8));
                pool.Add(new MemoryFieldLazy <double>("TyreTemperatureInsideLR", MemoryAddress.Static, 0x006DAB44, 8));
                pool.Add(new MemoryFieldLazy <double>("TyreTemperatureInsideRR", MemoryAddress.Static, 0x006DB134, 8));

                pool.Add(new MemoryFieldLazy <double>("TyreTemperatureMiddleLF", MemoryAddress.Static, 0x006D9F3C, 8));
                pool.Add(new MemoryFieldLazy <double>("TyreTemperatureMiddleRF", MemoryAddress.Static, 0x006DA53C, 8));
                pool.Add(new MemoryFieldLazy <double>("TyreTemperatureMiddleLR", MemoryAddress.Static, 0x006DAB3C, 8));
                pool.Add(new MemoryFieldLazy <double>("TyreTemperatureMiddleRR", MemoryAddress.Static, 0x006DB13C, 8));

                pool.Add(new MemoryFieldLazy <double>("TyreTemperatureOutsideLF", MemoryAddress.Static, 0x006D9F34, 8));
                pool.Add(new MemoryFieldLazy <double>("TyreTemperatureOutsideRF", MemoryAddress.Static, 0x006DA544, 8));
                pool.Add(new MemoryFieldLazy <double>("TyreTemperatureOutsideLR", MemoryAddress.Static, 0x006DAB34, 8));
                pool.Add(new MemoryFieldLazy <double>("TyreTemperatureOutsideRR", MemoryAddress.Static, 0x006DB144, 8));

                pool.Add(new MemoryFieldLazy <int>("AerodyanmicFrontwingSetting", MemoryAddress.Static, 0x006E182C, 4));
                pool.Add(new MemoryFieldLazy <int>("AerodyanmicRearwingSetting", MemoryAddress.Static, 0x006E186C, 4));
                pool.Add(new MemoryFieldLazy <int>("AerodyanmicRadiatorSetting", MemoryAddress.Static, 0x006E18AC, 4));
                pool.Add(new MemoryFieldLazy <int>("AerodyanmicBrakeductSetting", MemoryAddress.Static, 0x006E18EC, 4));

                pool.Add(new MemoryFieldLazy <double>("BrakeTemperatureLF", MemoryAddress.Static, 0x006DA0E0, 8));
                pool.Add(new MemoryFieldLazy <double>("BrakeTemperatureRF", MemoryAddress.Static, 0x006DA6E0, 8));
                pool.Add(new MemoryFieldLazy <double>("BrakeTemperatureLR", MemoryAddress.Static, 0x006DACE0, 8));
                pool.Add(new MemoryFieldLazy <double>("BrakeTemperatureRR", MemoryAddress.Static, 0x006DB2E0, 8));

                pool.Add(new MemoryFieldLazy <double>("BrakeThicknessLF", MemoryAddress.Static, 0x006DA110, 8));
                pool.Add(new MemoryFieldLazy <double>("BrakeThicknessRF", MemoryAddress.Static, 0x006DA710, 8));
                pool.Add(new MemoryFieldLazy <double>("BrakeThicknessLR", MemoryAddress.Static, 0x006DAD10, 8));
                pool.Add(new MemoryFieldLazy <double>("BrakeThicknessRR", MemoryAddress.Static, 0x006DB310, 8));

                pool.Add(new MemoryFieldLazy <double>("InputClutch", MemoryAddress.Static, 0x006D9744, 8));
                pool.Add(new MemoryFieldLazy <double>("InputSteering", MemoryAddress.Static, 0x006D972C, 8));

                pool.Add(new MemoryFieldLazy <byte>("HelpsABS", MemoryAddress.Static, 0x006D9786, 1));
                pool.Add(new MemoryFieldLazy <byte>("HelpsTC", MemoryAddress.Static, 0x006D977E, 1));
                pool.Add(new MemoryFieldLazy <byte>("HelpsSteer", MemoryAddress.Static, 0x006D9785, 1));

                pool.Add(new MemoryFieldLazy <bool>("HelpsLock", MemoryAddress.Static, 0x006D9784, 1));
                pool.Add(new MemoryFieldLazy <bool>("HelpsSpin", MemoryAddress.Static, 0x006D9787, 1));
                pool.Add(new MemoryFieldLazy <bool>("HelpsStability", MemoryAddress.Static, 0x006D9780, 1));
                pool.Add(new MemoryFieldLazy <bool>("HelpsClutch", MemoryAddress.Static, 0x006D9785, 1));
                //pool.Add(new MemoryFieldLazy<byte>("HelpsShift", MemoryAddress.Static, 0x006D9782, 1));
            }
            else
            {
                pool.Add(new MemoryFieldConstant <bool>("IsPlayer", false));
            }
        }
        async Task HandleBody(InventoryMessage message, int peerId)
        {
            // Data should not exceed the maximum size.
            var data = message.Data;

            if (data.Length > MaximumBlockSize)
            {
                throw new ArgumentException();
            }

            var id = message.IsBlock ?
                     BlockchainUtil.ComputeBlockId(data) :
                     Hash.ComputeDoubleSHA256(data);

            if (!ByteString.CopyFrom(id).Equals(message.ObjectId))
            {
                return;
            }

            if (message.IsBlock)
            {
                lock (Blocks)//並列処理を避ける
                {
                    if (Blocks.ContainsKey(message.ObjectId))
                    {
                        return;
                    }
                    Blocks.Add(message.ObjectId, data);
                }

                var prevId = Deserialize <Block>(data).PreviousHash;
                if (!Blocks.ContainsKey(prevId))
                {
                    await ConnectionManager.SendAsync(new InventoryMessage//当該ブロックの前のブロックを持っていない場合はそれもRequestする(前のblockのIDが必要なため
                    {
                        Type     = Request,
                        IsBlock  = true,
                        ObjectId = prevId,
                    }, peerId);
                }
                else
                {
                    Executor.ProcessBlock(data, prevId);
                }
            }
            else
            {
                if (MemoryPool.ContainsKey(message.ObjectId))
                {
                    return;
                }

                var tx = BlockchainUtil.DeserializeTransaction(data);

                // Ignore the coinbase transactions.
                if (tx.InEntries.Count == 0)
                {
                    return;
                }

                lock (MemoryPool)
                {
                    if (MemoryPool.ContainsKey(message.ObjectId))
                    {
                        return;
                    }
                    MemoryPool.Add(message.ObjectId, tx);
                }
            }

            message.Type = Advertise;
            message.Data = null;
            await ConnectionManager.BroadcastAsync(message, peerId);
        }//ネットワーク全体にもらったブロックを発信する