-
Notifications
You must be signed in to change notification settings - Fork 29
/
nwn_resman_extract.nim
50 lines (39 loc) · 1.65 KB
/
nwn_resman_extract.nim
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
import shared
let args = DOC """
Extract file(s) from resman into a directory.
Usage:
$0 [options] <file>...
$0 [options]
$USAGE
Options:
--all Match all files.
-p, --pattern PATTERN Match only files where the name contains PATTERN.
Wildcards are not supported at this time.
-b, --binary BINARY Match only files where the data contains BINARY.
-v, --invert-match Invert matching rules.
-d DIRECTORY Save files to DIRECTORY [default: .]
$OPTRESMAN
"""
if not (args["<file>"] xor (args["--pattern"] or args["--binary"] or args["--all"])):
quit("Give <file> OR any of (--pattern, --binary, --all); try -h to see all options")
let rm = newBasicResMan()
let destination = ($args["-d"])
doAssert(dirExists(destination), "destination directory does not exist")
if args["<file>"]:
let res = args["<file>"].mapIt(newResolvedResRef(it)).mapIt(rm[it].get())
for f in res:
if args["--verbose"]: echo $f.resRef
writeFile(destination / $f.resRef, f.readAll())
elif args["--all"]:
for rr in rm.contents:
let resolved = rm[rr].get()
if args["--verbose"]: echo $resolved.resRef
writeFile(destination / $resolved.resRef, resolved.readAll())
else:
let all = args["--all"]
let invert = args["--invert-match"]
let patternMatch = if args["--pattern"]: $args["--pattern"] else: ""
let binaryMatch = if args["--binary"]: $args["--binary"] else: ""
for resolved in filterByMatch(rm, all, patternMatch, binaryMatch, invert):
if args["--verbose"]: echo $resolved.resRef
writeFile(destination / $resolved.resRef, resolved.readAll())