Esempio n. 1
0
        public static Offset <Monster> BuildMonster(FlatBufferBuilder fbb)
        {
            var sword = Weapon.CreateWeapon(fbb, fbb.CreateString("Sword"), 3);
            var axe   = Weapon.CreateWeapon(fbb, fbb.CreateString("Axe"), 5);

            Monster.StartInventoryVector(fbb, 5);
            var inventoryOffset = BuildVector(fbb, true);;

            var weapons = new Offset <Weapon>[2] {
                sword, axe
            };
            var weaponsOffset = Monster.CreateWeaponsVector(fbb, weapons);

            Monster.StartPathVector(fbb, 2); //Vector of structs
            Vec3.CreateVec3(fbb, 1.0f, 2.0f, 3.0f);
            Vec3.CreateVec3(fbb, 4.0f, 5.0f, 6.0f);
            var path = fbb.EndVector();

            var nameOffset = fbb.CreateString("Orc");

            Monster.StartMonster(fbb);
            Monster.AddPos(fbb, Vec3.CreateVec3(fbb, 1.0f, 2.0f, 3.0f));
            Monster.AddHp(fbb, 300);
            Monster.AddName(fbb, nameOffset);
            Monster.AddInventory(fbb, inventoryOffset);
            Monster.AddColor(fbb, Color.Red);
            Monster.AddWeapons(fbb, weaponsOffset);
            Monster.AddEquippedType(fbb, Equipment.Weapon); //Type of union stored
            Monster.AddEquipped(fbb, axe.Value);            // Value of union
            Monster.AddPath(fbb, path);
            return(Monster.EndMonster(fbb));
        }
Esempio n. 2
0
    private byte[] Save()
    {
        Debug.Log("Creating and saving a Monster to byte array");
        // Create a `FlatBufferBuilder`, which will be used to create our
        // monsters' FlatBuffers.
        var builder = new FlatBufferBuilder(1024);

        var weaponOneName   = builder.CreateString("Sword");
        var weaponOneDamage = 3;
        var weaponTwoName   = builder.CreateString("Axe");
        var weaponTwoDamage = 5;
        // Use the `CreateWeapon()` helper function to create the weapons, since we set every field.
        var sword = Weapon.CreateWeapon(builder, weaponOneName, (short)weaponOneDamage);
        var axe   = Weapon.CreateWeapon(builder, weaponTwoName, (short)weaponTwoDamage);

        // Serialize a name for our monster, called "Orc".
        var name = builder.CreateString("Orc");

        // Create a `vector` representing the inventory of the Orc. Each number
        // could correspond to an item that can be claimed after he is slain.
        // Note: Since we prepend the bytes, this loop iterates in reverse order.
        Monster.StartInventoryVector(builder, 10);
        for (int i = 9; i >= 0; i--)
        {
            builder.AddByte((byte)i);
        }

        var inv = builder.EndVector();

        var weaps = new Offset <Weapon> [2];

        weaps[0] = sword;
        weaps[1] = axe;
        // Pass the `weaps` array into the `CreateWeaponsVector()` method to create a FlatBuffer vector.
        var weapons = Monster.CreateWeaponsVector(builder, weaps);

        Monster.StartPathVector(builder, 2);
        Vec3.CreateVec3(builder, 1.0f, 2.0f, 3.0f);
        Vec3.CreateVec3(builder, 4.0f, 5.0f, 6.0f);
        var path = builder.EndVector();

        // Create our monster using `StartMonster()` and `EndMonster()`.
        Monster.StartMonster(builder);
        Monster.AddPos(builder, Vec3.CreateVec3(builder, 1.0f, 2.0f, 3.0f));
        Monster.AddHp(builder, 300);
        Monster.AddName(builder, name);
        Monster.AddInventory(builder, inv);
        Monster.AddColor(builder, MyGame.Sample.Color.Red);
        Monster.AddWeapons(builder, weapons);
        Monster.AddEquippedType(builder, Equipment.Weapon);
        Monster.AddEquipped(builder, axe.Value); // Axe
        Monster.AddPath(builder, path);
        var orc = Monster.EndMonster(builder);

        // Call `Finish()` to instruct the builder that this monster is complete.
        builder.Finish(orc.Value); // You could also call `Monster.FinishMonsterBuffer(builder, orc);`.

        return(builder.SizedByteArray());
    }
