-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathapp.go
640 lines (580 loc) · 17 KB
/
app.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
package main
import (
"flag"
"fmt"
"log"
"os/exec"
"path/filepath"
"sort"
"strconv"
"time"
"github.com/atotto/clipboard"
"github.com/cheggaaa/pb/v3"
"github.com/linus4/csgoverview/common"
"github.com/linus4/csgoverview/match"
demoinfo "github.com/markus-wa/demoinfocs-golang/v2/pkg/demoinfocs/common"
"github.com/veandco/go-sdl2/img"
"github.com/veandco/go-sdl2/sdl"
"github.com/veandco/go-sdl2/ttf"
)
const (
winWidth int32 = 1624
winHeight int32 = 1024
nameMapFontSize int = 14
mapOverviewWidth int32 = 1024
mapOverviewHeight int32 = 1024
mapXOffset int32 = 300
mapYOffset int32 = 0
infobarElementHeight int32 = 100
appVersion string = "v1.2.0"
hotkeysString string = `
* a -> 3 s backwards
* d -> 3 s forwards
* A -> 10 s backwards
* D -> 10 s forwards
* w -> increase playback speed
* s -> decrease playback speed
* r -> reset playback speed to x 1
* W -> hold to speed up 5 x
* S -> hold to slow down to 0.5 x
* q -> round backwards
* e -> round forwards
* Q -> to start of previous half
* E -> to start of next half
* space -> toggle pause
* mouse wheel -> scroll 1 second forwards/backwards
* c -> switch to alternate overview image (normal / lower)
* h -> hide player names on map
* 0-9 -> copy players position and view angle to clipboard
* k -> show hotkeys
`
)
// Config contains information the application requires in order to run
type Config struct {
// Path to font file (.ttf)
FontPath string
// Path to overview directory
OverviewDir string
// Whether to just print the version number
PrintVersion bool
}
// App contains the state of the application.
type app struct {
window *sdl.Window
renderer *sdl.Renderer
mapTexture *sdl.Texture
alternateMapTexture *sdl.Texture
mapRect *sdl.Rect
font *ttf.Font
match *match.Match
config *Config
lastDrawnAt time.Time
isPaused bool
curFrame int
isOnNormalElevation bool
playbackSpeedModifier float64
staticPlaybackSpeedModifier float64
hidePlayerNames bool
}
func run(c *Config) error {
// Progress Bar template
pbTmpl := `{{string . "step" | green}} {{ bar . "[" "#" (cycle . "⬒" "⬔" "⬓" "⬕" ) "." "]"}} {{percent .}}`
pbInstance := pb.ProgressBarTemplate(pbTmpl).Start(8)
pbInstance.Set("step", "Starting...")
pbInstance.Start()
var demoFileName string
if c.PrintVersion {
fmt.Println(appVersion)
return nil
}
if len(flag.Args()) < 1 {
demoFileNameB, err := exec.Command("zenity", "--file-selection").Output()
if err != nil {
fmt.Println("Usage: ./csgoverview [path to demo]")
return err
}
demoFileName = string(demoFileNameB)[:len(demoFileNameB)-1]
} else {
demoFileName = flag.Args()[0]
}
pbInstance.Set("step", "Initializing SDL")
err := sdl.Init(sdl.INIT_VIDEO | sdl.INIT_EVENTS)
if err != nil {
errorString := fmt.Sprintf("trying to initialize SDL:\n%v", err)
_ = sdl.ShowSimpleMessageBox(sdl.MESSAGEBOX_ERROR, "Error", errorString, nil)
return err
}
defer sdl.Quit()
pbInstance.Increment()
pbInstance.Set("step", "Initializing TTF lib")
err = ttf.Init()
if err != nil {
errorString := fmt.Sprintf("trying to initialize the TTF lib:\n%v", err)
_ = sdl.ShowSimpleMessageBox(sdl.MESSAGEBOX_ERROR, "Error", errorString, nil)
return err
}
defer ttf.Quit()
pbInstance.Increment()
pbInstance.Set("step", "Opening font file")
font, err := ttf.OpenFont(c.FontPath, nameMapFontSize)
if err != nil {
errorString := fmt.Sprintf("trying to open font file (system):\n%v", err)
_ = sdl.ShowSimpleMessageBox(sdl.MESSAGEBOX_ERROR, "Error", errorString, nil)
return err
}
defer font.Close()
pbInstance.Increment()
pbInstance.Set("step", "Creating SDL window")
window, err := sdl.CreateWindow("csgoverview", sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED,
winWidth, winHeight, sdl.WINDOW_SHOWN|sdl.WINDOW_RESIZABLE)
if err != nil {
errorString := fmt.Sprintf("trying to create SDL window:\n%v", err)
_ = sdl.ShowSimpleMessageBox(sdl.MESSAGEBOX_ERROR, "Error", errorString, nil)
return err
}
defer func() {
err = window.Destroy()
if err != nil {
log.Fatalln(err)
}
}()
pbInstance.Increment()
pbInstance.Set("step", "Creating SDL renderer")
renderer, err := sdl.CreateRenderer(window, -1, sdl.RENDERER_SOFTWARE)
if err != nil {
errorString := fmt.Sprintf("trying to create SDL renderer:\n%v", err)
_ = sdl.ShowSimpleMessageBox(sdl.MESSAGEBOX_ERROR, "Error", errorString, window)
return err
}
defer func() {
err = renderer.Destroy()
if err != nil {
log.Fatalln(err)
}
}()
err = renderer.SetLogicalSize(mapOverviewWidth+2*mapXOffset, mapOverviewHeight+mapYOffset)
if err != nil {
errorString := fmt.Sprintf("trying to set logical size on renderer:\n%v", err)
_ = sdl.ShowSimpleMessageBox(sdl.MESSAGEBOX_ERROR, "Error", errorString, window)
return err
}
pbInstance.Increment()
pbInstance.Set("step", "Parsing demo file")
match, err := match.NewMatch(demoFileName, pbInstance)
if err != nil {
errorString := fmt.Sprintf("trying to parse demo file:\n%v", err)
_ = sdl.ShowSimpleMessageBox(sdl.MESSAGEBOX_ERROR, "Error", errorString, window)
return err
}
pbInstance.Increment()
pbInstance.Set("step", "Loading map overview surface")
mapSurface, err := img.Load(filepath.Join(c.OverviewDir, fmt.Sprintf("%v.jpg", match.MapName)))
if err != nil {
errorString := fmt.Sprintf("trying to load map overview image from %v: \n"+
"%v \nFollow the instructions on https://github.com/linus4/csgoverview "+
"to place the overview images in this directory.", c.OverviewDir, err)
_ = sdl.ShowSimpleMessageBox(sdl.MESSAGEBOX_ERROR, "Error", errorString, window)
return err
}
defer mapSurface.Free()
pbInstance.Increment()
pbInstance.Set("step", "Creating texture from surface")
mapTexture, err := renderer.CreateTextureFromSurface(mapSurface)
if err != nil {
errorString := fmt.Sprintf("trying to create mapTexture from Surface:\n%v", err)
_ = sdl.ShowSimpleMessageBox(sdl.MESSAGEBOX_ERROR, "Error", errorString, window)
return err
}
defer func() {
err = mapTexture.Destroy()
if err != nil {
log.Fatalln(err)
}
}()
var alternateMapTexture *sdl.Texture
if common.MapHasAlternateVersion(match.MapName) {
alternateFileName := common.MapGetAlternateVersion(match.MapName)
alternateMapSurface, err := img.Load(filepath.Join(c.OverviewDir, fmt.Sprintf("%v", alternateFileName)))
if err != nil {
errorString := fmt.Sprintf("trying to load map overview image from %v: \n"+
"%v \nFollow the instructions on https://github.com/linus4/csgoverview "+
"to place the overview images in this directory.", c.OverviewDir, err)
_ = sdl.ShowSimpleMessageBox(sdl.MESSAGEBOX_ERROR, "Error", errorString, window)
return err
}
defer alternateMapSurface.Free()
alternateMapTexture, err = renderer.CreateTextureFromSurface(alternateMapSurface)
if err != nil {
errorString := fmt.Sprintf("trying to create alternateMapTexture from Surface:\n%v", err)
_ = sdl.ShowSimpleMessageBox(sdl.MESSAGEBOX_ERROR, "Error", errorString, window)
return err
}
defer func() {
err = alternateMapTexture.Destroy()
if err != nil {
log.Fatalln(err)
}
}()
}
pbInstance.Increment()
mapRect := &sdl.Rect{X: mapXOffset, Y: mapYOffset, W: mapOverviewWidth, H: mapOverviewHeight}
app := app{
window: window,
renderer: renderer,
mapTexture: mapTexture,
alternateMapTexture: alternateMapTexture,
mapRect: mapRect,
font: font,
match: match,
config: c,
lastDrawnAt: time.Now().UTC(),
isPaused: false,
curFrame: 0,
isOnNormalElevation: true,
playbackSpeedModifier: 1,
staticPlaybackSpeedModifier: 1,
hidePlayerNames: false,
}
pbInstance.Set("step", "Starting app")
pbInstance.Finish()
err = app.run()
if err != nil {
errorString := fmt.Sprintf("while running the app:\n%v", err)
_ = sdl.ShowSimpleMessageBox(sdl.MESSAGEBOX_ERROR, "Error", errorString, nil)
return err
}
return nil
}
func (app *app) run() error {
m := app.match
// MAIN GAME LOOP
for {
frameStart := time.Now()
for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
switch eventT := event.(type) {
case *sdl.QuitEvent:
return nil
case *sdl.KeyboardEvent:
if eventT.Type != sdl.KEYDOWN {
break
}
app.handleKeyboardEvents(eventT)
if eventT.Keysym.Sym == sdl.K_c {
if common.MapHasAlternateVersion(m.MapName) {
tmp := app.mapTexture
app.mapTexture = app.alternateMapTexture
app.alternateMapTexture = tmp
app.isOnNormalElevation = !app.isOnNormalElevation
}
}
case *sdl.MouseWheelEvent:
// back
if eventT.Type == sdl.MOUSEWHEEL {
if eventT.Y > 0 {
if app.curFrame < m.FrameRate*1 {
app.curFrame = 0
} else {
app.curFrame -= m.FrameRate * 1
}
}
if eventT.Y < 0 {
// forward
if app.curFrame+m.FrameRate*1 > len(m.States)-1 {
app.curFrame = len(m.States) - 1
} else {
app.curFrame += m.FrameRate * 1
}
}
}
}
}
if app.isPaused {
sdl.Delay(32)
app.updateGraphics()
app.updateWindowTitle()
continue
}
app.updateGraphics()
app.updateWindowTitle()
app.playbackSpeedModifier = 1
// frameDuration is in ms
frameDuration := float64(time.Since(frameStart) / 1000000)
keyboardState := sdl.GetKeyboardState()
if keyboardState[sdl.GetScancodeFromKey(sdl.K_w)] != 0 &&
keyboardState[sdl.GetScancodeFromKey(sdl.K_LSHIFT)] != 0 {
app.playbackSpeedModifier = 5
}
if keyboardState[sdl.GetScancodeFromKey(sdl.K_s)] != 0 &&
keyboardState[sdl.GetScancodeFromKey(sdl.K_LSHIFT)] != 0 {
app.playbackSpeedModifier = 0.5
}
delay := (1/(app.playbackSpeedModifier*app.staticPlaybackSpeedModifier))*(1000/float64(m.FrameRate)) - frameDuration
if delay < 0 {
delay = 0
}
sdl.Delay(uint32(delay))
if app.curFrame < len(m.States)-1 {
app.curFrame = app.curFrame + 1
}
}
}
func (app *app) handleKeyboardEvents(eventT *sdl.KeyboardEvent) {
m := app.match
switch eventT.Keysym.Sym {
case sdl.K_SPACE:
app.isPaused = !app.isPaused
case sdl.K_a:
if isShiftPressed(eventT) {
if app.curFrame < m.FrameRate*10 {
app.curFrame = 0
} else {
app.curFrame -= m.FrameRate * 10
}
} else {
if app.curFrame < m.FrameRate*3 {
app.curFrame = 0
} else {
app.curFrame -= m.FrameRate * 3
}
}
case sdl.K_d:
if isShiftPressed(eventT) {
if app.curFrame+m.FrameRate*10 > len(m.States)-1 {
app.curFrame = len(m.States) - 1
} else {
app.curFrame += m.FrameRate * 10
}
} else {
if app.curFrame+m.FrameRate*3 > len(m.States)-1 {
app.curFrame = len(m.States) - 1
} else {
app.curFrame += m.FrameRate * 3
}
}
case sdl.K_q:
if isShiftPressed(eventT) {
set := false
for i, frame := range m.HalfStarts {
if app.curFrame < frame {
if i > 1 && app.curFrame < m.HalfStarts[i-1]+m.FrameRate/2 {
app.curFrame = m.HalfStarts[i-2]
set = true
break
}
if i-1 < 0 {
app.curFrame = 0
set = true
break
}
app.curFrame = m.HalfStarts[i-1]
set = true
break
}
}
// not set -> last round of match
if !set {
if len(m.HalfStarts) > 1 && app.curFrame < m.HalfStarts[len(m.HalfStarts)-1]+m.FrameRate/2 {
app.curFrame = m.HalfStarts[len(m.HalfStarts)-2]
} else {
app.curFrame = m.HalfStarts[len(m.HalfStarts)-1]
}
}
} else {
set := false
for i, frame := range m.RoundStarts {
if app.curFrame < frame {
if i > 1 && app.curFrame < m.RoundStarts[i-1]+m.FrameRate/2 {
app.curFrame = m.RoundStarts[i-2]
set = true
break
}
if i-1 < 0 {
app.curFrame = 0
set = true
break
}
app.curFrame = m.RoundStarts[i-1]
set = true
break
}
}
// not set -> last round of match
if !set {
if len(m.RoundStarts) > 1 && app.curFrame < m.RoundStarts[len(m.RoundStarts)-1]+m.FrameRate/2 {
app.curFrame = m.RoundStarts[len(m.RoundStarts)-2]
} else {
app.curFrame = m.RoundStarts[len(m.RoundStarts)-1]
}
}
}
case sdl.K_e:
if isShiftPressed(eventT) {
for _, frame := range m.HalfStarts {
if app.curFrame < frame {
app.curFrame = frame
break
}
}
} else {
for _, frame := range m.RoundStarts {
if app.curFrame < frame {
app.curFrame = frame
break
}
}
}
case sdl.K_h:
app.hidePlayerNames = !app.hidePlayerNames
case sdl.K_0:
app.copyPositionToClipboard(9)
case sdl.K_1:
app.copyPositionToClipboard(0)
case sdl.K_2:
app.copyPositionToClipboard(1)
case sdl.K_3:
app.copyPositionToClipboard(2)
case sdl.K_4:
app.copyPositionToClipboard(3)
case sdl.K_5:
app.copyPositionToClipboard(4)
case sdl.K_6:
app.copyPositionToClipboard(5)
case sdl.K_7:
app.copyPositionToClipboard(6)
case sdl.K_8:
app.copyPositionToClipboard(7)
case sdl.K_9:
app.copyPositionToClipboard(8)
case sdl.K_w:
if isShiftPressed(eventT) {
break
}
switch app.staticPlaybackSpeedModifier {
case 0.25:
app.staticPlaybackSpeedModifier = 0.5
case 0.5:
app.staticPlaybackSpeedModifier = 1
case 1:
app.staticPlaybackSpeedModifier = 1.25
case 1.25:
app.staticPlaybackSpeedModifier = 1.5
case 1.5:
app.staticPlaybackSpeedModifier = 2
}
case sdl.K_s:
if isShiftPressed(eventT) {
break
}
switch app.staticPlaybackSpeedModifier {
case 0.5:
app.staticPlaybackSpeedModifier = 0.25
case 1:
app.staticPlaybackSpeedModifier = 0.5
case 1.25:
app.staticPlaybackSpeedModifier = 1
case 1.5:
app.staticPlaybackSpeedModifier = 1.25
case 2:
app.staticPlaybackSpeedModifier = 1.5
}
case sdl.K_r:
app.staticPlaybackSpeedModifier = 1
case sdl.K_k:
_ = sdl.ShowSimpleMessageBox(sdl.MESSAGEBOX_INFORMATION, "Hotkeys", hotkeysString, nil)
}
}
func (app *app) copyPositionToClipboard(player int) {
m := app.match
players := m.States[app.curFrame].Players
if player >= len(players) {
return
}
sort.Slice(players, func(i, j int) bool {
if players[i].Team > players[j].Team {
return true
}
if players[i].Team < players[j].Team {
return false
}
return players[i].ID < players[j].ID
})
err := clipboard.WriteAll("setpos " +
strconv.FormatFloat(float64(m.States[app.curFrame].Players[player].Position.X), 'f', 2, 32) + " " +
strconv.FormatFloat(float64(m.States[app.curFrame].Players[player].Position.Y), 'f', 2, 32) + " " +
strconv.FormatFloat(float64(m.States[app.curFrame].Players[player].Position.Z), 'f', 2, 32) +
";setang " +
strconv.FormatFloat(float64(m.States[app.curFrame].Players[player].ViewDirectionY), 'f', 2, 32) + " " +
strconv.FormatFloat(float64(m.States[app.curFrame].Players[player].ViewDirectionX), 'f', 2, 32))
if err != nil {
errorString := fmt.Sprintf("Trying to write to clipboard:\n%v", err)
_ = sdl.ShowSimpleMessageBox(sdl.MESSAGEBOX_ERROR, "Error", errorString, nil)
}
}
func (app *app) updateWindowTitle() {
m := app.match
cts := m.States[app.curFrame].TeamCounterTerrorists
ts := m.States[app.curFrame].TeamTerrorists
clanNameCTs := cts.ClanName
if clanNameCTs == "" {
clanNameCTs = "Counter Terrorists"
}
clanNameTs := ts.ClanName
if clanNameTs == "" {
clanNameTs = "Terrorists"
}
windowTitle := fmt.Sprintf("%s [%d:%d] %s - Round %d", clanNameCTs, cts.Score, ts.Score, clanNameTs, cts.Score+ts.Score+1)
// expensive?
app.window.SetTitle(windowTitle)
}
func (app *app) updateGraphics() {
m := app.match
err := app.renderer.SetDrawColor(10, 10, 10, 255)
if err != nil {
log.Println("Trying to set draw color: " + err.Error())
}
err = app.renderer.Clear()
if err != nil {
log.Println("Trying to clear renderer: " + err.Error())
}
app.lastDrawnAt = time.Now().UTC()
app.drawInfobars()
err = app.renderer.Copy(app.mapTexture, nil, app.mapRect)
if err != nil {
log.Println("Trying to copy texture to renderer: " + err.Error())
}
shots := m.Shots[app.curFrame]
for _, shot := range shots {
app.drawShot(&shot)
}
infernos := m.States[app.curFrame].Infernos
for _, inferno := range infernos {
app.drawInferno(&inferno)
}
effects := m.Effects[app.curFrame]
for _, effect := range effects {
app.drawEffects(&effect)
}
grenades := m.States[app.curFrame].Grenades
for _, grenade := range grenades {
app.drawGrenade(&grenade)
}
bomb := m.States[app.curFrame].Bomb
app.drawBomb(&bomb)
players := m.States[app.curFrame].Players
var indexT, indexCT int
for _, player := range players {
if player.Team == demoinfo.TeamTerrorists {
app.drawPlayer(&player, indexT)
indexT++
} else {
app.drawPlayer(&player, indexCT)
indexCT++
}
}
app.drawString("k shows hotkeys", colorDarkGrey, mapXOffset+mapOverviewWidth+150, mapYOffset+mapOverviewHeight-40)
app.renderer.Present()
}
func isShiftPressed(event *sdl.KeyboardEvent) bool {
pressed := event.Keysym.Mod & sdl.KMOD_SHIFT
return pressed > 0
}