chubakabra 0.2.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 +43 -25
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,11 +70,23 @@ 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)
|
|
@@ -76,7 +97,6 @@ export function writeKvr(path, entries, codec)
|
|
|
76
97
|
let offset = HEADER_SIZE
|
|
77
98
|
const index = []
|
|
78
99
|
|
|
79
|
-
// DATA
|
|
80
100
|
for (const e of entries)
|
|
81
101
|
{
|
|
82
102
|
index.push({
|
|
@@ -89,21 +109,19 @@ export function writeKvr(path, entries, codec)
|
|
|
89
109
|
offset += e.data.length
|
|
90
110
|
}
|
|
91
111
|
|
|
92
|
-
// INDEX
|
|
93
112
|
const indexOffset = offset
|
|
94
113
|
|
|
95
114
|
for (const i of index)
|
|
96
115
|
{
|
|
97
|
-
const
|
|
98
|
-
i.key.copy(
|
|
99
|
-
|
|
100
|
-
|
|
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)
|
|
101
120
|
|
|
102
|
-
fs.writeSync(fd,
|
|
103
|
-
offset +=
|
|
121
|
+
fs.writeSync(fd, buf, 0, buf.length, offset)
|
|
122
|
+
offset += buf.length
|
|
104
123
|
}
|
|
105
124
|
|
|
106
|
-
// PATCH header
|
|
107
125
|
header.writeBigUInt64LE(BigInt(indexOffset), 10)
|
|
108
126
|
fs.writeSync(fd, header, 0, HEADER_SIZE, 0)
|
|
109
127
|
|
|
@@ -112,19 +130,19 @@ export function writeKvr(path, entries, codec)
|
|
|
112
130
|
|
|
113
131
|
export function packKey(arr, codec)
|
|
114
132
|
{
|
|
115
|
-
const
|
|
133
|
+
const buf = Buffer.alloc(arr.length * codec.size)
|
|
116
134
|
for (let i = 0; i < arr.length; i++)
|
|
117
|
-
codec.write(
|
|
118
|
-
return
|
|
135
|
+
codec.write(buf, arr[i], i * codec.size)
|
|
136
|
+
return buf
|
|
119
137
|
}
|
|
120
138
|
|
|
121
139
|
export function unpackKey(buf, codec)
|
|
122
140
|
{
|
|
123
|
-
const
|
|
124
|
-
const
|
|
125
|
-
for (let i = 0; i <
|
|
126
|
-
|
|
127
|
-
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
|
|
128
146
|
}
|
|
129
147
|
|
|
130
148
|
export function createElementCodec(type)
|