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]); }