forked from JustaPenguin/assetto-server-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lua.go
159 lines (117 loc) · 2.95 KB
/
lua.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package servermanager
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
"time"
"github.com/sirupsen/logrus"
lua "github.com/yuin/gopher-lua"
)
var luaFunctions = make(map[string]lua.LGFunction)
func InitLua(raceControl *RaceControl) {
luaFunctions["httpRequest"] = LuaHTTPRequest
luaFunctions["broadcastChat"] = raceControl.LuaBroadcastChat
luaFunctions["sendChat"] = raceControl.LuaSendChat
go func() {
err := managerStartPlugin()
if err != nil {
logrus.WithError(err).Error("manager start plugin script failed")
}
}()
}
type LuaPlugin struct {
state *lua.LState
inputs []interface{}
outputs []interface{}
}
func NewLuaPlugin() *LuaPlugin {
state := lua.NewState()
for name, fn := range luaFunctions {
state.SetGlobal(name, state.NewFunction(fn))
}
return &LuaPlugin{
state: state,
}
}
func (l *LuaPlugin) Inputs(i ...interface{}) *LuaPlugin {
l.inputs = append(l.inputs, i...)
return l
}
// remember outputs need to be reversed from whatever the plugin returns
func (l *LuaPlugin) Outputs(o ...interface{}) *LuaPlugin {
l.outputs = append(l.outputs, o...)
return l
}
func (l *LuaPlugin) Call(fileName, functionName string) error {
defer l.state.Close()
var jsonInputs []lua.LValue
err := l.state.DoFile(fileName)
if err != nil {
return err
}
for _, input := range l.inputs {
jsonInput, err := json.Marshal(input)
if err != nil {
return err
}
jsonInputs = append(jsonInputs, lua.LString(jsonInput))
}
if err := l.state.CallByParam(lua.P{
Fn: l.state.GetGlobal(functionName), // name of Lua function
NRet: len(l.outputs), // number of returned values
Protect: true, // return err or panic
}, jsonInputs...); err != nil {
return err
}
for i := range l.outputs {
err := json.Unmarshal([]byte(l.state.Get(-1).String()), l.outputs[i])
if err != nil {
return err
}
l.state.Pop(1)
}
return nil
}
func LuaHTTPRequest(l *lua.LState) int {
url := l.ToString(1)
method := l.ToString(2)
reqBodyString := l.ToString(3)
httpClient := http.Client{
Timeout: time.Second * 2, // Maximum of 2 secs
}
var req *http.Request
var err error
if method != "GET" && reqBodyString != "" {
req, err = http.NewRequest(method, url, bytes.NewBuffer([]byte(reqBodyString)))
} else {
req, err = http.NewRequest(method, url, nil)
}
if err != nil {
logrus.WithError(err).Error("Make new request")
return 0
}
req.Header.Set("User-Agent", "assetto-server-manager")
res, err := httpClient.Do(req)
if err != nil {
logrus.WithError(err).Error("do request")
return 0
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
logrus.WithError(err).Error("read response body")
return 0
}
l.Push(lua.LString(body))
l.Push(lua.LNumber(res.StatusCode))
return 2
}
func managerStartPlugin() error {
p := NewLuaPlugin()
err := p.Call("./plugins/manager.lua", "onManagerStart")
if err != nil {
return err
}
return nil
}