-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
217 lines (173 loc) · 5.08 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
package main
import (
"dalamud-updater/pkg/api"
"dalamud-updater/pkg/preset"
"dalamud-updater/pkg/util"
"fmt"
"os"
"path"
"path/filepath"
"strings"
"github.com/cavaliergopher/grab/v3"
"github.com/spf13/viper"
)
func getLocalAssetsVersion(dir string) int {
file := path.Join(dir, "asset.ver")
return util.ReadNumber(file)
}
const banner = `
_ ___ | ReiwaTech
|_) _ o _.| _ _|_ | Dalamud
| \(/_|\/\/(_||(/_(_| | | Updater
`
func main() {
fmt.Println(banner)
dir := util.GetWorkingDir()
// init config
viper.SetConfigName("dalamud")
viper.SetConfigType("yaml")
viper.AddConfigPath(dir)
if err := viper.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
// Config file not found; ignore error if desired
// fmt.Println("Config file not found, skip")
if !util.Confirm(fmt.Sprintf("Working in: %s", dir), true) {
fmt.Println("No - Exiting")
return
}
viper.SafeWriteConfig()
} else {
// Config file was found but another error was produced
panic(fmt.Errorf("fatal error config file: %w", err))
}
}
// check update
channel := viper.GetString("Channel")
if channel == "" {
channel = "cn"
viper.Set("Channel", channel)
}
localVersion := viper.GetString("Version")
fmt.Println("Requesting Github for latest version ...")
fmt.Println(" Channel :", channel)
fmt.Println(" Local Version :", localVersion)
latest := api.GetVersion(channel)
fmt.Println(" Dalamud version :", latest.AssemblyVersion)
fmt.Println(" Runtime version :", latest.RuntimeVersion)
downloadDir := path.Join(dir, "download")
os.MkdirAll(downloadDir, os.ModePerm)
// update dalamud
if localVersion == latest.AssemblyVersion {
fmt.Println("Local version is already latest")
} else {
// download latest file from github
fmt.Println("Downloading from Github ...")
fileName := fmt.Sprintf("dalamud-%s-%s.zip", channel, latest.AssemblyVersion)
resp, err := util.Download(path.Join(downloadDir, fileName), util.GetArchiveUrl(channel))
if err != nil {
panic(err)
}
fmt.Println("Download saved to", resp.Filename)
fmt.Println("Unziping ...")
if err := util.Unzip(resp.Filename, path.Join(dir, "Release")); err != nil {
panic(err)
}
// save current version
viper.Set("Version", latest.AssemblyVersion)
if err := viper.WriteConfig(); err != nil {
panic(err)
}
}
// update runtime
fmt.Println("Checking runtimes ...")
runtimes := preset.GetRuntimes(latest.RuntimeVersion)
for i := 0; i < len(runtimes); i += 2 {
if util.IsDir(path.Join(dir, "runtime", runtimes[i])) {
continue
}
resp, err := util.Download(downloadDir, runtimes[i+1])
if err != nil {
panic(err)
}
fmt.Println("Download saved to", resp.Filename)
fmt.Println("Unziping ...")
if err := util.Unzip(resp.Filename, path.Join(dir, "runtime")); err != nil {
panic(err)
}
}
// update assets
fmt.Println("Requesting Github for assets version ...")
latestAssets := api.GetAssetsVersion()
assetsDir := path.Join(dir, "XIVLauncher/dalamudAssets/dev")
assetsVerFile := path.Join(assetsDir, "asset.ver")
assetsVer := util.ReadNumber(assetsVerFile)
fmt.Println(" Local version :", assetsVer)
fmt.Println(" Latest version :", latestAssets.Version)
if assetsVer == latestAssets.Version {
fmt.Println("Local assets version is already latest")
} else {
fmt.Println("Downloading from Github ...")
reqs := []*grab.Request{}
assets:
for i := 0; i < len(latestAssets.Assets); i++ {
row := latestAssets.Assets[i]
filename := path.Join(assetsDir, path.Clean(row.FileName))
os.MkdirAll(filepath.Dir(filename), os.ModePerm)
// check overrides
for j := 0; j < len(preset.AssetsOverride); j += 2 {
rule := preset.AssetsOverride[j]
override := preset.AssetsOverride[j+1]
if rule == row.FileName {
fmt.Printf("Overriding %s with %s\n", rule, override)
if strings.HasPrefix(override, "https://") {
row.URL = override
row.Hash = ""
break
}
util.WriteString(filename, override)
continue assets
}
}
if row.Hash != "" {
hash, err := util.Sha1(filename)
if err == nil && hash == strings.ToLower(row.Hash) {
continue
}
}
os.Remove(filename)
req, err := grab.NewRequest(filename, row.URL)
if err != nil {
panic(err)
}
req.NoResume = true
reqs = append(reqs, req)
}
if len(reqs) > 0 {
util.DownloadBatch(10, reqs...)
}
util.WriteNumber(assetsVerFile, latestAssets.Version)
}
// update fonts
fmt.Println("Checking fonts ...")
installedFonts := viper.GetStringSlice("fonts")
for i := 0; i < len(preset.Fonts); i += 2 {
font := preset.Fonts[i]
fileName := path.Join(assetsDir, font)
if util.Contains[string](installedFonts, font) {
continue
}
if util.IsFile(fileName) {
os.Remove(fileName)
}
resp, err := util.Download(fileName, preset.Fonts[i+1])
if err != nil {
panic(err)
}
fmt.Println("Download saved to", resp.Filename)
installedFonts = append(installedFonts, font)
viper.Set("fonts", installedFonts)
viper.WriteConfig()
}
fmt.Println()
fmt.Println("All done!")
}