Home Excel转Protobuf/Json通用配置文件
Post
Cancel

Excel转Protobuf/Json通用配置文件

使用场景

最近工作流中有将Excel转Protobuf作为配置文件的技术方案。具体实现是先定一个proto文件,再在一个对应excel表中定义对应字段,由策划在excel进行更改。proto文件可以生成对应语言的脚本,然后将excel转成对应protobuf的binary。

我的想法就是优化掉自定义proto文件的步骤,根据约束在excel中定义数据类型,在导出数据前,自动导出对应的proto文件,以及生成对应的脚本文件。

Excel定义约束

Excel已定义的关键词,以下的所有的配置都可以放在任何sheet里面

关键词描述
#message同protobuf message
#enum同protobuf enum
#package同protobuf package
#config标识当前message导出为config
#desc描述/注释
#type数据类型, 基本同Protobuf类型,有自定义
#var变量名称
#value枚举的变量

message

基本的Message结构如下,RarityType参考下方的枚举定义,支持map和list,list同protobuf repeated

#messageCard-----
#typeint32stringboolRarityTypemap#string:string#sep=,list#string#sep=,
#varidnamehideraitysoundsanimations
#desc唯一标识名称隐藏罕见程度音效动画
-10001名称01false2apply:aa.ogg,walk:bb.oggrun,attack,idle
-10002名称02false1apply:aa.ogg,walk:bb.oggrun,attack,idle

enmu

枚举的变量赋值必须从0开始,受限于protobuf的限制

#enmuRarityType--
#desc罕见程度--
-#var#desc#value
-Basic基础0
-Common普通1
-Rare稀有2
-Epic史诗3
-Legendary传说4
-Fixed固定5

config

#config是加在#message上方的表示,后面表格的内容为当前的配置名称

#configcollect
#messageCollect
#typelist#Card
#varCardList
#desc列表

type

基本类型同protobuf的基本类型,比如int32、string等,list、map参考上方的message示例。如果是引用其他的类型结构,直接添加对应的类型名称即可, 具体的数据读取对应类型定义下方填写的数据,参考上方的config示例。

工具实现

都需要安装.net6或者以上的环境,工具在windows下可以直接调用Excel2Config执行,linux/mac环境,可以调用dotnet Excel2Config.dll --help

Excel2Config–help   
–helpShow this text.   
–versionShow version info. 0.1.0.   
–excel_path=The path to the excel file or folder.   
–recursive,-RTraverse all the subfolders of the excel folder.   
–output_path=Setting the output directory. If it is not set, it is the folder path of excel.   
–to_jsonConvert to a json configuration file.   
–to_protobuf=Convert to a protobuf configuration file. Input parameter prototextprotobinaryprotoall, all is recommended.
–protoc=Set the path to the protoc execution file.Environment variables are used by default protoc.   
–shell=Set the path to the shell execution file.Environment variables are used by default sh.   
–protoc_cmd=By default, the output file path of proto is set, and other protoc commands that need to be executed are added.   

使用示例

1
Excel2Config --excel_path=Excel/ --to_json --to_protobuf=all --protoc_cmd="--csharp_out=Excel/" --shell="C:\\Program Files\\Git\\bin\\bash.exe" --protoc="D:\\protoc.exe"

Excel配置

MessageEnmuConfig

导出文件

对应的protobuf的binary、proto文件、c#脚本,以及json配置文件

导出文件展示jsonproto

夹带私货

  • 虽然支持json导出,但是还是推荐导出protobuf作为配置使用,json只是作为可视化参考
  • 有json可以利用其他工具转成yaml等配置文件
  • 这里利用shell环境去调用protoc的命令,即使在windows下也需要设置shell环境,开发都安装了git-bash环境,所以这里的shell环境也不是啥大问题。windows下的路径确实有点恶心。

ToDo

  • 需要支持配置大文件的分割,并使用同一个结构脚本
This post is licensed under CC BY 4.0 by the author.

实现轻量级Git管理软件

编译protobuf的WASM库