19 local function generate(name)
20 local fnname, index, child, childfn, childtype
23 local open = name:find("%[")
24 local clos = name:find("%]")
27 index = name:sub(open + 1, clos - 1)
28 child = name:sub(clos + 1)
29 childfn, childtype = generate(child)
30 fnname = (index == "" and "Slice" or "Vec" .. index) .. childfn
32 type = "[" .. index .. "]" .. childtype
34 fnname = camel_case(name)
36 local c = name:sub(1, 1)
37 if c == c:upper() then
42 if not readers[fnname] then
43 local fun = "func read" .. fnname .. "(l *lua.LState, val lua.LValue, ptr *" .. type .. ") {\n"
46 fun = fun .. "\tif val.Type() != lua.LTTable {\n\t\tpanic(\"invalid value for "
47 .. name .. ": must be a table\")\n\t}\n"
52 tbl := val.(*lua.LTable)
54 *ptr = make(]] .. type .. [[, n)
56 read]] .. childfn .. [[(l, l.RawGetInt(tbl, i+1), &(*ptr)[i])
60 local n = tonumber(index)
61 for i, v in ipairs({"x", "y", "z"}) do
67 .. "\tread" .. childfn
68 .. "(l, l.GetField(val, \"" .. v .. "\"), &(*ptr)[" .. (i - 1) .. "])\n"
72 fun = fun .. "\tif val.Type() != lua.LTNumber {\n\t\tpanic(\"invalid value for "
73 .. name .. ": must be a number\")\n\t}\n"
74 .. "\t*ptr = " .. type .. "(val.(lua.LNumber))\n"
85 for _, use in ipairs(static_uses) do
89 local function signature(name, prefix, type)
90 local camel = camel_case(name)
91 return "func read" .. camel .. "(l *lua.LState, val lua.LValue, ptr *" .. prefix .. camel .. ") {\n"
94 for name, fields in spairs(parse_spec("server/enum")) do
95 local camel = camel_case(name)
96 local fun = signature(name, "mt.")
99 for _, var in ipairs(fields) do
100 local equals = "*ptr = mt." .. apply_prefix(fields, var) .. "\n"
103 fun = fun .. "\tif val.Type() == lua.LTNil {\n\t\t" .. equals .. "\t\treturn\n\t}\n"
105 impl = impl .. "\tcase \"" .. var .. "\":\n\t\t" .. equals
110 .. "\tif val.Type() != lua.LTString {\n\t\tpanic(\"invalid value for "
111 .. camel .. ": must be a string\")\n\t}\n"
112 .. "\tstr := string(val.(lua.LString))\n"
113 .. "\tswitch str {\n" .. impl
114 .. "\tdefault:\n\t\tpanic(\"invalid value for " .. name .. ": \" + str)\n\t}\n}\n\n"
119 for name, fields in spairs(parse_spec("server/flag")) do
120 local camel = camel_case(name)
121 local fun = signature(name, "mt.")
122 .. "\tif val.Type() != lua.LTTable {\n\t\tpanic(\"invalid value for "
123 .. camel .. ": must be a table\")\n\t}\n"
125 for _, var in ipairs(fields) do
126 fun = fun .. "\tif l.GetField(val, \"" .. var .. "\") == lua.LTrue {\n"
127 .. "\t\t*ptr = *ptr | mt." .. apply_prefix(fields, var) .. "\n\t}\n"
134 local function fields_fromlua(fields, indent)
137 for name, type in spairs(fields) do
138 impl = impl .. indent .. "read" .. generate(type) .. "(l, l.GetField(val, \"" .. name .. "\"), &ptr."
139 .. camel_case(name) .. ")\n"
145 for name, fields in spairs(parse_spec("server/struct", true)) do
146 local camel = camel_case(name)
147 readers[camel] = signature(name, "mt.")
148 .. "\tif val.Type() != lua.LTTable {\n"
149 .. "\t\tpanic(\"invalid value for " .. camel .. ": must be a table\")\n\t}\n"
150 .. fields_fromlua(fields, "\t")
156 for name, fields in spairs(parse_spec("server/pkt", true)) do
158 .. "\tcase \"" .. name .. "\"" .. "" .. ":\n"
159 .. "\t\tptr := &mt.ToSrv" .. camel_case(name) .. "{}\n"
163 .. "\t\tval := l.CheckTable(3)\n"
164 .. fields_fromlua(fields, "\t\t")
168 .. "\t\treturn ptr\n"
172 for _, fn in spairs(readers) do
173 if type(fn) == "string" then
178 local f = io.open("read_auto.go", "w")
180 // generated by read_mkauto.lua, DO NOT EDIT
184 "github.com/anon55555/mt"
185 "github.com/yuin/gopher-lua"
189 func ReadCmd(l *lua.LState) mt.Cmd {
190 str := l.CheckString(2)
195 panic("invalid packet type: " + str)