Skip to content

boyism80/flatbuffer-ex

Repository files navigation

FlatbufferEx

Introduce

FlatbufferEx helps to use flatbuffer simply.
support nullable type, declare multiple tables in one file, no more required flatbuffer builder to create DTO.
this repository is used in my 2d mmorpg game server project. if you want to see, visit my fb project.

only support c++ and c#

How to work?

FlatbufferEx re-generates fbs files and create new (header)file for access flatbuffer object simply.
you can see the wrapping file.

Declare flatbuffer files

// db.fbs

namespace fb.protocol.db;

// you can declare multiple tables but root_type can be only one.
// but here, all your tables are root_type.
// don't set each tables as root_type. its automatically set as root_type.
table Character {
    ...
	name: string;
	weapon_color: ubyte?; // you can make field as nullable type
	armor_color: ubyte?;
    ...
}

table Item {
	model: uint;
	durability: uint?;
	custom_name: string?;
    ...
}

table Spell {
	...
}
// db.response.fbs

include "db.fbs";
namespace fb.protocol.db.response;

table Login {
  character: fb.protocol.db.Character;
  items: [fb.protocol.db.Item];
  spells: [fb.protocol.db.Spell];
}

Use in C++

// main.cpp

#include <protocol.h> // header file generated by flatbuffer-ex

int main(int argc, const char** argv)
{
    auto character = fb::protocol::db::Character{};
    character.name = "character";
    character.weapon_color = std::nullopt;
    character.armor_color = 1;

    auto item1 = fb::protocol::db::Item{};
    item1.model = 1;
    item1.durability = std::nullopt;

    auto item2 = fb::protocol::db::Item{};
    item2.model = 2;
    item2.durability = 100;

    auto response = fb::protocol::db::response::Login
    {
        character,
        { item1, item2 }, 
        {}
    };

    auto bytes = response.Serialize();
    auto deserialized = fb::protocol::db::response::Login::Deserialize(bytes.data());

    assert(deserialized.character.name == "character");
    assert(deserialized.character.weapon_color == std::nullopt);
    assert(deserialized.character.armor_color == 1);

    assert(deserialized.items.size() == 2);
    assert(deserialized.items[0].model == 1);
    assert(deserialized.items[0].durability == std::nullopt);
    assert(deserialized.items[1].model == 2);
    assert(deserialized.items[1].durability == 100);

    assert(deserialized.spells.size() == 0);

    return 0;
}

Use in C#

// Program.cs

using fb.protocol.db;
using System.Diagnostics;

var response = new fb.protocol.db.response.Login
{ 
    Character = new Character
    { 
        Name = "character",
        WeaponColor = null,
        ArmorColor = 1,
    },
    Items = new List<Item>
    { 
        new Item
        { 
            Model = 1,
            Durability = null
        },
        new Item
        { 
            Model = 2,
            Durability = 100
        }
    }
};

var bytes = response.Serialize();
var deserialized = fb.protocol.db.response.Login.Deserialize(bytes);

Debug.Assert(deserialized.Character.Name == "character");
Debug.Assert(deserialized.Character.WeaponColor == null);
Debug.Assert(deserialized.Character.ArmorColor == 1);

Debug.Assert(deserialized.Items.Count == 2);
Debug.Assert(deserialized.Items[0].Model == 1);
Debug.Assert(deserialized.Items[0].Durability == null);
Debug.Assert(deserialized.Items[1].Model == 2);
Debug.Assert(deserialized.Items[1].Durability == 100);

Debug.Assert(deserialized.Spells.Count == 0);

About

flatbuffer wrapper

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages