-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
257 lines (213 loc) · 4.8 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
package main
import (
"archive/zip"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"path"
"strconv"
"strings"
resty "github.com/go-resty/resty/v2"
)
const (
API_URL = "https://api.curseforge.com"
CDN_URL = "https://edge.forgecdn.net/files"
OVERRIDES = "overrides"
)
type Ctx struct {
*resty.Client
}
type File struct {
ID int `json:"id"`
DownloadURL string `json:"downloadURL"`
FileName string `json:"fileName"`
}
func (file File) URL() string {
id := strconv.Itoa(file.ID)
if file.DownloadURL == "" {
return strings.Join([]string{
CDN_URL,
id[:4],
strings.TrimPrefix(id[4:], "0"),
file.FileName,
}, "/")
} else {
return file.DownloadURL
}
}
type Manifest struct {
Minecraft struct {
Version string `json:"version"`
ModLoaders []struct {
ID string `json:"id"`
} `json:"modLoaders"`
} `json:"minecraft"`
Files []struct {
ProjectID int `json:"projectID"`
FileID int `json:"fileID"`
} `json:"files"`
}
func main() {
if len(os.Args) < 2 {
log.Fatalf("Usage: %s <pack ID>\n", os.Args[0])
}
packID := os.Args[1]
key, err := ioutil.ReadFile("key")
if err != nil {
panic(err)
}
ctx := Ctx{resty.New()}
ctx.
SetHeader("Accept", "application/json").
SetHeader("x-api-key", strings.TrimSpace(string(key)))
// get pack URL
packURL, err := ctx.getProjectURL(packID)
if err != nil {
panic(err)
}
log.Print("Pack URL is ", packURL)
// download pack
file := path.Base(packURL)
if err := downloadFileTo(packURL, file); err != nil {
panic(err)
}
// extract pack
packDir := strings.TrimSuffix(file, ".zip")
log.Print("Extracting to directory ", packDir)
if err := unzip(file, packDir); err != nil {
panic(err)
}
os.Remove(file)
// read manifest
rawManifest, err := ioutil.ReadFile(path.Join(packDir, "manifest.json"))
if err != nil {
panic(err)
}
var manifest Manifest
if err := json.Unmarshal(rawManifest, &manifest); err != nil {
panic(err)
}
// clean up pack structure
entries, err := os.ReadDir(packDir)
if err != nil {
panic(err)
}
for _, entry := range entries {
if entry.Name() != OVERRIDES {
os.Remove(path.Join(packDir, entry.Name()))
}
}
entries, err = os.ReadDir(path.Join(packDir, OVERRIDES))
if err == nil {
for _, entry := range entries {
os.Rename(
path.Join(packDir, OVERRIDES, entry.Name()),
path.Join(packDir, entry.Name()),
)
}
}
os.Remove(path.Join(packDir, OVERRIDES))
// download mods
failures := []string{}
modDir := path.Join(packDir, "mods")
if err := os.MkdirAll(modDir, 0755); err != nil {
panic(err)
}
for _, mod := range manifest.Files {
url, err := ctx.getFileURL(strconv.Itoa(mod.ProjectID), strconv.Itoa(mod.FileID))
if err != nil || url == "" {
log.Printf("Could not get URL for file %d/%d", mod.ProjectID, mod.FileID)
failures = append(failures, fmt.Sprintf("%d/%d", mod.ProjectID, mod.FileID))
continue
}
log.Print("Downloading ", url)
downloadFileTo(url, path.Join(modDir, path.Base(url)))
}
// print final information
log.Print("\nMinecraft version ", manifest.Minecraft.Version)
for _, loader := range manifest.Minecraft.ModLoaders {
log.Print("ModLoader version ", loader.ID)
}
log.Printf("%d mods", len(manifest.Files))
if len(failures) > 0 {
log.Print("Failed IDs:")
for _, failure := range failures {
log.Print("\t", failure)
}
}
}
func (ctx Ctx) getProjectURL(projectID string) (string, error) {
type res struct {
Data []File `json:"data"`
}
resp, err := ctx.R().
SetResult(res{}).
Get(fmt.Sprintf("%s/v1/mods/%s/files", API_URL, projectID))
if err != nil {
return "", err
}
data := resp.Result().(*res).Data
if len(data) < 1 {
return "", errors.New("no data returned")
}
return data[0].URL(), nil
}
func (ctx Ctx) getFileURL(projectID, fileID string) (string, error) {
type res struct {
File File `json:"data"`
}
resp, err := ctx.R().
SetResult(res{}).
Get(fmt.Sprintf("%s/v1/mods/%s/files/%s", API_URL, projectID, fileID))
if err != nil {
return "", err
}
return resp.Result().(*res).File.URL(), nil
}
func downloadFileTo(url, path string) error {
file, err := os.Create(path)
if err != nil {
return err
}
defer file.Close()
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
_, err = io.Copy(file, resp.Body)
if err != nil {
return err
}
return nil
}
func unzip(source, dest string) error {
read, err := zip.OpenReader(source)
if err != nil {
return err
}
defer read.Close()
for _, file := range read.File {
if file.Mode().IsDir() {
continue
}
open, err := file.Open()
if err != nil {
return err
}
name := path.Join(dest, file.Name)
os.MkdirAll(path.Dir(name), 0755)
create, err := os.Create(name)
if err != nil {
return err
}
defer create.Close()
create.ReadFrom(open)
}
return nil
}