public void NullableStructSome()
        {
            var typebuilder  = new TypeBuilder();
            var twoIntStruct = typebuilder.AddStruct("TwoInt");

            twoIntStruct.AddField("a", new FlatBuffersType(BaseType.Int));
            twoIntStruct.AddField("b", new FlatBuffersType(BaseType.Int));

            var option = typebuilder.AddTable("NullableTwoInt");

            option.AddField("Some", new FlatBuffersType(BaseType.Struct, twoIntStruct));

            typebuilder.Compile();

            var builder        = new FlatBufferBuilder(1);
            var builderWrapper = new FlatBufferBuilderWrapper(typebuilder, builder);

            builderWrapper.StartTable("NullableTwoInt");
            builderWrapper.AddStruct("Some", new object[] { 5, 10 });
            builderWrapper.Finish(builderWrapper.EndTable());

            var beginData  = builder.DataBuffer.Position;
            var countData  = builder.DataBuffer.Length - beginData;
            var byteBuffer = new ByteBuffer(builder.DataBuffer.Data.Skip(beginData).Take(countData).ToArray());
            var wrapper    = new FlatBufferWrapper(typebuilder, "NullableTwoInt", byteBuffer);

            var wr = (FlatBufferWrapper)wrapper["Some"];

            Assert.AreEqual(5, wr["a"]);
            Assert.AreEqual(10, wr["b"]);
        }
        public void VectorOfStruct()
        {
            var typebuilder = new TypeBuilder();
            var elemStruct  = typebuilder.AddStruct("Stuff");

            elemStruct.AddField("a", new FlatBuffersType(BaseType.Int));
            elemStruct.AddField("b", new FlatBuffersType(BaseType.Int));
            elemStruct.AddField("c", new FlatBuffersType(BaseType.Double));

            var root        = typebuilder.AddTable("Root");
            var vectorField = root.AddField("vector", new FlatBuffersType(BaseType.Vector, elemStruct)
            {
                ElementType = BaseType.Struct
            });

            typebuilder.Compile();

            var builder        = new FlatBufferBuilder(1);
            var builderWrapper = new FlatBufferBuilderWrapper(typebuilder, builder);

            builderWrapper.StartVector(root.Name, vectorField.Name, 2);

            var expected = new object[][] {
                new object[] { 1, 2, 1.2345 },
                new object[] { 3, 4, 5.6780 },
            };

            var i1     = builderWrapper.CreateStruct(elemStruct.Name, expected[1]);
            var i2     = builderWrapper.CreateStruct(elemStruct.Name, expected[0]);
            var offset = builderWrapper.EndVector();

            builderWrapper.StartTable(root.Name);
            builderWrapper.AddVector(vectorField.Name, offset);
            builderWrapper.Finish(builderWrapper.EndTable());

            var beginData  = builder.DataBuffer.Position;
            var countData  = builder.DataBuffer.Length - beginData;
            var byteBuffer = new ByteBuffer(builder.DataBuffer.Data.Skip(beginData).Take(countData).ToArray());
            var wrapper    = new FlatBufferWrapper(typebuilder, root.Name, byteBuffer);

            var elemsUntyped = (IEnumerable)wrapper[vectorField.Name];
            var actual       = elemsUntyped.Cast <FlatBufferWrapper>().Select(wr => new[] { wr["a"], wr["b"], wr["c"] }).ToArray();

            CollectionAssert.AreEquivalent(expected[0], actual[0]);
            CollectionAssert.AreEquivalent(expected[1], actual[1]);
        }
        public void CreateMonsterFlatBufferTypes()
        {
            if (_typeBuilder.Structs.Count > 0)
            {
                return;
            }
            var color = _typeBuilder.AddEnum("Color", BaseType.Byte);

            color.Add(new EnumVal {
                Name = "Red", Value = 0,
            });
            color.Add(new EnumVal {
                Name = "Green",
            });                                       // didn't set value
            color.Add(new EnumVal {
                Name = "Blue", Value = 2,
            });

            // each of the EnumVals should end up associated to a table.
            var any = _typeBuilder.AddUnion("Any");

            any.Add(new EnumVal {
                Name = "Monster"
            });
            any.Add(new EnumVal {
                Name = "Weapon"
            });
            any.Add(new EnumVal {
                Name = "Pickup"
            });

            var vec3   = _typeBuilder.AddStruct("Vec3");
            var vec3_x = vec3.AddField("x", new FlatBuffersType {
                BaseType = BaseType.Float
            });
            var vec3_y = vec3.AddField("y", new FlatBuffersType {
                BaseType = BaseType.Float
            });
            var vec3_z = vec3.AddField("z", new FlatBuffersType {
                BaseType = BaseType.Float
            });

            var minion      = _typeBuilder.AddTable("Minion");
            var minion_name = minion.AddField("name", new FlatBuffersType {
                BaseType = BaseType.String
            });

            var monster     = _typeBuilder.AddTable("Monster");
            var monster_pos = monster.AddField("pos", new FlatBuffersType {
                BaseType = BaseType.Struct, StructDef = vec3,
            });
            var monster_mana = monster.AddField("mana", new FlatBuffersType {
                BaseType = BaseType.Short
            });
            var monster_hp = monster.AddField("hp", new FlatBuffersType {
                BaseType = BaseType.Short
            });
            var monster_name = monster.AddField("name", new FlatBuffersType {
                BaseType = BaseType.String
            });
            var monster_friendly = monster.AddField("friendly", new FlatBuffersType {
                BaseType = BaseType.Bool
            });
            var monster_inventory = monster.AddField("inventory",
                                                     new FlatBuffersType {
                BaseType = BaseType.Vector, ElementType = BaseType.UByte
            });
            var monster_color = monster.AddField("color",
                                                 new FlatBuffersType {
                BaseType = BaseType.UByte, EnumDef = color
            });
            var monster_minions = monster.AddField("minions",
                                                   new FlatBuffersType {
                BaseType = BaseType.Vector, ElementType = BaseType.Struct, StructDef = minion
            });
            var monster_main_minion = monster.AddField("mainMinion",
                                                       new FlatBuffersType {
                BaseType = BaseType.Struct, StructDef = minion
            });

            // should result in two fields, thingy which is a table and thingy_type for the type enum
            var monster_thingy = monster.AddField("thingy",
                                                  new FlatBuffersType {
                BaseType = BaseType.Union, EnumDef = any
            });

            var weapon      = _typeBuilder.AddTable("Weapon");
            var weapon_name = weapon.AddField("name", new FlatBuffersType {
                BaseType = BaseType.String
            });

            var pickup      = _typeBuilder.AddTable("Pickup");
            var pickup_name = pickup.AddField("name", new FlatBuffersType {
                BaseType = BaseType.String
            });

            _typeBuilder.Compile();

            var red   = color.Values.Lookup("Red");
            var green = color.Values.Lookup("Green");
            var blue  = color.Values.Lookup("Blue");

            Assert.AreEqual(0, red.Value);
            Assert.AreEqual(1, green.Value);
            Assert.AreEqual(2, blue.Value);

            Assert.AreEqual(0, vec3_x.Value.offset);
            Assert.AreEqual(4, vec3_y.Value.offset);
            Assert.AreEqual(8, vec3_z.Value.offset);
            Assert.IsTrue(vec3.Fixed);

            Assert.AreEqual(4, monster_pos.Value.offset);
            Assert.AreEqual(6, monster_mana.Value.offset);
            Assert.AreEqual(8, monster_hp.Value.offset);
            Assert.AreEqual(10, monster_name.Value.offset);
            Assert.AreEqual(12, monster_friendly.Value.offset);
            Assert.AreEqual(14, monster_inventory.Value.offset);
            Assert.AreEqual(16, monster_color.Value.offset);
        }