/// <summary>
        /// Vector cache is a superset of PropertyCache. The difference is that when deserializing in VectorCache mode, FlatSharp
        /// will allocate a vector for you that gets lazily filled in as elements are accessed. This leads to some array allocations
        /// behind the scenes, since FlatSharp needs to know what objects have been returned for what indices.
        /// </summary>
        public static void VectorCacheDeserialization(DemoTable demo)
        {
            var serializer = new FlatBufferSerializer(new FlatBufferSerializerOptions(FlatBufferDeserializationOption.VectorCache));

            byte[] buffer = new byte[1024];
            serializer.Serialize(demo, buffer);
            var parsed = serializer.Parse <DemoTable>(buffer);

            // Properties from tables and structs are cached after they are read.
            string name  = parsed.Name;
            string name2 = parsed.Name;

            Debug.Assert(
                object.ReferenceEquals(name, name2),
                "When reading table/struct properties, PropertyCache mode returns the same instance.");

            // VectorCache deserialization guarantees only one object per index.
            InnerTable index0_1 = parsed.ListVector[0];
            InnerTable index0_2 = parsed.ListVector[0];

            Debug.Assert(object.ReferenceEquals(index0_1, index0_2), "The same instance is returned each time from vectors in VectorCache mode.");
            Debug.Assert(object.ReferenceEquals(parsed.ListVector, parsed.ListVector), "And the vector instance itself is the same.");
            Debug.Assert(object.ReferenceEquals(index0_1.Fruit, index0_1.Fruit), "And the items returned from each vector exhibit property cache behavior");
        }
        public void TestInitialize()
        {
            this.stringVectorSource = new TableVector <string> {
                Vector = new IndexedVector <string, VectorMember <string> >()
            };
            this.intVectorSource = new TableVector <int> {
                Vector = new IndexedVector <int, VectorMember <int> >()
            };
            this.stringKeys = new List <string>();

            for (int i = 0; i < 10; ++i)
            {
                string key = i.ToString();
                stringKeys.Add(key);

                this.stringVectorSource.Vector.AddOrReplace(new VectorMember <string> {
                    Key = key, Value = Guid.NewGuid().ToString()
                });
                this.intVectorSource.Vector.AddOrReplace(new VectorMember <int> {
                    Key = i, Value = Guid.NewGuid().ToString()
                });
            }

            this.stringVectorSource.Vector.Freeze();
            this.intVectorSource.Vector.Freeze();

            Span <byte> buffer = new byte[1024];

            var serializer   = new FlatBufferSerializer(new FlatBufferSerializerOptions(FlatBufferDeserializationOption.Lazy));
            int bytesWritten = serializer.Serialize(this.stringVectorSource, buffer);

            this.stringVectorParsed = serializer.Parse <TableVector <string> >(buffer.Slice(0, bytesWritten).ToArray());

            bytesWritten         = serializer.Serialize(this.intVectorSource, buffer);
            this.intVectorParsed = serializer.Parse <TableVector <int> >(buffer.Slice(0, bytesWritten).ToArray());
        }
        public static void Run()
        {
            FooBarContainer container = new FooBarContainer
            {
                fruit       = Fruit.Pears,
                initialized = true,
                location    = "location",
                list        = new List <FooBar>
                {
                    new FooBar
                    {
                        name    = "name",
                        postfix = 1,
                        rating  = 3,
                        sibling = new Bar
                        {
                            ratio = 3.14f,
                            size  = ushort.MaxValue,
                            time  = int.MinValue,

                            // Nested structs are not intended to have null values,
                            // but it is possible due to FlatSharp modeling
                            // them as reference types. However, null structs
                            // do not cause a problem when serializing or parsing.
                            parent = null !,
                        }
                    }
                },
            };

            // Simple use case: make a deep copy of an object you're using.
            var copy = new FooBarContainer(container);

            Debug.Assert(!object.ReferenceEquals(copy.list, container.list), "A new list is created");
            for (int i = 0; i < container.list.Count; ++i)
            {
                var originalItem = container.list[i];

                Debug.Assert(copy.list is not null);
                var copyItem = copy.list[i];
                Debug.Assert(!object.ReferenceEquals(copyItem, originalItem));
            }

            // Now let's look at how this can be useful when operating on deserialized objects.
            var serializer = new FlatBufferSerializer(FlatBufferDeserializationOption.Lazy);

            byte[] data = new byte[1024];
            serializer.Serialize(container, data);
            var deserialized = serializer.Parse <FooBarContainer>(data);

            // Take a deserialized item and "upcast" it back to the original type.
            // This performs a full traversal of the object and allows the underlying buffer to be reused.
            Debug.Assert(deserialized.GetType() != container.GetType(), "The deserialized type is a subclass of the FooBarContainer type");
            copy = new FooBarContainer(deserialized);
            Debug.Assert(copy.GetType() == container.GetType(), "By using the copy constructor, we can get an instance of the original type.");

            // Next: Some deserialization modes, such as Lazy, don't permit mutation of the object.
            // Using the copy constructor can convert this to an object that we can mutate!
            try
            {
                // will throw
                deserialized.fruit = Fruit.Apples;
                Debug.Assert(false);
            }
            catch
            {
            }

            // Modifying the copy is just fine, though.
            copy.fruit = Fruit.Apples;
        }