Esempio n. 3
0
        static void Main(string[] args)
        {
            FlatBufferBuilder builder = new FlatBufferBuilder(1);
            var weaponOneName         = builder.CreateString("Sword");
            var weaponOneDamage       = 3;
            var weaponTwoName         = builder.CreateString("Axe");
            var weaponTwoDamage       = 5;
            // Use the `CreateWeapon()` helper function to create the weapons, since we set every field.
            var sword = Weapon.CreateWeapon(builder, weaponOneName, (short)weaponOneDamage);
            var axe   = Weapon.CreateWeapon(builder, weaponTwoName, (short)weaponTwoDamage);

            var name = builder.CreateString("Orc");

            Monster.StartInventoryVector(builder, 10);
            for (int i = 9; i >= 0; i--)
            {
                builder.AddByte((byte)i);
            }
            var inv = builder.EndVector();

            var weaps = new Offset <Weapon> [2];

            weaps[0] = sword;
            weaps[1] = axe;

            // Pass the `weaps` array into the `CreateWeaponsVector()` method to create a FlatBuffer vector.
            var weapons = Monster.CreateWeaponsVector(builder, weaps);
            var pos     = Vec3.CreateVec3(builder, 1.0f, 2.0f, 3.0f);

            Monster.StartMonster(builder);
            Monster.AddPos(builder, pos);
            Monster.AddHp(builder, (short)300);

            Monster.AddName(builder, name);
            Monster.AddInventory(builder, inv);
            Monster.AddColor(builder, Color.Red);

            Monster.AddWeapons(builder, weapons);
            Monster.AddEquippedType(builder, Equipment.Weapon);
            Monster.AddEquipped(builder, axe.Value); // Axe

            var orc = Monster.EndMonster(builder);

            builder.Finish(orc.Value);
            var buf = builder.SizedByteArray();

            // Initialize the client with the receive filter and request handler
            foo(buf);
            while (true)
            {
                ;
            }
        }
