From fc939885a8b58ac61c3f6e45986815c916a7256f Mon Sep 17 00:00:00 2001 From: Taso Date: Tue, 27 Jul 2021 23:04:46 -0400 Subject: [PATCH] feat(config): configure an export to be called at toggle to control phone access Can serve as a primitive form of access control that can for example call a client side inventory check --- config.json | 6 +++++- resources/client/cl_main.ts | 29 +++++++++++++++++++---------- typings/config.ts | 8 +++++++- 3 files changed, 31 insertions(+), 12 deletions(-) diff --git a/config.json b/config.json index 40f04ce..64e663f 100644 --- a/config.json +++ b/config.json @@ -2,7 +2,11 @@ "Locale": "en", "KeyTogglePhone": 288, "KeyTakeCall": 38, - "PhoneAsItem": false, + "PhoneAsItem": { + "enabled": false, + "exportResource": "my-core-resource", + "exportName": "myCheckerFunction" + }, "SwimDestroy": false, "RunRate": 10, "DestroyChance": 100, diff --git a/resources/client/cl_main.ts b/resources/client/cl_main.ts index 79ec90a..ddc404b 100644 --- a/resources/client/cl_main.ts +++ b/resources/client/cl_main.ts @@ -7,8 +7,12 @@ import { PhotoEvents } from '../../typings/photo'; import { CallEvents } from '../../typings/call'; import { config } from './client'; import { animationService } from './animations/animation.controller'; +import { RegisterNuiCB } from './cl_utils'; + let isPhoneOpen = false; +const exps = global.exports; + /* * * * * * * * * * * * * * * Phone initialize data @@ -75,7 +79,7 @@ RegisterCommand( 'phone', async () => { //-- Toggles Phone - await Phone(); + await togglePhone(); }, false, ); @@ -96,17 +100,22 @@ RegisterCommand( * * * * * * * * * * * * * */ -async function Phone(): Promise { - if (config.PhoneAsItem) { - // TODO: Do promise callback here - // const hasPhoneItem = await emitNetPromise('phone:hasPhoneItem') - // if (!hasPhoneItem) return +const checkExportCanOpen = (): boolean => { + const exportResp = exps[config.PhoneAsItem.exportResource][config.PhoneAsItem.exportFunction](); + if (typeof exportResp !== 'number' && typeof exportResp !== 'boolean') { + throw new Error('You must return either a boolean or number from your export function'); } - if (isPhoneOpen) { - await hidePhone(); - } else { - await showPhone(); + + return !!exportResp; +}; + +async function togglePhone(): Promise { + if (config.PhoneAsItem.enabled) { + const canAccess = checkExportCanOpen(); + if (!canAccess) return; } + if (isPhoneOpen) return await hidePhone(); + await showPhone(); } onNet(PhoneEvents.SEND_CREDENTIALS, (number: string) => { diff --git a/typings/config.ts b/typings/config.ts index a63bf3d..1dadac3 100644 --- a/typings/config.ts +++ b/typings/config.ts @@ -43,12 +43,18 @@ interface DatabaseConfig { useIdentifierPrefix: boolean; } +interface PhoneAsItemConfig { + enabled: boolean; + exportResource: string; + exportFunction: string; +} + export interface ResourceConfig { database: DatabaseConfig; Locale: string; KeyTogglePhone: number; KeyTakeCall: number; - PhoneAsItem: boolean; + PhoneAsItem: PhoneAsItemConfig; SwimDestroy: boolean; RunRate: number; twitter: TwitterConfig;