From e719faa74f25e0010f720dc7d333ad17263edf2d Mon Sep 17 00:00:00 2001 From: kac5a Date: Fri, 23 Sep 2022 21:43:19 +0200 Subject: [PATCH] add automatic framework detection --- client/client.lua | 36 +++++++++++++++++++++++------------- config.lua | 2 -- fxmanifest.lua | 4 ++-- server/server.lua | 25 +++++++++++++++++-------- 4 files changed, 42 insertions(+), 25 deletions(-) diff --git a/client/client.lua b/client/client.lua index 390d41d..d8d3d8b 100644 --- a/client/client.lua +++ b/client/client.lua @@ -6,8 +6,18 @@ local Notification local QBCore ESX = nil +local CurrentFramework -if Config.Framework == "esx" then +if GetResourceState("es_extended") == "started" then + CurrentFramework = "esx" +elseif GetResourceState("qb-core") == "started" then + CurrentFramework = "qb" +else + print("^8ERROR: ^3This script only supports ESX and QBCore frameworks, but non of these are not present. Unfortunatelly, you cannot use this script.^7") + return +end + +if CurrentFramework == "esx" then Citizen.CreateThread(function() while ESX == nil do TriggerEvent('esx:getSharedObject', function(obj) ESX = obj end) @@ -40,7 +50,7 @@ if Config.Framework == "esx" then ESX.ShowNotification(msg) end -elseif Config.Framework == "qb" then +elseif CurrentFramework == "qb" then QBCore = exports['qb-core']:GetCoreObject() TriggerCallback = function (name, cb, ...) QBCore.Functions.TriggerCallback(name, cb, ...) @@ -49,8 +59,6 @@ elseif Config.Framework == "qb" then Notification = function (msg) QBCore.Functions.Notify(msg) end -else - print("^8ERROR: ^3Unsupported or misspelled framework^7") end function holdDocument(shouldHold) @@ -114,9 +122,11 @@ RegisterNUICallback('hideDocument', function(_, cb) cb({}) end) -RegisterCommand(Config.Command, function() - toggleNuiFrame(true, true) -end) +if CurrentFramework then + RegisterCommand(Config.Command, function() + toggleNuiFrame(true, true) + end) +end RegisterNUICallback('hideFrame', function(_, cb) toggleNuiFrame(false, false) @@ -124,11 +134,11 @@ RegisterNUICallback('hideFrame', function(_, cb) end) RegisterNUICallback('getPlayerJob', function(data, cb) - if Config.Framework == "esx" then + if CurrentFramework == "esx" then local retData = ESX.PlayerData.job retData.isBoss = retData.grade_name == "boss" cb(retData) - elseif Config.Framework == "qb" then + elseif CurrentFramework == "qb" then local PlayerJob = QBCore.Functions.GetPlayerData().job local retData = { grade = PlayerJob.grade.level, @@ -146,11 +156,11 @@ RegisterNUICallback('getPlayerJob', function(data, cb) end) RegisterNUICallback('getPlayerData', function(data, cb) - if Config.Framework == "esx" then + if CurrentFramework == "esx" then ESX.TriggerServerCallback('k5_documents:getPlayerData', function(result) cb(result) end) - elseif Config.Framework == "qb" then + elseif CurrentFramework == "qb" then local PlayerData = QBCore.Functions.GetPlayerData().charinfo cb({ firstname = PlayerData.firstname, @@ -261,9 +271,9 @@ function playerSelector(confirmText) while selectingPlayer do local closestPlayer, closestPlayerDistance - if Config.Framework == "esx" then + if CurrentFramework == "esx" then closestPlayer, closestPlayerDistance = ESX.Game.GetClosestPlayer() - elseif Config.Framework == "qb" then + elseif CurrentFramework == "qb" then closestPlayer, closestPlayerDistance = QBCore.Functions.GetClosestPlayer() end local closestPlayerCoords = GetEntityCoords(GetPlayerPed(closestPlayer)) diff --git a/config.lua b/config.lua index 2fb936a..7ed7af2 100644 --- a/config.lua +++ b/config.lua @@ -1,7 +1,5 @@ Config = {} -Config.Framework = "esx" -- "esx" or "qb" - Config.Command = "documents" Config.PaperProp = { diff --git a/fxmanifest.lua b/fxmanifest.lua index f8d0f9e..ae67a6b 100644 --- a/fxmanifest.lua +++ b/fxmanifest.lua @@ -2,8 +2,8 @@ fx_version "cerulean" description "A better document management script" author "K5 Scripts" -version '1.1.5' -update "Custom document names, template creation set for job grades, bug fixes" +version '1.1.6' +update "Automatic framework detection" repository 'https://github.com/kac5a/k5_documents' lua54 'yes' diff --git a/server/server.lua b/server/server.lua index ef5885a..9b5381f 100644 --- a/server/server.lua +++ b/server/server.lua @@ -1,19 +1,28 @@ local RegisterCallback ESX = nil local QBCore -if Config.Framework == "esx" then +local CurrentFramework + +if GetResourceState("es_extended") == "started" then + CurrentFramework = "esx" +elseif GetResourceState("qb-core") == "started" then + CurrentFramework = "qb" +else + print("^8ERROR: ^3This script only supports ESX and QBCore frameworks, but non of these are present. Unfortunatelly, you cannot use this script.^7") + return +end + +if CurrentFramework == "esx" then ESX = nil TriggerEvent('esx:getSharedObject', function(obj) ESX = obj end) RegisterCallback = function (name, fn) ESX.RegisterServerCallback(name, fn) end -elseif Config.Framework == "qb" then +elseif CurrentFramework == "qb" then QBCore = exports['qb-core']:GetCoreObject() RegisterCallback = function (name, fn) QBCore.Functions.CreateCallback(name, fn) end -else - print("^8ERROR: ^3Unsupported or misspelled framework^7") end Citizen.CreateThread(function() @@ -273,9 +282,9 @@ end) function GetPlayerIdentifier(src) local PlayerIdentifier - if Config.Framework == "esx" then + if CurrentFramework == "esx" then PlayerIdentifier = ESX.GetPlayerFromId(src).identifier - elseif Config.Framework == "qb" then + elseif CurrentFramework == "qb" then PlayerIdentifier = QBCore.Functions.GetPlayer(src).PlayerData.citizenid end return PlayerIdentifier @@ -283,9 +292,9 @@ end function GetPlayerJobName(src) local PlayerJobName - if Config.Framework == "esx" then + if CurrentFramework == "esx" then PlayerJobName = ESX.GetPlayerFromId(src).job.name - elseif Config.Framework == "qb" then + elseif CurrentFramework == "qb" then PlayerJobName = QBCore.Functions.GetPlayer(src).PlayerData.job.name end return PlayerJobName