示例#4
0
        public virtual void GlobalSetup()
        {
            Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.Idle;

            FooBar[]                      fooBars                 = new FooBar[this.VectorLength];
            FooBarNonVirtual[]            fooBarsNV               = new FooBarNonVirtual[this.VectorLength];
            FooBar_ValueType[]            fooBarsValue            = new FooBar_ValueType[this.VectorLength];
            FooBar_ValueType_NonVirtual[] fooBarsValue_NonVirtual = new FooBar_ValueType_NonVirtual[this.VectorLength];

            for (int i = 0; i < fooBars.Length; i++)
            {
                var foo = new Foo
                {
                    Id     = 0xABADCAFEABADCAFE + (ulong)i,
                    Count  = (short)(10000 + i),
                    Prefix = (sbyte)('@' + i),
                    Length = (uint)(1000000 + i)
                };

                var fooNV = new FooNonVirtual
                {
                    Id     = 0xABADCAFEABADCAFE + (ulong)i,
                    Count  = (short)(10000 + i),
                    Prefix = (sbyte)('@' + i),
                    Length = (uint)(1000000 + i)
                };

                var fooValue = new Foo_ValueType
                {
                    Id     = 0xABADCAFEABADCAFE + (ulong)i,
                    Count  = (short)(10000 + i),
                    Prefix = (sbyte)('@' + i),
                    Length = (uint)(1000000 + i)
                };

                var bar = new Bar
                {
                    Parent = foo,
                    Ratio  = 3.14159f + i,
                    Size   = (ushort)(10000 + i),
                    Time   = 123456 + i
                };

                var barNV = new BarNonVirtual
                {
                    Parent = fooNV,
                    Ratio  = 3.14159f + i,
                    Size   = (ushort)(10000 + i),
                    Time   = 123456 + i
                };

                var barValue = new Bar_ValueType
                {
                    Parent = fooValue,
                    Ratio  = 3.14159f + i,
                    Size   = (ushort)(10000 + i),
                    Time   = 123456 + i
                };

                var fooBar = new FooBar
                {
                    Name    = Guid.NewGuid().ToString(),
                    PostFix = (byte)('!' + i),
                    Rating  = 3.1415432432445543543 + i,
                    Sibling = bar,
                };

                var fooBarNV = new FooBarNonVirtual
                {
                    Name    = Guid.NewGuid().ToString(),
                    PostFix = (byte)('!' + i),
                    Rating  = 3.1415432432445543543 + i,
                    Sibling = barNV,
                };

                var fooBarValue = new FooBar_ValueType
                {
                    Name    = Guid.NewGuid().ToString(),
                    PostFix = (byte)('!' + i),
                    Rating  = 3.1415432432445543543 + i,
                    Sibling = barValue,
                };

                var fooBarValueNV = new FooBar_ValueType_NonVirtual
                {
                    Name    = Guid.NewGuid().ToString(),
                    PostFix = (byte)('!' + i),
                    Rating  = 3.1415432432445543543 + i,
                    Sibling = barValue,
                };

                fooBars[i]                 = fooBar;
                fooBarsNV[i]               = fooBarNV;
                fooBarsValue[i]            = fooBarValue;
                fooBarsValue_NonVirtual[i] = fooBarValueNV;
            }

            this.defaultContainer = new FooBarListContainer
            {
                Fruit       = 123,
                Initialized = true,
                Location    = "http://google.com/flatbuffers/",
                List        = fooBars,
            };

            this.defaultContainerNonVirtual = new FooBarListContainerNonVirtual
            {
                Fruit       = 123,
                Initialized = true,
                Location    = "http://google.com/flatbuffers/",
                List        = fooBarsNV,
            };

            this.defaultContainerWithValueStructs = new FooBarListContainer_ValueType
            {
                Fruit       = 123,
                Initialized = true,
                Location    = "http://google.com/flatbuffers/",
                List        = fooBarsValue,
            };

            this.defaultContainerWithValueStructsNonVirtual = new FooBarListContainer_ValueType_NonVirtual
            {
                Fruit       = 123,
                Initialized = true,
                Location    = "http://google.com/flatbuffers/",
                List        = fooBarsValue_NonVirtual,
            };

            Random rng = new Random();

            this.sortedIntContainer = new SortedVectorTable <int> {
                Vector = new List <SortedVectorTableItem <int> >()
            };
            this.sortedStringContainer = new SortedVectorTable <string> {
                Vector = new List <SortedVectorTableItem <string> >()
            };
            this.unsortedIntContainer = new UnsortedVectorTable <int> {
                Vector = new List <UnsortedVectorTableItem <int> >()
            };
            this.unsortedStringContainer = new UnsortedVectorTable <string> {
                Vector = new List <UnsortedVectorTableItem <string> >()
            };

            for (int i = 0; i < this.VectorLength; ++i)
            {
                this.sortedIntContainer.Vector.Add(new SortedVectorTableItem <int> {
                    Key = rng.Next()
                });
                this.sortedStringContainer.Vector.Add(new SortedVectorTableItem <string> {
                    Key = Guid.NewGuid().ToString()
                });
                this.unsortedIntContainer.Vector.Add(new UnsortedVectorTableItem <int> {
                    Key = rng.Next()
                });
                this.unsortedStringContainer.Vector.Add(new UnsortedVectorTableItem <string> {
                    Key = Guid.NewGuid().ToString()
                });
            }

            this.valueTableVector = new ValueTableVector
            {
                ValueTables = Enumerable.Range(0, this.VectorLength).Select(x => new ValueTable
                {
                    A = 1,
                    B = 2,
                    C = 3,
                    D = 4,
                    E = 5,
                    F = 6,
                    G = 7,
                    H = 8,
                    I = 9,
                    J = 10,
                    K = true,
                }).ToArray()
            };

            {
                var options = new FlatBufferSerializerOptions(this.DeserializeOption);

                this.fs_serializer = new FlatBufferSerializer(options);

                int offset = this.fs_serializer.Serialize(this.defaultContainer, this.fs_writeMemory);
                this.fs_readMemory = this.fs_writeMemory.AsSpan(0, offset).ToArray();
                this.inputBuffer   = new InputBufferKind(this.fs_readMemory);
                this.FlatSharp_ParseAndTraverse();
            }

            int googleLength = 0;

            {
                this.google_ByteBuffer       = new FlatBuffers.ByteBuffer(this.fs_readMemory.ToArray());
                this.google_defaultContainer = Google.FooBarContainer.GetRootAsFooBarContainer(this.google_ByteBuffer).UnPack();
                googleLength = this.Google_FlatBuffers_Serialize_ObjectApi();
            }

            {
                this.PBDN_Serialize();
                this.pbdn_writeBuffer.Position = 0;
                this.pbdn_writeBuffer.CopyTo(this.pbdn_readBuffer);
                this.PBDN_ParseAndTraverse();
            }

            {
                this.MsgPack_Serialize_NonVirtual();
            }

            Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.RealTime;

            Console.WriteLine($"Sizes: MsgPack: {this.msgPackWriteData.Length}");
            Console.WriteLine($"Sizes: FlatSharp: {this.fs_readMemory.Length}");
            Console.WriteLine($"Sizes: Google: {googleLength}");
            Console.WriteLine($"Sizes: Pbdn: {this.pbdn_writeBuffer.Length}");
        }
