-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp-update.go
373 lines (305 loc) · 9.1 KB
/
app-update.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
package main
import (
"archive/zip"
"context"
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
"github.com/machinebox/graphql"
)
type gqlResponse struct {
DolphinVersion dolphinVersion `json:"getLatestDolphin"`
}
type dolphinVersion struct {
URL string `json:"windowsDownloadUrl"`
Version string `json:"version"`
}
func execAppUpdate(isFull, skipUpdaterUpdate, shouldLaunch bool, isoPath, prevVersion string) (returnErr error) {
defer func() {
if r := recover(); r != nil {
returnErr = errors.New("Error encountered updating app")
}
}()
// Get executable path
ex, err := os.Executable()
if err != nil {
log.Panic(err)
}
exPath := filepath.Dir(ex)
oldSlippiToolsPath := filepath.Join(exPath, "old-dolphin-slippi-tools.exe")
// If we are doing a full update or if we are done updating the updater, wait for Dolphin to close
if isFull || skipUpdaterUpdate {
waitForDolphinClose()
}
isBeta := strings.Contains(prevVersion, "-beta")
latest := getLatestVersion(isBeta)
dir, err := ioutil.TempDir("", "dolphin-update")
if err != nil {
log.Panic(err)
}
defer os.RemoveAll(dir)
zipFilePath := filepath.Join(dir, "dolphin.zip")
err = downloadFile(zipFilePath, latest.URL)
if err != nil {
log.Panic(err)
}
if !isFull && !skipUpdaterUpdate {
prevVersionDisplay := prevVersion
if prevVersionDisplay == "" {
prevVersionDisplay = "unknown"
}
fmt.Printf("Preparing to update app from %s to %s...\n", prevVersionDisplay, latest.Version)
slippiToolsPath := filepath.Join(exPath, "dolphin-slippi-tools.exe")
// If we get here, we need to extract the updater. Start by renaming the current updater
err = os.Rename(slippiToolsPath, oldSlippiToolsPath)
if err != nil {
log.Panicf("Failed to rename slippi tools. %s", err.Error())
}
// Now extract the updater
err = extractFiles(exPath, zipFilePath, updaterUpdateGen)
if err != nil {
log.Panic(err)
}
// Launch the new updater
launchArg := fmt.Sprintf("-launch=%t", shouldLaunch)
cmd := exec.Command(slippiToolsPath, "app-update", "-skip-updater", launchArg, "-iso", isoPath, "-version", prevVersion)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stdout
err = cmd.Start()
if err != nil {
log.Panicf("Failed to start app-update with new updater. %s", err.Error())
}
} else {
fmt.Printf("\n\nIMPORTANT:\nThis updater will soon no longer work. Future updates will be through the Slippi Launcher. We recommend switching at your earliest convenience. You can download it from slippi.gg\n\n")
fmt.Printf("Your update will resume shortly, please read warning above...")
time.Sleep(5000 * time.Millisecond)
// Delete old-dolphin-slippi-tools.exe if it exists. Deleting here because we should have waited
// for Dolphin to close which means the previous updater should no longer be running
os.RemoveAll(oldSlippiToolsPath)
// After 2.2.0 we stopped supporting non-melee games by default, this will delete all old inis
applyMeleeOnlyChanges(prevVersion, exPath)
// Delete previous install
err := deletePrevious(exPath)
if err != nil {
log.Panicf("Failed to delete old install. %s\n", err.Error())
}
// Extract all non-exe files used for update
err = extractFiles(exPath, zipFilePath, fullUpdateGen)
if err != nil {
log.Panic(err)
}
// Now extract the exe (do this last such that we can avoid a partial update)
err = extractFiles(exPath, zipFilePath, exeUpdateGen)
if err != nil {
log.Panic(err)
}
if shouldLaunch {
// Launch Dolphin
cmd := exec.Command(filepath.Join(exPath, "Slippi Dolphin.exe"), "-e", isoPath)
cmd.Start()
if err != nil {
log.Panicf("Failed to start Dolphin. %s", err.Error())
}
}
}
return nil
}
func waitForDolphinClose() {
// TODO: Look for specific dolphin process?
fmt.Printf("\nYou can find release notes at: https://github.com/project-slippi/Ishiiruka/releases \n\n")
fmt.Println("Waiting for Dolphin to close. Ensure ALL Dolphin instances are closed. Can take a few moments after they are all closed...")
for {
cmd, _ := exec.Command("TASKLIST", "/FI", "IMAGENAME eq Dolphin.exe").Output()
output := string(cmd[:])
splitOutp := strings.Split(output, "\n")
if len(splitOutp) > 3 {
time.Sleep(500 * time.Millisecond)
//fmt.Println("Process is running...")
continue
}
cmd, _ = exec.Command("TASKLIST", "/FI", "IMAGENAME eq Slippi Dolphin.exe").Output()
output = string(cmd[:])
splitOutp = strings.Split(output, "\n")
if len(splitOutp) > 3 {
time.Sleep(500 * time.Millisecond)
//fmt.Println("Process is running...")
continue
}
// If we get here, process is gone
break
}
}
func extractFiles(target, source string, genTargetFile func(string) string) error {
reader, err := zip.OpenReader(source)
if err != nil {
return err
}
defer reader.Close()
// First find Dolphin.exe
dolphinPath := ""
for _, file := range reader.File {
filePathName := file.Name
baseFile := filepath.Base(filePathName)
if baseFile == "Dolphin.exe" || baseFile == "Slippi Dolphin.exe" {
dolphinPath = filepath.Dir(filePathName)
break
}
}
// Path pattern
dolphinPathPattern := filepath.ToSlash(filepath.Join(dolphinPath, "*"))
// Iterate through all files, deciding whether to extract
for _, file := range reader.File {
isMatch, err := filepath.Match(dolphinPathPattern, file.Name)
if err != nil || !isMatch {
continue
}
relPath, err := filepath.Rel(dolphinPath, file.Name)
if err != nil {
continue
}
targetRelPath := genTargetFile(relPath)
if targetRelPath == "" {
continue
}
// Generate target path
path := filepath.Join(target, targetRelPath)
if file.FileInfo().IsDir() {
os.MkdirAll(path, file.Mode())
continue
}
fileReader, err := file.Open()
if err != nil {
return err
}
defer fileReader.Close()
start := time.Now()
for time.Now().Sub(start) < (time.Second * 20) {
targetFile, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, file.Mode())
if err != nil {
log.Printf("Failed to open file for write, will try again: %s\n", path)
time.Sleep(time.Second)
continue
}
defer targetFile.Close()
if _, err := io.Copy(targetFile, fileReader); err != nil {
log.Printf("Failed to copy file, will try again: %s\n", path)
time.Sleep(time.Second)
continue
}
// If everything succeeded, break immediately
break
}
// Return error if there was one above and we timed out
if err != nil {
return err
}
log.Printf("Finished copying file: %s\n", path)
}
return nil
}
func fullUpdateGen(path string) string {
slashPath := filepath.ToSlash(path)
// Check if Dolphin.exe
if slashPath == "Dolphin.exe" || slashPath == "Slippi Dolphin.exe" {
return ""
}
if slashPath == "dolphin-slippi-tools.exe" {
return ""
}
return path
}
func updaterUpdateGen(path string) string {
if path == "dolphin-slippi-tools.exe" {
return path
}
return ""
}
func exeUpdateGen(path string) string {
slashPath := filepath.ToSlash(path)
// Check if Dolphin.exe
if slashPath == "Dolphin.exe" || slashPath == "Slippi Dolphin.exe" {
return path
}
return ""
}
func deletePrevious(path string) error {
err := os.RemoveAll(filepath.Join(path, "Dolphin.exe"))
if err != nil {
return err
}
err = os.RemoveAll(filepath.Join(path, "Slippi Dolphin.exe"))
if err != nil {
return err
}
err = os.RemoveAll(filepath.Join(path, "Sys"))
if err != nil {
return err
}
return nil
}
func getLatestVersion(isBeta bool) dolphinVersion {
// TODO: Cache response?
client := graphql.NewClient("https://gql-gateway-dot-slippi.uc.r.appspot.com/graphql")
req := graphql.NewRequest(`
query GetLatestDolphin($includeBeta: Boolean) {
getLatestDolphin(includeBeta: $includeBeta) {
windowsDownloadUrl
version
}
}
`)
req.Var("includeBeta", isBeta)
ctx := context.Background()
var resp gqlResponse
err := client.Run(ctx, req, &resp)
if err != nil {
log.Printf("Failed to fetch version info from graphql server, got %s", err.Error())
}
return resp.DolphinVersion
}
// DownloadFile will download a url to a local file. It's efficient because it will
// write as it downloads and not load the whole file into memory.
// Taken from: https://golangcode.com/download-a-file-from-a-url/
func downloadFile(filepath string, url string) error {
// Get the data
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
// Create the file
out, err := os.Create(filepath)
if err != nil {
return err
}
defer out.Close()
// Write the body to file
_, err = io.Copy(out, resp.Body)
return err
}
func applyMeleeOnlyChanges(prevVersion, exPath string) {
if prevVersion != "" {
// Before version 2.2.1, we didn't include previous version, so if this isn't empty,
// we shouldn't be deleting these files
return
}
gameSettingsPath := filepath.Join(exPath, "Sys", "GameSettings")
log.Printf("Cleaning up old files...")
// Attempt to delete all files inside the Sys/GameSettings folder
dir, err := ioutil.ReadDir(gameSettingsPath)
for _, d := range dir {
err = os.RemoveAll(filepath.Join(gameSettingsPath, d.Name()))
if err != nil {
log.Panic(err)
}
}
log.Printf("Cleanup complete")
}