Actions

Jagex Store: Difference between revisions

From RuneWiki

(Created page with "This format is functionally the same as File Store, but uses more indices to separate file types. You can get the number of indices to read from by taking the length of idx255...")
 
No edit summary
Line 1: Line 1:
This format is functionally the same as File Store, but uses more indices to separate file types. You can get the number of indices to read from by taking the length of idx255 and dividing it by 6 (each idx255 entry is its own file). Reading works the same way (520 byte sectors) except there is now a 9-byte header before each entry.
This format is functionally the same as [[Old Engine/File Store|File Store]], but uses more indices to separate file types. You can get the number of indices to read from by taking the length of idx255 and dividing it by 6 (each idx255 entry is its own file). Reading works the same way (520 byte sectors) except there is now a 9-byte header before each entry.


The 9-byte header has the compression type (1 byte), file size (4 bytes), and uncompressed size (4 bytes). A compression type of 1 uses BZip2 and you have to prepend the magic number. A compression type of 2 uses gzip.
The 9-byte header has the compression type (1 byte), file size (4 bytes), and uncompressed size (4 bytes). A compression type of 1 uses BZip2 and you have to prepend the magic number. A compression type of 2 uses gzip.


=== Reading ===
===Reading===
<syntaxhighlight lang="js">
<syntaxhighlight lang="js">
const BZIP2_HEADER = new Uint8Array([0x42, 0x5A, 0x68, 0x31]);
const BZIP2_HEADER = new Uint8Array([0x42, 0x5A, 0x68, 0x31]);

Revision as of 21:37, 23 January 2022

This format is functionally the same as File Store, but uses more indices to separate file types. You can get the number of indices to read from by taking the length of idx255 and dividing it by 6 (each idx255 entry is its own file). Reading works the same way (520 byte sectors) except there is now a 9-byte header before each entry.

The 9-byte header has the compression type (1 byte), file size (4 bytes), and uncompressed size (4 bytes). A compression type of 1 uses BZip2 and you have to prepend the magic number. A compression type of 2 uses gzip.

Reading

const BZIP2_HEADER = new Uint8Array([0x42, 0x5A, 0x68, 0x31]);

export class FileStore {
    constructor(path) {
        this.dat2 = new RandomAccessFile(`${path}/main_file_cache.dat2`, false);
        this.idx255 = new RandomAccessFile(`${path}/main_file_cache.idx255`, false);

        let idxCount = this.idx255.length / 6;
        for (let i = 0; i < idxCount; ++i) {
            this[`idx${i}`] = new RandomAccessFile(`${path}/main_file_cache.idx${i}`, false);
        }
    }

    read(index, entry, extract = false) {
        let buffer = this[`idx${index}`].front().seek(entry * 6).read(6);
        if (!buffer.available) {
            return null;
        }

        let length = buffer.readSWord();
        if (length > this.dat2.length) {
            return null;
        }

        let data = new ByteBuffer(new Uint8Array(length), false);
        let nextSector = buffer.readSWord();

        if (nextSector * 520 > this.dat2.length) {
            return null;
        }

        let part = 0;
        while (data.available && nextSector !== 0) {
            let sector = this.dat2.front().seek(nextSector * 520).read(520);

            let bytes = data.available;
            if (bytes > 512) {
                bytes = 512;
            }

            let currentFile = sector.readWord();
            if (currentFile !== entry) {
                console.error('currentFile !== entry');
                return null;
            }

            let currentPart = sector.readWord();
            if (currentPart !== part) {
                console.error('currentPart !== part');
                return null;
            }

            nextSector = sector.readSWord();
            if (nextSector * 520 > this.dat2.length) {
                console.error('nextSector * 520 > this.dat2.length');
                return null;
            }

            let currentIndex = sector.readByte();
            if (currentIndex !== index) {
                console.error('currentIndex !== index');
                return null;
            }

            data.write(sector.read(bytes));
            part++;
        }
        data.front();

        let file = {
            compression: data.readByte(),
            size: data.readDWord(),
            decompressedSize: data.readDWord(),
            data: data.read()
        };

        if (extract) {
            if (file.compression === 1) {
                file.data = file.data.prepend(BZIP2_HEADER);
                file.data = bz2.decompress(file.data.raw);
            } else if (file.compression === 2) {
                file.data = new Uint8Array(zlib.gunzipSync(file.data.raw));
            }
        }

        return file;
    }
}