Skip to content

Commit

Permalink
feat(mpq): mpq reader that can extract files from patch_d2.mpq
Browse files Browse the repository at this point in the history
  • Loading branch information
blacha committed Sep 19, 2020
1 parent 20507e8 commit 433b05d
Show file tree
Hide file tree
Showing 22 changed files with 1,798 additions and 8 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ tsconfig.tsbuildinfo
dist
.cache
cdk.context.json
test-data
test-data
*.ndjson
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ Tools to work with diablo2
- [bintools](./packages/bintools) - Binary parsers to read the `.bin` files
- [huffman](./packages/huffman) - Decompressor for network data
- [packets](./packages/packets) - Diablo2 network protocol
- [map](./packages/map) - Diablo2 map generation api (Docker)
- [mpq](./packages/mpq) - MPQ reader / extractor
- [map](./packages/map) - Diablo2 map generation api (Docker based)
- [sniffer](./packages/sniffer) - Diablo2 network sniffer
2 changes: 1 addition & 1 deletion packages/bintools/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
},
"dependencies": {
"@diablo2/data": "^0.1.0",
"binparse": "^1.0.0",
"binparse": "^1.0.1",
"ulid": "^2.3.0"
},
"publishConfig": {
Expand Down
21 changes: 21 additions & 0 deletions packages/mpq/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
## @diablo2/mpq

MPQ Reader for diablo2

Supports decoding PKWARE compression


```typescript

import {Mpq} from '@diablo2/mpq'

// Read from file
const mpq = Mpq.load('patch_d2.mpq');
const lang = await mpq.extract('data\\local\\LNG\\ENG\\patchstring.tbl');


// Read from buffer
const mpqBuf = await fs.promises.read('patch_d2.mpq');
const mpq = Mpq.load(mpqBuf);
const lang = await mpq.extract('data\\local\\LNG\\ENG\\patchstring.tbl');
```
40 changes: 40 additions & 0 deletions packages/mpq/example/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { Mpq } from '../src';
import { promises as fs } from 'fs';
import { createHash } from 'crypto';
import { toHex } from 'binparse';

const MpqFileName = './patch_d2.mpq';
async function main(): Promise<void> {
// const bytes = await decompressSector(sector, 0, sector.length);
// console.log(bytes);

console.time('ReadMpq');
const fileData = await fs.readFile(MpqFileName);
const mpqFileHash = createHash('sha256').update(fileData).digest('base64');
const mpq = Mpq.load(fileData);
console.timeEnd('ReadMpq');

console.log('LoadedMPQ', MpqFileName, { hash: mpqFileHash });

const files: string[] = ['patchstring.tbl', 'expansionstring.tbl', 'string.tbl'];
for (const fileName of files) {
console.time(`extracting:${fileName}`);
const dat = await mpq.extract(`data\\local\\LNG\\ENG\\${fileName}`);
console.timeEnd(`extracting:${fileName}`);
if (dat == null) continue;
const hash = createHash('sha1').update(dat).digest('hex');

console.log('Extracted', { fileName, hash, size: dat.length });

const readFile = readFileSync(`/home/blacha/workspace/tmp/mpq-ex/ext/data/local/LNG/ENG/${fileName}`);
let ok = true;
for (let i = 0; i < readFile.length; i++) {
if (readFile[i] == dat[i]) continue;
console.log('MissMatch', i, { expected: toHex(readFile[i]), got: toHex(dat[i]) });
ok = false;
break;
}
console.log({ ok });
}
}
main();
25 changes: 25 additions & 0 deletions packages/mpq/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "@diablo2/mpq",
"version": "0.1.0",
"repository": {
"type": "git",
"url": "https://github.com/blacha/diablo2.git",
"directory": "packages/mpq"
},
"license": "MIT",
"main": "./build/index.js",
"types": "./build/index.d.ts",
"scripts": {
"test": "ospec --globs 'build/**/*.test.js'"
},
"dependencies": {
"binparse": "^1.0.1",
"implode-decoder": "^1.0.1"
},
"publishConfig": {
"access": "public"
},
"files": [
"build/"
]
}
18 changes: 18 additions & 0 deletions packages/mpq/src/__test__/mpq.encryption.data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export const MpqDecryptionTests = [
{
name: 'tableA',
input:
'W2NIPTTaCMqNVTb447FH6X8dP7akh8VkNgR/4uZkCz0K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Y+I9bsTvVVwY7nm7mffyUKd4NMyZqtzJ/m5BhNG/KAI5gX/5r3I0A9nbHvMObRPOGDKE7So/ZsROHeCkOMkLpb0RJE+JI428h+DRDnmtaQ9hbAXR+DXuF+l2l3IUaJ88zo2PLY763AUaWeKV0MJoeLmjY+LwNfGlOBWcW0euO8F30jZ2PeXVFhZGT2svOFFMSF17M0rKQslWKQLGpZKCrDRzt5nSBb9HUcqinHLXV1eM1rgWjd8OFAZgzfLQkpLDGvfnkmukQhakc7Llfsswobbr6Ns8N+3hrAqzV2Fjcty+ZTWAWdqmYLM=',
output:
'rsUJ1lnRqMoAAAAAAQAAAGTPaUyL40FgAAAAAAQAAAAJQvjByg1YQQAAAAAOAAAAS6XCqpUrdvQAAAAABgAAAP////////////////////9wt+XJtvYYOwAAAAAJAAAAewg8NII2jicAAAAACAAAAKAeKztX8C63AAAAAAwAAAD//////////////////////////////////////////5zCg9OSLkDvAAAAAAAAAAD/////////////////////C8Eu6YwgFEIAAAAAAgAAAP/////////////////////dmUR2sGEbawAAAAAFAAAAyzeE0+zq3wcAAAAAEAAAAOHXdC4QHieLAAAAAAoAAAD//////////////////////////////////////////57LGf5dcEBiAAAAAAsAAAD/////////////////////3It+Wlw/Jf8AAAAAAwAAAP///////////////////////////////////////////////////////////////xB5Zf2nmJtOAAAAAA8AAAD/////////////////////////////////////////////////////////////////////////////////////z7CoHSj/zqIAAAAADQAAAIkilTGj+l9qAAAAAAcAAAA=',
key: 3283040112,
},
];
Loading

0 comments on commit 433b05d

Please sign in to comment.