-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: attempt to make a qb-target compatibility layer
bridge: framework related functions providers: where we replace exports
- Loading branch information
Showing
7 changed files
with
861 additions
and
131 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
local resource_name = GetCurrentResourceName() | ||
|
||
local LoadResourceFile = LoadResourceFile | ||
local context = IsDuplicityVersion() and 'server' or 'client' | ||
|
||
string.split = function(str, pattern) | ||
pattern = pattern or "[^%s]+" | ||
if pattern:len() == 0 then | ||
pattern = "[^%s]+" | ||
end | ||
local parts = { __index = table.insert } | ||
setmetatable(parts, parts) | ||
str:gsub(pattern, parts) | ||
setmetatable(parts, nil) | ||
parts.__index = nil | ||
return parts | ||
end | ||
|
||
local function loadModule(module_name, dir) | ||
local chunk = LoadResourceFile(resource_name, ('%s.lua'):format(dir)) | ||
|
||
if chunk then | ||
local fn, err = load(chunk) | ||
if not fn or err then | ||
return error(('\n^1Error (%s): %s^0'):format(dir, err), 3) | ||
end | ||
|
||
local result = fn() | ||
|
||
return result[context]() | ||
end | ||
end | ||
|
||
---comment | ||
---@param module string | ||
local function link(module) | ||
local sub = module:split('[^.]+') | ||
local dir = ('lua/bridge/%s'):format(sub[1]) | ||
return loadModule(sub[1], dir) | ||
end | ||
|
||
Bridge = { | ||
active = false | ||
} | ||
|
||
if GetResourceState('qb-core') == 'started' then | ||
local bridge_link = link('qb') | ||
Bridge.hasItem = bridge_link.hasItem | ||
Bridge.getJob = bridge_link.getJob | ||
Bridge.getGang = bridge_link.getGang | ||
Bridge.active = true | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
local function updatePlayerItems(playerData, qb_items) | ||
for _, itemData in pairs(playerData.items or {}) do | ||
if qb_items[itemData.name] then | ||
qb_items[itemData.name] = qb_items[itemData.name] + itemData.amount | ||
else | ||
qb_items[itemData.name] = itemData.amount | ||
end | ||
end | ||
end | ||
|
||
local function reset(t) table.wipe(t) end | ||
|
||
return { | ||
client = function() | ||
local QBCore = exports['qb-core']:GetCoreObject() | ||
local playerData = QBCore.Functions.GetPlayerData() or {} | ||
local qb_items = {} | ||
|
||
AddEventHandler('QBCore:Client:OnPlayerLoaded', function() | ||
playerData = QBCore.Functions.GetPlayerData() | ||
reset(qb_items) | ||
updatePlayerItems(playerData, qb_items) | ||
end) | ||
|
||
RegisterNetEvent('QBCore:Client:OnPlayerUnload', function() | ||
playerData = {} | ||
reset(qb_items) | ||
end) | ||
|
||
RegisterNetEvent('QBCore:Player:SetPlayerData', function(val) | ||
playerData = QBCore.Functions.GetPlayerData() | ||
reset(qb_items) | ||
updatePlayerItems(playerData, qb_items) | ||
end) | ||
|
||
RegisterNetEvent('QBCore:Client:OnJobUpdate', function(JobInfo) | ||
playerData = QBCore.Functions.GetPlayerData() | ||
reset(qb_items) | ||
updatePlayerItems(playerData, qb_items) | ||
end) | ||
|
||
RegisterNetEvent('QBCore:Client:OnGangUpdate', function(GangInfo) | ||
playerData = QBCore.Functions.GetPlayerData() | ||
reset(qb_items) | ||
updatePlayerItems(playerData, qb_items) | ||
end) | ||
|
||
local function init() | ||
playerData = QBCore.Functions.GetPlayerData() | ||
reset(qb_items) | ||
updatePlayerItems(playerData, qb_items) | ||
end | ||
|
||
init() | ||
|
||
return { | ||
['getJob'] = function() | ||
local job = playerData['job'] | ||
return job.name, job.grade.level | ||
end, | ||
['getGang'] = function() | ||
local gang = playerData['gang'] | ||
return gang.name, gang.grade.level | ||
end, | ||
['hasItem'] = function(itemName, requiredAmount) | ||
if not requiredAmount then requiredAmount = 1 end | ||
local hasItem = qb_items[itemName] ~= nil | ||
local hasEnough = hasItem and qb_items[itemName] >= requiredAmount or false | ||
return hasItem, hasEnough | ||
end, | ||
['hasItems'] = function(itemNames, requiredAmount) | ||
if not requiredAmount then requiredAmount = 1 end | ||
|
||
for _, itemName in pairs(itemNames) do | ||
local hasItem = qb_items[itemName] ~= nil | ||
local hasEnough = hasItem and qb_items[itemName] >= requiredAmount or false | ||
|
||
if not hasEnough then | ||
return false, itemName | ||
end | ||
end | ||
|
||
return true | ||
end | ||
} | ||
end, | ||
server = function() | ||
|
||
end | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.