示例#5
0
        public virtual void GlobalSetup()
        {
            FooBar[] fooBars = new FooBar[this.VectorLength];
            for (int i = 0; i < fooBars.Length; i++)
            {
                var foo = new Foo
                {
                    Id     = 0xABADCAFEABADCAFE + (ulong)i,
                    Count  = (short)(10000 + i),
                    Prefix = (sbyte)('@' + i),
                    Length = (uint)(1000000 + i)
                };

                var bar = new Bar
                {
                    Parent = foo,
                    Ratio  = 3.14159f + i,
                    Size   = (ushort)(10000 + i),
                    Time   = 123456 + i
                };

                var fooBar = new FooBar
                {
                    Name    = Guid.NewGuid().ToString(),
                    PostFix = (byte)('!' + i),
                    Rating  = 3.1415432432445543543 + i,
                    Sibling = bar,
                };

                fooBars[i] = fooBar;
            }

            this.defaultContainer = new FooBarListContainer
            {
                Fruit       = 123,
                Initialized = true,
                Location    = "http://google.com/flatbuffers/",
                List        = fooBars,
            };

            Random rng = new Random();

            this.sortedIntContainer = new SortedVectorTable <int> {
                Vector = new List <SortedVectorTableItem <int> >()
            };
            this.sortedStringContainer = new SortedVectorTable <string> {
                Vector = new List <SortedVectorTableItem <string> >()
            };
            this.unsortedIntContainer = new UnsortedVectorTable <int> {
                Vector = new List <UnsortedVectorTableItem <int> >()
            };
            this.unsortedStringContainer = new UnsortedVectorTable <string> {
                Vector = new List <UnsortedVectorTableItem <string> >()
            };

            for (int i = 0; i < this.VectorLength; ++i)
            {
                this.sortedIntContainer.Vector.Add(new SortedVectorTableItem <int> {
                    Key = rng.Next()
                });
                this.sortedStringContainer.Vector.Add(new SortedVectorTableItem <string> {
                    Key = Guid.NewGuid().ToString()
                });
                this.unsortedIntContainer.Vector.Add(new UnsortedVectorTableItem <int> {
                    Key = rng.Next()
                });
                this.unsortedStringContainer.Vector.Add(new UnsortedVectorTableItem <string> {
                    Key = Guid.NewGuid().ToString()
                });
            }

            {
                var options = new FlatBufferSerializerOptions(this.DeserializeOption);

                this.fs_serializer = new FlatBufferSerializer(options);

                int offset = this.fs_serializer.Serialize(this.defaultContainer, this.fs_writeMemory);
                this.fs_readMemory = this.fs_writeMemory.AsSpan(0, offset).ToArray();
                this.inputBuffer   = new ArrayInputBuffer(this.fs_readMemory);
                this.FlatSharp_ParseAndTraverse();
            }

            {
                this.google_ByteBuffer       = new FlatBuffers.ByteBuffer(this.fs_readMemory.ToArray());
                this.google_defaultContainer = Google.FooBarContainer.GetRootAsFooBarContainer(this.google_ByteBuffer).UnPack();
            }

            {
                this.PBDN_Serialize();
                this.pbdn_writeBuffer.Position = 0;
                this.pbdn_writeBuffer.CopyTo(this.pbdn_readBuffer);
                this.PBDN_ParseAndTraverse();
            }
        }
