-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
110 lines (100 loc) · 2.74 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
package main
import (
"flag"
"fmt"
"github.com/vitalick/go-d2editor"
"os"
)
type flags struct {
toJSON bool
toD2s bool
empty bool
version uint
output string
input []string
}
var parsedFlags flags
func init() {
parsedFlags.version = 97
}
func main() {
parseArgs()
if parsedFlags.toJSON {
for _, f := range parsedFlags.input {
c := &d2editor.Character{}
var err error
if parsedFlags.empty {
c, err = d2editor.NewEmptyCharacter(parsedFlags.version)
if err != nil {
fmt.Fprintf(os.Stderr, "Error \"%v\" on create character \"%s\", skipped \n\n", err, f)
continue
}
c.Name = f
} else {
c, err = d2editor.Open(f)
if err != nil {
fmt.Fprintf(os.Stderr, "Error \"%v\" on open file \"%s\", skipped \n\n", err, f)
continue
}
}
c.Header.Version = uint32(parsedFlags.version)
err = d2editor.SaveJSON(c, parsedFlags.output)
if err != nil {
fmt.Fprintf(os.Stderr, "Error \"%v\" on save character \"%s\", skipped \n\n", err, c.Name)
continue
}
}
}
if parsedFlags.toD2s {
for _, f := range parsedFlags.input {
c := &d2editor.Character{}
var err error
if parsedFlags.empty {
c, err = d2editor.NewEmptyCharacter(parsedFlags.version)
if err != nil {
fmt.Fprintf(os.Stderr, "Error \"%v\" on create character \"%s\", skipped \n\n", err, f)
continue
}
c.Name = f
} else {
c, err = d2editor.OpenJSON(f)
if err != nil {
fmt.Fprintf(os.Stderr, "Error \"%v\" on open file \"%s\", skipped \n\n", err, f)
continue
}
}
c.Header.Version = uint32(parsedFlags.version)
err = d2editor.Save(c, parsedFlags.output)
if err != nil {
fmt.Fprintf(os.Stderr, "Error \"%v\" on save character \"%s\", skipped \n\n", err, c.Name)
continue
}
}
}
}
func parseArgs() {
flag.Usage = func() {
fmt.Printf("Usage: %s [options] <input files/names>\n\n", os.Args[0])
flag.PrintDefaults()
}
flag.BoolVar(&parsedFlags.empty, "e", parsedFlags.empty, "Create new empty character.")
flag.BoolVar(&parsedFlags.toJSON, "tojson", parsedFlags.toJSON, "Convert d2s to json or create json.")
flag.BoolVar(&parsedFlags.toD2s, "tod2s", parsedFlags.toD2s, "Convert json to d2s or create d2s.")
flag.StringVar(&parsedFlags.output, "o", parsedFlags.output, "Optional path of the output folder.")
flag.UintVar(&parsedFlags.version, "v", parsedFlags.version, "Save with specific version.")
flag.Parse()
// Make sure we have input paths.
if flag.NArg() == 0 {
_, err := fmt.Fprintf(os.Stderr, "Missing <input files/names>\n\n")
if err != nil {
return
}
flag.Usage()
os.Exit(1)
}
// Create input configurations.
parsedFlags.input = make([]string, flag.NArg())
for i := range parsedFlags.input {
parsedFlags.input[i] = flag.Arg(i)
}
}