forked from niv/neverwinter.nim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnwn_key_transparent.nim
53 lines (40 loc) · 1.56 KB
/
nwn_key_transparent.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
51
52
53
import shared, terminal
let Args = DOC """
This utility walks a set of keyfiles and and prints out statistics on how much
data is transparent (duplicates that do not add changes to resman).
Usage:
$0 [options] <key>...
$USAGE
$OPT
"""
# Load keytables in order.
let keyTables = @(Args["<key>"]).map() do (key: string) -> KeyTable:
let keyRoot = key.expandFilename.splitFile().dir
result = readKeyTable(openFileStream(key, fmRead), key) do (bif: string) -> Stream:
result = openFileStream(keyRoot / bif.extractFilename)
# Holds all resrefs per key table (idx) that are present in earlier key files
# and binary-identical.
let transparent = keyTables.map() do (keyIdx: int, key: KeyTable) -> seq[seq[ResRef]]:
result = newSeq[seq[ResRef]]()
for i in 0..keyTables.high: result.add(newSeq[ResRef]())
if keyIdx > 0:
for o in key.contents:
let thisKeyOdata = key.demand(o).readAll()
for chkIdx, chkKey in keyTables[0..<keyIdx-1]:
if chkKey.contains(o) and chkKey.demand(o).readAll() == thisKeyOdata:
result[chkIdx].add(o)
echo "Key has duplicates ->"
echo align("bytes", 15), " ",
align("files", 12), " in ",
"keyfile"
echo repeat("-", terminalWidth())
for keyIdx, key in keyTables:
let hasData = transparent[keyIdx].anyIt(it.len > 0)
if hasData:
echo key, " ->"
for chkIdx, refs in transparent[keyIdx]:
if refs.len > 0:
let bytes = foldl(refs, a + key.demand(b).ioSize, 0i64)
echo align(bytes.formatSize, 15), " ",
align($refs.len, 12), " in ",
keyTables[chkIdx]