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