sequFrameAnimLoader.lua 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. local sequFrameAnimLoader = class("sequFrameAnimLoader")
  2. sequFrameAnimLoader.DEFAULT_DIR = global.MMO.ORIENT_B
  3. function sequFrameAnimLoader:ctor()
  4. self:init()
  5. end
  6. function sequFrameAnimLoader:init()
  7. self._animDataInLoading = {}
  8. end
  9. function sequFrameAnimLoader:Cleanup()
  10. self._animDataInLoading = {}
  11. end
  12. function sequFrameAnimLoader:getConfig( animID )
  13. local ModelConfigProxy = global.Facade:retrieveProxy( global.ProxyTable.ModelConfigProxy )
  14. return ModelConfigProxy:GetConfig( animID )
  15. end
  16. function sequFrameAnimLoader:UNITE_ID( ID, PARAM )
  17. return ID * 0xFF + PARAM
  18. end
  19. function sequFrameAnimLoader:convertTexFileName( rawAnimID, sex, act )
  20. end
  21. function sequFrameAnimLoader:convertPlistFileName( rawAnimID, sex, act )
  22. end
  23. function sequFrameAnimLoader:convertTexFileNameByIdx( rawAnimID, sex, act, idx )
  24. end
  25. function sequFrameAnimLoader:convertPlistFileNameByIdx( rawAnimID, sex, act, idx )
  26. end
  27. function sequFrameAnimLoader:convertFrameName( rawAnimID, sex, act, dir, frameID )
  28. end
  29. function sequFrameAnimLoader:OnLoadingBegin()
  30. global.FrameAnimManager:AddLoadingCount()
  31. end
  32. function sequFrameAnimLoader:OnLoadingFinish()
  33. global.FrameAnimManager:DecLoadingCount()
  34. end
  35. function sequFrameAnimLoader:notifyCaller( animDataDesc, act, loadcode )
  36. if nil == self._animDataInLoading[self:UNITE_ID( animDataDesc.animID, act )] then
  37. return nil
  38. end
  39. if animDataDesc.loadCallback then
  40. animDataDesc.loadCallback( act, loadcode )
  41. end
  42. -- clear loading state
  43. self._animDataInLoading[self:UNITE_ID( animDataDesc.animID, act ) ] = nil
  44. end
  45. function sequFrameAnimLoader:loadSequAnim( animDataDesc, act )
  46. local animID = animDataDesc.animID
  47. local loadingID = self:UNITE_ID( animID, act )
  48. local loadDesc = self._animDataInLoading[loadingID]
  49. if nil == loadDesc then
  50. self._animDataInLoading[loadingID] = animDataDesc
  51. -- on load begin notice
  52. self:OnLoadingBegin()
  53. -- check unload unused
  54. global.FrameAnimManager:unloadUnusedInOOM()
  55. -- start loading
  56. self:animResTextureLoad( animDataDesc, act )
  57. end
  58. end
  59. function sequFrameAnimLoader:releaseCocosAnim( animDataDesc, act, actionDir )
  60. local animIndex = 0
  61. local animFrameVec = nil
  62. for dir = 0, actionDir do
  63. animIndex = GetFrameAnimIndex( act, dir )
  64. animFrameVec = animDataDesc.animFrameVecs[animIndex]
  65. if animFrameVec then
  66. for _, animFrame in ipairs( animFrameVec ) do
  67. if (not tolua.isnull(animFrame)) then
  68. animFrame:release()
  69. end
  70. end
  71. end
  72. animDataDesc.animFrameVecs[animIndex] = nil
  73. end
  74. end
  75. function sequFrameAnimLoader:unloadSequAnim( animDataDesc )
  76. local animCount = global.MMO.ANIM_COUNT
  77. local actionDir = 7
  78. for act = 0, animCount - 1 do
  79. if true == animDataDesc.frameLoaded[act] then
  80. -- step1 cocos anim
  81. self:releaseCocosAnim( animDataDesc, act, actionDir )
  82. -- remove anim texture
  83. self:removeTextureCache(animDataDesc, act)
  84. animDataDesc.frameLoaded[act] = nil
  85. end
  86. end
  87. end
  88. function sequFrameAnimLoader:animResTextureLoad( animDataDesc, act )
  89. local modelProxy = global.Facade:retrieveProxy( global.ProxyTable.ModelConfigProxy )
  90. local animID = animDataDesc.animID
  91. local config = modelProxy:GetConfig( animID )
  92. if nil == config then
  93. self:animResConfigFailed(animDataDesc, act)
  94. return
  95. end
  96. -- calc temporary ID
  97. local temporaryID = global.FrameAnimManager:GetAnimTemporaryID(animDataDesc.animType, animDataDesc.sex)
  98. -- out of memory
  99. if global.FrameAnimManager:isMemWarning(animDataDesc.animType) and animDataDesc.animID ~= temporaryID then
  100. self:animResMemoryWarning(animDataDesc, act)
  101. return
  102. end
  103. -- check loading
  104. if animDataDesc.animID == temporaryID then
  105. local texFile = self:convertTexFileName( animDataDesc.rawAnimID, animDataDesc.sex, act )
  106. local plistFile = self:convertPlistFileName( animDataDesc.rawAnimID, animDataDesc.sex, act )
  107. if not global.SpriteFrameCache:isSpriteFramesWithFileLoaded(plistFile) then
  108. local function addImageCallback( tex, plistOK )
  109. -- for memory record
  110. global.FrameAnimManager:textureMemAdd(tex, texFile)
  111. self:animResTextureLoadCB( animDataDesc, act, tex, plistOK, config )
  112. end
  113. global.SpriteFrameCache:addSpriteFramesWithFileAsync( plistFile, texFile, addImageCallback )
  114. else
  115. self:animResTextureLoadCB( animDataDesc, act, true, true, config )
  116. end
  117. else
  118. local atlasCount = modelProxy:GetSplitAtlas( animID, act )
  119. if atlasCount and atlasCount > 1 then
  120. for idx = 0, atlasCount-1 do
  121. local texFile = self:convertTexFileNameByIdx( animDataDesc.rawAnimID, animDataDesc.sex, act, idx )
  122. local plistFile = self:convertPlistFileNameByIdx( animDataDesc.rawAnimID, animDataDesc.sex, act, idx )
  123. if not global.FileUtilCtl:isFileExist(plistFile) then
  124. self:downloadRes(plistFile, function(isOK)
  125. if isOK then
  126. self:animResTextureLoad(animDataDesc, act)
  127. else
  128. self:animResDownloadFailed(animDataDesc, act)
  129. end
  130. end)
  131. break
  132. elseif not global.FileUtilCtl:isFileExist(texFile) then
  133. self:downloadRes(texFile, function(isOK)
  134. if isOK then
  135. self:animResTextureLoad(animDataDesc, act)
  136. else
  137. self:animResDownloadFailed(animDataDesc, act)
  138. end
  139. end)
  140. break
  141. elseif not global.SpriteFrameCache:isSpriteFramesWithFileLoaded(plistFile) then
  142. local function addImageCallback( tex, plistOK )
  143. -- for memory record
  144. global.FrameAnimManager:textureMemAdd(tex, texFile)
  145. if not plistOK or nil == tex then
  146. self:animResTextureLoadCB(animDataDesc, act, nil, false, config)
  147. else
  148. self:animResTextureLoad(animDataDesc, act)
  149. end
  150. end
  151. global.SpriteFrameCache:addSpriteFramesWithFileAsync( plistFile, texFile, addImageCallback )
  152. break
  153. elseif idx == atlasCount -1 then
  154. self:animResTextureLoadCB( animDataDesc, act, true, true, config )
  155. end
  156. end
  157. else
  158. local texFile = self:convertTexFileName( animDataDesc.rawAnimID, animDataDesc.sex, act )
  159. local plistFile = self:convertPlistFileName( animDataDesc.rawAnimID, animDataDesc.sex, act )
  160. if not global.FileUtilCtl:isFileExist(plistFile) then
  161. self:downloadRes(plistFile, function(isOK)
  162. if isOK then
  163. self:animResTextureLoad(animDataDesc, act)
  164. else
  165. self:animResDownloadFailed(animDataDesc, act)
  166. end
  167. end)
  168. elseif not global.FileUtilCtl:isFileExist(texFile) then
  169. self:downloadRes(texFile, function(isOK)
  170. if isOK then
  171. self:animResTextureLoad(animDataDesc, act)
  172. else
  173. self:animResDownloadFailed(animDataDesc, act)
  174. end
  175. end)
  176. elseif not global.SpriteFrameCache:isSpriteFramesWithFileLoaded(plistFile) then
  177. local function addImageCallback( tex, plistOK )
  178. -- for memory record
  179. global.FrameAnimManager:textureMemAdd(tex, texFile)
  180. self:animResTextureLoadCB( animDataDesc, act, tex, plistOK, config )
  181. end
  182. global.SpriteFrameCache:addSpriteFramesWithFileAsync( plistFile, texFile, addImageCallback )
  183. else
  184. self:animResTextureLoadCB( animDataDesc, act, true, true, config )
  185. end
  186. end
  187. end
  188. end
  189. function sequFrameAnimLoader:animResTextureLoadCB( animDataDesc, act, tex, plistOK, config )
  190. if not plistOK or nil == tex then
  191. -- remove anim texture
  192. self:removeTextureCache(animDataDesc, act)
  193. -- except temporary
  194. local temporaryID = global.FrameAnimManager:GetAnimTemporaryID(animDataDesc.animType, animDataDesc.sex)
  195. if animDataDesc.animID ~= temporaryID then
  196. local modelProxy = global.Facade:retrieveProxy( global.ProxyTable.ModelConfigProxy )
  197. local splitCount = modelProxy:GetSplitAtlas( animDataDesc.animID, act )
  198. if splitCount then
  199. for i = 0, splitCount-1 do
  200. -- del plist
  201. local plistFile = self:convertPlistFileNameByIdx( animDataDesc.rawAnimID, animDataDesc.sex, act , i )
  202. self:onLoadFileError(plistFile)
  203. -- del png
  204. local texFile = self:convertTexFileNameByIdx( animDataDesc.rawAnimID, animDataDesc.sex, act ,i )
  205. self:onLoadFileError(texFile)
  206. end
  207. else
  208. -- del plist
  209. local plistFile = self:convertPlistFileName( animDataDesc.rawAnimID, animDataDesc.sex, act )
  210. self:onLoadFileError(plistFile)
  211. -- del png
  212. local texFile = self:convertTexFileName( animDataDesc.rawAnimID, animDataDesc.sex, act )
  213. self:onLoadFileError(texFile)
  214. end
  215. end
  216. else
  217. -- creat sprite frame
  218. self:createAnimation( animDataDesc, act, config )
  219. end
  220. -- notify
  221. if not plistOK then
  222. self:notifyCaller( animDataDesc, act, global.MMO.ANIM_LOAD_FRAME )
  223. elseif nil == tex then
  224. self:notifyCaller( animDataDesc, act, global.MMO.ANIM_LOAD_TEX )
  225. else
  226. self:notifyCaller( animDataDesc, act, global.MMO.ANIM_LOAD_SUCCESS )
  227. end
  228. -- on load finish notice
  229. self:OnLoadingFinish()
  230. end
  231. function sequFrameAnimLoader:animResConfigFailed(animDataDesc, act)
  232. self:notifyCaller( animDataDesc, act, global.MMO.ANIM_LOAD_CONFIG )
  233. -- on load finish notice
  234. self:OnLoadingFinish()
  235. end
  236. function sequFrameAnimLoader:animResMemoryWarning(animDataDesc, act)
  237. self:notifyCaller( animDataDesc, act, global.MMO.ANIM_LOAD_OOM )
  238. -- remove anim texture
  239. self:removeTextureCache(animDataDesc, act)
  240. -- on load finish notice
  241. self:OnLoadingFinish()
  242. end
  243. function sequFrameAnimLoader:animResDownloadFailed( animDataDesc, act )
  244. self:notifyCaller( animDataDesc, act, global.MMO.ANIM_LOAD_TIMEOUT )
  245. -- on load finish notice
  246. self:OnLoadingFinish()
  247. end
  248. function sequFrameAnimLoader:createAnimation( animDataDesc, act, config )
  249. local animID = animDataDesc.animID
  250. local sex = animDataDesc.sex
  251. local dir = 0
  252. local frameName = ""
  253. local spriteFrame = nil
  254. local animFrame = nil
  255. local actionDir = 7
  256. -- 施法动作重复第五帧
  257. local additionACT = (act == global.MMO.ANIM_SKILL)
  258. for i = 0, actionDir do
  259. dir = i
  260. local animIndex = GetFrameAnimIndex( act, i )
  261. local animFrameVec = {}
  262. local animFrameCnt = 0
  263. for xy = 0, 100 do
  264. frameName = self:convertFrameName( animDataDesc.rawAnimID, sex, act, dir, xy )
  265. spriteFrame = global.SpriteFrameCache:getSpriteFrame( frameName, true )
  266. if spriteFrame then
  267. animFrame = cc.AnimationFrame:create( spriteFrame, 1, {} )
  268. animFrame:retain()
  269. animFrameCnt = animFrameCnt + 1
  270. animFrameVec[animFrameCnt] = animFrame
  271. if additionACT and xy == 4 then
  272. animFrame = cc.AnimationFrame:create( spriteFrame, 1, {} )
  273. animFrame:retain()
  274. animFrameCnt = animFrameCnt + 1
  275. animFrameVec[animFrameCnt] = animFrame
  276. animFrame = cc.AnimationFrame:create( spriteFrame, 1, {} )
  277. animFrame:retain()
  278. animFrameCnt = animFrameCnt + 1
  279. animFrameVec[animFrameCnt] = animFrame
  280. end
  281. else
  282. -- 其他帧可能没有,只取0方向作为间隔
  283. if not config.frame[act] then
  284. config.frame[act] = xy
  285. end
  286. break
  287. end
  288. end
  289. local inerval = config.interval[act] or global.MMO.ANIM_DEFAULT_INTERVAL
  290. animDataDesc.animFrameVecs[animIndex] = animFrameCnt > 0 and animFrameVec or nil
  291. animDataDesc.animDelays[animIndex] = additionACT and (inerval * 0.75) or inerval
  292. end
  293. animDataDesc.frameLoaded[act] = true
  294. end
  295. function sequFrameAnimLoader:removeTextureCache(animDataDesc, act)
  296. local modelProxy = global.Facade:retrieveProxy( global.ProxyTable.ModelConfigProxy )
  297. local atlasCount = modelProxy:GetSplitAtlas( animDataDesc.animID, act )
  298. if atlasCount and atlasCount > 1 then
  299. for idx = 0, atlasCount-1 do
  300. local texFile = self:convertTexFileNameByIdx( animDataDesc.rawAnimID, animDataDesc.sex, act, idx )
  301. local plistFile = self:convertPlistFileNameByIdx( animDataDesc.rawAnimID, animDataDesc.sex, act, idx )
  302. if global.SpriteFrameCache:isSpriteFramesWithFileLoaded( plistFile ) then
  303. global.SpriteFrameCache:removeSpriteFramesFromFile( plistFile )
  304. global.TextureCache:removeTextureForKey( texFile )
  305. -- for memory record
  306. global.FrameAnimManager:textureMemDel(texFile)
  307. end
  308. end
  309. else
  310. local texFile = self:convertTexFileName( animDataDesc.rawAnimID, animDataDesc.sex, act )
  311. local plistFile = self:convertPlistFileName( animDataDesc.rawAnimID, animDataDesc.sex, act )
  312. if global.SpriteFrameCache:isSpriteFramesWithFileLoaded( plistFile ) then
  313. global.SpriteFrameCache:removeSpriteFramesFromFile( plistFile )
  314. global.TextureCache:removeTextureForKey( texFile )
  315. -- for memory record
  316. global.FrameAnimManager:textureMemDel(texFile)
  317. end
  318. end
  319. end
  320. --------------------- download ---------------------
  321. function sequFrameAnimLoader:downloadRes(path, callback)
  322. global.ResDownloader:download(path, callback)
  323. end
  324. function sequFrameAnimLoader:onLoadFileError(filePath)
  325. release_print("onLoadFileError", filePath)
  326. if not global.FileUtilCtl:isFileExist(filePath) then
  327. return
  328. end
  329. local fullPath = global.FileUtilCtl:fullPathForFilename(filePath)
  330. if not (fullPath ~= "" and string.find(fullPath, global.FileUtilCtl:getWritablePath())) then
  331. return
  332. end
  333. global.FileUtilCtl:removeFile( fullPath )
  334. global.FileUtilCtl:purgeCachedEntries()
  335. end
  336. ----------------------------------------------------
  337. return sequFrameAnimLoader