mirror of
https://github.com/jimathy/jim_bridge.git
synced 2026-08-17 05:56:02 +01:00
Revamp framework caching
People were reporting issues with scripts claiming items didn't exist, I believe this was due to their servers having slot starts (eg. alot of clothes or vehicles being loaded) I've rewritten it to fully delay loading until everything is correctly cached and let the scripts continue. Also hopefully reducing memory footprint for it too. --- Also slipped in an `experimental` function that grabs inventory config files to check their set InventoryWeight and InventorySlots to automatically apply it for other functions ---
This commit is contained in:
@@ -197,22 +197,23 @@ function waitForSharedLoad()
|
||||
local timeout = 100000 -- 10 seconds in milliseconds
|
||||
local startTime = GetGameTimer()
|
||||
local loaded = true
|
||||
--Wait(1000)
|
||||
local count = {}
|
||||
local loop = 0
|
||||
while ((not Jobs or not next(Jobs)) and (not Items or not next(Items)) and (not Vehicles or not next(Vehicles))) and (GetGameTimer() - startTime) < timeout do
|
||||
if not messageShown then
|
||||
while ((not Jobs or not next(Jobs)) or
|
||||
(not Items or not next(Items)) or
|
||||
(not Vehicles or not next(Vehicles))
|
||||
) and ((GetGameTimer() - startTime) < timeout) do
|
||||
if loop >= 3 and not messageShown then
|
||||
if (not Jobs or not next(Jobs)) then
|
||||
debugPrint("^4Debug^7: ^2Waiting for Jobs to be loaded")
|
||||
print("^4Debug^7: ^2Waiting for ^7Jobs^2 to be loaded")
|
||||
end
|
||||
if (not Items or not next(Items)) then
|
||||
debugPrint("^4Debug^7: ^2Waiting for Items to be loaded")
|
||||
print("^4Debug^7: ^2Waiting for ^7Items^2 to be loaded")
|
||||
end
|
||||
if (not Vehicles or not next(Vehicles)) then
|
||||
debugPrint("^4Debug^7: ^2Waiting for Vehicles to be loaded")
|
||||
print("^4Debug^7: ^2Waiting for ^7Vehicles^2 to be loaded")
|
||||
end
|
||||
messageShown = true
|
||||
end
|
||||
messageShown = true
|
||||
--print((GetGameTimer() - startTime) < timeout)
|
||||
Wait(1000)
|
||||
if Jobs and Items and Vehicles then
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
Items, Vehicles, Jobs, Gangs, Core = {}, nil, nil, nil, nil
|
||||
Items, Vehicles, Jobs, Gangs = nil, nil, nil, nil
|
||||
|
||||
-- Shared Exports Initialization
|
||||
Exports.PSInv = isStarted("lj-inventory") and "lj-inventory" or Exports.PSInv
|
||||
@@ -33,44 +33,44 @@ end
|
||||
|
||||
|
||||
if IsDuplicityVersion() then
|
||||
CreateThread(function()
|
||||
local cache = nil
|
||||
local timeout = GetGameTimer() + 5000 -- 5 seconds max wait
|
||||
local cache = nil
|
||||
local timeout = GetGameTimer() + 5000 -- 5 seconds max wait
|
||||
|
||||
-- Wait until jim_bridge is started and export is available
|
||||
while not cache and GetGameTimer() < timeout do
|
||||
if GetResourceState("jim_bridge"):find("start") then
|
||||
local success, result = pcall(function()
|
||||
return exports["jim_bridge"]:GetSharedData()
|
||||
end)
|
||||
if success and result then
|
||||
cache = result
|
||||
--print(json.encode(cache, {indent = true}))
|
||||
end
|
||||
-- Wait until jim_bridge is started and export is available
|
||||
while not cache and GetGameTimer() < timeout do
|
||||
if GetResourceState("jim_bridge"):find("start") then
|
||||
local success, result = pcall(function()
|
||||
return exports["jim_bridge"]:GetSharedData()
|
||||
end)
|
||||
if success and result then
|
||||
cache = result
|
||||
end
|
||||
Wait(100)
|
||||
end
|
||||
Wait(100)
|
||||
end
|
||||
|
||||
if not cache then
|
||||
print("^1ERROR^7: ^2jim_bridge export not available after timeout^7.")
|
||||
return
|
||||
end
|
||||
if not cache then
|
||||
print("^1ERROR^7: ^2jim_bridge export not available after timeout^7.")
|
||||
return
|
||||
end
|
||||
|
||||
Items = cache.Items
|
||||
Vehicles = cache.Vehicles
|
||||
Jobs = cache.Jobs
|
||||
Gangs = cache.Gangs
|
||||
Items = cache.Items
|
||||
Vehicles = cache.Vehicles
|
||||
Jobs = cache.Jobs
|
||||
Gangs = cache.Gangs
|
||||
InventoryWeight = cache.InventoryWeight or InventoryWeight
|
||||
InventorySlots = cache.InventorySlots or 40
|
||||
|
||||
debugPrint("^6Bridge^7: ^2Shared cache successfully loaded from export^7.")
|
||||
end)
|
||||
debugPrint("^6Bridge^7: ^2Shared cache successfully loaded from export^7.")
|
||||
--print(countTable(Items), countTable(Vehicles), countTable(Jobs))
|
||||
else
|
||||
local hasCache = false
|
||||
-- 🔹 Client Side: Request from server
|
||||
_G.__jimBridgeDataCache = {}
|
||||
cache = {}
|
||||
|
||||
RegisterNetEvent("jim_bridge:receiveCache", function(data)
|
||||
if not hasCache then
|
||||
_G.__jimBridgeDataCache = data
|
||||
cache = data
|
||||
hasCache = true
|
||||
else
|
||||
return
|
||||
@@ -79,34 +79,34 @@ else
|
||||
|
||||
TriggerServerEvent("jim_bridge:requestCache")
|
||||
|
||||
CreateThread(function()
|
||||
while not _G.__jimBridgeDataCache or not next(_G.__jimBridgeDataCache) do Wait(50) end
|
||||
local cache = _G.__jimBridgeDataCache
|
||||
Items = cache.Items or {}
|
||||
Vehicles = cache.Vehicles or {}
|
||||
Jobs = cache.Jobs or {}
|
||||
Gangs = cache.Gangs or {}
|
||||
while not cache or not next(cache) do Wait(50) end
|
||||
Items = cache.Items or {}
|
||||
Vehicles = cache.Vehicles or {}
|
||||
Jobs = cache.Jobs or {}
|
||||
Gangs = cache.Gangs or {}
|
||||
InventoryWeight = cache.InventoryWeight or InventoryWeight
|
||||
InventorySlots = cache.InventorySlots or 50
|
||||
|
||||
if isStarted(ESXExport) then
|
||||
for _, v in pairs(Vehicles) do
|
||||
Vehicles[v.model] = {
|
||||
model = v.model,
|
||||
hash = v.hash,
|
||||
price = v.price,
|
||||
name = v.name,
|
||||
brand = GetMakeNameFromVehicleModel(v.model):lower():gsub("^%l", string.upper)
|
||||
}
|
||||
if isStarted(ESXExport) then
|
||||
for _, v in pairs(Vehicles) do
|
||||
Vehicles[v.model] = {
|
||||
model = v.model,
|
||||
hash = v.hash,
|
||||
price = v.price,
|
||||
name = v.name,
|
||||
brand = GetMakeNameFromVehicleModel(v.model):lower():gsub("^%l", string.upper)
|
||||
}
|
||||
end
|
||||
end
|
||||
if isStarted(OXInv) then
|
||||
for k, v in pairsByKeys(Items) do
|
||||
local tempInfo = exports[OXInv]:Items(k)
|
||||
if tempInfo and tempInfo.client then
|
||||
Items[k].image = (tempInfo.client and tempInfo.client.image) and tempInfo.client.image:gsub("nui://"..OXInv.."/web/images/", "") or k..".png"
|
||||
Items[k].hunger = tempInfo.client and tempInfo.client.hunger
|
||||
Items[k].thirst = tempInfo.client and tempInfo.client.thirst
|
||||
end
|
||||
end
|
||||
if isStarted(OXInv) then
|
||||
for k, v in pairsByKeys(Items) do
|
||||
local tempInfo = exports[OXInv]:Items(k)
|
||||
if tempInfo and tempInfo.client then
|
||||
Items[k].image = (tempInfo.client and tempInfo.client.image) and tempInfo.client.image:gsub("nui://"..OXInv.."/web/images/", "") or k..".png"
|
||||
Items[k].hunger = tempInfo.client and tempInfo.client.hunger
|
||||
Items[k].thirst = tempInfo.client and tempInfo.client.thirst
|
||||
end
|
||||
end
|
||||
end
|
||||
end)
|
||||
end
|
||||
debugPrint("^6Bridge^7: ^2Shared cache successfully loaded from export^7.")
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user