Skip to content

Commit

Permalink
payload helper
Browse files Browse the repository at this point in the history
  • Loading branch information
BuckarooBanzay committed Mar 31, 2023
1 parent 30b6d3e commit 10e5628
Show file tree
Hide file tree
Showing 13 changed files with 75 additions and 41 deletions.
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ services:
- "node_modules:/app/node_modules"
user: root
working_dir: "/app"
command: "bash -c 'npm ci && npm run watch'"
command: "bash -c '(test -d node_modules || npm ci) && npm run watch'"

integration_test:
image: node:18.9.0
Expand Down
2 changes: 1 addition & 1 deletion webapp/src/command/client/ClientFirstSRP.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class ClientFirstSRP implements ClientCommand {
}

marshalPacket(): Uint8Array {
const p = new PayloadBuilder();
const p = new PayloadBuilder(2 + this.salt.length + 2 + this.verificationKey.length + 1);
p.appendArray(this.salt)
p.appendArray(this.verificationKey)
p.appendUint8(0)
Expand Down
16 changes: 15 additions & 1 deletion webapp/src/command/client/ClientGotBlocks.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
import { Pos, PosType } from "../../util/pos";
import { ClientCommand } from "../ClientCommand";
import { PayloadBuilder } from "../packet/PayloadBuilder";

export class ClientGotBlocks implements ClientCommand {

constructor(public blocks: Array<Pos<PosType.Mapblock>>) {}

getCommandID(): number {
return 0x24
}
marshalPacket(): Uint8Array {
throw new Error("Method not implemented.");
const pb = new PayloadBuilder(1 + (6*this.blocks.length))
pb.appendUint8(this.blocks.length)

this.blocks.forEach(p => {
pb.appendUint16(p.x)
pb.appendUint16(p.y)
pb.appendUint16(p.z)
})

return pb.toUint8Array()
}

}
2 changes: 1 addition & 1 deletion webapp/src/command/client/ClientInit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export class ClientInit implements ClientCommand {
}

marshalPacket(): Uint8Array {
const p = new PayloadBuilder();
const p = new PayloadBuilder(7 + 2 + this.playername.length);
p.appendUint8(this.clientMax);
p.appendUint16(this.supportedCompressionModes);
p.appendUint16(this.minNetProtoVersion);
Expand Down
14 changes: 12 additions & 2 deletions webapp/src/command/client/ClientPlayerPos.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,19 @@ import { ClientPlayerPos } from "./ClientPlayerPos"

describe("ClientPlayerPos", function() {
test("marshal", function() {
const pp = new ClientPlayerPos(new Pos<PosType.Entity>(10,20,30))
const pp = new ClientPlayerPos(new Pos<PosType.Entity>(312.238,15,389.573))
const a = pp.marshalPacket()

console.log(dataViewToHexDump(new DataView(a.buffer)))
// y
expect(a[4]).toBe(0x00)
expect(a[5]).toBe(0x00)
expect(a[6]).toBe(0x3A)
expect(a[7]).toBe(0x98)

// x
expect(a[0]).toBe(0x00)
expect(a[1]).toBe(0x04)
expect(a[2]).toBe(0xC3)
expect(a[3]).toBe(0xAE)
})
})
16 changes: 13 additions & 3 deletions webapp/src/command/client/ClientPlayerPos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,19 @@ export class ClientPlayerPos implements ClientCommand {
}

marshalPacket(): Uint8Array {
const pb = new PayloadBuilder()
pb.appendPos(this.pos) //position
pb.appendPos(new Pos<PosType.Entity>(0,0,0)) //speed
const pb = new PayloadBuilder(38)
//note: player-position and speed are scaled up by x1000

//position
pb.appendUint32(this.pos.x * 1000)
pb.appendUint32(this.pos.y * 1000)
pb.appendUint32(this.pos.z * 1000)

//speed
pb.appendUint32(0)
pb.appendUint32(0)
pb.appendUint32(0)

pb.appendFloat32(500) //pitch
pb.appendFloat32(6000) //yaw
pb.appendUint32(0) //pressed keys
Expand Down
2 changes: 1 addition & 1 deletion webapp/src/command/client/ClientReady.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class ClientReady implements ClientCommand {
}

marshalPacket(): Uint8Array {
const pb = new PayloadBuilder();
const pb = new PayloadBuilder(6 + 2 + this.fullVersion.length);
pb.appendUint8(this.versionMajor);
pb.appendUint8(this.versionMinor);
pb.appendUint8(this.versionPatch);
Expand Down
5 changes: 4 additions & 1 deletion webapp/src/command/client/ClientRequestMedia.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ export class ClientRequestMedia implements ClientCommand {
}

marshalPacket(): Uint8Array {
const pb = new PayloadBuilder();
let name_size = 0
this.names.forEach(n => name_size += n.length + 2)

const pb = new PayloadBuilder(2 + name_size);
pb.appendUint16(this.names.length);
this.names.forEach(n => pb.appendString(n));
return pb.toUint8Array();
Expand Down
2 changes: 1 addition & 1 deletion webapp/src/command/client/ClientSRPBytesA.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class ClientSRPBytesA implements ClientCommand {
}

marshalPacket(): Uint8Array {
const p = new PayloadBuilder();
const p = new PayloadBuilder(2 + this.bytesA.length + 1);
p.appendArray(this.bytesA)
p.appendUint8(0x01)
return p.toUint8Array();
Expand Down
2 changes: 1 addition & 1 deletion webapp/src/command/client/ClientSRPBytesM.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class ClientSRPBytesM implements ClientCommand {
}

marshalPacket(): Uint8Array {
const p = new PayloadBuilder();
const p = new PayloadBuilder(2 + this.bytesM.length + 1);
p.appendArray(this.bytesM)
p.appendUint8(0x01)
return p.toUint8Array();
Expand Down
41 changes: 16 additions & 25 deletions webapp/src/command/packet/PayloadBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,39 @@ import { Pos, PosType } from "../../util/pos";

export class PayloadBuilder {

private data: Array<number>
data: Uint8Array
dv: DataView
index = 0

constructor(data?: number[]) {
this.data = data || new Array<number>()
constructor(size: number) {
this.data = new Uint8Array(size)
this.dv = new DataView(this.data.buffer)
}

appendUint8(v: number) {
this.data.push(v);
this.dv.setUint8(this.index, v)
this.index++
}

appendUint16(v: number) {
this.data.push((v >> 8) & 0xFF);
this.data.push(v & 0xFF);
this.dv.setUint16(this.index, v)
this.index += 2
}

appendUint32(v: number){
this.data.push((v >> 32) & 0xFF);
this.data.push((v >> 16) & 0xFF);
this.data.push((v >> 8) & 0xFF);
this.data.push(v & 0xFF);
this.dv.setUint32(this.index, v)
this.index += 4
}

appendFloat32(v: number) {
const a = new Uint8Array(4)
const dv = new DataView(a.buffer)
dv.setFloat32(0, v)
this.appendUint8(dv.getUint8(0))
this.appendUint8(dv.getUint8(1))
this.appendUint8(dv.getUint8(2))
this.appendUint8(dv.getUint8(3))
}

appendPos(p: Pos<PosType.Entity>) {
this.appendUint32(p.x * 1000)
this.appendUint32(p.y * 1000)
this.appendUint32(p.z * 1000)
this.dv.setFloat32(this.index, v)
this.index += 4
}

appendString(s: string) {
this.appendUint16(s.length);
for (let i=0; i<s.length; i++){
this.data.push(s.charCodeAt(i));
this.appendUint8(s.charCodeAt(i));
}
}

Expand All @@ -55,7 +46,7 @@ export class PayloadBuilder {
}

toUint8Array(): Uint8Array {
return new Uint8Array(this.data);
return this.data
}

}
6 changes: 3 additions & 3 deletions webapp/src/command/server/ServerAuthAccept.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ export class ServerAuthAccept implements ServerCommand {
public sendInterval!: number

unmarshalPacket(dv: DataView): void {
this.posX = dv.getInt32(0) //TODO: scale?
this.posY = dv.getInt32(4)
this.posZ = dv.getInt32(8)
this.posX = dv.getFloat32(0) / 10
this.posY = dv.getFloat32(4) / 10
this.posZ = dv.getFloat32(8) / 10
this.seed = dv.getBigUint64(12).toString()
this.sendInterval = dv.getFloat32(20)
}
Expand Down
6 changes: 6 additions & 0 deletions webapp/src/scene/Scene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { Pos, PosType, toMatrix4 } from "../util/pos";
import { ServerMovePlayer } from "../command/server/ServerMovePlayer";
import { ClientPlayerPos } from "../command/client/ClientPlayerPos";
import { PacketType } from "../command/packet/types";
import { ClientGotBlocks } from "../command/client/ClientGotBlocks";



Expand Down Expand Up @@ -44,6 +45,9 @@ export class Scene {
const cv = this.cvm.create(b.pos, b.pos)
Logger.debug(`Adding ${cv.meshes.length} meshes to scene`)
cv.meshes.forEach(m => this.scene.add(m))

const gotblocks = new ClientGotBlocks([b.pos])
client.cc.sendCommand(gotblocks, PacketType.Original)
})

client.cc.events.on("ServerCommand", cmd => {
Expand All @@ -53,6 +57,8 @@ export class Scene {
})

client.events.on("Tick", c => {
// XXX: move forward 1m/s
//this.pos = this.pos.add(new Pos<PosType.Entity>(1, 0, 0))
c.cc.sendCommand(new ClientPlayerPos(this.pos), PacketType.Original)
})

Expand Down

0 comments on commit 10e5628

Please sign in to comment.