Actions

Jagex Store: Difference between revisions

From RuneWiki

No edit summary
No edit summary
 
Line 3: Line 3:
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.


=== File Types ===
===File Types===
dat = data
dat = data


dat0 = sd client in pack200 format
dat0 = sd client in pack200 format (minus gzip header `1f 8b`)


dat1 = sd client in packclass format
dat1 = sd client in packclass format

Latest revision as of 22:27, 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 included as part of 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.

File Types

dat = data

dat0 = sd client in pack200 format (minus gzip header `1f 8b`)

dat1 = sd client in packclass format

dat2 = data

dat3 = hd client in pack200 format

dat4 = hd client in packclass format

dat5 = jogl/jaggl library in pack200 format

dat6 = jogl/jaggl library in packclass format

idx* = file index

Reading

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

// Backwards compatible with 234-402 caches (legacy)
export class JagexStore {
    constructor(path) {
        this.legacy = false;

        if (fs.existsSync(`${path}/main_file_cache.dat`)) {
            this.legacy = true;
        }

        if (!this.legacy) {
            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);
            }

            this.index_offset = 0;
        } else {
            this.dat2 = new RandomAccessFile(`${path}/main_file_cache.dat`, false);

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

            this.index_offset = 1;
        }
    }

    read(index, entry, extract = false) {
        if (!this[`idx${index}`]) {
            return null;
        }

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

        let length = buffer.readSWord();
        if (length === 0 || 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) {
                return null;
            }

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

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

            let currentIndex = sector.readByte();
            if (currentIndex - this.index_offset !== index) {
                return null;
            }

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

        let file = { compression: 0, size: 0, decompressedSize: 0, data };

        if (!this.legacy) {
            file.compression = data.readByte(),
            file.size = data.readDWord(),
            file.decompressedSize = data.readDWord(),
            file.data = data.read()
        } else {
            file.compression = index === 0 ? 1 : 2;
            file.size = file.data.length;
        }

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

        return file;
    }
}