@nativewrappers/common 0.0.63 → 0.0.65
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/Command.js +18 -4
- package/GlobalData.d.ts +1 -0
- package/GlobalData.js +1 -0
- package/Kvp.d.ts +1 -1
- package/decors/Events.d.ts +41 -1
- package/decors/Events.js +40 -14
- package/package.json +1 -2
package/Command.js
CHANGED
|
@@ -3,6 +3,23 @@ const commands = [];
|
|
|
3
3
|
if (GlobalData.IS_SERVER) {
|
|
4
4
|
on("playerJoining", () => emitNet("chat:addSuggestions", source, commands));
|
|
5
5
|
}
|
|
6
|
+
function registerCommand(name, commandHandler, restricted) {
|
|
7
|
+
if (Array.isArray(name))
|
|
8
|
+
return name.forEach((name) => registerCommand(name, commandHandler, restricted));
|
|
9
|
+
RegisterCommand(name, commandHandler, !!restricted);
|
|
10
|
+
if (GlobalData.IS_CLIENT)
|
|
11
|
+
return;
|
|
12
|
+
const ace = `command.${name}`;
|
|
13
|
+
if (typeof restricted === "string") {
|
|
14
|
+
if (IsPrincipalAceAllowed(restricted, ace))
|
|
15
|
+
return;
|
|
16
|
+
return ExecuteCommand(`add_ace ${restricted} ${ace} allow`);
|
|
17
|
+
}
|
|
18
|
+
if (Array.isArray(restricted)) {
|
|
19
|
+
restricted.forEach((principal) => !IsPrincipalAceAllowed(principal, ace) &&
|
|
20
|
+
ExecuteCommand(`add_ace ${restricted} ${ace} allow`));
|
|
21
|
+
}
|
|
22
|
+
}
|
|
6
23
|
export class Command {
|
|
7
24
|
name;
|
|
8
25
|
help;
|
|
@@ -12,12 +29,9 @@ export class Command {
|
|
|
12
29
|
this.name = name;
|
|
13
30
|
this.help = help;
|
|
14
31
|
this.params = params;
|
|
15
|
-
const commandHandler = (source, args, raw) => this.call(source, args, raw);
|
|
16
32
|
this.#handler = handler;
|
|
17
33
|
this.name = `/${name}`;
|
|
18
|
-
|
|
19
|
-
? RegisterCommand(name, commandHandler, !!restricted)
|
|
20
|
-
: name.forEach((name) => RegisterCommand(name, commandHandler, !!restricted));
|
|
34
|
+
registerCommand(name, (source, args, raw) => this.call(source, args, raw), restricted);
|
|
21
35
|
if (params) {
|
|
22
36
|
params.forEach((param) => {
|
|
23
37
|
if (param.type)
|
package/GlobalData.d.ts
CHANGED
package/GlobalData.js
CHANGED
package/Kvp.d.ts
CHANGED
|
@@ -19,7 +19,7 @@ export declare class Kvp<Schema extends KvpSchema> {
|
|
|
19
19
|
/**
|
|
20
20
|
* Returns the value associated with a key as a parsed JSON string.
|
|
21
21
|
*/
|
|
22
|
-
getJson<K extends string, O = KvpObject<K>>(key: K extends ValidJsonKey<O> ? K : never): O extends string ? Schema[O] : null;
|
|
22
|
+
getJson<K extends string, O = KvpObject<K>>(key: K extends ValidJsonKey<O> ? K : never): (O extends string ? Schema[O] : null) | null;
|
|
23
23
|
/**
|
|
24
24
|
* Sets the value associated with a key as a number.
|
|
25
25
|
* @param async set the value using an async operation.
|
package/decors/Events.d.ts
CHANGED
|
@@ -4,11 +4,51 @@ export declare enum ConVarType {
|
|
|
4
4
|
Float = 2,
|
|
5
5
|
Boolean = 3
|
|
6
6
|
}
|
|
7
|
+
/**
|
|
8
|
+
* Disables pretty printing in error messages
|
|
9
|
+
*/
|
|
10
|
+
export declare const DisablePrettyPrint: () => boolean;
|
|
7
11
|
export declare function Exports(exportName: string): (originalMethod: any, context: ClassMethodDecoratorContext) => void;
|
|
12
|
+
/**
|
|
13
|
+
* Registers the Event call for {@link eventName} to this method.
|
|
14
|
+
*
|
|
15
|
+
* This has internal pretty-printing to make errors easier to track, if
|
|
16
|
+
* you want to disable this you will need to call {@link DisablePrettyPrint}, or if you're
|
|
17
|
+
* using esbuild you can add `REMOVE_EVENT_LOG` to your drop label {@link https://esbuild.github.io/api/#drop-labels}
|
|
18
|
+
*
|
|
19
|
+
* @param eventName the event to bind to
|
|
20
|
+
*/
|
|
8
21
|
export declare function Event(eventName: string): (originalMethod: any, context: ClassMethodDecoratorContext) => void;
|
|
22
|
+
/**
|
|
23
|
+
* Registers the Net Event call for {@link eventName} to this method
|
|
24
|
+
*
|
|
25
|
+
*
|
|
26
|
+
* This has internal pretty-printing to make errors easier to track, if
|
|
27
|
+
* you want to disable this you will need to call {@link DisablePrettyPrint}, or if you're
|
|
28
|
+
* using esbuild you can add `REMOVE_EVENT_LOG` to your drop label {@link https://esbuild.github.io/api/#drop-labels}
|
|
29
|
+
*
|
|
30
|
+
* @param eventName the event to bind this net event to
|
|
31
|
+
* @param remoteOnly if the event should only accept remote calls, if set to true it will ignore any local call via `emit`, defaults to true
|
|
32
|
+
*/
|
|
9
33
|
export declare function NetEvent(eventName: string, remoteOnly?: boolean): (originalMethod: any, context: ClassMethodDecoratorContext) => void;
|
|
34
|
+
/**
|
|
35
|
+
* Registers the NUI Event call for {eventName} to this method, the function signature
|
|
36
|
+
* should be (data: unknown, cb: (data?: any) => void) => void
|
|
37
|
+
* You shoud always execute `cb` with 'ok' if you don't want to send data back to
|
|
38
|
+
* the UI, otherwise you'll cause a network error for the `fetch` request
|
|
39
|
+
* @param eventName the event this will listen for
|
|
40
|
+
*/
|
|
10
41
|
export declare function NuiEvent(eventName: string): (originalMethod: any, context: ClassMethodDecoratorContext) => void;
|
|
11
42
|
type DeserializeFn<T> = (data: T) => unknown;
|
|
12
|
-
|
|
43
|
+
/**
|
|
44
|
+
* Gets the specified `ConVar`s value, this will bind to the param.
|
|
45
|
+
* @param name the convar name
|
|
46
|
+
* @param is_floating_point if the convar is floating point, this should be explicitly set to true if your convar will be a float
|
|
47
|
+
*/
|
|
48
|
+
export declare function ConVar<T>(name: string, is_floating_point?: boolean, deserialize?: DeserializeFn<T>): (_initialValue: any, context: ClassFieldDecoratorContext, ..._args: any[]) => void;
|
|
49
|
+
/**
|
|
50
|
+
* Gets called per server/client tick, this is asyncronous though, if you await
|
|
51
|
+
* in it, it will not be called until whatever was being awaited resolves.
|
|
52
|
+
*/
|
|
13
53
|
export declare function SetTick(): (originalMethod: any, context: ClassMethodDecoratorContext) => void;
|
|
14
54
|
export {};
|
package/decors/Events.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { GlobalData } from "../GlobalData";
|
|
1
2
|
export var ConVarType;
|
|
2
3
|
(function (ConVarType) {
|
|
3
4
|
ConVarType[ConVarType["String"] = 0] = "String";
|
|
@@ -5,6 +6,10 @@ export var ConVarType;
|
|
|
5
6
|
ConVarType[ConVarType["Float"] = 2] = "Float";
|
|
6
7
|
ConVarType[ConVarType["Boolean"] = 3] = "Boolean";
|
|
7
8
|
})(ConVarType || (ConVarType = {}));
|
|
9
|
+
/**
|
|
10
|
+
* Disables pretty printing in error messages
|
|
11
|
+
*/
|
|
12
|
+
export const DisablePrettyPrint = () => (GlobalData.EnablePrettyPrint = false);
|
|
8
13
|
// TODO: Have a way to clean all of this up (maybe hook Symbol.disposable
|
|
9
14
|
// somehow?)
|
|
10
15
|
/*
|
|
@@ -23,8 +28,14 @@ export function Exports(exportName) {
|
|
|
23
28
|
});
|
|
24
29
|
};
|
|
25
30
|
}
|
|
26
|
-
|
|
27
|
-
* Registers the Event call for {eventName} to this method
|
|
31
|
+
/**
|
|
32
|
+
* Registers the Event call for {@link eventName} to this method.
|
|
33
|
+
*
|
|
34
|
+
* This has internal pretty-printing to make errors easier to track, if
|
|
35
|
+
* you want to disable this you will need to call {@link DisablePrettyPrint}, or if you're
|
|
36
|
+
* using esbuild you can add `REMOVE_EVENT_LOG` to your drop label {@link https://esbuild.github.io/api/#drop-labels}
|
|
37
|
+
*
|
|
38
|
+
* @param eventName the event to bind to
|
|
28
39
|
*/
|
|
29
40
|
export function Event(eventName) {
|
|
30
41
|
return function actualDecorator(originalMethod, context) {
|
|
@@ -39,6 +50,8 @@ export function Event(eventName) {
|
|
|
39
50
|
}
|
|
40
51
|
catch (e) {
|
|
41
52
|
REMOVE_EVENT_LOG: {
|
|
53
|
+
if (!GlobalData.EnablePrettyPrint)
|
|
54
|
+
return;
|
|
42
55
|
console.error(`------- EVENT ERROR --------`);
|
|
43
56
|
console.error(`Call to ${eventName} errored`);
|
|
44
57
|
console.error(`Data: ${JSON.stringify(args)}`);
|
|
@@ -50,10 +63,18 @@ export function Event(eventName) {
|
|
|
50
63
|
});
|
|
51
64
|
};
|
|
52
65
|
}
|
|
53
|
-
|
|
54
|
-
* Registers the Net Event call for {eventName} to this method
|
|
66
|
+
/**
|
|
67
|
+
* Registers the Net Event call for {@link eventName} to this method
|
|
68
|
+
*
|
|
69
|
+
*
|
|
70
|
+
* This has internal pretty-printing to make errors easier to track, if
|
|
71
|
+
* you want to disable this you will need to call {@link DisablePrettyPrint}, or if you're
|
|
72
|
+
* using esbuild you can add `REMOVE_EVENT_LOG` to your drop label {@link https://esbuild.github.io/api/#drop-labels}
|
|
73
|
+
*
|
|
74
|
+
* @param eventName the event to bind this net event to
|
|
75
|
+
* @param remoteOnly if the event should only accept remote calls, if set to true it will ignore any local call via `emit`, defaults to true
|
|
55
76
|
*/
|
|
56
|
-
export function NetEvent(eventName, remoteOnly =
|
|
77
|
+
export function NetEvent(eventName, remoteOnly = true) {
|
|
57
78
|
return function actualDecorator(originalMethod, context) {
|
|
58
79
|
if (context.private) {
|
|
59
80
|
throw new Error("NetEvent does not work on private methods, please mark the method as public");
|
|
@@ -72,6 +93,8 @@ export function NetEvent(eventName, remoteOnly = false) {
|
|
|
72
93
|
}
|
|
73
94
|
catch (e) {
|
|
74
95
|
REMOVE_NET_EVENT_LOG: {
|
|
96
|
+
if (!GlobalData.EnablePrettyPrint)
|
|
97
|
+
return;
|
|
75
98
|
console.error(`------- NET EVENT ERROR --------`);
|
|
76
99
|
console.error(`Call to ${eventName} errored`);
|
|
77
100
|
console.error(`Caller: ${src}`);
|
|
@@ -84,11 +107,12 @@ export function NetEvent(eventName, remoteOnly = false) {
|
|
|
84
107
|
});
|
|
85
108
|
};
|
|
86
109
|
}
|
|
87
|
-
|
|
110
|
+
/**
|
|
88
111
|
* Registers the NUI Event call for {eventName} to this method, the function signature
|
|
89
112
|
* should be (data: unknown, cb: (data?: any) => void) => void
|
|
90
|
-
* You shoud always execute `cb` with '' if you don't want to send data back to
|
|
113
|
+
* You shoud always execute `cb` with 'ok' if you don't want to send data back to
|
|
91
114
|
* the UI, otherwise you'll cause a network error for the `fetch` request
|
|
115
|
+
* @param eventName the event this will listen for
|
|
92
116
|
*/
|
|
93
117
|
export function NuiEvent(eventName) {
|
|
94
118
|
return function actualDecorator(originalMethod, context) {
|
|
@@ -113,19 +137,21 @@ const get_convar_fn = (con_var_type) => {
|
|
|
113
137
|
return GetConvarFloat;
|
|
114
138
|
case ConVarType.Boolean:
|
|
115
139
|
return GetConvarBool;
|
|
140
|
+
// needed so typescript wont complain about "unreachable code" for the error below
|
|
141
|
+
default:
|
|
116
142
|
}
|
|
117
143
|
// never guess people wont manage to hit this
|
|
118
144
|
throw new Error("Got invalid ConVarType");
|
|
119
145
|
};
|
|
120
|
-
|
|
121
|
-
* Gets the specified `ConVar`s value,
|
|
122
|
-
*
|
|
123
|
-
*
|
|
146
|
+
/**
|
|
147
|
+
* Gets the specified `ConVar`s value, this will bind to the param.
|
|
148
|
+
* @param name the convar name
|
|
149
|
+
* @param is_floating_point if the convar is floating point, this should be explicitly set to true if your convar will be a float
|
|
124
150
|
*/
|
|
125
151
|
export function ConVar(name, is_floating_point, deserialize) {
|
|
126
152
|
// the implementation shows that this will be _initialValue, but it doesn't
|
|
127
153
|
// seem to actually be???
|
|
128
|
-
return function actualDecorator(_initialValue, context, ...
|
|
154
|
+
return function actualDecorator(_initialValue, context, ..._args) {
|
|
129
155
|
if (context.private) {
|
|
130
156
|
throw new Error("ConVar does not work on private types, please mark the field as public");
|
|
131
157
|
}
|
|
@@ -166,13 +192,13 @@ export function ConVar(name, is_floating_point, deserialize) {
|
|
|
166
192
|
return deserialize ? deserialize(data) : data;
|
|
167
193
|
};
|
|
168
194
|
Reflect.set(t, context.name, get_convar_value());
|
|
169
|
-
AddConvarChangeListener(name, (
|
|
195
|
+
AddConvarChangeListener(name, () => {
|
|
170
196
|
Reflect.set(t, context.name, get_convar_value());
|
|
171
197
|
});
|
|
172
198
|
});
|
|
173
199
|
};
|
|
174
200
|
}
|
|
175
|
-
|
|
201
|
+
/**
|
|
176
202
|
* Gets called per server/client tick, this is asyncronous though, if you await
|
|
177
203
|
* in it, it will not be called until whatever was being awaited resolves.
|
|
178
204
|
*/
|
package/package.json
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nativewrappers/common",
|
|
3
3
|
"description": "Native wrappers and utilities for use with Cfx.re's scripting runtimes.",
|
|
4
|
-
"author": "Remco Troost <d0p3t>",
|
|
5
4
|
"license": "MIT",
|
|
6
5
|
"type": "module",
|
|
7
|
-
"version": "0.0.
|
|
6
|
+
"version": "0.0.65",
|
|
8
7
|
"repository": {
|
|
9
8
|
"type": "git",
|
|
10
9
|
"url": "https://github.com/nativewrappers/nativewrappers.git"
|