This repository has been archived by the owner on Jun 15, 2020. It is now read-only.
/
generate.go
312 lines (266 loc) · 7.26 KB
/
generate.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
package internal
import (
"bytes"
"crypto/rand"
"fmt"
"github.com/liagame/lia-SDK"
"github.com/liagame/lia-SDK/internal/config"
"io"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"time"
)
type GameFlags struct {
GameSeed int
MapSeed int
Port int
MapPath string
ReplayPath string
ConfigPath string
DebugBots []int
}
func GenerateGame(bot1Dir string, bot2Dir string, gameFlags *GameFlags) {
// Allows running deferred functions before exiting
osExitStatus := -1
defer func() {
if osExitStatus != -1 {
os.Exit(osExitStatus)
}
}()
bot1Debug := contains(gameFlags.DebugBots, 1)
uidBot1 := getBotUid(bot1Debug)
bot2Debug := contains(gameFlags.DebugBots, 2)
uidBot2 := getBotUid(bot2Debug)
configureReplayFilePath(gameFlags)
// Set config path if not provided
if gameFlags.ConfigPath == "" {
gameFlags.ConfigPath = filepath.Join(gameFlags.ConfigPath, "game-config.json")
if len(gameFlags.DebugBots) > 0 {
gameFlags.ConfigPath = strings.Replace(gameFlags.ConfigPath, ".json", "-debug.json", 1)
}
}
// Set port if not already set
if gameFlags.Port == 0 {
gameFlags.Port = config.Cfg.GamePort
}
// Create channel that will listen to results
// from game engine and both bots
result := make(chan error)
cmdBot1 := &CommandRef{}
cmdBot2 := &CommandRef{}
cmdGameEngine := &CommandRef{}
engineStarted := make(chan bool)
// Run game-engine
go func() {
fmt.Printf("Running game engine\n")
bot1Name := parseBotName(bot1Dir)
bot2Name := parseBotName(bot2Dir)
err := runGameEngine(engineStarted, cmdGameEngine, gameFlags, bot1Name, bot2Name, uidBot1, uidBot2)
cmdGameEngine.cmd = nil
result <- err
}()
// Wait until game engine has started
<-engineStarted
engineFinished := false
// Run bots
runBotWrapper := func(cmdBot *CommandRef, botDir, botUid string) {
fmt.Printf("Running bot %s\n", botDir)
err := runBot(cmdBot, botDir, botUid, gameFlags.Port)
if err != nil {
if !engineFinished {
fmt.Fprintf(os.Stderr, "Running bot %s failed. %s\n", botDir, err)
}
}
cmdBot.cmd = nil
}
if !bot1Debug {
go runBotWrapper(cmdBot1, bot1Dir, uidBot1)
}
if !bot2Debug {
go runBotWrapper(cmdBot2, bot2Dir, uidBot2)
}
// Wait for game engine to finish
err := <-result
if err != nil {
fmt.Fprintf(os.Stderr, "failed to generate game\n %s\n", err)
osExitStatus = lia_SDK.FailedToGenerateGame
return
}
engineFinished = true
// Attempt to kill the process to prevent daemons
killProcess(cmdBot1)
killProcess(cmdBot2)
// Wait for outputs to appear on the console (nicer way to fix this?)
time.Sleep(time.Millisecond * 100)
}
func configureReplayFilePath(gameFlags *GameFlags) {
replayPath := gameFlags.ReplayPath
// If the replay file was not provided
if replayPath == "" {
path := filepath.Join(config.PathToBots, "replays")
os.MkdirAll(path, os.ModePerm)
//"2006-01-02T15:04:05Z07:00"
fileName := time.Now().Format("2006-01-02T15-04-05") + ".lia"
gameFlags.ReplayPath = filepath.Join(path, fileName)
} else {
if !strings.HasSuffix(replayPath, ".lia") {
fmt.Fprintf(os.Stderr, "ERROR: Provided replay file %s does not end with .lia suffix, please add it.",
replayPath)
os.Exit(lia_SDK.Generic)
}
if !filepath.IsAbs(replayPath) {
gameFlags.ReplayPath = filepath.Join(config.PathToBots, replayPath)
}
}
}
func parseBotName(botDir string) string {
if config.OperatingSystem == "windows" {
split := strings.Split(botDir, "\\")
return split[len(split)-1]
} else {
split := strings.Split(botDir, "/")
return split[len(split)-1]
}
}
func contains(slice []int, e int) bool {
for _, e2 := range slice {
if e == e2 {
return true
}
}
return false
}
func getBotUid(debug bool) string {
if debug {
return ""
}
uid, err := generateUuid()
if err != nil {
fmt.Fprintf(os.Stderr, "failed to generate uid. %s", err)
os.Exit(lia_SDK.Generic)
}
return uid
}
func killProcess(cmdRef *CommandRef) {
if cmdRef.cmd != nil {
if err := cmdRef.cmd.Process.Kill(); err != nil {
// Ignore, no valuable information
}
}
}
type CommandRef struct {
cmd *exec.Cmd
}
func runBot(cmdRef *CommandRef, name, uid string, port int) error {
runScriptName := "run.sh"
if config.OperatingSystem == "windows" {
runScriptName = "run.bat"
}
botDir := filepath.Join(config.PathToBots, name)
var cmd *exec.Cmd
if config.OperatingSystem == "windows" {
cmd = exec.Command(".\\"+runScriptName, strconv.Itoa(port), uid)
} else {
cmd = exec.Command("/bin/bash", runScriptName, strconv.Itoa(port), uid)
}
cmdRef.cmd = cmd
cmd.Dir = botDir
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
return err
}
return nil
}
func runGameEngine(started chan bool, cmdRef *CommandRef, gameFlags *GameFlags, nameBot1, nameBot2, uidBot1, uidBot2 string) error {
cmd := exec.Command(
"java",
// quiet warnings from com.google.protobuf.UnsafeUtil,
// see: https://github.com/google/protobuf/issues/3781
"-XX:+IgnoreUnrecognizedVMOptions",
"--add-opens=java.base/java.nio=ALL-UNNAMED",
"--add-opens=java.base/java.lang=ALL-UNNAMED",
"-jar", "game-engine.jar",
"-g", fmt.Sprint(gameFlags.GameSeed),
"-m", fmt.Sprint(gameFlags.MapSeed),
"-p", fmt.Sprint(gameFlags.Port),
)
cmdRef.cmd = cmd
// Append string flags if they are not empty
if len(gameFlags.MapPath) > 0 {
cmd.Args = append(cmd.Args, "-M", gameFlags.MapPath)
}
if len(gameFlags.ReplayPath) > 0 {
cmd.Args = append(cmd.Args, "-r", gameFlags.ReplayPath)
}
if len(gameFlags.ConfigPath) > 0 {
cmd.Args = append(cmd.Args, "-c", gameFlags.ConfigPath)
}
// Append bot1 and his uid
cmd.Args = append(cmd.Args, nameBot1, uidBot1)
// Append bot2 and his uid
cmd.Args = append(cmd.Args, nameBot2, uidBot2)
cmd.Dir = config.PathToData
// Get pipes for stdout and stderr
stdoutIn, err := cmd.StdoutPipe()
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to create stdout pipe for game engine.")
return err
}
stderrIn, err := cmd.StderrPipe()
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to create stdin pipe for game engine.")
return err
}
// Create multi writer that will pass result to stdout, stderr and buffers
var stdoutBuf, stderrBuf bytes.Buffer
stdout := io.MultiWriter(os.Stdout, &stdoutBuf)
stderr := io.MultiWriter(os.Stderr, &stderrBuf)
// Set the data flow from command to writers
var errStdout, errStderr error
go func() {
_, errStdout = io.Copy(stdout, stdoutIn)
}()
go func() {
_, errStderr = io.Copy(stderr, stderrIn)
}()
// Send true to started channel when game engine outputs something
// (means that websocket server is prepared)
go func() {
for {
if stdoutBuf.Len() > 0 || stderrBuf.Len() > 0 {
started <- true
return
}
time.Sleep(time.Millisecond * 20)
}
}()
// Run game engine
if err := cmd.Run(); err != nil {
fmt.Fprintf(os.Stderr, "Game engine failed.")
return err
}
if errStdout != nil {
fmt.Fprintf(os.Stderr, "Failed to capture stdout.")
return err
}
if errStderr != nil {
fmt.Fprintf(os.Stderr, "Failed to capture stderr.")
return err
}
return nil
}
func generateUuid() (string, error) {
b := make([]byte, 16)
_, err := rand.Read(b)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to get random number.")
return "", err
}
uuid := fmt.Sprintf("%x-%x-%x-%x-%x", b[0:4], b[4:6], b[6:8], b[8:10], b[10:])
return uuid, nil
}