6 for name, fields in spairs(parse_spec("client/enum")) do
7 local camel = camel_case(name)
8 funcs = funcs .. "func Push" .. camel .. "(l *lua.LState, val mt." .. camel .. ") lua.LValue {\n\tswitch val {\n"
10 for _, var in ipairs(fields) do
11 funcs = funcs .. "\tcase mt." .. apply_prefix(fields, var) .. ":\n\t\t" ..
12 (var == "no" and "return lua.LNil" or "return lua.LString(\"" .. var .. "\")") .. "\n"
15 funcs = funcs .. "\t}\n\tpanic(\"impossible\")\n\treturn lua.LNil\n}\n\n"
18 for name, fields in spairs(parse_spec("client/flag")) do
19 local camel = camel_case(name)
20 funcs = funcs .. "func Push" .. camel .. "(l *lua.LState, val mt." .. camel .. ") lua.LValue {\n\ttbl := l.NewTable()\n"
22 for _, var in ipairs(fields) do
23 funcs = funcs .. "\tl.SetField(tbl, \"" .. var .. "\", lua.LBool(val&mt." .. apply_prefix(fields, var) .. " != 0))\n"
26 funcs = funcs .. "\treturn tbl\n}\n\n"
30 string = "lua.LString(string(VAL))",
31 fixed_string = "lua.LString(string(VAL[:]))",
32 boolean = "lua.LBool(VAL)",
33 number = "lua.LNumber(VAL)",
34 vec2 = "PushVec2(l, [2]lua.LNumber{lua.LNumber(VAL[0]), lua.LNumber(VAL[1])})",
35 vec3 = "PushVec3(l, [3]lua.LNumber{lua.LNumber(VAL[0]), lua.LNumber(VAL[1]), lua.LNumber(VAL[2])})",
36 box1 = "PushBox1(l, [2]lua.LNumber{lua.LNumber(VAL[0]), lua.LNumber(VAL[1])})",
37 box2 = "PushBox2(l, [2][2]lua.LNumber{{lua.LNumber(VAL[0][0]), lua.LNumber(VAL[0][1])}, {lua.LNumber(VAL[1][0]), lua.LNumber(VAL[1][1])}})",
38 box3 = "PushBox3(l, [2][3]lua.LNumber{{lua.LNumber(VAL[0][0]), lua.LNumber(VAL[0][1]), lua.LNumber(VAL[0][2])}, {lua.LNumber(VAL[1][0]), lua.LNumber(VAL[1][1]), lua.LNumber(VAL[1][2])}})",
41 local function fields_tolua(fields, indent)
44 for name, type in spairs(fields) do
45 local c = name:sub(1, 1)
46 if c ~= "{" and c ~= "%" then
47 local camel = "val." .. camel_case(name)
50 local condition = fields["{" .. name .. "}"]
51 local typeparam = fields["%" .. name .. "%"]
54 impl = impl .. indent .. "if " .. condition .. " {\n"
58 impl = impl .. idt .. "l.SetField(tbl, \"" .. name .. "\", "
60 impl = impl .. tolua[type]:gsub("VAL", camel)
62 impl = impl .. "Push" .. camel_case(type)
63 .. (typeparam and "[" .. typeparam .. "]" or "")
64 .. "(l, " .. camel .. ")"
69 impl = impl .. indent .. "}\n"
77 for name, fields in spairs(parse_spec("client/struct", true)) do
78 local camel = camel_case(name)
80 .. "func Push" .. camel .. "(l *lua.LState, val mt." .. camel .. ") lua.LValue {\n\ttbl := l.NewTable()\n"
81 .. fields_tolua(fields, "\t")
82 .. "\treturn tbl\n}\n\n"
85 local pkt_type_impl = ""
88 for name, fields in spairs(parse_spec("client/pkt", true)) do
89 local case = "\tcase *mt.ToClt" .. camel_case(name) .. ":\n"
91 pkt_type_impl = pkt_type_impl
92 .. case .. "\t\treturn lua.LString(\"" .. name .. "\")\n"
95 pkt_impl = pkt_impl .. case .. fields_tolua(fields, "\t\t")
99 local f = io.open("push_auto.go", "w")
101 // generated by push_mkauto.lua, DO NOT EDIT
105 "github.com/anon55555/mt"
106 "github.com/yuin/gopher-lua"
110 func PushPktType(pkt *mt.Pkt) lua.LString {
111 switch pkt.Cmd.(type) {
112 ]] .. pkt_type_impl .. [[
118 func PushPkt(l *lua.LState, pkt *mt.Pkt) lua.LValue {
123 l.SetField(tbl, "_type", PushPktType(pkt))
124 switch val := pkt.Cmd.(type) {