]> git.lizzy.rs Git - hydra-dragonfire.git/blob - convert/push_mkauto.lua
Use dragonfire fork of mt
[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         boolean = "lua.LBool(VAL)",
32         number = "lua.LNumber(VAL)",
33         vec2 = "PushVec2(l, [2]lua.LNumber{lua.LNumber(VAL[0]), lua.LNumber(VAL[1])})",
34         vec3 = "PushVec3(l, [3]lua.LNumber{lua.LNumber(VAL[0]), lua.LNumber(VAL[1]), lua.LNumber(VAL[2])})",
35         box1 = "PushBox1(l, [2]lua.LNumber{lua.LNumber(VAL[0]), lua.LNumber(VAL[1])})",
36         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])}})",
37         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])}})",
38 }
39
40 local function fields_tolua(fields, indent)
41         local impl = ""
42
43         for name, type in spairs(fields) do
44                 local c = name:sub(1, 1)
45                 if c ~= "{" and c ~= "%" then
46                         local camel = "val." .. camel_case(name)
47
48                         local idt = indent
49                         local condition = fields["{" .. name .. "}"]
50                         local typeparam = fields["%" .. name .. "%"]
51
52                         if condition then
53                                 impl = impl .. indent .. "if " .. condition .. " {\n"
54                                 idt = idt .. "\t"
55                         end
56
57                         impl = impl .. idt .. "l.SetField(tbl, \"" .. name .. "\", "
58                         if tolua[type] then
59                                 impl = impl .. tolua[type]:gsub("VAL", camel)
60                         else
61                                 impl = impl .. "Push" .. camel_case(type)
62                                         .. (typeparam and "[" .. typeparam .. "]" or "")
63                                         .. "(l, " .. camel .. ")"
64                         end
65                         impl = impl .. ")\n"
66
67                         if condition then
68                                 impl = impl .. indent .. "}\n"
69                         end
70                 end
71         end
72
73         return impl
74 end
75
76 for name, fields in spairs(parse_spec("client/struct", true)) do
77         local camel = camel_case(name)
78         funcs = funcs
79                 .. "func Push" .. camel .. "(l *lua.LState, val mt." .. camel .. ") lua.LValue {\n\ttbl := l.NewTable()\n"
80                 .. fields_tolua(fields, "\t")
81                 .. "\treturn tbl\n}\n\n"
82 end
83
84 local pkt_type_impl = ""
85 local pkt_impl = ""
86
87 for name, fields in spairs(parse_spec("client/pkt", true)) do
88         local case = "\tcase *mt.ToClt" .. camel_case(name) .. ":\n"
89
90         pkt_type_impl = pkt_type_impl
91                 .. case .. "\t\treturn lua.LString(\"" .. name .. "\")\n"
92
93         if next(fields) then
94                 pkt_impl = pkt_impl .. case .. fields_tolua(fields, "\t\t")
95         end
96 end
97
98 local f = io.open("push_auto.go", "w")
99 f:write([[
100 // generated by push_mkauto.lua, DO NOT EDIT
101 package convert
102
103 import (
104         "github.com/dragonfireclient/mt"
105         "github.com/yuin/gopher-lua"
106 )
107
108 ]] .. funcs .. [[
109 func PushPktType(pkt *mt.Pkt) lua.LString {
110         switch pkt.Cmd.(type) {
111 ]] .. pkt_type_impl .. [[
112         }
113         panic("impossible")
114         return ""
115 }
116
117 func PushPkt(l *lua.LState, pkt *mt.Pkt) lua.LValue {
118         if pkt == nil {
119                 return lua.LNil
120         }
121         tbl := l.NewTable()
122         switch val := pkt.Cmd.(type) {
123 ]] .. pkt_impl .. [[
124         }
125         return tbl
126 }
127 ]])
128 f:close()