mirror of
https://github.com/esx-framework/esx_core.git
synced 2026-08-28 23:01:26 +00:00
Merge pull request #1863 from rwixy/ox-inv
fix(overrides/oxinventory): replace broken Inventory() export with direct export proxy
This commit is contained in:
@@ -14,29 +14,107 @@ local requiredMethods = {
|
|||||||
"SetMaxWeight",
|
"SetMaxWeight",
|
||||||
}
|
}
|
||||||
|
|
||||||
---Stores the reference to the internal ox_inventory module.
|
---Parses the inventory:accounts convar to build a lookup table.
|
||||||
---@param module table
|
---Uses the same default and JSON format as ox_inventory.
|
||||||
---@return boolean
|
---@return table<string, boolean>
|
||||||
---@return string? error
|
local function getAccountList()
|
||||||
local function setOxInventory(module)
|
local convar = GetConvar("inventory:accounts", '["money"]')
|
||||||
if type(module) ~= "table" then
|
local ok, list = pcall(json.decode, convar)
|
||||||
return false, "module is not a table"
|
|
||||||
|
if not ok or type(list) ~= "table" then
|
||||||
|
error(
|
||||||
|
("[es_extended] Invalid inventory:accounts convar: %s")
|
||||||
|
:format(tostring(convar)),
|
||||||
|
2
|
||||||
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
for i = 1, #requiredMethods do
|
local accounts = {}
|
||||||
local method = requiredMethods[i]
|
|
||||||
|
|
||||||
if type(module[method]) ~= "function" then
|
for i = 1, #list do
|
||||||
return false, ("missing method %s"):format(method)
|
local account = list[i]
|
||||||
|
|
||||||
|
if type(account) == "string" and account ~= "" then
|
||||||
|
accounts[account] = true
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
OxInventory = module
|
return accounts
|
||||||
|
end
|
||||||
|
|
||||||
|
---Safely resolves an ox_inventory export without executing it.
|
||||||
|
---@param resourceExports table
|
||||||
|
---@param method string
|
||||||
|
---@return function
|
||||||
|
local function resolveExport(resourceExports, method)
|
||||||
|
local ok, exportFn = pcall(function()
|
||||||
|
return resourceExports[method]
|
||||||
|
end)
|
||||||
|
|
||||||
|
if not ok then
|
||||||
|
error(
|
||||||
|
("[es_extended] Missing required ox_inventory export '%s': %s")
|
||||||
|
:format(method, tostring(exportFn)),
|
||||||
|
2
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
if type(exportFn) ~= "function" then
|
||||||
|
error(
|
||||||
|
("[es_extended] Invalid ox_inventory export '%s' (expected function, got %s)")
|
||||||
|
:format(method, type(exportFn)),
|
||||||
|
2
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
return exportFn
|
||||||
|
end
|
||||||
|
|
||||||
|
---Creates a proxy table that routes method calls to public ox_inventory exports.
|
||||||
|
---@return table
|
||||||
|
local function createOxInventoryProxy()
|
||||||
|
local proxy = {}
|
||||||
|
local resourceExports = exports.ox_inventory
|
||||||
|
|
||||||
|
print("^3[es_extended] ox_inventory: using direct export proxy (Inventory() interface unavailable or incomplete)^7")
|
||||||
|
|
||||||
|
for i = 1, #requiredMethods do
|
||||||
|
local method = requiredMethods[i]
|
||||||
|
local exportFn = resolveExport(resourceExports, method)
|
||||||
|
|
||||||
|
proxy[method] = function(...)
|
||||||
|
return exportFn(resourceExports, ...)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
proxy.accounts = getAccountList()
|
||||||
|
|
||||||
|
return proxy
|
||||||
|
end
|
||||||
|
|
||||||
|
---Checks if an Inventory() module provides everything ESX requires.
|
||||||
|
---@param module table
|
||||||
|
---@return boolean
|
||||||
|
local function isValidModule(module)
|
||||||
|
if type(module) ~= "table" then
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
|
for i = 1, #requiredMethods do
|
||||||
|
if type(module[requiredMethods[i]]) ~= "function" then
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
if type(module.accounts) ~= "table" then
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
---Returns the internal ox_inventory module.
|
---Returns the ox_inventory interface.
|
||||||
---If es_extended missed the load event, it will attempt to retrieve it via the export.
|
---Uses Inventory() when available, otherwise falls back to public exports.
|
||||||
---@return table
|
---@return table
|
||||||
local function getOxInventory()
|
local function getOxInventory()
|
||||||
if OxInventory then
|
if OxInventory then
|
||||||
@@ -57,31 +135,21 @@ local function getOxInventory()
|
|||||||
return exports.ox_inventory:Inventory()
|
return exports.ox_inventory:Inventory()
|
||||||
end)
|
end)
|
||||||
|
|
||||||
if not success then
|
if success and isValidModule(module) then
|
||||||
error(
|
OxInventory = module
|
||||||
("[es_extended] Failed to execute exports.ox_inventory:Inventory(): %s")
|
else
|
||||||
:format(tostring(module)),
|
OxInventory = createOxInventoryProxy()
|
||||||
2
|
|
||||||
)
|
|
||||||
end
|
|
||||||
|
|
||||||
local ok, err = setOxInventory(module)
|
|
||||||
if not ok then
|
|
||||||
error(
|
|
||||||
("[es_extended] The Inventory export from ox_inventory returned an invalid module: %s")
|
|
||||||
:format(err),
|
|
||||||
2
|
|
||||||
)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
return OxInventory
|
return OxInventory
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Standard method used when ox_inventory finishes loading..
|
-- Standard method used when ox_inventory finishes loading.
|
||||||
AddEventHandler("ox_inventory:loadInventory", function(module)
|
AddEventHandler("ox_inventory:loadInventory", function(module)
|
||||||
local ok, err = setOxInventory(module)
|
if isValidModule(module) then
|
||||||
if not ok then
|
OxInventory = module
|
||||||
print(("^1[es_extended] ox_inventory:loadInventory returned an invalid module: %s^7"):format(err))
|
else
|
||||||
|
OxInventory = createOxInventoryProxy()
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user