示例#6
0
        static void Main(string[] args)
        {
            Upstream up = new Upstream();

            up.Structure      = new FSeed();
            up.Structure.Data = new ConfigEntry[1];
            var e0 = new ConfigEntry();

            up.Structure.Data[0] = e0;
            e0.Tag  = "stuff";
            e0.Type = "string";
            e0.Used = true;

            Console.WriteLine("test serializing up");

            var testBuffer = new byte[1000];

            var serializer = new FlatBufferSerializer(new FlatBufferSerializerOptions(FlatBufferDeserializationOption.Default));

            var testLength = serializer.Serialize(up, testBuffer);

            Upstream up2 = serializer.Parse <Upstream>(testBuffer);


            Console.WriteLine("Starting Main");

            // For some reason, I have to copy the args into a local array, or else they disappear
            int[] fileDescriptors = new int[2];
            for (int i = 0; i < fileDescriptors.Length && i < args.Length; i++)
            {
                int n = ParsedFd(args[i]);
                Console.WriteLine(n);
                fileDescriptors[i] = n;
            }

            // Grab the FDs from the environment variables and translate to a IntPtr
            int            fdNumIn      = fileDescriptors[0];
            SafeFileHandle inPipeHandle = new SafeFileHandle(new IntPtr(fdNumIn), true);

            int            fdNumOut      = fileDescriptors[1];
            SafeFileHandle outPipeHandle = new SafeFileHandle(new IntPtr(fdNumOut), true);

            Console.WriteLine("GO_FUZZ_IN_FD: " + fdNumIn);
            Console.WriteLine("GO_FUZZ_OUT_FD: " + fdNumOut);

            string[] commName = new string[4];
            for (int i = 2; i < args.Length; i++)
            {
                commName[i - 2] = args[i];
                Console.WriteLine(("comm" + (i - 2)) + ": " + commName[(i - 2)]);
            }

            const int MaxInputSize     = 1 << 24;
            const int ReturnResultSize = 1 << 25;
            const int CoverSize        = 64 << 10;
            const int SonarRegionSize  = 1 << 20;

            // Use the filehandles
            Stream inStream = new FileStream(inPipeHandle, FileAccess.Read);

            Console.WriteLine("created inStream");
            Stream outStream = new FileStream(outPipeHandle, FileAccess.Write);

            Console.WriteLine("created outStream");

            MemoryMappedFileSecurity security = new MemoryMappedFileSecurity();

            security.AddAccessRule(new AccessRule <MemoryMappedFileRights>(("Everyone"), MemoryMappedFileRights.FullControl, AccessControlType.Allow));

            Console.WriteLine("created security");

            MemoryMappedFile comm0 = MemoryMappedFile.OpenExisting(commName[0], MemoryMappedFileRights.ReadWrite, HandleInheritability.Inheritable);

            Console.WriteLine("created comm0");
            var comm0Accessor = comm0.CreateViewAccessor(0, MaxInputSize);

            Console.WriteLine("created comm0Accessor");

            MemoryMappedFile comm1 = MemoryMappedFile.OpenExisting(commName[1], MemoryMappedFileRights.ReadWrite, HandleInheritability.Inheritable);

            Console.WriteLine("created comm1");
            var comm1Accessor = comm1.CreateViewAccessor(0, ReturnResultSize);

            Console.WriteLine("created comm1Accessor");

            MemoryMappedFile comm2 = MemoryMappedFile.OpenExisting(commName[2], MemoryMappedFileRights.ReadWrite, HandleInheritability.Inheritable);

            Console.WriteLine("created comm2");
            var comm2Accessor = comm2.CreateViewAccessor(0, CoverSize);

            Console.WriteLine("created comm2Accessor");

            //var comm3Stream = new FileStream(commNames[3], FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
            //Console.WriteLine("created comm3Stream");
            MemoryMappedFile comm3 = MemoryMappedFile.OpenExisting(commName[3], MemoryMappedFileRights.ReadWrite, HandleInheritability.Inheritable);

            Console.WriteLine("created comm3");
            var comm3Accessor = comm3.CreateViewAccessor(0, SonarRegionSize);

            Console.WriteLine("created comm3Accessor");

            char[] inPipeBuffer       = new char[10];
            char[] outputBuffer       = new char[24];
            char[] returnLengthBuffer = new char[8];

            StreamReader inPipeReader = new StreamReader(inStream);
            StreamWriter outputWriter = new StreamWriter(outStream);

            while (true)
            {
                inPipeReader.Read(inPipeBuffer, 0, inPipeBuffer.Length);

                int fnidx = inPipeBuffer[0];
                fnidx += inPipeBuffer[1] << 8;
                Console.WriteLine("fnidx: " + fnidx);

                char[] lengthBuffer = new char[8];
                for (int i = 2; i < inPipeBuffer.Length; i++)
                {
                    lengthBuffer[i - 2] = inPipeBuffer[i];
                }
                Int64 inputLength = Deserialize64(lengthBuffer);
                Console.WriteLine("input length: " + inputLength);

                // read inputBuffer data from comm0
                var inputBuffer = new byte[inputLength];
                comm0Accessor.ReadArray(0, inputBuffer, 0, (int)inputLength);
                for (int i = 0; i < inputLength; i++)
                {
                    inputBuffer[i] = comm0Accessor.ReadByte(i);
                }

                var inputString = Encoding.UTF8.GetString(inputBuffer);

                Console.WriteLine("downstream: ");
                Console.WriteLine(inputString);

                //var downstream = Downstream.Deserialize(inputString);
                var downstream = FlatBufferSerializer.Default.Parse <Downstream>(inputBuffer);
                Console.WriteLine("downstream deserialized");

                var         seed    = downstream.Seed;
                var         entries = seed.Data;
                ConfigEntry entry   = null;
                if (entries.Count >= 1)
                {
                    entry = entries[0];
                }
                else
                {
                    Console.WriteLine("zero entries!");
                }

                var value = entry.Value;
                Console.WriteLine("got entry value: " + value);

                Int64 res = 0;
                Int64 ns;
                Int64 sonar        = 0;
                Int64 returnLength = 0;

                // Start the clock
                var nsStart = DateTime.UtcNow.Ticks;

                // Actually run the function to fuzz
                Console.WriteLine("BrokenMethod()");
                Console.WriteLine(BrokenMethod(value));

                ns = DateTime.UtcNow.Ticks - nsStart;
                Console.WriteLine("ns: " + ns);

                char[] resBuffer   = new char[8];
                char[] nsBuffer    = new char[8];
                char[] sonarBuffer = new char[8];

                Serialize56(resBuffer, res);
                Serialize56(nsBuffer, ns);
                Serialize56(sonarBuffer, sonar);

                Console.WriteLine("instantiating upstream");

                Upstream upstream = new Upstream();
                upstream.Structure      = new FSeed();
                upstream.Structure.Data = new ConfigEntry[1];
                var upEntry = upstream.Structure.Data[0];
                upEntry.Tag  = "stuff";
                upEntry.Type = "string";
                upEntry.Used = true;

                Console.WriteLine("serializing upstream");

                int maxReturnSize = FlatBufferSerializer.Default.GetMaxSize(upstream);
                var returnBuffer  = new byte[maxReturnSize];
                returnLength = FlatBufferSerializer.Default.Serialize(upstream, returnBuffer);
                Console.WriteLine("return length: " + returnLength);
                Serialize56(returnLengthBuffer, returnLength);

                for (int i = 0; i < 8; i++)
                {
                    Console.WriteLine("returnLengthBuffer: " + (byte)returnLengthBuffer[i]);
                    comm1Accessor.Write(i, returnLengthBuffer[i]);
                }

                for (int i = 0; i < returnLength; i++)
                {
                    comm1Accessor.Write(i + 8, returnBuffer[i]);
                }

                comm1Accessor.Flush();
                Console.WriteLine("wrote to comm1Accessor");

                for (int i = 0; i < 8; i++)
                {
                    outputBuffer[i]      = resBuffer[i];
                    outputBuffer[i + 8]  = nsBuffer[i];
                    outputBuffer[i + 16] = sonarBuffer[i];
                }

                outputWriter.Write(outputBuffer, 0, outputBuffer.Length);
                outputWriter.Flush();
                Console.WriteLine("wrote outputbuffer");
            }
        }
