Update main.lua

This commit is contained in:
Lorraxs 2023-12-27 07:14:37 +07:00
parent 9a64a0d00f
commit 77dc46460b

131
main.lua
View File

@ -1,14 +1,20 @@
Framework = nil Framework = nil
if Config.Framework == 'esx' then if Config.Framework == 'esx' then
Framework = exports["es_extended"]:getSharedObject() Framework = exports["es_extended"]:getSharedObject()
elseif Config.Framework == "qb" then elseif Config.Framework == "qb" then
Framework = exports['qb-core']:GetCoreObject() Framework = exports['qb-core']:GetCoreObject()
else
Framework = {}
Framework.Functions = {}
Framework.Functions.GetPlayerData = function()
return {}
end
end end
Main = {} Main = {}
ResourceName = GetCurrentResourceName() ResourceName = GetCurrentResourceName()
local RegisteredEvents = {} local RegisteredEvents = {}
if IsDuplicityVersion() then if IsDuplicityVersion() then
function GetGameTimer() function GetGameTimer()
return os.clock() * 1000 return os.clock() * 1000
end end
@ -20,7 +26,7 @@ else
end end
function Main:Init() function Main:Init()
local o = {} local o = {}
setmetatable(o, {__index = Main}) setmetatable(o, { __index = Main })
o.impls = {} o.impls = {}
o.initializedImpls = {} o.initializedImpls = {}
o.lastTimeImplRegistered = 0 o.lastTimeImplRegistered = 0
@ -32,7 +38,6 @@ function Main:Init()
o.playerHeading = GetEntityHeading(o.playerPed) o.playerHeading = GetEntityHeading(o.playerPed)
o.playerServerId = GetPlayerServerId(o.playerId) o.playerServerId = GetPlayerServerId(o.playerId)
o:Thread1() o:Thread1()
else else
o.ClientImpls = {} o.ClientImpls = {}
for k, v in pairs(Config.EnableModules) do for k, v in pairs(Config.EnableModules) do
@ -48,7 +53,7 @@ function Main:Init()
end end
end end
end end
lib.callback.register(ResourceName..":getClientImpl", function(source, implName) lib.callback.register(ResourceName .. ":getClientImpl", function(source, implName)
return o.ClientImpls[implName] return o.ClientImpls[implName]
end) end)
end end
@ -57,6 +62,7 @@ function Main:Init()
o:RegisterEvents() o:RegisterEvents()
return o return o
end end
if not IsDuplicityVersion() then if not IsDuplicityVersion() then
function Main:Thread1() function Main:Thread1()
Citizen.CreateThread(function() Citizen.CreateThread(function()
@ -73,11 +79,11 @@ end
function Main:RegisterCommands() function Main:RegisterCommands()
if not IsDuplicityVersion() then if not IsDuplicityVersion() then
RegisterCommand("toggledebug:"..ResourceName, function(source, args, rawCommand) RegisterCommand("toggledebug:" .. ResourceName, function(source, args, rawCommand)
Config.Debug = not Config.Debug Config.Debug = not Config.Debug
self:LogInfo("Debug %s", Config.Debug) self:LogInfo("Debug %s", Config.Debug)
end) end, false)
RegisterCommand("toggledev:"..ResourceName, function(source, args, rawCommand) RegisterCommand("toggledev:" .. ResourceName, function(source, args, rawCommand)
Config.Dev = not Config.Dev Config.Dev = not Config.Dev
self:LogInfo("Dev %s", Config.Dev) self:LogInfo("Dev %s", Config.Dev)
SendNUIMessage({ SendNUIMessage({
@ -86,18 +92,19 @@ function Main:RegisterCommands()
isDev = Config.Dev, isDev = Config.Dev,
} }
}) })
end) end, false)
RegisterCommand("implinfo:"..ResourceName, function(source, args, rawCommand) RegisterCommand("implinfo:" .. ResourceName, function(source, args, rawCommand)
self:ImplInfo() self:ImplInfo()
end) end, false)
RegisterCommand("test", function() RegisterCommand("test", function()
TriggerEvent("test") TriggerEvent("test")
end) end, false)
else else
RegisterCommand("reload:"..ResourceName, function(source, args, rawCommand) RegisterCommand("reload:" .. ResourceName, function(source, args, rawCommand)
if Config.ClientLazyLoad == false then return print("Lazyload was disabled") end
local implName = args[1] local implName = args[1]
local mode = args[2] local mode = args[2]
if mode == nil then if mode == nil then
mode = "0" mode = "0"
end end
self:LogInfo("Restarting impl: %s | side: %s (0: both, 1: client, 2: server)", implName, mode) self:LogInfo("Restarting impl: %s | side: %s (0: both, 1: client, 2: server)", implName, mode)
@ -122,7 +129,7 @@ function Main:RegisterCommands()
self:LogWarning("Failed to load %s", path) self:LogWarning("Failed to load %s", path)
else else
self:LogInfo("Loading %s", "client/impl/" .. implName .. ".impl.lua") self:LogInfo("Loading %s", "client/impl/" .. implName .. ".impl.lua")
TriggerClientEvent(ResourceName..":restartClientImpl", -1, implName, clSource) TriggerClientEvent(ResourceName .. ":restartClientImpl", -1, implName, clSource)
end end
end end
end, true) end, true)
@ -130,7 +137,7 @@ function Main:RegisterCommands()
end end
function Main:RegisterEvents() function Main:RegisterEvents()
RegisterNetEvent(ResourceName..":restartClientImpl", function(implName, source) RegisterNetEvent(ResourceName .. ":restartClientImpl", function(implName, source)
local clImpl = self:GetImpl(implName) local clImpl = self:GetImpl(implName)
if clImpl then if clImpl then
clImpl:Destroy() clImpl:Destroy()
@ -187,9 +194,9 @@ function Main:RegisterImpl(name, impl)
end end
self.impls[name] = impl self.impls[name] = impl
self.lastTimeImplRegistered = GetGameTimer() self.lastTimeImplRegistered = GetGameTimer()
self:LogSuccess("Impl %s registered", name) self:LogSuccess("Impl %s registered", name)
if self.ready then if self.ready then
Citizen.CreateThread(function() Citizen.CreateThread(function()
self:LogSuccess("Impl %s hot reloading", name) self:LogSuccess("Impl %s hot reloading", name)
Wait(1000) Wait(1000)
@ -205,26 +212,40 @@ function Main:RegisterImpl(name, impl)
end end
function Main:InitImpl() function Main:InitImpl()
if not IsDuplicityVersion() then if not IsDuplicityVersion() then
for k, v in pairs(Config.EnableModules) do if Config.ClientLazyLoad then
if v.enabled and v.priority == 1 and v.client then for k, v in pairs(Config.EnableModules) do
self:LogInfo("Loading %s", k) if v.enabled and v.priority == 1 and v.client then
local source = lib.callback.await(ResourceName..":getClientImpl", false, k) self:LogInfo("Loading %s", k)
if source ~= nil then local source = lib.callback.await(ResourceName .. ":getClientImpl", false, k)
self:LogInfo("Loaded %s", k) if source ~= nil then
load(source)() self:LogInfo("Loaded %s", k)
load(source)()
end
end end
end end
end for name, impl in pairs(self.impls) do
for name, impl in pairs(self.impls) do if Config.EnableModules[name] and Config.EnableModules[name].priority == 1 then
if Config.EnableModules[name] and Config.EnableModules[name].priority == 1 then self.initializedImpls[name] = impl(self)
self.initializedImpls[name] = impl(self) end
end end
end self:LogInfo("All priority 1 initialized")
self:LogInfo("All priority 1 initialized") for name, impl in pairs(self.initializedImpls) do
for name, impl in pairs(self.initializedImpls) do if Config.EnableModules[name] and Config.EnableModules[name].priority == 1 then
if Config.EnableModules[name] and Config.EnableModules[name].priority == 1 then impl:OnReady()
impl:OnReady() end
end
else
for name, impl in pairs(self.impls) do
if Config.EnableModules[name] and Config.EnableModules[name].priority == 1 then
self.initializedImpls[name] = impl(self)
end
end
self:LogInfo("All priority 1 initialized")
for name, impl in pairs(self.initializedImpls) do
if Config.EnableModules[name] and Config.EnableModules[name].priority == 1 then
impl:OnReady()
end
end end
end end
else else
@ -238,14 +259,20 @@ function Main:InitImpl()
end end
function Main:InitImplAfterPlayerLoaded() function Main:InitImplAfterPlayerLoaded()
if not IsDuplicityVersion() then if not IsDuplicityVersion() then
for k, v in pairs(Config.EnableModules) do for k, v in pairs(Config.EnableModules) do
if v.enabled and v.priority == 2 and v.client then if v.enabled and v.priority == 2 and v.client then
self:LogInfo("Loading %s", k) self:LogInfo("Loading %s", k)
local source = lib.callback.await(ResourceName..":getClientImpl", false, k) if Config.ClientLazyLoad then
if source ~= nil then local source = lib.callback.await(ResourceName .. ":getClientImpl", false, k)
self:LogInfo("Loaded %s", k) if source ~= nil then
load(source)() self:LogInfo("Loaded %s", k)
load(source)()
end
else
if self.initializedImpls[k] then
self.initializedImpls[k]:OnReady()
end
end end
end end
end end
@ -285,13 +312,12 @@ function Main:ImplCall(name, func, ...)
return return
end end
if not impl[func] then if not impl[func] then
self:LogError("Impl %s missing function %s - args %s", name, func, json.encode({...})) self:LogError("Impl %s missing function %s - args %s", name, func, json.encode({ ... }))
return return
end end
return impl[func](impl, ...) return impl[func](impl, ...)
end end
function Main:ImplInfo() function Main:ImplInfo()
for name, impl in pairs(self.impls) do for name, impl in pairs(self.impls) do
local debug = debug.getinfo(impl.OnReady, "S") local debug = debug.getinfo(impl.OnReady, "S")
@ -309,47 +335,46 @@ main = Main:Init()
local origAddEventHandler = AddEventHandler local origAddEventHandler = AddEventHandler
function AddEventHandler(eventName, ...) function AddEventHandler(eventName, ...)
if RegisteredEvents[eventName] then if RegisteredEvents[eventName] then
main:LogWarning("Event %s already registered. Removing", eventName) main:LogWarning("Event %s already registered. Removing", eventName)
RemoveEventHandler(RegisteredEvents[eventName]) RemoveEventHandler(RegisteredEvents[eventName])
end end
RegisteredEvents[eventName] = origAddEventHandler(eventName, ...) RegisteredEvents[eventName] = origAddEventHandler(eventName, ...)
return RegisteredEvents[eventName] return RegisteredEvents[eventName]
end end
local origRegisterNetEvent = RegisterNetEvent local origRegisterNetEvent = RegisterNetEvent
function RegisterNetEvent(eventName, ...) function RegisterNetEvent(eventName, ...)
if RegisteredEvents[eventName] then if RegisteredEvents[eventName] then
main:LogWarning("Event %s already registered. Removing", eventName) main:LogWarning("Event %s already registered. Removing", eventName)
RemoveEventHandler(RegisteredEvents[eventName]) RemoveEventHandler(RegisteredEvents[eventName])
end end
RegisteredEvents[eventName] = origRegisterNetEvent(eventName, ...) RegisteredEvents[eventName] = origRegisterNetEvent(eventName, ...)
return RegisteredEvents[eventName] return RegisteredEvents[eventName]
end end
Citizen.CreateThread(function() Citizen.CreateThread(function()
while GetGameTimer() < main.lastTimeImplRegistered + 1000 do while GetGameTimer() < main.lastTimeImplRegistered + 1000 do
Citizen.Wait(0) Citizen.Wait(0)
end end
while Framework == nil do while Framework == nil do
main:LogInfo("Waiting for Framework") main:LogInfo("Waiting for Framework")
Wait(100) Wait(100)
end end
main:InitImpl() main:InitImpl()
if not IsDuplicityVersion() then if not IsDuplicityVersion() then
if Config.Framework == 'esx' then if Config.Framework == 'esx' then
while not Framework.IsPlayerLoaded() do while not Framework.IsPlayerLoaded() do
Wait(100) Wait(100)
end end
elseif Config.Framework == 'qb' then elseif Config.Framework == 'qb' then
local player = Framework.Functions.GetPlayerData() local player = Framework.Functions.GetPlayerData()
while player == nil do while player == nil do
Wait(100) Wait(100)
player = Framework.Functions.GetPlayerData() player = Framework.Functions.GetPlayerData()
end end
end end
while not NuiReady and Config.Nui do while not NuiReady and Config.Nui do
Wait(100) Wait(100)
end end
end end