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): Add unpackers for Vector2, Vector3, and Vector4 u… #2946

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
13 changes: 13 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 @@ -47,6 +50,16 @@ const EXT_LOCALFUNCREF = 11;
binarraybuffer: true
});

// Unpack a msgpack buffer into an array of vector components with preserved precision
const vectorUnpacker = (data => Array.from(new Float32Array(data.buffer), (v) => Number(v.toPrecision(7))));

// Vector2 unpacker
codec.addExtUnpacker(EXT_VECTOR2, vectorUnpacker);
// Vector3 unpacker
codec.addExtUnpacker(EXT_VECTOR3, vectorUnpacker);
// Vector4 unpacker
codec.addExtUnpacker(EXT_VECTOR4, vectorUnpacker);

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

Expand Down
Loading