This repository has been archived by the owner on Apr 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
commands.go
296 lines (240 loc) · 5.29 KB
/
commands.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
package main
import (
"fmt"
"github.com/minero/minero-go/proto/nbt"
"os"
"strings"
"path"
"sort"
"strconv"
. "github.com/logrusorgru/aurora"
"bufio"
"io"
"compress/gzip"
"compress/zlib"
)
// represents a command
type command func(args string) error
// represents the root of the compound
var root *nbt.Compound
// represents the nbt path we are at
var curPath = "/"
// represents a map of command names to the functions they run
var commands = map[string]command{
"help": helpCommand,
"cd": cdCommand,
"ls": lsCommand,
"tree": treeCommand,
"cat": catCommand,
"set": setCommand,
"save": saveCommand,
"exit": exitCommand,
}
var help = map[string]string{
"help": "Shows command list.",
"cd <compound>": "Switches context to the provided compound. \"..\" supported.",
"ls [compound]": "Lists the tags in the current context, or provided compound.",
"tree [compound]": "Same as ls, but recursive.",
"cat <tag>": "Prints out the value of the provided tag.",
"set <tag> <type> [value]": "Sets a tag's value/type.",
"save [compress] [output]": "Saves to output. Compression can be gzip, zlib, or none.",
"exit": "Exits nbtnav.",
}
func helpCommand(arg string) error {
keys := make([]string, len(help))
i := 0
maxLen := -1
for k := range help {
keys[i] = k
i++
if len(k) > maxLen {
maxLen = len(k)
}
}
max := strconv.Itoa(maxLen)
sort.Strings(keys)
for _, k := range keys {
fmt.Printf("%"+max+"s - %s\n", Blue(k), Cyan(help[k]))
}
return nil
}
// Enters an nbt compound
func cdCommand(arg string) error {
next, err := pathToTag(arg)
if err != nil {
return err
}
_, ok := next.(*nbt.Compound)
if !ok {
return errNotCompound
}
curPath = resolve(curPath, arg)
return nil
}
// View everything inside the current compound
func lsCommand(arg string) error {
if arg == "" {
tag, _ := pathToTag(".")
if comp, ok := tag.(*nbt.Compound); ok {
prettyPrint(comp.Value)
} else {
return errNotCompound
}
} else {
next := resolve(curPath, arg)
tag, _ := pathToTag(next)
if comp, ok := tag.(*nbt.Compound); ok {
prettyPrint(comp.Value)
} else {
return errNotCompound
}
}
return nil
}
// Similar to ls, but views the whole tree
func treeCommand(arg string) error {
if arg == "" {
tag, _ := pathToTag(".")
deepPrettyPrint(tag.(*nbt.Compound).Value)
} else {
next := resolve(curPath, arg)
tag, _ := pathToTag(next)
if comp, ok := tag.(*nbt.Compound); ok {
deepPrettyPrint(comp.Value)
} else {
return errNotCompound
}
}
return nil
}
// Prints out a value
func catCommand(arg string) error {
if arg == "" {
return errNotEnoughArgs
} else {
next := resolve(curPath, arg)
tag, err := pathToTag(next)
if err != nil {
return err
}
_, ok32 := tag.(*nbt.Float32)
_, ok64 := tag.(*nbt.Float64)
if ok32 || ok64 {
fmt.Println(prettyFloat(tag, true))
}
if _, ok := tag.(*nbt.Compound); ok {
return errPrintedCompound
} else if barr, ok := tag.(*nbt.ByteArray); ok {
fmt.Println(prettyByteArray(barr, true))
} else {
fmt.Println(prettyString(tag))
}
}
return nil
}
// Exit the repl
func exitCommand(arg string) error {
os.Exit(0)
return nil
}
func setCommand(arg string) error {
args := parseMultiArgs(arg)
if len(args) < 2 {
return errNotEnoughArgs
}
targ := args[0]
typ, err := typeFromString(args[1])
if err != nil {
return err
}
val := ""
if len(args) >= 3 {
val = args[2]
}
parentPath, tagName := path.Split(resolve(curPath, targ))
if tagName == "" {
return errNotFound
}
newTag := typ.New()
if val != "" {
err = setTagValue(newTag, val)
if err != nil {
return err
}
}
parentTag, err := pathToTag(parentPath)
if err != nil {
return err
}
if comp, ok := parentTag.(*nbt.Compound); ok {
comp.Value[tagName] = newTag
return nil
} else {
return errNotFound
}
}
func saveCommand(arg string) error {
args := parseMultiArgs(arg)
var compress, output string
if len(args) >= 1 {
compress = strings.ToLower(args[0])
} else {
compress = "none"
}
if len(args) >= 2 {
output = args[1]
} else {
output = os.Args[1]
}
if compress != "gzip" && compress != "zlib" && compress != "none" {
return errInvalidCompression
}
f, err := os.Create(output)
if err != nil {
return err
}
defer f.Close()
var w io.Writer = f
if compress == "gzip" {
gWriter := gzip.NewWriter(w)
defer gWriter.Close()
w = gWriter
} else if compress == "zlib" {
zWriter := zlib.NewWriter(w)
defer zWriter.Close()
w = zWriter
}
err = nbt.Write(w, root)
if err != nil {
return err
}
return nil
}
// Simple way to parse multiple arguments into a slice. Not very advanced but avoids
// needing to use some giant framework.
func parseMultiArgs(args string) []string {
scan := bufio.NewScanner(strings.NewReader(args))
scan.Split(bufio.ScanWords)
stringMode := false
var argSlice []string
index := -1
for scan.Scan() {
txt := scan.Text()
if !stringMode {
index++
argSlice = append(argSlice, "")
} else {
argSlice[index] += " "
}
oldTxt := txt
if txt = strings.TrimPrefix(oldTxt, `"`); oldTxt != txt {
stringMode = true
}
oldTxt = txt
if txt = strings.TrimSuffix(oldTxt, `"`); oldTxt != txt {
stringMode = false
}
argSlice[index] += txt
}
return argSlice
}