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