示例#7
0
 public virtual void Deserialize(object incoming)
 {
     Serial.FBComponent data = (Serial.FBComponent)incoming;
     ID     = FlatBufferSerializer.GetOrCreateDeserialize <UID>(data.Id);
     Entity = FlatBufferSerializer.GetOrCreateDeserialize <UID>(data.Entity);
 }
示例#8
0
    private void RunTest <T>(string fbsType, int length, FlatBufferDeserializationOption option) where T : struct
    {
        string schema = $@"
            {MetadataHelpers.AllAttributes}
            namespace StructVectorTests;

            table Table ({MetadataKeys.SerializerKind}) {{
                foo:Foo;
            }}
            struct Foo {{
              V:[{fbsType}:{length}];
            }}";

        (Assembly asm, string code) = FlatSharpCompiler.CompileAndLoadAssemblyWithCode(
            schema,
            new());

        Type tableType = asm.GetType("StructVectorTests.Table");
        Type fooType   = asm.GetType("StructVectorTests.Foo");
        var  typeModel = TypeModelContainer.CreateDefault().CreateTypeModel(fooType);

        Assert.Equal(FlatBufferSchemaType.Struct, typeModel.SchemaType);

        for (int i = 0; i < length; ++i)
        {
            PropertyInfo p = fooType.GetProperty($"__flatsharp__V_{i}", BindingFlags.Instance | BindingFlags.NonPublic);
            Assert.True(p.GetMethod.IsFamily);
            Assert.True(p.SetMethod.IsFamily);
            Assert.True(p.GetMethod.IsVirtual);
            Assert.True(p.SetMethod.IsVirtual);

            var metaAttr = p.GetCustomAttribute <FlatBufferMetadataAttribute>();
            var attr     = p.GetCustomAttribute <FlatBufferItemAttribute>();
            Assert.NotNull(attr);
            Assert.NotNull(metaAttr);
            Assert.Equal(i, attr.Index);
            Assert.Equal(FlatBufferMetadataKind.Accessor, metaAttr.Kind);
            Assert.Equal($"V[{attr.Index}]", metaAttr.Value);
        }

        var vectorProperty = fooType.GetProperty("V");

        Assert.Null(vectorProperty.GetCustomAttribute <FlatBufferItemAttribute>()); // pseudo-item, not actual.
        Assert.True(vectorProperty.GetMethod.IsPublic);
        Assert.False(vectorProperty.GetMethod.IsVirtual);
        Assert.Null(vectorProperty.SetMethod);

        dynamic table = Activator.CreateInstance(tableType);
        dynamic foo   = Activator.CreateInstance(fooType);

        table.foo = foo;

        Assert.Equal(length, foo.V.Count);

        // Test copyFrom with full array.
        List <T> items = new List <T>();

        for (int i = 0; i < length; ++i)
        {
            items.Add(GetRandom <T>());
        }

        table.foo.V.CopyFrom((IReadOnlyList <T>)items);
        for (int i = 0; i < length; ++i)
        {
            CheckRandom <T>(items[i], table.foo.V[i]);
        }

        for (int i = 0; i < length; ++i)
        {
            foo.V[i] = GetRandom <T>();
        }

        byte[] data = new byte[1024];

        var fbs = new FlatBufferSerializer(
            new FlatBufferSerializerOptions(option)
        {
            EnableAppDomainInterceptOnAssemblyLoad = true
        });

        var serializer = fbs.Compile((object)table);

        serializer.Write(data, (object)table);
        dynamic parsed = serializer.Parse(data);

        dynamic copy = Activator.CreateInstance(tableType, (object)parsed);

        Assert.Equal(length, parsed.foo.V.Count);
        for (int i = 0; i < length; ++i)
        {
            CheckRandom <T>(foo.V[i], parsed.foo.V[i]);
            CheckRandom <T>(foo.V[i], copy.foo.V[i]);
        }

        bool isMutable = option is FlatBufferDeserializationOption.GreedyMutable;

        if (length == 0)
        {
            return;
        }

        try
        {
            for (int i = 0; i < length; ++i)
            {
                parsed.foo.V[i] = GetRandom <T>();
            }

            Assert.True(isMutable);
        }
        catch (NotMutableException)
        {
            Assert.False(isMutable);
        }
    }
