dolphin-tool
Version:
🐬 dolphin-tool binaries and wrapper for Node.js.
125 lines • 6.22 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __asyncValues = (this && this.__asyncValues) || function (o) {
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
var m = o[Symbol.asyncIterator], i;
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
};
import fs from 'node:fs';
import util from 'node:util';
import DolphinToolBin from './dolphinToolBin.js';
import { CompressionMethodGcz, CompressionMethodWiaRvz } from './common.js';
/**
* Parses information found in file headers.
*/
export default {
/**
* Have `dolphin-tool` parse the file header.
*/
header(options_1) {
return __awaiter(this, arguments, void 0, function* (options, attempt = 1) {
const output = yield DolphinToolBin.run([
'header',
'-i', options.inputFilename,
'-j',
'-b',
'-c',
'-l',
], options);
// Try to detect failures, and then retry them automatically
if (!output.trim() && attempt <= 3) {
yield new Promise((resolve) => {
setTimeout(resolve, Math.random() * (Math.pow(2, (attempt - 1)) * 20));
});
return this.header(options, attempt + 1);
}
const object = JSON.parse(output);
let compressionMethod = CompressionMethodWiaRvz.NONE;
if (typeof object.compression_method === 'string') {
if (object.compression_method.toLowerCase() === 'none') {
compressionMethod = CompressionMethodWiaRvz.NONE;
}
else if (object.compression_method.toLowerCase() === 'zstd'
|| object.compression_method.toLowerCase() === 'zstandard') {
compressionMethod = CompressionMethodWiaRvz.ZSTD;
}
else if (object.compression_method.toLowerCase() === 'deflate') {
compressionMethod = CompressionMethodGcz.DEFLATE;
}
else if (object.compression_method.toLowerCase() === 'bzip2') {
compressionMethod = CompressionMethodWiaRvz.BZIP2;
}
else if (object.compression_method.toLowerCase() === 'lzma') {
compressionMethod = CompressionMethodWiaRvz.LZMA;
}
else if (object.compression_method.toLowerCase() === 'lzma2') {
compressionMethod = CompressionMethodWiaRvz.LZMA2;
}
}
return {
blockSize: object.block_size,
compressionLevel: object.compression_level,
compressionMethod,
country: object.country,
gameId: object.game_id,
internalName: object.internal_name,
region: object.region,
revision: object.revision,
};
});
},
/**
* Read the file header
*/
uncompressedSize(inputFilename) {
return __awaiter(this, void 0, void 0, function* () {
var _a, e_1, _b, _c;
// WIA, RVZ?
const chunks = [];
try {
for (var _d = true, _e = __asyncValues(fs.createReadStream(inputFilename, { start: 0, end: 0x24 + 8 })), _f; _f = yield _e.next(), _a = _f.done, !_a; _d = true) {
_c = _f.value;
_d = false;
const chunk = _c;
chunks.push(chunk);
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (!_d && !_a && (_b = _e.return)) yield _b.call(_e);
}
finally { if (e_1) throw e_1.error; }
}
const contents = Buffer.concat(chunks);
if (contents.subarray(0, 4).equals(Buffer.from('01C00BB1', 'hex'))) {
// GCZ
// @see https://github.com/dolphin-emu/dolphin/blob/1f5e100a0e6dd4f9ab3784fd6373d452054d08bf/Source/Core/DiscIO/CompressedBlob.h#L38
return contents.subarray(0x10, 0x10 + 8).readBigUInt64LE();
}
if (contents.subarray(0, 4).equals(Buffer.from('RVZ\u0001'))) {
// RVZ
// @see https://github.com/dolphin-emu/dolphin/blob/f93781d91a90a937973534298b67b789f6a0db0a/docs/WiaAndRvz.md#wia_file_head_t
return contents.subarray(0x24, 0x24 + 8).readBigUInt64BE();
}
if (contents.subarray(0, 4).equals(Buffer.from('WIA\u0001'))) {
// WIA
// @see https://github.com/dolphin-emu/dolphin/blob/f93781d91a90a937973534298b67b789f6a0db0a/docs/WiaAndRvz.md#wia_file_head_t
return contents.subarray(0x24, 0x24 + 8).readBigUInt64BE();
}
// ISO
const stat = yield util.promisify(fs.stat)(inputFilename);
return BigInt(stat.size);
});
},
};
//# sourceMappingURL=dolphinToolHeader.js.map