@nativewrappers/common 0.0.50 → 0.0.51

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/Kvp.d.ts CHANGED
@@ -1,91 +1,77 @@
1
- export declare class Kvp {
1
+ interface Schema {
2
+ [key: string]: "string" | "number" | "float" | object;
3
+ }
4
+ export declare class Kvp<T extends Schema> {
5
+ private schema;
6
+ constructor(schema: T);
2
7
  /**
3
- * Sets the resource key to the specified value this is a blocking operation, if you're doing large write operations you should use [[setKvpAsync]] instead.
4
- * @param key the key string
5
- * @param value the value to set the key to
8
+ * Returns the type associated with a schema key.
6
9
  */
7
- setKvp<T = number | string>(key: string, value: T): void;
10
+ getType(key: keyof T): "string" | "number" | "float" | "object";
8
11
  /**
9
- * Sets the resource key to the specified value, this doesn't immediately write to disk and needs [[flush]] called afterwards.
10
- * @param key the key string
11
- * @param value the value to set the key to
12
+ * Returns the value associated with a key as a number.
12
13
  */
13
- setKvpAsync<T = number | string>(key: string, value: T): void;
14
+ getNumber(key: string): number;
14
15
  /**
15
- * Sets the specified key to the specified json value
16
- * This can error if given an invalid object
17
- * @param key the key string
18
- * @param value the value to set the key to
16
+ * Returns the value associated with a key as a float.
19
17
  */
20
- setKvpJson(key: string, value: any): void;
18
+ getFloat(key: string): number;
21
19
  /**
22
- * Gets the specified value for key
23
- * @param key the key of the value to get
24
- * @returns a string, or null if there is no value
20
+ * Returns the value associated with a key as a string.
25
21
  */
26
- getKvpString(key: string): string | null;
22
+ getString(key: string): string | null;
27
23
  /**
28
- * Gets the specified value for key
29
- * @param key the key of the value to get
30
- * @returns the value stored, as a number, or 0 if there is no value
24
+ * Returns the value associated with a key as a parsed JSON string.
31
25
  */
32
- getKvpNumber(key: string): number;
26
+ getJson<T>(key: string): T | null;
33
27
  /**
34
- * Gets the specified value for key
35
- * @param key the key of the value to get
36
- * @returns the value stored as a float, or 0.0 if there is no value
28
+ * Sets the value associated with a key as a number.
29
+ * @param async set the value using an async operation.
37
30
  */
38
- getKvpFloat(key: string): number;
39
- getKvpJson<T>(key: string): T;
31
+ setNumber(key: string, value: number, async?: boolean): void;
40
32
  /**
41
- * Deletes the specified value for key, this is a blocking operation, if you're deleting a bunch of keys you should use [[deleteAsync]]
42
- * @param key the key of the value to delete
33
+ * Sets the value associated with a key as a float.
34
+ * @param async set the value using an async operation.
43
35
  */
44
- delete(key: string): void;
36
+ setFloat(key: string, value: number, async?: boolean): void;
45
37
  /**
46
- * Deletes the specified resource keys value, this doesn't immediately write to disk and needs [[flush]] called afterwards.
47
- * @param key the key to delete
38
+ * Sets the value associated with a key as a string.
39
+ * @param async set the value using an async operation.
48
40
  */
49
- deleteAsync(key: string): void;
41
+ setString(key: string, value: string, async?: boolean): void;
50
42
  /**
51
- * Ensures that any previous async call is flushed to disk
43
+ * Sets the value associated with a key as a JSON string.
44
+ * @param async set the value using an async operation.
52
45
  */
53
- flush(): void;
54
- private handleKvp;
46
+ setJson(key: string, value: object, async?: boolean): void;
55
47
  /**
56
- * enumerates over any kvp prefixed with the prefix
57
- *
58
- * ```typescript
59
- * for (const value of Kvp.getKvpsAsString("native:")) {
60
- * console.log(value);
61
- * }
62
- * ```
63
- *
64
- * @param prefix the prefix to search for
48
+ * Returns the value associated with a key, determining the type using the declared Kvp schema.
65
49
  */
66
- getKvpsAsString(prefix: string): IterableIterator<string>;
50
+ get<K extends keyof T>(key: K): T[K] extends "number" | "float" ? number : T[K] extends object ? T[K] | null : string | null;
67
51
  /**
68
- * enumerates over any kvp prefixed with the prefix
69
- *
70
- * ```typescript
71
- * for (const value of Kvp.getKvpsAsNumber("native:")) {
72
- * console.log(value);
73
- * }
74
- * ```
75
- *
76
- * @param prefix the prefix to search for
52
+ * Sets the value associated with a key as a value, using its type from the declared Kvp stricture.
53
+ * @param async set the value using an async operation.
77
54
  */
78
- getKvpsAsNumber(prefix: string): IterableIterator<number>;
55
+ set<K extends string>(key: K extends keyof T ? K : never, value: T[K] extends "number" | "float" ? number : T[K] extends object ? T[K] | null : string | null, async?: boolean): void;
79
56
  /**
80
- * enumerates over any kvp prefixed with the prefix
81
- *
82
- * ```typescript
83
- * for (const value of Kvp.getKvpsAsFloat("native:")) {
84
- * console.log(value);
85
- * }
86
- * ```
57
+ * Deletes the specified value for key.
58
+ * @param async remove the value using an async operation
59
+ */
60
+ delete(key: string, async?: boolean): void;
61
+ /**
62
+ * Commits pending asynchronous operations to disk, ensuring data consistency.
87
63
  *
88
- * @param prefix the prefix to search for
64
+ * Should be called after calling set methods using the async flag.
65
+ */
66
+ flush(): void;
67
+ private getAllKeys;
68
+ /**
69
+ * Returns an array of keys which match or contain the given keys.
70
+ */
71
+ getKeys(prefix: string | string[]): string[];
72
+ /**
73
+ * Get all values from keys in an array as the specified type.
89
74
  */
90
- getKvpsAsFloat(prefix: string): IterableIterator<number>;
75
+ getValuesAsType(prefix: string[], type: "string" | "number" | "float" | "object"): unknown[];
91
76
  }
77
+ export {};
package/Kvp.js CHANGED
@@ -1,161 +1,167 @@
1
- /* eslint-disable @typescript-eslint/no-explicit-any */
2
1
  export class Kvp {
3
- // TODO: Find a way to do this without casting to any
2
+ schema;
3
+ constructor(schema) {
4
+ this.schema = { ...schema };
5
+ for (const key in this.schema) {
6
+ if (typeof this.schema[key] === "object")
7
+ this.schema[key] = Object;
8
+ }
9
+ }
4
10
  /**
5
- * Sets the resource key to the specified value this is a blocking operation, if you're doing large write operations you should use [[setKvpAsync]] instead.
6
- * @param key the key string
7
- * @param value the value to set the key to
11
+ * Returns the type associated with a schema key.
8
12
  */
9
- setKvp(key, value) {
10
- const type = typeof value;
11
- if (type === "string") {
12
- SetResourceKvp(key, value);
13
- }
14
- else {
15
- if (Number.isInteger(value)) {
16
- SetResourceKvpInt(key, value);
17
- }
18
- else {
19
- SetResourceKvpFloat(key, value);
20
- }
21
- }
13
+ getType(key) {
14
+ return typeof this.schema[key] === "object" ? "object" : this.schema[key];
22
15
  }
23
16
  /**
24
- * Sets the resource key to the specified value, this doesn't immediately write to disk and needs [[flush]] called afterwards.
25
- * @param key the key string
26
- * @param value the value to set the key to
17
+ * Returns the value associated with a key as a number.
27
18
  */
28
- setKvpAsync(key, value) {
29
- const type = typeof value;
30
- if (type === "string") {
31
- SetResourceKvpNoSync(key, value);
32
- }
33
- else {
34
- if (Number.isInteger(value)) {
35
- SetResourceKvpIntNoSync(key, value);
36
- }
37
- else {
38
- SetResourceKvpFloatNoSync(key, value);
39
- }
40
- }
19
+ getNumber(key) {
20
+ return GetResourceKvpInt(key);
41
21
  }
42
22
  /**
43
- * Sets the specified key to the specified json value
44
- * This can error if given an invalid object
45
- * @param key the key string
46
- * @param value the value to set the key to
23
+ * Returns the value associated with a key as a float.
47
24
  */
48
- setKvpJson(key, value) {
49
- const stringified = JSON.stringify(value);
50
- this.setKvp(key, stringified);
25
+ getFloat(key) {
26
+ return GetResourceKvpFloat(key);
51
27
  }
52
28
  /**
53
- * Gets the specified value for key
54
- * @param key the key of the value to get
55
- * @returns a string, or null if there is no value
29
+ * Returns the value associated with a key as a string.
56
30
  */
57
- getKvpString(key) {
31
+ getString(key) {
58
32
  return GetResourceKvpString(key);
59
33
  }
60
34
  /**
61
- * Gets the specified value for key
62
- * @param key the key of the value to get
63
- * @returns the value stored, as a number, or 0 if there is no value
35
+ * Returns the value associated with a key as a parsed JSON string.
64
36
  */
65
- getKvpNumber(key) {
66
- return GetResourceKvpInt(key);
37
+ getJson(key) {
38
+ const str = GetResourceKvpString(key);
39
+ return str ? JSON.parse(str) : null;
67
40
  }
68
41
  /**
69
- * Gets the specified value for key
70
- * @param key the key of the value to get
71
- * @returns the value stored as a float, or 0.0 if there is no value
42
+ * Sets the value associated with a key as a number.
43
+ * @param async set the value using an async operation.
72
44
  */
73
- getKvpFloat(key) {
74
- return GetResourceKvpFloat(key);
45
+ setNumber(key, value, async = false) {
46
+ return async
47
+ ? SetResourceKvpIntNoSync(key, value)
48
+ : SetResourceKvpInt(key, value);
49
+ }
50
+ /**
51
+ * Sets the value associated with a key as a float.
52
+ * @param async set the value using an async operation.
53
+ */
54
+ setFloat(key, value, async = false) {
55
+ return async
56
+ ? SetResourceKvpFloatNoSync(key, value)
57
+ : SetResourceKvpFloat(key, value);
75
58
  }
76
- getKvpJson(key) {
77
- const kvp = this.getKvpString(key);
78
- return JSON.parse(kvp || "{}");
59
+ /**
60
+ * Sets the value associated with a key as a string.
61
+ * @param async set the value using an async operation.
62
+ */
63
+ setString(key, value, async = false) {
64
+ return async
65
+ ? SetResourceKvpNoSync(key, value)
66
+ : SetResourceKvp(key, value);
79
67
  }
80
68
  /**
81
- * Deletes the specified value for key, this is a blocking operation, if you're deleting a bunch of keys you should use [[deleteAsync]]
82
- * @param key the key of the value to delete
69
+ * Sets the value associated with a key as a JSON string.
70
+ * @param async set the value using an async operation.
83
71
  */
84
- delete(key) {
85
- DeleteResourceKvp(key);
72
+ setJson(key, value, async = false) {
73
+ const str = JSON.stringify(value);
74
+ return async ? SetResourceKvpNoSync(key, str) : SetResourceKvp(key, str);
86
75
  }
87
76
  /**
88
- * Deletes the specified resource keys value, this doesn't immediately write to disk and needs [[flush]] called afterwards.
89
- * @param key the key to delete
77
+ * Returns the value associated with a key, determining the type using the declared Kvp schema.
90
78
  */
91
- deleteAsync(key) {
92
- DeleteResourceKvpNoSync(key);
79
+ get(key) {
80
+ const type = this.getType(key);
81
+ switch (type) {
82
+ case "number":
83
+ return this.getNumber(key);
84
+ case "float":
85
+ return this.getFloat(key);
86
+ case "object":
87
+ return this.getJson(key);
88
+ default:
89
+ return this.getString(key);
90
+ }
93
91
  }
94
92
  /**
95
- * Ensures that any previous async call is flushed to disk
93
+ * Sets the value associated with a key as a value, using its type from the declared Kvp stricture.
94
+ * @param async set the value using an async operation.
95
+ */
96
+ set(key, value, async = false) {
97
+ const type = this.getType(key);
98
+ const valueType = typeof value;
99
+ if (valueType !== type && type !== "float" && valueType === "number")
100
+ throw new Error(`Expected '${key}' to be type '${type}' but received '${valueType}'`);
101
+ switch (type) {
102
+ case "number":
103
+ return this.setNumber(key, value, async);
104
+ case "float":
105
+ return this.setFloat(key, value, async);
106
+ case "object":
107
+ return this.setJson(key, value, async);
108
+ default:
109
+ return this.setString(key, value, async);
110
+ }
111
+ }
112
+ /**
113
+ * Deletes the specified value for key.
114
+ * @param async remove the value using an async operation
115
+ */
116
+ delete(key, async = false) {
117
+ return async ? DeleteResourceKvpNoSync(key) : DeleteResourceKvp(key);
118
+ }
119
+ /**
120
+ * Commits pending asynchronous operations to disk, ensuring data consistency.
121
+ *
122
+ * Should be called after calling set methods using the async flag.
96
123
  */
97
124
  flush() {
98
125
  FlushResourceKvp();
99
126
  }
100
- *handleKvp(prefix, iterType) {
127
+ getAllKeys(prefix) {
128
+ const keys = [];
101
129
  const handle = StartFindKvp(prefix);
102
130
  if (handle === -1)
103
- return;
131
+ return keys;
104
132
  let key;
105
133
  do {
106
134
  key = FindKvp(handle);
107
- if (iterType === "string") {
108
- yield GetResourceKvpString(key);
109
- }
110
- else if (iterType === "number") {
111
- yield GetResourceKvpInt(key);
112
- }
113
- else if (iterType === "float") {
114
- yield GetResourceKvpFloat(key);
115
- }
135
+ if (key)
136
+ keys.push(key);
116
137
  } while (key);
117
138
  EndFindKvp(handle);
139
+ return keys;
118
140
  }
119
141
  /**
120
- * enumerates over any kvp prefixed with the prefix
121
- *
122
- * ```typescript
123
- * for (const value of Kvp.getKvpsAsString("native:")) {
124
- * console.log(value);
125
- * }
126
- * ```
127
- *
128
- * @param prefix the prefix to search for
129
- */
130
- getKvpsAsString(prefix) {
131
- return this.handleKvp(prefix, "string");
132
- }
133
- /**
134
- * enumerates over any kvp prefixed with the prefix
135
- *
136
- * ```typescript
137
- * for (const value of Kvp.getKvpsAsNumber("native:")) {
138
- * console.log(value);
139
- * }
140
- * ```
141
- *
142
- * @param prefix the prefix to search for
142
+ * Returns an array of keys which match or contain the given keys.
143
143
  */
144
- getKvpsAsNumber(prefix) {
145
- return this.handleKvp(prefix, "number");
144
+ getKeys(prefix) {
145
+ return typeof prefix === "string"
146
+ ? this.getAllKeys(prefix)
147
+ : prefix.flatMap((key) => this.getAllKeys(key));
146
148
  }
147
149
  /**
148
- * enumerates over any kvp prefixed with the prefix
149
- *
150
- * ```typescript
151
- * for (const value of Kvp.getKvpsAsFloat("native:")) {
152
- * console.log(value);
153
- * }
154
- * ```
155
- *
156
- * @param prefix the prefix to search for
150
+ * Get all values from keys in an array as the specified type.
157
151
  */
158
- getKvpsAsFloat(prefix) {
159
- return this.handleKvp(prefix, "float");
152
+ getValuesAsType(prefix, type) {
153
+ const values = this.getKeys(prefix);
154
+ return values.map((key) => {
155
+ switch (type) {
156
+ case "number":
157
+ return this.getNumber(key);
158
+ case "float":
159
+ return this.getFloat(key);
160
+ case "object":
161
+ return this.getJson(key);
162
+ default:
163
+ return this.getString(key);
164
+ }
165
+ });
160
166
  }
161
167
  }
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.50",
7
+ "version": "0.0.51",
8
8
  "repository": {
9
9
  "type": "git",
10
10
  "url": "https://github.com/nativewrappers/fivem.git"
package/utils/Vector.d.ts CHANGED
@@ -50,20 +50,20 @@ type InferVector<T> = T extends Vec4 | VectorN<4> ? Vector4 : T extends Vec3 | V
50
50
  /**
51
51
  * A base vector class inherited by all vector classes.
52
52
  */
53
- declare class Vector {
54
- protected static create(x: number | Vec, y?: number, z?: number, w?: number): Vector;
53
+ declare abstract class Vector {
54
+ protected static create<T>(this: T, x: number | Vec, y?: number, z?: number, w?: number): Vector4 | Vector3 | Vector2;
55
+ /**
56
+ * Creates a deep copy of the provided vector.
57
+ * @param obj The vector to clone.
58
+ * @returns A new vector instance that is a copy of the provided vector.
59
+ */
60
+ static clone<T extends VectorType, U extends VectorLike>(this: T, obj: U): Vector4 | Vector3 | Vector2;
55
61
  /**
56
62
  * Creates a vector from binary data in a MsgpackBuffer.
57
63
  * @param msgpackBuffer The buffer containing binary data.
58
64
  * @returns A new vector instance.
59
65
  */
60
66
  static fromBuffer<T extends VectorType>(this: T, { buffer, type }: MsgpackBuffer): InstanceType<T>;
61
- /**
62
- * Creates a deep copy of the provided vector.
63
- * @param obj The vector to clone.
64
- * @returns A new vector instance that is a copy of the provided vector.
65
- */
66
- static clone<T extends VectorType, U extends VectorLike>(this: T, obj: U): InstanceType<T>;
67
67
  /**
68
68
  * Performs an operation between a vector and either another vector or scalar value.
69
69
  * @param a - The first vector.
@@ -92,21 +92,21 @@ declare class Vector {
92
92
  * @param y - The value to add to the y-component.
93
93
  * @returns A new vector with the y-component incremented.
94
94
  */
95
- static addY<T extends VectorType, U extends VectorLike>(this: T, obj: U, y: number): Vector;
95
+ static addY<T extends VectorType, U extends VectorLike>(this: T, obj: U, y: number): U;
96
96
  /**
97
97
  * Adds a scalar value to the z-component of a vector.
98
98
  * @param obj - The vector.
99
99
  * @param z - The value to add to the z-component.
100
100
  * @returns A new vector with the z-component incremented.
101
101
  */
102
- static addZ<T extends VectorType, U extends Vec3 | Vec4>(this: T, obj: U, z: number): Vector;
102
+ static addZ<T extends VectorType, U extends Vec3 | Vec4>(this: T, obj: U, z: number): U;
103
103
  /**
104
104
  * Adds a scalar value to the w-component of a vector.
105
105
  * @param obj - The vector.
106
106
  * @param w - The value to add to the w-component.
107
107
  * @returns A new vector with the w-component incremented.
108
108
  */
109
- static addW<T extends VectorType, U extends Vec4>(this: T, obj: U, w: number): Vector;
109
+ static addW<T extends VectorType, U extends Vec4>(this: T, obj: U, w: number): U;
110
110
  /**
111
111
  * Subtracts one vector from another or subtracts a scalar value from a vector.
112
112
  * @param a - The vector.
@@ -259,7 +259,7 @@ declare class Vector {
259
259
  /**
260
260
  * @see Vector.addY
261
261
  */
262
- addY(y: number): Vector;
262
+ addY(y: number): this;
263
263
  /**
264
264
  * @see Vector.subtract
265
265
  */
@@ -363,7 +363,7 @@ export declare class Vector3 extends Vector implements Vec3 {
363
363
  /**
364
364
  * @see Vector.addZ
365
365
  */
366
- addZ(z: number): Vector;
366
+ addZ(z: number): this;
367
367
  /**
368
368
  * @see Vector.crossProduct
369
369
  */
@@ -402,11 +402,11 @@ export declare class Vector4 extends Vector {
402
402
  /**
403
403
  * @see Vector.addZ
404
404
  */
405
- addZ(z: number): Vector;
405
+ addZ(z: number): this;
406
406
  /**
407
407
  * @see Vector.addW
408
408
  */
409
- addW(w: number): Vector;
409
+ addW(w: number): this;
410
410
  /**
411
411
  * @see Vector.crossProduct
412
412
  */
package/utils/Vector.js CHANGED
@@ -10,8 +10,28 @@ const size = Symbol("size");
10
10
  class Vector {
11
11
  static create(x, y = x, z, w) {
12
12
  if (typeof x === "object")
13
- ({ x, y, z = y, w = z } = x);
14
- return new this(x, y, z, w);
13
+ ({ x, y, z, w } = x);
14
+ const size = (this instanceof Vector && this.size) ||
15
+ [x, y, z, w].filter((arg) => arg !== undefined).length;
16
+ switch (size) {
17
+ case 1:
18
+ case 2:
19
+ return new Vector2(x, y);
20
+ case 3:
21
+ return new Vector3(x, y, z);
22
+ case 4:
23
+ return new Vector4(x, y, z, w);
24
+ default:
25
+ throw new Error(`Cannot instantiate Vector with size of ${size}.`);
26
+ }
27
+ }
28
+ /**
29
+ * Creates a deep copy of the provided vector.
30
+ * @param obj The vector to clone.
31
+ * @returns A new vector instance that is a copy of the provided vector.
32
+ */
33
+ static clone(obj) {
34
+ return this.create(obj);
15
35
  }
16
36
  /**
17
37
  * Creates a vector from binary data in a MsgpackBuffer.
@@ -26,14 +46,6 @@ class Vector {
26
46
  arr[i] = Number(buffer.readFloatLE(i * 4).toPrecision(7));
27
47
  return this.fromArray(arr);
28
48
  }
29
- /**
30
- * Creates a deep copy of the provided vector.
31
- * @param obj The vector to clone.
32
- * @returns A new vector instance that is a copy of the provided vector.
33
- */
34
- static clone(obj) {
35
- return this.create(obj);
36
- }
37
49
  /**
38
50
  * Performs an operation between a vector and either another vector or scalar value.
39
51
  * @param a - The first vector.