public void NullableIntSome() { var typebuilder = new TypeBuilder(); var option = typebuilder.AddTable("NullableInt"); option.AddField("Some", new FlatBuffersType(BaseType.Int)); typebuilder.Compile(); var builder = new FlatBufferBuilder(1); builder.ForceDefaults = true; var builderWrapper = new FlatBufferBuilderWrapper(typebuilder, builder); builderWrapper.StartTable("NullableInt"); builderWrapper.AddInt("Some", 5); //builderWrapper.("Some", new object[0]); 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, "NullableInt", byteBuffer, true); var wr = wrapper["Some"]; Assert.AreEqual(5, wr); }
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 Union_Pickup() { CreateMonsterFlatBufferTypes(); var builder = new FlatBufferBuilder(1); var builderWrapper = new FlatBufferBuilderWrapper(_typeBuilder, builder); var pickup_name = builderWrapper.CreateString("Pickup"); builderWrapper.StartTable("Pickup"); builderWrapper.AddString("name", pickup_name); var pickup = builderWrapper.EndTable(); var monster_name = builderWrapper.CreateString("Monster2"); builderWrapper.StartTable("Monster"); builderWrapper.AddByte("thingy_type", 2); // Pickup builderWrapper.AddTable("thingy", pickup); builderWrapper.AddString("name", monster_name); 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, "Monster", byteBuffer); Assert.AreEqual("Monster2", (string)wrapper["name"]); Assert.AreEqual(2, (byte)wrapper["thingy_type"]); var thingy_wrapper = (FlatBufferWrapper)wrapper["thingy"]; Assert.IsNotNull(thingy_wrapper); Assert.AreEqual("Pickup", (string)thingy_wrapper["name"]); }
internal FlatBufferWrapper GetMonsterWrapper(TypeBuilder typeBuilder) { var builder = new FlatBufferBuilder(1); var builderWrapper = new FlatBufferBuilderWrapper(typeBuilder, builder); var monsterName = builderWrapper.CreateString("Fred"); builderWrapper.StartVector("Monster", "inventory", 2); builderWrapper.AddByte(3); //idx 1 builderWrapper.AddByte(2); //idx 0 var monsterInventory = builderWrapper.EndVector(); var minion1_name = builderWrapper.CreateString("Banana"); var minion2_name = builderWrapper.CreateString("Ananab"); var main_minion_name = builderWrapper.CreateString("MainMinion"); builderWrapper.StartTable("Minion"); builderWrapper.AddString("name", minion1_name); var minion1 = builderWrapper.EndTable(); builderWrapper.StartTable("Minion"); builderWrapper.AddString("name", minion2_name); var minion2 = builderWrapper.EndTable(); builderWrapper.StartTable("Minion"); builderWrapper.AddString("name", main_minion_name); var mainMinion = builderWrapper.EndTable(); builderWrapper.StartVector("Monster", "minions", 2); builderWrapper.AddTable(minion2); //idx 1 builderWrapper.AddTable(minion1); //idx 0 var minions = builderWrapper.EndVector(); builderWrapper.StartTable("Monster"); builderWrapper.AddStruct("pos", new object[] { 1.0f, 2.0f, 3.0f }); // x, y, z builderWrapper.AddShort("mana", 42); builderWrapper.AddShort("hp", 17); builderWrapper.AddString("name", monsterName); builderWrapper.AddBool("friendly", true); builderWrapper.AddVector("inventory", monsterInventory); builderWrapper.AddByte("color", 2); // Blue builderWrapper.AddVector("minions", minions); builderWrapper.AddTable("mainMinion", mainMinion); 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()); return(new FlatBufferWrapper(_typeBuilder, "Monster", byteBuffer)); }
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]); }