This commit is contained in:
Lorraxs 2024-04-08 12:37:20 +07:00
parent 0935cbf272
commit d0275487e0
2 changed files with 41 additions and 19 deletions

View File

@ -1,9 +1,15 @@
---@class Class
---@field destroyed boolean
---@field originalMethods table
---@field eventHandlers table
Class = {}
env = IsDuplicityVersion() and "sv" or "cl"
-- default (empty) constructor
function Class:Init(...) end
-- create a subclass
---comment
---@param obj any
---@return any
function Class:extend(obj)
local obj = obj or {}
@ -81,9 +87,11 @@ end
function Class:set(prop, value)
if not value and type(prop) == "table" then
for k, v in pairs(prop) do
---@diagnostic disable-next-line: undefined-field
rawset(self._, k, v)
end
else
---@diagnostic disable-next-line: undefined-field
rawset(self._, prop, value)
end
end
@ -99,11 +107,16 @@ function Class:new(...)
return obj
end
---@diagnostic disable-next-line: lowercase-global
function class(attr)
attr = attr or {}
return Class:extend(attr)
end
---@class Impl:Class
---@field name string
---@field config table
---@field implType "impl"
Impl = class()
function Impl:GetName()
@ -288,7 +301,11 @@ function Impl:GetConfig()
return self.config
end
---Create a new Implemented class
---@param name string
---@return Impl
function NewImpl(name)
---@type Impl
local impl = Impl:extend({
name = name,
config = Config[name] or {},

View File

@ -13,6 +13,9 @@ else
end
end
---@class Main
---@field impls table<string, Impl>
---@field initializedImpls table<string, Impl>
Main = {}
ResourceName = GetCurrentResourceName()
local RegisteredEvents = {}
@ -26,6 +29,9 @@ else
NuiReady = true
end)
end
---Init main class
---@return Main
function Main:Init()
local o = {}
setmetatable(o, { __index = Main })
@ -42,7 +48,6 @@ function Main:Init()
o.playerServerId = GetPlayerServerId(o.playerId)
o:Thread1()
else
if Config.ClientLazyLoad then
o.ClientImpls = {}
for k, v in pairs(Config.EnableModules) do
if v then
@ -61,7 +66,6 @@ function Main:Init()
return o.ClientImpls[implName]
end)
end
end
o:Exports()
o:RegisterCommands()
o:RegisterEvents()
@ -75,6 +79,7 @@ end
if not IsDuplicityVersion() then
function Main:Thread1()
self.playerServerId = GetPlayerServerId(self.playerId)
Citizen.CreateThread(function()
while true do
self.playerId = PlayerId()
@ -127,7 +132,7 @@ function Main:RegisterCommands()
end
local source = LoadResourceFile(ResourceName, "server/impl/" .. implName .. ".impl.lua")
if source == nil then
self:LogWarning("Failed to load %s", path)
self:LogWarning("Failed to load %s", ResourceName, "server/impl/" .. implName .. ".impl.lua")
else
self:LogInfo("Loading %s", implName)
load(source)()
@ -136,7 +141,7 @@ function Main:RegisterCommands()
if mode == "0" or mode == "1" then
local clSource = LoadResourceFile(ResourceName, "client/impl/" .. implName .. ".impl.lua")
if clSource == nil then
self:LogWarning("Failed to load %s", path)
self:LogWarning("Failed to load %s", ResourceName, "client/impl/" .. implName .. ".impl.lua")
else
self:LogInfo("Loading %s", "client/impl/" .. implName .. ".impl.lua")
TriggerClientEvent(ResourceName .. ":restartClientImpl", -1, implName, clSource)