diff --git a/README.md b/README.md index 617bc229..ffd38415 100644 --- a/README.md +++ b/README.md @@ -148,29 +148,9 @@ Actor {ID: 104 Name: "邋遢大王" Struct {HP: 10 AttackRate: 1} BuffID: 0 Buff ``` -# 导出格式 -默认导出pbt, 通过参数可以导出lua, json格式 -* --fmt=pbt - 默认导出pbt -* --fmt=lua - 导出以return开头的lua文件, 通过require的返回值回去表格table - 兼容pbc格式, 枚举值以值名字字符串导出, 64位以字符串方式导出 -* --fmt=json - 标准json, 与proto定义的结构导出json相同 - -# Protobuf Text格式(pbt) -Google Protobuf 官方支持的格式 -可通过官方protobuf库读取,写入这种格式 -与json区别在于: -json的字段名必须是带双引号, 且数组需要用[]圈住, 多重字段尾部必须加逗号 -* 格式 - key1: value1 key2: value2 - 冒号组合key,value, 空格分隔字段 - - \#作为注释 - # Proto文件规则 proto文件格式范例参考test/test.proto + 需要配合github.com/davyxu/pbmeta的protobuf插件protoc-gen-meta导出proto文件的meta信息 在proto的字段后方的注释中以[tabtoy]开头的注释将被理解为meta信息, 用于描述字段导出功能修饰 @@ -184,8 +164,49 @@ proto文件格式范例参考test/test.proto https://github.com/davyxu/tabtoy/blob/master/test/test.proto +# 导出格式 +默认导出pbt, 通过参数可以导出lua, json格式 + +## Protobuf 文本格式(*.pbt) + 参数: --fmt=pbt + + Google Protobuf 官方支持的格式 + + 可通过官方protobuf库读取,写入这种格式 + + 与json区别在于: + + json的字段名必须是带双引号, 且数组需要用[]圈住, 多重字段尾部必须加逗号 + + * 格式 + key1: value1 key2: value2 + 冒号组合key,value, 空格分隔字段 + + \#作为注释 + + 默认导出pbt + + +## Lua格式(*.lua) + 参数: --fmt=lua + 导出以return开头的lua文件, 通过require的返回值回去表格table + 兼容pbc格式, 枚举值以值名字字符串导出, 64位以字符串方式导出 + +### lua导出字段的索引 + + 为了方便lua导出文件的使用, 可以创建字段索引, 步骤如下, 参考test/test.proto: + + * 为ActorDefine消息的ID字段和Name字段增加描述LuaMapper: true + +## json格式(*.json) + 参数: --fmt=json + 标准json, 与proto定义的结构导出json相同 + + + -# 电子表格文件头格式 + +# 电子表格格式及写法 ## 导出头 @@ -242,6 +263,17 @@ Proto字段列, 必须放在第二行 支持在Struct中的字段添加Alias别名以使用别名方式的字段或中文字段描述单元格 +范例步骤: 请参考test/test.proto + +* 为Prop消息的需要的字段增加描述 AttackRate 为Alias:"攻击速率" ExType为Alias:"额外类型" 等 + +* 确认StrStruct字段拥有描述String2Struct: true + +* 在StrStruct的单元格里填写 血量: 3 攻击速率: 1 额外类型:超能 + +* 导出测试 + + ## 字段重复性检查 格式: RepeatCheck: true diff --git a/filter/val2struct.go b/filter/val2struct.go index 501f98d2..27e70418 100644 --- a/filter/val2struct.go +++ b/filter/val2struct.go @@ -142,7 +142,6 @@ func Value2Struct(meta *tool.FieldMeta, structValue string, fd *pbmeta.FieldDesc p.NextToken() - log.Debugln("end of term") } return diff --git a/main.go b/main.go index 6fd9b3ff..9d7c126e 100644 --- a/main.go +++ b/main.go @@ -43,7 +43,7 @@ func main() { // 版本 if *paramVersion { - fmt.Println("tabtoy 1.1.1") + fmt.Println("tabtoy 1.2.0") return } diff --git a/mode_xls2pbt.go b/mode_xls2pbt.go index dfbcf237..17fb6f43 100644 --- a/mode_xls2pbt.go +++ b/mode_xls2pbt.go @@ -175,7 +175,7 @@ func setFieldValue(ri *scanner.RecordInfo, fieldName, value string) bool { type sheetData struct { name string - msg *data.DynamicMessage + msg *data.DynamicMessage // 对应XXFile } func getOutputExt() string { diff --git a/printer/lua.go b/printer/lua.go index ffcb17f2..ab5b8410 100644 --- a/printer/lua.go +++ b/printer/lua.go @@ -73,12 +73,73 @@ func (self *luaWriter) WriteFieldSpliter() { self.printer.WriteString(", ") } +// msg类型=XXFile func (self *luaWriter) PrintMessage(msg *data.DynamicMessage) { - self.printer.WriteString("return {\n\n") + self.printer.WriteString("local data = {\n\n") + rawWriteMessage(self.printer, self, msg, 0) - self.printer.WriteString("\n\n}") + self.printer.WriteString("\n\n}\n") + + /* + + data.ActorByID = {} + for _, rec in pairs( data.Actor ) do + + data.ActorByID[rec.ID] = rec + + end + + */ + + // 输出lua索引 + fdset, lineFieldName := findMapperField(msg) + + for _, fd := range fdset { + + mapperVarName := fmt.Sprintf("data.%sBy%s", lineFieldName, fd.Name()) + + self.printer.WriteString("\n-- " + fd.Name() + "\n") + self.printer.WriteString(mapperVarName + " = {}\n") + self.printer.WriteString("for _, rec in pairs(data." + lineFieldName + ") do\n") + self.printer.WriteString("\t" + mapperVarName + "[rec." + fd.Name() + "] = rec\n") + self.printer.WriteString("end\n") + } + + self.printer.WriteString("\nreturn data") +} + +func findMapperField(msg *data.DynamicMessage) (fdset []*pbmeta.FieldDescriptor, lineFieldName string) { + + var lineMsgDesc *pbmeta.Descriptor + // 找到行描述符 + for i := 0; i < msg.Desc.FieldCount(); i++ { + fd := msg.Desc.Field(i) + + if fd.IsRepeated() { + lineMsgDesc = fd.MessageDesc() + lineFieldName = fd.Name() + break + } + } + + // 在结构中寻找需要导出的lua字段 + for i := 0; i < lineMsgDesc.FieldCount(); i++ { + fd := lineMsgDesc.Field(i) + meta := data.GetFieldMeta(fd) + if meta == nil { + continue + } + + if !meta.LuaMapper { + continue + } + + fdset = append(fdset, fd) + } + + return } func NewLuaWriter(printer *bytes.Buffer) IWriter { diff --git a/proto/tool.proto b/proto/tool.proto index 574540b9..1142bc5c 100644 --- a/proto/tool.proto +++ b/proto/tool.proto @@ -25,4 +25,6 @@ message FieldMeta bool String2Struct = 5; // 字符串转为结构体 string DefaultValue = 6; // 默认值, 比pb2的默认值有更高的优先度 + + bool LuaMapper = 7; // 为字段创建lua记录索引 } diff --git a/proto/tool/tool.pb.go b/proto/tool/tool.pb.go index 6472d0a7..a817e2f2 100644 --- a/proto/tool/tool.pb.go +++ b/proto/tool/tool.pb.go @@ -46,6 +46,7 @@ type FieldMeta struct { Alias string `protobuf:"bytes,4,opt,name=Alias" json:"Alias,omitempty"` String2Struct bool `protobuf:"varint,5,opt,name=String2Struct" json:"String2Struct,omitempty"` DefaultValue string `protobuf:"bytes,6,opt,name=DefaultValue" json:"DefaultValue,omitempty"` + LuaMapper bool `protobuf:"varint,7,opt,name=LuaMapper" json:"LuaMapper,omitempty"` } func (m *FieldMeta) Reset() { *m = FieldMeta{} } @@ -59,20 +60,21 @@ func init() { } var fileDescriptor0 = []byte{ - // 237 bytes of a gzipped FileDescriptorProto + // 252 bytes of a gzipped FileDescriptorProto 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0x64, 0x8f, 0xbf, 0x4e, 0xc3, 0x30, - 0x10, 0xc6, 0x15, 0x68, 0x2a, 0x72, 0x4d, 0x96, 0x13, 0x83, 0x07, 0x86, 0x2a, 0x62, 0x60, 0xea, - 0x00, 0x4f, 0x80, 0x80, 0x8a, 0x01, 0x10, 0x4a, 0x11, 0xfb, 0xd1, 0x1c, 0x60, 0x61, 0xb0, 0xe5, - 0x5c, 0xf8, 0xf3, 0x70, 0xbc, 0x1b, 0xf8, 0x28, 0xc2, 0x12, 0x93, 0xed, 0xdf, 0x7d, 0xfe, 0x4e, - 0x3f, 0x00, 0xf1, 0xde, 0x2d, 0x42, 0xf4, 0xe2, 0x71, 0x92, 0xee, 0xed, 0x2b, 0xd4, 0x67, 0xef, - 0xc1, 0x47, 0x39, 0x67, 0xea, 0x39, 0xe2, 0x3e, 0x34, 0xd7, 0x69, 0x7c, 0xf3, 0x11, 0xf8, 0x8a, - 0x9e, 0xd9, 0x14, 0xf3, 0xe2, 0xa0, 0xea, 0x9a, 0x90, 0x43, 0x6c, 0xa1, 0xee, 0xfc, 0xdb, 0xd2, - 0xb2, 0xeb, 0x35, 0xb4, 0xa5, 0xa1, 0x3a, 0x66, 0x0c, 0xf7, 0xa0, 0xd2, 0xa6, 0xa5, 0x75, 0x6c, - 0xb6, 0x35, 0x50, 0x85, 0x5f, 0xd0, 0x7e, 0x16, 0x50, 0x69, 0xf6, 0x92, 0x85, 0x70,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d, 0x4e, 0x55, 0x5d, 0x86, 0x21, 0xd4, 0x15, 0x14, 0xb5, 0xff, 0x9c, + 0x5b, 0x72, 0x8d, 0x84, 0xf6, 0x24, 0x54, 0xc4, 0x01, 0xd3, 0xc7, 0xa0, 0xa4, 0x69, 0x6e, 0x1d, + 0x99, 0x7d, 0x09, 0xa8, 0xd0, 0x83, 0xea, 0x27, 0x03, 0x25, 0xd9, 0x25, 0x31, 0xea, 0x19, 0xe8, + 0x15, 0x47, 0xfb, 0xfe, 0x7c, 0xb6, 0xb0, 0x2d, 0xaf, 0x82, 0xb3, 0x4c, 0x71, 0xdb, 0xaa, 0xdb, + 0x9d, 0x89, 0x9e, 0xc2, 0xa4, 0xa6, 0x40, 0xc8, 0x97, 0x2f, 0xb4, 0x7e, 0x95, 0xf6, 0xc3, 0x7a, + 0x12, 0xff, 0x91, 0x3e, 0x82, 0xfc, 0xc2, 0x59, 0x6c, 0xcd, 0x48, 0x4a, 0x72, 0x4c, 0x87, 0x64, + 0xb7, 0x7d, 0x67, 0xb3, 0x74, 0x6b, 0x36, 0xb9, 0xdc, 0x2c, 0xdb, 0x21, 0x4c, 0x76, 0x57, 0xf4, + 0x84, 0x9d, 0xe3, 0x07, 0x74, 0x1d, 0x99, 0xf1, 0x9f, 0x5d, 0x33, 0x60, 0xc9, 0x6e, 0xd1, 0xe1, + 0x12, 0x43, 0xd8, 0x7c, 0xf4, 0x40, 0x5a, 0x94, 0xeb, 0xc1, 0xe3, 0x58, 0x44, 0xcf, 0x7f, 0x03, + 0x00, 0x00, 0xff, 0xff, 0x98, 0xee, 0x61, 0xe6, 0x70, 0x01, 0x00, 0x00, } diff --git a/scanner/sheet.go b/scanner/sheet.go index e4ad8e53..c3e4a3b4 100644 --- a/scanner/sheet.go +++ b/scanner/sheet.go @@ -6,7 +6,6 @@ import ( "github.com/davyxu/pbmeta" pbprotos "github.com/davyxu/pbmeta/proto" "github.com/davyxu/tabtoy/data" - "github.com/davyxu/tabtoy/proto/filter" "github.com/davyxu/tabtoy/proto/tool" "github.com/tealeg/xlsx" ) diff --git a/test/Actor.json b/test/Actor.json index f46f484c..4355eb29 100644 --- a/test/Actor.json +++ b/test/Actor.json @@ -3,7 +3,7 @@ "Actor" : [ {"ID": 100, "Name": "黑猫警长", "Struct" : {"HP": 100, "AttackRate": 0.6}, "BuffID" : [0, 0], "SkillID" : [4, 6, 7], "StrStruct" : []}, {"ID": 101, "Name": "葫芦\n娃", "Struct" : {"HP": 10, "AttackRate": 0.8}, "BuffID" : [3, 1], "Type": 21, "SkillID" : [1], "StrStruct" : []}, -{"ID": 102, "Name": "舒\"克\"", "Struct" : {"HP": 10, "AttackRate": 0.7}, "BuffID" : [0, 0], "SkillID" : [0], "StrStruct" : [{"HP": 2}, {"AttackRate": 0.5}, {"HP": 3, "AttackRate": 1}]}, +{"ID": 102, "Name": "舒\"克\"", "Struct" : {"HP": 10, "AttackRate": 0.7}, "BuffID" : [0, 0], "SkillID" : [0], "StrStruct" : [{"HP": 2}, {"AttackRate": 0.5}, {"HP": 3, "AttackRate": 1, "ExType": 21}]}, {"ID": 103, "Name": "贝\n塔", "Struct" : {"HP": 205}, "BuffID" : [0, 0], "SkillID" : [0], "StrStruct" : []}, {"ID": 104, "Name": "邋遢大王", "Struct" : {"HP": 10, "AttackRate": 1}, "BuffID" : [0, 0], "SkillID" : [0], "StrStruct" : []} ] diff --git a/test/Actor.lua b/test/Actor.lua index 6ffc530c..a9808d71 100644 --- a/test/Actor.lua +++ b/test/Actor.lua @@ -1,12 +1,26 @@ -- Generated by github.com/davyxu/tabtoy -return { +local data = { Actor = { {ID = "100", Name = "黑猫警长", Struct = {HP = 100, AttackRate = 0.6}, BuffID = 0, BuffID = 0, SkillID = 4, SkillID = 6, SkillID = 7, StrStruct = {{}, {}, {}}}, {ID = "101", Name = "葫芦\n娃", Struct = {HP = 10, AttackRate = 0.8}, BuffID = 3, BuffID = 1, Type = "Power", SkillID = 1, StrStruct = {{}, {}, {}}}, -{ID = "102", Name = "舒\"克\"", Struct = {HP = 10, AttackRate = 0.7}, BuffID = 0, BuffID = 0, SkillID = 0, StrStruct = {{HP = 2}, {AttackRate = 0.5}, {HP = 3, AttackRate = 1}}}, +{ID = "102", Name = "舒\"克\"", Struct = {HP = 10, AttackRate = 0.7}, BuffID = 0, BuffID = 0, SkillID = 0, StrStruct = {{HP = 2}, {AttackRate = 0.5}, {HP = 3, AttackRate = 1, ExType = "Power"}}}, {ID = "103", Name = "贝\n塔", Struct = {HP = 205}, BuffID = 0, BuffID = 0, SkillID = 0, StrStruct = {{}, {}, {}}}, {ID = "104", Name = "邋遢大王", Struct = {HP = 10, AttackRate = 1}, BuffID = 0, BuffID = 0, SkillID = 0, StrStruct = {{}, {}, {}}} } -} \ No newline at end of file +} + +-- ID +data.ActorByID = {} +for _, rec in pairs(data.Actor) do + data.ActorByID[rec.ID] = rec +end + +-- Name +data.ActorByName = {} +for _, rec in pairs(data.Actor) do + data.ActorByName[rec.Name] = rec +end + +return data \ No newline at end of file diff --git a/test/Actor.pbt b/test/Actor.pbt index b42fc957..a779db54 100644 --- a/test/Actor.pbt +++ b/test/Actor.pbt @@ -1,6 +1,6 @@ # Generated by github.com/davyxu/tabtoy Actor {ID: 100 Name: "黑猫警长" Struct {HP: 100 AttackRate: 0.6} BuffID: 0 BuffID: 0 SkillID: 4 SkillID: 6 SkillID: 7 } Actor {ID: 101 Name: "葫芦\n娃" Struct {HP: 10 AttackRate: 0.8} BuffID: 3 BuffID: 1 Type: Power SkillID: 1 } -Actor {ID: 102 Name: "舒\"克\"" Struct {HP: 10 AttackRate: 0.7} BuffID: 0 BuffID: 0 SkillID: 0 StrStruct {HP: 2} StrStruct {AttackRate: 0.5} StrStruct {HP: 3 AttackRate: 1}} +Actor {ID: 102 Name: "舒\"克\"" Struct {HP: 10 AttackRate: 0.7} BuffID: 0 BuffID: 0 SkillID: 0 StrStruct {HP: 2} StrStruct {AttackRate: 0.5} StrStruct {HP: 3 AttackRate: 1 ExType: Power}} Actor {ID: 103 Name: "贝\n塔" Struct {HP: 205} BuffID: 0 BuffID: 0 SkillID: 0 } Actor {ID: 104 Name: "邋遢大王" Struct {HP: 10 AttackRate: 1} BuffID: 0 BuffID: 0 SkillID: 0 } diff --git a/test/Actor.xlsx b/test/Actor.xlsx index c347d601..46e51e4a 100644 Binary files a/test/Actor.xlsx and b/test/Actor.xlsx differ diff --git a/test/test.lua b/test/test.lua new file mode 100644 index 00000000..31d5b4a3 --- /dev/null +++ b/test/test.lua @@ -0,0 +1,5 @@ +local t = require "Actor" + +print(t.ActorByID["103"].ID) + +print(t.ActorByName["黑猫警长"].ID) \ No newline at end of file diff --git a/test/test.proto b/test/test.proto index fd8d5b0b..9d554d37 100644 --- a/test/test.proto +++ b/test/test.proto @@ -20,10 +20,10 @@ message Prop message ActorDefine { // 唯一ID - int64 ID = 1; // [tabtoy] RepeatCheck: true #ID重复检查 + int64 ID = 1; // [tabtoy] RepeatCheck: true LuaMapper: true #ID重复检查 及lua索引 // 角色名称 - string Name = 5; + string Name = 5; // [tabtoy] LuaMapper: true Prop Struct = 10;