chubakabra 0.1.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/node/index.js +48 -22
package/package.json
CHANGED
package/src/node/index.js
CHANGED
|
@@ -7,17 +7,22 @@ const RANGE_SIZE = 16
|
|
|
7
7
|
export function readKvrHeader(path)
|
|
8
8
|
{
|
|
9
9
|
const fd = fs.openSync(path, "r")
|
|
10
|
-
const
|
|
11
|
-
fs.readSync(fd,
|
|
10
|
+
const buf = Buffer.alloc(HEADER_SIZE)
|
|
11
|
+
fs.readSync(fd, buf, 0, HEADER_SIZE, 0)
|
|
12
12
|
fs.closeSync(fd)
|
|
13
13
|
|
|
14
|
-
if (
|
|
14
|
+
if (
|
|
15
|
+
buf[0] !== 0x4B || // K
|
|
16
|
+
buf[1] !== 0x56 || // V
|
|
17
|
+
buf[2] !== 0x52 || // R
|
|
18
|
+
buf[3] !== 1
|
|
19
|
+
)
|
|
15
20
|
throw new Error("Invalid KVR file")
|
|
16
21
|
|
|
17
22
|
return {
|
|
18
|
-
keySize:
|
|
19
|
-
indexCount:
|
|
20
|
-
indexOffset: Number(
|
|
23
|
+
keySize: buf.readUInt16LE(4),
|
|
24
|
+
indexCount: buf.readUInt32LE(6),
|
|
25
|
+
indexOffset: Number(buf.readBigUInt64LE(10))
|
|
21
26
|
}
|
|
22
27
|
}
|
|
23
28
|
|
|
@@ -37,6 +42,7 @@ export function readKvrIndex(path)
|
|
|
37
42
|
for (let i = 0; i < h.indexCount; i++)
|
|
38
43
|
{
|
|
39
44
|
const o = i * entrySize
|
|
45
|
+
|
|
40
46
|
entries.push({
|
|
41
47
|
key: buf.subarray(o, o + h.keySize),
|
|
42
48
|
range: [
|
|
@@ -46,7 +52,10 @@ export function readKvrIndex(path)
|
|
|
46
52
|
})
|
|
47
53
|
}
|
|
48
54
|
|
|
49
|
-
return {
|
|
55
|
+
return {
|
|
56
|
+
keySize: h.keySize,
|
|
57
|
+
entries
|
|
58
|
+
}
|
|
50
59
|
}
|
|
51
60
|
|
|
52
61
|
export function readKvrRange(path, range)
|
|
@@ -61,16 +70,29 @@ export function readKvrRange(path, range)
|
|
|
61
70
|
|
|
62
71
|
export function writeKvr(path, entries, codec)
|
|
63
72
|
{
|
|
73
|
+
if (entries.length === 0)
|
|
74
|
+
throw new Error("Empty entries")
|
|
75
|
+
|
|
64
76
|
const keySize = entries[0].key.length * codec.size
|
|
77
|
+
|
|
78
|
+
for (const e of entries)
|
|
79
|
+
{
|
|
80
|
+
if (e.key.length * codec.size !== keySize)
|
|
81
|
+
throw new Error("Inconsistent key size")
|
|
82
|
+
}
|
|
83
|
+
|
|
65
84
|
const fd = fs.openSync(path, "w")
|
|
66
85
|
|
|
67
86
|
const header = Buffer.alloc(HEADER_SIZE)
|
|
68
|
-
header
|
|
87
|
+
header[0] = 0x4B
|
|
88
|
+
header[1] = 0x56
|
|
89
|
+
header[2] = 0x52
|
|
69
90
|
header[3] = 1
|
|
70
91
|
header.writeUInt16LE(keySize, 4)
|
|
71
92
|
header.writeUInt32LE(entries.length, 6)
|
|
72
93
|
header.writeBigUInt64LE(0n, 10)
|
|
73
|
-
|
|
94
|
+
|
|
95
|
+
fs.writeSync(fd, header, 0, HEADER_SIZE, 0)
|
|
74
96
|
|
|
75
97
|
let offset = HEADER_SIZE
|
|
76
98
|
const index = []
|
|
@@ -82,6 +104,7 @@ export function writeKvr(path, entries, codec)
|
|
|
82
104
|
from: offset,
|
|
83
105
|
to: offset + e.data.length
|
|
84
106
|
})
|
|
107
|
+
|
|
85
108
|
fs.writeSync(fd, e.data, 0, e.data.length, offset)
|
|
86
109
|
offset += e.data.length
|
|
87
110
|
}
|
|
@@ -90,33 +113,36 @@ export function writeKvr(path, entries, codec)
|
|
|
90
113
|
|
|
91
114
|
for (const i of index)
|
|
92
115
|
{
|
|
93
|
-
const
|
|
94
|
-
i.key.copy(
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
116
|
+
const buf = Buffer.alloc(keySize + RANGE_SIZE)
|
|
117
|
+
i.key.copy(buf, 0)
|
|
118
|
+
buf.writeBigUInt64LE(BigInt(i.from), keySize)
|
|
119
|
+
buf.writeBigUInt64LE(BigInt(i.to), keySize + 8)
|
|
120
|
+
|
|
121
|
+
fs.writeSync(fd, buf, 0, buf.length, offset)
|
|
122
|
+
offset += buf.length
|
|
98
123
|
}
|
|
99
124
|
|
|
100
125
|
header.writeBigUInt64LE(BigInt(indexOffset), 10)
|
|
101
126
|
fs.writeSync(fd, header, 0, HEADER_SIZE, 0)
|
|
127
|
+
|
|
102
128
|
fs.closeSync(fd)
|
|
103
129
|
}
|
|
104
130
|
|
|
105
131
|
export function packKey(arr, codec)
|
|
106
132
|
{
|
|
107
|
-
const
|
|
133
|
+
const buf = Buffer.alloc(arr.length * codec.size)
|
|
108
134
|
for (let i = 0; i < arr.length; i++)
|
|
109
|
-
codec.write(
|
|
110
|
-
return
|
|
135
|
+
codec.write(buf, arr[i], i * codec.size)
|
|
136
|
+
return buf
|
|
111
137
|
}
|
|
112
138
|
|
|
113
139
|
export function unpackKey(buf, codec)
|
|
114
140
|
{
|
|
115
|
-
const
|
|
116
|
-
const
|
|
117
|
-
for (let i = 0; i <
|
|
118
|
-
|
|
119
|
-
return
|
|
141
|
+
const count = buf.length / codec.size
|
|
142
|
+
const arr = new Array(count)
|
|
143
|
+
for (let i = 0; i < count; i++)
|
|
144
|
+
arr[i] = codec.read(buf, i * codec.size)
|
|
145
|
+
return arr
|
|
120
146
|
}
|
|
121
147
|
|
|
122
148
|
export function createElementCodec(type)
|