示例#9
0
        private void RunTest <T>(string fbsType, int length, FlatBufferDeserializationOption option) where T : struct
        {
            string schema = $@"
            namespace StructVectorTests;

            table Table ({MetadataKeys.SerializerKind}) {{
                foo:Foo;
            }}
            struct Foo {{
              V:[{fbsType}:{length}];
            }}";

            Assembly asm = FlatSharpCompiler.CompileAndLoadAssembly(
                schema,
                new());

            Type tableType = asm.GetType("StructVectorTests.Table");
            Type fooType   = asm.GetType("StructVectorTests.Foo");
            var  typeModel = TypeModelContainer.CreateDefault().CreateTypeModel(fooType);

            Assert.AreEqual(FlatBufferSchemaType.Struct, typeModel.SchemaType);

            for (int i = 0; i < length; ++i)
            {
                PropertyInfo p = fooType.GetProperty($"__flatsharp__V_{i}");
                Assert.IsTrue(p.GetMethod.IsPublic);
                Assert.IsTrue(p.SetMethod.IsPublic);
                Assert.IsTrue(p.GetMethod.IsVirtual);
                Assert.IsTrue(p.SetMethod.IsVirtual);

                var attr = p.GetCustomAttribute <FlatBufferItemAttribute>();
                Assert.IsNotNull(attr);
                Assert.AreEqual(i, attr.Index);

                var browsableAttr = p.GetCustomAttribute <EditorBrowsableAttribute>();
                Assert.IsNotNull(browsableAttr);
                Assert.AreEqual(EditorBrowsableState.Advanced, browsableAttr.State);
            }

            var vectorProperty = fooType.GetProperty("V");

            Assert.IsNull(vectorProperty.GetCustomAttribute <FlatBufferItemAttribute>()); // pseudo-item, not actual.
            Assert.IsNull(vectorProperty.GetCustomAttribute <EditorBrowsableAttribute>());
            Assert.IsTrue(vectorProperty.GetMethod.IsPublic);
            Assert.IsFalse(vectorProperty.GetMethod.IsVirtual);
            Assert.IsNull(vectorProperty.SetMethod);

            dynamic table = Activator.CreateInstance(tableType);
            dynamic foo   = Activator.CreateInstance(fooType);

            table.foo = foo;

            Assert.AreEqual(length, foo.V.Count);

            for (int i = 0; i < length; ++i)
            {
                foo.V[i] = GetRandom <T>();
            }

            byte[] data = new byte[1024];

            var fbs = new FlatBufferSerializer(
                new FlatBufferSerializerOptions(option)
            {
                EnableAppDomainInterceptOnAssemblyLoad = true
            });

            var serializer = fbs.Compile((object)table);

            serializer.Write(data, (object)table);
            dynamic parsed = serializer.Parse(data);

            dynamic copy = Activator.CreateInstance(tableType, (object)parsed);

            Assert.AreEqual(length, parsed.foo.V.Count);
            for (int i = 0; i < length; ++i)
            {
                CheckRandom <T>(foo.V[i], parsed.foo.V[i]);
                CheckRandom <T>(foo.V[i], copy.foo.V[i]);
            }

            bool isMutable = option is FlatBufferDeserializationOption.VectorCacheMutable or FlatBufferDeserializationOption.GreedyMutable;

            if (length == 0)
            {
                return;
            }

            try
            {
                for (int i = 0; i < length; ++i)
                {
                    parsed.foo.V[i] = GetRandom <T>();
                }

                Assert.IsTrue(isMutable);
            }
            catch (NotMutableException)
            {
                Assert.IsFalse(isMutable);
            }
        }
