CCDataReaderHelper.h
8.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/****************************************************************************
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#ifndef __CCDATAREADERHELPER_H__
#define __CCDATAREADERHELPER_H__
#include "editor-support/cocostudio/CCArmatureDefine.h"
#include "editor-support/cocostudio/CCDatas.h"
#include "editor-support/cocostudio/CCArmature.h"
#include "editor-support/cocostudio/DictionaryHelper.h"
#include "editor-support/cocostudio/CocosStudioExport.h"
#include "json/document-wrapper.h"
#include <string>
#include <queue>
#include <mutex>
#include <thread>
#include <condition_variable>
namespace tinyxml2
{
class XMLElement;
}
namespace cocostudio {
class CocoLoader;
struct stExpCocoNode;
/**
* @js NA
* @lua NA
*/
class CC_STUDIO_DLL DataReaderHelper : cocos2d::Ref
{
protected:
enum ConfigType
{
DragonBone_XML,
CocoStudio_JSON,
CocoStudio_Binary
};
typedef struct _AsyncStruct
{
std::string filename;
std::string fileContent;
ConfigType configType;
std::string baseFilePath;
cocos2d::Ref *target;
cocos2d::SEL_SCHEDULE selector;
bool autoLoadSpriteFile;
std::string imagePath;
std::string plistPath;
} AsyncStruct;
typedef struct _DataInfo
{
AsyncStruct *asyncStruct;
std::queue<std::string> configFileQueue;
float contentScale;
std::string filename;
std::string baseFilePath;
float flashToolVersion;
float cocoStudioVersion;
} DataInfo;
public:
/** @deprecated Use getInstance() instead */
CC_DEPRECATED_ATTRIBUTE static DataReaderHelper *sharedDataReaderHelper() { return DataReaderHelper::getInstance(); }
static DataReaderHelper *getInstance();
/**
* Scale the position data, used for multiresolution adapter
* It won't effect the data already read.
*/
static void setPositionReadScale(float scale);
static float getPositionReadScale();
static void purge();
public:
/**
* @js ctor
*/
DataReaderHelper();
/**
* @js NA
* @lua NA
*/
~DataReaderHelper();
void addDataFromFile(const std::string& filePath);
void addDataFromFileAsync(const std::string& imagePath, const std::string& plistPath, const std::string& filePath, cocos2d::Ref *target, cocos2d::SEL_SCHEDULE selector);
void addDataAsyncCallBack(float dt);
void removeConfigFile(const std::string& configFile);
public:
/**
* Translate XML export from Dragon Bone flash tool to datas, and save them.
* When you add a new xml, the data already saved will be kept.
*
* @param xmlPath The cache of the xml
*/
static void addDataFromCache(const std::string& pFileContent, DataInfo *dataInfo = nullptr);
/**
* Decode Armature Datas from xml export from Dragon Bone flash tool
*/
static ArmatureData *decodeArmature(tinyxml2::XMLElement *armatureXML, DataInfo *dataInfo);
static BoneData *decodeBone(tinyxml2::XMLElement *boneXML, tinyxml2::XMLElement *parentXML, DataInfo *dataInfo);
static DisplayData *decodeBoneDisplay(tinyxml2::XMLElement *displayXML, DataInfo *dataInfo);
/**
* Decode ArmatureAnimation Datas from xml export from Dragon Bone flash tool
*/
static AnimationData *decodeAnimation(tinyxml2::XMLElement *animationXML, DataInfo *dataInfo);
static MovementData *decodeMovement(tinyxml2::XMLElement *movementXML, ArmatureData *armatureData, DataInfo *dataInfo);
static MovementBoneData *decodeMovementBone(tinyxml2::XMLElement *movBoneXml, tinyxml2::XMLElement *parentXml, BoneData *boneData, DataInfo *dataInfo);
static FrameData *decodeFrame(tinyxml2::XMLElement *frameXML, tinyxml2::XMLElement *parentFrameXml, BoneData *boneData, DataInfo *dataInfo);
/**
* Decode Texture Datas from xml export from Dragon Bone flash tool
*/
static TextureData *decodeTexture(tinyxml2::XMLElement *textureXML, DataInfo *dataInfo);
/**
* Decode Contour Datas from xml export from Dragon Bone flash tool
*/
static ContourData *decodeContour(tinyxml2::XMLElement *contourXML, DataInfo *dataInfo);
public:
static void addDataFromJsonCache(const std::string& fileContent, DataInfo *dataInfo = nullptr);
static ArmatureData *decodeArmature(const rapidjson::Value& json, DataInfo *dataInfo);
static BoneData *decodeBone(const rapidjson::Value& json, DataInfo *dataInfo);
static DisplayData *decodeBoneDisplay(const rapidjson::Value& json, DataInfo *dataInfo);
static AnimationData *decodeAnimation(const rapidjson::Value& json, DataInfo *dataInfo);
static MovementData *decodeMovement(const rapidjson::Value& json, DataInfo *dataInfo);
static MovementBoneData *decodeMovementBone(const rapidjson::Value& json, DataInfo *dataInfo);
static FrameData *decodeFrame(const rapidjson::Value& json, DataInfo *dataInfo);
static TextureData *decodeTexture(const rapidjson::Value& json);
static ContourData *decodeContour(const rapidjson::Value& json);
static void decodeNode(BaseData *node, const rapidjson::Value& json, DataInfo *dataInfo);
// for binary decode
public:
static void addDataFromBinaryCache(const char *fileContent, DataInfo *dataInfo = nullptr);
static ArmatureData *decodeArmature(CocoLoader *cocoLoader, stExpCocoNode *pCocoNode, DataInfo *dataInfo);
static BoneData *decodeBone(CocoLoader *cocoLoader, stExpCocoNode *pCocoNode, DataInfo *dataInfo);
static DisplayData *decodeBoneDisplay(CocoLoader *cocoLoader, stExpCocoNode *pCocoNode, DataInfo *dataInfo);
static AnimationData *decodeAnimation(CocoLoader *cocoLoader, stExpCocoNode *pCocoNode, DataInfo *dataInfo);
static MovementData *decodeMovement(CocoLoader *cocoLoader, stExpCocoNode *pCocoNode, DataInfo *dataInfo);
static MovementBoneData *decodeMovementBone(CocoLoader *cocoLoader, stExpCocoNode *pCocoNode, DataInfo *dataInfo);
static FrameData *decodeFrame(CocoLoader *cocoLoader, stExpCocoNode *pCocoNode, DataInfo *dataInfo);
static TextureData *decodeTexture(CocoLoader *cocoLoader, stExpCocoNode *pCocoNode);
static ContourData *decodeContour(CocoLoader *cocoLoader, stExpCocoNode *pCocoNode);
static void decodeNode(BaseData *node, CocoLoader *cocoLoader, stExpCocoNode *pCocoNode, DataInfo *dataInfo);
protected:
void loadData();
std::condition_variable _sleepCondition;
std::thread *_loadingThread;
std::mutex _sleepMutex;
std::mutex _asyncStructQueueMutex;
std::mutex _dataInfoMutex;
std::mutex _addDataMutex;
std::mutex _getFileMutex;
unsigned long _asyncRefCount;
unsigned long _asyncRefTotalCount;
bool need_quit;
std::queue<AsyncStruct *> *_asyncStructQueue;
std::queue<DataInfo *> *_dataQueue;
static std::vector<std::string> _configFileList;
static DataReaderHelper *_dataReaderHelper;
};
}
#endif /*__CCDATAREADERHELPER_H__*/