Skip to content

Commit

Permalink
Refact and fix some bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
EmanuelJr committed Mar 21, 2019
1 parent d226f31 commit 464db2d
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 77 deletions.
37 changes: 0 additions & 37 deletions FileReader.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,43 +126,6 @@ class FileReader {
return out;
}

getPos() {
const byteBuff = new ArrayBuffer(4);
const wba = new Int8Array(byteBuff);
const wia = new Int32Array(byteBuff);

wba[2] = this.getUInt8();
wba[1] = this.getUInt8();
wba[0] = this.getUInt8();
wba[3] = 0;

let p = 0 + wia[0];
const dir = p & 0x0f;
p >>= 4;

const y = p & 0x03FF;
p >>= 10;

const x = p & 0x03FF;

return [x, y, dir];
}

getPos2() {
const a = this.getInt8();
const b = this.getInt8();
const c = this.getInt8();
const d = this.getInt8();
const e = this.getInt8();

return [
((a & 0xFF) << 2) | ((b & 0xC0) >> 6), // x1
((b & 0x3F) << 4) | ((c & 0xF0) >> 4), // y1
((d & 0xFC) >> 2) | ((c & 0x0F) << 6), // x2
((d & 0x03) << 8) | ((e & 0xFF)), // y2
];
}

getBuffer(start, end, setPos = true) {
const length = end - start;
const buffer = new Buffer.alloc(length);
Expand Down
70 changes: 30 additions & 40 deletions GRF.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
const zlib = require('zlib');
const FileReader = require('./FileReader');
const DES = require('./DES');
const utils = require('./utils');

class GRF {
constructor(file) {
this.fr = new FileReader(file);

const header = {
signature: this.fr.getMany('UInt8', 15),
key: this.fr.getMany('UInt8', 15),
Expand All @@ -14,37 +16,37 @@ class GRF {
version: this.fr.getUInt32(),
};

header.signature = String.fromCharCode.apply(null, header.signature);
header.signature = String.fromCharCode(...header.signature);
header.filecount -= header.skip + 7;

if (header.signature !== 'Master of Magic') {
const error = `Incorrect header signature: "${header.signature}", must be "Master of Magic"`;
throw new Error(error);
const error = `Incorrect header signature: "${header.signature}", should be "Master of Magic"`;
throw error;
}

if (parseInt(header.version, 10) !== 0x200) {
const error = `Incorrect header version "0x${parseInt(header.version, 10).toString(16)}", must be "0x200"`;
throw new Error(error);
const error = `Incorrect header version "0x${parseInt(header.version, 10).toString(16)}", should be "0x200"`;
throw error;
}

if (header.file_table_offset + 46 > file.size || header.file_table_offset < 0) {
const error = `Can't jump to table list (${header.file_table_offset}), file length: ${file.size}`;
throw new Error(error);
const error = `Can not jump to table list (${header.file_table_offset}), file length: ${file.size}`;
throw error;
}

const tableBuffer = this.fr.getBuffer(header.file_table_offset + 46, header.file_table_offset + 46 + 8);
const table = {
pack_size: this.fr.getUInt32(),
real_size: this.fr.getUInt32(),
pack_size: tableBuffer.readUInt32LE(0),
real_size: tableBuffer.readUInt32LE(4),
};

const buffer = this.fr.getBuffer(header.file_table_offset + 46 + 8, table.pack_size);
const data = new Uint8Array(buffer);
const out = zlib.inflateSync(data);
const buffer = this.fr.getBuffer(header.file_table_offset + 46 + 8, header.file_table_offset + 46 + 8 + table.pack_size);
const out = zlib.inflateSync(buffer);

const entries = this.loadEntries(out, header.filecount);

table.data = '';
for (let i = 0, count = entries.length; i < count; i += 1) {
for (let i = 0; i < entries.length; i += 1) {
table.data += entries[i].filename + '\0';
entries[i].filename = entries[i].filename.toLowerCase();
}
Expand Down Expand Up @@ -89,7 +91,7 @@ class GRF {
return entries;
}

decodeEntry(buffer, entry, callback) {
async decodeEntry(buffer, entry) {
const data = new Uint8Array(buffer);

if (entry.type & GRF.FILELIST_TYPE_ENCRYPT_MIXED) {
Expand All @@ -98,7 +100,7 @@ class GRF {
DES.decodeHeader(data, entry.length_aligned);
}

zlib.inflate(data, callback);
return utils.inflateAsync(data);
}

search(filename) {
Expand All @@ -118,39 +120,27 @@ class GRF {
return -1;
}

getFile(filename) {
async getFile(filename) {
const path = filename.toLowerCase();
const pos = this.search(path);

return new Promise((resolve, reject) => {
if (pos !== -1) {
const entry = this.entries[pos];
if (pos !== -1) {
const entry = this.entries[pos];

if (!(entry.type & GRF.FILELIST_TYPE_FILE)) {
reject('Probably it\'s a folder');
return;
}
if (!(entry.type & GRF.FILELIST_TYPE_FILE)) {
throw 'Probably it is a folder';
}

const buffer = this.fr.getBuffer(entry.offset + 46, entry.length_aligned + entry.offset + 46);
const buffer = this.fr.getBuffer(entry.offset + 46, entry.length_aligned + entry.offset + 46);

if (entry.real_size === entry.pack_size) {
resolve(buffer);
return;
}
if (entry.real_size === entry.pack_size) {
return buffer;
}

this.decodeEntry(buffer, entry, (error, buff) => {
if (error) {
reject(error);
return;
}
return this.decodeEntry(buffer, entry);
}

resolve(buff);
});
return;
}

reject('File doesn\'t exist');
});
throw 'File does not exist';
}
}

Expand Down
20 changes: 20 additions & 0 deletions utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const zlib = require('zlib');

const inflateAsync = (data) => {
return new Promise((resolve, reject) => {
const cb = (err, buff) => {
if (err) {
reject(err);
return;
}

resolve(buff);
};

zlib.inflate(data, cb);
});
};

module.exports = {
inflateAsync,
};

0 comments on commit 464db2d

Please sign in to comment.