@nativewrappers/common 0.0.47 → 0.0.49

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.47",
7
+ "version": "0.0.49",
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
+ readonly type: unknown;
209
+ readonly [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,7 @@ 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;
318
311
  static readonly Zero: Vector2;
319
312
  /**
320
313
  * Constructs a new 2D vector.
@@ -322,12 +315,19 @@ export declare class Vector2 extends Vector {
322
315
  * @param y The y-component of the vector (optional, defaults to x).
323
316
  */
324
317
  constructor(x: number, y?: number);
318
+ /**
319
+ * Creates a new vector based on the provided parameters.
320
+ * @param x The x-component of the vector.
321
+ * @param y The y-component of the vector (optional, defaults to the value of x).
322
+ * @returns A new vector instance.
323
+ */
324
+ protected static create(x: number | Vec, y?: number): Vector2;
325
325
  }
326
326
  /**
327
327
  * Represents a 3-dimensional vector.
328
328
  */
329
329
  export declare class Vector3 extends Vector implements Vec3 {
330
- type: ClassTypes;
330
+ readonly type = ClassTypes.Vector3;
331
331
  z: number;
332
332
  static readonly Zero: Vector3;
333
333
  static readonly UnitX: Vector3;
@@ -350,10 +350,18 @@ export declare class Vector3 extends Vector implements Vec3 {
350
350
  * @param z The z-component of the vector (optional, defaults to y).
351
351
  */
352
352
  constructor(x: number, y?: number, z?: number);
353
+ /**
354
+ * Creates a new vector based on the provided parameters.
355
+ * @param x The x-component of the vector.
356
+ * @param y The y-component of the vector (optional, defaults to the value of x).
357
+ * @param z The z-component of the vector (optional, defaults to the value of y).
358
+ * @returns A new vector instance.
359
+ */
360
+ protected static create(x: number | Vec, y?: number, z?: number): Vector3;
353
361
  /**
354
362
  * @see Vector.addZ
355
363
  */
356
- addZ(z: number): InferVector<this>;
364
+ addZ(z: number): Vector;
357
365
  /**
358
366
  * @see Vector.crossProduct
359
367
  */
@@ -367,7 +375,7 @@ export declare class Vector3 extends Vector implements Vec3 {
367
375
  * Represents a 4-dimensional vector.
368
376
  */
369
377
  export declare class Vector4 extends Vector {
370
- type: ClassTypes;
378
+ readonly type = ClassTypes.Vector4;
371
379
  z: number;
372
380
  w: number;
373
381
  static readonly Zero: Vector4;
@@ -379,14 +387,23 @@ export declare class Vector4 extends Vector {
379
387
  * @param w The w-component of the vector (optional, defaults to z).
380
388
  */
381
389
  constructor(x: number, y?: number, z?: number, w?: number);
390
+ /**
391
+ * Creates a new vector based on the provided parameters.
392
+ * @param x The x-component of the vector.
393
+ * @param y The y-component of the vector (optional, defaults to the value of x).
394
+ * @param z The z-component of the vector (optional, defaults to the value of y).
395
+ * @param w The w-component of the vector (optional, defaults to the value of z).
396
+ * @returns A new vector instance.
397
+ */
398
+ protected static create(x: number | Vec, y?: number, z?: number, w?: number): Vector4;
382
399
  /**
383
400
  * @see Vector.addZ
384
401
  */
385
- addZ(z: number): InferVector<this>;
402
+ addZ(z: number): Vector;
386
403
  /**
387
404
  * @see Vector.addW
388
405
  */
389
- addW(w: number): InferVector<this>;
406
+ addW(w: number): Vector;
390
407
  /**
391
408
  * @see Vector.crossProduct
392
409
  */
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,35 @@ 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;
289
+ if (z !== undefined) {
290
+ this.z = z;
291
+ ++this[size];
292
+ }
293
+ if (w !== undefined) {
294
+ this.w = w;
295
+ ++this[size];
296
+ }
316
297
  }
317
298
  *[Symbol.iterator]() {
318
299
  yield this.x;
@@ -322,6 +303,9 @@ export class Vector {
322
303
  if (this.w !== undefined)
323
304
  yield this.w;
324
305
  }
306
+ get size() {
307
+ return this[size];
308
+ }
325
309
  toString() {
326
310
  return `vector${this.size}(${this.toArray().join(", ")})`;
327
311
  }
@@ -463,7 +447,18 @@ export class Vector2 extends Vector {
463
447
  * @param y The y-component of the vector (optional, defaults to x).
464
448
  */
465
449
  constructor(x, y = x) {
466
- super(2, x, y);
450
+ super(x, y);
451
+ }
452
+ /**
453
+ * Creates a new vector based on the provided parameters.
454
+ * @param x The x-component of the vector.
455
+ * @param y The y-component of the vector (optional, defaults to the value of x).
456
+ * @returns A new vector instance.
457
+ */
458
+ static create(x, y = x) {
459
+ if (typeof x === "object")
460
+ ({ x, y } = x);
461
+ return new this(x, y);
467
462
  }
468
463
  }
469
464
  /**
@@ -473,7 +468,7 @@ export class Vector3 extends Vector {
473
468
  // DO NOT USE, ONLY EXPOSED BECAUSE TS IS TRASH, THIS TYPE IS NOT GUARANTEED
474
469
  // TO EXIST, CHANGING IT WILL BREAK STUFF
475
470
  type = ClassTypes.Vector3;
476
- z;
471
+ z = 0;
477
472
  static Zero = new Vector3(0, 0, 0);
478
473
  static UnitX = new Vector3(1.0, 0.0, 0.0);
479
474
  static UnitY = new Vector3(0.0, 1.0, 0.0);
@@ -495,8 +490,19 @@ export class Vector3 extends Vector {
495
490
  * @param z The z-component of the vector (optional, defaults to y).
496
491
  */
497
492
  constructor(x, y = x, z = y) {
498
- super(3, x, y, z);
499
- this.z = z;
493
+ super(x, y, z);
494
+ }
495
+ /**
496
+ * Creates a new vector based on the provided parameters.
497
+ * @param x The x-component of the vector.
498
+ * @param y The y-component of the vector (optional, defaults to the value of x).
499
+ * @param z The z-component of the vector (optional, defaults to the value of y).
500
+ * @returns A new vector instance.
501
+ */
502
+ static create(x, y = x, z = y) {
503
+ if (typeof x === "object")
504
+ ({ x, y, z = y } = x);
505
+ return new this(x, y, z);
500
506
  }
501
507
  /**
502
508
  * @see Vector.addZ
@@ -524,8 +530,8 @@ export class Vector4 extends Vector {
524
530
  // DO NOT USE, ONLY EXPOSED BECAUSE TS IS TRASH, THIS TYPE IS NOT GUARANTEED
525
531
  // TO EXIST, CHANGING IT WILL BREAK STUFF
526
532
  type = ClassTypes.Vector4;
527
- z;
528
- w;
533
+ z = 0;
534
+ w = 0;
529
535
  static Zero = new Vector4(0, 0, 0, 0);
530
536
  /**
531
537
  * Constructs a new 4D vector.
@@ -535,9 +541,20 @@ export class Vector4 extends Vector {
535
541
  * @param w The w-component of the vector (optional, defaults to z).
536
542
  */
537
543
  constructor(x, y = x, z = y, w = z) {
538
- super(4, x, y, z, w);
539
- this.z = z;
540
- this.w = w;
544
+ super(x, y, z, w);
545
+ }
546
+ /**
547
+ * Creates a new vector based on the provided parameters.
548
+ * @param x The x-component of the vector.
549
+ * @param y The y-component of the vector (optional, defaults to the value of x).
550
+ * @param z The z-component of the vector (optional, defaults to the value of y).
551
+ * @param w The w-component of the vector (optional, defaults to the value of z).
552
+ * @returns A new vector instance.
553
+ */
554
+ static create(x, y = x, z = y, w = z) {
555
+ if (typeof x === "object")
556
+ ({ x, y, z = y, w = z } = x);
557
+ return new this(x, y, z, w);
541
558
  }
542
559
  /**
543
560
  * @see Vector.addZ