Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit aa671cc

Browse files
authoredMar 19, 2025··
feat(scripting/v8): implement vector classes
1 parent 52f3489 commit aa671cc

File tree

1 file changed

+98
-0
lines changed
  • data/shared/citizen/scripting/v8

1 file changed

+98
-0
lines changed
 

‎data/shared/citizen/scripting/v8/main.js

+98
Original file line numberDiff line numberDiff line change
@@ -752,6 +752,104 @@ const EXT_LOCALFUNCREF = 11;
752752
global.LocalPlayer = Player(-1);
753753
}
754754

755+
756+
757+
if (!isDuplicityVersion) {
758+
global.LocalPlayer = Player(-1);
759+
}
760+
761+
class Vector {
762+
#length;
763+
764+
constructor(...args) {
765+
this.#length = args.length;
766+
this.x = args[0];
767+
this.y = args[1];
768+
}
769+
770+
*[Symbol.iterator]() {
771+
yield this.x;
772+
yield this.y;
773+
774+
if (this.z !== undefined) yield this.z;
775+
if (this.w !== undefined) yield this.w;
776+
}
777+
778+
get size() {
779+
return this.#length;
780+
}
781+
}
782+
783+
class Vector2 extends Vector {
784+
static ext = 20;
785+
786+
constructor(x, y = x) {
787+
super(x, y);
788+
}
789+
}
790+
791+
class Vector3 extends Vector {
792+
static ext = 21;
793+
794+
constructor(x, y = x, z = y) {
795+
super(x, y, z);
796+
797+
this.z = z;
798+
}
799+
}
800+
801+
class Vector4 extends Vector {
802+
static ext = 22;
803+
804+
constructor(x, y = x, z = y, w = z) {
805+
super(x, y, z, w);
806+
807+
this.z = z;
808+
this.w = w;
809+
}
810+
}
811+
812+
function vectorUnpacker(data) {
813+
const buffer = Buffer.from(data);
814+
const length = buffer.length / 4;
815+
const float32 = new Float32Array(buffer.buffer, buffer.byteOffset, length);
816+
817+
if (length === 2) return new Vector2(...float32);
818+
if (length === 3) return new Vector3(...float32);
819+
if (length === 4) return new Vector4(...float32);
820+
}
821+
822+
function vectorPacker(vec) {
823+
const length = vec.size;
824+
const float32 = new Float32Array(length);
825+
let index = 0;
826+
827+
for (const value of vec) float32[index++] = value;
828+
829+
return new Uint8Array(float32.buffer);
830+
}
831+
832+
msgpack_extend({
833+
type: Vector2.ext,
834+
Class: Vector2,
835+
pack: vectorPacker,
836+
unpack: vectorUnpacker,
837+
});
838+
839+
msgpack_extend({
840+
type: Vector3.ext,
841+
Class: Vector3,
842+
pack: vectorPacker,
843+
unpack: vectorUnpacker,
844+
});
845+
846+
msgpack_extend({
847+
type: Vector4.ext,
848+
Class: Vector4,
849+
pack: vectorPacker,
850+
unpack: vectorUnpacker,
851+
});
852+
755853
/*
756854
BEGIN
757855
https://github.com/errwischt/stacktrace-parser/blob/0121cc6e7d57495437818676f6b69be7d34c2fa7/src/stack-trace-parser.js

0 commit comments

Comments
 (0)
Please sign in to comment.