Encapsulate alignment into a function to make things slightly easier

This just returns the extra amount needed to add onto the string/array.

Also fixed up writeSLArray() not adding slack like it should have been. This wasn't an issue previously because the only place that currently uses this function is the password feature which is a fixed 16byte length, so it was already aligned. An upcoming commit will be using this, however, so I needed the alignment to be correct.

Finally, the read/write string functions were doing unnecessary conditional checks so I removed them. skipWrite() already does nothing if you tell it to skip 0, and adding 0 to readOffset won't do anything, so neither conditional is necessary at this point.
This commit is contained in:
2020-02-09 14:28:03 -06:00
parent 22858061f7
commit 22357f11e2

View File

@ -27,17 +27,13 @@ exports.SLMessage = class SLMessage extends SmartBuffer {
writeSLString(str) {
this.writeInt32LE(str.length);
this.writeString(str);
if (str.length % 4 !== 0) {
this.skipWrite(4 - str.length % 4);
}
this.skipWrite(SLMessage.slackForAlignment(str.length));
}
readSLString() {
var len = this.readInt32LE();
var str = this.readString(len);
if (len % 4 !== 0) {
this.readOffset += 4 - len % 4;
}
this.readOffset += SLMessage.slackForAlignment(len);
return str;
}
@ -48,9 +44,12 @@ exports.SLMessage = class SLMessage extends SmartBuffer {
writeSLArray(arr) {
this.writeInt32LE(arr.length);
for (var i = 0; i < arr.length; i++) {
this.writeUInt8(arr[i]);
}
this.skipWrite(SLMessage.slackForAlignment(arr.length));
}
skipWrite(num) {
@ -66,5 +65,9 @@ exports.SLMessage = class SLMessage extends SmartBuffer {
this.dataLength = this.readInt32LE();
}
static slackForAlignment(val) {
return (4 - val % 4) % 4;
}
encode() {}
};