Update frameworkCache.lua

This commit is contained in:
Jim Shield
2025-06-11 16:30:29 +01:00
parent 87cdd5502e
commit 9c769f4e8e

View File

@@ -1,10 +1,11 @@
--[[ --[[
Cached Resource Initialization Module Cached Resource Initialization Module
-------------------------------------- --------------------------------------
This module initializes shared data (Items, Vehicles, Jobs, Gangs) Initializes shared resource data (Items, Vehicles, Jobs, Gangs) once and caches it
only once and stores it in _G.__jimBridgeDataCache for reuse across scripts. to improve performance and reduce redundant data fetching across scripts.
]] ]]
-- Define resource names used in different frameworks
local Exports = { local Exports = {
QBExport = "qb-core", QBExport = "qb-core",
QBXExport = "qbx_core", QBXExport = "qbx_core",
@@ -32,17 +33,20 @@ local Exports = {
RSGInv = "rsg-inventory" RSGInv = "rsg-inventory"
} }
-- Ensure cache only runs once -- Prevent reloading if cache is already initialized
if _G.__jimBridgeDataCache then return end if _G.__jimBridgeDataCache then return end
_G.__jimBridgeDataCache = {} _G.__jimBridgeDataCache = {}
local cache = _G.__jimBridgeDataCache local cache = _G.__jimBridgeDataCache
function checkExists(resourceName) -- Helper function to check if resource exists in server (instead of if it is already started)
local function checkExists(resourceName)
return GetResourceState(resourceName):find("start") or GetResourceState(resourceName):find("stopped") return GetResourceState(resourceName):find("start") or GetResourceState(resourceName):find("stopped")
end end
-- Ensure oxmysql resource is loaded
local fileLoader = assert(load(LoadResourceFile("oxmysql", ('lib/MySQL.lua')), ('@@oxmysql/lib/MySQL.lua'))) local fileLoader = assert(load(LoadResourceFile("oxmysql", ('lib/MySQL.lua')), ('@@oxmysql/lib/MySQL.lua')))
fileLoader() fileLoader()
if checkExists(Exports.OXCoreExport) then if checkExists(Exports.OXCoreExport) then
-- Detected OX_Core in server, wait for it to be started if needed -- Detected OX_Core in server, wait for it to be started if needed
while GetResourceState(Exports.OXCoreExport) ~= "started" do Wait(100) end while GetResourceState(Exports.OXCoreExport) ~= "started" do Wait(100) end
@@ -56,7 +60,7 @@ if checkExists(Exports.ESXExport) then
fileLoader() fileLoader()
end end
-- Init variables -- Initialize variables for caching
local Items, Vehicles, Jobs, Gangs, Core = nil, nil, nil, nil, nil local Items, Vehicles, Jobs, Gangs, Core = nil, nil, nil, nil, nil
local itemResource, jobResource, vehResource = "", "", "" local itemResource, jobResource, vehResource = "", "", ""
@@ -70,6 +74,7 @@ end
--------------------- ---------------------
---- Load Items ----- ---- Load Items -----
--------------------- ---------------------
-- Items initialization based on detected inventory system
if checkExists(Exports.OXInv) then if checkExists(Exports.OXInv) then
-- Wait for OX Inventory to start if it's not already started -- Wait for OX Inventory to start if it's not already started
while GetResourceState(Exports.OXInv) ~= "started" do Wait(100) end while GetResourceState(Exports.OXInv) ~= "started" do Wait(100) end
@@ -95,6 +100,7 @@ elseif checkExists(Exports.QBExport) then
Items = Core.Shared.Items Items = Core.Shared.Items
if Items == nil then if Items == nil then
print("^1ERROR^7: ^1Can NOT find shared ^7Items ^1table^7, ^1possible error in that file^7?") print("^1ERROR^7: ^1Can NOT find shared ^7Items ^1table^7, ^1possible error in that file^7?")
Items = {} -- Fallback to an empty table
end end
elseif checkExists(Exports.ESXExport) then elseif checkExists(Exports.ESXExport) then
@@ -116,6 +122,7 @@ elseif checkExists(Exports.RSGExport) then
Items = Core.Shared.Items Items = Core.Shared.Items
if Items == nil then if Items == nil then
print("^1ERROR^7: ^1Can NOT find shared ^7Items ^1table^7, ^1possible error in that file^7?") print("^1ERROR^7: ^1Can NOT find shared ^7Items ^1table^7, ^1possible error in that file^7?")
Items = {} -- Fallback to an empty table
end end
end end
@@ -123,12 +130,14 @@ end
--------------------- ---------------------
--- Load Vehicles --- --- Load Vehicles ---
--------------------- ---------------------
-- Vehicle loading depending on framework
if checkExists(Exports.QBXExport) or checkExists(Exports.QBExport) then if checkExists(Exports.QBXExport) or checkExists(Exports.QBExport) then
vehResource = Exports.QBExport vehResource = Exports.QBExport
Core = Core or exports[Exports.QBExport]:GetCoreObject() Core = Core or exports[Exports.QBExport]:GetCoreObject()
Vehicles = Core.Shared.Vehicles Vehicles = Core.Shared.Vehicles
if Vehicles == nil then if Vehicles == nil then
print("^1ERROR^7: ^1Can NOT find shared ^7Vehicles ^1table^7, ^1possible error in that file^7?") print("^1ERROR^7: ^1Can NOT find shared ^7Vehicles ^1table^7, ^1possible error in that file^7?")
Vehicles = {} -- Fallback to an empty table
end end
elseif checkExists(Exports.OXCoreExport) then elseif checkExists(Exports.OXCoreExport) then
@@ -162,6 +171,7 @@ elseif checkExists(Exports.RSGExport) then
Vehicles = Core.Shared.Vehicles Vehicles = Core.Shared.Vehicles
if Vehicles == nil then if Vehicles == nil then
print("^1ERROR^7: ^1Can NOT find shared ^7Vehicles ^1table^7, ^1possible error in that file^7?") print("^1ERROR^7: ^1Can NOT find shared ^7Vehicles ^1table^7, ^1possible error in that file^7?")
Vehicles = {} -- Fallback to an empty table
end end
end end
@@ -169,12 +179,14 @@ end
--------------------- ---------------------
----- Load Jobs ----- ----- Load Jobs -----
--------------------- ---------------------
-- Jobs loading based on framework
if checkExists(Exports.QBXExport) then if checkExists(Exports.QBXExport) then
jobResource = Exports.QBXExport jobResource = Exports.QBXExport
Core = Core or exports[Exports.QBXExport]:GetCoreObject() Core = Core or exports[Exports.QBXExport]:GetCoreObject()
Jobs, Gangs = exports[Exports.QBXExport]:GetJobs(), exports[Exports.QBXExport]:GetGangs() Jobs, Gangs = exports[Exports.QBXExport]:GetJobs(), exports[Exports.QBXExport]:GetGangs()
if Jobs == nil then if Jobs == nil then
print("^1ERROR^7: ^1Can NOT find shared ^7Jobs ^1table^7, ^1possible error in that file^7?") print("^1ERROR^7: ^1Can NOT find shared ^7Jobs ^1table^7, ^1possible error in that file^7?")
Jobs = {} -- Fallback to an empty table
end end
elseif checkExists(Exports.OXCoreExport) then elseif checkExists(Exports.OXCoreExport) then
@@ -241,6 +253,7 @@ elseif checkExists(Exports.RSGExport) then
Jobs, Gangs = Core.Shared.Jobs, Core.Shared.Gangs Jobs, Gangs = Core.Shared.Jobs, Core.Shared.Gangs
if Jobs == nil then if Jobs == nil then
print("^1ERROR^7: ^1Can NOT find shared ^7Jobs ^1table^7, ^1possible error in that file^7?") print("^1ERROR^7: ^1Can NOT find shared ^7Jobs ^1table^7, ^1possible error in that file^7?")
Jobs = {} -- Fallback to an empty table
end end
end end
@@ -269,14 +282,11 @@ CreateThread(function()
print("^6FrameworkCache^7: ^2Loaded ^5"..tostring(counts.Gangs).."^2 Gangs from ^7"..jobResource) print("^6FrameworkCache^7: ^2Loaded ^5"..tostring(counts.Gangs).."^2 Gangs from ^7"..jobResource)
end) end)
--print(json.encode(cache.Items , { indent = true}))
RegisterNetEvent("jim_bridge:requestCache", function() RegisterNetEvent("jim_bridge:requestCache", function()
local src = source local src = source
TriggerClientEvent("jim_bridge:receiveCache", src, _G.__jimBridgeDataCache) TriggerClientEvent("jim_bridge:receiveCache", src, _G.__jimBridgeDataCache)
end) end)
exports("GetSharedData", function() exports("GetSharedData", function()
-- Wait for data to be ready before returning it -- Wait for data to be ready before returning it
local timeout = GetGameTimer() + 5000 local timeout = GetGameTimer() + 5000