move bin<->bool conversion to UhkBuffer

This commit is contained in:
Sam Rang
2016-04-18 20:00:27 -05:00
parent 0c53b49c6a
commit 634aa6c532
5 changed files with 16 additions and 18 deletions

View File

@@ -1,6 +1,5 @@
abstract class Serializable<T> {
static boolFlag = 0x80;
private static depth = 0;
private static maxDisplayedJsonLength = 160;
private static enableDump = true;
@@ -46,15 +45,6 @@ abstract class Serializable<T> {
return value;
}
/* should have assert uint8? */
binToBool(bin: number) {
return (bin & Serializable.boolFlag) !== 0;
}
boolToBin(bool: boolean) {
return bool ? Serializable.boolFlag : 0;
}
abstract _fromJsObject(jsObject: any): Serializable<T>;
abstract _fromBinary(buffer: UhkBuffer): Serializable<T>;
abstract _toJsObject(): any;

View File

@@ -143,6 +143,14 @@ class UhkBuffer {
this.offset += stringByteLength;
}
readBoolean(): boolean {
return this.readUInt8() !== 0;
}
writeBoolean(bool: boolean) {
this.writeUInt8(bool ? 1 : 0);
}
backtrack(): void {
this.offset -= this.bytesToBacktrack;
this.bytesToBacktrack = 0;

View File

@@ -22,7 +22,7 @@ class KeyMap extends Serializable<KeyMap> {
_fromBinary(buffer: UhkBuffer): KeyMap {
this.id = buffer.readUInt8();
this.isDefault = this.binToBool(buffer.readUInt8());
this.isDefault = buffer.readBoolean();
this.abbreviation = buffer.readString();
this.name = buffer.readString();
this.layers = new Layers().fromBinary(buffer);
@@ -41,7 +41,7 @@ class KeyMap extends Serializable<KeyMap> {
_toBinary(buffer: UhkBuffer): void {
buffer.writeUInt8(this.id);
buffer.writeUInt8(this.boolToBin(this.isDefault));
buffer.writeBoolean(this.isDefault);
buffer.writeString(this.abbreviation);
buffer.writeString(this.name);
this.layers.toBinary(buffer);

View File

@@ -22,8 +22,8 @@ class Macro extends Serializable<Macro> {
_fromBinary(buffer: UhkBuffer): Macro {
this.id = buffer.readUInt8();
this.isLooped = this.binToBool(buffer.readUInt8());
this.isPrivate = this.binToBool(buffer.readUInt8());
this.isLooped = buffer.readBoolean();
this.isPrivate = buffer.readBoolean();
this.name = buffer.readString();
this.macroActions = new MacroActions().fromBinary(buffer);
return this;
@@ -41,8 +41,8 @@ class Macro extends Serializable<Macro> {
_toBinary(buffer: UhkBuffer): void {
buffer.writeUInt8(this.id);
buffer.writeUInt8(this.boolToBin(this.isLooped));
buffer.writeUInt8(this.boolToBin(this.isPrivate));
buffer.writeBoolean(this.isLooped);
buffer.writeBoolean(this.isPrivate);
buffer.writeString(this.name);
this.macroActions.toBinary(buffer);
}

View File

@@ -21,7 +21,7 @@ class SwitchLayerAction extends KeyAction {
_fromBinary(buffer: UhkBuffer): SwitchLayerAction {
this.readAndAssertKeyActionId(buffer);
this.layer = buffer.readUInt8();
this.isLayerToggleable = this.binToBool(buffer.readUInt8());
this.isLayerToggleable = buffer.readBoolean();
return this;
}
@@ -36,7 +36,7 @@ class SwitchLayerAction extends KeyAction {
_toBinary(buffer: UhkBuffer) {
buffer.writeUInt8(KeyActionId.SwitchLayerAction);
buffer.writeUInt8(this.layer);
buffer.writeUInt8(this.boolToBin(this.isLayerToggleable));
buffer.writeBoolean(this.isLayerToggleable);
}
toString(): string {