Skip to content

Commit

Permalink
refactor(bridge): replace script loading with lib.load
Browse files Browse the repository at this point in the history
  • Loading branch information
thelindat committed Dec 10, 2023
1 parent 52e90d3 commit 02fc2e5
Show file tree
Hide file tree
Showing 12 changed files with 32 additions and 72 deletions.
17 changes: 0 additions & 17 deletions init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -137,23 +137,6 @@ local function spamError(err)
error(err, 0)
end

CreateThread(function()
if shared.framework == 'ox' then
local file = ('imports/%s.lua'):format(lib.context)
local import = LoadResourceFile('ox_core', file)
local func, err = load(import, ('@@ox_core/%s'):format(file))

if not func or err then
shared.ready = false
return spamError(err)
end

func()

Ox = Ox or {}
end
end)

---@param name string
---@return table
---@deprecated
Expand Down
21 changes: 9 additions & 12 deletions modules/bridge/client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,16 @@ function client.onLogout()
Weapon.Disarm()
end

local scriptPath = ('modules/bridge/%s/client.lua'):format(shared.framework)
local resourceFile = LoadResourceFile(cache.resource, scriptPath)

if not resourceFile then
lib = nil
return error(("Unable to find framework bridge for '%s'"):format(shared.framework))
if shared.framework == 'ox' then
CreateThread(function()
lib.load(('@ox_core.imports.%s'):format(lib.context))
end)
end

local func, err = load(resourceFile, ('@@%s/%s'):format(cache.resource, scriptPath))
local success, result = pcall(lib.load, ('modules.bridge.%s.client'):format(shared.framework))

if not func or err then
lib = nil
return error(err)
if not success then
lib.print.error(result)
lib = nil
return
end

func(client.onLogout)
4 changes: 1 addition & 3 deletions modules/bridge/esx/client.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
local onLogout = ...

local ESX = setmetatable({}, {
__index = function(self, index)
local obj = exports.es_extended:getSharedObject()
Expand All @@ -22,7 +20,7 @@ function client.setPlayerStatus(values)
end
end

RegisterNetEvent('esx:onPlayerLogout', onLogout)
RegisterNetEvent('esx:onPlayerLogout', client.onLogout)

AddEventHandler('esx:setPlayerData', function(key, value)
if not PlayerData.loaded or GetInvokingResource() ~= 'es_extended' then return end
Expand Down
3 changes: 1 addition & 2 deletions modules/bridge/esx/server.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
local playerDropped = ...
local Inventory = require 'modules.inventory.server'
local Items = require 'modules.items.server'

AddEventHandler('esx:playerDropped', playerDropped)
AddEventHandler('esx:playerDropped', server.playerDropped)

AddEventHandler('esx:setJob', function(source, job, lastJob)
local inventory = Inventory(source)
Expand Down
8 changes: 4 additions & 4 deletions modules/bridge/nd/client.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
local onLogout = ...
local file = LoadResourceFile("ND_Core", "init.lua")
load(file, "@ND_Core/init.lua")()
local NDCore = lib.load('@ND_Core.init')

RegisterNetEvent("ND:characterUnloaded", onLogout)
if lib.checkDependency('ND_Core', '2.0.0', true) then return end

RegisterNetEvent("ND:characterUnloaded", client.onLogout)

local function reorderGroups(groups)
groups = groups or {}
Expand Down
11 changes: 5 additions & 6 deletions modules/bridge/nd/server.lua
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
local playerDropped = ...
local Inventory = require 'modules.inventory.server'
local file = LoadResourceFile("ND_Core", "init.lua")
load(file, "@ND_Core/init.lua")()

AddEventHandler("ND:characterUnloaded", playerDropped)
local NDCore = lib.load('@ND_Core.init')

if lib.checkDependency('ND_Core', '2.0.0', true) then return end

AddEventHandler("ND:characterUnloaded", server.playerDropped)

local Inventory = require 'modules.inventory.server'

local function reorderGroups(groups)
groups = groups or {}
for group, info in pairs(groups) do
Expand Down
4 changes: 1 addition & 3 deletions modules/bridge/ox/client.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
local onLogout = ...

RegisterNetEvent('ox:playerLogout', onLogout)
RegisterNetEvent('ox:playerLogout', client.onLogout)

RegisterNetEvent('ox:setGroup', function(name, grade)
PlayerData.groups[name] = grade
Expand Down
3 changes: 1 addition & 2 deletions modules/bridge/ox/server.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
local playerDropped = ...
local Inventory = require 'modules.inventory.server'

AddEventHandler('ox:playerLogout', playerDropped)
AddEventHandler('ox:playerLogout', server.playerDropped)

AddEventHandler('ox:setGroup', function(source, name, grade)
local inventory = Inventory(source)
Expand Down
3 changes: 1 addition & 2 deletions modules/bridge/qb/client.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
local onLogout, Weapon = ...
local QBCore = exports['qb-core']:GetCoreObject()
local Inventory = require 'modules.inventory.client'
local Weapon = require 'modules.weapon.client'

RegisterNetEvent('QBCore:Client:OnPlayerUnload', onLogout)
RegisterNetEvent('QBCore:Client:OnPlayerUnload', client.onLogout)

RegisterNetEvent('QBCore:Player:SetPlayerData', function(data)
if source == '' or not PlayerData.loaded then return end
Expand Down
3 changes: 1 addition & 2 deletions modules/bridge/qb/server.lua
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
local playerDropped = ...
local Inventory = require 'modules.inventory.server'
local Items = require 'modules.items.server'

local QBCore

AddEventHandler('QBCore:Server:OnPlayerUnload', playerDropped)
AddEventHandler('QBCore:Server:OnPlayerUnload', server.playerDropped)

AddEventHandler('QBCore:Server:OnJobUpdate', function(source, job)
local inventory = Inventory(source)
Expand Down
25 changes: 6 additions & 19 deletions modules/bridge/server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ end

local Inventory = require 'modules.inventory.server'

local function playerDropped(source)
function server.playerDropped(source)
local inv = Inventory(source) --[[@as OxInventory]]

if inv?.player then
Expand All @@ -48,25 +48,12 @@ local function playerDropped(source)
end
end

AddEventHandler('playerDropped', function()
playerDropped(source)
end)
local success, result = pcall(lib.load, ('modules.bridge.%s.server'):format(shared.framework))

local scriptPath = ('modules/bridge/%s/server.lua'):format(shared.framework)
local resourceFile = LoadResourceFile(cache.resource, scriptPath)

if not resourceFile then
lib = nil
return error(("Unable to find framework bridge for '%s'"):format(shared.framework))
if not success then
lib.print.error(result)
lib = nil
return
end

local func, err = load(resourceFile, ('@@%s/%s'):format(cache.resource, scriptPath))

if not func or err then
lib = nil
return error(err)
end

func(playerDropped)

if server.convertInventory then exports('ConvertItems', server.convertInventory) end
2 changes: 2 additions & 0 deletions modules/inventory/server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2309,6 +2309,8 @@ function Inventory.SaveInventories(lock, clearInventories)
end

AddEventHandler('playerDropped', function()
server.playerDropped(source)

if GetNumPlayerIndices() == 0 then
Inventory.SaveInventories(false, true)
end
Expand Down

0 comments on commit 02fc2e5

Please sign in to comment.