forked from JustaPenguin/assetto-server-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lua.go
143 lines (106 loc) · 2.72 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
package servermanager
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
"time"
"github.com/sirupsen/logrus"
lua "github.com/yuin/gopher-lua"
)
var Lua *lua.LState
func InitLua(raceControl *RaceControl) {
Lua.SetGlobal("httpRequest", Lua.NewFunction(LuaHTTPRequest))
Lua.SetGlobal("broadcastChat", Lua.NewFunction(raceControl.LuaBroadcastChat))
Lua.SetGlobal("sendChat", Lua.NewFunction(raceControl.LuaSendChat))
go func() {
err := managerStartPlugin()
if err != nil {
logrus.WithError(err).Error("manager start plugin script failed")
}
}()
}
type LuaPlugin struct {
inputs []interface{}
outputs []interface{}
}
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 {
var jsonInputs []lua.LValue
err := Lua.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(string(jsonInput)))
}
if err := Lua.CallByParam(lua.P{
Fn: Lua.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(Lua.Get(-1).String()), l.outputs[i])
if err != nil {
return err
}
Lua.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 := &LuaPlugin{}
err := p.Call("./plugins/manager.lua", "onManagerStart")
if err != nil {
return err
}
return nil
}