mirror of
https://github.com/Keyslam-Group/Concord.git
synced 2025-09-02 12:24:11 -04:00
CONCORD IS DEAD
Long live Concord!!
This commit is contained in:
parent
5dffe04b72
commit
c640641b09
16 changed files with 263 additions and 424 deletions
|
@ -7,42 +7,51 @@ local Type = require(PATH..".type")
|
|||
|
||||
local Components = {}
|
||||
|
||||
--- Registers a ComponentClass.
|
||||
-- @string name Name to register under
|
||||
-- @tparam Component componentClass ComponentClass to register
|
||||
function Components.register(name, componentClass)
|
||||
if (type(name) ~= "string") then
|
||||
error("bad argument #1 to 'Components.register' (string expected, got "..type(name)..")", 3)
|
||||
local try = function (name)
|
||||
if type(name) ~= "string" then
|
||||
return false, "ComponentsClass name is expected to be a string, got "..type(name)..")"
|
||||
end
|
||||
|
||||
if (not Type.isComponentClass(componentClass)) then
|
||||
error("bad argument #2 to 'Components.register' (ComponentClass expected, got "..type(componentClass)..")", 3)
|
||||
local value = rawget(Components, name)
|
||||
if not value then
|
||||
return false, "ComponentClass '"..name.."' does not exist / was not registered"
|
||||
end
|
||||
|
||||
if (rawget(Components, name)) then
|
||||
error("bad argument #2 to 'Components.register' (ComponentClass with name '"..name.."' was already registerd)", 3) -- luacheck: ignore
|
||||
end
|
||||
|
||||
Components[name] = componentClass
|
||||
componentClass.__name = name
|
||||
return true, value
|
||||
end
|
||||
|
||||
--- Returns true if the containter has the ComponentClass with the specified name
|
||||
-- @string name Name of the ComponentClass to check
|
||||
-- @treturn boolean
|
||||
function Components.has(name)
|
||||
return Components[name] and true or false
|
||||
return rawget(Components, name) and true or false
|
||||
end
|
||||
|
||||
--- Returns true and the ComponentClass if one was registered with the specified name
|
||||
-- or false and an error otherwise
|
||||
-- @string name Name of the ComponentClass to check
|
||||
-- @treturn boolean
|
||||
-- @treturn Component or error string
|
||||
function Components.try(name)
|
||||
return try(name)
|
||||
end
|
||||
|
||||
--- Returns the ComponentClass with the specified name
|
||||
-- @string name Name of the ComponentClass to get
|
||||
-- @treturn Component
|
||||
function Components.get(name)
|
||||
return Components[name]
|
||||
local ok, value = try(name)
|
||||
|
||||
if not ok then error(value, 2) end
|
||||
|
||||
return value
|
||||
end
|
||||
|
||||
return setmetatable(Components, {
|
||||
__index = function(_, name)
|
||||
error("Attempt to index ComponentClass '"..tostring(name).."' that does not exist / was not registered", 2)
|
||||
end
|
||||
local ok, value = try(name)
|
||||
|
||||
if not ok then error(value, 2) end
|
||||
|
||||
return value end
|
||||
})
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue