fix function ref
This commit is contained in:
parent
65c77d1621
commit
5e67c0d7e0
107
impl.lua
107
impl.lua
|
@ -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)
|
||||
|
@ -221,6 +220,7 @@ 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
|
||||
impl = impl:GetName()
|
||||
|
@ -230,6 +230,7 @@ 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
|
||||
impl = impl:GetName()
|
||||
|
@ -247,6 +248,7 @@ else
|
|||
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
|
||||
impl = impl:GetName()
|
||||
|
@ -255,6 +257,7 @@ else
|
|||
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
|
||||
impl = impl:GetName()
|
||||
|
@ -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
|
||||
})
|
||||
main:RegisterImpl(name, impl)
|
||||
return impl
|
||||
end
|
Loading…
Reference in New Issue
Block a user