@nativewrappers/common 0.0.48 → 0.0.50

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "author": "Remco Troost <d0p3t>",
5
5
  "license": "MIT",
6
6
  "type": "module",
7
- "version": "0.0.48",
7
+ "version": "0.0.50",
8
8
  "repository": {
9
9
  "type": "git",
10
10
  "url": "https://github.com/nativewrappers/fivem.git"
package/utils/Vector.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import type { MsgpackBuffer } from "../types";
2
2
  import { ClassTypes } from "./ClassTypes";
3
+ declare const size: unique symbol;
3
4
  /**
4
5
  * Represents a 2-dimensional vector.
5
6
  */
@@ -49,22 +50,8 @@ type InferVector<T> = T extends Vec4 | VectorN<4> ? Vector4 : T extends Vec3 | V
49
50
  /**
50
51
  * A base vector class inherited by all vector classes.
51
52
  */
52
- export declare class Vector {
53
- size: number;
54
- x: number;
55
- y: number;
56
- z?: number | undefined;
57
- w?: number | undefined;
58
- type: ClassTypes;
59
- protected static create(x: number, y?: number): Vector2;
60
- protected static create(x: number, y?: number, z?: number): Vector3;
61
- protected static create(x: number, y?: number, z?: number, w?: number): Vector4;
62
- /**
63
- * Creates a new vector based on the provided vector-like object.
64
- * @param obj The object representing the vector.
65
- * @returns A new vector instance.
66
- */
67
- protected static create<T extends VectorType, U extends VectorLike>(this: T, obj: U): InferVector<U>;
53
+ declare class Vector {
54
+ protected static create(x: number | Vec, y?: number, z?: number, w?: number): Vector;
68
55
  /**
69
56
  * Creates a vector from binary data in a MsgpackBuffer.
70
57
  * @param msgpackBuffer The buffer containing binary data.
@@ -76,7 +63,7 @@ export declare class Vector {
76
63
  * @param obj The vector to clone.
77
64
  * @returns A new vector instance that is a copy of the provided vector.
78
65
  */
79
- static clone<T extends VectorType, U extends VectorLike>(this: T, obj: U): InferVector<U>;
66
+ static clone<T extends VectorType, U extends VectorLike>(this: T, obj: U): InstanceType<T>;
80
67
  /**
81
68
  * Performs an operation between a vector and either another vector or scalar value.
82
69
  * @param a - The first vector.
@@ -98,28 +85,28 @@ export declare class Vector {
98
85
  * @param x - The value to add to the x-component.
99
86
  * @returns A new vector with the x-component incremented.
100
87
  */
101
- static addX<T extends VectorType, U extends VectorLike>(this: T, obj: U, x: number): InferVector<U>;
88
+ static addX<U extends VectorLike>(obj: U, x: number): U;
102
89
  /**
103
90
  * Adds a scalar value to the y-component of a vector.
104
91
  * @param obj - The vector.
105
92
  * @param y - The value to add to the y-component.
106
93
  * @returns A new vector with the y-component incremented.
107
94
  */
108
- static addY<T extends VectorType, U extends VectorLike>(this: T, obj: U, y: number): InferVector<U>;
95
+ static addY<T extends VectorType, U extends VectorLike>(this: T, obj: U, y: number): Vector;
109
96
  /**
110
97
  * Adds a scalar value to the z-component of a vector.
111
98
  * @param obj - The vector.
112
99
  * @param z - The value to add to the z-component.
113
100
  * @returns A new vector with the z-component incremented.
114
101
  */
115
- static addZ<T extends VectorType, U extends Vec3 | Vec4>(this: T, obj: U, z: number): InferVector<U>;
102
+ static addZ<T extends VectorType, U extends Vec3 | Vec4>(this: T, obj: U, z: number): Vector;
116
103
  /**
117
104
  * Adds a scalar value to the w-component of a vector.
118
105
  * @param obj - The vector.
119
106
  * @param w - The value to add to the w-component.
120
107
  * @returns A new vector with the w-component incremented.
121
108
  */
122
- static addW<T extends VectorType, U extends Vec4>(this: T, obj: U, w: number): InferVector<U>;
109
+ static addW<T extends VectorType, U extends Vec4>(this: T, obj: U, w: number): Vector;
123
110
  /**
124
111
  * Subtracts one vector from another or subtracts a scalar value from a vector.
125
112
  * @param a - The vector.
@@ -218,21 +205,27 @@ export declare class Vector {
218
205
  * @returns The magnitude of the vector.
219
206
  */
220
207
  static Length<T extends VectorType, U extends VectorLike>(this: T, obj: U): number;
208
+ type: unknown;
209
+ [size]: number;
210
+ x: number;
211
+ y: number;
212
+ z: number | undefined;
213
+ w: number | undefined;
221
214
  /**
222
215
  * Constructs a new vector.
223
- * @param size The size of the vector (number of components).
224
216
  * @param x The x-component of the vector.
225
217
  * @param y The y-component of the vector (optional, defaults to x).
226
218
  * @param z The z-component of the vector (optional).
227
219
  * @param w The w-component of the vector (optional).
228
220
  */
229
- constructor(size: number, x?: number, y?: number, z?: number | undefined, w?: number | undefined);
221
+ constructor(x: number, y?: number, z?: number, w?: number);
230
222
  [Symbol.iterator](): Iterator<number>;
223
+ get size(): number;
231
224
  toString(): string;
232
225
  /**
233
226
  * @see Vector.clone
234
227
  */
235
- clone(): InferVector<this>;
228
+ clone<T extends this>(): T;
236
229
  /**
237
230
  * The product of the Euclidean magnitudes of this and another Vector.
238
231
  *
@@ -262,11 +255,11 @@ export declare class Vector {
262
255
  /**
263
256
  * @see Vector.addX
264
257
  */
265
- addX(x: number): InferVector<this>;
258
+ addX(x: number): this;
266
259
  /**
267
260
  * @see Vector.addY
268
261
  */
269
- addY(y: number): InferVector<this>;
262
+ addY(y: number): Vector;
270
263
  /**
271
264
  * @see Vector.subtract
272
265
  */
@@ -314,7 +307,8 @@ export declare class Vector {
314
307
  * Represents a 2-dimensional vector.
315
308
  */
316
309
  export declare class Vector2 extends Vector {
317
- type: ClassTypes;
310
+ readonly type = ClassTypes.Vector2;
311
+ readonly [size]: number;
318
312
  static readonly Zero: Vector2;
319
313
  /**
320
314
  * Constructs a new 2D vector.
@@ -322,12 +316,20 @@ export declare class Vector2 extends Vector {
322
316
  * @param y The y-component of the vector (optional, defaults to x).
323
317
  */
324
318
  constructor(x: number, y?: number);
319
+ /**
320
+ * Creates a new vector based on the provided parameters.
321
+ * @param x The x-component of the vector.
322
+ * @param y The y-component of the vector (optional, defaults to the value of x).
323
+ * @returns A new vector instance.
324
+ */
325
+ protected static create(x: number | Vec, y?: number): Vector2;
325
326
  }
326
327
  /**
327
328
  * Represents a 3-dimensional vector.
328
329
  */
329
330
  export declare class Vector3 extends Vector implements Vec3 {
330
- type: ClassTypes;
331
+ readonly type = ClassTypes.Vector3;
332
+ readonly [size]: number;
331
333
  z: number;
332
334
  static readonly Zero: Vector3;
333
335
  static readonly UnitX: Vector3;
@@ -350,10 +352,18 @@ export declare class Vector3 extends Vector implements Vec3 {
350
352
  * @param z The z-component of the vector (optional, defaults to y).
351
353
  */
352
354
  constructor(x: number, y?: number, z?: number);
355
+ /**
356
+ * Creates a new vector based on the provided parameters.
357
+ * @param x The x-component of the vector.
358
+ * @param y The y-component of the vector (optional, defaults to the value of x).
359
+ * @param z The z-component of the vector (optional, defaults to the value of y).
360
+ * @returns A new vector instance.
361
+ */
362
+ protected static create(x: number | Vec, y?: number, z?: number): Vector3;
353
363
  /**
354
364
  * @see Vector.addZ
355
365
  */
356
- addZ(z: number): InferVector<this>;
366
+ addZ(z: number): Vector;
357
367
  /**
358
368
  * @see Vector.crossProduct
359
369
  */
@@ -367,7 +377,8 @@ export declare class Vector3 extends Vector implements Vec3 {
367
377
  * Represents a 4-dimensional vector.
368
378
  */
369
379
  export declare class Vector4 extends Vector {
370
- type: ClassTypes;
380
+ readonly type = ClassTypes.Vector4;
381
+ readonly [size]: number;
371
382
  z: number;
372
383
  w: number;
373
384
  static readonly Zero: Vector4;
@@ -379,14 +390,23 @@ export declare class Vector4 extends Vector {
379
390
  * @param w The w-component of the vector (optional, defaults to z).
380
391
  */
381
392
  constructor(x: number, y?: number, z?: number, w?: number);
393
+ /**
394
+ * Creates a new vector based on the provided parameters.
395
+ * @param x The x-component of the vector.
396
+ * @param y The y-component of the vector (optional, defaults to the value of x).
397
+ * @param z The z-component of the vector (optional, defaults to the value of y).
398
+ * @param w The w-component of the vector (optional, defaults to the value of z).
399
+ * @returns A new vector instance.
400
+ */
401
+ protected static create(x: number | Vec, y?: number, z?: number, w?: number): Vector4;
382
402
  /**
383
403
  * @see Vector.addZ
384
404
  */
385
- addZ(z: number): InferVector<this>;
405
+ addZ(z: number): Vector;
386
406
  /**
387
407
  * @see Vector.addW
388
408
  */
389
- addW(w: number): InferVector<this>;
409
+ addW(w: number): Vector;
390
410
  /**
391
411
  * @see Vector.crossProduct
392
412
  */
package/utils/Vector.js CHANGED
@@ -3,43 +3,15 @@ import { ClassTypes } from "./ClassTypes";
3
3
  const EXT_VECTOR2 = 20;
4
4
  const EXT_VECTOR3 = 21;
5
5
  const EXT_VECTOR4 = 22;
6
+ const size = Symbol("size");
6
7
  /**
7
8
  * A base vector class inherited by all vector classes.
8
9
  */
9
- export class Vector {
10
- size;
11
- x;
12
- y;
13
- z;
14
- w;
15
- // DO NOT USE, ONLY EXPOSED BECAUSE TS IS TRASH, THIS TYPE IS NOT GUARANTEED
16
- // TO EXIST
17
- type = ClassTypes.Vector2;
18
- /**
19
- * Creates a new vector based on the provided parameters.
20
- * @param x The x-component of the vector.
21
- * @param y The y-component of the vector (optional, defaults to the value of x).
22
- * @param z The z-component of the vector (optional, defaults to the value of y).
23
- * @param w The w-component of the vector (optional, defaults to the value of z).
24
- * @returns A new vector instance.
25
- */
26
- static create(x, y, z, w) {
27
- if (y === undefined && typeof x === "number")
28
- return new Vector2(x, x);
10
+ class Vector {
11
+ static create(x, y = x, z, w) {
29
12
  if (typeof x === "object")
30
- ({ x, y, z, w } = x);
31
- const size = this instanceof Vector
32
- ? this.size
33
- : [x, y, z, w].filter((arg) => arg !== undefined).length;
34
- switch (size) {
35
- default:
36
- case 2:
37
- return new Vector2(x, y);
38
- case 3:
39
- return new Vector3(x, y, z);
40
- case 4:
41
- return new Vector4(x, y, z, w);
42
- }
13
+ ({ x, y, z = y, w = z } = x);
14
+ return new this(x, y, z, w);
43
15
  }
44
16
  /**
45
17
  * Creates a vector from binary data in a MsgpackBuffer.
@@ -49,7 +21,9 @@ export class Vector {
49
21
  static fromBuffer({ buffer, type }) {
50
22
  if (type !== EXT_VECTOR2 && type !== EXT_VECTOR3 && type !== EXT_VECTOR4)
51
23
  throw new Error("Buffer type is not a valid Vector.");
52
- const arr = Array.from(new Float32Array(buffer), (v) => Number(v.toPrecision(7)));
24
+ const arr = new Array(buffer.length / 4);
25
+ for (let i = 0; i < arr.length; i++)
26
+ arr[i] = Number(buffer.readFloatLE(i * 4).toPrecision(7));
53
27
  return this.fromArray(arr);
54
28
  }
55
29
  /**
@@ -94,9 +68,7 @@ export class Vector {
94
68
  * @returns A new vector with the x-component incremented.
95
69
  */
96
70
  static addX(obj, x) {
97
- const vec = this.clone(obj);
98
- vec.x += x;
99
- return vec;
71
+ return this.create(obj.x + x, obj.y, obj.z, obj.w);
100
72
  }
101
73
  /**
102
74
  * Adds a scalar value to the y-component of a vector.
@@ -105,9 +77,7 @@ export class Vector {
105
77
  * @returns A new vector with the y-component incremented.
106
78
  */
107
79
  static addY(obj, y) {
108
- const vec = this.clone(obj);
109
- vec.y += y;
110
- return vec;
80
+ return this.create(obj.x, obj.y + y, obj.z, obj.w);
111
81
  }
112
82
  /**
113
83
  * Adds a scalar value to the z-component of a vector.
@@ -116,9 +86,7 @@ export class Vector {
116
86
  * @returns A new vector with the z-component incremented.
117
87
  */
118
88
  static addZ(obj, z) {
119
- const vec = this.clone(obj);
120
- vec.z += z;
121
- return vec;
89
+ return this.create(obj.x, obj.y, obj.z + z, obj.w);
122
90
  }
123
91
  /**
124
92
  * Adds a scalar value to the w-component of a vector.
@@ -127,9 +95,7 @@ export class Vector {
127
95
  * @returns A new vector with the w-component incremented.
128
96
  */
129
97
  static addW(obj, w) {
130
- const vec = this.clone(obj);
131
- vec.w += w;
132
- return vec;
98
+ return this.create(obj.x, obj.y, obj.z, obj.w + w);
133
99
  }
134
100
  /**
135
101
  * Subtracts one vector from another or subtracts a scalar value from a vector.
@@ -299,20 +265,27 @@ export class Vector {
299
265
  }
300
266
  return Math.sqrt(sum);
301
267
  }
268
+ type;
269
+ [size] = 2;
270
+ x = 0;
271
+ y = 0;
272
+ z;
273
+ w;
302
274
  /**
303
275
  * Constructs a new vector.
304
- * @param size The size of the vector (number of components).
305
276
  * @param x The x-component of the vector.
306
277
  * @param y The y-component of the vector (optional, defaults to x).
307
278
  * @param z The z-component of the vector (optional).
308
279
  * @param w The w-component of the vector (optional).
309
280
  */
310
- constructor(size, x = 0, y = x, z, w) {
311
- this.size = size;
281
+ constructor(x, y = x, z, w) {
282
+ for (let i = 0; i < arguments.length; i++) {
283
+ if (typeof arguments[i] !== "number") {
284
+ throw new TypeError(`${this.constructor.name} argument at index ${i} must be a number, but got ${typeof arguments[i]}`);
285
+ }
286
+ }
312
287
  this.x = x;
313
288
  this.y = y;
314
- this.z = z;
315
- this.w = w;
316
289
  }
317
290
  *[Symbol.iterator]() {
318
291
  yield this.x;
@@ -322,6 +295,9 @@ export class Vector {
322
295
  if (this.w !== undefined)
323
296
  yield this.w;
324
297
  }
298
+ get size() {
299
+ return this[size];
300
+ }
325
301
  toString() {
326
302
  return `vector${this.size}(${this.toArray().join(", ")})`;
327
303
  }
@@ -456,6 +432,7 @@ export class Vector2 extends Vector {
456
432
  // DO NOT USE, ONLY EXPOSED BECAUSE TS IS TRASH, THIS TYPE IS NOT GUARANTEED
457
433
  // TO EXIST, CHANGING IT WILL BREAK STUFF
458
434
  type = ClassTypes.Vector2;
435
+ [size] = 2;
459
436
  static Zero = new Vector2(0, 0);
460
437
  /**
461
438
  * Constructs a new 2D vector.
@@ -463,7 +440,18 @@ export class Vector2 extends Vector {
463
440
  * @param y The y-component of the vector (optional, defaults to x).
464
441
  */
465
442
  constructor(x, y = x) {
466
- super(2, x, y);
443
+ super(x, y);
444
+ }
445
+ /**
446
+ * Creates a new vector based on the provided parameters.
447
+ * @param x The x-component of the vector.
448
+ * @param y The y-component of the vector (optional, defaults to the value of x).
449
+ * @returns A new vector instance.
450
+ */
451
+ static create(x, y = x) {
452
+ if (typeof x === "object")
453
+ ({ x, y } = x);
454
+ return new this(x, y);
467
455
  }
468
456
  }
469
457
  /**
@@ -473,7 +461,8 @@ export class Vector3 extends Vector {
473
461
  // DO NOT USE, ONLY EXPOSED BECAUSE TS IS TRASH, THIS TYPE IS NOT GUARANTEED
474
462
  // TO EXIST, CHANGING IT WILL BREAK STUFF
475
463
  type = ClassTypes.Vector3;
476
- z;
464
+ [size] = 3;
465
+ z = 0;
477
466
  static Zero = new Vector3(0, 0, 0);
478
467
  static UnitX = new Vector3(1.0, 0.0, 0.0);
479
468
  static UnitY = new Vector3(0.0, 1.0, 0.0);
@@ -495,9 +484,21 @@ export class Vector3 extends Vector {
495
484
  * @param z The z-component of the vector (optional, defaults to y).
496
485
  */
497
486
  constructor(x, y = x, z = y) {
498
- super(3, x, y, z);
487
+ super(x, y, z);
499
488
  this.z = z;
500
489
  }
490
+ /**
491
+ * Creates a new vector based on the provided parameters.
492
+ * @param x The x-component of the vector.
493
+ * @param y The y-component of the vector (optional, defaults to the value of x).
494
+ * @param z The z-component of the vector (optional, defaults to the value of y).
495
+ * @returns A new vector instance.
496
+ */
497
+ static create(x, y = x, z = y) {
498
+ if (typeof x === "object")
499
+ ({ x, y, z = y } = x);
500
+ return new this(x, y, z);
501
+ }
501
502
  /**
502
503
  * @see Vector.addZ
503
504
  */
@@ -524,8 +525,9 @@ export class Vector4 extends Vector {
524
525
  // DO NOT USE, ONLY EXPOSED BECAUSE TS IS TRASH, THIS TYPE IS NOT GUARANTEED
525
526
  // TO EXIST, CHANGING IT WILL BREAK STUFF
526
527
  type = ClassTypes.Vector4;
527
- z;
528
- w;
528
+ [size] = 4;
529
+ z = 0;
530
+ w = 0;
529
531
  static Zero = new Vector4(0, 0, 0, 0);
530
532
  /**
531
533
  * Constructs a new 4D vector.
@@ -535,10 +537,23 @@ export class Vector4 extends Vector {
535
537
  * @param w The w-component of the vector (optional, defaults to z).
536
538
  */
537
539
  constructor(x, y = x, z = y, w = z) {
538
- super(4, x, y, z, w);
539
- this.z = z;
540
+ super(x, y, z, w);
541
+ this.z = w;
540
542
  this.w = w;
541
543
  }
544
+ /**
545
+ * Creates a new vector based on the provided parameters.
546
+ * @param x The x-component of the vector.
547
+ * @param y The y-component of the vector (optional, defaults to the value of x).
548
+ * @param z The z-component of the vector (optional, defaults to the value of y).
549
+ * @param w The w-component of the vector (optional, defaults to the value of z).
550
+ * @returns A new vector instance.
551
+ */
552
+ static create(x, y = x, z = y, w = z) {
553
+ if (typeof x === "object")
554
+ ({ x, y, z = y, w = z } = x);
555
+ return new this(x, y, z, w);
556
+ }
542
557
  /**
543
558
  * @see Vector.addZ
544
559
  */