Skip to content

Commit

Permalink
[fix] replace all protobuf words and remove some warning.
Browse files Browse the repository at this point in the history
- remove downgrade warning for fbs code gen. use a global warning instead. write corresponding document to describe the issue related to generated fbs files.
- replace all protobuf words. use flatbuffer word instead.
  • Loading branch information
yyc12345 committed Jul 22, 2023
1 parent f4d7539 commit 7e6962d
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 79 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# bmmo-protocol-compiler

Yet Another Protobuf.
Yet Another Protobuf / Flatbuffers.
It just my exercise for Flex/Bison. It should not be used in any production environment, however I will keep it as stable as I can.
This compiler is written in pure C (I hate C++).

Expand All @@ -15,21 +15,21 @@ Imagine following circumstance, You have developed a new network application, yo

Congratulation! This project is very suit for you.

This project is originally served for [Swung0x48/BallanceMMO](https://github.com/Swung0x48/BallanceMMO), because I want to create a standalone protocol parser to erase the dependency to original BallanceMMO project. Original BallanceMMO protocol become more and more complex. It is hard to be written by hand, so I create this compiler. Now, I can get effective code in any language via just writing a tiny protocol prototype file like Google Protobuf proto file. However, it is okay that you want to use this project in any other applications, because what your demo is facing is usually the same as current BMMO's circumstance.
This project is originally served for [Swung0x48/BallanceMMO](https://github.com/Swung0x48/BallanceMMO), because I want to create a standalone protocol parser to erase the dependency to original BallanceMMO project. Original BallanceMMO protocol become more and more complex. It is hard to be written by hand, so I create this compiler. Now, I can get effective code in any language via just writing a tiny protocol prototype file like Google Flatbuffers fbs file. However, it is okay that you want to use this project in any other applications, because what your demo is facing is usually the same as current BMMO's circumstance.
Now, because the protocol of BMMO no longer follow some crucial syntax. This project is not focus on BMMO protocol anymore. However I don't want to change its name. I just want keep its name to show where it comes from.

Considering Google Protobuf, this project is not suit for production environment, just like I said previously. If your application become more and more strong and ready for release, I suggest you switch to more strong serializer, for example, Google Protobuf. This project also provide the function that convert bp file used by this program to proto file used by Google Protobuf. It is convenient for migration.
Considering Google Flatbuffers, this project is not suit for production environment, just like I said previously. If your application become more and more strong and ready for release, I suggest you switch to more strong serializer, for example, Google Flatbuffers. This project also provide the function that convert bp file used by this program to fbs file used by Google Flatbuffers. It is convenient for migration.

## Features

### Supported Language

* Python (at least 3.5.3)
* Python (at least 3.5.3 in theory. 3.7 higher suggested.)
* C\#
- Legacy Mode (For Unity / Godot user. Low performance.): at least .Net Framework 4.6. `unsafe` switch required.
- Modern Mode (High performance): at least .Net Core 2.1
* C++ (at least C++ 17. Optimized for C++ 20 and 23)
* Google Protobuf 3
* Google Flatbuffers

### Supported Features

Expand All @@ -55,7 +55,7 @@ Syntax: `bmmo_protocol_compiler [switches]`
* `-c --cs PATH`: Generate a C\# code file in `PATH`.
* `-d --cpp-header PATH`: Generate a C++ header file in `PATH`.
* `-s --cpp-source PATH`: Generate a C++ source file in `PATH`.
* `-t --proto PATH`: Generate a Proto3 file in `PATH`.
* `-b --flatbuffers PATH`: Generate a Flatbuffers file in `PATH`.

`-d` and `-s` both are usually used together to generate full C++ code file.
If there are no any generation switches chosen, the compiler will only check the syntax of the input file.
Expand Down
51 changes: 32 additions & 19 deletions docs/GenCode.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

Before starting your reading, please allow me introduce the aim of each generated languages. And, a small, arrogant and realistic warning for C++ code user.

Compiler supports 4 languages now, Python, C\#, C++ and Proto. Python code is generated for quick test, small demo and etc. Python code is not ready for production environment.
Compiler supports 4 languages now, Python, C\#, C++ and Flatbuffers. Python code is generated for quick test, small demo and etc. Python code is not ready for production environment.
Oppositely, C\# and C++ code is more suit for your productions. C\# code can be used in some game engine easily, such as Unity. C++ code can be applied to game server and etc.
Proto code generation is a migration way. As I said in readme, if you want to migrate to more stable binary protocol, use this output can let you feel more fluent about migration.
Flatbuffers code generation is a migration way. As I said in readme, if you want to migrate to more stable binary protocol, use this output can let you feel more fluent about migration.

You may notice the requirement of some laguages are extremely high. For example, we order C++17 for C++ and .Net Core 2.1 for C\#. That's because the feature we needed only provided in the version higher that our requested version. If you think these version are too high, you can create a fork and change the code on your requirement freely.

Expand All @@ -16,26 +16,26 @@ Following table introduce the data type conversion used by Bp Compiler.

### Basic Type

|Bp |Python |C\# |C++ |Proto |
|:--- |:--- |:--- |:--- |:--- |
|float |float |float |float |float |
|double |float |double |double |double |
|int8 |int |sbyte |int8_t |int32 |
|int16 |int |short |int16_t |int32 |
|int32 |int |int |int32_t |int32 |
|int64 |int |long |int64_t |int64 |
|uint8 |int |byte |uint8_t |uint32 |
|uint16 |int |ushort |uint16_t |uint32 |
|uint32 |int |uint |uint32_t |uint32 |
|uint64 |int |ulong |uint64_t |uint64 |
|string |str |string |std::string |string |
|Bp |Python |C\# |C++ |Flatbuffers|
|:--- |:--- |:--- |:--- |:--- |
|float |float |float |float |float |
|double |float |double |double |double |
|int8 |int |sbyte |int8_t |byte |
|int16 |int |short |int16_t |short |
|int32 |int |int |int32_t |int |
|int64 |int |long |int64_t |long |
|uint8 |int |byte |uint8_t |ubyte |
|uint16 |int |ushort |uint16_t |ushort |
|uint32 |int |uint |uint32_t |uint |
|uint64 |int |ulong |uint64_t |ulong |
|string |str |string |std::string |string |

## Container

|Bp |Python |C\# |C++ |Proto |
|:--- |:--- |:--- |:--- |:--- |
|tuple |list[T] |T[] |CStyleArray\<T, N\>|repeated |
|list |list[T] |System.Collections.Generic.List\<T\> |std::vector\<T\> |repeated |
|Bp |Python |C\# |C++ |Flatbuffers |
|:--- |:--- |:--- |:--- |:--- |
|tuple |list[T] |T[] |CStyleArray\<T, N\>|[T:N] (Array) |
|list |list[T] |System.Collections.Generic.List\<T\> |std::vector\<T\> |[T] (Vector) |

Tips:

Expand Down Expand Up @@ -249,3 +249,16 @@ delete your_data;
0. Send gotten binary sequence via stream or anything you like.
0. Clear buffer like deserialization.
0. Free your initialized message in anytime you want.

## Flatbuffers

The generation of Flatbuffers just interpret the BP file's entries one by one, because Flatbuffers is served for migration.
Some essential properies and features may be ignored during generation.
There is a list containing what you should notice during conversion.

* All align number and padding will be wiped out.
* Natural and narrow keywords are ignored.
* Reliability is ignored.
* `struct` will be translate to `struct` and `msg` will be translated to `table`.
* Compiler do not check field type legality during conversion. For example, Flatbuffers `table` do not accept static array, but we still write it if you specify a static array in BP file's `msg`.
* No mechanism to ensure the geenerated fbs can pass compiling without any modifications.
59 changes: 5 additions & 54 deletions src/codegen/codefbs.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,41 +35,6 @@ static void write_enum(FILE* fs, BPCSMTV_ENUM* smtv_enum) {

}

static bool has_static_array(GSList* bond_vars) {
GSList* cursor = NULL;
for (cursor = bond_vars; cursor != NULL; cursor = cursor->next) {
BOND_VARS* data = (BOND_VARS*)cursor->data;
uint32_t c;
for (c = 0; c < data->bond_vars_len; ++c) {
switch (data->vars_type[c]) {
case BPCGEN_VARTYPE_STATIC_PRIMITIVE:
case BPCGEN_VARTYPE_STATIC_STRING:
case BPCGEN_VARTYPE_STATIC_NARROW:
case BPCGEN_VARTYPE_STATIC_NATURAL:
{
return true; // has static array
}
case BPCGEN_VARTYPE_SINGLE_PRIMITIVE:
case BPCGEN_VARTYPE_SINGLE_STRING:
case BPCGEN_VARTYPE_SINGLE_NARROW:
case BPCGEN_VARTYPE_SINGLE_NATURAL:
case BPCGEN_VARTYPE_DYNAMIC_PRIMITIVE:
case BPCGEN_VARTYPE_DYNAMIC_STRING:
case BPCGEN_VARTYPE_DYNAMIC_NARROW:
case BPCGEN_VARTYPE_DYNAMIC_NATURAL:
{
break;
}

default:
g_assert_not_reached();
}
}
}

return false; // do not have static array
}

static void write_struct_or_msg(FILE* fs, BPCGEN_STRUCT_LIKE* union_data) {
GSList* cursor = NULL;
bool is_msg; GSList* variables; char* struct_like_name;
Expand All @@ -80,25 +45,9 @@ static void write_struct_or_msg(FILE* fs, BPCGEN_STRUCT_LIKE* union_data) {
GSList* bond_vars = bpcgen_constructor_bond_vars(variables, BPCGEN_VARTYPE_NONE);

// message body
// decide table or struct
BPCGEN_INDENT_PRINT;
if (is_msg) {
if (has_static_array(bond_vars)) {
// downgrade to struct
bpcerr_codegen_warning(BPCERR_ERROR_CODEGEN_SOURCE_FLATBUFFERS,
"Downgrade \"%s\" from table to struct because flatbuffers do not support array in table.",
struct_like_name
);
fputs("struct", fs);
} else {
// still use table
fputs("table", fs);
}
} else {
// use struct in default
fputs("struct", fs);
}
// write name and bracket
if (is_msg) { fputs("table", fs); }
else { fputs("struct", fs); }
fprintf(fs, " %s {", struct_like_name); BPCGEN_INDENT_INC;

for (cursor = bond_vars; cursor != NULL; cursor = cursor->next) {
Expand Down Expand Up @@ -156,7 +105,9 @@ void codefbs_write_document(FILE* fs, BPCSMTV_DOCUMENT* document) {

// raise general warning for fbs
bpcerr_codegen_warning(BPCERR_ERROR_CODEGEN_SOURCE_FLATBUFFERS,
"Flatbuffers output is designed for migration. Some properties may be ignored in migration, such as padding and align."
"Flatbuffers output is designed for migration. "
"Some properties may be ignored in migration, such as padding and align. "
"The generated schema also may contain issues, such as table and struct restrictions."
);

// write namespace
Expand Down

0 comments on commit 7e6962d

Please sign in to comment.