]> git.lizzy.rs Git - hydra-dragonfire.git/blob - convert/push_mkauto.lua
Add map component
[hydra-dragonfire.git] / convert / push_mkauto.lua
1 #!/usr/bin/env lua
2 require("spec")
3
4 local funcs = ""
5
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"
9
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"
13         end
14
15         funcs = funcs .. "\t}\n\tpanic(\"impossible\")\n\treturn lua.LNil\n}\n\n"
16 end
17
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"
21
22         for _, var in ipairs(fields) do
23                 funcs = funcs .. "\tl.SetField(tbl, \"" .. var  .. "\", lua.LBool(val&mt." .. apply_prefix(fields, var) .. " != 0))\n"
24         end
25
26         funcs = funcs .. "\treturn tbl\n}\n\n"
27 end
28
29 local tolua = {
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])}})",
39 }
40
41 local function fields_tolua(fields, indent)
42         local impl = ""
43
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)
48
49                         local idt = indent
50                         local condition = fields["{" .. name .. "}"]
51                         local typeparam = fields["%" .. name .. "%"]
52
53                         if condition then
54                                 impl = impl .. indent .. "if " .. condition .. " {\n"
55                                 idt = idt .. "\t"
56                         end
57
58                         impl = impl .. idt .. "l.SetField(tbl, \"" .. name .. "\", "
59                         if tolua[type] then
60                                 impl = impl .. tolua[type]:gsub("VAL", camel)
61                         else
62                                 impl = impl .. "Push" .. camel_case(type)
63                                         .. (typeparam and "[" .. typeparam .. "]" or "")
64                                         .. "(l, " .. camel .. ")"
65                         end
66                         impl = impl .. ")\n"
67
68                         if condition then
69                                 impl = impl .. indent .. "}\n"
70                         end
71                 end
72         end
73
74         return impl
75 end
76
77 for name, fields in spairs(parse_spec("client/struct", true)) do
78         local camel = camel_case(name)
79         funcs = funcs
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"
83 end
84
85 local pkt_type_impl = ""
86 local pkt_impl = ""
87
88 for name, fields in spairs(parse_spec("client/pkt", true)) do
89         local case = "\tcase *mt.ToClt" .. camel_case(name) .. ":\n"
90
91         pkt_type_impl = pkt_type_impl
92                 .. case .. "\t\treturn lua.LString(\"" .. name .. "\")\n"
93
94         if next(fields) then
95                 pkt_impl = pkt_impl .. case .. fields_tolua(fields, "\t\t")
96         end
97 end
98
99 local f = io.open("push_auto.go", "w")
100 f:write([[
101 // generated by push_mkauto.lua, DO NOT EDIT
102 package convert
103
104 import (
105         "github.com/anon55555/mt"
106         "github.com/yuin/gopher-lua"
107 )
108
109 ]] .. funcs .. [[
110 func PushPktType(pkt *mt.Pkt) lua.LString {
111         switch pkt.Cmd.(type) {
112 ]] .. pkt_type_impl .. [[
113         }
114         panic("impossible")
115         return ""
116 }
117
118 func PushPkt(l *lua.LState, pkt *mt.Pkt) lua.LValue {
119         if pkt == nil {
120                 return lua.LNil
121         }
122         tbl := l.NewTable()
123         l.SetField(tbl, "_type", PushPktType(pkt))
124         switch val := pkt.Cmd.(type) {
125 ]] .. pkt_impl .. [[
126         }
127         return tbl
128 }
129 ]])
130 f:close()