Repository navigation
Expand file tree
/
Copy pathproton.go
More file actions
157 lines (127 loc) · 3.67 KB
/
Copy pathproton.go
File metadata and controls
157 lines (127 loc) · 3.67 KB
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
package main
import (
"fmt"
"os/exec"
"path"
"strconv"
"strings"
"github.com/NatoBoram/BlackCompany/log"
"github.com/aiseeq/s2l/protocol/api"
"github.com/aiseeq/s2l/protocol/client"
"github.com/shirou/gopsutil/v4/process"
)
// launchProton launches StarCraft II using Proton.
//
// - https://levelup.gitconnected.com/guide-to-starcraft-ii-proto-api-264811da8a50
// - https://gist.github.com/michaelbutler/f364276f4030c5f449252f2c4d960bd2
func launchProton(paths *Sc2Paths, flags Flags) error {
client.SetExecutable(paths.Exe)
args := []string{
"run", paths.Exe,
"-displaymode", strconv.Itoa(flags.DisplayMode),
"-listen", flags.Listen,
"-port", strconv.Itoa(flags.Port),
}
if flags.Realtime {
log.Info("Running StarCraft II in real-time")
args = append(args, "-realtime", strconv.FormatBool(flags.Realtime))
client.SetRealtime()
}
if flags.Timeout > 0 {
args = append(args, "-timeout", flags.Timeout.String())
client.SetConnectTimeout(flags.Timeout)
}
if flags.WindowWidth > 0 {
args = append(args, "-windowwidth", strconv.Itoa(flags.WindowWidth))
}
if flags.WindowHeight > 0 {
args = append(args, "-windowheight", strconv.Itoa(flags.WindowHeight))
}
if flags.WindowX > 0 {
args = append(args, "-windowx", strconv.Itoa(flags.WindowX))
}
if flags.WindowY > 0 {
args = append(args, "-windowy", strconv.Itoa(flags.WindowY))
}
cmd := exec.Command(paths.Proton, args...)
cmd.Dir = path.Join(paths.Sc2, "Support64")
running, err := runningProton()
if err != nil {
return fmt.Errorf("failed to check if Proton is running: %w", err)
}
if running != nil {
log.Info("Proton is already running")
return nil
}
log.Info("Launching StarCraft II using Proton")
go func() {
if err := cmd.Run(); err != nil {
log.Fatal("failed to run StarCraft II: %v", err)
}
}()
return nil
}
func runningProton() (*process.Process, error) {
processes, err := process.Processes()
if err != nil {
return nil, fmt.Errorf("failed to get processes: %w", err)
}
for _, p := range processes {
exe, err := p.Exe()
if err != nil {
continue
}
if strings.HasSuffix(exe, "/files/bin/wineserver") {
return p, nil
}
}
return nil, nil
}
func protonConfig(bot *api.PlayerSetup, participants ...*api.PlayerSetup) *client.GameConfig {
mapPath := random1v1Map()
log.Info("Using map %q\n", mapPath)
client.SetMap(mapPath)
participants = append([]*api.PlayerSetup{bot}, participants...)
config := client.NewGameConfig(participants...)
config.Connect(8168)
config.StartGame(mapPath)
return config
}
func replayConfig(flags Flags) (*client.GameConfig, error) {
if flags.Replay == "" {
log.Fatal("no replay file provided")
}
if flags.Map == "" {
log.Fatal("no map provided")
}
client.SetMap(flags.Map + ".SC2Map")
observer := client.NewParticipant(api.Race_NoRace, "Observer")
config := client.NewGameConfig(observer)
config.Connect(8168)
replayPath := path.Join("C:\\Program Files (x86)\\StarCraft II\\Replays", flags.Replay)
request := &api.Request{Request: &api.Request_StartReplay{
StartReplay: &api.RequestStartReplay{
Replay: &api.RequestStartReplay_ReplayPath{
ReplayPath: replayPath,
},
ObservedPlayerId: 1,
Options: &api.InterfaceOptions{
Raw: true,
Score: true,
},
DisableFog: false,
Realtime: false,
},
}}
response, err := config.Client.Request(request)
if err != nil {
return nil, fmt.Errorf("failed to start replay: %w", err)
}
if response.GetStartReplay().GetError() != api.ResponseStartReplay_nil {
return nil, fmt.Errorf("failed to start replay. Error: %v, Details: %v",
response.GetStartReplay().GetError(),
response.GetStartReplay().GetErrorDetails(),
)
}
return config, nil
}