-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
177 lines (147 loc) · 4.64 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
package main
import (
"bufio"
"flag"
"fmt"
"os"
"os/user"
"path/filepath"
"runtime"
"strconv"
"strings"
"github.com/go-ini/ini"
)
var (
iniConfig = flag.String("config", ".ueversionator", "ueversionator config file")
userIniConfig = flag.String("user-config", ".uev-user", "ueversionator user config file")
baseDir = flag.String("basedir", "ue4", "base directory to download engine bundles in")
bundle = flag.String("bundle", "editor", "request UE build bundle")
ue5 = flag.Bool("ue5", false, "UE5 build compat")
fetchSymbols = flag.Bool("with-symbols", false, "include UE engine debug symbols")
virgin = flag.Bool("virgin", false, "ask configuration options like the first time")
assumeValid = flag.Bool("assume-valid", false, "assumes current archive is valid, if present")
)
func handleError(err error) {
if err == nil {
return
}
fmt.Println(err)
os.Exit(1)
}
func ueversionator() (string, string, error) {
flag.Parse()
// load ini config
cfg, err := ini.LooseLoad(*iniConfig, *userIniConfig)
handleError(err)
baseURL, err := cfg.Section("ueversionator").GetKey("baseurl")
if err != nil || baseURL.String() == "" {
handleError(fmt.Errorf("%s config file has no baseurl setting", *iniConfig))
}
// uproject path
path := "."
if len(flag.Args()) > 0 {
path = flag.Args()[0]
}
path, _ = filepath.Abs(path)
version, err := GetEngineAssociation(path)
if err != nil {
handleError(fmt.Errorf("error finding engine association with path %s: %v", path, err))
}
downloadDir, err := filepath.Abs(getDownloadDirectory(path))
if err != nil {
handleError(err)
}
handleError(os.MkdirAll(downloadDir, 0777))
shouldFetchSymbols := *fetchSymbols
if !shouldFetchSymbols {
section := "ue4v-user"
if *ue5 {
section = "uev-user"
}
symbolsConfig, err := cfg.Section(section).GetKey("symbols")
if err == nil {
shouldFetchSymbols = symbolsConfig.MustBool(false)
}
}
dest, err := FetchEngine(downloadDir, baseURL.String(), version, DownloadOptions{
EngineBundle: *bundle,
FetchSymbols: shouldFetchSymbols,
AssumeValid: *assumeValid,
UsesUE5: *ue5,
})
return version, dest, err
}
func getDownloadDirectory(path string) string {
cfg, _ := ini.LooseLoad(*userIniConfig)
section := "ue4v-user"
if *ue5 {
section = "uev-user"
}
key, err := cfg.Section(section).GetKey("download_dir")
if err != nil {
key, _ = cfg.Section(section).NewKey("download_dir", "")
}
if !*virgin && key.String() != "" {
return key.String()
}
fmt.Fprintf(os.Stdout, "=========================================================================\n")
fmt.Fprintf(os.Stdout, "| A custom Unreal Engine build needs to be downloaded for this project. |\n")
fmt.Fprintf(os.Stdout, "| These builds can be quite large. Lots of disk space is required. |\n")
fmt.Fprintf(os.Stdout, "=========================================================================\n\n")
fmt.Fprintf(os.Stdout, "Project path: %v\n\n", path)
fmt.Fprintf(os.Stdout, "Which directory should these engine downloads be stored in?\n")
var options []string
const custom = "custom location"
if user, err := user.Current(); err == nil {
options = append(options, filepath.Join(user.HomeDir, *baseDir))
}
if abs, err := filepath.Abs(filepath.Join(path, "..", *baseDir)); err == nil {
options = append(options, abs)
}
if runtime.GOOS == "windows" {
options = append(options, filepath.Join(filepath.VolumeName(path), "/", *baseDir))
}
options = append(options, custom)
var directory string
for {
for i, option := range options {
fmt.Fprintf(os.Stdout, "\n%d) %s", i+1, option)
}
fmt.Printf("\n\nSelect an option (%d-%d) and press enter: ", 1, len(options))
input, _ := bufio.NewReader(os.Stdin).ReadString('\n')
input = strings.TrimSpace(input)
parsed, _ := strconv.ParseInt(input, 10, 8)
chosen := int(parsed) - 1
if chosen < 0 || chosen >= len(options) {
fmt.Printf("Invalid '%s' option. Try again:\n\n", input)
continue
}
directory = options[chosen]
if directory == custom {
var err error
fmt.Printf("\nCustom location: ")
directory, _ = bufio.NewReader(os.Stdin).ReadString('\n')
directory, err = filepath.Abs(strings.TrimSpace(directory))
if err != nil {
fmt.Println(err)
continue
}
if strings.HasPrefix(directory, path) {
fmt.Println("download directory cannot reside in the project directory")
continue
}
if err = os.MkdirAll(directory, 0777); err != nil {
fmt.Println(err)
continue
}
}
if err := os.MkdirAll(directory, 0777); err != nil {
fmt.Println(err)
continue
}
break
}
key.SetValue(directory)
cfg.SaveTo(*userIniConfig)
return directory
}