示例#10
0
        public static void SaveGame(String pathToFolder, NamelessGame game)
        {
            var  saveFile = new NamelessRogueSaveFile();
            Type iStorageInterfaceType = typeof(IStorage <>);

            foreach (var componentTypeCollection in EntityInfrastructureManager.Components)
            {
                if (componentTypeCollection.Key.GetCustomAttributes(true).Any(a => a.GetType() == typeof(NamelessRogue.Engine.Serialization.SkipClassGeneration)))
                {
                    continue;
                }

                foreach (var componentDictionary in componentTypeCollection.Value)
                {
                    //the type of the storage that can store current type
                    Type storageType = saveFile.ComponentTypeToStorge[componentTypeCollection.Key];

                    var constructor = storageType.GetConstructor(Type.EmptyTypes);
                    if (constructor == null)
                    {
                        throw new Exception($@"{storageType.Name} must contain a parameterless constructor in order for Save manager to serialize it");
                    }

                    dynamic storageObject = constructor.Invoke(null);

                    storageObject.FillFrom(ObjectExtensions.CastToReflected(componentDictionary.Value, componentDictionary.Value.GetType()));

                    saveFile.StoragesDictionary[storageObject.GetType()].Add(storageObject);
                }
            }
            foreach (var entity in EntityInfrastructureManager.Entities.Values)
            {
                var storage = new EntityStorage();
                storage.FillFrom(entity);
                saveFile.EntityStorageTable.Add(storage);
            }

            {
                int    maxBytesNeeded = FlatBufferSerializer.Default.GetMaxSize(saveFile);
                byte[] buffer         = new byte[maxBytesNeeded];
                int    bytesWritten   = FlatBufferSerializer.Default.Serialize(saveFile, buffer);

                var stream = File.OpenWrite("objectdata.nrs");
                stream.Write(buffer, 0, bytesWritten);
                stream.Close();
            }

            {
                TimelineStorage timelinesStorage = new TimelineStorage();
                var             timeline         = game.TimelineEntity.GetComponentOfType <TimeLine>();
                timelinesStorage.FillFrom(timeline);
                {
                    FlatBufferSerializer serializer = new FlatBufferSerializer(FlatBufferDeserializationOption.Greedy);
                    int maxBytesNeeded = serializer.GetMaxSize(timelinesStorage);

                    int byteNeeded = serializer.GetMaxSize(timelinesStorage.CurrentTimelineLayer.WorldTiles[50 * 10]);

                    byte[] buffer = new byte[maxBytesNeeded];

                    int bytesWritten = serializer.Serialize(timelinesStorage, buffer);

                    var stream = File.OpenWrite("chunksmemory.nrs");
                    stream.Write(buffer, 0, bytesWritten);
                    stream.Close();
                }
            }
        }
示例#11
0
 public T GetTypedObject <T>(int fbPos)
 {
     Serial.FBRef?fbRef = GetFBRef(fbPos);
     return(FlatBufferSerializer.DeserializeTypedObject <T>(fbRef));
 }
示例#12
0
        public TResult GetObject <TSerialized, TResult>(int fbPos) where TResult : IFBSerializable, new() where TSerialized : IFlatbufferObject, new()
        {
            TResult result = FlatBufferSerializer.GetOrCreateDeserialize <TResult>(CreateSerialObject <TSerialized>(fbPos));

            return(result);
        }
示例#13
0
        public T GetOrCreate <T, S>(int fbPos) where T : new() where S : IFlatbufferObject, new()
        {
            S s = CreateSerialObject <S>(fbPos);

            return(FlatBufferSerializer.GetOrCreateDeserialize <T>(s));
        }
    private void TestType <T>(FlatBufferDeserializationOption option, Func <T> generator) where T : IEquatable <T>
    {
        var options = new FlatBufferSerializerOptions(option);
        FlatBufferSerializer serializer = new FlatBufferSerializer(options);

        {
            var memoryTable = new RootTable <IList <T> >
            {
                Vector = Enumerable.Range(0, 10).Select(i => generator()).ToArray()
            };

            Span <byte> memory            = new byte[10240];
            int         offset            = serializer.Serialize(memoryTable, memory);
            var         memoryTableResult = serializer.Parse <RootTable <IList <T> > >(memory.Slice(0, offset).ToArray());

            var resultVector = memoryTableResult.Vector;
            for (int i = 0; i < memoryTableResult.Vector.Count; ++i)
            {
                Assert.Equal <T>(memoryTable.Vector[i], resultVector[i]);

                // reference equality should correspond to the serializer.
                Assert.Equal(option != FlatBufferDeserializationOption.Lazy, object.ReferenceEquals(resultVector[i], resultVector[i]));
            }
        }

        {
            var memoryTable = new RootTable <IReadOnlyList <T> >
            {
                Vector = Enumerable.Range(0, 10).Select(i => generator()).ToArray()
            };

            Span <byte> memory            = new byte[10240];
            int         offset            = serializer.Serialize(memoryTable, memory);
            var         memoryTableResult = serializer.Parse <RootTable <IReadOnlyList <T> > >(memory.Slice(0, offset).ToArray());
            var         resultVector      = memoryTableResult.Vector;
            for (int i = 0; i < memoryTableResult.Vector.Count; ++i)
            {
                Assert.Equal(memoryTable.Vector[i], resultVector[i]);

                // reference equality should correspond to the serializer.
                Assert.Equal(option != FlatBufferDeserializationOption.Lazy, object.ReferenceEquals(resultVector[i], resultVector[i]));
            }
        }

        if (option != FlatBufferDeserializationOption.Lazy && option != FlatBufferDeserializationOption.Progressive)
        {
            var memoryTable = new RootTable <T[]>
            {
                Vector = Enumerable.Range(0, 10).Select(i => generator()).ToArray()
            };

            Span <byte> memory            = new byte[10240];
            int         offset            = serializer.Serialize(memoryTable, memory);
            var         memoryTableResult = serializer.Parse <RootTable <T[]> >(memory.Slice(0, offset).ToArray());
            var         resultVector      = memoryTableResult.Vector;
            for (int i = 0; i < memoryTableResult.Vector.Length; ++i)
            {
                Assert.Equal(memoryTable.Vector[i], resultVector[i]);
            }
        }
    }
