furnace-module-interface
Version:
An interface for loading, processing and saving furnace modules in an object-oriented manner. Fully typed via TypeScript.
116 lines (115 loc) • 4.07 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.FurnaceRow = exports.FurnacePattern = void 0;
const defaults_1 = require("./defaults");
class FurnacePattern {
constructor(rows, effects) {
this.name = defaults_1.defaults.patternName;
this.rows = [];
for (let i = 0; i < rows; i++) {
this.rows.push(new FurnaceRow(effects));
}
}
static create(rows, effects) {
return new FurnacePattern(rows, effects);
}
static fromHelper(helper, version) {
const pat = FurnacePattern.create(0, 0);
if (FurnacePattern.magic !== helper.getASCII(4)) {
throw new Error("Expected format magic string \"" + FurnacePattern.magic + "\" when loading a pattern block.");
}
helper.skipBytes(4);
const channel = helper.readUInt16LE();
const index = helper.readUInt16LE();
const song = helper.readUInt16LE();
helper.skipBytes(2);
return { pattern: pat, channel, index, song: version < 95 ? 0 : song, };
}
loadData(helper, version, rows, effects) {
for (let i = 0; i < rows; i++) {
const row = new FurnaceRow(effects);
this.rows.push(row);
row.fromHelper(helper);
}
if (version >= 51) {
this.name = helper.readString("utf-8");
}
}
writeToBuffer(helper, version, channel, index, song) {
const _begin = helper.getPosition(8);
helper.writeString(FurnacePattern.magic, "ascii", false);
helper.skipBytes(4);
helper.writeUInt16LE(channel);
helper.writeUInt16LE(index);
helper.writeUInt16LE(song);
helper.skipBytes(2);
for (const row of this.rows) {
row.writeToBuffer(helper);
}
if (version >= 51) {
helper.writeString(this.name, "utf-8", true);
}
helper.writeUInt32LE(helper.getPosition() - _begin, _begin - 4);
}
}
exports.FurnacePattern = FurnacePattern;
FurnacePattern.magic = "PATR";
class FurnaceRow {
constructor(effects) {
this.note = null;
this.instrument = null;
this.volume = null;
this.effects = [];
for (let i = 0; i < effects; i++) {
this.effects.push({ id: null, value: null, });
}
}
fromHelper(helper) {
const note = helper.readInt16LE();
const octave = helper.readInt8();
helper.skipBytes(1);
if (note === 0) {
this.note = null;
}
else {
this.note = { note, octave, };
}
this.instrument = helper.readUInt16LE();
this.volume = helper.readUInt16LE();
if (this.instrument === 0xFFFF) {
this.instrument = null;
}
if (this.volume === 0xFFFF) {
this.volume = null;
}
for (const e of this.effects) {
e.id = helper.readUInt16LE();
e.value = helper.readUInt16LE();
if (e.id === 0xFFFF) {
e.id = null;
}
if (e.value === 0xFFFF) {
e.value = null;
}
}
}
writeToBuffer(helper) {
var _a, _b, _c, _d;
if (this.note) {
helper.writeInt16LE(this.note.note);
helper.writeInt8(this.note.octave);
helper.skipBytes(1);
}
else {
helper.writeUInt16LE(0);
helper.writeUInt16LE(0);
}
helper.writeUInt16LE((_a = this.instrument) !== null && _a !== void 0 ? _a : 0xFFFF);
helper.writeUInt16LE((_b = this.volume) !== null && _b !== void 0 ? _b : 0xFFFF);
for (const e of this.effects) {
helper.writeUInt16LE((_c = e.id) !== null && _c !== void 0 ? _c : 0xFFFF);
helper.writeUInt16LE((_d = e.value) !== null && _d !== void 0 ? _d : 0xFFFF);
}
}
}
exports.FurnaceRow = FurnaceRow;