-
Notifications
You must be signed in to change notification settings - Fork 45
/
main.go
executable file
·436 lines (370 loc) · 12.7 KB
/
main.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
package main
import (
"context"
"encoding/json"
"errors"
"flag"
"fmt"
"io"
"net/http"
"os"
"os/exec"
"os/signal"
"runtime/debug"
"sync"
"syscall"
"time"
"unsafe"
"github.com/blang/semver/v4"
"github.com/inconshreveable/go-update"
"github.com/optix2000/totsugeki/patcher"
"github.com/optix2000/totsugeki/proxy"
"golang.org/x/sys/windows"
)
//go:generate go-winres make --product-version=git-tag --file-version=git-tag
// Filled in at build time
var Version string = "(unknown version)"
var UngaBungaMode string = ""
const GGStriveExe = "GGST-Win64-Shipping.exe"
const APIOffsetAddr uintptr = 0x34D23F8 // 1.16
const PatchRetries = 3
const GGStriveAPIURL = "https://ggst-game.guiltygear.com/api/"
const PatchedAPIURL = "http://127.0.0.1:21611/api/"
const GithubDownloadURL = "https://github.com/optix2000/totsugeki/releases/latest/download/"
const GithubReleasesURL = "https://api.github.com/repos/optix2000/totsugeki/releases/latest"
const UpdateTimeout = 30 * time.Second
const UpdateName = "totsugeki.exe"
const UpdateNameUngaBunga = "totsugeki-unga-bunga.exe"
const totsugeki = " _____ _ _ _ \n" +
"|_ _|___ | |_ ___ _ _ __ _ ___ | | __(_)\n" +
" | | / _ \\ | __|/ __|| | | | / _` | / _ \\| |/ /| |\n" +
" | || (_) || |_ \\__ \\| |_| || (_| || __/| < | |\n" +
" |_| \\___/ \\__||___/ \\__,_| \\__, | \\___||_|\\_\\|_|\n" +
" |___/ "
var server *proxy.StriveAPIProxy
var sig chan os.Signal
var modKernel32 *windows.LazyDLL = windows.NewLazySystemDLL("kernel32.dll")
var procSetConsoleTitle *windows.LazyProc = modKernel32.NewProc("SetConsoleTitleW")
type Release = struct {
TagName string `json:"tag_name"`
}
func panicBox(v interface{}) {
const header = `Totsugeki has encountered a fatal error.
Please report this to https://github.com/optix2000/totsugeki/issues
===================
Error: %v
%v`
messageBox(fmt.Sprintf(header, v, string(debug.Stack())))
panic(v)
}
func messageBox(message string) {
msg, e := windows.UTF16PtrFromString(message)
if e != nil {
fmt.Println(e)
panic(e)
}
_, e = windows.MessageBox(0, msg, nil, windows.MB_OK|windows.MB_ICONWARNING|windows.MB_SETFOREGROUND)
if e != nil {
fmt.Println(e)
panic(e)
}
}
func cancelableSleep(ctx context.Context, delay time.Duration) {
wait, waitCancel := context.WithTimeout(ctx, delay)
<-wait.Done()
waitCancel()
}
func clearScreen() {
handle := windows.Handle(os.Stdout.Fd())
var mode uint32
if windows.GetConsoleMode(handle, &mode) == nil {
if windows.SetConsoleMode(handle, (mode|windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING)) == nil {
windows.SetConsoleCursorPosition(handle, windows.Coord{X: 0, Y: 0})
fmt.Printf("\x1b[2J") // Clear screen
}
windows.SetConsoleMode(handle, mode) // Reset console even on failure since it's technically set with an invalid setting.
}
}
func disableQuickEdit() {
handle, err := windows.GetStdHandle(windows.STD_INPUT_HANDLE)
if err == nil {
defer windows.CloseHandle(handle)
var mode uint32
if windows.GetConsoleMode(handle, &mode) == nil {
windows.SetConsoleMode(handle, (mode&^windows.ENABLE_QUICK_EDIT_MODE)|windows.ENABLE_EXTENDED_FLAGS) // https://docs.microsoft.com/en-us/windows/console/setconsolemode
}
}
}
// Patch GGST as it starts
func watchGGST(noClose bool, ctx context.Context) {
var patchedPid uint32 = 1
var close bool = false
for {
select {
case <-ctx.Done():
return
default:
pid, err := patcher.GetProc(GGStriveExe)
if err != nil {
if errors.Is(err, patcher.ErrProcessNotFound) {
if close {
sig <- os.Interrupt // Gracefully shutdown
return
}
if patchedPid != 0 {
fmt.Println("Waiting for GGST process...")
patchedPid = 0
}
cancelableSleep(ctx, 2*time.Second)
continue
} else {
panic(err)
}
}
if pid == patchedPid {
cancelableSleep(ctx, 5*time.Second)
continue
} else if patchedPid > 1 { // Catch case where GGST restarts in between detection loop.
continue
}
var retry int
for retry = 0; retry < PatchRetries; retry++ {
cancelableSleep(ctx, 1000*time.Millisecond) // Give GGST some time to finish loading. EnumProcessModules() doesn't like modules changing while it's running.
var offset uintptr
offset, err = patcher.PatchProc(pid, GGStriveExe, APIOffsetAddr, []byte(GGStriveAPIURL), []byte(PatchedAPIURL))
if errors.Is(err, patcher.ErrOffsetMismatch) {
fmt.Printf("WARNING: Offset found at unknown location. This version of Totsugeki has not been tested with this version of GGST and may cause issues.\n")
err = nil
}
if err != nil {
if errors.Is(err, patcher.ErrProcessAlreadyPatched) {
fmt.Printf("GGST with PID %d is already patched at offset 0x%x.\n", pid, offset)
if !noClose {
close = true
}
break
} else if errors.Unwrap(err) == syscall.Errno(windows.ERROR_ACCESS_DENIED) {
messageBox("Could not patch GGST. Steam/GGST may be running as Administrator. Try re-running Totsugeki as Administrator.")
os.Exit(1)
} else {
fmt.Printf("Error with PID %d at offset 0x%x: %v\n", pid, offset, err)
continue
}
} else {
fmt.Printf("Patched GGST with PID %d at offset 0x%x.\n", pid, offset)
if !noClose {
close = true
}
break
}
}
if retry >= PatchRetries {
break
}
patchedPid = pid
}
}
}
func autoUpdate() error {
url := GithubDownloadURL + UpdateName
if UngaBungaMode != "" {
url = GithubDownloadURL + UpdateNameUngaBunga
}
client := http.Client{
Timeout: UpdateTimeout,
}
resp, err := client.Get(GithubReleasesURL)
if err != nil {
return errors.New("could not get latest release version number")
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return errors.New("could not read latest release version")
}
release := &Release{}
err = json.Unmarshal(body, release)
if err != nil {
return errors.New("could not read JSON response with version number")
}
currentVersion, err := semver.ParseTolerant(Version)
if err != nil {
return errors.New("could not parse current version number")
}
latestVersion, err := semver.ParseTolerant(release.TagName)
if err != nil {
return errors.New("could not parse latest version number")
}
if currentVersion.LT(latestVersion) { // Only update if newer and we are running a stable version.
if len(currentVersion.Pre) != 0 {
if currentVersion.Pre[0].String() == "unga-bunga" {
if currentVersion.FinalizeVersion() == latestVersion.FinalizeVersion() { // Versions are actually the same
return nil
}
} else {
fmt.Printf("New version v%v available, but cannot update from test/pre-release version v%v.\n", latestVersion, currentVersion)
return nil
}
}
exePath, err := os.Executable()
if err != nil {
return errors.New("could not get executable path")
}
fmt.Println("New version released, downloading...")
resp, err := http.Get(url)
if err != nil {
return errors.New("could not download new version")
}
defer resp.Body.Close()
err = update.Apply(resp.Body, update.Options{})
if err != nil {
return errors.New("could not update totsugeki")
}
clearScreen()
command := exec.Command(exePath, os.Args[1:]...)
command.Stdout = os.Stdout
command.Stderr = os.Stderr
command.Stdin = os.Stdin
err = command.Start()
if err != nil {
return errors.New("could not start new totsugeki version")
}
os.Exit(0)
}
return nil
}
func main() {
var noProxy = flag.Bool("no-proxy", false, "Don't start local proxy. Useful if you want to run your own proxy.")
var noLaunch = flag.Bool("no-launch", false, "Don't launch GGST. Useful if you want to launch GGST through other means.")
var noPatch = flag.Bool("no-patch", false, "Don't patch GGST with proxy address.")
var noClose = flag.Bool("no-close", false, "Don't automatically close totsugeki alongside GGST.")
var noUpdate = flag.Bool("no-update", false, "Don't check for totsugeki updates.")
var unsafeAsyncStatsSet = flag.Bool("unsafe-async-stats-set", false, "UNSAFE: Asynchronously upload stats (R-Code) in the background.")
var unsafePredictStatsGet = flag.Bool("unsafe-predict-stats-get", false, "UNSAFE: Asynchronously precache expected statistics/get calls.")
var unsafeCacheNews = flag.Bool("unsafe-cache-news", false, "UNSAFE: Cache first news call and return cached version on subsequent calls.")
var unsafeNoNews = flag.Bool("unsafe-no-news", false, "UNSAFE: Return an empty response for news.")
var unsafePredictReplay = flag.Bool("unsafe-predict-replay", false, "UNSAFE: Asynchronously precache expected get_replay calls. Needs unsafe-predict-stats-get to work.")
var unsafeCacheEnv = flag.Bool("unsafe-cache-env", false, "UNSAFE: Cache first get_env call and return cached version on subsequent calls.")
var unsafeCacheFollow = flag.Bool("unsafe-cache-follow", false, "UNSAFE: Cache first get_follow and get_block calls and return cached version on subsequent calls.")
var ungaBunga = flag.Bool("unga-bunga", UngaBungaMode != "", "UNSAFE: Enable all unsafe speedups for maximum speed. Please read https://github.com/optix2000/totsugeki/blob/master/UNSAFE_SPEEDUPS.md")
var iKnowWhatImDoing = flag.Bool("i-know-what-im-doing", false, "UNSAFE: Suppress any UNSAFE warnings. I hope you know what you're doing...")
var ratingUpdate = flag.Bool("rating-update", false, "Display ratings from ratingupdate.info instead of character levels.")
var ver = flag.Bool("version", false, "Print the version number and exit.")
flag.Parse()
if *ver {
fmt.Printf("totsugeki %v\n", Version)
os.Exit(0)
}
title, err := windows.UTF16PtrFromString(fmt.Sprintf("Totsugeki %v", Version))
if err == nil {
procSetConsoleTitle.Call(uintptr(unsafe.Pointer(title)))
}
disableQuickEdit()
fmt.Println(totsugeki)
fmt.Printf(" %s\n", Version)
// Raise an alert box on panic so non-technical users don't lose the output.
defer func() {
r := recover()
if r != nil {
panicBox(r)
}
}()
if !*noUpdate && Version != "(unknown version)" {
err := autoUpdate()
if err != nil {
fmt.Printf("Failed to update totsugeki: %v.\n", err.Error())
}
}
if *ungaBunga { // Mash only
*unsafeAsyncStatsSet = true
*unsafePredictStatsGet = true
*unsafeNoNews = true
*unsafePredictReplay = true
*unsafeCacheEnv = true
*unsafeCacheFollow = true
}
// Drop process priority
handle := windows.CurrentProcess()
err = windows.SetPriorityClass(handle, windows.BELOW_NORMAL_PRIORITY_CLASS)
if err != nil {
fmt.Println(err)
}
windows.CloseHandle(handle)
// Launch GGST if it's not already running
if !*noLaunch {
_, err := patcher.GetProc(GGStriveExe)
if err != nil {
if errors.Is(err, patcher.ErrProcessNotFound) {
fmt.Println("Starting GGST...")
err = exec.Command("rundll32", "url.dll,FileProtocolHandler", "steam://rungameid/1384160").Start()
if err != nil {
fmt.Println(err)
}
} else {
panic(err)
}
}
}
var wg sync.WaitGroup
ctx, cancel := context.WithCancel(context.Background()) // Context for graceful shutdown
defer cancel()
sig = make(chan os.Signal, 1)
// Start Patcher
if !*noPatch {
wg.Add(1)
go func() {
// Raise an alert box on panic so non-technical users don't lose the output.
defer func() {
r := recover()
if r != nil {
panicBox(r)
}
}()
defer wg.Done()
watchGGST(*noClose, ctx)
}()
}
// Proxy side
if !*noProxy {
wg.Add(1)
go func() {
// Raise an alert box on panic so non-technical users don't lose the output.
defer func() {
r := recover()
if r != nil {
panicBox(r)
}
}()
defer wg.Done()
server = proxy.CreateStriveProxy("127.0.0.1:21611", GGStriveAPIURL, PatchedAPIURL, &proxy.StriveAPIProxyOptions{
AsyncStatsSet: *unsafeAsyncStatsSet,
PredictStatsGet: *unsafePredictStatsGet,
CacheNews: *unsafeCacheNews,
NoNews: *unsafeNoNews,
PredictReplay: *unsafePredictReplay,
CacheEnv: *unsafeCacheEnv,
CacheFollow: *unsafeCacheFollow,
RatingUpdate: *ratingUpdate,
})
fmt.Println("Started Proxy Server on port 21611.")
err := server.Server.ListenAndServe()
if err != nil {
if !errors.Is(err, http.ErrServerClosed) {
panic(err)
}
}
}()
// Watch for signal to do graceful shutdown
wg.Add(1)
go func() {
defer wg.Done()
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
<-sig
cancel()
server.Shutdown()
}()
}
if !*iKnowWhatImDoing && (*unsafeAsyncStatsSet || *unsafePredictStatsGet || *unsafeCacheNews || *unsafeNoNews || *unsafeCacheEnv || *unsafePredictReplay || *unsafeCacheFollow) {
fmt.Println("WARNING: Unsafe feature used. Make sure you understand the implications: https://github.com/optix2000/totsugeki/blob/master/UNSAFE_SPEEDUPS.md")
}
wg.Wait()
}