From bd176059473f4dbfd841c9578bd9e7221c21c25e Mon Sep 17 00:00:00 2001 From: ABCxFF <79597906+ABCxFF@users.noreply.github.com> Date: Fri, 11 Nov 2022 14:33:26 -0500 Subject: [PATCH] patch: #2 --- src/Coder/Writer.ts | 22 ++++++++++++++++++-- src/Native/Camera.ts | 49 +++++++++++++++++++++++++++++--------------- 2 files changed, 52 insertions(+), 19 deletions(-) diff --git a/src/Coder/Writer.ts b/src/Coder/Writer.ts index f60fe7b3..39f0e702 100644 --- a/src/Coder/Writer.ts +++ b/src/Coder/Writer.ts @@ -22,7 +22,10 @@ // TODO(ABC): // Code a compressor (The previous was not OOP and used lz4js code) // If this code gets out of bounds (only happens if you play around with dev too much), the server crashes [better than out of bounds r/w]. -const OUTPUT_BUFFER = new Uint8Array(65536); +// +// TEMP FIX - 2022/11/11: +// OUTPUT_BUFFER now gets resized if running out of space for the packet +let OUTPUT_BUFFER = new Uint8Array(65536); const convo = new ArrayBuffer(4); const u8 = new Uint8Array(convo); @@ -40,7 +43,22 @@ const endianSwap = (num: number) => ((num >> 24) & 0xff); export default class Writer { - private at: number = 0; + private _at: number = 0; + + private get at() { + return this._at; + } + + private set at(v) { + this._at = v; + + if (OUTPUT_BUFFER.byteLength <= this._at + 5) { + const newBuffer = new Uint8Array(OUTPUT_BUFFER.byteLength + (OUTPUT_BUFFER.byteLength >> 1)); + newBuffer.set(OUTPUT_BUFFER, 0); + + OUTPUT_BUFFER = newBuffer; + } + } public u8(val: number) { OUTPUT_BUFFER[this.at++] = val; diff --git a/src/Native/Camera.ts b/src/Native/Camera.ts index 6bf080e1..16fa5e12 100644 --- a/src/Native/Camera.ts +++ b/src/Native/Camera.ts @@ -156,8 +156,6 @@ export default class Camera extends CameraEntity { /** Updates the camera's current view. */ private updateView(tick: number) { - const w = this.client.write().u8(ClientBound.Update).vu(tick); - const deletes: { id: number, hash: number, noDelete?: boolean }[] = []; const updates: Entity[] = []; const creations: Entity[] = []; @@ -222,13 +220,8 @@ export default class Camera extends CameraEntity { } } - // Now compile - w.vu(deletes.length); - for (let i = 0; i < deletes.length; ++i) { - w.entid(deletes[i]); - if (!deletes[i].noDelete) this.removeFromView(deletes[i].id); - } + // Next pick updates const entities = this.game.entities; for (const id of this.game.entities.otherEntities) { if (this.view.findIndex(r => r.id === id) === -1) { @@ -269,16 +262,38 @@ export default class Camera extends CameraEntity { } } - // Arrays of entities - w.vu(creations.length + updates.length); - for (let i = 0; i < updates.length; ++i) { - this.compileUpdate(w, updates[i]); - } - for (let i = 0; i < creations.length; ++i) { - this.compileCreation(w, creations[i]); - } + // UPDATE 2022/11/11 - Send out only chunks of 64 updates per packet to prevent intense packet size + // - WHEN COMPRESSION IS ADDED, UNDO + const CHUNK_SIZE = 64; + let entitiesSent = 0; + while (entitiesSent < creations.length + updates.length) { + // Prep for compilation + const w = this.client.write().u8(ClientBound.Update).vu(tick); + // If the first packet of tick, compile deletions + if (entitiesSent === 0) { + w.vu(deletes.length); + for (let i = 0; i < deletes.length; ++i) { + w.entid(deletes[i]); + if (!deletes[i].noDelete) this.removeFromView(deletes[i].id); + } + } else w.vu(0); // or assume they've already been completed - w.send(); + const lastChunk = entitiesSent; + const sizeOfChunk = creations.length + updates.length - lastChunk < CHUNK_SIZE ? creations.length + updates.length - lastChunk : CHUNK_SIZE; + + // Chunk out the arrays of entities + w.vu(sizeOfChunk); + while (entitiesSent < updates.length && entitiesSent - lastChunk < sizeOfChunk) { + this.compileUpdate(w, updates[entitiesSent]); + entitiesSent += 1; + } + while (entitiesSent - updates.length < creations.length && entitiesSent - lastChunk < sizeOfChunk) { + this.compileCreation(w, creations[entitiesSent - updates.length]); + entitiesSent += 1; + } + + w.send(); + } } /** Entity creation compiler function... Run! */