Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(scripting/v8): support unpacking Lua vectors #3019

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading