-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.go
63 lines (55 loc) · 1.43 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
// The rbxfile-dump command dumps the content of a rbxl/rbxm file.
package main
import (
"flag"
"fmt"
"io"
"os"
"github.com/robloxapi/rbxfile/rbxl"
)
const usage = `usage: rbxfile-dump [INPUT] [OUTPUT]
Reads a binary RBXL or RBXM file from INPUT, and dumps a human-readable
representation of the binary format to OUTPUT.
INPUT and OUTPUT are paths to files. If INPUT is "-" or unspecified, then stdin
is used. If OUTPUT is "-" or unspecified, then stdout is used. Warnings and
errors are written to stderr.
`
func main() {
var input io.Reader = os.Stdin
var output io.Writer = os.Stdout
flag.Usage = func() { fmt.Fprintf(flag.CommandLine.Output(), usage) }
flag.Parse()
args := flag.Args()
if len(args) >= 1 && args[0] != "-" {
in, err := os.Open(args[0])
if err != nil {
fmt.Fprintln(os.Stderr, fmt.Errorf("open input: %w", err))
return
}
input = in
defer in.Close()
}
if len(args) >= 2 && args[1] != "-" {
out, err := os.Create(args[1])
if err != nil {
fmt.Fprintln(os.Stderr, fmt.Errorf("create output: %w", err))
return
}
defer out.Close()
defer func() {
err := out.Sync()
if err != nil {
fmt.Fprintln(os.Stderr, fmt.Errorf("sync output: %w", err))
return
}
}()
output = out
}
warn, err := rbxl.Decoder{}.Dump(output, input)
if warn != nil {
fmt.Fprintln(os.Stderr, fmt.Errorf("warning: %w", warn))
}
if err != nil {
fmt.Fprintln(os.Stderr, fmt.Errorf("error: %w", warn))
}
}