CCDataReaderHelper.cpp 86 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576
  1. /****************************************************************************
  2. Copyright (c) 2013-2017 Chukong Technologies Inc.
  3. http://www.cocos2d-x.org
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. ****************************************************************************/
  20. #include "platform/CCFileUtils.h"
  21. #include "base/CCDirector.h"
  22. #include "base/CCScheduler.h"
  23. #include "base/ccUtils.h"
  24. #include "tinyxml2.h"
  25. #include "editor-support/cocostudio/CCDataReaderHelper.h"
  26. #include "editor-support/cocostudio/CCArmatureDataManager.h"
  27. #include "editor-support/cocostudio/CCTransformHelp.h"
  28. #include "editor-support/cocostudio/CCUtilMath.h"
  29. #include "editor-support/cocostudio/CCArmatureDefine.h"
  30. #include "editor-support/cocostudio/CCDatas.h"
  31. #include "editor-support/cocostudio/CocoLoader.h"
  32. using namespace cocos2d;
  33. static const char *VERSION = "version";
  34. static const float VERSION_2_0 = 2.0f;
  35. static const char *ARMATURES = "armatures";
  36. static const char *ARMATURE = "armature";
  37. static const char *BONE = "b";
  38. static const char *DISPLAY = "d";
  39. static const char *ANIMATIONS = "animations";
  40. static const char *ANIMATION = "animation";
  41. static const char *MOVEMENT = "mov";
  42. static const char *FRAME = "f";
  43. static const char *TEXTURE_ATLAS = "TextureAtlas";
  44. static const char *SUB_TEXTURE = "SubTexture";
  45. static const char *A_NAME = "name";
  46. static const char *A_DURATION = "dr";
  47. static const char *A_FRAME_INDEX = "fi";
  48. static const char *A_DURATION_TO = "to";
  49. static const char *A_DURATION_TWEEN = "drTW";
  50. static const char *A_LOOP = "lp";
  51. static const char *A_MOVEMENT_SCALE = "sc";
  52. static const char *A_MOVEMENT_DELAY = "dl";
  53. static const char *A_DISPLAY_INDEX = "dI";
  54. static const char *A_PLIST = "plist";
  55. static const char *A_PARENT = "parent";
  56. static const char *A_SKEW_X = "kX";
  57. static const char *A_SKEW_Y = "kY";
  58. static const char *A_SCALE_X = "cX";
  59. static const char *A_SCALE_Y = "cY";
  60. static const char *A_Z = "z";
  61. static const char *A_EVENT = "evt";
  62. static const char *A_SOUND = "sd";
  63. static const char *A_SOUND_EFFECT = "sdE";
  64. static const char *A_TWEEN_EASING = "twE";
  65. static const char *A_EASING_PARAM = "twEP";
  66. static const char *A_TWEEN_ROTATE = "twR";
  67. static const char *A_IS_ARMATURE = "isArmature";
  68. static const char *A_DISPLAY_TYPE = "displayType";
  69. static const char *A_MOVEMENT = "mov";
  70. static const char *A_X = "x";
  71. static const char *A_Y = "y";
  72. static const char *A_COCOS2DX_X = "cocos2d_x";
  73. static const char *A_COCOS2DX_Y = "cocos2d_y";
  74. static const char *A_WIDTH = "width";
  75. static const char *A_HEIGHT = "height";
  76. static const char *A_PIVOT_X = "pX";
  77. static const char *A_PIVOT_Y = "pY";
  78. static const char *A_COCOS2D_PIVOT_X = "cocos2d_pX";
  79. static const char *A_COCOS2D_PIVOT_Y = "cocos2d_pY";
  80. static const char *A_BLEND_TYPE = "bd";
  81. static const char *A_BLEND_SRC = "bd_src";
  82. static const char *A_BLEND_DST = "bd_dst";
  83. static const char *A_ALPHA = "a";
  84. static const char *A_RED = "r";
  85. static const char *A_GREEN = "g";
  86. static const char *A_BLUE = "b";
  87. static const char *A_ALPHA_OFFSET = "aM";
  88. static const char *A_RED_OFFSET = "rM";
  89. static const char *A_GREEN_OFFSET = "gM";
  90. static const char *A_BLUE_OFFSET = "bM";
  91. static const char *A_COLOR_TRANSFORM = "colorTransform";
  92. static const char *A_TWEEN_FRAME = "tweenFrame";
  93. static const char *CONTOUR = "con";
  94. static const char *CONTOUR_VERTEX = "con_vt";
  95. static const char *FL_NAN = "NaN";
  96. static const char *FRAME_DATA = "frame_data";
  97. static const char *MOVEMENT_BONE_DATA = "mov_bone_data";
  98. static const char *MOVEMENT_DATA = "mov_data";
  99. static const char *ANIMATION_DATA = "animation_data";
  100. static const char *DISPLAY_DATA = "display_data";
  101. static const char *SKIN_DATA = "skin_data";
  102. static const char *BONE_DATA = "bone_data";
  103. static const char *ARMATURE_DATA = "armature_data";
  104. static const char *CONTOUR_DATA = "contour_data";
  105. static const char *TEXTURE_DATA = "texture_data";
  106. static const char *VERTEX_POINT = "vertex";
  107. static const char *COLOR_INFO = "color";
  108. static const char *CONFIG_FILE_PATH = "config_file_path";
  109. static const char *CONTENT_SCALE = "content_scale";
  110. static std::string readFileContent(const std::string& filename, bool binary) {
  111. auto fs = FileUtils::getInstance();
  112. std::string s;
  113. if (binary)
  114. fs->getContents(filename, &s);
  115. else
  116. s = fs->getStringFromFile(filename);
  117. return s;
  118. };
  119. namespace cocostudio {
  120. float s_PositionReadScale = 1;
  121. std::vector<std::string> DataReaderHelper::_configFileList;
  122. DataReaderHelper *DataReaderHelper::_dataReaderHelper = nullptr;
  123. //! Async load
  124. void DataReaderHelper::loadData()
  125. {
  126. AsyncStruct *pAsyncStruct = nullptr;
  127. while (true)
  128. {
  129. std::queue<AsyncStruct *> *pQueue = _asyncStructQueue;
  130. _asyncStructQueueMutex.lock(); // get async struct from queue
  131. if (pQueue->empty())
  132. {
  133. _asyncStructQueueMutex.unlock();
  134. if (need_quit)
  135. {
  136. break;
  137. }
  138. else
  139. {
  140. std::unique_lock<std::mutex> lk(_sleepMutex);
  141. _sleepCondition.wait(lk);
  142. continue;
  143. }
  144. }
  145. else
  146. {
  147. pAsyncStruct = pQueue->front();
  148. pQueue->pop();
  149. _asyncStructQueueMutex.unlock();
  150. }
  151. // generate data info
  152. DataInfo *pDataInfo = new (std::nothrow) DataInfo();
  153. pDataInfo->asyncStruct = pAsyncStruct;
  154. pDataInfo->filename = pAsyncStruct->filename;
  155. pDataInfo->baseFilePath = pAsyncStruct->baseFilePath;
  156. if (pAsyncStruct->configType == DragonBone_XML)
  157. {
  158. DataReaderHelper::addDataFromCache(pAsyncStruct->fileContent, pDataInfo);
  159. }
  160. else if(pAsyncStruct->configType == CocoStudio_JSON)
  161. {
  162. DataReaderHelper::addDataFromJsonCache(pAsyncStruct->fileContent, pDataInfo);
  163. }
  164. else if(pAsyncStruct->configType == CocoStudio_Binary)
  165. {
  166. DataReaderHelper::addDataFromBinaryCache(pAsyncStruct->fileContent.c_str(),pDataInfo);
  167. }
  168. // put the image info into the queue
  169. _dataInfoMutex.lock();
  170. _dataQueue->push(pDataInfo);
  171. _dataInfoMutex.unlock();
  172. }
  173. if( _asyncStructQueue != nullptr )
  174. {
  175. delete _asyncStructQueue;
  176. _asyncStructQueue = nullptr;
  177. delete _dataQueue;
  178. _dataQueue = nullptr;
  179. }
  180. }
  181. DataReaderHelper *DataReaderHelper::getInstance()
  182. {
  183. if(!_dataReaderHelper)
  184. {
  185. _dataReaderHelper = new (std::nothrow) DataReaderHelper();
  186. }
  187. return _dataReaderHelper;
  188. }
  189. void DataReaderHelper::setPositionReadScale(float scale)
  190. {
  191. s_PositionReadScale = scale;
  192. }
  193. float DataReaderHelper::getPositionReadScale()
  194. {
  195. return s_PositionReadScale;
  196. }
  197. void DataReaderHelper::purge()
  198. {
  199. _configFileList.clear();
  200. CC_SAFE_RELEASE_NULL(_dataReaderHelper);
  201. }
  202. DataReaderHelper::DataReaderHelper()
  203. : _loadingThread(nullptr)
  204. , _asyncRefCount(0)
  205. , _asyncRefTotalCount(0)
  206. , need_quit(false)
  207. , _asyncStructQueue(nullptr)
  208. , _dataQueue(nullptr)
  209. {
  210. }
  211. DataReaderHelper::~DataReaderHelper()
  212. {
  213. need_quit = true;
  214. _sleepCondition.notify_one();
  215. if (_loadingThread) _loadingThread->join();
  216. CC_SAFE_DELETE(_loadingThread);
  217. _dataReaderHelper = nullptr;
  218. }
  219. void DataReaderHelper::addDataFromFile(const std::string& filePath)
  220. {
  221. /*
  222. * Check if file is already added to ArmatureDataManager, if then return.
  223. */
  224. for(unsigned int i = 0; i < _configFileList.size(); i++)
  225. {
  226. if (_configFileList[i] == filePath)
  227. {
  228. return;
  229. }
  230. }
  231. _configFileList.push_back(filePath);
  232. //! find the base file path
  233. std::string basefilePath = filePath;
  234. size_t pos = basefilePath.find_last_of("/");
  235. if (pos != std::string::npos)
  236. {
  237. basefilePath = basefilePath.substr(0, pos + 1);
  238. }
  239. else
  240. {
  241. basefilePath = "";
  242. }
  243. std::string fileExtension = cocos2d::FileUtils::getInstance()->getFileExtension(filePath);
  244. // Read content from file
  245. std::string fullPath = FileUtils::getInstance()->fullPathForFilename(filePath);
  246. bool isbinaryfilesrc = fileExtension == ".csb";
  247. _dataReaderHelper->_getFileMutex.lock();
  248. std::string contentStr(readFileContent(fullPath, isbinaryfilesrc));
  249. _dataReaderHelper->_getFileMutex.unlock();
  250. DataInfo dataInfo;
  251. dataInfo.filename = filePath;
  252. dataInfo.asyncStruct = nullptr;
  253. dataInfo.baseFilePath = basefilePath;
  254. if (fileExtension == ".xml")
  255. {
  256. DataReaderHelper::addDataFromCache(contentStr, &dataInfo);
  257. }
  258. else if(fileExtension == ".json" || fileExtension == ".exportjson")
  259. {
  260. DataReaderHelper::addDataFromJsonCache(contentStr, &dataInfo);
  261. }
  262. else if(isbinaryfilesrc)
  263. {
  264. DataReaderHelper::addDataFromBinaryCache(contentStr.c_str(),&dataInfo);
  265. }
  266. }
  267. void DataReaderHelper::addDataFromFileAsync(const std::string& imagePath, const std::string& plistPath, const std::string& filePath, Ref *target, SEL_SCHEDULE selector)
  268. {
  269. /*
  270. * Check if file is already added to ArmatureDataManager, if then return.
  271. */
  272. for(unsigned int i = 0; i < _configFileList.size(); i++)
  273. {
  274. if (_configFileList[i] == filePath)
  275. {
  276. if (target && selector)
  277. {
  278. if (_asyncRefTotalCount == 0 && _asyncRefCount == 0)
  279. {
  280. (target->*selector)(1);
  281. }
  282. else
  283. {
  284. (target->*selector)((_asyncRefTotalCount - _asyncRefCount) / (float)_asyncRefTotalCount);
  285. }
  286. }
  287. return;
  288. }
  289. }
  290. _configFileList.push_back(filePath);
  291. //! find the base file path
  292. std::string basefilePath = filePath;
  293. size_t pos = basefilePath.find_last_of("/");
  294. if (pos != std::string::npos)
  295. {
  296. basefilePath = basefilePath.substr(0, pos + 1);
  297. }
  298. else
  299. {
  300. basefilePath = "";
  301. }
  302. // lazy init
  303. if (_asyncStructQueue == nullptr)
  304. {
  305. _asyncStructQueue = new std::queue<AsyncStruct *>();
  306. _dataQueue = new (std::nothrow) std::queue<DataInfo *>();
  307. // create a new thread to load images
  308. _loadingThread = new std::thread(&DataReaderHelper::loadData, this);
  309. need_quit = false;
  310. }
  311. if (0 == _asyncRefCount)
  312. {
  313. Director::getInstance()->getScheduler()->schedule(CC_SCHEDULE_SELECTOR(DataReaderHelper::addDataAsyncCallBack), this, 0, false);
  314. }
  315. ++_asyncRefCount;
  316. ++_asyncRefTotalCount;
  317. if (target)
  318. {
  319. target->retain();
  320. }
  321. // generate async struct
  322. AsyncStruct *data = new (std::nothrow) AsyncStruct();
  323. data->filename = filePath;
  324. data->baseFilePath = basefilePath;
  325. data->target = target;
  326. data->selector = selector;
  327. data->autoLoadSpriteFile = ArmatureDataManager::getInstance()->isAutoLoadSpriteFile();
  328. data->imagePath = imagePath;
  329. data->plistPath = plistPath;
  330. std::string fileExtension = cocos2d::FileUtils::getInstance()->getFileExtension(filePath);
  331. std::string fullPath = FileUtils::getInstance()->fullPathForFilename(filePath);
  332. bool isbinaryfilesrc = fileExtension == ".csb";
  333. // This only read exportJson file, it takes only a little time.
  334. // Large image files are loaded in DataReaderHelper::addDataFromJsonCache(dataInfo) asynchronously.
  335. _dataReaderHelper->_getFileMutex.lock();
  336. data->fileContent.assign(readFileContent(fullPath, isbinaryfilesrc));
  337. _dataReaderHelper->_getFileMutex.unlock();
  338. if (fileExtension == ".xml")
  339. {
  340. data->configType = DragonBone_XML;
  341. }
  342. else if(fileExtension == ".json" || fileExtension == ".exportjson")
  343. {
  344. data->configType = CocoStudio_JSON;
  345. }
  346. else if(isbinaryfilesrc)
  347. {
  348. data->configType = CocoStudio_Binary;
  349. }
  350. // add async struct into queue
  351. _asyncStructQueueMutex.lock();
  352. _asyncStructQueue->push(data);
  353. _asyncStructQueueMutex.unlock();
  354. _sleepCondition.notify_one();
  355. }
  356. void DataReaderHelper::addDataAsyncCallBack(float /*dt*/)
  357. {
  358. // the data is generated in loading thread
  359. std::queue<DataInfo *> *dataQueue = _dataQueue;
  360. _dataInfoMutex.lock();
  361. if (dataQueue->empty())
  362. {
  363. _dataInfoMutex.unlock();
  364. }
  365. else
  366. {
  367. DataInfo *pDataInfo = dataQueue->front();
  368. dataQueue->pop();
  369. _dataInfoMutex.unlock();
  370. AsyncStruct *pAsyncStruct = pDataInfo->asyncStruct;
  371. if (pAsyncStruct->imagePath != "" && pAsyncStruct->plistPath != "")
  372. {
  373. _getFileMutex.lock();
  374. ArmatureDataManager::getInstance()->addSpriteFrameFromFile(pAsyncStruct->plistPath, pAsyncStruct->imagePath, pDataInfo->filename);
  375. _getFileMutex.unlock();
  376. }
  377. while (!pDataInfo->configFileQueue.empty())
  378. {
  379. std::string configPath = pDataInfo->configFileQueue.front();
  380. _getFileMutex.lock();
  381. ArmatureDataManager::getInstance()->addSpriteFrameFromFile((pAsyncStruct->baseFilePath + configPath + ".plist"), (pAsyncStruct->baseFilePath + configPath + ".png"), pDataInfo->filename);
  382. _getFileMutex.unlock();
  383. pDataInfo->configFileQueue.pop();
  384. }
  385. Ref* target = pAsyncStruct->target;
  386. SEL_SCHEDULE selector = pAsyncStruct->selector;
  387. --_asyncRefCount;
  388. if (target && selector)
  389. {
  390. (target->*selector)((_asyncRefTotalCount - _asyncRefCount) / (float)_asyncRefTotalCount);
  391. target->release();
  392. }
  393. delete pAsyncStruct;
  394. delete pDataInfo;
  395. if (0 == _asyncRefCount)
  396. {
  397. _asyncRefTotalCount = 0;
  398. Director::getInstance()->getScheduler()->unschedule(CC_SCHEDULE_SELECTOR(DataReaderHelper::addDataAsyncCallBack), this);
  399. }
  400. }
  401. }
  402. void DataReaderHelper::removeConfigFile(const std::string& configFile)
  403. {
  404. auto it_end = _configFileList.end();
  405. for (auto it = _configFileList.begin(); it != it_end; ++it)
  406. {
  407. if (*it == configFile)
  408. {
  409. _configFileList.erase(it);
  410. break;
  411. }
  412. }
  413. }
  414. void DataReaderHelper::addDataFromCache(const std::string& pFileContent, DataInfo *dataInfo)
  415. {
  416. tinyxml2::XMLDocument document;
  417. document.Parse(pFileContent.c_str());
  418. tinyxml2::XMLElement *root = document.RootElement();
  419. CCASSERT(root, "XML error or XML is empty.");
  420. root->QueryFloatAttribute(VERSION, &dataInfo->flashToolVersion);
  421. /*
  422. * Begin decode armature data from xml
  423. */
  424. tinyxml2::XMLElement *armaturesXML = root->FirstChildElement(ARMATURES);
  425. tinyxml2::XMLElement *armatureXML = armaturesXML->FirstChildElement(ARMATURE);
  426. while(armatureXML)
  427. {
  428. ArmatureData *armatureData = DataReaderHelper::decodeArmature(armatureXML, dataInfo);
  429. if (dataInfo->asyncStruct)
  430. {
  431. _dataReaderHelper->_addDataMutex.lock();
  432. }
  433. ArmatureDataManager::getInstance()->addArmatureData(armatureData->name, armatureData, dataInfo->filename);
  434. armatureData->release();
  435. if (dataInfo->asyncStruct)
  436. {
  437. _dataReaderHelper->_addDataMutex.unlock();
  438. }
  439. armatureXML = armatureXML->NextSiblingElement(ARMATURE);
  440. }
  441. /*
  442. * Begin decode animation data from xml
  443. */
  444. tinyxml2::XMLElement *animationsXML = root->FirstChildElement(ANIMATIONS);
  445. tinyxml2::XMLElement *animationXML = animationsXML->FirstChildElement(ANIMATION);
  446. while(animationXML)
  447. {
  448. AnimationData *animationData = DataReaderHelper::decodeAnimation(animationXML, dataInfo);
  449. if (dataInfo->asyncStruct)
  450. {
  451. _dataReaderHelper->_addDataMutex.lock();
  452. }
  453. ArmatureDataManager::getInstance()->addAnimationData(animationData->name, animationData, dataInfo->filename);
  454. animationData->release();
  455. if (dataInfo->asyncStruct)
  456. {
  457. _dataReaderHelper->_addDataMutex.unlock();
  458. }
  459. animationXML = animationXML->NextSiblingElement(ANIMATION);
  460. }
  461. /*
  462. * Begin decode texture data from xml
  463. */
  464. tinyxml2::XMLElement *texturesXML = root->FirstChildElement(TEXTURE_ATLAS);
  465. tinyxml2::XMLElement *textureXML = texturesXML->FirstChildElement(SUB_TEXTURE);
  466. while(textureXML)
  467. {
  468. TextureData *textureData = DataReaderHelper::decodeTexture(textureXML, dataInfo);
  469. if (dataInfo->asyncStruct)
  470. {
  471. _dataReaderHelper->_addDataMutex.lock();
  472. }
  473. ArmatureDataManager::getInstance()->addTextureData(textureData->name, textureData, dataInfo->filename);
  474. textureData->release();
  475. if (dataInfo->asyncStruct)
  476. {
  477. _dataReaderHelper->_addDataMutex.unlock();
  478. }
  479. textureXML = textureXML->NextSiblingElement(SUB_TEXTURE);
  480. }
  481. }
  482. ArmatureData *DataReaderHelper::decodeArmature(tinyxml2::XMLElement *armatureXML, DataInfo *dataInfo)
  483. {
  484. ArmatureData *armatureData = new (std::nothrow) ArmatureData();
  485. armatureData->init();
  486. armatureData->name = armatureXML->Attribute(A_NAME);
  487. tinyxml2::XMLElement *boneXML = armatureXML->FirstChildElement(BONE);
  488. while( boneXML )
  489. {
  490. /*
  491. * If this bone have parent, then get the parent bone xml
  492. */
  493. const char *parentName = boneXML->Attribute(A_PARENT);
  494. tinyxml2::XMLElement *parentXML = nullptr;
  495. if (parentName)
  496. {
  497. parentXML = armatureXML->FirstChildElement(BONE);
  498. std::string parentNameStr = parentName;
  499. while (parentXML)
  500. {
  501. if (parentNameStr == parentXML->Attribute(A_NAME))
  502. {
  503. break;
  504. }
  505. parentXML = parentXML->NextSiblingElement(BONE);
  506. }
  507. }
  508. BoneData *boneData = decodeBone(boneXML, parentXML, dataInfo);
  509. armatureData->addBoneData(boneData);
  510. boneData->release();
  511. boneXML = boneXML->NextSiblingElement(BONE);
  512. }
  513. return armatureData;
  514. }
  515. BoneData *DataReaderHelper::decodeBone(tinyxml2::XMLElement *boneXML, tinyxml2::XMLElement* /*parentXml*/, DataInfo *dataInfo)
  516. {
  517. BoneData *boneData = new (std::nothrow) BoneData();
  518. boneData->init();
  519. std::string name = boneXML->Attribute(A_NAME);
  520. boneData->name = name;
  521. if( boneXML->Attribute(A_PARENT) != nullptr )
  522. {
  523. boneData->parentName = boneXML->Attribute(A_PARENT);
  524. }
  525. boneXML->QueryIntAttribute(A_Z, &boneData->zOrder);
  526. tinyxml2::XMLElement *displayXML = boneXML->FirstChildElement(DISPLAY);
  527. while(displayXML)
  528. {
  529. DisplayData *displayData = decodeBoneDisplay(displayXML, dataInfo);
  530. boneData->addDisplayData(displayData);
  531. displayData->release();
  532. displayXML = displayXML->NextSiblingElement(DISPLAY);
  533. }
  534. return boneData;
  535. }
  536. DisplayData *DataReaderHelper::decodeBoneDisplay(tinyxml2::XMLElement *displayXML, DataInfo* /*dataInfo*/)
  537. {
  538. int _isArmature = 0;
  539. DisplayData *displayData;
  540. if( displayXML->QueryIntAttribute(A_IS_ARMATURE, &(_isArmature)) == tinyxml2::XML_SUCCESS )
  541. {
  542. if(!_isArmature)
  543. {
  544. displayData = new (std::nothrow) SpriteDisplayData();
  545. displayData->displayType = CS_DISPLAY_SPRITE;
  546. }
  547. else
  548. {
  549. displayData = new (std::nothrow) ArmatureDisplayData();
  550. displayData->displayType = CS_DISPLAY_ARMATURE;
  551. }
  552. }
  553. else
  554. {
  555. displayData = new (std::nothrow) SpriteDisplayData();
  556. displayData->displayType = CS_DISPLAY_SPRITE;
  557. }
  558. if(displayXML->Attribute(A_NAME) != nullptr )
  559. {
  560. if(!_isArmature)
  561. {
  562. ((SpriteDisplayData *)displayData)->displayName = displayXML->Attribute(A_NAME);
  563. }
  564. else
  565. {
  566. ((ArmatureDisplayData *)displayData)->displayName = displayXML->Attribute(A_NAME);
  567. }
  568. }
  569. return displayData;
  570. }
  571. AnimationData *DataReaderHelper::decodeAnimation(tinyxml2::XMLElement *animationXML, DataInfo *dataInfo)
  572. {
  573. AnimationData *aniData = new AnimationData();
  574. const char *name = animationXML->Attribute(A_NAME);
  575. ArmatureData *armatureData = ArmatureDataManager::getInstance()->getArmatureData(name);
  576. aniData->name = name;
  577. tinyxml2::XMLElement *movementXML = animationXML->FirstChildElement(MOVEMENT);
  578. while( movementXML )
  579. {
  580. MovementData *movementData = decodeMovement(movementXML, armatureData, dataInfo);
  581. aniData->addMovement(movementData);
  582. movementData->release();
  583. movementXML = movementXML->NextSiblingElement(MOVEMENT);
  584. }
  585. return aniData;
  586. }
  587. MovementData *DataReaderHelper::decodeMovement(tinyxml2::XMLElement *movementXML, ArmatureData *armatureData, DataInfo *dataInfo)
  588. {
  589. MovementData *movementData = new (std::nothrow) MovementData();
  590. const char *movName = movementXML->Attribute(A_NAME);
  591. movementData->name = movName;
  592. int duration, durationTo, durationTween, loop, tweenEasing = 0;
  593. if( movementXML->QueryIntAttribute(A_DURATION, &(duration)) == tinyxml2::XML_SUCCESS)
  594. {
  595. movementData->duration = duration;
  596. }
  597. if( movementXML->QueryIntAttribute(A_DURATION_TO, &(durationTo)) == tinyxml2::XML_SUCCESS)
  598. {
  599. movementData->durationTo = durationTo;
  600. }
  601. if( movementXML->QueryIntAttribute(A_DURATION_TWEEN, &(durationTween)) == tinyxml2::XML_SUCCESS)
  602. {
  603. movementData->durationTween = durationTween;
  604. }
  605. if( movementXML->QueryIntAttribute(A_LOOP, &(loop)) == tinyxml2::XML_SUCCESS)
  606. {
  607. movementData->loop = (loop != 0);
  608. }
  609. const char *_easing = movementXML->Attribute(A_TWEEN_EASING);
  610. if(_easing != nullptr)
  611. {
  612. std::string str = _easing;
  613. if(str != FL_NAN)
  614. {
  615. if( movementXML->QueryIntAttribute(A_TWEEN_EASING, &(tweenEasing)) == tinyxml2::XML_SUCCESS)
  616. {
  617. movementData->tweenEasing = tweenEasing == 2 ? cocos2d::tweenfunc::Sine_EaseInOut : (TweenType)tweenEasing;
  618. }
  619. }
  620. else
  621. {
  622. movementData->tweenEasing = cocos2d::tweenfunc::Linear;
  623. }
  624. }
  625. tinyxml2::XMLElement *movBoneXml = movementXML->FirstChildElement(BONE);
  626. while(movBoneXml)
  627. {
  628. const char *boneName = movBoneXml->Attribute(A_NAME);
  629. if (movementData->getMovementBoneData(boneName))
  630. {
  631. movBoneXml = movBoneXml->NextSiblingElement();
  632. continue;
  633. }
  634. BoneData *boneData = (BoneData *)armatureData->getBoneData(boneName);
  635. std::string parentName = boneData->parentName;
  636. tinyxml2::XMLElement *parentXml = nullptr;
  637. if (!parentName.empty())
  638. {
  639. parentXml = movementXML->FirstChildElement(BONE);
  640. while (parentXml)
  641. {
  642. if (parentName == parentXml->Attribute(A_NAME))
  643. {
  644. break;
  645. }
  646. parentXml = parentXml->NextSiblingElement(BONE);
  647. }
  648. }
  649. MovementBoneData *moveBoneData = decodeMovementBone(movBoneXml, parentXml, boneData, dataInfo);
  650. movementData->addMovementBoneData(moveBoneData);
  651. moveBoneData->release();
  652. movBoneXml = movBoneXml->NextSiblingElement(BONE);
  653. }
  654. return movementData;
  655. }
  656. MovementBoneData *DataReaderHelper::decodeMovementBone(tinyxml2::XMLElement *movBoneXml, tinyxml2::XMLElement *parentXml, BoneData *boneData, DataInfo *dataInfo)
  657. {
  658. MovementBoneData *movBoneData = new (std::nothrow) MovementBoneData();
  659. movBoneData->init();
  660. float scale, delay;
  661. if( movBoneXml )
  662. {
  663. if( movBoneXml->QueryFloatAttribute(A_MOVEMENT_SCALE, &scale) == tinyxml2::XML_SUCCESS )
  664. {
  665. movBoneData->scale = scale;
  666. }
  667. if( movBoneXml->QueryFloatAttribute(A_MOVEMENT_DELAY, &delay) == tinyxml2::XML_SUCCESS )
  668. {
  669. if(delay > 0)
  670. {
  671. delay -= 1;
  672. }
  673. movBoneData->delay = delay;
  674. }
  675. }
  676. unsigned long length = 0;
  677. unsigned long index = 0;
  678. int parentTotalDuration = 0;
  679. int currentDuration = 0;
  680. tinyxml2::XMLElement *parentFrameXML = nullptr;
  681. std::vector<tinyxml2::XMLElement *> parentXmlList;
  682. /*
  683. * get the parent frame xml list, we need get the origin data
  684. */
  685. if( parentXml != nullptr )
  686. {
  687. parentFrameXML = parentXml->FirstChildElement(FRAME);
  688. while (parentFrameXML)
  689. {
  690. parentXmlList.push_back(parentFrameXML);
  691. parentFrameXML = parentFrameXML->NextSiblingElement(FRAME);
  692. }
  693. parentFrameXML = nullptr;
  694. length = parentXmlList.size();
  695. }
  696. int totalDuration = 0;
  697. std::string name = movBoneXml->Attribute(A_NAME);
  698. movBoneData->name = name;
  699. tinyxml2::XMLElement *frameXML = movBoneXml->FirstChildElement(FRAME);
  700. while( frameXML )
  701. {
  702. if(parentXml)
  703. {
  704. /*
  705. * in this loop we get the corresponding parent frame xml
  706. */
  707. while(index < length && (parentFrameXML ? (totalDuration < parentTotalDuration || totalDuration >= parentTotalDuration + currentDuration) : true))
  708. {
  709. parentFrameXML = parentXmlList[index];
  710. parentTotalDuration += currentDuration;
  711. parentFrameXML->QueryIntAttribute(A_DURATION, &currentDuration);
  712. index++;
  713. }
  714. }
  715. FrameData *frameData = decodeFrame( frameXML, parentFrameXML, boneData, dataInfo);
  716. movBoneData->addFrameData(frameData);
  717. frameData->release();
  718. frameData->frameID = totalDuration;
  719. totalDuration += frameData->duration;
  720. movBoneData->duration = totalDuration;
  721. frameXML = frameXML->NextSiblingElement(FRAME);
  722. }
  723. //! Change rotation range from (-180 -- 180) to (-infinity -- infinity)
  724. auto frames = movBoneData->frameList;
  725. for (long j = movBoneData->frameList.size() - 1; j >= 0; j--)
  726. {
  727. if (j > 0)
  728. {
  729. float difSkewX = frames.at(j)->skewX - frames.at(j-1)->skewX;
  730. float difSkewY = frames.at(j)->skewY - frames.at(j-1)->skewY;
  731. if (difSkewX < -M_PI || difSkewX > M_PI)
  732. {
  733. frames.at(j-1)->skewX = difSkewX < 0 ? frames.at(j-1)->skewX - 2 * M_PI : frames.at(j-1)->skewX + 2 * M_PI;
  734. }
  735. if (difSkewY < -M_PI || difSkewY > M_PI)
  736. {
  737. frames.at(j-1)->skewY = difSkewY < 0 ? frames.at(j-1)->skewY - 2 * M_PI : frames.at(j-1)->skewY + 2 * M_PI;
  738. }
  739. }
  740. }
  741. //
  742. FrameData *frameData = new (std::nothrow) FrameData();
  743. frameData->copy((FrameData *)movBoneData->frameList.back());
  744. frameData->frameID = movBoneData->duration;
  745. movBoneData->addFrameData(frameData);
  746. frameData->release();
  747. return movBoneData;
  748. }
  749. FrameData *DataReaderHelper::decodeFrame(tinyxml2::XMLElement *frameXML, tinyxml2::XMLElement *parentFrameXml, BoneData* /*boneData*/, DataInfo *dataInfo)
  750. {
  751. float x = 0, y = 0, scale_x = 0, scale_y = 0, skew_x = 0, skew_y = 0, tweenRotate = 0;
  752. int duration = 0, displayIndex = 0, zOrder = 0, tweenEasing = 0, blendType = 0;
  753. FrameData *frameData = new (std::nothrow) FrameData();
  754. if(frameXML->Attribute(A_MOVEMENT) != nullptr)
  755. {
  756. frameData->strMovement = frameXML->Attribute(A_MOVEMENT);
  757. }
  758. if(frameXML->Attribute(A_EVENT) != nullptr)
  759. {
  760. frameData->strEvent = frameXML->Attribute(A_EVENT);
  761. }
  762. if(frameXML->Attribute(A_SOUND) != nullptr)
  763. {
  764. frameData->strSound = frameXML->Attribute(A_SOUND);
  765. }
  766. if(frameXML->Attribute(A_SOUND_EFFECT) != nullptr)
  767. {
  768. frameData->strSoundEffect = frameXML->Attribute(A_SOUND_EFFECT);
  769. }
  770. bool tweenFrame = false;
  771. if (frameXML->QueryBoolAttribute(A_TWEEN_FRAME, &tweenFrame) == tinyxml2::XML_SUCCESS)
  772. {
  773. frameData->isTween = tweenFrame;
  774. }
  775. if (dataInfo->flashToolVersion >= VERSION_2_0)
  776. {
  777. if(frameXML->QueryFloatAttribute(A_COCOS2DX_X, &x) == tinyxml2::XML_SUCCESS)
  778. {
  779. frameData->x = x;
  780. frameData->x *= s_PositionReadScale;
  781. }
  782. if(frameXML->QueryFloatAttribute(A_COCOS2DX_Y, &y) == tinyxml2::XML_SUCCESS)
  783. {
  784. frameData->y = -y;
  785. frameData->y *= s_PositionReadScale;
  786. }
  787. }
  788. else
  789. {
  790. if(frameXML->QueryFloatAttribute(A_X, &x) == tinyxml2::XML_SUCCESS)
  791. {
  792. frameData->x = x;
  793. frameData->x *= s_PositionReadScale;
  794. }
  795. if(frameXML->QueryFloatAttribute(A_Y, &y) == tinyxml2::XML_SUCCESS)
  796. {
  797. frameData->y = -y;
  798. frameData->y *= s_PositionReadScale;
  799. }
  800. }
  801. if( frameXML->QueryFloatAttribute(A_SCALE_X, &scale_x) == tinyxml2::XML_SUCCESS )
  802. {
  803. frameData->scaleX = scale_x;
  804. }
  805. if( frameXML->QueryFloatAttribute(A_SCALE_Y, &scale_y) == tinyxml2::XML_SUCCESS )
  806. {
  807. frameData->scaleY = scale_y;
  808. }
  809. if( frameXML->QueryFloatAttribute(A_SKEW_X, &skew_x) == tinyxml2::XML_SUCCESS )
  810. {
  811. frameData->skewX = CC_DEGREES_TO_RADIANS(skew_x);
  812. }
  813. if( frameXML->QueryFloatAttribute(A_SKEW_Y, &skew_y) == tinyxml2::XML_SUCCESS )
  814. {
  815. frameData->skewY = CC_DEGREES_TO_RADIANS(-skew_y);
  816. }
  817. if( frameXML->QueryIntAttribute(A_DURATION, &duration) == tinyxml2::XML_SUCCESS )
  818. {
  819. frameData->duration = duration;
  820. }
  821. if( frameXML->QueryIntAttribute(A_DISPLAY_INDEX, &displayIndex) == tinyxml2::XML_SUCCESS )
  822. {
  823. frameData->displayIndex = displayIndex;
  824. }
  825. if( frameXML->QueryIntAttribute(A_Z, &zOrder) == tinyxml2::XML_SUCCESS )
  826. {
  827. frameData->zOrder = zOrder;
  828. }
  829. if( frameXML->QueryFloatAttribute(A_TWEEN_ROTATE, &tweenRotate) == tinyxml2::XML_SUCCESS )
  830. {
  831. frameData->tweenRotate = tweenRotate;
  832. }
  833. if ( frameXML->QueryIntAttribute(A_BLEND_TYPE, &blendType) == tinyxml2::XML_SUCCESS )
  834. {
  835. switch (blendType)
  836. {
  837. case BLEND_NORMAL:
  838. {
  839. frameData->blendFunc = BlendFunc::ALPHA_PREMULTIPLIED;
  840. }
  841. break;
  842. case BLEND_ADD:
  843. {
  844. frameData->blendFunc.src = GL_SRC_ALPHA;
  845. frameData->blendFunc.dst = GL_ONE;
  846. }
  847. break;
  848. case BLEND_MULTIPLY:
  849. {
  850. frameData->blendFunc.src = GL_DST_COLOR;
  851. frameData->blendFunc.dst = GL_ONE_MINUS_SRC_ALPHA;
  852. }
  853. break;
  854. case BLEND_SCREEN:
  855. {
  856. frameData->blendFunc.src = GL_ONE;
  857. frameData->blendFunc.dst = GL_ONE_MINUS_SRC_COLOR;
  858. }
  859. break;
  860. default:
  861. {
  862. frameData->blendFunc.src = CC_BLEND_SRC;
  863. frameData->blendFunc.dst = CC_BLEND_DST;
  864. }
  865. break;
  866. }
  867. }
  868. tinyxml2::XMLElement *colorTransformXML = frameXML->FirstChildElement(A_COLOR_TRANSFORM);
  869. if (colorTransformXML)
  870. {
  871. int alpha, red, green, blue = 100;
  872. int alphaOffset, redOffset, greenOffset, blueOffset = 0;
  873. colorTransformXML->QueryIntAttribute(A_ALPHA, &alpha);
  874. colorTransformXML->QueryIntAttribute(A_RED, &red);
  875. colorTransformXML->QueryIntAttribute(A_GREEN, &green);
  876. colorTransformXML->QueryIntAttribute(A_BLUE, &blue) ;
  877. colorTransformXML->QueryIntAttribute(A_ALPHA_OFFSET, &alphaOffset);
  878. colorTransformXML->QueryIntAttribute(A_RED_OFFSET, &redOffset);
  879. colorTransformXML->QueryIntAttribute(A_GREEN_OFFSET, &greenOffset);
  880. colorTransformXML->QueryIntAttribute(A_BLUE_OFFSET, &blueOffset) ;
  881. frameData->a = 2.55 * alphaOffset + alpha;
  882. frameData->r = 2.55 * redOffset + red;
  883. frameData->g = 2.55 * greenOffset + green;
  884. frameData->b = 2.55 * blueOffset + blue;
  885. frameData->isUseColorInfo = true;
  886. }
  887. const char *_easing = frameXML->Attribute(A_TWEEN_EASING);
  888. if(_easing != nullptr)
  889. {
  890. std::string str = _easing;
  891. if(str != FL_NAN)
  892. {
  893. if( frameXML->QueryIntAttribute(A_TWEEN_EASING, &(tweenEasing)) == tinyxml2::XML_SUCCESS)
  894. {
  895. frameData->tweenEasing = tweenEasing == 2 ? cocos2d::tweenfunc::Sine_EaseInOut : (cocos2d::tweenfunc::TweenType)tweenEasing;
  896. }
  897. }
  898. else
  899. {
  900. frameData->tweenEasing = cocos2d::tweenfunc::Linear;
  901. }
  902. }
  903. if(parentFrameXml)
  904. {
  905. /*
  906. * recalculate frame data from parent frame data, use for translate matrix
  907. */
  908. BaseData helpNode;
  909. if (dataInfo->flashToolVersion >= VERSION_2_0)
  910. {
  911. parentFrameXml->QueryFloatAttribute(A_COCOS2DX_X, &helpNode.x);
  912. parentFrameXml->QueryFloatAttribute(A_COCOS2DX_Y, &helpNode.y);
  913. }
  914. else
  915. {
  916. parentFrameXml->QueryFloatAttribute(A_X, &helpNode.x);
  917. parentFrameXml->QueryFloatAttribute(A_Y, &helpNode.y);
  918. }
  919. parentFrameXml->QueryFloatAttribute(A_SKEW_X, &helpNode.skewX);
  920. parentFrameXml->QueryFloatAttribute(A_SKEW_Y, &helpNode.skewY);
  921. helpNode.y = -helpNode.y;
  922. helpNode.skewX = CC_DEGREES_TO_RADIANS(helpNode.skewX);
  923. helpNode.skewY = CC_DEGREES_TO_RADIANS(-helpNode.skewY);
  924. TransformHelp::transformFromParent(*frameData, helpNode);
  925. }
  926. return frameData;
  927. }
  928. TextureData *DataReaderHelper::decodeTexture(tinyxml2::XMLElement *textureXML, DataInfo *dataInfo)
  929. {
  930. TextureData *textureData = new (std::nothrow) TextureData();
  931. textureData->init();
  932. if( textureXML->Attribute(A_NAME) != nullptr)
  933. {
  934. textureData->name = textureXML->Attribute(A_NAME);
  935. }
  936. float px, py, width, height = 0;
  937. if(dataInfo->flashToolVersion >= VERSION_2_0)
  938. {
  939. textureXML->QueryFloatAttribute(A_COCOS2D_PIVOT_X, &px);
  940. textureXML->QueryFloatAttribute(A_COCOS2D_PIVOT_Y, &py);
  941. }
  942. else
  943. {
  944. textureXML->QueryFloatAttribute(A_PIVOT_X, &px);
  945. textureXML->QueryFloatAttribute(A_PIVOT_Y, &py);
  946. }
  947. textureXML->QueryFloatAttribute(A_WIDTH, &width);
  948. textureXML->QueryFloatAttribute(A_HEIGHT, &height);
  949. float anchorPointX = px / width;
  950. float anchorPointY = (height - py) / height;
  951. textureData->pivotX = anchorPointX;
  952. textureData->pivotY = anchorPointY;
  953. tinyxml2::XMLElement *contourXML = textureXML->FirstChildElement(CONTOUR);
  954. while (contourXML)
  955. {
  956. ContourData *contourData = decodeContour(contourXML, dataInfo);
  957. textureData->addContourData(contourData);
  958. contourData->release();
  959. contourXML = contourXML->NextSiblingElement(CONTOUR);
  960. }
  961. return textureData;
  962. }
  963. ContourData *DataReaderHelper::decodeContour(tinyxml2::XMLElement *contourXML, DataInfo* /*dataInfo*/)
  964. {
  965. ContourData *contourData = new (std::nothrow) ContourData();
  966. contourData->init();
  967. tinyxml2::XMLElement *vertexDataXML = contourXML->FirstChildElement(CONTOUR_VERTEX);
  968. while (vertexDataXML)
  969. {
  970. Vec2 vertex;
  971. vertexDataXML->QueryFloatAttribute(A_X, &vertex.x);
  972. vertexDataXML->QueryFloatAttribute(A_Y, &vertex.y);
  973. vertex.y = -vertex.y;
  974. contourData->vertexList.push_back(vertex);
  975. vertexDataXML = vertexDataXML->NextSiblingElement(CONTOUR_VERTEX);
  976. }
  977. return contourData;
  978. }
  979. void DataReaderHelper::addDataFromJsonCache(const std::string& fileContent, DataInfo *dataInfo)
  980. {
  981. rapidjson::Document json;
  982. rapidjson::StringStream stream(fileContent.c_str());
  983. if (fileContent.size() >= 3) {
  984. // Skip BOM if exists
  985. const unsigned char* c = (const unsigned char *)fileContent.c_str();
  986. unsigned bom = c[0] | (c[1] << 8) | (c[2] << 16);
  987. if (bom == 0xBFBBEF) // UTF8 BOM
  988. {
  989. stream.Take();
  990. stream.Take();
  991. stream.Take();
  992. }
  993. }
  994. json.ParseStream<0>(stream);
  995. if (json.HasParseError()) {
  996. CCLOG("GetParseError %d\n",json.GetParseError());
  997. }
  998. dataInfo->contentScale = DICTOOL->getFloatValue_json(json, CONTENT_SCALE, 1.0f);
  999. // Decode armatures
  1000. int length = DICTOOL->getArrayCount_json(json, ARMATURE_DATA);
  1001. for (int i = 0; i < length; i++)
  1002. {
  1003. const rapidjson::Value &armatureDic = DICTOOL->getSubDictionary_json(json, ARMATURE_DATA, i);
  1004. ArmatureData *armatureData = decodeArmature(armatureDic, dataInfo);
  1005. if (dataInfo->asyncStruct)
  1006. {
  1007. _dataReaderHelper->_addDataMutex.lock();
  1008. }
  1009. ArmatureDataManager::getInstance()->addArmatureData(armatureData->name, armatureData, dataInfo->filename);
  1010. armatureData->release();
  1011. if (dataInfo->asyncStruct)
  1012. {
  1013. _dataReaderHelper->_addDataMutex.unlock();
  1014. }
  1015. }
  1016. // Decode animations
  1017. length = DICTOOL->getArrayCount_json(json, ANIMATION_DATA); //json[ANIMATION_DATA].IsNull() ? 0 : json[ANIMATION_DATA].Size();
  1018. for (int i = 0; i < length; i++)
  1019. {
  1020. const rapidjson::Value &animationDic = DICTOOL->getSubDictionary_json(json, ANIMATION_DATA, i);
  1021. AnimationData *animationData = decodeAnimation(animationDic, dataInfo);
  1022. if (dataInfo->asyncStruct)
  1023. {
  1024. _dataReaderHelper->_addDataMutex.lock();
  1025. }
  1026. ArmatureDataManager::getInstance()->addAnimationData(animationData->name, animationData, dataInfo->filename);
  1027. animationData->release();
  1028. if (dataInfo->asyncStruct)
  1029. {
  1030. _dataReaderHelper->_addDataMutex.unlock();
  1031. }
  1032. }
  1033. // Decode textures
  1034. length = DICTOOL->getArrayCount_json(json, TEXTURE_DATA);
  1035. for (int i = 0; i < length; i++)
  1036. {
  1037. const rapidjson::Value &textureDic = DICTOOL->getSubDictionary_json(json, TEXTURE_DATA, i);
  1038. TextureData *textureData = decodeTexture(textureDic);
  1039. if (dataInfo->asyncStruct)
  1040. {
  1041. _dataReaderHelper->_addDataMutex.lock();
  1042. }
  1043. ArmatureDataManager::getInstance()->addTextureData(textureData->name, textureData, dataInfo->filename);
  1044. textureData->release();
  1045. if (dataInfo->asyncStruct)
  1046. {
  1047. _dataReaderHelper->_addDataMutex.unlock();
  1048. }
  1049. }
  1050. // Auto load sprite file
  1051. bool autoLoad = dataInfo->asyncStruct == nullptr ? ArmatureDataManager::getInstance()->isAutoLoadSpriteFile() : dataInfo->asyncStruct->autoLoadSpriteFile;
  1052. if (autoLoad)
  1053. {
  1054. length = DICTOOL->getArrayCount_json(json, CONFIG_FILE_PATH); // json[CONFIG_FILE_PATH].IsNull() ? 0 : json[CONFIG_FILE_PATH].Size();
  1055. for (int i = 0; i < length; i++)
  1056. {
  1057. const char *path = DICTOOL->getStringValueFromArray_json(json, CONFIG_FILE_PATH, i); // json[CONFIG_FILE_PATH][i].IsNull() ? nullptr : json[CONFIG_FILE_PATH][i].GetString();
  1058. if (path == nullptr)
  1059. {
  1060. CCLOG("load CONFIG_FILE_PATH error.");
  1061. return;
  1062. }
  1063. std::string filePath = path;
  1064. filePath = filePath.erase(filePath.find_last_of("."));
  1065. if (dataInfo->asyncStruct)
  1066. {
  1067. dataInfo->configFileQueue.push(filePath);
  1068. }
  1069. else
  1070. {
  1071. std::string plistPath = filePath + ".plist";
  1072. std::string pngPath = filePath + ".png";
  1073. if (FileUtils::getInstance()->isFileExist(dataInfo->baseFilePath + plistPath) && FileUtils::getInstance()->isFileExist(dataInfo->baseFilePath + pngPath))
  1074. {
  1075. ValueMap dict = FileUtils::getInstance()->getValueMapFromFile(dataInfo->baseFilePath + plistPath);
  1076. if (dict.find("particleLifespan") != dict.end()) continue;
  1077. ArmatureDataManager::getInstance()->addSpriteFrameFromFile((dataInfo->baseFilePath + plistPath), (dataInfo->baseFilePath + pngPath), dataInfo->filename);
  1078. }
  1079. }
  1080. }
  1081. }
  1082. }
  1083. ArmatureData *DataReaderHelper::decodeArmature(const rapidjson::Value& json, DataInfo *dataInfo)
  1084. {
  1085. ArmatureData *armatureData = new (std::nothrow) ArmatureData();
  1086. armatureData->init();
  1087. const char *name = DICTOOL->getStringValue_json(json, A_NAME);
  1088. if(name != nullptr)
  1089. {
  1090. armatureData->name = name;
  1091. }
  1092. dataInfo->cocoStudioVersion = armatureData->dataVersion = DICTOOL->getFloatValue_json(json, VERSION, 0.1f);
  1093. int length = DICTOOL->getArrayCount_json(json, BONE_DATA, 0);
  1094. for (int i = 0; i < length; i++)
  1095. {
  1096. const rapidjson::Value &dic = DICTOOL->getSubDictionary_json(json, BONE_DATA, i); //json[BONE_DATA][i];
  1097. BoneData *boneData = decodeBone(dic, dataInfo);
  1098. armatureData->addBoneData(boneData);
  1099. boneData->release();
  1100. }
  1101. return armatureData;
  1102. }
  1103. BoneData *DataReaderHelper::decodeBone(const rapidjson::Value& json, DataInfo *dataInfo)
  1104. {
  1105. BoneData *boneData = new (std::nothrow) BoneData();
  1106. boneData->init();
  1107. decodeNode(boneData, json, dataInfo);
  1108. const char *str = DICTOOL->getStringValue_json(json, A_NAME);
  1109. if(str != nullptr)
  1110. {
  1111. boneData->name = str;
  1112. }
  1113. str = DICTOOL->getStringValue_json(json, A_PARENT);
  1114. if(str != nullptr)
  1115. {
  1116. boneData->parentName = str;
  1117. }
  1118. int length = DICTOOL->getArrayCount_json(json, DISPLAY_DATA);
  1119. for (int i = 0; i < length; i++)
  1120. {
  1121. const rapidjson::Value &dic = DICTOOL->getSubDictionary_json(json, DISPLAY_DATA, i);
  1122. DisplayData *displayData = decodeBoneDisplay(dic, dataInfo);
  1123. boneData->addDisplayData(displayData);
  1124. displayData->release();
  1125. }
  1126. return boneData;
  1127. }
  1128. DisplayData *DataReaderHelper::decodeBoneDisplay(const rapidjson::Value& json, DataInfo *dataInfo)
  1129. {
  1130. DisplayType displayType = (DisplayType)(DICTOOL->getIntValue_json(json, A_DISPLAY_TYPE, CS_DISPLAY_SPRITE));
  1131. DisplayData *displayData = nullptr;
  1132. switch (displayType)
  1133. {
  1134. case CS_DISPLAY_SPRITE:
  1135. {
  1136. displayData = new (std::nothrow) SpriteDisplayData();
  1137. const char *name = DICTOOL->getStringValue_json(json, A_NAME);
  1138. if(name != nullptr)
  1139. {
  1140. ((SpriteDisplayData *)displayData)->displayName = name;
  1141. }
  1142. if(json.HasMember(SKIN_DATA))
  1143. {
  1144. const rapidjson::Value &dicArray = DICTOOL->getSubDictionary_json(json, SKIN_DATA);
  1145. if(!dicArray.IsNull())
  1146. {
  1147. rapidjson::SizeType index = 0;
  1148. const rapidjson::Value &dic = DICTOOL->getSubDictionary_json(dicArray, index);
  1149. if (!dic.IsNull())
  1150. {
  1151. SpriteDisplayData *sdd = (SpriteDisplayData *)displayData;
  1152. sdd->skinData.x = DICTOOL->getFloatValue_json(dic, A_X) * s_PositionReadScale;
  1153. sdd->skinData.y = DICTOOL->getFloatValue_json(dic, A_Y) * s_PositionReadScale;
  1154. sdd->skinData.scaleX = DICTOOL->getFloatValue_json(dic, A_SCALE_X, 1.0f);
  1155. sdd->skinData.scaleY = DICTOOL->getFloatValue_json(dic, A_SCALE_Y, 1.0f);
  1156. sdd->skinData.skewX = DICTOOL->getFloatValue_json(dic, A_SKEW_X, 1.0f);
  1157. sdd->skinData.skewY = DICTOOL->getFloatValue_json(dic, A_SKEW_Y, 1.0f);
  1158. sdd->skinData.x *= dataInfo->contentScale;
  1159. sdd->skinData.y *= dataInfo->contentScale;
  1160. }
  1161. }
  1162. }
  1163. }
  1164. break;
  1165. case CS_DISPLAY_ARMATURE:
  1166. {
  1167. displayData = new (std::nothrow) ArmatureDisplayData();
  1168. const char *name = DICTOOL->getStringValue_json(json, A_NAME);
  1169. if(name != nullptr)
  1170. {
  1171. ((ArmatureDisplayData *)displayData)->displayName = name;
  1172. }
  1173. }
  1174. break;
  1175. case CS_DISPLAY_PARTICLE:
  1176. {
  1177. displayData = new (std::nothrow) ParticleDisplayData();
  1178. const char *plist = DICTOOL->getStringValue_json(json, A_PLIST);
  1179. if(plist != nullptr)
  1180. {
  1181. if (dataInfo->asyncStruct)
  1182. {
  1183. static_cast<ParticleDisplayData *>(displayData)->displayName = dataInfo->asyncStruct->baseFilePath + plist;
  1184. }
  1185. else
  1186. {
  1187. static_cast<ParticleDisplayData *>(displayData)->displayName = dataInfo->baseFilePath + plist;
  1188. }
  1189. }
  1190. }
  1191. break;
  1192. default:
  1193. displayData = new (std::nothrow) SpriteDisplayData();
  1194. break;
  1195. }
  1196. displayData->displayType = displayType;
  1197. return displayData;
  1198. }
  1199. AnimationData *DataReaderHelper::decodeAnimation(const rapidjson::Value& json, DataInfo *dataInfo)
  1200. {
  1201. AnimationData *aniData = new (std::nothrow) AnimationData();
  1202. const char *name = DICTOOL->getStringValue_json(json, A_NAME);
  1203. if(name != nullptr)
  1204. {
  1205. aniData->name = name;
  1206. }
  1207. int length = DICTOOL->getArrayCount_json(json, MOVEMENT_DATA);
  1208. for (int i = 0; i < length; i++)
  1209. {
  1210. const rapidjson::Value &dic = DICTOOL->getSubDictionary_json(json, MOVEMENT_DATA, i);
  1211. MovementData *movementData = decodeMovement(dic, dataInfo);
  1212. aniData->addMovement(movementData);
  1213. movementData->release();
  1214. }
  1215. return aniData;
  1216. }
  1217. MovementData *DataReaderHelper::decodeMovement(const rapidjson::Value& json, DataInfo *dataInfo)
  1218. {
  1219. MovementData *movementData = new (std::nothrow) MovementData();
  1220. movementData->loop = DICTOOL->getBooleanValue_json(json, A_LOOP, true);
  1221. movementData->durationTween = DICTOOL->getIntValue_json(json, A_DURATION_TWEEN, 0);
  1222. movementData->durationTo = DICTOOL->getIntValue_json(json, A_DURATION_TO, 0);
  1223. movementData->duration = DICTOOL->getIntValue_json(json, A_DURATION, 0);
  1224. if (!DICTOOL->checkObjectExist_json(json, A_DURATION))
  1225. {
  1226. movementData->scale = 1.0f;
  1227. }
  1228. else
  1229. {
  1230. movementData->scale = DICTOOL->getFloatValue_json(json, A_MOVEMENT_SCALE, 1.0f);
  1231. }
  1232. movementData->tweenEasing = (TweenType)(DICTOOL->getIntValue_json(json, A_TWEEN_EASING, cocos2d::tweenfunc::Linear));
  1233. const char *name = DICTOOL->getStringValue_json(json, A_NAME);
  1234. if(name != nullptr)
  1235. {
  1236. movementData->name = name;
  1237. }
  1238. int length = DICTOOL->getArrayCount_json(json, MOVEMENT_BONE_DATA);
  1239. for (int i = 0; i < length; i++)
  1240. {
  1241. const rapidjson::Value &dic = DICTOOL->getSubDictionary_json(json, MOVEMENT_BONE_DATA, i);
  1242. MovementBoneData *movementBoneData = decodeMovementBone(dic, dataInfo);
  1243. movementData->addMovementBoneData(movementBoneData);
  1244. movementBoneData->release();
  1245. }
  1246. return movementData;
  1247. }
  1248. MovementBoneData *DataReaderHelper::decodeMovementBone(const rapidjson::Value& json, DataInfo *dataInfo)
  1249. {
  1250. MovementBoneData *movementBoneData = new (std::nothrow) MovementBoneData();
  1251. movementBoneData->init();
  1252. movementBoneData->delay = DICTOOL->getFloatValue_json(json, A_MOVEMENT_DELAY);
  1253. const char *name = DICTOOL->getStringValue_json(json, A_NAME);
  1254. if(name != nullptr)
  1255. {
  1256. movementBoneData->name = name;
  1257. }
  1258. rapidjson::SizeType length = DICTOOL->getArrayCount_json(json, FRAME_DATA);
  1259. for (rapidjson::SizeType i = 0; i < length; i++)
  1260. {
  1261. const rapidjson::Value &dic = DICTOOL->getSubDictionary_json(json, FRAME_DATA, i);
  1262. FrameData *frameData = decodeFrame(dic, dataInfo);
  1263. movementBoneData->addFrameData(frameData);
  1264. frameData->release();
  1265. if (dataInfo->cocoStudioVersion < VERSION_COMBINED)
  1266. {
  1267. frameData->frameID = movementBoneData->duration;
  1268. movementBoneData->duration += frameData->duration;
  1269. }
  1270. }
  1271. if (dataInfo->cocoStudioVersion < VERSION_CHANGE_ROTATION_RANGE)
  1272. {
  1273. //! Change rotation range from (-180 -- 180) to (-infinity -- infinity)
  1274. auto frames = movementBoneData->frameList;
  1275. for (long i = frames.size() - 1; i >= 0; i--)
  1276. {
  1277. if (i > 0)
  1278. {
  1279. float difSkewX = frames.at(i)->skewX - frames.at(i-1)->skewX;
  1280. float difSkewY = frames.at(i)->skewY - frames.at(i-1)->skewY;
  1281. if (difSkewX < -M_PI || difSkewX > M_PI)
  1282. {
  1283. frames.at(i-1)->skewX = difSkewX < 0 ? frames.at(i-1)->skewX - 2 * M_PI : frames.at(i-1)->skewX + 2 * M_PI;
  1284. }
  1285. if (difSkewY < -M_PI || difSkewY > M_PI)
  1286. {
  1287. frames.at(i-1)->skewY = difSkewY < 0 ? frames.at(i-1)->skewY - 2 * M_PI : frames.at(i-1)->skewY + 2 * M_PI;
  1288. }
  1289. }
  1290. }
  1291. }
  1292. if (dataInfo->cocoStudioVersion < VERSION_COMBINED)
  1293. {
  1294. if (movementBoneData->frameList.size() > 0)
  1295. {
  1296. FrameData *frameData = new (std::nothrow) FrameData();
  1297. frameData->copy((FrameData *)movementBoneData->frameList.back());
  1298. movementBoneData->addFrameData(frameData);
  1299. frameData->release();
  1300. frameData->frameID = movementBoneData->duration;
  1301. }
  1302. }
  1303. return movementBoneData;
  1304. }
  1305. FrameData *DataReaderHelper::decodeFrame(const rapidjson::Value& json, DataInfo *dataInfo)
  1306. {
  1307. FrameData *frameData = new (std::nothrow) FrameData();
  1308. decodeNode(frameData, json, dataInfo);
  1309. frameData->tweenEasing = (TweenType)(DICTOOL->getIntValue_json(json, A_TWEEN_EASING, cocos2d::tweenfunc::Linear));
  1310. frameData->displayIndex = DICTOOL->getIntValue_json(json, A_DISPLAY_INDEX);
  1311. frameData->blendFunc.src = (GLenum)(DICTOOL->getIntValue_json(json, A_BLEND_SRC, BlendFunc::ALPHA_PREMULTIPLIED.src));
  1312. frameData->blendFunc.dst = (GLenum)(DICTOOL->getIntValue_json(json, A_BLEND_DST, BlendFunc::ALPHA_PREMULTIPLIED.dst));
  1313. frameData->isTween = DICTOOL->getBooleanValue_json(json, A_TWEEN_FRAME, true);
  1314. const char *event = DICTOOL->getStringValue_json(json, A_EVENT);
  1315. if (event != nullptr)
  1316. {
  1317. frameData->strEvent = event;
  1318. }
  1319. if (dataInfo->cocoStudioVersion < VERSION_COMBINED)
  1320. {
  1321. frameData->duration = DICTOOL->getIntValue_json(json, A_DURATION, 1);
  1322. }
  1323. else
  1324. {
  1325. frameData->frameID = DICTOOL->getIntValue_json(json, A_FRAME_INDEX);
  1326. }
  1327. int length = DICTOOL->getArrayCount_json(json, A_EASING_PARAM);
  1328. if (length != 0)
  1329. {
  1330. frameData->easingParams = new (std::nothrow) float[length];
  1331. frameData->easingParamNumber = length;
  1332. for (int i = 0; i < length; i++)
  1333. {
  1334. frameData->easingParams[i] = DICTOOL->getFloatValueFromArray_json(json, A_EASING_PARAM, i);
  1335. }
  1336. }
  1337. return frameData;
  1338. }
  1339. TextureData *DataReaderHelper::decodeTexture(const rapidjson::Value& json)
  1340. {
  1341. TextureData *textureData = new (std::nothrow) TextureData();
  1342. textureData->init();
  1343. const char *name = DICTOOL->getStringValue_json(json, A_NAME);
  1344. if(name != nullptr)
  1345. {
  1346. textureData->name = name;
  1347. }
  1348. textureData->width = DICTOOL->getFloatValue_json(json, A_WIDTH);
  1349. textureData->height = DICTOOL->getFloatValue_json(json, A_HEIGHT);
  1350. textureData->pivotX = DICTOOL->getFloatValue_json(json, A_PIVOT_X);
  1351. textureData->pivotY = DICTOOL->getFloatValue_json(json, A_PIVOT_Y);
  1352. int length = DICTOOL->getArrayCount_json(json, CONTOUR_DATA);
  1353. for (int i = 0; i < length; i++)
  1354. {
  1355. const rapidjson::Value &dic = DICTOOL->getSubDictionary_json(json, CONTOUR_DATA, i);
  1356. ContourData *contourData = decodeContour(dic);
  1357. textureData->contourDataList.pushBack(contourData);
  1358. contourData->release();
  1359. }
  1360. return textureData;
  1361. }
  1362. ContourData *DataReaderHelper::decodeContour(const rapidjson::Value& json)
  1363. {
  1364. ContourData *contourData = new (std::nothrow) ContourData();
  1365. contourData->init();
  1366. int length = DICTOOL->getArrayCount_json(json, VERTEX_POINT);
  1367. for (int i = length - 1; i >= 0; i--)
  1368. {
  1369. const rapidjson::Value &dic = DICTOOL->getSubDictionary_json(json, VERTEX_POINT, i);
  1370. Vec2 vertex;
  1371. vertex.x = DICTOOL->getFloatValue_json(dic, A_X);
  1372. vertex.y = DICTOOL->getFloatValue_json(dic, A_Y);
  1373. contourData->vertexList.push_back(vertex);
  1374. }
  1375. return contourData;
  1376. }
  1377. void DataReaderHelper::decodeNode(BaseData *node, const rapidjson::Value& json, DataInfo *dataInfo)
  1378. {
  1379. node->x = DICTOOL->getFloatValue_json(json, A_X) * s_PositionReadScale;
  1380. node->y = DICTOOL->getFloatValue_json(json, A_Y) * s_PositionReadScale;
  1381. node->x *= dataInfo->contentScale;
  1382. node->y *= dataInfo->contentScale;
  1383. node->zOrder = DICTOOL->getIntValue_json(json, A_Z);
  1384. node->skewX = DICTOOL->getFloatValue_json(json, A_SKEW_X);
  1385. node->skewY = DICTOOL->getFloatValue_json(json, A_SKEW_Y);
  1386. node->scaleX = DICTOOL->getFloatValue_json(json, A_SCALE_X, 1.0f);
  1387. node->scaleY = DICTOOL->getFloatValue_json(json, A_SCALE_Y, 1.0f);
  1388. if (dataInfo->cocoStudioVersion < VERSION_COLOR_READING)
  1389. {
  1390. if (DICTOOL->checkObjectExist_json(json, 0))
  1391. {
  1392. const rapidjson::Value &colorDic = DICTOOL->getSubDictionary_json(json, 0);
  1393. node->a = DICTOOL->getIntValue_json(colorDic, A_ALPHA, 255);
  1394. node->r = DICTOOL->getIntValue_json(colorDic, A_RED, 255);
  1395. node->g = DICTOOL->getIntValue_json(colorDic, A_GREEN, 255);
  1396. node->b = DICTOOL->getIntValue_json(colorDic, A_BLUE, 255);
  1397. node->isUseColorInfo = true;
  1398. }
  1399. }
  1400. else
  1401. {
  1402. if (DICTOOL->checkObjectExist_json(json, COLOR_INFO))
  1403. {
  1404. const rapidjson::Value &colorDic = DICTOOL->getSubDictionary_json(json, COLOR_INFO); //json.getSubDictionary(COLOR_INFO);
  1405. node->a = DICTOOL->getIntValue_json(colorDic, A_ALPHA, 255);
  1406. node->r = DICTOOL->getIntValue_json(colorDic, A_RED, 255);
  1407. node->g = DICTOOL->getIntValue_json(colorDic, A_GREEN, 255);
  1408. node->b = DICTOOL->getIntValue_json(colorDic, A_BLUE, 255);
  1409. node->isUseColorInfo = true;
  1410. }
  1411. }
  1412. }
  1413. void DataReaderHelper::addDataFromBinaryCache(const char *fileContent, DataInfo *dataInfo)
  1414. {
  1415. CocoLoader tCocoLoader;
  1416. if (tCocoLoader.ReadCocoBinBuff((char*)fileContent))
  1417. {
  1418. stExpCocoNode *tpRootCocoNode = tCocoLoader.GetRootCocoNode();
  1419. rapidjson::Type tType = tpRootCocoNode->GetType(&tCocoLoader);
  1420. if (rapidjson::kObjectType == tType)
  1421. {
  1422. stExpCocoNode *tpChildArray = tpRootCocoNode->GetChildArray(&tCocoLoader);
  1423. int nCount = tpRootCocoNode->GetChildNum();
  1424. dataInfo->contentScale = 1.0f;
  1425. int length = 0;
  1426. std::string key;
  1427. stExpCocoNode* pDataArray;
  1428. for (int i = 0; i < nCount; ++i)
  1429. {
  1430. key = tpChildArray[i].GetName(&tCocoLoader);
  1431. if (key.compare(CONTENT_SCALE) == 0)
  1432. {
  1433. std::string value = tpChildArray[i].GetValue(&tCocoLoader);
  1434. dataInfo->contentScale = utils::atof(value.c_str());
  1435. }
  1436. else if ( 0 == key.compare(ARMATURE_DATA))
  1437. {
  1438. pDataArray = tpChildArray[i].GetChildArray(&tCocoLoader);
  1439. length = tpChildArray[i].GetChildNum();
  1440. ArmatureData * armatureData;
  1441. for (int ii = 0; ii < length; ++ii)
  1442. {
  1443. armatureData = decodeArmature(&tCocoLoader, &pDataArray[ii], dataInfo);
  1444. if (dataInfo->asyncStruct)
  1445. {
  1446. _dataReaderHelper->_addDataMutex.lock();
  1447. }
  1448. ArmatureDataManager::getInstance()->addArmatureData(armatureData->name, armatureData, dataInfo->filename);
  1449. armatureData->release();
  1450. if (dataInfo->asyncStruct)
  1451. {
  1452. _dataReaderHelper->_addDataMutex.unlock();
  1453. }
  1454. }
  1455. }
  1456. else if ( 0 == key.compare(ANIMATION_DATA))
  1457. {
  1458. pDataArray = tpChildArray[i].GetChildArray(&tCocoLoader);
  1459. length = tpChildArray[i].GetChildNum();
  1460. AnimationData *animationData;
  1461. for (int ii = 0; ii < length; ++ii)
  1462. {
  1463. animationData = decodeAnimation(&tCocoLoader, &pDataArray[ii], dataInfo);
  1464. if (dataInfo->asyncStruct)
  1465. {
  1466. _dataReaderHelper->_addDataMutex.lock();
  1467. }
  1468. ArmatureDataManager::getInstance()->addAnimationData(animationData->name, animationData, dataInfo->filename);
  1469. animationData->release();
  1470. if (dataInfo->asyncStruct)
  1471. {
  1472. _dataReaderHelper->_addDataMutex.unlock();
  1473. }
  1474. }
  1475. }
  1476. else if (key.compare(TEXTURE_DATA) == 0)
  1477. {
  1478. pDataArray = tpChildArray[i].GetChildArray(&tCocoLoader);
  1479. length = tpChildArray[i].GetChildNum();
  1480. for (int ii = 0; ii < length; ++ii)
  1481. {
  1482. TextureData *textureData = decodeTexture(&tCocoLoader, &pDataArray[ii]);
  1483. if (dataInfo->asyncStruct)
  1484. {
  1485. _dataReaderHelper->_addDataMutex.lock();
  1486. }
  1487. ArmatureDataManager::getInstance()->addTextureData(textureData->name, textureData, dataInfo->filename);
  1488. textureData->release();
  1489. if (dataInfo->asyncStruct)
  1490. {
  1491. _dataReaderHelper->_addDataMutex.unlock();
  1492. }
  1493. }
  1494. }
  1495. }
  1496. // Auto losprite file
  1497. bool autoLoad = dataInfo->asyncStruct == nullptr ? ArmatureDataManager::getInstance()->isAutoLoadSpriteFile() : dataInfo->asyncStruct->autoLoadSpriteFile;
  1498. if (autoLoad)
  1499. {
  1500. for (int i = 0; i < nCount; ++i)
  1501. {
  1502. key = tpChildArray[i].GetName(&tCocoLoader);
  1503. if( 0 != key.compare(CONFIG_FILE_PATH))
  1504. {
  1505. continue;
  1506. }
  1507. length = tpChildArray[i].GetChildNum();
  1508. stExpCocoNode *pConfigFilePath = tpChildArray[i].GetChildArray(&tCocoLoader);
  1509. for (int ii = 0; ii < length; ii++)
  1510. {
  1511. const char *path = pConfigFilePath[ii].GetValue(&tCocoLoader);
  1512. if (path == nullptr)
  1513. {
  1514. CCLOG("load CONFIG_FILE_PATH error.");
  1515. return;
  1516. }
  1517. std::string filePath = path;
  1518. filePath = filePath.erase(filePath.find_last_of("."));
  1519. if (dataInfo->asyncStruct)
  1520. {
  1521. dataInfo->configFileQueue.push(filePath);
  1522. }
  1523. else
  1524. {
  1525. std::string plistPath = filePath + ".plist";
  1526. std::string pngPath = filePath + ".png";
  1527. ArmatureDataManager::getInstance()->addSpriteFrameFromFile((dataInfo->baseFilePath + plistPath), (dataInfo->baseFilePath + pngPath), dataInfo->filename);
  1528. }
  1529. }
  1530. }
  1531. }
  1532. }
  1533. }
  1534. }
  1535. ArmatureData* DataReaderHelper::decodeArmature(CocoLoader *cocoLoader, stExpCocoNode *cocoNode, DataInfo *dataInfo)
  1536. {
  1537. ArmatureData *armatureData = new (std::nothrow) ArmatureData();
  1538. armatureData->init();
  1539. stExpCocoNode *pAramtureDataArray = cocoNode->GetChildArray(cocoLoader);
  1540. const char *name = pAramtureDataArray[2].GetValue(cocoLoader); //DICTOOL->getStringValue_json(json, A_NAME);
  1541. if(name != nullptr)
  1542. {
  1543. armatureData->name = name;
  1544. }
  1545. float version = utils::atof(pAramtureDataArray[1].GetValue(cocoLoader));
  1546. dataInfo->cocoStudioVersion = armatureData->dataVersion = version; //DICTOOL->getFloatValue_json(json, VERSION, 0.1f);
  1547. int length = pAramtureDataArray[3].GetChildNum(); //DICTOOL->getArrayCount_json(json, BONE_DATA, 0);
  1548. stExpCocoNode *pBoneChildren = pAramtureDataArray[3].GetChildArray(cocoLoader);
  1549. stExpCocoNode* child;
  1550. for (int i = 0; i < length; i++)
  1551. {
  1552. //const rapidjson::Value &dic = DICTOOL->getSubDictionary_json(json, BONE_DATA, i); //json[BONE_DATA][i];
  1553. child = &pBoneChildren[i];
  1554. BoneData *boneData = decodeBone(cocoLoader, child, dataInfo);
  1555. armatureData->addBoneData(boneData);
  1556. boneData->release();
  1557. }
  1558. return armatureData;
  1559. }
  1560. BoneData* DataReaderHelper::decodeBone(CocoLoader *cocoLoader, stExpCocoNode *cocoNode, DataInfo *dataInfo)
  1561. {
  1562. BoneData *boneData = new (std::nothrow) BoneData();
  1563. boneData->init();
  1564. decodeNode(boneData, cocoLoader, cocoNode, dataInfo);
  1565. int length = cocoNode->GetChildNum();
  1566. stExpCocoNode *pBoneChildren = cocoNode->GetChildArray(cocoLoader);
  1567. stExpCocoNode* child;
  1568. const char *str = nullptr;
  1569. std::string key;
  1570. for (int i = 0; i < length; ++i)
  1571. {
  1572. child = &pBoneChildren[i];
  1573. key = child->GetName(cocoLoader);
  1574. str = child->GetValue(cocoLoader);
  1575. if (key.compare(A_NAME) == 0)
  1576. {
  1577. //DICTOOL->getStringValue_json(json, A_NAME);
  1578. if(str != nullptr)
  1579. {
  1580. boneData->name = str;
  1581. }
  1582. }
  1583. else if (key.compare(A_PARENT) == 0)
  1584. {
  1585. //DICTOOL->getStringValue_json(json, A_PARENT);
  1586. if(str != nullptr)
  1587. {
  1588. boneData->parentName = str;
  1589. }
  1590. }
  1591. else if (key.compare(DISPLAY_DATA) == 0)
  1592. {
  1593. int count = child->GetChildNum();
  1594. stExpCocoNode *pDisplayData = child->GetChildArray(cocoLoader);
  1595. for (int ii = 0; ii < count; ++ii)
  1596. {
  1597. DisplayData *displayData = decodeBoneDisplay(cocoLoader, &pDisplayData[ii], dataInfo);
  1598. if(displayData == nullptr)
  1599. continue;
  1600. boneData->addDisplayData(displayData);
  1601. displayData->release();
  1602. }
  1603. }
  1604. }
  1605. return boneData;
  1606. }
  1607. DisplayData* DataReaderHelper::decodeBoneDisplay(CocoLoader *cocoLoader, stExpCocoNode *cocoNode, DataInfo *dataInfo)
  1608. {
  1609. stExpCocoNode* children = cocoNode->GetChildArray(cocoLoader);
  1610. stExpCocoNode* child = &children[1];
  1611. std::string key = child->GetName(cocoLoader);
  1612. const char *str = child->GetValue(cocoLoader);
  1613. DisplayData *displayData = nullptr;
  1614. if (key.compare(A_DISPLAY_TYPE) == 0)
  1615. {
  1616. str = child->GetValue(cocoLoader);
  1617. DisplayType displayType = (DisplayType)(atoi(str));
  1618. int length = 0;
  1619. switch (displayType)
  1620. {
  1621. case CS_DISPLAY_SPRITE:
  1622. {
  1623. displayData = new (std::nothrow) SpriteDisplayData();
  1624. const char *name = children[0].GetValue(cocoLoader);
  1625. if(name != nullptr)
  1626. {
  1627. ((SpriteDisplayData *)displayData)->displayName = name;
  1628. }
  1629. stExpCocoNode *pSkinDataArray = children[2].GetChildArray(cocoLoader);
  1630. if (pSkinDataArray != nullptr)
  1631. {
  1632. stExpCocoNode *pSkinData = &pSkinDataArray[0];
  1633. if (pSkinData != nullptr)
  1634. {
  1635. SpriteDisplayData *sdd = (SpriteDisplayData *)displayData;
  1636. length = pSkinData->GetChildNum();
  1637. stExpCocoNode *SkinDataValue = pSkinData->GetChildArray(cocoLoader);
  1638. for (int i = 0; i < length; ++i)
  1639. {
  1640. key = SkinDataValue[i].GetName(cocoLoader);
  1641. str = SkinDataValue[i].GetValue(cocoLoader);
  1642. if (key.compare(A_X) == 0)
  1643. {
  1644. sdd->skinData.x = utils::atof(str) * s_PositionReadScale;
  1645. }
  1646. else if (key.compare(A_Y) == 0)
  1647. {
  1648. sdd->skinData.y = utils::atof(str) * s_PositionReadScale;
  1649. }
  1650. else if (key.compare(A_SCALE_X) == 0)
  1651. {
  1652. sdd->skinData.scaleX = utils::atof(str);
  1653. }
  1654. else if (key.compare(A_SCALE_Y) == 0)
  1655. {
  1656. sdd->skinData.scaleY = utils::atof(str);
  1657. }
  1658. else if (key.compare(A_SKEW_X) == 0)
  1659. {
  1660. sdd->skinData.skewX = utils::atof(str);
  1661. }
  1662. else if (key.compare(A_SKEW_Y) == 0)
  1663. {
  1664. sdd->skinData.skewY = utils::atof(str);
  1665. }
  1666. }
  1667. sdd->skinData.x *= dataInfo->contentScale;
  1668. sdd->skinData.y *= dataInfo->contentScale;
  1669. }
  1670. }
  1671. }
  1672. break;
  1673. case CS_DISPLAY_ARMATURE:
  1674. {
  1675. displayData = new (std::nothrow) ArmatureDisplayData();
  1676. const char *name = cocoNode[0].GetValue(cocoLoader);
  1677. if(name != nullptr)
  1678. {
  1679. ((ArmatureDisplayData *)displayData)->displayName = name;
  1680. }
  1681. }
  1682. break;
  1683. case CS_DISPLAY_PARTICLE:
  1684. {
  1685. displayData = new (std::nothrow) ParticleDisplayData();
  1686. length = cocoNode->GetChildNum();
  1687. stExpCocoNode *pDisplayData = cocoNode->GetChildArray(cocoLoader);
  1688. for (int i = 0; i < length; ++i)
  1689. {
  1690. key = pDisplayData[i].GetName(cocoLoader);
  1691. str = pDisplayData[i].GetValue(cocoLoader);
  1692. if (key.compare(A_PLIST) == 0)
  1693. {
  1694. const char *plist = str;
  1695. if(plist != nullptr)
  1696. {
  1697. if (dataInfo->asyncStruct)
  1698. {
  1699. ((ParticleDisplayData *)displayData)->displayName = dataInfo->asyncStruct->baseFilePath + plist;
  1700. }
  1701. else
  1702. {
  1703. ((ParticleDisplayData *)displayData)->displayName = dataInfo->baseFilePath + plist;
  1704. }
  1705. }
  1706. }
  1707. }
  1708. }
  1709. break;
  1710. default:
  1711. displayData = new (std::nothrow) SpriteDisplayData();
  1712. break;
  1713. }
  1714. displayData->displayType = displayType;
  1715. }
  1716. return displayData;
  1717. }
  1718. AnimationData* DataReaderHelper::decodeAnimation(CocoLoader *cocoLoader, stExpCocoNode *cocoNode, DataInfo *dataInfo)
  1719. {
  1720. AnimationData *aniData = new (std::nothrow) AnimationData();
  1721. int length = cocoNode->GetChildNum();
  1722. stExpCocoNode *pAnimationData = cocoNode->GetChildArray(cocoLoader);
  1723. const char *str = nullptr;
  1724. std::string key;
  1725. stExpCocoNode* child;
  1726. MovementData *movementData;
  1727. for (int i = 0; i < length; ++i)
  1728. {
  1729. child = &pAnimationData[i];
  1730. key = child->GetName(cocoLoader);
  1731. str = child->GetValue(cocoLoader);
  1732. if (key.compare(A_NAME) == 0)
  1733. {
  1734. if(str != nullptr)
  1735. {
  1736. aniData->name = str;
  1737. }
  1738. }
  1739. else if (key.compare(MOVEMENT_DATA) == 0)
  1740. {
  1741. int movcount = child->GetChildNum();
  1742. stExpCocoNode* movArray = child->GetChildArray(cocoLoader);
  1743. for( int movnum =0; movnum <movcount; movnum++)
  1744. {
  1745. movementData = decodeMovement(cocoLoader, &movArray[movnum], dataInfo);
  1746. aniData->addMovement(movementData);
  1747. movementData->release();
  1748. }
  1749. }
  1750. }
  1751. return aniData;
  1752. }
  1753. MovementData* DataReaderHelper::decodeMovement(CocoLoader *cocoLoader, stExpCocoNode *cocoNode, DataInfo *dataInfo)
  1754. {
  1755. MovementData *movementData = new (std::nothrow) MovementData();
  1756. movementData->scale = 1.0f;
  1757. int length = cocoNode->GetChildNum();
  1758. stExpCocoNode *pMoveDataArray = cocoNode->GetChildArray(cocoLoader);
  1759. const char *str = nullptr;
  1760. std::string key;
  1761. stExpCocoNode* child;
  1762. for (int i = 0; i < length; ++i)
  1763. {
  1764. child = &pMoveDataArray[i];
  1765. key = child->GetName(cocoLoader);
  1766. str = child->GetValue(cocoLoader);
  1767. if (key.compare(A_NAME) == 0)
  1768. {
  1769. if(str != nullptr)
  1770. {
  1771. movementData->name = str;
  1772. }
  1773. }
  1774. else if (key.compare(A_LOOP) == 0)
  1775. {
  1776. movementData->loop = true;
  1777. if(str != nullptr)
  1778. {
  1779. if (strcmp("1", str) != 0)
  1780. {
  1781. movementData->loop = false;
  1782. }
  1783. }
  1784. }
  1785. else if (key.compare(A_DURATION_TWEEN) == 0)
  1786. {
  1787. movementData->durationTween = 0;
  1788. if(str != nullptr)
  1789. {
  1790. movementData->durationTween = atoi(str);
  1791. }
  1792. }
  1793. else if (key.compare(A_DURATION_TO) == 0)
  1794. {
  1795. movementData->durationTo = 0;
  1796. if(str != nullptr)
  1797. {
  1798. movementData->durationTo = atoi(str);
  1799. }
  1800. }
  1801. else if (key.compare(A_DURATION) == 0)
  1802. {
  1803. movementData->duration = 0;
  1804. if(str != nullptr)
  1805. {
  1806. movementData->duration = atoi(str);
  1807. }
  1808. }
  1809. else if (key.compare(A_MOVEMENT_SCALE) == 0)
  1810. {
  1811. movementData->scale = 1.0;
  1812. if(str != nullptr)
  1813. {
  1814. movementData->scale = utils::atof(str);
  1815. }
  1816. }
  1817. else if (key.compare(A_TWEEN_EASING) == 0)
  1818. {
  1819. movementData->tweenEasing = cocos2d::tweenfunc::Linear;
  1820. if(str != nullptr)
  1821. {
  1822. movementData->tweenEasing = (TweenType)(atoi(str));
  1823. }
  1824. }
  1825. else if (key.compare(MOVEMENT_BONE_DATA) == 0)
  1826. {
  1827. int count = child->GetChildNum();
  1828. stExpCocoNode *pMoveBoneData = child->GetChildArray(cocoLoader);
  1829. MovementBoneData *movementBoneData;
  1830. for (int ii = 0; ii < count; ++ii)
  1831. {
  1832. movementBoneData = decodeMovementBone(cocoLoader, &pMoveBoneData[ii],dataInfo);
  1833. movementData->addMovementBoneData(movementBoneData);
  1834. movementBoneData->release();
  1835. }
  1836. }
  1837. }
  1838. return movementData;
  1839. }
  1840. MovementBoneData* DataReaderHelper::decodeMovementBone(CocoLoader *cocoLoader, stExpCocoNode *cocoNode, DataInfo *dataInfo)
  1841. {
  1842. MovementBoneData *movementBoneData = new (std::nothrow) MovementBoneData();
  1843. movementBoneData->init();
  1844. int length = cocoNode->GetChildNum();
  1845. stExpCocoNode *pMovementBoneDataArray = cocoNode->GetChildArray(cocoLoader);
  1846. stExpCocoNode* movebonechild;
  1847. const char *str = nullptr;
  1848. for (int i = 0; i < length; ++i)
  1849. {
  1850. movebonechild = &pMovementBoneDataArray[i];
  1851. std::string key = movebonechild->GetName(cocoLoader);
  1852. str = movebonechild->GetValue(cocoLoader);
  1853. if (key.compare(A_NAME) == 0)
  1854. {
  1855. if(str != nullptr)
  1856. {
  1857. movementBoneData->name = str;
  1858. }
  1859. }
  1860. else if (key.compare(A_MOVEMENT_DELAY) == 0)
  1861. {
  1862. if(str != nullptr)
  1863. {
  1864. movementBoneData->delay = utils::atof(str);
  1865. }
  1866. }
  1867. else if (key.compare(FRAME_DATA) == 0)
  1868. {
  1869. int count =movebonechild->GetChildNum();
  1870. stExpCocoNode *pFrameDataArray = movebonechild->GetChildArray(cocoLoader);
  1871. for (int ii = 0; ii < count; ++ii)
  1872. {
  1873. FrameData *frameData = decodeFrame(cocoLoader, &pFrameDataArray[ii], dataInfo);
  1874. movementBoneData->addFrameData(frameData);
  1875. frameData->release();
  1876. if (dataInfo->cocoStudioVersion < VERSION_COMBINED)
  1877. {
  1878. frameData->frameID = movementBoneData->duration;
  1879. movementBoneData->duration += frameData->duration;
  1880. }
  1881. }
  1882. }
  1883. }
  1884. const ssize_t framesizemusone = movementBoneData->frameList.size()-1;
  1885. if (dataInfo->cocoStudioVersion < VERSION_CHANGE_ROTATION_RANGE)
  1886. {
  1887. //! Change rotation range from (-180 -- 180) to (-infinity -- infinity)
  1888. cocos2d::Vector<FrameData*> frames =movementBoneData->frameList;
  1889. ssize_t imusone =0;
  1890. ssize_t i =0;
  1891. for (i = framesizemusone; i >= 0; i--)
  1892. {
  1893. if (i > 0)
  1894. {
  1895. imusone = i-1;
  1896. float difSkewX = frames.at(i)->skewX - frames.at(imusone)->skewX;
  1897. float difSkewY = frames.at(i)->skewY - frames.at(imusone)->skewY;
  1898. if (difSkewX < -M_PI || difSkewX > M_PI)
  1899. {
  1900. frames.at(imusone)->skewX = difSkewX < 0 ? frames.at(imusone)->skewX - 2 * M_PI : frames.at(imusone)->skewX + 2 * M_PI;
  1901. }
  1902. if (difSkewY < -M_PI || difSkewY > M_PI)
  1903. {
  1904. frames.at(imusone)->skewY = difSkewY < 0 ? frames.at(imusone)->skewY - 2 * M_PI : frames.at(imusone)->skewY + 2 * M_PI;
  1905. }
  1906. }
  1907. }
  1908. }
  1909. if (dataInfo->cocoStudioVersion < VERSION_COMBINED)
  1910. {
  1911. if (movementBoneData->frameList.size() > 0)
  1912. {
  1913. auto frameData = movementBoneData->frameList.at(framesizemusone);
  1914. movementBoneData->addFrameData(frameData);
  1915. frameData->release();
  1916. frameData->frameID = movementBoneData->duration;
  1917. }
  1918. }
  1919. return movementBoneData;
  1920. }
  1921. FrameData* DataReaderHelper::decodeFrame(CocoLoader *cocoLoader, stExpCocoNode *cocoNode, DataInfo *dataInfo)
  1922. {
  1923. FrameData *frameData = new (std::nothrow) FrameData();
  1924. decodeNode(frameData, cocoLoader, cocoNode, dataInfo);
  1925. int length = cocoNode->GetChildNum();
  1926. stExpCocoNode *pFrameDataArray = cocoNode->GetChildArray(cocoLoader);
  1927. const char *str = nullptr;
  1928. for (int i = 0; i < length; ++i)
  1929. {
  1930. std::string key = pFrameDataArray[i].GetName(cocoLoader);
  1931. str = pFrameDataArray[i].GetValue(cocoLoader);
  1932. if (key.compare(A_TWEEN_EASING) == 0)
  1933. {
  1934. frameData->tweenEasing = cocos2d::tweenfunc::Linear;
  1935. if(str != nullptr)
  1936. {
  1937. frameData->tweenEasing = (TweenType)(atoi(str));
  1938. }
  1939. }
  1940. else if (key.compare(A_DISPLAY_INDEX) == 0)
  1941. {
  1942. if(str != nullptr)
  1943. {
  1944. frameData->displayIndex = atoi(str);
  1945. }
  1946. }
  1947. else if (key.compare(A_BLEND_SRC) == 0)
  1948. {
  1949. if(str != nullptr)
  1950. {
  1951. frameData->blendFunc.src = (GLenum)(atoi(str));
  1952. }
  1953. }
  1954. else if (key.compare(A_BLEND_DST) == 0)
  1955. {
  1956. if(str != nullptr)
  1957. {
  1958. frameData->blendFunc.dst = (GLenum)(atoi(str));
  1959. }
  1960. }
  1961. else if (key.compare(A_TWEEN_FRAME) == 0)
  1962. {
  1963. frameData->isTween = true;
  1964. if(str != nullptr)
  1965. {
  1966. if (strcmp("1", str) != 0)
  1967. {
  1968. frameData->isTween = false;
  1969. }
  1970. }
  1971. }
  1972. else if (key.compare(A_EVENT) == 0)
  1973. {
  1974. if(str != nullptr)
  1975. {
  1976. frameData->strEvent = str;
  1977. }
  1978. }
  1979. else if (key.compare(A_DURATION) == 0)
  1980. {
  1981. if (dataInfo->cocoStudioVersion < VERSION_COMBINED)
  1982. {
  1983. frameData->duration = 1;
  1984. if(str != nullptr)
  1985. {
  1986. frameData->duration = atoi(str);
  1987. }
  1988. }
  1989. }
  1990. else if (key.compare(A_FRAME_INDEX) == 0)
  1991. {
  1992. if (dataInfo->cocoStudioVersion >= VERSION_COMBINED)
  1993. {
  1994. if(str != nullptr)
  1995. {
  1996. frameData->frameID = atoi(str);
  1997. }
  1998. }
  1999. }
  2000. else if (key.compare(A_EASING_PARAM) == 0)
  2001. {
  2002. int count = pFrameDataArray[i].GetChildNum();
  2003. if (count != 0 )
  2004. {
  2005. frameData->easingParams = new (std::nothrow) float[count];
  2006. stExpCocoNode *pFrameData = pFrameDataArray[i].GetChildArray(cocoLoader);
  2007. for (int ii = 0; ii < count; ++ii)
  2008. {
  2009. str = pFrameData[ii].GetValue(cocoLoader);
  2010. if (str != nullptr)
  2011. {
  2012. frameData->easingParams[ii] = utils::atof(str);
  2013. }
  2014. }
  2015. }
  2016. }
  2017. }
  2018. return frameData;
  2019. }
  2020. TextureData* DataReaderHelper::decodeTexture(CocoLoader *cocoLoader, stExpCocoNode *cocoNode)
  2021. {
  2022. TextureData *textureData = new (std::nothrow) TextureData();
  2023. textureData->init();
  2024. if(cocoNode == nullptr)
  2025. {
  2026. return textureData;
  2027. }
  2028. int length = cocoNode->GetChildNum();
  2029. stExpCocoNode *pTextureDataArray = cocoNode->GetChildArray(cocoLoader);
  2030. const char *str = nullptr;
  2031. for (int i = 0; i < length; ++i)
  2032. {
  2033. std::string key = pTextureDataArray[i].GetName(cocoLoader);
  2034. str = pTextureDataArray[i].GetValue(cocoLoader);
  2035. if (key.compare(A_NAME) == 0)
  2036. {
  2037. if(str != nullptr)
  2038. {
  2039. textureData->name = str;
  2040. }
  2041. }
  2042. else if (key.compare(A_WIDTH) == 0)
  2043. {
  2044. if(str != nullptr)
  2045. {
  2046. textureData->width = utils::atof(str);
  2047. }
  2048. }
  2049. else if (key.compare(A_HEIGHT) == 0)
  2050. {
  2051. if(str != nullptr)
  2052. {
  2053. textureData->height = utils::atof(str);
  2054. }
  2055. }
  2056. else if (key.compare(A_PIVOT_X) == 0)
  2057. {
  2058. if(str != nullptr)
  2059. {
  2060. textureData->pivotX = utils::atof(str);
  2061. }
  2062. }
  2063. else if (key.compare(A_PIVOT_Y) == 0)
  2064. {
  2065. if(str != nullptr)
  2066. {
  2067. textureData->pivotY = utils::atof(str);
  2068. }
  2069. }
  2070. else if (key.compare(CONTOUR_DATA) == 0)
  2071. {
  2072. int count = pTextureDataArray[i].GetChildNum();
  2073. stExpCocoNode *pContourArray = pTextureDataArray[i].GetChildArray(cocoLoader);
  2074. for (int ii = 0; ii < count; ++ii)
  2075. {
  2076. ContourData *contourData = decodeContour(cocoLoader, &pContourArray[ii]);
  2077. textureData->contourDataList.pushBack(contourData);
  2078. contourData->release();
  2079. }
  2080. }
  2081. }
  2082. return textureData;
  2083. }
  2084. ContourData* DataReaderHelper::decodeContour(CocoLoader *cocoLoader, stExpCocoNode *cocoNode)
  2085. {
  2086. ContourData *contourData = new (std::nothrow) ContourData();
  2087. contourData->init();
  2088. int length = cocoNode->GetChildNum();
  2089. stExpCocoNode *verTexPointArray = cocoNode->GetChildArray(cocoLoader);
  2090. const char *str = nullptr;
  2091. for (int i = 0; i < length; ++i)
  2092. {
  2093. std::string key = verTexPointArray[i].GetName(cocoLoader);
  2094. str = verTexPointArray[i].GetValue(cocoLoader);
  2095. if (key.compare(VERTEX_POINT) == 0)
  2096. {
  2097. int count = verTexPointArray[i].GetChildNum();
  2098. stExpCocoNode *pVerTexPointArray = verTexPointArray[i].GetChildArray(cocoLoader);
  2099. stExpCocoNode *pVerTexPoint;
  2100. for (int ii = count - 1; ii >= 0; --ii)
  2101. {
  2102. pVerTexPoint = pVerTexPointArray[ii].GetChildArray(cocoLoader);
  2103. Vec2 vertex;
  2104. vertex.x = utils::atof(pVerTexPoint[0].GetValue(cocoLoader));
  2105. vertex.y = utils::atof(pVerTexPoint[1].GetValue(cocoLoader));
  2106. contourData->vertexList.push_back(vertex); }
  2107. break;
  2108. }
  2109. }
  2110. return contourData;
  2111. }
  2112. void DataReaderHelper::decodeNode(BaseData *node, CocoLoader *cocoLoader, stExpCocoNode* cocoNode, DataInfo *dataInfo)
  2113. {
  2114. int length = cocoNode->GetChildNum();
  2115. stExpCocoNode *NodeArray = cocoNode->GetChildArray(cocoLoader);
  2116. const char *str = nullptr;
  2117. bool isVersionL = dataInfo->cocoStudioVersion < VERSION_COLOR_READING;
  2118. stExpCocoNode* child;
  2119. for (int i = 0; i < length; ++i)
  2120. {
  2121. child = &NodeArray[i];
  2122. std::string key = child->GetName(cocoLoader);
  2123. str = child->GetValue(cocoLoader);
  2124. if (key.compare(A_X) == 0)
  2125. {
  2126. node->x = utils::atof(str) * dataInfo->contentScale;
  2127. }
  2128. else if (key.compare(A_Y) == 0)
  2129. {
  2130. node->y = utils::atof(str) * dataInfo->contentScale;
  2131. }
  2132. else if (key.compare(A_Z) == 0)
  2133. {
  2134. node->zOrder = atoi(str);
  2135. }
  2136. else if (key.compare(A_SKEW_X) == 0)
  2137. {
  2138. node->skewX = utils::atof(str);
  2139. }
  2140. else if (key.compare(A_SKEW_Y) == 0)
  2141. {
  2142. node->skewY = utils::atof(str);
  2143. }
  2144. else if (key.compare(A_SCALE_X) == 0)
  2145. {
  2146. node->scaleX = utils::atof(str);
  2147. }
  2148. else if (key.compare(A_SCALE_Y) == 0)
  2149. {
  2150. node->scaleY = utils::atof(str);
  2151. }
  2152. else if (key.compare(COLOR_INFO) == 0)
  2153. {
  2154. if (!isVersionL)
  2155. {
  2156. if (child->GetType(cocoLoader) == rapidjson::kObjectType)
  2157. {
  2158. if(child->GetChildNum() == 4)
  2159. {
  2160. stExpCocoNode *ChildArray = child->GetChildArray(cocoLoader);
  2161. node->a = atoi(ChildArray[0].GetValue(cocoLoader));
  2162. node->r = atoi(ChildArray[1].GetValue(cocoLoader));
  2163. node->g = atoi(ChildArray[2].GetValue(cocoLoader));
  2164. node->b = atoi(ChildArray[3].GetValue(cocoLoader));
  2165. }
  2166. }
  2167. node->isUseColorInfo = true;
  2168. }
  2169. }
  2170. }
  2171. if (isVersionL)
  2172. {
  2173. int colorcoount = NodeArray[0].GetChildNum();
  2174. if(colorcoount>0)
  2175. {
  2176. if (NodeArray[0].GetType(cocoLoader) == rapidjson::kObjectType)
  2177. {
  2178. if(NodeArray[0].GetChildNum() == 4)
  2179. {
  2180. stExpCocoNode *ChildArray = NodeArray[0].GetChildArray(cocoLoader);
  2181. node->a = atoi(ChildArray[0].GetValue(cocoLoader));
  2182. node->r = atoi(ChildArray[1].GetValue(cocoLoader));
  2183. node->g = atoi(ChildArray[2].GetValue(cocoLoader));
  2184. node->b = atoi(ChildArray[3].GetValue(cocoLoader));
  2185. }
  2186. }
  2187. node->isUseColorInfo = true;
  2188. }
  2189. }
  2190. }
  2191. }