-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
576 lines (520 loc) · 14.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
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
package main
import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/http/cookiejar"
"net/url"
"os"
"os/exec"
"path/filepath"
"sort"
"strconv"
"strings"
"time"
"github.com/anaminus/but"
"github.com/anaminus/rbxauth"
"github.com/jessevdk/go-flags"
)
func findCookie(cookies []*http.Cookie, name string) *http.Cookie {
for _, cookie := range cookies {
if cookie.Name == name {
return cookie
}
}
return nil
}
func findGit() string {
git, err := exec.LookPath("git")
if err != nil {
return ""
}
return git
}
const cookieDomain = `https://www.roblox.com/`
const assetVersions = `https://api.roblox.com/assets/%d/versions?page=%d`
const assetVersion = `https://assetgame.roblox.com/Asset?versionId=%d`
type AssetVersion struct {
Id int64
AssetId int64
VersionNumber int64
ParentAssetVersionId int64
CreatorType int
CreatorTargetId int64
CreatingUniverseId *int64
Created time.Time
Updated time.Time
}
////////////////////////////////////////////////////////////////////////////////
type StatusError interface {
StatusCode() int
}
// statusError represents an error derived from the status code of an HTTP
// response. It also wraps an API error response.
type statusError struct {
code int
resp error
}
// Error implements the error interface.
func (err statusError) Error() string {
if err.resp == nil {
return "http " + strconv.Itoa(err.code) + ": " + http.StatusText(err.code)
}
return "http " + strconv.Itoa(err.code) + ": " + err.resp.Error()
}
// Unwrap implements the Unwrap interface.
func (err statusError) Unwrap() error {
return err.resp
}
// StatusCode returns the status code of the error.
func (err statusError) StatusCode() int {
return err.code
}
// if Status wraps err in a statusError if code is not 2XX, and returns err
// otherwise.
func ifStatus(code int, err error) error {
if code < 200 || code >= 300 {
return &statusError{code: code, resp: err}
}
return err
}
////////////////////////////////////////////////////////////////////////////////
type Client struct {
*http.Client
}
func (client *Client) Login(authFile string) (err error) {
defer func() {
if err != nil {
err = fmt.Errorf("login: %w", err)
}
}()
var session []*http.Cookie
if authFile == "" {
stream := rbxauth.StandardStream()
if _, session, err = stream.Prompt(""); err != nil {
return err
}
} else {
file, err := os.Open(authFile)
if err != nil {
return fmt.Errorf("auth file: %w", err)
}
session, err = rbxauth.ReadCookies(file)
file.Close()
if err != nil {
return fmt.Errorf("auth file: %w", err)
}
}
if findCookie(session, ".ROBLOSECURITY") == nil {
return errors.New("missing session cookie")
}
u, err := url.Parse(cookieDomain)
client.Jar, _ = cookiejar.New(nil)
client.Jar.SetCookies(u, session)
return nil
}
func (client *Client) GetAssetVersions(assetID int64, page int) (versions []AssetVersion, err error) {
defer func() {
if err != nil {
err = fmt.Errorf("get versions of asset %d (page %d): %w", assetID, page, err)
}
}()
logf("getting page %d of asset %d\n", page, assetID)
url := fmt.Sprintf(assetVersions, assetID, page)
resp, err := client.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if err := ifStatus(resp.StatusCode, nil); err != nil {
return nil, err
}
if err := json.NewDecoder(resp.Body).Decode(&versions); err != nil {
return nil, err
}
return versions, nil
}
func (client *Client) GetAssetVersion(version AssetVersion, cb func(AssetVersion, io.Reader) error) error {
logf("getting version %d (vid %d)\n", version.VersionNumber, version.Id)
if cb == nil {
return errors.New("missing callback")
}
url := fmt.Sprintf(assetVersion, version.Id)
resp, err := client.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
if err := ifStatus(resp.StatusCode, nil); err != nil {
return err
}
if err := cb(version, resp.Body); err != nil {
return fmt.Errorf("callback: %w", err)
}
return nil
}
////////////////////////////////////////////////////////////////////////////////
type Commander struct {
Cmd string
Args []string
Dir string
Err func([]string, []byte, error) error
}
func (c *Commander) run(cmd *exec.Cmd) error {
b, err := cmd.CombinedOutput()
if err != nil && c.Err != nil {
return c.Err(cmd.Args, b, err)
}
return err
}
func (c *Commander) Run() error {
cmd := exec.Command(c.Cmd, c.Args...)
cmd.Dir = c.Dir
return c.run(cmd)
}
func (c *Commander) Pipe(r io.Reader) error {
cmd := exec.Command(c.Cmd, c.Args...)
cmd.Dir = c.Dir
cmd.Stdin = r
return c.run(cmd)
}
func (c *Commander) RunArgs(args ...string) error {
cmd := exec.Command(c.Cmd, args...)
cmd.Dir = c.Dir
return c.run(cmd)
}
func (c *Commander) PipeArgs(r io.Reader, args ...string) error {
cmd := exec.Command(c.Cmd, args...)
cmd.Dir = c.Dir
cmd.Stdin = r
return c.run(cmd)
}
////////////////////////////////////////////////////////////////////////////////
func commitMessage(v AssetVersion, filename string) string {
const format = "Update %s to version %d\n\n" +
"Id: %d\n" +
"AssetId: %d\n" +
"VersionNumber: %d\n" +
"ParentAssetVersionId: %d\n" +
"CreatorType: %d\n" +
"CreatorTargetId: %d\n" +
"Created: %s\n" +
"Updated: %s\n"
return fmt.Sprintf(format,
filename,
v.VersionNumber,
v.Id,
v.AssetId,
v.VersionNumber,
v.ParentAssetVersionId,
v.CreatorType,
v.CreatorTargetId,
v.Created.Format("Mon, 02 Jan 2006 15:04:05 +0700"),
v.Updated.Format("Mon, 02 Jan 2006 15:04:05 +0700"),
)
}
func selectVersionField(v AssetVersion, field string) string {
switch strings.ToLower(field) {
case "id", "vid":
return strconv.FormatInt(v.Id, 10)
case "assetid", "aid":
return strconv.FormatInt(v.AssetId, 10)
case "versionnumber", "v":
return strconv.FormatInt(v.VersionNumber, 10)
case "parentassetversionid", "pid":
return strconv.FormatInt(v.ParentAssetVersionId, 10)
case "creatortype", "ct":
return strconv.FormatInt(int64(v.CreatorType), 10)
case "creatortargetid", "cid":
return strconv.FormatInt(v.CreatorTargetId, 10)
case "creatinguniverseid":
if v.CreatingUniverseId != nil {
return strconv.FormatInt(*v.CreatingUniverseId, 10)
}
case "created", "t":
return v.Created.UTC().Format("2006-01-02-15-04-05")
case "updated", "u":
return v.Updated.UTC().Format("2006-01-02-15-04-05")
}
return ""
}
func formatFilename(format string, v AssetVersion) string {
var buf strings.Builder
i := 0
for j := 0; j < len(format); j++ {
if format[j] == '%' && j+1 < len(format) {
buf.WriteString(format[i:j])
j++
i = j
if format[j] == '%' {
// buf.WriteByte(format[j])
continue
}
for ; j < len(format); j++ {
if c := format[j]; '0' <= c && c <= '9' ||
'a' <= c && c <= 'z' ||
'A' <= c && c <= 'Z' {
continue
}
break
}
if j == i {
buf.WriteByte(format[i-1])
} else {
buf.WriteString(selectVersionField(v, format[i:j]))
}
i = j
}
}
buf.WriteString(format[i:])
return buf.String()
}
////////////////////////////////////////////////////////////////////////////////
const Usage = `-i ASSET [options] [transform [args...]]
Downloads versions of Roblox assets to a Git repository.
Any unprocessed arguments are interpreted as a command with arguments, which can
be used to transform files. This command runs with --output as the working
directory, and runs after each version is downloaded. If the command fails, then
that version is skipped. If --git is enabled, then the entire working tree is
committed after the command succeeds.
The --filename format may contain variables of the form %VARIABLE that expand
based on data from the version currently being processed. %% emits a literal %
character, and unknown variables emit empty strings. Variables are
case-insensitive.
Variable Alias Description
------------------------------------------------------------------
Id vid Asset version ID.
AssetId aid Asset ID.
VersionNumber v Current version number.
ParentAssetVersionId pid ID of the parent or previous version.
CreatorTargetId cid ID of the asset creator.
CreatorType ct Number indicating the creator type.
CreatingUniverseId Universe ID, if present.
Created t When the version was created.
Updated u When the version was last updated.
When --git is disabled, the format must produce names that are unique per
version. If not, "_v%VersionNumber" is appended to the filename, before the file
extension. Using any of the Id, VersionNumber, Created, or Updated variables
will produce unique names.`
var Options = struct {
AssetID int64 `long:"id" short:"i"`
AuthFile string `long:"auth" short:"a"`
Output string `long:"output" short:"o"`
Filename string `long:"filename" short:"f"`
Git bool `long:"git"`
Tag bool `long:"tag"`
Pipe bool `long:"pipe"`
Verbose bool `long:"verbose" short:"v"`
}{0, "", "", "asset.rbxl", true, false, false, false}
var optionData = map[string]*flags.Option{
"id": &flags.Option{
Description: `ID of asset to retrieve versions of.`,
ValueName: "INTEGER",
Required: false,
},
"auth": &flags.Option{
Description: "Path to a file containing authentication " +
"(.ROBLOSECURITY) cookies. The file is formatted as a number of " +
"'Set-Cookie' HTTP headers. Prompts the user to login if " +
"unspecified.",
ValueName: "PATH",
},
"output": &flags.Option{
Description: "The directory to which files will be written. Defaults " +
"to the working directory.",
ValueName: "PATH",
},
"filename": &flags.Option{
Description: "Format the name of written version files.",
ValueName: "FORMAT",
Default: []string{"asset.rbxl"},
},
"git": &flags.Option{
Description: "Compile version files into a git repository. Set " +
"--git=false to disable.",
Default: []string{"true"},
},
"tag": &flags.Option{
Description: "Tag each commit with the version number.",
Default: []string{"false"},
},
"pipe": &flags.Option{
Description: "Pipe version files into transform command instead of " +
"writing.",
Default: []string{"false"},
},
"verbose": &flags.Option{
Description: "Verbose logging.",
},
}
func ParseOptions(data interface{}, opts flags.Options) *flags.Parser {
fp := flags.NewParser(data, opts)
for name, info := range optionData {
opt := fp.FindOptionByLongName(name)
if opt == nil {
continue
}
opt.Description = info.Description
opt.ValueName = info.ValueName
}
return fp
}
////////////////////////////////////////////////////////////////////////////////
func log(args ...interface{}) {
if Options.Verbose {
but.Log(args...)
}
}
func logf(format string, args ...interface{}) {
if Options.Verbose {
but.Logf(format, args...)
}
}
var errContinue = errors.New("continue")
func transformFile(transform *Commander, filename string, r io.Reader) error {
if transform == nil || !Options.Pipe {
file, err := os.Create(filepath.Join(Options.Output, filename))
if err != nil {
return fmt.Errorf("create file: %w", err)
}
if _, err := io.Copy(file, r); err != nil {
file.Close()
return fmt.Errorf("write file: %w", err)
}
err = file.Sync()
file.Close()
if err != nil {
return fmt.Errorf("sync file: %w", err)
}
}
if transform != nil {
var err error
if Options.Pipe {
err = transform.Pipe(r)
} else {
err = transform.Run()
}
if err != nil {
// If the transform command fails, continue on to the next version.
log("transform: ", err)
return errContinue
}
}
return nil
}
////////////////////////////////////////////////////////////////////////////////
func main() {
fp := ParseOptions(&Options, flags.Default|flags.PassAfterNonOption)
fp.Usage = Usage
transformArgs, err := fp.Parse()
if err != nil {
return
}
if Options.AssetID <= 0 {
but.Log("must specify --id flag")
fp.WriteHelp(os.Stderr)
return
}
if Options.Git && findGit() == "" {
but.Fatal("git not installed")
}
if Options.Output == "" || Options.Output == "." {
var err error
Options.Output, err = os.Getwd()
but.IfFatal(err, "get working directory")
} else {
but.IfFatal(os.MkdirAll(Options.Output, 0755))
}
var versions []AssetVersion
client := &Client{Client: &http.Client{}}
for page, authed := 1, false; ; {
v, err := client.GetAssetVersions(Options.AssetID, page)
if err != nil && !authed {
if status := StatusError(nil); errors.As(err, &status) && status.StatusCode() == 403 {
// Get without auth failed; retry with.
but.IfFatal(client.Login(Options.AuthFile))
authed = true
continue
}
}
but.IfFatal(err)
if len(v) == 0 {
break
}
versions = append(versions, v...)
page++
}
sort.Slice(versions, func(i, j int) bool {
return versions[i].VersionNumber < versions[j].VersionNumber
})
var transform *Commander
if len(transformArgs) > 0 {
transform = &Commander{
Cmd: transformArgs[0],
Args: transformArgs[1:],
Dir: Options.Output,
}
logf("found transform command %q\n", transform.Cmd)
}
var callback func(AssetVersion, io.Reader) error
if Options.Git {
log("using git")
git := &Commander{
Cmd: "git",
Dir: Options.Output,
}
but.IfFatal(git.RunArgs("init"), "initialize repository")
callback = func(v AssetVersion, r io.Reader) error {
if err := transformFile(transform, Options.Filename, r); err != nil {
return err
}
if err := git.RunArgs("add", Options.Filename); err != nil {
return err
}
commit := strings.NewReader(commitMessage(v, Options.Filename))
if err := git.PipeArgs(commit, "commit",
// Read message from stdin.
"-F", "-",
// Allow empty commits.
"--allow-empty",
// Use asset creation time for commit date.
"--date", strconv.FormatInt(v.Created.Unix(), 10),
); err != nil {
return err
}
if Options.Tag {
return git.RunArgs("tag", fmt.Sprintf("v%d", v.VersionNumber))
}
return nil
}
} else {
log("using file list")
// Check whether format will produce unique filenames.
filename := Options.Filename
ta := time.Now()
tb := ta.AddDate(0, 0, 1)
a := formatFilename(filename, AssetVersion{0, 0, 0, 0, 0, 0, nil, ta, ta})
b := formatFilename(filename, AssetVersion{1, 0, 1, 0, 0, 0, nil, tb, tb})
if a == b {
logf("filename %q not unique\n", filename)
// If not, append the version number.
ext := filepath.Ext(filename)
filename = filename[:len(filename)-len(ext)] + "_v%v" + ext
logf("using %q as filename\n", filename)
} else {
logf("filename %q is unique\n", filename)
}
callback = func(v AssetVersion, r io.Reader) error {
return transformFile(transform, formatFilename(filename, v), r)
}
}
for _, version := range versions {
err := client.GetAssetVersion(version, callback)
if err == errContinue {
continue
}
but.IfFatalf(err, "get asset version %d", version.VersionNumber)
}
}