@armoury/fivem-framework-common 1.0.55 → 1.0.62

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.
@@ -3,32 +3,51 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Command = Command;
4
4
  exports.Commands = Commands;
5
5
  const decorator_utils_1 = require("./decorator.utils");
6
- // TODO: adminLevelRequired is not supposed to be here. Move it to @armoury/fivem-gamemode
7
6
  function Command(data) {
8
7
  return function (target, propertyKey) {
9
8
  Reflect.defineMetadata(`arm_command_${propertyKey}`, data, target);
10
9
  };
11
10
  }
12
11
  function Commands(target, _prototype, providers) {
13
- decorator_utils_1.DecoratorUtils.map(target, _prototype, providers ?? [])
14
- .filter((mapping) => mapping.key.startsWith('arm_command_'))
15
- .forEach((mapping) => {
16
- const rawCommandName = mapping.key.split('_').slice(2).join('_');
12
+ const mappings = decorator_utils_1.DecoratorUtils.map(target, _prototype, providers ?? []);
13
+ const isServer = Cfx.Client.IsDuplicityVersion();
14
+ for (const mapping of mappings) {
15
+ if (!mapping.key.startsWith('arm_command_')) {
16
+ continue;
17
+ }
18
+ const rawCommandName = mapping.key.substring(12);
17
19
  const commandName = rawCommandName.toLowerCase() + (mapping.value?.suffix || '');
18
- if (!Cfx.Client.IsDuplicityVersion()) {
19
- Cfx.Client.RegisterCommand(commandName, (_source, args, _raw) => {
20
- mapping.target[rawCommandName].apply(mapping.target, args);
20
+ const instance = mapping.target;
21
+ const handler = instance[rawCommandName];
22
+ if (!isServer) {
23
+ Cfx.Client.RegisterCommand(commandName, (_source, args) => {
24
+ handler.apply(instance, args);
21
25
  }, false);
26
+ continue;
22
27
  }
23
- else {
24
- Cfx.Server.RegisterCommand(commandName, (source, args, _raw) => {
25
- // TODO: This conditional is not generic enough. This conditional should actually check a function given through an injection token.
26
- if (mapping.value?.adminLevelRequired && Number(Cfx.exports['authentication'].getPlayerInfo(source, 'adminLevel')) < mapping.value?.adminLevelRequired) {
27
- // TODO: Add error chat message OR some kind of visual notice here
28
- return;
29
- }
30
- mapping.target[rawCommandName].apply(mapping.target, [source, ...args]);
31
- }, false);
28
+ const configuredPermissions = mapping.value?.permissions;
29
+ let permissions = null;
30
+ if (configuredPermissions != null) {
31
+ if (Array.isArray(configuredPermissions)) {
32
+ permissions = configuredPermissions.slice();
33
+ }
34
+ else if (typeof configuredPermissions === 'string') {
35
+ permissions = [configuredPermissions];
36
+ }
37
+ else {
38
+ console.error(`Invalid permissions specified for command ${commandName}.`);
39
+ continue;
40
+ }
32
41
  }
33
- });
42
+ Cfx.Server.RegisterCommand(commandName, (source, args) => {
43
+ if (permissions) {
44
+ for (let i = 0; i < permissions.length; i++) {
45
+ if (!Cfx.Server.IsPlayerAceAllowed(source, permissions[i])) {
46
+ return;
47
+ }
48
+ }
49
+ }
50
+ handler.call(instance, source, ...args);
51
+ }, false);
52
+ }
34
53
  }
@@ -1,30 +1,49 @@
1
- import { DecoratorUtils } from "./decorator.utils";
2
- // TODO: adminLevelRequired is not supposed to be here. Move it to @armoury/fivem-gamemode
1
+ import { DecoratorUtils } from './decorator.utils';
3
2
  export function Command(data) {
4
3
  return function (target, propertyKey) {
5
4
  Reflect.defineMetadata(`arm_command_${propertyKey}`, data, target);
6
5
  };
7
6
  }
8
7
  export function Commands(target, _prototype, providers) {
9
- DecoratorUtils.map(target, _prototype, providers ?? [])
10
- .filter((mapping) => mapping.key.startsWith('arm_command_'))
11
- .forEach((mapping) => {
12
- const rawCommandName = mapping.key.split('_').slice(2).join('_');
8
+ const mappings = DecoratorUtils.map(target, _prototype, providers ?? []);
9
+ const isServer = Cfx.Client.IsDuplicityVersion();
10
+ for (const mapping of mappings) {
11
+ if (!mapping.key.startsWith('arm_command_')) {
12
+ continue;
13
+ }
14
+ const rawCommandName = mapping.key.substring(12);
13
15
  const commandName = rawCommandName.toLowerCase() + (mapping.value?.suffix || '');
14
- if (!Cfx.Client.IsDuplicityVersion()) {
15
- Cfx.Client.RegisterCommand(commandName, (_source, args, _raw) => {
16
- mapping.target[rawCommandName].apply(mapping.target, args);
16
+ const instance = mapping.target;
17
+ const handler = instance[rawCommandName];
18
+ if (!isServer) {
19
+ Cfx.Client.RegisterCommand(commandName, (_source, args) => {
20
+ handler.apply(instance, args);
17
21
  }, false);
22
+ continue;
18
23
  }
19
- else {
20
- Cfx.Server.RegisterCommand(commandName, (source, args, _raw) => {
21
- // TODO: This conditional is not generic enough. This conditional should actually check a function given through an injection token.
22
- if (mapping.value?.adminLevelRequired && Number(Cfx.exports['authentication'].getPlayerInfo(source, 'adminLevel')) < mapping.value?.adminLevelRequired) {
23
- // TODO: Add error chat message OR some kind of visual notice here
24
- return;
25
- }
26
- mapping.target[rawCommandName].apply(mapping.target, [source, ...args]);
27
- }, false);
24
+ const configuredPermissions = mapping.value?.permissions;
25
+ let permissions = null;
26
+ if (configuredPermissions != null) {
27
+ if (Array.isArray(configuredPermissions)) {
28
+ permissions = configuredPermissions.slice();
29
+ }
30
+ else if (typeof configuredPermissions === 'string') {
31
+ permissions = [configuredPermissions];
32
+ }
33
+ else {
34
+ console.error(`Invalid permissions specified for command ${commandName}.`);
35
+ continue;
36
+ }
28
37
  }
29
- });
38
+ Cfx.Server.RegisterCommand(commandName, (source, args) => {
39
+ if (permissions) {
40
+ for (let i = 0; i < permissions.length; i++) {
41
+ if (!Cfx.Server.IsPlayerAceAllowed(source, permissions[i])) {
42
+ return;
43
+ }
44
+ }
45
+ }
46
+ handler.call(instance, source, ...args);
47
+ }, false);
48
+ }
30
49
  }
@@ -1,5 +1,5 @@
1
1
  export declare function Command(data?: {
2
- adminLevelRequired?: number;
2
+ permissions?: string[] | string;
3
3
  suffix?: string;
4
4
  }): (target: any, propertyKey: string) => void;
5
5
  export declare function Commands(target: any, _prototype: any, providers?: Object[]): void;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@armoury/fivem-framework-common",
3
- "version": "1.0.55",
3
+ "version": "1.0.62",
4
4
  "description": "Shared APIs for the Armoury FiveM framework",
5
5
  "main": "dist/cjs/common/index.js",
6
6
  "module": "dist/esm/common/index.js",