💡 Improve comments and code quality

This commit is contained in:
C-3PO 2018-06-23 23:40:22 +02:00
parent b87236a435
commit 57d45b942b
Signed by: c3po
GPG key ID: 62993C4BB4D86F24
2 changed files with 13 additions and 9 deletions

View file

@ -7,29 +7,33 @@ import ByteReader from './extractFileByteReader';
* Will throw an error when end of final DataView is reached. * Will throw an error when end of final DataView is reached.
*/ */
export default async function extractFile(file: ISsnFileEntry, dvArray: DataView[]): Promise<ArrayBuffer> { export default async function extractFile(file: ISsnFileEntry, dvArray: DataView[]): Promise<ArrayBuffer> {
//use ByteReader for reading a uint8 and seeking forward across DataView boundaries //Use ByteReader for reading a uint8 and seeking forward across DataView boundaries
const byteReader = new ByteReader(dvArray, file.diskNumberStart, file.offset); const byteReader = new ByteReader(dvArray, file.diskNumberStart, file.offset);
//Local file header signature must be 0x04034B50
if (byteReader.readByte() !== 0x50 || byteReader.readByte() !== 0x4B || byteReader.readByte() !== 0x03 || byteReader.readByte() !== 0x04) { if (byteReader.readByte() !== 0x50 || byteReader.readByte() !== 0x4B || byteReader.readByte() !== 0x03 || byteReader.readByte() !== 0x04) {
throw new Error('Local file header had wrong magic'); throw new Error('Local file header had wrong magic');
} }
//All fields in the local file header are copies of the central file header, so we can skip them.
//FIXME: Maybe we should actually read these fields to verify that they are identical?
byteReader.seek(22); byteReader.seek(22);
const localFilenameSize = byteReader.readByte() + (byteReader.readByte() << 8); const localFilenameSize = byteReader.readByte() + (byteReader.readByte() << 8);
const localExtraSize = byteReader.readByte() + (byteReader.readByte() << 8); const localExtraSize = byteReader.readByte() + (byteReader.readByte() << 8);
byteReader.seek(localFilenameSize + localExtraSize); byteReader.seek(localFilenameSize + localExtraSize);
//Extract actual file contents
let dvFinal = byteReader.extractDv(file.comprSize); let dvFinal = byteReader.extractDv(file.comprSize);
//decrypt file if necessary //Decrypt file if necessary
if (file.decryptionKeys !== undefined) { if (file.decryptionKeys !== undefined) {
dvFinal = decryptFile(dvFinal, file.comprSize, file.decryptionKeys); dvFinal = decryptFile(dvFinal, file.comprSize, file.decryptionKeys);
} }
//uncompress file //Uncompress file
const uncompressedBuffer: Buffer = await new Promise((resolve, reject) => { const uncompressedBuffer: Buffer = await new Promise((resolve, reject) => {
zlib.inflateRaw(dvFinal, (error, result) => { zlib.inflateRaw(dvFinal, (error, result) => {
if (error !== null) { if (error !== null) {
reject(error); return reject(error);
} }
resolve(result); resolve(result);
}); });

View file

@ -7,13 +7,14 @@ import readSsnFile from './readSsnFile';
export default async function getPatch(product: Product, from: number, to: number) { export default async function getPatch(product: Product, from: number, to: number) {
const solidPkg = await getSolidpkg(product, from, to); const solidPkg = await getSolidpkg(product, from, to);
console.log(solidPkg.files); console.debug(solidPkg.files);
const bufferArray = await Promise.all(solidPkg.files.map((file) => getUrlContents({ host: 'cdn-patch.swtor.com', path: `/patch/${product}/${product}_${from}to${to}/${file.name}` }))); const bufferArray = await Promise.all(solidPkg.files.map((file) => getUrlContents({ host: 'cdn-patch.swtor.com', path: `/patch/${product}/${product}_${from}to${to}/${file.name}` })));
const zipFile = bufferArray[bufferArray.length - 1]; const zipFile = bufferArray[bufferArray.length - 1];
const dvArray = bufferArray.map((buffer) => new DataView(buffer)); const dvArray = bufferArray.map((buffer) => new DataView(buffer));
const fileEntries = readSsnFile(zipFile); const fileEntries = readSsnFile(zipFile);
console.debug(fileEntries);
//Verify file entries //Verify file entries
if (from === -1) { if (from === -1) {
@ -22,19 +23,18 @@ export default async function getPatch(product: Product, from: number, to: numbe
}); });
} }
//TODO: last file must always be `${product}.version` with diff type. Other files depend on diffType. //TODO: last file must always be `${product}.version` with diff type. Other files depend on diffType.
console.log(fileEntries);
//Extract newly added files //Extract newly added files
fileEntries.filter((file) => file.diffType === SsnDiffType.NewFile).forEach(async (file) => { fileEntries.filter((file) => file.diffType === SsnDiffType.NewFile).forEach(async (file) => {
const fileContents = await extractFile(file, dvArray); const fileContents = await extractFile(file, dvArray);
console.log(fileContents); console.debug(fileContents);
//TODO //TODO
}); });
//Extract changed files //Extract changed files
fileEntries.filter((file) => file.diffType === SsnDiffType.Changed).forEach(async (file) => { fileEntries.filter((file) => file.diffType === SsnDiffType.Changed).forEach(async (file) => {
const fileContents = await extractFile(file, dvArray); const fileContents = await extractFile(file, dvArray);
console.log(fileContents); console.debug(fileContents);
//TODO //TODO
}); });