-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
780 lines (694 loc) · 23.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
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
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
package main
import (
"bufio"
"context"
"crypto/sha1"
"crypto/sha256"
"encoding/hex"
"errors"
"flag"
"fmt"
"ftb-server-downloader/modloaders"
"ftb-server-downloader/repos"
"ftb-server-downloader/structs"
"ftb-server-downloader/util"
"github.com/cavaliergopher/grab/v3"
"github.com/codeclysm/extract/v4"
"github.com/pterm/pterm"
"github.com/pterm/pterm/putils"
"io"
"os"
"path/filepath"
"runtime"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
)
var (
packId int
versionId int
installDir string
threads int
provider string
auto bool
force bool
latest bool
apiKey string
validate bool
skipModloader bool
noJava bool
noColours bool
verbose bool
logFile *os.File
)
func init() {
if util.ReleaseVersion == "" || util.ReleaseVersion == "main" {
util.ReleaseVersion = "v0.0.0-beta.0"
}
if util.GitCommit == "" {
util.GitCommit = "Dev"
}
userAgentVersion := util.ReleaseVersion
if strings.HasPrefix(util.ReleaseVersion, "v") {
userAgentVersion = strings.TrimPrefix(util.ReleaseVersion, "v")
}
util.UserAgent = fmt.Sprintf("ftb-server-installer/%s", userAgentVersion)
}
func main() {
flag.StringVar(&provider, "provider", "ftb", "Modpack provider (Currently only 'ftb' is supported)")
flag.IntVar(&packId, "pack", 0, "Modpack ID")
flag.IntVar(&versionId, "version", 0, "Modpack version ID, if not provided, the latest version will be used")
flag.StringVar(&installDir, "dir", "", "Installation directory")
flag.BoolVar(&auto, "auto", false, "Dont ask questions, just install the server")
flag.BoolVar(&latest, "latest", false, "Gets the latest (alpha/beta/release) version of the modpack")
flag.BoolVar(&force, "force", false, "Force the modpack install, dont ask questions just continue (only works with -auto)")
flag.IntVar(&threads, "threads", runtime.NumCPU()*2, "Number of threads to use (Default: CPU Cores * 2)")
flag.StringVar(&apiKey, "apikey", "public", "FTB API key (Only for private FTB modpacks)")
flag.BoolVar(&validate, "validate", false, "Validate the modpack after install")
flag.BoolVar(&skipModloader, "skip-modloader", false, "Skip installing the modloader")
flag.BoolVar(&noJava, "no-java", false, "Do not install Java")
flag.BoolVar(&noColours, "no-colours", false, "Do not display console/terminal colours")
flag.BoolVar(&verbose, "verbose", false, "Verbose output")
flag.Parse()
var err error
logFile, err = os.OpenFile("ftb-server-installer.log", os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0666)
if err != nil {
panic(err)
}
util.LogMw = io.MultiWriter(os.Stdout, util.NewCustomWriter(logFile))
// Temp fix for loggers not logging to file
pterm.Debug.Writer = nil
pterm.Info.Writer = nil
pterm.Warning.Writer = nil
pterm.Error.Writer = nil
pterm.Fatal.Writer = nil
pterm.Success.Writer = nil
pterm.Description.Writer = nil
pterm.SetDefaultOutput(util.LogMw)
pterm.Debug.Prefix = pterm.Prefix{
Text: "DEBUG",
Style: pterm.NewStyle(pterm.BgLightMagenta, pterm.FgBlack),
}
pterm.Debug.MessageStyle = pterm.NewStyle(98)
if noColours {
pterm.DisableStyling()
}
logo, _ := pterm.DefaultBigText.WithLetters(
putils.LettersFromStringWithStyle("F", pterm.NewStyle(pterm.FgCyan)),
putils.LettersFromStringWithStyle("T", pterm.NewStyle(pterm.FgGreen)),
putils.LettersFromStringWithStyle("B", pterm.NewStyle(pterm.FgRed))).Srender()
pterm.DefaultCenter.Println(logo)
pterm.DefaultCenter.WithCenterEachLineSeparately().Printfln("Server installer version: %s(%s)\n%s", util.ReleaseVersion, util.GitCommit, time.Now().UTC().Format(time.RFC1123))
pterm.DefaultCenter.WithCenterEachLineSeparately().Println(pterm.Bold.Sprintf("Installer Issue tracker\nhttps://github.com/FTBTeam/FTB-Server-Installer/issues"))
updateAvailable, err := util.CheckForUpdate()
if err != nil {
pterm.Warning.Printfln("Error checking for update: %v", err)
}
if updateAvailable {
ext := ""
if runtime.GOOS == "windows" {
ext = ".exe"
}
installerName := fmt.Sprintf("ftb-server-%s-%s%s", runtime.GOOS, runtime.GOARCH, ext)
updateUrl := fmt.Sprintf("https://cdn.feed-the-beast.com/bin/server-installer/latest/%s", installerName)
updateStyle := pterm.NewStyle(pterm.FgLightMagenta, pterm.Bold)
updateURLStyle := pterm.NewStyle(pterm.FgLightCyan, pterm.Bold, pterm.Underscore)
pterm.Info.Printfln("%s%s", updateStyle.Sprint("Installer update available download from: "), updateURLStyle.Sprint(updateUrl))
pterm.Println()
}
if verbose {
pterm.EnableDebugMessages()
pterm.Debug.Println("Verbose output enabled")
}
abs, err := filepath.Abs(installDir)
if err != nil {
pterm.Fatal.Println("Error getting absolute path:", err.Error())
}
installDir = abs
defer logFile.Close()
// Get the pack ID and version ID from the installer name if not provided as flags
if packId == 0 {
pId, vId, err := util.ParseInstallerName(filepath.Base(os.Args[0]))
if err != nil {
pterm.Warning.Println("Unable to parse installer name for modpack and version id:", err)
pId, vId, err = modpackQuestion()
if err != nil {
pterm.Fatal.Println(err)
}
}
packId = pId
versionId = vId
}
// Get the provider
selectedProvider, err := getProvider()
if err != nil {
pterm.Fatal.Printfln("Error getting provider: %s\nValid providers are 'ftb'", err.Error())
}
pterm.Debug.Printfln("Got provider '%s'", provider)
var filesToDownload []structs.File
// Get modpack details from the provider
modpack, err := selectedProvider.GetModpack()
if err != nil {
selectedProvider.FailedInstall()
pterm.Error.Println("Error getting modpack:", err.Error())
os.Exit(1)
}
pterm.Debug.Printfln("Modpack: %+v", modpack)
// Get the latest version id if not provided
if versionId == 0 {
latestVersion, err := getLatestRelease(modpack.Versions, latest)
if err != nil {
pterm.Error.Println("Error getting latest release:", err.Error())
os.Exit(1)
}
selectedProvider.SetVersionId(latestVersion.Id)
pterm.Info.Printfln("No version provided, using latest version: %d", latestVersion.Id)
}
// Get the version information for the modpack from the provider
modpackVersion, err := selectedProvider.GetVersion()
if err != nil {
selectedProvider.FailedInstall()
pterm.Error.Println("Error getting modpack version:", err.Error())
os.Exit(1)
}
filesToDownload = append(filesToDownload, modpackVersion.Files...)
// build the version manifest
manifest := structs.Manifest{
Id: modpack.Id,
Name: modpack.Name,
VersionName: modpackVersion.Name,
VersionId: modpackVersion.Id,
ModpackTargets: modpackVersion.Targets,
Files: modpackVersion.Files,
}
// Check if the install location exists, if it doesnt, ask if they want to create the folder(s)
exists, err := util.PathExists(installDir)
if err != nil {
selectedProvider.FailedInstall()
pterm.Fatal.Println("Unable to check if path exists:", err.Error())
}
mkdir := true
if !exists {
if !auto {
mkdir = util.ConfirmYN("Install folder does not exists, do you want to create it?", true, pterm.Info.MessageStyle)
if !mkdir {
pterm.Error.Println("Installation path does not exist...")
os.Exit(1)
}
}
}
var updatedFiles, removedFiles, unchangedFiles []structs.File
updateMsg := ""
isUpdate := false
if exists {
manifestExists, err := util.PathExists(filepath.Join(installDir, util.ManifestName))
if err != nil {
return
}
if !manifestExists {
installDirEmpty, err := util.IsEmptyDir(installDir)
if err != nil {
selectedProvider.FailedInstall()
pterm.Fatal.Println("Error checking if directory is empty:", err.Error())
}
if !installDirEmpty {
if !auto {
pterm.Warning.Printfln("Install directory is not empty, installing the modpack may cause issues")
cont := util.ConfirmYN("Would you like to continue?", false, pterm.Warning.MessageStyle)
if !cont {
pterm.Error.Println("Installation path is not empty, exiting...")
os.Exit(1)
}
}
if auto && !force {
pterm.Warning.Printfln("Install directory is not empty, installing the modpack may cause issues")
pterm.Warning.Printfln("To force install use the -force flag")
os.Exit(1)
}
}
}
if manifestExists {
existingManifest, err := util.ReadManifest(installDir)
if err != nil {
selectedProvider.FailedInstall()
pterm.Fatal.Println("Error reading manifest:", err.Error())
}
isSamePack := isSameModpack(existingManifest, manifest)
if !isSamePack {
if !auto && !force {
pterm.Warning.Printfln("You currently have a different modpack installed, installing this modpack may cause issues")
cont := util.ConfirmYN("Would you like to continue?", false, pterm.Warning.MessageStyle)
if !cont {
os.Exit(1)
}
}
if auto && !force {
pterm.Warning.Printfln("You currently have a different modpack installed, installing this modpack may cause issues")
pterm.Warning.Printfln("To force install use the -force flag")
os.Exit(1)
}
}
sameVersion := isSameModpackVersion(existingManifest, manifest)
if !sameVersion && isSamePack {
isUpdate, err = checkUpdate(existingManifest, manifest)
if err != nil {
selectedProvider.FailedInstall()
pterm.Fatal.Println("Check Update error:", err.Error())
}
if isUpdate {
existingManifest, err := util.ReadManifest(installDir)
if err != nil {
selectedProvider.FailedInstall()
pterm.Fatal.Println("Error reading manifest:", err.Error())
return
}
updatedFiles, removedFiles, unchangedFiles, err = computeUpdatedFiles(existingManifest.Files, manifest.Files)
if err != nil {
return
}
filesToDownload = removeUnchangedFiles(filesToDownload, unchangedFiles)
}
}
}
}
// set up the modloader getter and installer
modLoader, err := getModLoader(modpackVersion.Targets, modpackVersion.Memory)
if err != nil {
selectedProvider.FailedInstall()
pterm.Error.Println("Error getting modloader:", err.Error())
os.Exit(1)
}
// Add the modloader downloads to the files list
mlDownloads, err := modLoader.GetDownload()
if err != nil {
selectedProvider.FailedInstall()
pterm.Fatal.Println("Error getting mod loader downloads:", err.Error())
}
filesToDownload = append(filesToDownload, mlDownloads...)
if isUpdate {
updateMsg = fmt.Sprintf("\nUnchanged Files: %d\nFiles changed: %d\nFiles removed: %d", len(unchangedFiles), len(updatedFiles), len(removedFiles))
}
pterm.Debug.Printfln("Files to download: %d", len(filesToDownload))
// Show a quick overview of the pack they are installing then ask if they want to continue with downloading the pack
pterm.Info.Printfln("Fetched modpack:\nName: %s\nVersion: %s\nModLoader: %s (%s)\nIs Update: %t%s\nInstall Path: %s", modpack.Name, modpackVersion.Name, modpackVersion.Targets.ModLoader.Name, modpackVersion.Targets.ModLoader.Version, isUpdate, updateMsg, installDir)
if !auto {
cont := util.ConfirmYN("Do you want to continue?", true, pterm.Info.MessageStyle)
if !cont {
os.Exit(1)
}
}
// Ask the user if they want to download java then set the noJava flag depending on their answer
var java structs.File
jreAlreadyExists := false
jrePath, _ := util.GetJavaPath(modpackVersion.Targets.JavaVersion)
if _, err = os.Stat(filepath.Join(installDir, jrePath)); err == nil {
jreAlreadyExists = true
}
// If noJava is set or we already have java downloaded, we skip the java download
if !noJava && !auto && !jreAlreadyExists {
noJava = !util.ConfirmYN("Do you want to download java?", true, pterm.Info.MessageStyle)
}
if !noJava && !jreAlreadyExists {
java, err = util.GetJava(modpackVersion.Targets.JavaVersion)
if err != nil {
selectedProvider.FailedInstall()
pterm.Fatal.Println("Error getting java:", err.Error())
}
filesToDownload = append(filesToDownload, java)
}
if mkdir {
err = os.MkdirAll(installDir, 0777)
if err != nil {
selectedProvider.FailedInstall()
pterm.Fatal.Println("Unable to create install directory:", err.Error())
}
} else {
pterm.Error.Println("Installation path does not exist...")
os.Exit(1)
}
if isUpdate {
for _, f := range removedFiles {
err := os.Remove(filepath.Join(installDir, f.Path, f.Name))
if err != nil {
pterm.Error.Printfln("Removing files error: %s", err.Error())
continue
}
}
// For now, we remove the files that have been updated so they can be freshly downloaded.
for _, f := range updatedFiles {
err := os.Remove(filepath.Join(installDir, f.Path, f.Name))
if err != nil {
pterm.Error.Printfln("Removing update files error: %s", err.Error())
continue
}
}
// Remove unchanged files from filesToDownload
for _, f := range unchangedFiles {
for i, v := range filesToDownload {
if v.Name == f.Name && v.Path == f.Path {
filesToDownload = append(filesToDownload[:i], filesToDownload[i+1:]...)
}
}
}
}
// download the modpack files
pterm.Info.Printfln("Starting mod pack download...")
err = doDownload(filesToDownload...)
if err != nil {
selectedProvider.FailedInstall()
pterm.Fatal.Printfln(err.Error())
}
pterm.Info.Printfln("Modpack files downloaded")
// If we downloaded java, extract the files to a jre folder
if !noJava && !jreAlreadyExists {
javaFile, err := os.Open(filepath.Join(installDir, java.Name))
if err != nil {
selectedProvider.FailedInstall()
pterm.Fatal.Println("Error opening java archive", err.Error())
}
javaPkg := bufio.NewReader(javaFile)
var shift = func(path string) string {
// Apparently zips in windows can use / instead of \
// So we need to check if the path is using / or \
sep := filepath.Separator
if len(strings.Split(path, "\\")) > 1 {
sep = '\\'
} else if len(strings.Split(path, "/")) > 1 {
sep = '/'
}
parts := strings.Split(path, string(sep))
parts = parts[1:]
join := strings.Join(parts, string(sep))
return join
}
err = extract.Archive(context.TODO(), javaPkg, filepath.Join(installDir, "jre", modpackVersion.Targets.JavaVersion), shift)
if err != nil {
selectedProvider.FailedInstall()
pterm.Fatal.Println("Error extracting java archive:", err.Error())
}
javaFile.Close()
err = os.Remove(filepath.Join(installDir, java.Name))
if err != nil {
pterm.Warning.Println("Error removing java archive:", err.Error())
}
}
// Ask if the user would like to run the modloader installer
// todo: if the modloader is already installed check if its the same and ignore the update
if !auto && !skipModloader {
skipModloader = !util.ConfirmYN(
fmt.Sprintf("Would you like to run the %s installer?", modpackVersion.Targets.ModLoader.Name),
true,
pterm.Info.MessageStyle,
)
}
if noJava && !util.OsJavaExists() {
// Revisit this, and possibly ask if they want to download java
pterm.Warning.Printfln("Java is not installed, skipping modloader installer")
skipModloader = true
}
if !skipModloader {
err = modLoader.Install(!noJava)
if err != nil {
selectedProvider.FailedInstall()
pterm.Fatal.Println("ModLoader installer error:", err.Error())
}
}
// if the validate flag has been enabled, validate the files we downloaded and check if they match what they should be
if validate {
err = runValidation(manifest)
if err != nil {
selectedProvider.FailedInstall()
pterm.Fatal.Println("Error running validation:", err.Error())
}
}
// write the version manifest
err = util.WriteManifest(installDir, manifest)
if err != nil {
selectedProvider.FailedInstall()
pterm.Fatal.Println("Error creating manifest:", err.Error())
}
/*// Ask if the user would like to copy the overrides
overridesExist, err := util.PathExists(filepath.Join(installDir, "overrides"))
if err != nil {
pterm.Fatal.Println("Error checking if overrides exists:", err.Error())
}
if overridesExist {
copyOverriddenFiles()
}*/
selectedProvider.SuccessfulInstall()
pterm.Success.Println("Modpack installed successfully")
}
// getProvider Gets and sets up the repo provider
func getProvider() (repos.ModpackRepo, error) {
switch provider {
case "ftb":
return repos.GetFTB(packId, versionId, apiKey), nil
// case "curseforge":
// return repos.GetCurseForge(packId, versionId), nil
default:
return nil, errors.New(fmt.Sprintf("'%s' not recognised", provider))
}
}
// getModLoader function to get the correct modloader for the pack
func getModLoader(targets structs.ModpackTargets, memory structs.Memory) (modloaders.ModLoader, error) {
switch targets.ModLoader.Name {
case "neoforge":
return modloaders.GetNeoForge(targets, memory, installDir), nil
case "fabric":
return modloaders.GetFabric(targets, memory, installDir)
case "forge":
return modloaders.GetForge(targets, memory, installDir), nil
default:
return nil, errors.New(fmt.Sprintf("'%s' not recognised", targets.ModLoader.Name))
}
}
func doDownload(files ...structs.File) error {
// failedDownloads = []structs.File{}
p, _ := pterm.DefaultProgressbar.WithTitle("Downloading...").WithTotal(len(files)).Start()
var wg sync.WaitGroup
threadLimit := make(chan int, threads)
var pCount atomic.Uint64
for _, file := range files {
wg.Add(1)
threadLimit <- 1
go func() {
defer func() { wg.Done(); <-threadLimit; pCount.Add(1); p.Current = int(pCount.Load()) }()
grab.DefaultClient.UserAgent = util.UserAgent
destPath := filepath.Join(installDir, file.Path, file.Name)
urls := append([]string{file.Url}, file.Mirrors...)
var attempts = 0
for _, u := range urls {
attempts++
pterm.Debug.Printfln("Downloading file: %s from %s | attempt: %d | Urls %d", file.Name, u, attempts, len(urls))
reqUrl := u
req, err := grab.NewRequest(destPath, reqUrl)
if err != nil {
pterm.Error.Printfln("Download request error: %s", err.Error())
if attempts == len(urls) {
pterm.Error.Printfln("Failed to download file: %s\nAll mirrors failed", file.Name)
os.Exit(1)
}
continue
}
req.NoResume = true
if file.Hash != "" {
hexHash, _ := hex.DecodeString(file.Hash)
switch file.HashType {
case "sha1":
req.SetChecksum(sha1.New(), hexHash, false)
case "sha256":
req.SetChecksum(sha256.New(), hexHash, false)
default:
pterm.Warning.Printfln("Unsupported hash type: %s", file.HashType)
}
}
resp := grab.DefaultClient.Do(req)
if resp.Err() == nil {
pterm.Debug.Printfln("Downloaded file: %s", file.Name)
break
} else if resp.Err() != nil {
_ = os.Remove(filepath.Join(installDir, file.Path, file.Name))
pterm.Warning.Printfln("Failed to download:\nFile: %s (%s)\nResp Status: %s(%d)\nError:%s", file.Name, reqUrl, resp.HTTPResponse.Status, resp.HTTPResponse.StatusCode, resp.Err().Error())
if attempts == len(urls) {
pterm.Error.Printfln("Failed to download file: %s\nAll mirrors failed", file.Name)
os.Exit(1)
}
}
}
}()
}
wg.Wait()
p.UpdateTitle("Download complete")
p.Current = int(pCount.Load()) // Hack to fix race condition in progress bar printer
p.Stop()
return nil
}
func runValidation(manifest structs.Manifest) error {
var invalidFiles []structs.File
for _, f := range manifest.Files {
if f.HashType != "" && f.Hash != "" {
fileHash, err := util.FileHash(filepath.Join(installDir, f.Path, f.Name), f.HashType)
if err != nil {
pterm.Error.Println("Error getting file hash:", err.Error())
continue
}
if fileHash != f.Hash {
pterm.Warning.Printfln(fmt.Sprintf("Unexpected file hash from %s\nExpected: %s\nGot: %s", f.Name, f.Hash, fileHash))
invalidFiles = append(invalidFiles, f)
}
}
}
if len(invalidFiles) > 0 {
if !auto {
show := util.ConfirmYN(
fmt.Sprintf("%d files failed validation, would you like to repair them?", len(invalidFiles)),
true,
pterm.Info.MessageStyle,
)
if !show {
return nil
}
}
err := doDownload(invalidFiles...)
if err != nil {
return err
}
}
return nil
}
func isSameModpack(currentManifest, newManifest structs.Manifest) bool {
if currentManifest.Id != newManifest.Id {
return false
}
return true
}
func isSameModpackVersion(currentManifest, newManifest structs.Manifest) bool {
if currentManifest.Id != newManifest.Id {
return false
}
if currentManifest.VersionId != newManifest.VersionId {
return false
}
return true
}
func checkUpdate(currentManifest, newManifest structs.Manifest) (isUpdate bool, err error) {
if currentManifest.Id != newManifest.Id {
return false, errors.New("mismatched modpack")
}
if currentManifest.VersionId != newManifest.VersionId {
if newManifest.VersionId > currentManifest.VersionId {
return true, nil
}
if newManifest.VersionId < currentManifest.VersionId {
if !auto {
show := util.ConfirmYN(
fmt.Sprintf("%s will be downgraded from %s to version %s, are you sure you want to downgrade?", newManifest.Name, currentManifest.VersionName, newManifest.VersionName),
false,
pterm.Warning.MessageStyle,
)
if !show {
pterm.Info.Println("Cancelling update...")
os.Exit(0)
}
}
if auto && !force {
pterm.Warning.Printfln("Cancelling update... %s would be downgraded from %s to %s. To force this downgrade use the -force flag", newManifest.Name, currentManifest.VersionName, newManifest.VersionName)
os.Exit(1)
} else if auto && force {
pterm.Warning.Printfln("Forcing downgrade")
}
return true, nil
}
} else {
return false, nil
}
return currentManifest.VersionId != newManifest.VersionId, nil
}
func computeUpdatedFiles(currentFiles, newFiles []structs.File) (updatedFiles, removedFiles, unchangedFiles []structs.File, err error) {
for _, v1 := range currentFiles {
fileFound := false
fileChanged := false
for _, v2 := range newFiles {
if v1.Name == v2.Name && v1.Path == v2.Path {
// file still exists, so check if it has changed
if v1.Hash != v2.Hash {
fileChanged = true
} else {
unchangedFiles = append(unchangedFiles, v1)
}
fileFound = true
}
}
if !fileFound {
removedFiles = append(removedFiles, v1)
} else if fileChanged {
updatedFiles = append(updatedFiles, v1)
}
}
return
}
func removeUnchangedFiles(files []structs.File, unchangedFiles []structs.File) []structs.File {
// removed unchanged files from files
for _, f := range unchangedFiles {
for i, v := range files {
if v.Name == f.Name && v.Path == f.Path {
files = append(files[:i], files[i+1:]...)
}
}
}
return files
}
func getLatestRelease(versions []structs.ModpackV, latest bool) (structs.ModpackV, error) {
pterm.Debug.Printfln("versions: %+v", versions)
for _, v := range versions {
if !latest {
if v.Type == "release" {
return v, nil
}
} else {
return v, nil
}
}
if !latest {
return structs.ModpackV{}, errors.New("no stable release found, please rerun the installer with the -latest flag or specify a version using the -version flag")
}
return structs.ModpackV{}, errors.New("no release found, please rerun the installer with the -version flag")
}
func modpackQuestion() (int, int, error) {
sPId, _ := pterm.DefaultInteractiveTextInput.
WithDefaultText("Please enter the modpack ID").
Show()
pId, err := strconv.Atoi(sPId)
if err != nil {
return 0, 0, err
}
getLatest := util.ConfirmYN("Would you like to get the latest version?", true, pterm.Info.MessageStyle)
vId := 0
if !getLatest {
sVId, _ := pterm.DefaultInteractiveTextInput.
WithDefaultText("Please enter the version id").
Show()
vId, err = strconv.Atoi(sVId)
if err != nil {
return 0, 0, err
}
}
return pId, vId, nil
}
/*func copyOverriddenFiles() {
pterm.Info.Printfln("Overrides folder found")
doCopy := true
if !auto {
doCopy = util.ConfirmYN("Would you like to copy the overrides folder contents?", true, pterm.Info.MessageStyle)
} else {
pterm.Info.Printfln("Copying overrides folder contents")
}
if doCopy {
err := util.CopyDir(filepath.Join(installDir, "overrides"), filepath.Join(installDir))
if err != nil {
pterm.Fatal.Println("Error copying overrides folder:", err.Error())
}
}
}*/