@ritsuwastaken/farchive 0.0.0-stage → 1.0.1
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/LICENSE +24 -0
- package/README.md +26 -2
- package/dist/constants.d.ts +13 -0
- package/dist/constants.js +13 -0
- package/dist/error.d.ts +5 -0
- package/dist/error.js +10 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +6 -0
- package/dist/reader.d.ts +48 -0
- package/dist/reader.js +150 -0
- package/dist/text.d.ts +3 -0
- package/dist/text.js +24 -0
- package/dist/versions.d.ts +6 -0
- package/dist/versions.js +6 -0
- package/dist/writer.d.ts +32 -0
- package/dist/writer.js +164 -0
- package/package.json +50 -5
package/LICENSE
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
This is free and unencumbered software released into the public domain.
|
|
2
|
+
|
|
3
|
+
Anyone is free to copy, modify, publish, use, compile, sell, or
|
|
4
|
+
distribute this software, either in source code form or as a compiled
|
|
5
|
+
binary, for any purpose, commercial or non-commercial, and by any
|
|
6
|
+
means.
|
|
7
|
+
|
|
8
|
+
In jurisdictions that recognize copyright laws, the author or authors
|
|
9
|
+
of this software dedicate any and all copyright interest in the
|
|
10
|
+
software to the public domain. We make this dedication for the benefit
|
|
11
|
+
of the public at large and to the detriment of our heirs and
|
|
12
|
+
successors. We intend this dedication to be an overt act of
|
|
13
|
+
relinquishment in perpetuity of all present and future rights to this
|
|
14
|
+
software under copyright law.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
19
|
+
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
|
|
20
|
+
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
|
21
|
+
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
22
|
+
OTHER DEALINGS IN THE SOFTWARE.
|
|
23
|
+
|
|
24
|
+
For more information, please refer to <https://unlicense.org>
|
package/README.md
CHANGED
|
@@ -1,3 +1,27 @@
|
|
|
1
|
-
#
|
|
1
|
+
# farchive
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
reader and writer for unreal engine 1, 2 and 3 binary data
|
|
4
|
+
|
|
5
|
+
## Usage
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
import { Reader, Writer } from "farchive";
|
|
9
|
+
|
|
10
|
+
const reader = new Reader(bytes, { version: 123 });
|
|
11
|
+
const count = reader.index();
|
|
12
|
+
const names = Array.from({ length: count }, () => reader.nameEntry());
|
|
13
|
+
|
|
14
|
+
const writer = new Writer({ version: 123 });
|
|
15
|
+
writer.index(names.length);
|
|
16
|
+
for (const entry of names) writer.nameEntry(entry);
|
|
17
|
+
const out = writer.result();
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## 8-bit text
|
|
21
|
+
|
|
22
|
+
```ts
|
|
23
|
+
import { decodeAnsi } from "farchive";
|
|
24
|
+
|
|
25
|
+
decodeAnsi(reader.str(), "euc-kr");
|
|
26
|
+
decodeAnsi(reader.str(), "windows-1251");
|
|
27
|
+
```
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export declare const packageTag = 2653586369;
|
|
2
|
+
export declare const guidSize = 16;
|
|
3
|
+
export declare const maxIndex = 2147483647;
|
|
4
|
+
export declare const compactIndex: {
|
|
5
|
+
readonly maxSize: 5;
|
|
6
|
+
readonly negative: 128;
|
|
7
|
+
readonly firstHasMore: 64;
|
|
8
|
+
readonly firstBits: 6;
|
|
9
|
+
readonly firstMask: 63;
|
|
10
|
+
readonly hasMore: 128;
|
|
11
|
+
readonly bits: 7;
|
|
12
|
+
readonly mask: 127;
|
|
13
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export const packageTag = 0x9e2a83c1;
|
|
2
|
+
export const guidSize = 16;
|
|
3
|
+
export const maxIndex = 0x7fffffff;
|
|
4
|
+
export const compactIndex = {
|
|
5
|
+
maxSize: 5,
|
|
6
|
+
negative: 0x80,
|
|
7
|
+
firstHasMore: 0x40,
|
|
8
|
+
firstBits: 6,
|
|
9
|
+
firstMask: 0x3f,
|
|
10
|
+
hasMore: 0x80,
|
|
11
|
+
bits: 7,
|
|
12
|
+
mask: 0x7f,
|
|
13
|
+
};
|
package/dist/error.d.ts
ADDED
package/dist/error.js
ADDED
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { compactIndex, guidSize, maxIndex, packageTag } from "./constants.ts";
|
|
2
|
+
export { ArchiveError } from "./error.ts";
|
|
3
|
+
export { Reader, usesCompactIndex, type ArchiveOptions, type NameEntry, type NameRef } from "./reader.ts";
|
|
4
|
+
export { ansiBytes, decodeAnsi, isAnsi } from "./text.ts";
|
|
5
|
+
export { versions } from "./versions.ts";
|
|
6
|
+
export { Writer } from "./writer.ts";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { compactIndex, guidSize, maxIndex, packageTag } from "./constants.js";
|
|
2
|
+
export { ArchiveError } from "./error.js";
|
|
3
|
+
export { Reader, usesCompactIndex } from "./reader.js";
|
|
4
|
+
export { ansiBytes, decodeAnsi, isAnsi } from "./text.js";
|
|
5
|
+
export { versions } from "./versions.js";
|
|
6
|
+
export { Writer } from "./writer.js";
|
package/dist/reader.d.ts
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
export interface ArchiveOptions {
|
|
2
|
+
version?: number;
|
|
3
|
+
licensee?: number;
|
|
4
|
+
}
|
|
5
|
+
export interface NameEntry {
|
|
6
|
+
name: string;
|
|
7
|
+
flags: bigint;
|
|
8
|
+
}
|
|
9
|
+
export interface NameRef {
|
|
10
|
+
index: number;
|
|
11
|
+
number: number;
|
|
12
|
+
}
|
|
13
|
+
export declare const usesCompactIndex: (version: number | undefined) => boolean;
|
|
14
|
+
export declare class Reader {
|
|
15
|
+
readonly bytes: Uint8Array;
|
|
16
|
+
private readonly view;
|
|
17
|
+
readonly version: number | undefined;
|
|
18
|
+
readonly licensee: number | undefined;
|
|
19
|
+
offset: number;
|
|
20
|
+
constructor(bytes: Uint8Array, options?: ArchiveOptions & {
|
|
21
|
+
offset?: number;
|
|
22
|
+
});
|
|
23
|
+
get remaining(): number;
|
|
24
|
+
get atEnd(): boolean;
|
|
25
|
+
need(size: number): void;
|
|
26
|
+
seek(offset: number): void;
|
|
27
|
+
skip(size: number): void;
|
|
28
|
+
take(size: number): Uint8Array;
|
|
29
|
+
private advance;
|
|
30
|
+
u8(): number;
|
|
31
|
+
i8(): number;
|
|
32
|
+
u16(): number;
|
|
33
|
+
i16(): number;
|
|
34
|
+
u32(): number;
|
|
35
|
+
i32(): number;
|
|
36
|
+
u64(): bigint;
|
|
37
|
+
i64(): bigint;
|
|
38
|
+
f32(): number;
|
|
39
|
+
f64(): number;
|
|
40
|
+
compactIndex(): number;
|
|
41
|
+
index(): number;
|
|
42
|
+
str(): string;
|
|
43
|
+
ansiz(): string;
|
|
44
|
+
nameEntry(): NameEntry;
|
|
45
|
+
name(): NameRef;
|
|
46
|
+
guid(): Uint8Array;
|
|
47
|
+
array<T>(read: (reader: this) => T): T[];
|
|
48
|
+
}
|
package/dist/reader.js
ADDED
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import { compactIndex as compact, guidSize, maxIndex } from "./constants.js";
|
|
2
|
+
import { ArchiveError } from "./error.js";
|
|
3
|
+
import { versions } from "./versions.js";
|
|
4
|
+
const latin1 = new TextDecoder("latin1");
|
|
5
|
+
const utf16 = new TextDecoder("utf-16le");
|
|
6
|
+
export const usesCompactIndex = (version) => version === undefined || version < versions.int32Indexes;
|
|
7
|
+
export class Reader {
|
|
8
|
+
bytes;
|
|
9
|
+
view;
|
|
10
|
+
version;
|
|
11
|
+
licensee;
|
|
12
|
+
offset;
|
|
13
|
+
constructor(bytes, options = {}) {
|
|
14
|
+
this.bytes = bytes;
|
|
15
|
+
this.view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
16
|
+
this.version = options.version;
|
|
17
|
+
this.licensee = options.licensee;
|
|
18
|
+
this.offset = options.offset ?? 0;
|
|
19
|
+
}
|
|
20
|
+
get remaining() {
|
|
21
|
+
return this.bytes.length - this.offset;
|
|
22
|
+
}
|
|
23
|
+
get atEnd() {
|
|
24
|
+
return this.offset >= this.bytes.length;
|
|
25
|
+
}
|
|
26
|
+
need(size) {
|
|
27
|
+
if (size < 0 || this.offset + size > this.bytes.length)
|
|
28
|
+
throw new ArchiveError("Unexpected end of data", this.offset);
|
|
29
|
+
}
|
|
30
|
+
seek(offset) {
|
|
31
|
+
if (offset < 0 || offset > this.bytes.length)
|
|
32
|
+
throw new ArchiveError(`Cannot seek to ${offset}`, this.offset);
|
|
33
|
+
this.offset = offset;
|
|
34
|
+
}
|
|
35
|
+
skip(size) {
|
|
36
|
+
this.need(size);
|
|
37
|
+
this.offset += size;
|
|
38
|
+
}
|
|
39
|
+
take(size) {
|
|
40
|
+
this.need(size);
|
|
41
|
+
const value = this.bytes.slice(this.offset, this.offset + size);
|
|
42
|
+
this.offset += size;
|
|
43
|
+
return value;
|
|
44
|
+
}
|
|
45
|
+
advance(size) {
|
|
46
|
+
this.need(size);
|
|
47
|
+
const at = this.offset;
|
|
48
|
+
this.offset += size;
|
|
49
|
+
return at;
|
|
50
|
+
}
|
|
51
|
+
u8() {
|
|
52
|
+
return this.view.getUint8(this.advance(1));
|
|
53
|
+
}
|
|
54
|
+
i8() {
|
|
55
|
+
return this.view.getInt8(this.advance(1));
|
|
56
|
+
}
|
|
57
|
+
u16() {
|
|
58
|
+
return this.view.getUint16(this.advance(2), true);
|
|
59
|
+
}
|
|
60
|
+
i16() {
|
|
61
|
+
return this.view.getInt16(this.advance(2), true);
|
|
62
|
+
}
|
|
63
|
+
u32() {
|
|
64
|
+
return this.view.getUint32(this.advance(4), true);
|
|
65
|
+
}
|
|
66
|
+
i32() {
|
|
67
|
+
return this.view.getInt32(this.advance(4), true);
|
|
68
|
+
}
|
|
69
|
+
u64() {
|
|
70
|
+
return this.view.getBigUint64(this.advance(8), true);
|
|
71
|
+
}
|
|
72
|
+
i64() {
|
|
73
|
+
return this.view.getBigInt64(this.advance(8), true);
|
|
74
|
+
}
|
|
75
|
+
f32() {
|
|
76
|
+
return this.view.getFloat32(this.advance(4), true);
|
|
77
|
+
}
|
|
78
|
+
f64() {
|
|
79
|
+
return this.view.getFloat64(this.advance(8), true);
|
|
80
|
+
}
|
|
81
|
+
compactIndex() {
|
|
82
|
+
const start = this.offset;
|
|
83
|
+
const first = this.u8();
|
|
84
|
+
let value = first & compact.firstMask;
|
|
85
|
+
if (first & compact.firstHasMore) {
|
|
86
|
+
let shift = compact.firstBits;
|
|
87
|
+
for (let i = 1; i < compact.maxSize; i++) {
|
|
88
|
+
const byte = this.u8();
|
|
89
|
+
if (i === compact.maxSize - 1) {
|
|
90
|
+
value += byte * 2 ** shift;
|
|
91
|
+
break;
|
|
92
|
+
}
|
|
93
|
+
value += (byte & compact.mask) * 2 ** shift;
|
|
94
|
+
shift += compact.bits;
|
|
95
|
+
if (!(byte & compact.hasMore))
|
|
96
|
+
break;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
if (value > maxIndex)
|
|
100
|
+
throw new ArchiveError("Compact index out of range", start);
|
|
101
|
+
return first & compact.negative ? -value : value;
|
|
102
|
+
}
|
|
103
|
+
index() {
|
|
104
|
+
return usesCompactIndex(this.version) ? this.compactIndex() : this.i32();
|
|
105
|
+
}
|
|
106
|
+
str() {
|
|
107
|
+
const start = this.offset;
|
|
108
|
+
const length = this.index();
|
|
109
|
+
if (length === 0)
|
|
110
|
+
return "";
|
|
111
|
+
const wide = length < 0;
|
|
112
|
+
const size = wide ? -length * 2 : length;
|
|
113
|
+
this.need(size);
|
|
114
|
+
const end = this.offset + size;
|
|
115
|
+
const terminated = wide ? this.bytes[end - 2] === 0 && this.bytes[end - 1] === 0 : this.bytes[end - 1] === 0;
|
|
116
|
+
if (!terminated)
|
|
117
|
+
throw new ArchiveError("String is not terminated", start);
|
|
118
|
+
const text = (wide ? utf16 : latin1).decode(this.bytes.subarray(this.offset, end - (wide ? 2 : 1)));
|
|
119
|
+
this.offset = end;
|
|
120
|
+
return text;
|
|
121
|
+
}
|
|
122
|
+
ansiz() {
|
|
123
|
+
const end = this.bytes.indexOf(0, this.offset);
|
|
124
|
+
if (end < 0)
|
|
125
|
+
throw new ArchiveError("String is not terminated", this.offset);
|
|
126
|
+
const text = latin1.decode(this.bytes.subarray(this.offset, end));
|
|
127
|
+
this.offset = end + 1;
|
|
128
|
+
return text;
|
|
129
|
+
}
|
|
130
|
+
nameEntry() {
|
|
131
|
+
const name = this.version !== undefined && this.version < versions.lengthPrefixedNames ? this.ansiz() : this.str();
|
|
132
|
+
const flags = this.version !== undefined && this.version >= versions.wideNameFlags ? this.u64() : BigInt(this.u32());
|
|
133
|
+
return { name, flags };
|
|
134
|
+
}
|
|
135
|
+
name() {
|
|
136
|
+
const index = this.index();
|
|
137
|
+
const number = this.version !== undefined && this.version >= versions.nameNumbers ? this.i32() : 0;
|
|
138
|
+
return { index, number };
|
|
139
|
+
}
|
|
140
|
+
guid() {
|
|
141
|
+
return this.take(guidSize);
|
|
142
|
+
}
|
|
143
|
+
array(read) {
|
|
144
|
+
const at = this.offset;
|
|
145
|
+
const count = this.index();
|
|
146
|
+
if (count < 0 || count > this.remaining)
|
|
147
|
+
throw new ArchiveError(`Invalid array length ${count}`, at);
|
|
148
|
+
return Array.from({ length: count }, () => read(this));
|
|
149
|
+
}
|
|
150
|
+
}
|
package/dist/text.d.ts
ADDED
package/dist/text.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
const maxAnsi = 0xff;
|
|
2
|
+
export const isAnsi = (text) => !/[^\x00-\xff]/.test(text);
|
|
3
|
+
export const ansiBytes = (text) => {
|
|
4
|
+
const bytes = new Uint8Array(text.length);
|
|
5
|
+
for (let i = 0; i < text.length; i++) {
|
|
6
|
+
const code = text.charCodeAt(i);
|
|
7
|
+
if (code > maxAnsi)
|
|
8
|
+
throw new RangeError(`Not an 8-bit string: ${JSON.stringify(text[i])}`);
|
|
9
|
+
bytes[i] = code;
|
|
10
|
+
}
|
|
11
|
+
return bytes;
|
|
12
|
+
};
|
|
13
|
+
export const decodeAnsi = (text, encoding) => {
|
|
14
|
+
if (!/[\x80-\xff]/.test(text))
|
|
15
|
+
return text;
|
|
16
|
+
if (!isAnsi(text))
|
|
17
|
+
return null;
|
|
18
|
+
try {
|
|
19
|
+
return new TextDecoder(encoding, { fatal: true }).decode(ansiBytes(text));
|
|
20
|
+
}
|
|
21
|
+
catch {
|
|
22
|
+
return null;
|
|
23
|
+
}
|
|
24
|
+
};
|
package/dist/versions.js
ADDED
package/dist/writer.d.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { type ArchiveOptions, type NameEntry, type NameRef } from "./reader.ts";
|
|
2
|
+
export declare class Writer {
|
|
3
|
+
readonly version: number | undefined;
|
|
4
|
+
readonly licensee: number | undefined;
|
|
5
|
+
private buffer;
|
|
6
|
+
private view;
|
|
7
|
+
length: number;
|
|
8
|
+
constructor(options?: ArchiveOptions);
|
|
9
|
+
private reserve;
|
|
10
|
+
bytes(value: Uint8Array): void;
|
|
11
|
+
u8(value: number): void;
|
|
12
|
+
i8(value: number): void;
|
|
13
|
+
u16(value: number): void;
|
|
14
|
+
i16(value: number): void;
|
|
15
|
+
u32(value: number): void;
|
|
16
|
+
i32(value: number): void;
|
|
17
|
+
u64(value: bigint): void;
|
|
18
|
+
i64(value: bigint): void;
|
|
19
|
+
f32(value: number): void;
|
|
20
|
+
f64(value: number): void;
|
|
21
|
+
compactIndex(value: number): void;
|
|
22
|
+
index(value: number): void;
|
|
23
|
+
str(value: string): void;
|
|
24
|
+
ansiz(value: string): void;
|
|
25
|
+
nameEntry(entry: NameEntry): void;
|
|
26
|
+
name(ref: NameRef | number): void;
|
|
27
|
+
guid(value: Uint8Array): void;
|
|
28
|
+
array<T>(values: readonly T[], write: (writer: this, value: T) => void): void;
|
|
29
|
+
patchU32(at: number, value: number): void;
|
|
30
|
+
patchI32(at: number, value: number): void;
|
|
31
|
+
result(): Uint8Array;
|
|
32
|
+
}
|
package/dist/writer.js
ADDED
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import { compactIndex as compact, guidSize, maxIndex } from "./constants.js";
|
|
2
|
+
import { ArchiveError } from "./error.js";
|
|
3
|
+
import { usesCompactIndex } from "./reader.js";
|
|
4
|
+
import { isAnsi } from "./text.js";
|
|
5
|
+
import { versions } from "./versions.js";
|
|
6
|
+
const initialCapacity = 4096;
|
|
7
|
+
const u32Mask = 0xffffffffn;
|
|
8
|
+
export class Writer {
|
|
9
|
+
version;
|
|
10
|
+
licensee;
|
|
11
|
+
buffer = new Uint8Array(initialCapacity);
|
|
12
|
+
view = new DataView(this.buffer.buffer);
|
|
13
|
+
length = 0;
|
|
14
|
+
constructor(options = {}) {
|
|
15
|
+
this.version = options.version;
|
|
16
|
+
this.licensee = options.licensee;
|
|
17
|
+
}
|
|
18
|
+
reserve(size) {
|
|
19
|
+
const at = this.length;
|
|
20
|
+
if (at + size > this.buffer.length) {
|
|
21
|
+
let capacity = this.buffer.length * 2;
|
|
22
|
+
while (capacity < at + size)
|
|
23
|
+
capacity *= 2;
|
|
24
|
+
const next = new Uint8Array(capacity);
|
|
25
|
+
next.set(this.buffer.subarray(0, at));
|
|
26
|
+
this.buffer = next;
|
|
27
|
+
this.view = new DataView(next.buffer);
|
|
28
|
+
}
|
|
29
|
+
this.length += size;
|
|
30
|
+
return at;
|
|
31
|
+
}
|
|
32
|
+
bytes(value) {
|
|
33
|
+
const at = this.reserve(value.length);
|
|
34
|
+
this.buffer.set(value, at);
|
|
35
|
+
}
|
|
36
|
+
u8(value) {
|
|
37
|
+
const at = this.reserve(1);
|
|
38
|
+
this.view.setUint8(at, value);
|
|
39
|
+
}
|
|
40
|
+
i8(value) {
|
|
41
|
+
const at = this.reserve(1);
|
|
42
|
+
this.view.setInt8(at, value);
|
|
43
|
+
}
|
|
44
|
+
u16(value) {
|
|
45
|
+
const at = this.reserve(2);
|
|
46
|
+
this.view.setUint16(at, value, true);
|
|
47
|
+
}
|
|
48
|
+
i16(value) {
|
|
49
|
+
const at = this.reserve(2);
|
|
50
|
+
this.view.setInt16(at, value, true);
|
|
51
|
+
}
|
|
52
|
+
u32(value) {
|
|
53
|
+
const at = this.reserve(4);
|
|
54
|
+
this.view.setUint32(at, value >>> 0, true);
|
|
55
|
+
}
|
|
56
|
+
i32(value) {
|
|
57
|
+
const at = this.reserve(4);
|
|
58
|
+
this.view.setInt32(at, value, true);
|
|
59
|
+
}
|
|
60
|
+
u64(value) {
|
|
61
|
+
const at = this.reserve(8);
|
|
62
|
+
this.view.setBigUint64(at, value, true);
|
|
63
|
+
}
|
|
64
|
+
i64(value) {
|
|
65
|
+
const at = this.reserve(8);
|
|
66
|
+
this.view.setBigInt64(at, value, true);
|
|
67
|
+
}
|
|
68
|
+
f32(value) {
|
|
69
|
+
const at = this.reserve(4);
|
|
70
|
+
this.view.setFloat32(at, value, true);
|
|
71
|
+
}
|
|
72
|
+
f64(value) {
|
|
73
|
+
const at = this.reserve(8);
|
|
74
|
+
this.view.setFloat64(at, value, true);
|
|
75
|
+
}
|
|
76
|
+
compactIndex(value) {
|
|
77
|
+
if (!Number.isInteger(value) || Math.abs(value) > maxIndex)
|
|
78
|
+
throw new ArchiveError(`Compact index out of range: ${value}`);
|
|
79
|
+
let rest = Math.abs(value);
|
|
80
|
+
const fits = rest <= compact.firstMask;
|
|
81
|
+
const bytes = [(value < 0 ? compact.negative : 0) | (fits ? rest : (rest & compact.firstMask) | compact.firstHasMore)];
|
|
82
|
+
if (!fits) {
|
|
83
|
+
rest >>>= compact.firstBits;
|
|
84
|
+
for (let i = 1; i < compact.maxSize; i++) {
|
|
85
|
+
const last = i === compact.maxSize - 1 || rest <= compact.mask;
|
|
86
|
+
bytes.push(last ? rest : (rest & compact.mask) | compact.hasMore);
|
|
87
|
+
if (last)
|
|
88
|
+
break;
|
|
89
|
+
rest >>>= compact.bits;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
this.bytes(Uint8Array.from(bytes));
|
|
93
|
+
}
|
|
94
|
+
index(value) {
|
|
95
|
+
if (usesCompactIndex(this.version))
|
|
96
|
+
this.compactIndex(value);
|
|
97
|
+
else
|
|
98
|
+
this.i32(value);
|
|
99
|
+
}
|
|
100
|
+
str(value) {
|
|
101
|
+
if (value === "") {
|
|
102
|
+
this.index(0);
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
const wide = !isAnsi(value);
|
|
106
|
+
const size = value.length + 1;
|
|
107
|
+
this.index(wide ? -size : size);
|
|
108
|
+
if (wide) {
|
|
109
|
+
const at = this.reserve(size * 2);
|
|
110
|
+
for (let i = 0; i < value.length; i++)
|
|
111
|
+
this.view.setUint16(at + i * 2, value.charCodeAt(i), true);
|
|
112
|
+
this.view.setUint16(at + value.length * 2, 0, true);
|
|
113
|
+
}
|
|
114
|
+
else {
|
|
115
|
+
const at = this.reserve(size);
|
|
116
|
+
for (let i = 0; i < value.length; i++)
|
|
117
|
+
this.buffer[at + i] = value.charCodeAt(i);
|
|
118
|
+
this.buffer[at + value.length] = 0;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
ansiz(value) {
|
|
122
|
+
if (!isAnsi(value) || value.includes("\0"))
|
|
123
|
+
throw new ArchiveError("A null-terminated string must be Latin-1 without zero characters");
|
|
124
|
+
const at = this.reserve(value.length + 1);
|
|
125
|
+
for (let i = 0; i < value.length; i++)
|
|
126
|
+
this.buffer[at + i] = value.charCodeAt(i);
|
|
127
|
+
this.buffer[at + value.length] = 0;
|
|
128
|
+
}
|
|
129
|
+
nameEntry(entry) {
|
|
130
|
+
if (this.version !== undefined && this.version < versions.lengthPrefixedNames)
|
|
131
|
+
this.ansiz(entry.name);
|
|
132
|
+
else
|
|
133
|
+
this.str(entry.name);
|
|
134
|
+
if (this.version !== undefined && this.version >= versions.wideNameFlags)
|
|
135
|
+
this.u64(entry.flags);
|
|
136
|
+
else
|
|
137
|
+
this.u32(Number(entry.flags & u32Mask));
|
|
138
|
+
}
|
|
139
|
+
name(ref) {
|
|
140
|
+
const { index, number } = typeof ref === "number" ? { index: ref, number: 0 } : ref;
|
|
141
|
+
this.index(index);
|
|
142
|
+
if (this.version !== undefined && this.version >= versions.nameNumbers)
|
|
143
|
+
this.i32(number);
|
|
144
|
+
}
|
|
145
|
+
guid(value) {
|
|
146
|
+
if (value.length !== guidSize)
|
|
147
|
+
throw new ArchiveError(`A GUID is ${guidSize} bytes, got ${value.length}`);
|
|
148
|
+
this.bytes(value);
|
|
149
|
+
}
|
|
150
|
+
array(values, write) {
|
|
151
|
+
this.index(values.length);
|
|
152
|
+
for (const value of values)
|
|
153
|
+
write(this, value);
|
|
154
|
+
}
|
|
155
|
+
patchU32(at, value) {
|
|
156
|
+
this.view.setUint32(at, value >>> 0, true);
|
|
157
|
+
}
|
|
158
|
+
patchI32(at, value) {
|
|
159
|
+
this.view.setInt32(at, value, true);
|
|
160
|
+
}
|
|
161
|
+
result() {
|
|
162
|
+
return this.buffer.slice(0, this.length);
|
|
163
|
+
}
|
|
164
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,51 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
"name": "@ritsuwastaken/farchive",
|
|
3
|
+
"version": "1.0.1",
|
|
4
|
+
"description": "reader and writer for unreal engine 1, 2 and 3 binary data",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"sideEffects": false,
|
|
7
|
+
"main": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"default": "./dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist",
|
|
17
|
+
"README.md"
|
|
18
|
+
],
|
|
19
|
+
"keywords": [
|
|
20
|
+
"farchive",
|
|
21
|
+
"archive",
|
|
22
|
+
"serialization",
|
|
23
|
+
"compact index",
|
|
24
|
+
"fstring",
|
|
25
|
+
"binary",
|
|
26
|
+
"typescript",
|
|
27
|
+
"javascript",
|
|
28
|
+
"unreal",
|
|
29
|
+
"ue",
|
|
30
|
+
"ue2",
|
|
31
|
+
"ue3"
|
|
32
|
+
],
|
|
33
|
+
"author": "ritsuwastaken",
|
|
34
|
+
"license": "Unlicense",
|
|
35
|
+
"repository": {
|
|
36
|
+
"type": "git",
|
|
37
|
+
"url": "git+https://github.com/ritsuwastaken/farchive.git"
|
|
38
|
+
},
|
|
39
|
+
"engines": {
|
|
40
|
+
"node": ">=22"
|
|
41
|
+
},
|
|
42
|
+
"scripts": {
|
|
43
|
+
"build": "tsc -p tsconfig.json",
|
|
44
|
+
"test": "npm run build && node --test \"test/**/*.test.ts\"",
|
|
45
|
+
"prepack": "npm run build"
|
|
46
|
+
},
|
|
47
|
+
"devDependencies": {
|
|
48
|
+
"@types/node": "^22.20.5",
|
|
49
|
+
"typescript": "7.0.2"
|
|
50
|
+
}
|
|
51
|
+
}
|