示例#15
0
        public void GlobalSetup()
        {
            FooBar[] fooBars = new FooBar[this.VectorLength];
            for (int i = 0; i < fooBars.Length; i++)
            {
                var foo = new Foo
                {
                    Id     = 0xABADCAFEABADCAFE + (ulong)i,
                    Count  = (short)(10000 + i),
                    Prefix = (sbyte)('@' + i),
                    Length = (uint)(1000000 + i)
                };

                var bar = new Bar
                {
                    Parent = foo,
                    Ratio  = 3.14159f + i,
                    Size   = (ushort)(10000 + i),
                    Time   = 123456 + i
                };

                var fooBar = new FooBar
                {
                    Name    = Guid.NewGuid().ToString(),
                    PostFix = (byte)('!' + i),
                    Rating  = 3.1415432432445543543 + i,
                    Sibling = bar,
                };

                fooBars[i] = fooBar;
            }

            this.defaultContainer = new FooBarListContainer
            {
                Fruit       = 123,
                Initialized = true,
                Location    = "http://google.com/flatbuffers/",
                List        = fooBars,
            };

            {
                this.Google_FlatBuffers_Serialize();
                this.google_ByteBuffer = new FlatBuffers.ByteBuffer(this.google_flatBufferBuilder.SizedByteArray());
            }

            {
                var options = new FlatBufferSerializerOptions(this.DeserializeOption);

                this.fs_serializer = new FlatBufferSerializer(options);
                int offset = this.fs_serializer.Serialize(this.defaultContainer, this.fs_writeMemory);
                this.fs_readMemory = this.fs_writeMemory.AsSpan(0, offset).ToArray();
                this.inputBuffer   = new ArrayInputBuffer(this.fs_readMemory);
                this.FlatSharp_ParseAndTraverse();
            }

            {
                this.PBDN_Serialize();
                this.pbdn_writeBuffer.Position = 0;
                this.pbdn_writeBuffer.CopyTo(this.pbdn_readBuffer);
                this.PBDN_ParseAndTraverse();
            }
        }
            static void Test(FlatBufferDeserializationOption option)
            {
                var table = new Table <WriteThroughStruct>
                {
                    Struct = new WriteThroughStruct
                    {
                        Value = new OtherStruct {
                            Prop1 = 10, Prop2 = 10
                        },
                        ValueStruct = new() { Value = 3, }
                    }
                };

                FlatBufferSerializer serializer = new FlatBufferSerializer(option);

                byte[] buffer = new byte[1024];
                serializer.Serialize(table, buffer);

                // parse
                var parsed1 = serializer.Parse <Table <WriteThroughStruct> >(buffer);

                // mutate
                Assert.Equal(10, parsed1.Struct.Value.Prop1);
                Assert.Equal(10, parsed1.Struct.Value.Prop2);
                Assert.Equal(3, parsed1.Struct.ValueStruct.Value);
                parsed1.Struct.Value = new OtherStruct {
                    Prop1 = 300, Prop2 = 300
                };
                parsed1.Struct.ValueStruct = new() { Value = -1 };
                Assert.Equal(300, parsed1.Struct.Value.Prop1);
                Assert.Equal(300, parsed1.Struct.Value.Prop2);
                Assert.Equal(-1, parsed1.Struct.ValueStruct.Value);

                // verify, set to null
                var parsed2 = serializer.Parse <Table <WriteThroughStruct> >(buffer);

                Assert.Equal(300, parsed2.Struct.Value.Prop1);
                Assert.Equal(300, parsed2.Struct.Value.Prop2);
                Assert.Equal(-1, parsed2.Struct.ValueStruct.Value);
                parsed2.Struct.Value = null !;

                if (option == FlatBufferDeserializationOption.Progressive)
                {
                    // we are null temporarily until we re-parse.
                    Assert.Null(parsed2.Struct.Value);
                }
                else if (option == FlatBufferDeserializationOption.Lazy)
                {
                    // lazy write through clears it out.
                    Assert.Equal(0, parsed2.Struct.Value.Prop1);
                    Assert.Equal(0, parsed2.Struct.Value.Prop2);
                }
                else
                {
                    Assert.False(true);
                }

                // verify, set to null
                var parsed3 = serializer.Parse <Table <WriteThroughStruct> >(buffer);

                Assert.Equal(0, parsed3.Struct.Value.Prop1);
                Assert.Equal(0, parsed3.Struct.Value.Prop2);
            }

            Test(FlatBufferDeserializationOption.Progressive);
            Test(FlatBufferDeserializationOption.Lazy);
        }