UNPKG

@runejs/filestore

Version:

Tools for managing the RuneJS filestore.

100 lines (99 loc) 3.15 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SoundStore = exports.SoundFile = void 0; const common_1 = require("@runejs/common"); const node_fs_1 = require("node:fs"); /** * A single sound file object. */ class SoundFile { fileData; constructor(fileData) { this.fileData = fileData; } /** * Writes this unpacked sound file to the disk under `./unpacked/sounds/{soundId}.wav` */ async writeToDisk() { return new Promise((resolve, reject) => { try { if (!(0, node_fs_1.existsSync)('./unpacked/sounds')) { (0, node_fs_1.mkdirSync)('./unpacked/sounds'); } const data = this.fileData.decompress(); (0, node_fs_1.writeFileSync)(`./unpacked/sounds/${this.fileId}.wav`, Buffer.from(data)); resolve(); } catch (error) { reject(error); } }); } get fileId() { return this.fileData?.fileId || -1; } } exports.SoundFile = SoundFile; /** * Controls sound file storage. */ class SoundStore { fileStore; constructor(fileStore) { this.fileStore = fileStore; } /** * Writes all unpacked WAV files to the disk under `./unpacked/sounds/` */ async writeToDisk() { const files = this.decodeSoundStore(); for (const wav of files) { try { await wav.writeToDisk(); } catch (e) { common_1.logger.error(e); } } } /** * Decodes the specified sound file. * @param id The ID of the sound file. * @returns The decoded SoundFile object, or null if the file is not found. */ getSound(id) { if (id === undefined || id === null) { return null; } const soundArchiveIndex = this.fileStore.getIndex('sounds'); const fileData = soundArchiveIndex.getFile(id); return fileData ? new SoundFile(fileData) : null; } /** * Decodes all WAV files within the filestore. * @returns The list of decoded SoundFile objects from the sound store. */ decodeSoundStore() { const soundArchiveIndex = this.fileStore.getIndex('sounds'); const fileCount = soundArchiveIndex.files.size; const soundFiles = new Array(fileCount); for (let soundId = 0; soundId < fileCount; soundId++) { try { const fileData = soundArchiveIndex.getFile(soundId); if (!fileData) { soundFiles[soundId] = null; common_1.logger.warn(`No file found for sound ID ${soundId}.`); continue; } soundFiles[soundId] = new SoundFile(fileData); } catch (e) { soundFiles[soundId] = null; common_1.logger.error(`Error parsing sound ID ${soundId}.`); common_1.logger.error(e); } } return soundFiles; } } exports.SoundStore = SoundStore;