CCDataReaderHelper.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. #ifndef __CCDATAREADERHELPER_H__
  21. #define __CCDATAREADERHELPER_H__
  22. #include "editor-support/cocostudio/CCArmatureDefine.h"
  23. #include "editor-support/cocostudio/CCDatas.h"
  24. #include "editor-support/cocostudio/CCArmature.h"
  25. #include "editor-support/cocostudio/DictionaryHelper.h"
  26. #include "editor-support/cocostudio/CocosStudioExport.h"
  27. #include "json/document-wrapper.h"
  28. #include <string>
  29. #include <queue>
  30. #include <mutex>
  31. #include <thread>
  32. #include <condition_variable>
  33. namespace tinyxml2
  34. {
  35. class XMLElement;
  36. }
  37. namespace cocostudio {
  38. class CocoLoader;
  39. struct stExpCocoNode;
  40. /**
  41. * @js NA
  42. * @lua NA
  43. */
  44. class CC_STUDIO_DLL DataReaderHelper : cocos2d::Ref
  45. {
  46. protected:
  47. enum ConfigType
  48. {
  49. DragonBone_XML,
  50. CocoStudio_JSON,
  51. CocoStudio_Binary
  52. };
  53. typedef struct _AsyncStruct
  54. {
  55. std::string filename;
  56. std::string fileContent;
  57. ConfigType configType;
  58. std::string baseFilePath;
  59. cocos2d::Ref *target;
  60. cocos2d::SEL_SCHEDULE selector;
  61. bool autoLoadSpriteFile;
  62. std::string imagePath;
  63. std::string plistPath;
  64. } AsyncStruct;
  65. typedef struct _DataInfo
  66. {
  67. AsyncStruct *asyncStruct;
  68. std::queue<std::string> configFileQueue;
  69. float contentScale;
  70. std::string filename;
  71. std::string baseFilePath;
  72. float flashToolVersion;
  73. float cocoStudioVersion;
  74. } DataInfo;
  75. public:
  76. /** @deprecated Use getInstance() instead */
  77. CC_DEPRECATED_ATTRIBUTE static DataReaderHelper *sharedDataReaderHelper() { return DataReaderHelper::getInstance(); }
  78. static DataReaderHelper *getInstance();
  79. /**
  80. * Scale the position data, used for multiresolution adapter
  81. * It won't effect the data already read.
  82. */
  83. static void setPositionReadScale(float scale);
  84. static float getPositionReadScale();
  85. static void purge();
  86. public:
  87. /**
  88. * @js ctor
  89. */
  90. DataReaderHelper();
  91. /**
  92. * @js NA
  93. * @lua NA
  94. */
  95. ~DataReaderHelper();
  96. void addDataFromFile(const std::string& filePath);
  97. void addDataFromFileAsync(const std::string& imagePath, const std::string& plistPath, const std::string& filePath, cocos2d::Ref *target, cocos2d::SEL_SCHEDULE selector);
  98. void addDataAsyncCallBack(float dt);
  99. void removeConfigFile(const std::string& configFile);
  100. public:
  101. /**
  102. * Translate XML export from Dragon Bone flash tool to datas, and save them.
  103. * When you add a new xml, the data already saved will be kept.
  104. *
  105. * @param xmlPath The cache of the xml
  106. */
  107. static void addDataFromCache(const std::string& pFileContent, DataInfo *dataInfo = nullptr);
  108. /**
  109. * Decode Armature Datas from xml export from Dragon Bone flash tool
  110. */
  111. static ArmatureData *decodeArmature(tinyxml2::XMLElement *armatureXML, DataInfo *dataInfo);
  112. static BoneData *decodeBone(tinyxml2::XMLElement *boneXML, tinyxml2::XMLElement *parentXML, DataInfo *dataInfo);
  113. static DisplayData *decodeBoneDisplay(tinyxml2::XMLElement *displayXML, DataInfo *dataInfo);
  114. /**
  115. * Decode ArmatureAnimation Datas from xml export from Dragon Bone flash tool
  116. */
  117. static AnimationData *decodeAnimation(tinyxml2::XMLElement *animationXML, DataInfo *dataInfo);
  118. static MovementData *decodeMovement(tinyxml2::XMLElement *movementXML, ArmatureData *armatureData, DataInfo *dataInfo);
  119. static MovementBoneData *decodeMovementBone(tinyxml2::XMLElement *movBoneXml, tinyxml2::XMLElement *parentXml, BoneData *boneData, DataInfo *dataInfo);
  120. static FrameData *decodeFrame(tinyxml2::XMLElement *frameXML, tinyxml2::XMLElement *parentFrameXml, BoneData *boneData, DataInfo *dataInfo);
  121. /**
  122. * Decode Texture Datas from xml export from Dragon Bone flash tool
  123. */
  124. static TextureData *decodeTexture(tinyxml2::XMLElement *textureXML, DataInfo *dataInfo);
  125. /**
  126. * Decode Contour Datas from xml export from Dragon Bone flash tool
  127. */
  128. static ContourData *decodeContour(tinyxml2::XMLElement *contourXML, DataInfo *dataInfo);
  129. public:
  130. static void addDataFromJsonCache(const std::string& fileContent, DataInfo *dataInfo = nullptr);
  131. static ArmatureData *decodeArmature(const rapidjson::Value& json, DataInfo *dataInfo);
  132. static BoneData *decodeBone(const rapidjson::Value& json, DataInfo *dataInfo);
  133. static DisplayData *decodeBoneDisplay(const rapidjson::Value& json, DataInfo *dataInfo);
  134. static AnimationData *decodeAnimation(const rapidjson::Value& json, DataInfo *dataInfo);
  135. static MovementData *decodeMovement(const rapidjson::Value& json, DataInfo *dataInfo);
  136. static MovementBoneData *decodeMovementBone(const rapidjson::Value& json, DataInfo *dataInfo);
  137. static FrameData *decodeFrame(const rapidjson::Value& json, DataInfo *dataInfo);
  138. static TextureData *decodeTexture(const rapidjson::Value& json);
  139. static ContourData *decodeContour(const rapidjson::Value& json);
  140. static void decodeNode(BaseData *node, const rapidjson::Value& json, DataInfo *dataInfo);
  141. // for binary decode
  142. public:
  143. static void addDataFromBinaryCache(const char *fileContent, DataInfo *dataInfo = nullptr);
  144. static ArmatureData *decodeArmature(CocoLoader *cocoLoader, stExpCocoNode *pCocoNode, DataInfo *dataInfo);
  145. static BoneData *decodeBone(CocoLoader *cocoLoader, stExpCocoNode *pCocoNode, DataInfo *dataInfo);
  146. static DisplayData *decodeBoneDisplay(CocoLoader *cocoLoader, stExpCocoNode *pCocoNode, DataInfo *dataInfo);
  147. static AnimationData *decodeAnimation(CocoLoader *cocoLoader, stExpCocoNode *pCocoNode, DataInfo *dataInfo);
  148. static MovementData *decodeMovement(CocoLoader *cocoLoader, stExpCocoNode *pCocoNode, DataInfo *dataInfo);
  149. static MovementBoneData *decodeMovementBone(CocoLoader *cocoLoader, stExpCocoNode *pCocoNode, DataInfo *dataInfo);
  150. static FrameData *decodeFrame(CocoLoader *cocoLoader, stExpCocoNode *pCocoNode, DataInfo *dataInfo);
  151. static TextureData *decodeTexture(CocoLoader *cocoLoader, stExpCocoNode *pCocoNode);
  152. static ContourData *decodeContour(CocoLoader *cocoLoader, stExpCocoNode *pCocoNode);
  153. static void decodeNode(BaseData *node, CocoLoader *cocoLoader, stExpCocoNode *pCocoNode, DataInfo *dataInfo);
  154. protected:
  155. void loadData();
  156. std::condition_variable _sleepCondition;
  157. std::thread *_loadingThread;
  158. std::mutex _sleepMutex;
  159. std::mutex _asyncStructQueueMutex;
  160. std::mutex _dataInfoMutex;
  161. std::mutex _addDataMutex;
  162. std::mutex _getFileMutex;
  163. unsigned long _asyncRefCount;
  164. unsigned long _asyncRefTotalCount;
  165. bool need_quit;
  166. std::queue<AsyncStruct *> *_asyncStructQueue;
  167. std::queue<DataInfo *> *_dataQueue;
  168. static std::vector<std::string> _configFileList;
  169. static DataReaderHelper *_dataReaderHelper;
  170. };
  171. }
  172. #endif /*__CCDATAREADERHELPER_H__*/