fix function ref

This commit is contained in:
Lorraxs 2024-01-10 13:56:52 +07:00
parent 65c77d1621
commit 5e67c0d7e0

121
impl.lua
View File

@ -3,7 +3,6 @@ env = IsDuplicityVersion() and "sv" or "cl"
-- default (empty) constructor -- default (empty) constructor
function Class:Init(...) end function Class:Init(...) end
-- create a subclass -- create a subclass
function Class:extend(obj) function Class:extend(obj)
local obj = obj or {} local obj = obj or {}
@ -39,7 +38,8 @@ function Class:extend(obj)
-- allow for getters and setters -- allow for getters and setters
mt.__index = function(table, key) mt.__index = function(table, key)
local val = rawget(table._, key) local val = rawget(table._, key)
if val and type(val) == "table" and (val.get ~= nil or val.value ~= nil) then --print(key, type(val))
if val and type(val) == "table" and (rawget(val, '__cfx_functionReference') == nil) and (val.get ~= nil or val.value ~= nil) then
if val.get then if val.get then
if type(val.get) == "function" then if type(val.get) == "function" then
return val.get(table, val.value) return val.get(table, val.value)
@ -56,7 +56,7 @@ function Class:extend(obj)
mt.__newindex = function(table, key, value) mt.__newindex = function(table, key, value)
local val = rawget(table._, key) local val = rawget(table._, key)
if val and type(val) == "table" and ((val.set ~= nil and val._ == nil) or val.value ~= nil) then if val and type(val) == "table" and (rawget(val, '__cfx_functionReference') == nil) and ((val.set ~= nil and val._ == nil) or val.value ~= nil) then
local v = value local v = value
if val.set then if val.set then
if type(val.set) == "function" then if type(val.set) == "function" then
@ -91,15 +91,14 @@ end
-- create an instance of an object with constructor parameters -- create an instance of an object with constructor parameters
function Class:new(...) function Class:new(...)
local obj = self:extend({ local obj = self:extend({
destroyed = false, destroyed = false,
originalMethods = {}, originalMethods = {},
eventHandlers = {} eventHandlers = {}
}) })
if obj.Init then obj:Init(...) end if obj.Init then obj:Init(...) end
return obj return obj
end end
function class(attr) function class(attr)
attr = attr or {} attr = attr or {}
return Class:extend(attr) return Class:extend(attr)
@ -108,12 +107,12 @@ end
Impl = class() Impl = class()
function Impl:GetName() function Impl:GetName()
return self.name return self.name
end end
function Impl:Destroy() function Impl:Destroy()
self.destroyed = true self.destroyed = true
main:LogInfo("%s destroyed", self.name) main:LogInfo("%s destroyed", self.name)
for k, v in pairs(self.eventHandlers) do for k, v in pairs(self.eventHandlers) do
RemoveEventHandler(v) RemoveEventHandler(v)
end end
@ -127,49 +126,49 @@ function Impl:OnDestroy(...)
end end
function Impl:HookMethod(method, hookFn) function Impl:HookMethod(method, hookFn)
local oldMethod = self[method] local oldMethod = self[method]
if not oldMethod then if not oldMethod then
main:LogError("Impl %s missing method %s", self.name, method) main:LogError("Impl %s missing method %s", self.name, method)
return return
end end
self.originalMethods[method] = oldMethod self.originalMethods[method] = oldMethod
self[method] = function(...) self[method] = function(...)
if self.destroyed then if self.destroyed then
return return
end end
local result = {pcall(hookFn, ...)} local result = { pcall(hookFn, ...) }
local success = table.remove(result, 1) local success = table.remove(result, 1)
if not success then if not success then
main:LogError("Impl %s hook %s error: %s", self.name, method, result[2]) main:LogError("Impl %s hook %s error: %s", self.name, method, result[2])
self[method] = oldMethod self[method] = oldMethod
return oldMethod(...) return oldMethod(...)
end end
return oldMethod(self, table.unpack(result)) return oldMethod(self, table.unpack(result))
end end
end end
function Impl:GetMethod(method) function Impl:GetMethod(method)
return self[method] return self[method]
end end
function Impl:ReplaceMethod(method, newMethod) function Impl:ReplaceMethod(method, newMethod)
if not self[method] then if not self[method] then
main:LogError("Impl %s missing method %s", self.name, method) main:LogError("Impl %s missing method %s", self.name, method)
return return
end end
if not self.originalMethods[method] then if not self.originalMethods[method] then
self.originalMethods[method] = self[method] self.originalMethods[method] = self[method]
end end
self[method] = newMethod self[method] = newMethod
end end
function Impl:RefreshMethod(method) function Impl:RefreshMethod(method)
if not self.originalMethods[method] then if not self.originalMethods[method] then
main:LogError("Impl %s missing method %s", self.name, method) main:LogError("Impl %s missing method %s", self.name, method)
return return
end end
self[method] = self.originalMethods[method] self[method] = self.originalMethods[method]
end end
function Impl:RegisterCallback(name, cb) function Impl:RegisterCallback(name, cb)
@ -213,7 +212,7 @@ end
if env == 'sv' then if env == 'sv' then
function Impl:Callback(impl, name, source, ...) function Impl:Callback(impl, name, source, ...)
if type(impl) == "object" then if type(impl) == "object" then
impl = impl:GetName() impl = impl:GetName()
end end
if not impl then return main:LogError("param impl missing") end if not impl then return main:LogError("param impl missing") end
@ -221,8 +220,9 @@ if env == 'sv' then
if not source then return main:LogError("param source missing") end if not source then return main:LogError("param source missing") end
return lib.callback.await(("%s_%s:%s"):format(impl, "cl", name), source, ...) return lib.callback.await(("%s_%s:%s"):format(impl, "cl", name), source, ...)
end end
function Impl:EmitNet(impl, name, source, ...) function Impl:EmitNet(impl, name, source, ...)
if type(impl) == "object" then if type(impl) == "object" then
impl = impl:GetName() impl = impl:GetName()
end end
if not impl then return main:LogError("param impl missing") end if not impl then return main:LogError("param impl missing") end
@ -230,8 +230,9 @@ if env == 'sv' then
if not source then return main:LogError("param source missing") end if not source then return main:LogError("param source missing") end
return TriggerClientEvent(("%s_%s:%s"):format(impl, "cl", name), source, ...) return TriggerClientEvent(("%s_%s:%s"):format(impl, "cl", name), source, ...)
end end
function Impl:Emit(impl, name, ...) function Impl:Emit(impl, name, ...)
if type(impl) == "object" then if type(impl) == "object" then
impl = impl:GetName() impl = impl:GetName()
end end
if not impl then return main:LogError("param impl missing") end if not impl then return main:LogError("param impl missing") end
@ -240,23 +241,25 @@ if env == 'sv' then
end end
else else
function Impl:Callback(impl, name, ...) function Impl:Callback(impl, name, ...)
if type(impl) == "object" then if type(impl) == "object" then
impl = impl:GetName() impl = impl:GetName()
end end
if not impl then return main:LogError("param impl missing") end if not impl then return main:LogError("param impl missing") end
if not name then return main:LogError("param name missing") end if not name then return main:LogError("param name missing") end
return lib.callback.await(("%s_%s:%s"):format(impl, "sv", name), false, ...) return lib.callback.await(("%s_%s:%s"):format(impl, "sv", name), false, ...)
end end
function Impl:Emit(impl, name, ...) function Impl:Emit(impl, name, ...)
if type(impl) == "object" then if type(impl) == "object" then
impl = impl:GetName() impl = impl:GetName()
end end
if not impl then return main:LogError("param impl missing") end if not impl then return main:LogError("param impl missing") end
if not name then return main:LogError("param name missing") end if not name then return main:LogError("param name missing") end
return TriggerEvent(("%s_%s:%s"):format(impl, "cl", name), ...) return TriggerEvent(("%s_%s:%s"):format(impl, "cl", name), ...)
end end
function Impl:EmitNet(impl, name, ...) function Impl:EmitNet(impl, name, ...)
if type(impl) == "object" then if type(impl) == "object" then
impl = impl:GetName() impl = impl:GetName()
end end
if not impl then return main:LogError("param impl missing") end if not impl then return main:LogError("param impl missing") end
@ -266,19 +269,19 @@ else
end end
function Impl:LogInfo(msg, ...) function Impl:LogInfo(msg, ...)
main:LogInfo("[^6"..self.name.."^0] "..msg, ...) main:LogInfo("[^6" .. self.name .. "^0] " .. msg, ...)
end end
function Impl:LogError(msg, ...) function Impl:LogError(msg, ...)
main:LogError("[^6"..self.name.."^0] "..msg, ...) main:LogError("[^6" .. self.name .. "^0] " .. msg, ...)
end end
function Impl:LogSuccess(msg, ...) function Impl:LogSuccess(msg, ...)
main:LogSuccess("[^6"..self.name.."^0] "..msg, ...) main:LogSuccess("[^6" .. self.name .. "^0] " .. msg, ...)
end end
function Impl:LogWarning(msg, ...) function Impl:LogWarning(msg, ...)
main:LogWarning("[^6"..self.name.."^0] "..msg, ...) main:LogWarning("[^6" .. self.name .. "^0] " .. msg, ...)
end end
function Impl:GetConfig() function Impl:GetConfig()
@ -286,10 +289,10 @@ function Impl:GetConfig()
end end
function NewImpl(name) function NewImpl(name)
local impl = Impl:extend({ local impl = Impl:extend({
name = name, name = name,
config = Config[name] or {}, config = Config[name] or {},
}) })
main:RegisterImpl(name, impl) main:RegisterImpl(name, impl)
return impl return impl
end end