package_support.lua 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. --[[
  2. Copyright (c) 2011-2015 chukong-incc.com
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. ]]
  19. -- Cocos2d-Lua core functions
  20. cc.loaded_packages = {}
  21. local loaded_packages = cc.loaded_packages
  22. function cc.register(name, package)
  23. cc.loaded_packages[name] = package
  24. end
  25. function cc.load(...)
  26. local names = {...}
  27. assert(#names > 0, "cc.load() - invalid package names")
  28. local packages = {}
  29. for _, name in ipairs(names) do
  30. assert(type(name) == "string", string.format("cc.load() - invalid package name \"%s\"", tostring(name)))
  31. if not loaded_packages[name] then
  32. local packageName = string.format("packages.%s.init", name)
  33. local cls = require(packageName)
  34. assert(cls, string.format("cc.load() - package class \"%s\" load failed", packageName))
  35. loaded_packages[name] = cls
  36. if DEBUG > 1 then
  37. printInfo("cc.load() - load module \"packages.%s.init\"", name)
  38. end
  39. end
  40. packages[#packages + 1] = loaded_packages[name]
  41. end
  42. return unpack(packages)
  43. end
  44. local load_ = cc.load
  45. local bind_
  46. bind_ = function(target, ...)
  47. local t = type(target)
  48. assert(t == "table" or t == "userdata", string.format("cc.bind() - invalid target, expected is object, actual is %s", t))
  49. local names = {...}
  50. assert(#names > 0, "cc.bind() - package names expected")
  51. load_(...)
  52. if not target.components_ then target.components_ = {} end
  53. for _, name in ipairs(names) do
  54. assert(type(name) == "string" and name ~= "", string.format("cc.bind() - invalid package name \"%s\"", name))
  55. if not target.components_[name] then
  56. local cls = loaded_packages[name]
  57. for __, depend in ipairs(cls.depends or {}) do
  58. if not target.components_[depend] then
  59. bind_(target, depend)
  60. end
  61. end
  62. local component = cls:create()
  63. target.components_[name] = component
  64. component:bind(target)
  65. end
  66. end
  67. return target
  68. end
  69. cc.bind = bind_
  70. function cc.unbind(target, ...)
  71. if not target.components_ then return end
  72. local names = {...}
  73. assert(#names > 0, "cc.unbind() - invalid package names")
  74. for _, name in ipairs(names) do
  75. assert(type(name) == "string" and name ~= "", string.format("cc.unbind() - invalid package name \"%s\"", name))
  76. local component = target.components_[name]
  77. assert(component, string.format("cc.unbind() - component \"%s\" not found", tostring(name)))
  78. component:unbind(target)
  79. target.components_[name] = nil
  80. end
  81. return target
  82. end
  83. function cc.setmethods(target, component, methods)
  84. for _, name in ipairs(methods) do
  85. local method = component[name]
  86. target[name] = function(__, ...)
  87. return method(component, ...)
  88. end
  89. end
  90. end
  91. function cc.unsetmethods(target, methods)
  92. for _, name in ipairs(methods) do
  93. target[name] = nil
  94. end
  95. end