Skip to content

Commit

Permalink
增加: lua格式的字段索引功能
Browse files Browse the repository at this point in the history
  • Loading branch information
davyxu committed Aug 15, 2016
1 parent 39c10eb commit 983be43
Show file tree
Hide file tree
Showing 14 changed files with 164 additions and 50 deletions.
76 changes: 54 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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信息, 用于描述字段导出功能修饰
Expand All @@ -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相同




# 电子表格文件头格式

# 电子表格格式及写法

## 导出头

Expand Down Expand Up @@ -242,6 +263,17 @@ Proto字段列, 必须放在第二行

支持在Struct中的字段添加Alias别名以使用别名方式的字段或中文字段描述单元格

范例步骤: 请参考test/test.proto

* 为Prop消息的需要的字段增加描述 AttackRate 为Alias:"攻击速率" ExType为Alias:"额外类型" 等

* 确认StrStruct字段拥有描述String2Struct: true

* 在StrStruct的单元格里填写 血量: 3 攻击速率: 1 额外类型:超能

* 导出测试


## 字段重复性检查

格式: RepeatCheck: true
Expand Down
1 change: 0 additions & 1 deletion filter/val2struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@ func Value2Struct(meta *tool.FieldMeta, structValue string, fd *pbmeta.FieldDesc

p.NextToken()

log.Debugln("end of term")
}

return
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func main() {

// 版本
if *paramVersion {
fmt.Println("tabtoy 1.1.1")
fmt.Println("tabtoy 1.2.0")
return
}

Expand Down
2 changes: 1 addition & 1 deletion mode_xls2pbt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
65 changes: 63 additions & 2 deletions printer/lua.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions proto/tool.proto
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,6 @@ message FieldMeta
bool String2Struct = 5; // 字符串转为结构体

string DefaultValue = 6; // 默认值, 比pb2的默认值有更高的优先度

bool LuaMapper = 7; // 为字段创建lua记录索引
}
32 changes: 17 additions & 15 deletions proto/tool/tool.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion scanner/sheet.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down
2 changes: 1 addition & 1 deletion test/Actor.json
Original file line number Diff line number Diff line change
Expand Up @@ -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" : []}
]
Expand Down
20 changes: 17 additions & 3 deletions test/Actor.lua
Original file line number Diff line number Diff line change
@@ -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 = {{}, {}, {}}}
}

}
}

-- 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
2 changes: 1 addition & 1 deletion test/Actor.pbt
Original file line number Diff line number Diff line change
@@ -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 }
Binary file modified test/Actor.xlsx
Binary file not shown.
5 changes: 5 additions & 0 deletions test/test.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
local t = require "Actor"

print(t.ActorByID["103"].ID)

print(t.ActorByName["黑猫警长"].ID)
4 changes: 2 additions & 2 deletions test/test.proto
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit 983be43

Please sign in to comment.