Little endian

This commit is contained in:
Emily 2019-08-01 01:56:46 +02:00
parent 4cd9e4daf9
commit f1efdd534d

View File

@ -17,7 +17,7 @@ class FLYSocket {
let data = new DataView(buff); let data = new DataView(buff);
this.callbackRaw(data); this.callbackRaw(data);
if (this.callbackData != this.callbackDummy || true) { // DEBUG if (this.callbackData != this.callbackDummy) {
this.callbackData( this.parse(data) ); this.callbackData( this.parse(data) );
} }
}.bind(this) }.bind(this)
@ -30,7 +30,7 @@ class FLYSocket {
this.pointer = 0; this.pointer = 0;
while (this.pointer < data.byteLength) { while (this.pointer < data.byteLength) {
let packetID = data.getInt8(this.pointer++); let packetID = this.readUint8(data);
packets.push({ packets.push({
packetID: packetID, packetID: packetID,
value: (this["handle_packet" + packetID] || this.handle_packetInvalid).call(this, data) value: (this["handle_packet" + packetID] || this.handle_packetInvalid).call(this, data)
@ -40,23 +40,45 @@ class FLYSocket {
return packets; return packets;
} }
readUint8(data) {
return data.getUint8(this.pointer++, true);
}
readUint16(data) {
let ret = data.getUint16(this.pointer, true); this.pointer += 2;
return ret;
}
readInt16(data) {
let ret = data.getInt16(this.pointer, true); this.pointer += 2;
return ret;
}
readUint32(data) {
let ret = data.getUint32(this.pointer, true); this.pointer += 4;
return ret;
}
readFloat32(data) {
let ret = data.getFloat32(this.pointer, true); this.pointer += 4;
return ret;
}
handle_packet0(data) { handle_packet0(data) {
// BxI // BxI
// B: Length // B: Length
// I: FlightUUID // I: FlightUUID
let ret = []; let ret = [];
let length = data.getUint8(this.pointer++); let length = this.readUint8(data);
for (let i = 0; i < length; i++) { for (let i = 0; i < length; i++)
ret.push( data.getUint32(this.pointer) ); ret.push( this.readUint32(data) );
this.pointer += 4;
}
return ret; return ret;
} }
handle_packet1(data) { handle_packet1(data) {
// ? // ?
// ?: Subscribed to changes // ?: Subscribed to changes
return data.getUint8(this.pointer++) != 0; return this.readUint8(data) != 0;
} }
handle_packet2(data) { handle_packet2(data) {
@ -64,8 +86,8 @@ class FLYSocket {
// I: FlightUUID // I: FlightUUID
// ?: Subscribed to flight data // ?: Subscribed to flight data
let ret = []; let ret = [];
ret.push( data.getUint32(this.pointer) ); this.pointer += 4; ret.push( this.readUint32(data) );
ret.push( data.getUint8(this.pointer++) != 0 ); ret.push( this.readUint8(data) != 0 );
return ret; return ret;
} }
@ -73,13 +95,13 @@ class FLYSocket {
// xs // xs
// s: DataStream // s: DataStream
let ret = {}; let ret = {};
ret["uuid"] = data.getUint32(this.pointer); this.pointer += 4; ret["uuid"] = this.readUint32(data);
ret["maxFuel"] = data.getFloat32(this.pointer); this.pointer += 4; ret["maxFuel"] = this.readFloat32(data);
ret["model"] = data.getUint16(this.pointer); this.pointer += 2; ret["model"] = this.readUint16(data);
let playernameLength = data.getUint8(this.pointer++); let playernameLength = this.readUint8(data);
let playername = ""; let playername = "";
for (let i = 0; i < playernameLength; i++) for (let i = 0; i < playernameLength; i++)
playername += String.fromCharCode( data.getUint8(this.pointer++) ); playername += String.fromCharCode( this.readUint8(data) );
ret["playername"] = playername; ret["playername"] = playername;
let timeline = []; let timeline = [];
@ -94,8 +116,8 @@ class FLYSocket {
// I: FlightUUID // I: FlightUUID
// ?: Flight active // ?: Flight active
let ret = []; let ret = [];
ret.push( data.getUint32(this.pointer) ); this.pointer += 4; ret.push( this.readUint32(data) );
ret.push( data.getUint8(this.pointer++) != 0 ); ret.push( this.readUint8(data) != 0 );
return ret; return ret;
} }
@ -104,7 +126,7 @@ class FLYSocket {
// I: FlightUUID // I: FlightUUID
// s: DataStream // s: DataStream
let ret = []; let ret = [];
ret.push( data.getUint32(this.pointer) ); this.pointer += 4; ret.push( this.readUint32(data) );
ret.push( this.parseFrame(data) ); ret.push( this.parseFrame(data) );
return ret; return ret;
} }
@ -114,9 +136,9 @@ class FLYSocket {
// H: Length // H: Length
// s: ErrorMessage // s: ErrorMessage
let ret = ""; let ret = "";
let length = data.getUint16(this.pointer); this.pointer += 2; let length = this.readUint16(data);
for (let i = 0; i < length; i++) { for (let i = 0; i < length; i++) {
ret += String.fromCharCode( data.getUint8(this.pointer++) ); ret += String.fromCharCode( this.readUint8(data) );
} }
return ret; return ret;
} }
@ -131,33 +153,33 @@ class FLYSocket {
// B: Flag // B: Flag
// s: FrameStream // s: FrameStream
let ret = {}; let ret = {};
ret["timestamp"] = data.getUint32(this.pointer); this.pointer += 4; ret["timestamp"] = this.readUint32(data);
let flags = data.getUint8(this.pointer++); let flags = this.readUint8(data);
if (flags & (1<<0)) { if (flags & (1<<0)) {
let statusFlag = data.getUint8(this.pointer++); let statusFlag = this.readUint8(data);
ret["paused"] = !!( statusFlag & (1<<0) ); ret["paused"] = !!( statusFlag & (1<<0) );
ret["engine"] = !!( statusFlag & (1<<1) ); ret["engine"] = !!( statusFlag & (1<<1) );
} }
if (flags & (1<<1)) { if (flags & (1<<1)) {
ret["satisfaction"] = data.getUint8(this.pointer++); ret["satisfaction"] = this.readUint8(data);
} }
if (flags & (1<<2)) { if (flags & (1<<2)) {
ret["speed"] = data.getUint16(this.pointer); this.pointer += 2; ret["speed"] = this.readUint16(data);
} }
if (flags & (1<<3)) { if (flags & (1<<3)) {
ret["altitude"] = data.getInt16(this.pointer); this.pointer += 2; ret["altitude"] = this.readInt16(data);
} }
if (flags & (1<<4)) { if (flags & (1<<4)) {
ret["health"] = data.getUint16(this.pointer); this.pointer += 2; ret["health"] = this.readUint16(data);
} }
if (flags & (1<<5)) { if (flags & (1<<5)) {
ret["fuel"] = data.getFloat32(this.pointer); this.pointer += 4; ret["fuel"] = this.readFloat32(data);
} }
if (flags & (1<<6)) { if (flags & (1<<6)) {
ret["x"] = data.getFloat32(this.pointer); this.pointer += 4; ret["x"] = this.readFloat32(data);
} }
if (flags & (1<<7)) { if (flags & (1<<7)) {
ret["y"] = data.getFloat32(this.pointer); this.pointer += 4; ret["y"] = this.readFloat32(data);
} }
return ret; return ret;
} }