@runejs/filestore
Version:
Tools for managing the RuneJS filestore.
92 lines (91 loc) • 3.09 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.VarbitStore = exports.VarbitConfig = void 0;
const common_1 = require("@runejs/common");
/**
* Contains game client need-to-know level information about a single varbit.
*/
class VarbitConfig {
gameId;
index;
leastSignificantBit;
mostSignificantBit;
}
exports.VarbitConfig = VarbitConfig;
/**
* Controls files within the Varbit Archive of the configuration index.
*/
class VarbitStore {
configStore;
/**
* The Varbit Archive, containing details about every game varbit.
*/
varbitArchive;
constructor(configStore) {
this.configStore = configStore;
this.varbitArchive = this.configStore.getArchive('varbits');
}
/**
* Fetches the VarbitConfig object for the specified varbit game id.
* @param varbitId The game id of the varbit to find.
*/
getVarbit(varbitId) {
const varbitArchive = this.varbitArchive;
if (!varbitArchive) {
common_1.logger.error('Varbit archive not found.');
return null;
}
const varbitFile = varbitArchive.getFile(varbitId) || null;
if (!varbitFile) {
common_1.logger.error('Varbit file not found.');
return null;
}
return this.decodeVarbitFile(varbitFile);
}
/**
* Parses a raw varbit data file into a readable VarbitConfig object.
* @param varbitFile The raw file-store varbit data.
*/
decodeVarbitFile(varbitFile) {
const varbitConfig = new VarbitConfig();
const buffer = varbitFile.content;
varbitConfig.gameId = varbitFile.fileId;
let run = true;
while (run) {
const opcode = buffer.get('BYTE', 'UNSIGNED');
if (opcode === 0) {
run = false;
break;
}
if (opcode === 1) {
varbitConfig.index = buffer.get('SHORT', 'UNSIGNED');
varbitConfig.leastSignificantBit = buffer.get('BYTE', 'UNSIGNED');
varbitConfig.mostSignificantBit = buffer.get('BYTE', 'UNSIGNED');
}
}
varbitFile.content.readerIndex = 0;
return varbitConfig;
}
/**
* Decodes every varbit file within the varbit archive and returns
* the resulting VarbitConfig array.
*/
decodeVarbitStore() {
if (!this.varbitArchive) {
common_1.logger.error('Varbit archive not found.');
return null;
}
const varbitCount = this.varbitArchive.files.size;
const varbitList = new Array(varbitCount);
for (let varbitId = 0; varbitId < varbitCount; varbitId++) {
const varbitFile = this.varbitArchive.getFile(varbitId) || null;
if (!varbitFile) {
common_1.logger.error('Varbit file not found.');
return null;
}
varbitList[varbitId] = this.decodeVarbitFile(varbitFile);
}
return varbitList;
}
}
exports.VarbitStore = VarbitStore;