Skip to content

Commit

Permalink
feat(scripting/v8): support unpacking Lua vectors
Browse files Browse the repository at this point in the history
  • Loading branch information
thelindat authored Dec 19, 2024
1 parent c970038 commit 5b3afa6
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions data/shared/citizen/scripting/v8/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

const EXT_FUNCREF = 10;
const EXT_LOCALFUNCREF = 11;
const EXT_VECTOR2 = 20;
const EXT_VECTOR3 = 21;
const EXT_VECTOR4 = 22;

(function (global) {
let boundaryIdx = 1;
Expand Down Expand Up @@ -49,6 +52,30 @@ const EXT_LOCALFUNCREF = 11;

const pack = data => msgpack.encode(data, { codec });
const unpack = data => msgpack.decode(data, { codec });

const vectorUnpacker = (data) => {
const buffer = Buffer.from(data);

return Array.from(
new Float32Array(buffer.buffer, buffer.byteOffset, buffer.length / 4),
(value) => Number(value.toPrecision(7))
);
};

addExtension({
type: EXT_VECTOR2,
unpack: vectorUnpacker,
});

addExtension({
type: EXT_VECTOR3,
unpack: vectorUnpacker,
});

addExtension({
type: EXT_VECTOR4,
unpack: vectorUnpacker,
});

// store for use by natives.js
global.msgpack_pack = pack;
Expand Down

0 comments on commit 5b3afa6

Please sign in to comment.