@runejs/filestore
Version:
Tools for managing the RuneJS filestore.
100 lines (99 loc) • 3.08 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.JingleStore = exports.OggFile = void 0;
const common_1 = require("@runejs/common");
const node_fs_1 = require("node:fs");
/**
* A single OGG file object.
*/
class OggFile {
fileData;
constructor(fileData) {
this.fileData = fileData;
}
/**
* Writes this unpacked OGG file to the disk under `./unpacked/ogg/{oggId}.ogg`
*/
async writeToDisk() {
return new Promise((resolve, reject) => {
try {
if (!(0, node_fs_1.existsSync)('./unpacked/ogg')) {
(0, node_fs_1.mkdirSync)('./unpacked/ogg');
}
const data = this.fileData.decompress();
(0, node_fs_1.writeFileSync)(`./unpacked/ogg/${this.fileId}.ogg`, Buffer.from(data));
resolve();
}
catch (error) {
reject(error);
}
});
}
get fileId() {
return this.fileData?.fileId || -1;
}
}
exports.OggFile = OggFile;
/**
* Controls short jingle (.ogg) file storage.
*/
class JingleStore {
fileStore;
constructor(fileStore) {
this.fileStore = fileStore;
}
/**
* Writes all unpacked OGG files to the disk under `./unpacked/ogg/`
*/
async writeToDisk() {
const files = this.decodeJingleStore();
for (const ogg of files) {
try {
await ogg.writeToDisk();
}
catch (e) {
common_1.logger.error(e);
}
}
}
/**
* Decodes the specified OGG file.
* @param id The ID of the OGG file.
* @returns The decoded OggFile object, or null if the file is not found.
*/
getOgg(id) {
if (id === undefined || id === null) {
return null;
}
const oggArchiveIndex = this.fileStore.getIndex('jingles');
const fileData = oggArchiveIndex.getFile(id);
return fileData ? new OggFile(fileData) : null;
}
/**
* Decodes all OGG files within the filestore.
* @returns The list of decoded OggFile objects from the OGG store.
*/
decodeJingleStore() {
const oggArchiveIndex = this.fileStore.getIndex('jingles');
const fileCount = oggArchiveIndex.files.size;
const oggFiles = new Array(fileCount);
for (let oggId = 0; oggId < fileCount; oggId++) {
try {
const fileData = oggArchiveIndex.getFile(oggId);
if (!fileData) {
oggFiles[oggId] = null;
common_1.logger.warn(`No file found for OGG ID ${oggId}.`);
continue;
}
oggFiles[oggId] = new OggFile(fileData);
}
catch (e) {
oggFiles[oggId] = null;
common_1.logger.error(`Error parsing OGG ID ${oggId}.`);
common_1.logger.error(e);
}
}
return oggFiles;
}
}
exports.JingleStore = JingleStore;