-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathinstaller.go
400 lines (337 loc) · 10.8 KB
/
installer.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
package main
import (
"fmt"
"github.com/charmbracelet/huh"
"github.com/charmbracelet/huh/spinner"
"github.com/charmbracelet/log"
"io"
"net/http"
"os"
"path/filepath"
"strings"
"sync"
"time"
)
type installationInfo struct {
installationFolder string
downloadURL string
gameRegion Region
LatestVersion string
}
type installationProcess struct {
games map[string]installationInfo
updateMode bool
selectedGames []string
installationLinks []string
installationFolders []string
successfullyInstalled []string
}
func newInstallationProcess() *installationProcess {
return &installationProcess{
games: make(map[string]installationInfo),
}
}
func (ip *installationProcess) addGame(title string) {
ip.games[title] = installationInfo{}
}
func (ip *installationProcess) updateGameRegion(title string, region Region) {
if game, exists := ip.games[title]; exists {
game.gameRegion = region
ip.games[title] = game
} else {
log.Fatalf("Game title '%s' does not exist.", title)
}
}
func (ip *installationProcess) updateGameLatestRelease(title, latestRelease string) {
if game, exists := ip.games[title]; exists {
game.LatestVersion = latestRelease
ip.games[title] = game
} else {
log.Fatalf("Game title '%s' does not exist.", title)
}
}
func (ip *installationProcess) getGameInfo(title string) (installationInfo, bool) {
gameInfo, exists := ip.games[title]
return gameInfo, exists
}
func startCheatInstallation() {
ip := newInstallationProcess()
supportedTitles := config.getSupportedGameTitles()
if len(supportedTitles) == 0 {
fmt.Println("\n\nIt seems we've hit a dead end. No supported games could be found. The Pipsi project may have quietly faded away, or perhaps you're trying to use this tool long after it was abandoned. There's a chance it's just a temporary issue with the cheat catalog, maybe a lost connection or a server that no longer answers. Whatever it is, we regret the inconvenience, and we can only hope that things turn around someday.")
time.Sleep(10 * time.Second)
os.Exit(0)
return
}
form := huh.NewForm(
huh.NewGroup(
huh.NewMultiSelect[string]().
Title("Which game(s) would you like to install Pipsi for?").
Options(huh.NewOptions(supportedTitles...)...).
Value(&ip.selectedGames),
),
).WithAccessible(IsAccessible())
err := form.Run()
if err != nil {
log.Fatal(err)
}
if len(ip.selectedGames) == 0 {
showMainMenu()
return
}
for _, game := range ip.selectedGames {
ip.addGame(game)
link := config.getInstallationLink(game)
if link == "" {
log.Errorf("No installation link found for game: %s", game)
continue
}
ip.installationLinks = append(ip.installationLinks, link)
folder := config.getInstallationFolder(game)
if folder == "" {
log.Errorf("No installation folder found for game: %s", game)
continue
}
ip.installationFolders = append(ip.installationFolders, folder)
}
if len(ip.installationLinks) == 0 || len(ip.installationLinks) != len(ip.installationFolders) {
log.Fatal("Error: No valid installation links or folders found, or mismatched data.")
return
}
ip.startDownload()
hasInstalledCheats := len(ip.successfullyInstalled) > 0
showShortcutMenu(ip.successfullyInstalled)
if hasInstalledCheats {
log.Infof("Successfully installed Pipsi for %s.", formatGameList(ip.successfullyInstalled))
}
fmt.Print("\n")
if hasInstalledCheats {
go func() {
createPipsiShortcuts(ip.successfullyInstalled, currentDir)
}()
fmt.Println(
WarningText.Render("NOTE: ") +
HighlightText.Render("Press Insert (or FN + Insert on laptops) in-game to toggle the cheats."),
)
}
pauseAndReturnToMainMenu()
}
func startCheatUpdate(updateMap map[string]map[string]string) {
defer func() {
if r := recover(); r != nil {
log.Errorf("Recovered from panic in startCheatUpdate: %v", r)
}
}()
ip := newInstallationProcess()
ip.updateMode = true
for title, details := range updateMap {
downloadURL, latestRelease := details["downloadURL"], details["latestVersion"]
ip.selectedGames = append(ip.selectedGames, title)
ip.addGame(title)
ip.installationLinks = append(ip.installationLinks, downloadURL)
ip.updateGameLatestRelease(title, latestRelease)
folder := config.getInstallationFolder(title)
if folder == "" {
log.Errorf("No installation folder found for game: %s", title)
continue
}
ip.installationFolders = append(ip.installationFolders, folder)
}
if len(ip.installationLinks) == 0 || len(ip.installationLinks) != len(ip.installationFolders) {
log.Error("Error: No valid installation links or folders found, or mismatched data.")
return
}
ip.startDownload()
hasInstalledCheats := len(ip.successfullyInstalled) > 0
if hasInstalledCheats {
log.Infof("Successfully updated Pipsi for %s.", formatGameList(ip.successfullyInstalled))
}
fmt.Print("\n")
pauseAndReturnToMainMenu()
}
func (ip *installationProcess) startDownload() {
action := func() {
manageDefenderExclusion()
}
if err := spinner.New().Title("Configuring Microsoft Defender Antivirus...").Action(action).Run(); err != nil {
log.Errorf("Spinner widget error: %v", err)
pauseAndReturnToMainMenu()
}
var wg sync.WaitGroup
joinedTitles := formatGameList(ip.selectedGames)
log.Infof("Beginning to download Pipsi for %s...", joinedTitles)
for i := 0; i < len(ip.installationLinks); i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
title := ip.selectedGames[i]
downloadURL := ip.installationLinks[i]
installationFolder := ip.installationFolders[i]
installDir := fmt.Sprintf("Pipsi Installations/%s", installationFolder)
if err := os.MkdirAll(installDir, os.ModePerm); err != nil {
log.Errorf("Failed to create directory for %s: %v", title, err)
return
}
filePath := fmt.Sprintf("%s/x64.rar", installDir)
if err := ip.downloadFile(downloadURL, filePath, title); err != nil {
log.Errorf("Download failed for %s: %v", title, err)
return
}
gameData := cheatInstallationData.getGame(title)
if gameData != nil {
installationInfo, _ := ip.getGameInfo(title)
gameData.Installed = true
gameData.InstalledVersion = installationInfo.LatestVersion
gameData.Region = installationInfo.gameRegion
if err := saveConfigToFile("installation_data.yaml", &cheatInstallationData); err != nil {
log.Errorf(
"Error saving installation data for Pipsi (%s). Without saving this data, shortcut creation and update checks may not work properly. Error: %v",
title, err,
)
return
}
}
ip.successfullyInstalled = append(ip.successfullyInstalled, title)
}(i)
}
wg.Wait()
}
func (ip *installationProcess) downloadFile(url, filePath, title string) error {
var tagName, downloadURL string
action := func() {
if !ip.updateMode {
var err error
tagName, downloadURL, err = getLatestReleaseInfo(url)
if err != nil {
log.Errorf("Failed to get release info for %s: %v", title, err)
return
}
ip.updateGameLatestRelease(title, tagName)
} else {
downloadURL = url
}
fileResponse, err := http.Get(downloadURL)
if err != nil {
log.Errorf("Failed to download file from %s: %v", downloadURL, err)
return
}
defer func() {
if closeErr := fileResponse.Body.Close(); closeErr != nil {
log.Warnf("Error closing response body for %s: %v", title, closeErr)
}
}()
if fileResponse.StatusCode != http.StatusOK {
log.Errorf("Failed to download file for %s. HTTP status: %d", title, fileResponse.StatusCode)
return
}
file, err := os.Create(filePath)
if err != nil {
log.Errorf("Failed to create file for %s: %v", title, err)
return
}
defer func() {
if closeErr := file.Close(); closeErr != nil {
log.Warnf("Encountered an issue while closing the file for %s: %v", title, closeErr)
}
}()
log.Debugf(
"Downloading %s for %s (%.2f MB)...", downloadURL, title, float64(fileResponse.ContentLength)/(1024*1024),
)
if _, err := io.Copy(file, fileResponse.Body); err != nil {
log.Errorf("Error downloading file for %s: %v", title, err)
return
}
log.Debugf("Download complete for %s. File saved to '%s'.", title, filePath)
}
spinnerTitle := "Downloading Pipsi for %s..."
if ip.updateMode {
spinnerTitle = "Updating Pipsi for %s..."
}
spinnerTitle = fmt.Sprintf(spinnerTitle, formatGameList(ip.selectedGames))
if err := spinner.New().Title(spinnerTitle).Action(action).Run(); err != nil {
log.Errorf("Spinner widget error: %v", err)
return err
}
err := ip.unzip(filePath, title)
if err != nil {
log.Errorf("Failed to unzip the file for Pipsi (%s): %v", title, err)
return err
}
return nil
}
func (ip *installationProcess) unzip(filePath, title string) error {
outputDir := fmt.Sprintf("Pipsi Installations/%s", config.getInstallationFolder(title))
err := extractRar(filePath, outputDir)
if err != nil {
return fmt.Errorf("failed to extract archive: %w", err)
}
globalDir := filepath.Join(outputDir, "Global")
if _, err := os.Stat(globalDir); err == nil {
if err := extractFiles(globalDir, outputDir); err != nil {
return fmt.Errorf("failed to extract contents of 'Global' folder: %w", err)
}
ip.updateGameRegion(title, Global)
} else if os.IsNotExist(err) {
ip.updateGameRegion(title, Unknown)
} else {
return fmt.Errorf("failed to stat 'Global' directory: %w", err)
}
return nil
}
func extractFiles(source, destination string) error {
files, err := os.ReadDir(source)
if err != nil {
return fmt.Errorf("failed to read source directory: %w", err)
}
for _, file := range files {
srcPath := filepath.Join(source, file.Name())
destPath := filepath.Join(destination, file.Name())
if err := os.MkdirAll(filepath.Dir(destPath), os.ModePerm); err != nil {
return fmt.Errorf("failed to create directory for file %s: %w", file.Name(), err)
}
if err := copyFile(srcPath, destPath); err != nil {
return fmt.Errorf("failed to extract file %s: %w", file.Name(), err)
}
}
return nil
}
func copyFile(source, destination string) error {
in, err := os.Open(source)
if err != nil {
return err
}
defer func() {
if closeErr := in.Close(); closeErr != nil {
err = fmt.Errorf("error closing input file: %w", closeErr)
}
}()
out, err := os.Create(destination)
if err != nil {
return err
}
defer func() {
if closeErr := out.Close(); closeErr != nil {
if err != nil {
err = fmt.Errorf("error closing output file after write: %w", closeErr)
} else {
err = fmt.Errorf("error closing output file: %w", closeErr)
}
}
}()
if _, err := io.Copy(out, in); err != nil {
return err
}
return err
}
func formatGameList(titles []string) string {
switch len(titles) {
case 1:
return titles[0]
case 2:
return fmt.Sprintf("%s and %s", titles[0], titles[1])
default:
return fmt.Sprintf(
"%s, %s, and %s", titles[0], strings.Join(titles[1:len(titles)-1], ", "), titles[len(titles)-1],
)
}
}