Esempio n. 4
0
        static FlatBufferBuilder MakeMonster()
        {
            var builder = new FlatBufferBuilder(1);

            // Create some weapons for our Monster ('Sword' and 'Axe').
            var weapon1Name   = builder.CreateString("Sword");
            var weapon1Damage = 3;
            var weapon2Name   = builder.CreateString("Axe");
            var weapon2Damage = 5;

            // Use the `CreateWeapon()` helper function to create the weapons, since we set every field.
            var weaps = new Offset <Weapon> [2];

            weaps[0] = Weapon.CreateWeapon(builder, weapon1Name, (short)weapon1Damage);
            weaps[1] = Weapon.CreateWeapon(builder, weapon2Name, (short)weapon2Damage);

            // Serialize the FlatBuffer data.
            var name     = builder.CreateString("Orc");
            var treasure = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            var inv      = Monster.CreateInventoryVector(builder, treasure);
            var weapons  = Monster.CreateWeaponsVector(builder, weaps);
            var pos      = Vec3.CreateVec3(builder, 1.0f, 2.0f, 3.0f);

            Monster.StartMonster(builder);
            Monster.AddPos(builder, pos);
            Monster.AddHp(builder, (short)300);
            Monster.AddName(builder, name);
            Monster.AddInventory(builder, inv);
            Monster.AddColor(builder, Color.Red);
            Monster.AddWeapons(builder, weapons);
            Monster.AddEquippedType(builder, Equipment.Weapon);
            Monster.AddEquipped(builder, weaps[1].Value);
            var orc = Monster.EndMonster(builder);

            builder.Finish(orc.Value);
            return(builder);
        }
    // Example how to use FlatBuffers to create and read binary buffers.
    static void Main()
    {
        var builder = new FlatBufferBuilder(1);

        // Create some weapons for our Monster ('Sword' and 'Axe').
        var weapon1Name   = builder.CreateString("Sword");
        var weapon1Damage = 3;
        var weapon2Name   = builder.CreateString("Axe");
        var weapon2Damage = 5;

        // Use the `CreateWeapon()` helper function to create the weapons, since we set every field.
        var weaps = new Offset <Weapon> [2];

        weaps[0] = Weapon.CreateWeapon(builder, weapon1Name, (short)weapon1Damage);
        weaps[1] = Weapon.CreateWeapon(builder, weapon2Name, (short)weapon2Damage);

        // Serialize the FlatBuffer data.
        var name     = builder.CreateString("Orc");
        var treasure = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
        var inv      = Monster.CreateInventoryVector(builder, treasure);
        var weapons  = Monster.CreateWeaponsVector(builder, weaps);
        var pos      = Vec3.CreateVec3(builder, 1.0f, 2.0f, 3.0f);

        Monster.StartMonster(builder);
        Monster.AddPos(builder, pos);
        Monster.AddHp(builder, (short)300);
        Monster.AddName(builder, name);
        Monster.AddInventory(builder, inv);
        Monster.AddColor(builder, Color.Red);
        Monster.AddWeapons(builder, weapons);
        Monster.AddEquippedType(builder, Equipment.Weapon);
        Monster.AddEquipped(builder, weaps[1].Value);
        var orc = Monster.EndMonster(builder);

        builder.Finish(orc.Value); // You could also call `Monster.FinishMonsterBuffer(builder, orc);`.

        // We now have a FlatBuffer that we could store on disk or send over a network.

        // ...Code to store to disk or send over a network goes here...

        // Instead, we are going to access it right away, as if we just received it.

        var buf = builder.DataBuffer;

        // Get access to the root:
        var monster = Monster.GetRootAsMonster(buf);

        // For C#, unlike other languages, most values (except for vectors and unions) are available as
        // properties instead of accessor methods.

        // Note: We did not set the `Mana` field explicitly, so we get back the default value.
        Assert(monster.Mana == 150, "monster.Mana", Convert.ToString(monster.Mana),
               Convert.ToString(150));
        Assert(monster.Hp == 300, "monster.Hp", Convert.ToString(monster.Hp), Convert.ToString(30));
        Assert(monster.Name.Equals("Orc", StringComparison.Ordinal), "monster.Name", monster.Name,
               "Orc");
        Assert(monster.Color == Color.Red, "monster.Color", Convert.ToString(monster.Color),
               Convert.ToString(Color.Red));

        var vec = monster.Pos.Value;

        Assert(vec.X == 1.0f, "vec.X",
               Convert.ToString(vec.X), Convert.ToString(1.0f));
        Assert(vec.Y == 2.0f, "vec.Y",
               Convert.ToString(vec.Y), Convert.ToString(2.0f));
        Assert(vec.Z == 3.0f, "vec.Z",
               Convert.ToString(vec.Z), Convert.ToString(3.0f));

        // Get and test the `Inventory` FlatBuffer `vector`.
        for (int i = 0; i < monster.InventoryLength; i++)
        {
            Assert(monster.Inventory(i) == i, "monster.Inventory",
                   Convert.ToString(monster.Inventory(i)), Convert.ToString(i));
        }

        // Get and test the `Weapons` FlatBuffer `vector` of `table`s.
        var expectedWeaponNames   = new string[] { "Sword", "Axe" };
        var expectedWeaponDamages = new short[] { 3, 5 };

        for (int i = 0; i < monster.WeaponsLength; i++)
        {
            Assert(monster.Weapons(i).Value.Name.Equals(expectedWeaponNames[i], StringComparison.Ordinal),
                   "monster.Weapons", monster.Weapons(i).Value.Name, expectedWeaponNames[i]);
            Assert(monster.Weapons(i).Value.Damage == expectedWeaponDamages[i], "monster.GetWeapons",
                   Convert.ToString(monster.Weapons(i).Value.Damage),
                   Convert.ToString(expectedWeaponDamages[i]));
        }

        // Get and test the `Equipped` FlatBuffer `union`.
        Assert(monster.EquippedType == Equipment.Weapon, "monster.EquippedType",
               Convert.ToString(monster.EquippedType), Convert.ToString(Equipment.Weapon));
        var equipped = monster.Equipped <Weapon>().Value;

        Assert(equipped.Name.Equals("Axe", StringComparison.Ordinal), "equipped.Name", equipped.Name,
               "Axe");
        Assert(equipped.Damage == 5, "equipped.Damage", Convert.ToString(equipped.Damage),
               Convert.ToString(5));

        Console.WriteLine("The FlatBuffer was successfully created and verified!");
    }