private void CanCreateNewFlatBufferFromScratch(bool sizePrefix) { // Second, let's create a FlatBuffer from scratch in C#, and test it also. // We use an initial size of 1 to exercise the reallocation algorithm, // normally a size larger than the typical FlatBuffer you generate would be // better for performance. var fbb = new FlatBufferBuilder(1); StringOffset[] names = { fbb.CreateString("Frodo"), fbb.CreateString("Barney"), fbb.CreateString("Wilma") }; Offset <Monster>[] off = new Offset <Monster> [3]; Monster.StartMonster(fbb); Monster.AddName(fbb, names[0]); off[0] = Monster.EndMonster(fbb); Monster.StartMonster(fbb); Monster.AddName(fbb, names[1]); off[1] = Monster.EndMonster(fbb); Monster.StartMonster(fbb); Monster.AddName(fbb, names[2]); off[2] = Monster.EndMonster(fbb); var sortMons = Monster.CreateSortedVectorOfMonster(fbb, off); // We set up the same values as monsterdata.json: var str = fbb.CreateString("MyMonster"); var test1 = fbb.CreateString("test1"); var test2 = fbb.CreateString("test2"); Monster.StartInventoryVector(fbb, 5); for (int i = 4; i >= 0; i--) { fbb.AddByte((byte)i); } var inv = fbb.EndVector(); var fred = fbb.CreateString("Fred"); Monster.StartMonster(fbb); Monster.AddName(fbb, fred); var mon2 = Monster.EndMonster(fbb); Monster.StartTest4Vector(fbb, 2); MyGame.Example.Test.CreateTest(fbb, (short)10, (sbyte)20); MyGame.Example.Test.CreateTest(fbb, (short)30, (sbyte)40); var test4 = fbb.EndVector(); Monster.StartTestarrayofstringVector(fbb, 2); fbb.AddOffset(test2.Value); fbb.AddOffset(test1.Value); var testArrayOfString = fbb.EndVector(); Monster.StartMonster(fbb); Monster.AddPos(fbb, Vec3.CreateVec3(fbb, 1.0f, 2.0f, 3.0f, 3.0, Color.Green, (short)5, (sbyte)6)); Monster.AddHp(fbb, (short)80); Monster.AddName(fbb, str); Monster.AddInventory(fbb, inv); Monster.AddTestType(fbb, Any.Monster); Monster.AddTest(fbb, mon2.Value); Monster.AddTest4(fbb, test4); Monster.AddTestarrayofstring(fbb, testArrayOfString); Monster.AddTestbool(fbb, true); Monster.AddTestarrayoftables(fbb, sortMons); var mon = Monster.EndMonster(fbb); if (sizePrefix) { Monster.FinishSizePrefixedMonsterBuffer(fbb, mon); } else { Monster.FinishMonsterBuffer(fbb, mon); } // Dump to output directory so we can inspect later, if needed #if ENABLE_SPAN_T var data = fbb.DataBuffer.ToSizedArray(); string filename = @"Resources/monsterdata_cstest" + (sizePrefix ? "_sp" : "") + ".mon"; File.WriteAllBytes(filename, data); #else using (var ms = fbb.DataBuffer.ToMemoryStream(fbb.DataBuffer.Position, fbb.Offset)) { var data = ms.ToArray(); string filename = @"Resources/monsterdata_cstest" + (sizePrefix ? "_sp" : "") + ".mon"; File.WriteAllBytes(filename, data); } #endif // Remove the size prefix if necessary for further testing ByteBuffer dataBuffer = fbb.DataBuffer; if (sizePrefix) { Assert.AreEqual(ByteBufferUtil.GetSizePrefix(dataBuffer) + FlatBufferConstants.SizePrefixLength, dataBuffer.Length - dataBuffer.Position); dataBuffer = ByteBufferUtil.RemoveSizePrefix(dataBuffer); } // Now assert the buffer TestBuffer(dataBuffer); //Attempt to mutate Monster fields and check whether the buffer has been mutated properly // revert to original values after testing Monster monster = Monster.GetRootAsMonster(dataBuffer); // mana is optional and does not exist in the buffer so the mutation should fail // the mana field should retain its default value Assert.AreEqual(monster.MutateMana((short)10), false); Assert.AreEqual(monster.Mana, (short)150); // Accessing a vector of sorted by the key tables Assert.AreEqual(monster.Testarrayoftables(0).Value.Name, "Barney"); Assert.AreEqual(monster.Testarrayoftables(1).Value.Name, "Frodo"); Assert.AreEqual(monster.Testarrayoftables(2).Value.Name, "Wilma"); // Example of searching for a table by the key Assert.IsTrue(monster.TestarrayoftablesByKey("Frodo") != null); Assert.IsTrue(monster.TestarrayoftablesByKey("Barney") != null); Assert.IsTrue(monster.TestarrayoftablesByKey("Wilma") != null); // testType is an existing field Assert.AreEqual(monster.TestType, Any.Monster); //mutate the inventory vector Assert.AreEqual(monster.MutateInventory(0, 1), true); Assert.AreEqual(monster.MutateInventory(1, 2), true); Assert.AreEqual(monster.MutateInventory(2, 3), true); Assert.AreEqual(monster.MutateInventory(3, 4), true); Assert.AreEqual(monster.MutateInventory(4, 5), true); for (int i = 0; i < monster.InventoryLength; i++) { Assert.AreEqual(monster.Inventory(i), i + 1); } //reverse mutation Assert.AreEqual(monster.MutateInventory(0, 0), true); Assert.AreEqual(monster.MutateInventory(1, 1), true); Assert.AreEqual(monster.MutateInventory(2, 2), true); Assert.AreEqual(monster.MutateInventory(3, 3), true); Assert.AreEqual(monster.MutateInventory(4, 4), true); // get a struct field and edit one of its fields Vec3 pos = (Vec3)monster.Pos; Assert.AreEqual(pos.X, 1.0f); pos.MutateX(55.0f); Assert.AreEqual(pos.X, 55.0f); pos.MutateX(1.0f); Assert.AreEqual(pos.X, 1.0f); TestBuffer(dataBuffer); TestObjectAPI(Monster.GetRootAsMonster(dataBuffer)); }