-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathindex.js
105 lines (88 loc) · 2.46 KB
/
index.js
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import ref from 'ref-napi';
import Files from './files';
import { default as StormLib, HANDLEPtr } from './storm-lib';
class MPQ {
static OPEN = {
READ_ONLY: 0x00000100,
WRITE_SHARE: 0x00000200,
USE_BITMAP: 0x00000400,
NO_LISTFILE: 0x00010000,
NO_ATTRIBUTES: 0x00020000,
NO_HEADER_SEARCH: 0x00040000,
FORCE_MPQ_V1: 0x00080000,
CHECK_SECTOR_CRC: 0x00100000,
};
static CREATE = {
LISTFILE: 0x00100000,
ATTRIBUTES: 0x00200000,
SIGNATURE: 0x00400000,
ARCHIVE_V1: 0x00000000,
ARCHIVE_V2: 0x01000000,
ARCHIVE_V3: 0x02000000,
ARCHIVE_V4: 0x03000000,
};
constructor(path, flags, handle) {
this.path = path;
this.flags = flags;
this.handle = handle;
this.files = new Files(this);
}
close() {
const handle = this.handle;
if (handle) {
this.handle = null;
return StormLib.SFileCloseArchive(handle);
}
}
get opened() {
return !!this.handle;
}
patch(path, prefix = null) {
if (!(this.flags & this.constructor.OPEN.READ_ONLY)) {
throw new Error('archive must be read-only');
}
const flags = 0;
return StormLib.SFileOpenPatchArchive(this.handle, path, prefix, flags);
}
get patched() {
if (this.handle) {
return StormLib.SFileIsPatchedArchive(this.handle);
}
}
static get locale() {
return StormLib.SFileGetLocale();
}
static set locale(locale) {
StormLib.SFileSetLocale(locale);
}
static open(path, flags = 0, callback) {
if (typeof flags === 'function' && callback === undefined) {
return this.open(path, null, flags);
}
const priority = 0;
const handlePtr = ref.alloc(HANDLEPtr);
if (StormLib.SFileOpenArchive(path, priority, flags, handlePtr)) {
const handle = handlePtr.deref();
const mpq = new this(path, flags, handle);
if (callback !== undefined) {
callback(mpq);
mpq.close();
return true;
}
return mpq;
}
const errno = StormLib.GetLastError();
throw new Error(`archive could not be found or opened (${errno})`);
}
static create(path, callback) {
const flags = 0;
const maxFileCount = 0;
const handlePtr = ref.alloc(HANDLEPtr);
if (StormLib.SFileCreateArchive(path, flags, maxFileCount, handlePtr)) {
return this.open(path, callback);
}
const errno = StormLib.GetLastError();
throw new Error(`archive could not be created (${errno})`);
}
}
export default MPQ;