-
Notifications
You must be signed in to change notification settings - Fork 16
/
files.js
72 lines (59 loc) · 1.52 KB
/
files.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
import ref from 'ref-napi';
import File from './file';
import { default as StormLib, FIND_DATA, HANDLEPtr } from './storm-lib';
class Files {
static FROM_MPQ = 0x00000000;
static FROM_LOCAL = 0xFFFFFFFF;
constructor(mpq) {
this.mpq = mpq;
}
get handle() {
return this.mpq.handle;
}
contains(file) {
return !!this.handle && StormLib.SFileHasFile(this.handle, file);
}
get(file) {
if (this.handle) {
const fileHandlePtr = ref.alloc(HANDLEPtr);
if (StormLib.SFileOpenFileEx(this.handle, file, this.constructor.FROM_MPQ, fileHandlePtr)) {
return new File(fileHandlePtr.deref());
}
}
return null;
}
extract(file, target) {
if (!StormLib.SFileExtractFile(this.handle, file, target, this.constructor.FROM_MPQ)) {
const errno = StormLib.GetLastError();
throw new Error(`file could not be extracted (${errno})`);
}
return true;
}
get all() {
return this.find('*');
}
find(pattern) {
let handle = null;
const next = () => {
const data = new FIND_DATA();
if (!handle) {
handle = StormLib.SFileFindFirstFile(this.handle, pattern, data.ref(), null);
if (!handle.isNull()) {
return data;
}
} else {
if (StormLib.SFileFindNextFile(handle, data.ref())) {
return data;
}
}
};
const results = [];
let data;
while (data = next()) {
results.push(data);
}
StormLib.SFileFindClose(handle);
return results;
}
}
export default Files;