FlatBuffersSerialize.cpp 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659
  1. /****************************************************************************
  2. Copyright (c) 2013 cocos2d-x.org
  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 "editor-support/cocostudio/FlatBuffersSerialize.h"
  21. #include "base/ObjectFactory.h"
  22. #include "ui/CocosGUI.h"
  23. #include "platform/CCFileUtils.h"
  24. #include "editor-support/cocostudio/CocoStudio.h"
  25. #include "editor-support/cocostudio/CSLanguageDataBinary_generated.h"
  26. #include "editor-support/cocostudio/CSParseBinary_generated.h"
  27. #include "editor-support/cocostudio/WidgetReader/NodeReaderProtocol.h"
  28. #include "editor-support/cocostudio/WidgetReader/NodeReaderDefine.h"
  29. #include "editor-support/cocostudio/WidgetReader/NodeReader/NodeReader.h"
  30. #include "editor-support/cocostudio/WidgetReader/SingleNodeReader/SingleNodeReader.h"
  31. #include "editor-support/cocostudio/WidgetReader/SpriteReader/SpriteReader.h"
  32. #include "editor-support/cocostudio/WidgetReader/ParticleReader/ParticleReader.h"
  33. #include "editor-support/cocostudio/WidgetReader/GameMapReader/GameMapReader.h"
  34. #include "editor-support/cocostudio/WidgetReader/ComAudioReader/ComAudioReader.h"
  35. #include "editor-support/cocostudio/WidgetReader/ProjectNodeReader/ProjectNodeReader.h"
  36. #include "editor-support/cocostudio/WidgetReader/ButtonReader/ButtonReader.h"
  37. #include "editor-support/cocostudio/WidgetReader/CheckBoxReader/CheckBoxReader.h"
  38. #include "editor-support/cocostudio/WidgetReader/ImageViewReader/ImageViewReader.h"
  39. #include "editor-support/cocostudio/WidgetReader/TextBMFontReader/TextBMFontReader.h"
  40. #include "editor-support/cocostudio/WidgetReader/TextReader/TextReader.h"
  41. #include "editor-support/cocostudio/WidgetReader/TextFieldReader/TextFieldReader.h"
  42. #include "editor-support/cocostudio/WidgetReader/TextAtlasReader/TextAtlasReader.h"
  43. #include "editor-support/cocostudio/WidgetReader/LoadingBarReader/LoadingBarReader.h"
  44. #include "editor-support/cocostudio/WidgetReader/SliderReader/SliderReader.h"
  45. #include "editor-support/cocostudio/WidgetReader/LayoutReader/LayoutReader.h"
  46. #include "editor-support/cocostudio/WidgetReader/ScrollViewReader/ScrollViewReader.h"
  47. #include "editor-support/cocostudio/WidgetReader/PageViewReader/PageViewReader.h"
  48. #include "editor-support/cocostudio/WidgetReader/ListViewReader/ListViewReader.h"
  49. #include "tinyxml2.h"
  50. #include "flatbuffers/flatbuffers.h"
  51. #include "flatbuffers/util.h"
  52. USING_NS_CC;
  53. using namespace cocos2d::ui;
  54. using namespace cocostudio;
  55. using namespace cocostudio::timeline;
  56. using namespace flatbuffers;
  57. namespace cocostudio {
  58. static const char* Property_VisibleForFrame = "VisibleForFrame";
  59. static const char* Property_Position = "Position";
  60. static const char* Property_Scale = "Scale";
  61. static const char* Property_RotationSkew = "RotationSkew";
  62. static const char* Property_CColor = "CColor";
  63. static const char* Property_FileData = "FileData";
  64. static const char* Property_FrameEvent = "FrameEvent";
  65. static const char* Property_Alpha = "Alpha";
  66. static const char* Property_AnchorPoint = "AnchorPoint";
  67. static const char* Property_ZOrder = "ZOrder";
  68. static const char* Property_ActionValue = "ActionValue";
  69. static const char* Property_BlendValue = "BlendFunc";
  70. static FlatBuffersSerialize* _instanceFlatBuffersSerialize = nullptr;
  71. FlatBuffersSerialize::FlatBuffersSerialize()
  72. : _isSimulator(false)
  73. , _builder(nullptr)
  74. , _csparsebinary(nullptr)
  75. {
  76. CREATE_CLASS_NODE_READER_INFO(NodeReader);
  77. CREATE_CLASS_NODE_READER_INFO(SingleNodeReader);
  78. CREATE_CLASS_NODE_READER_INFO(SpriteReader);
  79. CREATE_CLASS_NODE_READER_INFO(ParticleReader);
  80. CREATE_CLASS_NODE_READER_INFO(GameMapReader);
  81. CREATE_CLASS_NODE_READER_INFO(ButtonReader);
  82. CREATE_CLASS_NODE_READER_INFO(CheckBoxReader);
  83. CREATE_CLASS_NODE_READER_INFO(ImageViewReader);
  84. CREATE_CLASS_NODE_READER_INFO(TextBMFontReader);
  85. CREATE_CLASS_NODE_READER_INFO(TextReader);
  86. CREATE_CLASS_NODE_READER_INFO(TextFieldReader);
  87. CREATE_CLASS_NODE_READER_INFO(TextAtlasReader);
  88. CREATE_CLASS_NODE_READER_INFO(LoadingBarReader);
  89. CREATE_CLASS_NODE_READER_INFO(SliderReader);
  90. CREATE_CLASS_NODE_READER_INFO(LayoutReader);
  91. CREATE_CLASS_NODE_READER_INFO(ScrollViewReader);
  92. CREATE_CLASS_NODE_READER_INFO(PageViewReader);
  93. CREATE_CLASS_NODE_READER_INFO(ListViewReader);
  94. }
  95. FlatBuffersSerialize::~FlatBuffersSerialize()
  96. {
  97. }
  98. FlatBuffersSerialize* FlatBuffersSerialize::getInstance()
  99. {
  100. if (!_instanceFlatBuffersSerialize)
  101. {
  102. _instanceFlatBuffersSerialize = new (std::nothrow) FlatBuffersSerialize();
  103. }
  104. return _instanceFlatBuffersSerialize;
  105. }
  106. void FlatBuffersSerialize::purge()
  107. {
  108. CC_SAFE_DELETE(_instanceFlatBuffersSerialize);
  109. }
  110. void FlatBuffersSerialize::destroyInstance()
  111. {
  112. CC_SAFE_DELETE(_instanceFlatBuffersSerialize);
  113. }
  114. void FlatBuffersSerialize::deleteFlatBufferBuilder()
  115. {
  116. if (_builder != nullptr)
  117. {
  118. _builder->Clear();
  119. CC_SAFE_DELETE(_builder);
  120. }
  121. }
  122. std::string FlatBuffersSerialize::serializeFlatBuffersWithXMLFile(const std::string &xmlFileName,
  123. const std::string &flatbuffersFileName)
  124. {
  125. std::string inFullpath = FileUtils::getInstance()->fullPathForFilename(xmlFileName);
  126. // xml read
  127. if (!FileUtils::getInstance()->isFileExist(inFullpath))
  128. {
  129. return ".csd file does not exist.";
  130. }
  131. std::string content = FileUtils::getInstance()->getStringFromFile(inFullpath);
  132. // xml parse
  133. tinyxml2::XMLDocument* document = new (std::nothrow) tinyxml2::XMLDocument();
  134. document->Parse(content.c_str());
  135. const tinyxml2::XMLElement* rootElement = document->RootElement();// Root
  136. // CCLOG("rootElement name = %s", rootElement->Name());
  137. const tinyxml2::XMLElement* element = rootElement->FirstChildElement();
  138. bool serializeEnabled = false;
  139. std::string rootType = "";
  140. while (element)
  141. {
  142. // CCLOG("entity name = %s", element->Name());
  143. if (strcmp("PropertyGroup", element->Name()) == 0)
  144. {
  145. const tinyxml2::XMLAttribute* attribute = element->FirstAttribute();
  146. while (attribute && strcmp("Version", attribute->Name()) != 0)
  147. attribute = attribute->Next();
  148. if (attribute)
  149. _csdVersion = attribute->Value();
  150. _csdVersion = "2.1.0.0";
  151. }
  152. if (strcmp("Content", element->Name()) == 0)
  153. {
  154. const tinyxml2::XMLAttribute* attribute = element->FirstAttribute();
  155. //
  156. if (!attribute)
  157. {
  158. serializeEnabled = true;
  159. rootType = "NodeObjectData";
  160. }
  161. //
  162. //
  163. // while (attribute)
  164. // {
  165. // std::string name = attribute->Name();
  166. // std::string value = attribute->Value();
  167. // CCLOG("attribute name = %s, value = %s", name, value);
  168. // if (name == "")
  169. // {
  170. // serializeEnabled = true;
  171. // rootType = (strcmp("", value) == 0) ? "Node" : value;
  172. // }
  173. //
  174. // if (serializeEnabled)
  175. // {
  176. // break;
  177. // }
  178. //
  179. // attribute = attribute->Next();
  180. // }
  181. //
  182. }
  183. if (serializeEnabled)
  184. {
  185. break;
  186. }
  187. const tinyxml2::XMLElement* child = element->FirstChildElement();
  188. if (child)
  189. {
  190. element = child;
  191. }
  192. else
  193. {
  194. element = element->NextSiblingElement();
  195. }
  196. }
  197. if (serializeEnabled)
  198. {
  199. _builder = new (std::nothrow) FlatBufferBuilder();
  200. Offset<NodeTree> nodeTree;
  201. Offset<NodeAction> aciton;
  202. std::vector<Offset<flatbuffers::AnimationInfo>> animationInfos;
  203. const tinyxml2::XMLElement* child = element->FirstChildElement();
  204. while (child)
  205. {
  206. std::string name = child->Name();
  207. if (name == "Animation") // action
  208. {
  209. const tinyxml2::XMLElement* animation = child;
  210. aciton = createNodeAction(animation);
  211. }
  212. else if (name == "ObjectData") // nodeTree
  213. {
  214. const tinyxml2::XMLElement* objectData = child;
  215. auto nameElem = objectData->FirstAttribute();
  216. while (nameElem)
  217. {
  218. if (0 == strcmp("ctype", nameElem->Name()))
  219. {
  220. rootType = nameElem->Value();
  221. break;
  222. }
  223. else
  224. nameElem = nameElem->Next();
  225. }
  226. if (rootType == "GameNodeObjectData" || rootType == "GameLayerObjectData") // for adaptate old version
  227. rootType = "NodeObjectData";
  228. nodeTree = createNodeTree(objectData, rootType);
  229. }
  230. else if (name == "AnimationList") // animation list
  231. {
  232. const tinyxml2::XMLElement* animationinfoElement = child->FirstChildElement();
  233. while (animationinfoElement)
  234. {
  235. auto animationinfo = createAnimationInfo(animationinfoElement);
  236. animationInfos.push_back(animationinfo);
  237. animationinfoElement = animationinfoElement->NextSiblingElement();
  238. }
  239. }
  240. child = child->NextSiblingElement();
  241. }
  242. auto csparsebinary = CreateCSParseBinary(*_builder,
  243. _builder->CreateString(_csdVersion),
  244. _builder->CreateVector(_textures),
  245. _builder->CreateVector(_texturePngs),
  246. nodeTree,
  247. aciton,
  248. _builder->CreateVector(animationInfos));
  249. _builder->Finish(csparsebinary);
  250. _textures.clear();
  251. _texturePngs.clear();
  252. std::string outFullPath = FileUtils::getInstance()->fullPathForFilename(flatbuffersFileName);
  253. size_t pos = outFullPath.find_last_of('.');
  254. std::string convert = outFullPath.substr(0, pos).append(".csb");
  255. auto save = flatbuffers::SaveFile(convert.c_str(),
  256. reinterpret_cast<const char *>(_builder->GetBufferPointer()),
  257. _builder->GetSize(),
  258. true);
  259. if (!save)
  260. {
  261. return "couldn't save files!";
  262. }
  263. deleteFlatBufferBuilder();
  264. }
  265. return "";
  266. }
  267. // NodeTree
  268. Offset<NodeTree> FlatBuffersSerialize::createNodeTree(const tinyxml2::XMLElement *objectData,
  269. std::string classType)
  270. {
  271. std::string classname = classType.substr(0, classType.find("ObjectData"));
  272. // CCLOG("classname = %s", classname.c_str());
  273. std::string name = "";
  274. Offset<Options> options;
  275. std::vector<Offset<NodeTree>> children;
  276. if (classname == "ProjectNode")
  277. {
  278. auto reader = ProjectNodeReader::getInstance();
  279. auto tempOptions = reader->createOptionsWithFlatBuffers(objectData, _builder);
  280. options = CreateOptions(*_builder, *(Offset<WidgetOptions>*)(&tempOptions));
  281. }
  282. else if (classname == "SimpleAudio")
  283. {
  284. auto reader = ComAudioReader::getInstance();
  285. auto tempOptions = reader->createOptionsWithFlatBuffers(objectData, _builder);
  286. options = CreateOptions(*_builder, *(Offset<WidgetOptions>*)(&tempOptions));
  287. }
  288. else
  289. {
  290. std::string readername = getGUIClassName(classname);
  291. readername.append("Reader");
  292. NodeReaderProtocol* reader = dynamic_cast<NodeReaderProtocol*>(ObjectFactory::getInstance()->createObject(readername));
  293. if (reader != nullptr)
  294. {
  295. auto tempOptions = reader->createOptionsWithFlatBuffers(objectData, _builder);
  296. options = CreateOptions(*_builder, *(Offset<WidgetOptions>*)(&tempOptions));
  297. }
  298. }
  299. // children
  300. bool containChildrenElement = false;
  301. const tinyxml2::XMLElement* child = objectData->FirstChildElement();
  302. while (child)
  303. {
  304. // CCLOG("child name = %s", child->Name());
  305. if (strcmp("Children", child->Name()) == 0)
  306. {
  307. containChildrenElement = true;
  308. break;
  309. }
  310. child = child->NextSiblingElement();
  311. }
  312. if (containChildrenElement)
  313. {
  314. child = child->FirstChildElement();
  315. // CCLOG("element name = %s", child->Name());
  316. while (child)
  317. {
  318. const tinyxml2::XMLAttribute* attribute = child->FirstAttribute();
  319. bool bHasType = false;
  320. while (attribute)
  321. {
  322. std::string attriname = attribute->Name();
  323. std::string value = attribute->Value();
  324. if (attriname == "ctype")
  325. {
  326. children.push_back(createNodeTree(child, value));
  327. bHasType = true;
  328. break;
  329. }
  330. attribute = attribute->Next();
  331. }
  332. if(!bHasType)
  333. {
  334. children.push_back(createNodeTree(child, "NodeObjectData"));
  335. }
  336. child = child->NextSiblingElement();
  337. }
  338. }
  339. //
  340. std::string customClassName = "";
  341. const tinyxml2::XMLAttribute* attribute = objectData->FirstAttribute();
  342. while (attribute)
  343. {
  344. std::string attriname = attribute->Name();
  345. std::string value = attribute->Value();
  346. if (attriname == "CustomClassName")
  347. {
  348. customClassName = value;
  349. break;
  350. }
  351. attribute = attribute->Next();
  352. }
  353. return CreateNodeTree(*_builder,
  354. _builder->CreateString(classname),
  355. _builder->CreateVector(children),
  356. options,
  357. _builder->CreateString(customClassName));
  358. }
  359. int FlatBuffersSerialize::getResourceType(std::string key)
  360. {
  361. if(key == "Normal" || key == "Default")
  362. {
  363. return 0;
  364. }
  365. if(_isSimulator)
  366. {
  367. if(key == "MarkedSubImage")
  368. {
  369. return 0;
  370. }
  371. }
  372. return 1;
  373. }
  374. std::string FlatBuffersSerialize::getGUIClassName(const std::string &name)
  375. {
  376. std::string convertedClassName = name;
  377. if (name == "Panel")
  378. {
  379. convertedClassName = "Layout";
  380. }
  381. else if (name == "TextArea")
  382. {
  383. convertedClassName = "Text";
  384. }
  385. else if (name == "TextButton")
  386. {
  387. convertedClassName = "Button";
  388. }
  389. else if (name == "Label")
  390. {
  391. convertedClassName = "Text";
  392. }
  393. else if (name == "LabelAtlas")
  394. {
  395. convertedClassName = "TextAtlas";
  396. }
  397. else if (name == "LabelBMFont")
  398. {
  399. convertedClassName = "TextBMFont";
  400. }
  401. return convertedClassName;
  402. }
  403. std::string FlatBuffersSerialize::getWidgetReaderClassName(Widget* widget)
  404. {
  405. std::string readerName;
  406. // 1st., custom widget parse properties of parent widget with parent widget reader
  407. if (dynamic_cast<Button*>(widget))
  408. {
  409. readerName = "ButtonReader";
  410. }
  411. else if (dynamic_cast<CheckBox*>(widget))
  412. {
  413. readerName = "CheckBoxReader";
  414. }
  415. else if (dynamic_cast<ImageView*>(widget))
  416. {
  417. readerName = "ImageViewReader";
  418. }
  419. else if (dynamic_cast<TextAtlas*>(widget))
  420. {
  421. readerName = "TextAtlasReader";
  422. }
  423. else if (dynamic_cast<TextBMFont*>(widget))
  424. {
  425. readerName = "TextBMFontReader";
  426. }
  427. else if (dynamic_cast<Text*>(widget))
  428. {
  429. readerName = "TextReader";
  430. }
  431. else if (dynamic_cast<LoadingBar*>(widget))
  432. {
  433. readerName = "LoadingBarReader";
  434. }
  435. else if (dynamic_cast<Slider*>(widget))
  436. {
  437. readerName = "SliderReader";
  438. }
  439. else if (dynamic_cast<TextField*>(widget))
  440. {
  441. readerName = "TextFieldReader";
  442. }
  443. else if (dynamic_cast<ListView*>(widget))
  444. {
  445. readerName = "ListViewReader";
  446. }
  447. else if (dynamic_cast<PageView*>(widget))
  448. {
  449. readerName = "PageViewReader";
  450. }
  451. else if (dynamic_cast<ScrollView*>(widget))
  452. {
  453. readerName = "ScrollViewReader";
  454. }
  455. else if (dynamic_cast<Layout*>(widget))
  456. {
  457. readerName = "LayoutReader";
  458. }
  459. else if (dynamic_cast<Widget*>(widget))
  460. {
  461. readerName = "WidgetReader";
  462. }
  463. return readerName;
  464. }
  465. // NodeAction
  466. Offset<NodeAction> FlatBuffersSerialize::createNodeAction(const tinyxml2::XMLElement *objectData)
  467. {
  468. int duration = 0;
  469. float speed = 0.0f;
  470. std::string currentAnimationName = "";
  471. // CCLOG("animation name = %s", objectData->Name());
  472. // ActionTimeline
  473. const tinyxml2::XMLAttribute* attribute = objectData->FirstAttribute();
  474. // attributes
  475. while (attribute)
  476. {
  477. std::string name = attribute->Name();
  478. std::string value = attribute->Value();
  479. if (name == "Duration")
  480. {
  481. duration = atoi(value.c_str());
  482. }
  483. else if (name == "Speed")
  484. {
  485. speed = atof(value.c_str());
  486. }
  487. else if (name == "ActivedAnimationName")
  488. {
  489. currentAnimationName = value.c_str();
  490. }
  491. attribute = attribute->Next();
  492. }
  493. // all Timeline
  494. std::vector<Offset<TimeLine>> timelines;
  495. const tinyxml2::XMLElement* timelineElement = objectData->FirstChildElement();
  496. while (timelineElement)
  497. {
  498. auto timeLine = createTimeLine(timelineElement);
  499. timelines.push_back(timeLine);
  500. timelineElement = timelineElement->NextSiblingElement();
  501. }
  502. return CreateNodeAction(*_builder,
  503. duration,
  504. speed,
  505. _builder->CreateVector(timelines),
  506. _builder->CreateString(currentAnimationName));
  507. }
  508. Offset<flatbuffers::AnimationInfo> FlatBuffersSerialize::createAnimationInfo(const tinyxml2::XMLElement *objectData)
  509. {
  510. std::string infoName = "";
  511. int startIndex = 0;
  512. int endIndex = 0;
  513. const tinyxml2::XMLAttribute* attribute = objectData->FirstAttribute();
  514. while (attribute)
  515. {
  516. std::string attriname = attribute->Name();
  517. std::string attrivalue = attribute->Value();
  518. if (attriname == "Name")
  519. {
  520. infoName = attrivalue;
  521. }
  522. else if (attriname == "StartIndex")
  523. {
  524. startIndex = atoi(attrivalue.c_str());
  525. }
  526. else if (attriname == "EndIndex")
  527. {
  528. endIndex = atoi(attrivalue.c_str());
  529. }
  530. attribute = attribute->Next();
  531. }
  532. return CreateAnimationInfo(*_builder, _builder->CreateString(infoName), startIndex, endIndex);
  533. }
  534. Offset<TimeLine> FlatBuffersSerialize::createTimeLine(const tinyxml2::XMLElement *objectData)
  535. {
  536. int actionTag = 0;
  537. std::string property = "";
  538. // TimelineData attributes
  539. const tinyxml2::XMLAttribute* attribute = objectData->FirstAttribute();
  540. while (attribute)
  541. {
  542. std::string name = attribute->Name();
  543. std::string value = attribute->Value();
  544. if (name == "ActionTag")
  545. {
  546. actionTag = atoi(value.c_str());
  547. }
  548. else if (name == "Property")
  549. {
  550. property = value;
  551. }
  552. attribute = attribute->Next();
  553. }
  554. // all Frame
  555. std::vector<Offset<flatbuffers::Frame>> frames;
  556. const tinyxml2::XMLElement* frameElement = objectData->FirstChildElement();
  557. while (frameElement)
  558. {
  559. Offset<flatbuffers::Frame> frame;
  560. if (property == Property_VisibleForFrame)
  561. {
  562. auto boolFrame = createBoolFrame(frameElement);
  563. frame = CreateFrame(*_builder,
  564. 0, // PointFrame
  565. 0, // ScaleFrame
  566. 0, // ColorFrame
  567. 0, // TextureFrame
  568. 0, // EventFrame
  569. 0, // IntFrame
  570. boolFrame);
  571. }
  572. else if (property == Property_Position)
  573. {
  574. auto pointFrame = createPointFrame(frameElement);
  575. frame = CreateFrame(*_builder,
  576. pointFrame);
  577. }
  578. else if (property == Property_Scale)
  579. {
  580. auto scaleFrame = createScaleFrame(frameElement);
  581. frame = CreateFrame(*_builder,
  582. 0, // PointFrame
  583. scaleFrame);
  584. }
  585. else if (property == Property_RotationSkew)
  586. {
  587. auto scaleFrame = createScaleFrame(frameElement);
  588. frame = CreateFrame(*_builder,
  589. 0, // PointFrame
  590. scaleFrame);
  591. }
  592. else if (property == Property_CColor)
  593. {
  594. auto colorFrame = createColorFrame(frameElement);
  595. frame = CreateFrame(*_builder,
  596. 0, // PointFrame
  597. 0, // ScaleFrame
  598. colorFrame);
  599. }
  600. else if (property == Property_FileData)
  601. {
  602. auto textureFrame = createTextureFrame(frameElement);
  603. frame = CreateFrame(*_builder,
  604. 0, // PointFrame
  605. 0, // ScaleFrame
  606. 0, // ColorFrame
  607. textureFrame);
  608. }
  609. else if (property == Property_FrameEvent)
  610. {
  611. auto eventFrame = createEventFrame(frameElement);
  612. frame = CreateFrame(*_builder,
  613. 0, // PointFrame
  614. 0, // ScaleFrame
  615. 0, // ColorFrame
  616. 0, // TextureFrame
  617. eventFrame);
  618. }
  619. else if (property == Property_Alpha)
  620. {
  621. auto intFrame = createIntFrame(frameElement);
  622. frame = CreateFrame(*_builder,
  623. 0, // PointFrame
  624. 0, // ScaleFrame
  625. 0, // ColorFrame
  626. 0, // TextureFrame
  627. 0, // EventFrame
  628. intFrame);
  629. }
  630. else if (property == Property_AnchorPoint)
  631. {
  632. auto scaleFrame = createScaleFrame(frameElement);
  633. frame = CreateFrame(*_builder,
  634. 0, // PointFrame
  635. scaleFrame);
  636. }
  637. else if (property == Property_ZOrder)
  638. {
  639. auto intFrame = createIntFrame(frameElement);
  640. frame = CreateFrame(*_builder,
  641. 0, // PointFrame
  642. 0, // ScaleFrame
  643. 0, // ColorFrame
  644. 0, // TextureFrame
  645. 0, // EventFrame
  646. intFrame);
  647. }
  648. else if (property == Property_ActionValue)
  649. {
  650. auto innerActionFrame = createInnerActionFrame(frameElement);
  651. frame = CreateFrame(*_builder,
  652. 0, // PointFrame
  653. 0, // ScaleFrame
  654. 0, // ColorFrame
  655. 0, // TextureFrame
  656. 0, // EventFrame
  657. 0, // IntFrame
  658. 0, // BoolFrame
  659. innerActionFrame);
  660. }
  661. else if (property == Property_BlendValue)
  662. {
  663. auto blendFrame = createBlendFrame(frameElement);
  664. frame = CreateFrame(*_builder,
  665. 0, // PointFrame
  666. 0, // ScaleFrame
  667. 0, // ColorFrame
  668. 0, // TextureFrame
  669. 0, // EventFrame
  670. 0, // IntFrame
  671. 0, // BoolFrame
  672. 0, //InnerActionFrame
  673. blendFrame);
  674. }
  675. frames.push_back(frame);
  676. frameElement = frameElement->NextSiblingElement();
  677. }
  678. return CreateTimeLine(*_builder,
  679. _builder->CreateString(property),
  680. actionTag,
  681. _builder->CreateVector(frames));
  682. }
  683. Offset<flatbuffers::PointFrame> FlatBuffersSerialize::createPointFrame(const tinyxml2::XMLElement *objectData)
  684. {
  685. int frameIndex = 0;
  686. bool tween = true;
  687. Vec2 position;
  688. const tinyxml2::XMLAttribute* attribute = objectData->FirstAttribute();
  689. while (attribute)
  690. {
  691. std::string name = attribute->Name();
  692. std::string value = attribute->Value();
  693. if (name == "X")
  694. {
  695. position.x = atof(value.c_str());
  696. }
  697. else if (name == "Y")
  698. {
  699. position.y = atof(value.c_str());
  700. }
  701. else if (name == "FrameIndex")
  702. {
  703. frameIndex = atoi(value.c_str());
  704. }
  705. else if (name == "Tween")
  706. {
  707. tween = (value == "True") ? true : false;
  708. }
  709. attribute = attribute->Next();
  710. }
  711. Position f_position(position.x, position.y);
  712. return CreatePointFrame(*_builder,
  713. frameIndex,
  714. tween,
  715. &f_position,
  716. createEasingData(objectData->FirstChildElement()));
  717. }
  718. Offset<flatbuffers::ScaleFrame> FlatBuffersSerialize::createScaleFrame(const tinyxml2::XMLElement *objectData)
  719. {
  720. int frameIndex = 0;
  721. bool tween = true;
  722. Vec2 scale;
  723. const tinyxml2::XMLAttribute* attribute = objectData->FirstAttribute();
  724. while (attribute)
  725. {
  726. std::string name = attribute->Name();
  727. std::string value = attribute->Value();
  728. if (name == "X")
  729. {
  730. scale.x = atof(value.c_str());
  731. }
  732. else if (name == "Y")
  733. {
  734. scale.y = atof(value.c_str());
  735. }
  736. else if (name == "FrameIndex")
  737. {
  738. frameIndex = atoi(value.c_str());
  739. }
  740. else if (name == "Tween")
  741. {
  742. tween = (value == "True") ? true : false;
  743. }
  744. attribute = attribute->Next();
  745. }
  746. Scale f_scale(scale.x, scale.y);
  747. return CreateScaleFrame(*_builder,
  748. frameIndex,
  749. tween,
  750. &f_scale,
  751. createEasingData(objectData->FirstChildElement()));
  752. }
  753. Offset<flatbuffers::ColorFrame> FlatBuffersSerialize::createColorFrame(const tinyxml2::XMLElement *objectData)
  754. {
  755. int frameIndex = 0;
  756. bool tween = true;
  757. Color3B color;
  758. const tinyxml2::XMLAttribute* attribute = objectData->FirstAttribute();
  759. while (attribute)
  760. {
  761. std::string name = attribute->Name();
  762. std::string value = attribute->Value();
  763. if (name == "FrameIndex")
  764. {
  765. frameIndex = atoi(value.c_str());
  766. }
  767. else if (name == "Tween")
  768. {
  769. tween = (value == "True") ? true : false;
  770. }
  771. attribute = attribute->Next();
  772. }
  773. // color
  774. const tinyxml2::XMLElement* child = objectData->FirstChildElement();
  775. while (child)
  776. {
  777. attribute = child->FirstAttribute();
  778. while (attribute)
  779. {
  780. std::string name = attribute->Name();
  781. std::string value = attribute->Value();
  782. if (name == "R")
  783. {
  784. color.r = atoi(value.c_str());
  785. }
  786. else if (name == "G")
  787. {
  788. color.g = atoi(value.c_str());
  789. }
  790. else if (name == "B")
  791. {
  792. color.b = atoi(value.c_str());
  793. }
  794. attribute = attribute->Next();
  795. }
  796. child = child->NextSiblingElement();
  797. }
  798. Color f_color(255, color.r, color.g, color.b);
  799. return CreateColorFrame(*_builder,
  800. frameIndex,
  801. tween,
  802. &f_color,
  803. createEasingData(objectData->FirstChildElement()));
  804. }
  805. Offset<flatbuffers::TextureFrame> FlatBuffersSerialize::createTextureFrame(const tinyxml2::XMLElement *objectData)
  806. {
  807. int frameIndex = 0;
  808. bool tween = true;
  809. std::string path = "";
  810. std::string plistFile = "";
  811. int resourceType = 0;
  812. std::string texture = "";
  813. std::string texturePng = "";
  814. const tinyxml2::XMLAttribute* attribute = objectData->FirstAttribute();
  815. while (attribute)
  816. {
  817. std::string attriname = attribute->Name();
  818. std::string value = attribute->Value();
  819. if (attriname == "FrameIndex")
  820. {
  821. frameIndex = atoi(value.c_str());
  822. }
  823. else if (attriname == "Tween")
  824. {
  825. tween = (value == "True") ? true : false;
  826. }
  827. attribute = attribute->Next();
  828. }
  829. const tinyxml2::XMLElement* child = objectData->FirstChildElement();
  830. while (child)
  831. {
  832. attribute = child->FirstAttribute();
  833. while (attribute)
  834. {
  835. std::string attriname = attribute->Name();
  836. std::string value = attribute->Value();
  837. if (attriname == "Path")
  838. {
  839. path = value;
  840. }
  841. else if (attriname == "Type")
  842. {
  843. resourceType = getResourceType(value);
  844. }
  845. else if (attriname == "Plist")
  846. {
  847. plistFile = value;
  848. texture = value;
  849. }
  850. attribute = attribute->Next();
  851. }
  852. if (resourceType == 1)
  853. {
  854. _textures.push_back(_builder->CreateString(texture));
  855. }
  856. child = child->NextSiblingElement();
  857. }
  858. return CreateTextureFrame(*_builder,
  859. frameIndex,
  860. tween,
  861. CreateResourceData(*_builder,
  862. _builder->CreateString(path),
  863. _builder->CreateString(plistFile),
  864. resourceType),
  865. createEasingData(objectData->FirstChildElement()));
  866. }
  867. Offset<flatbuffers::EventFrame> FlatBuffersSerialize::createEventFrame(const tinyxml2::XMLElement *objectData)
  868. {
  869. int frameIndex = 0;
  870. bool tween = true;
  871. std::string value = "";
  872. const tinyxml2::XMLAttribute* attribute = objectData->FirstAttribute();
  873. while (attribute)
  874. {
  875. std::string name = attribute->Name();
  876. std::string attrivalue = attribute->Value();
  877. if (name == "Value") // to be gonna modify
  878. {
  879. value = attrivalue;
  880. }
  881. else if (name == "FrameIndex")
  882. {
  883. frameIndex = atoi(attrivalue.c_str());
  884. }
  885. else if (name == "Tween")
  886. {
  887. tween = (attrivalue == "True") ? true : false;
  888. }
  889. attribute = attribute->Next();
  890. }
  891. return CreateEventFrame(*_builder,
  892. frameIndex,
  893. tween,
  894. _builder->CreateString(value),
  895. createEasingData(objectData->FirstChildElement()));
  896. }
  897. Offset<flatbuffers::IntFrame> FlatBuffersSerialize::createIntFrame(const tinyxml2::XMLElement *objectData)
  898. {
  899. int frameIndex = 0;
  900. bool tween = true;
  901. int value = 0;
  902. const tinyxml2::XMLAttribute* attribute = objectData->FirstAttribute();
  903. while (attribute)
  904. {
  905. std::string name = attribute->Name();
  906. std::string attrivalue = attribute->Value();
  907. if (name == "Value") // to be gonna modify
  908. {
  909. value = atoi(attrivalue.c_str());
  910. }
  911. else if (name == "FrameIndex")
  912. {
  913. frameIndex = atoi(attrivalue.c_str());
  914. }
  915. else if (name == "Tween")
  916. {
  917. tween = (attrivalue == "True") ? true : false;
  918. }
  919. attribute = attribute->Next();
  920. }
  921. return CreateIntFrame(*_builder,
  922. frameIndex,
  923. tween,
  924. value,
  925. createEasingData(objectData->FirstChildElement()));
  926. }
  927. Offset<flatbuffers::BoolFrame> FlatBuffersSerialize::createBoolFrame(const tinyxml2::XMLElement *objectData)
  928. {
  929. int frameIndex = 0;
  930. bool tween = true;
  931. bool value = true;
  932. const tinyxml2::XMLAttribute* attribute = objectData->FirstAttribute();
  933. while (attribute)
  934. {
  935. std::string name = attribute->Name();
  936. std::string attrivalue = attribute->Value();
  937. if (name == "Value")
  938. {
  939. value = (attrivalue == "True") ? true : false;
  940. }
  941. else if (name == "FrameIndex")
  942. {
  943. frameIndex = atoi(attrivalue.c_str());
  944. }
  945. else if (name == "Tween")
  946. {
  947. tween = (attrivalue == "True") ? true : false;
  948. }
  949. attribute = attribute->Next();
  950. }
  951. return CreateBoolFrame(*_builder,
  952. frameIndex,
  953. tween,
  954. value,
  955. createEasingData(objectData->FirstChildElement()));
  956. }
  957. Offset<flatbuffers::InnerActionFrame> FlatBuffersSerialize::createInnerActionFrame(const tinyxml2::XMLElement *objectData)
  958. {
  959. int frameIndex = 0;
  960. bool tween = true;
  961. int innerActionType = 0;
  962. std::string currentAniamtionName = "";
  963. int singleFrameIndex = 0;
  964. const tinyxml2::XMLAttribute* attribute = objectData->FirstAttribute();
  965. while (attribute)
  966. {
  967. std::string name = attribute->Name();
  968. std::string attrivalue = attribute->Value();
  969. if (name == "InnerActionType")
  970. {
  971. if (attrivalue == "LoopAction")
  972. {
  973. innerActionType = 0;
  974. }
  975. else if (attrivalue == "NoLoopAction")
  976. {
  977. innerActionType = 1;
  978. }
  979. else if (attrivalue == "SingleFrame")
  980. {
  981. innerActionType = 2;
  982. }
  983. }
  984. else if (name == "CurrentAniamtionName")
  985. {
  986. currentAniamtionName = attrivalue;
  987. }
  988. else if (name == "SingleFrameIndex")
  989. {
  990. singleFrameIndex = atoi(attrivalue.c_str());
  991. }
  992. else if (name == "FrameIndex")
  993. {
  994. frameIndex = atoi(attrivalue.c_str());
  995. }
  996. else if (name == "Tween")
  997. {
  998. tween = (attrivalue == "True") ? true : false;
  999. }
  1000. attribute = attribute->Next();
  1001. }
  1002. return CreateInnerActionFrame(*_builder,
  1003. frameIndex,
  1004. tween,
  1005. innerActionType,
  1006. _builder->CreateString(currentAniamtionName),
  1007. singleFrameIndex,
  1008. createEasingData(objectData->FirstChildElement()));
  1009. }
  1010. flatbuffers::Offset<flatbuffers::BlendFrame> FlatBuffersSerialize::createBlendFrame(const tinyxml2::XMLElement* objectData)
  1011. {
  1012. int frameIndex = 0;
  1013. bool tween = true;
  1014. int32_t src = GL_ONE, dst = GL_ONE_MINUS_SRC_ALPHA;
  1015. std::string name = "";
  1016. std::string value = "";
  1017. const tinyxml2::XMLAttribute* attribute = objectData->FirstAttribute();
  1018. while (attribute)
  1019. {
  1020. name = attribute->Name();
  1021. value = attribute->Value();
  1022. if (name == "FrameIndex")
  1023. {
  1024. frameIndex = atoi(value.c_str());
  1025. }
  1026. else if (name == "Tween")
  1027. {
  1028. tween = (value == "True") ? true : false;
  1029. }
  1030. else if (name == "Src")
  1031. {
  1032. src = atoi(value.c_str());
  1033. }
  1034. else if (name == "Dst")
  1035. {
  1036. dst = atoi(value.c_str());
  1037. }
  1038. attribute = attribute->Next();
  1039. }
  1040. flatbuffers::Offset<flatbuffers::EasingData> easingData;
  1041. flatbuffers::BlendFunc blendFunc(src, dst);
  1042. return CreateBlendFrame(*_builder,
  1043. frameIndex,
  1044. tween,
  1045. &blendFunc,
  1046. easingData);
  1047. }
  1048. flatbuffers::Offset<flatbuffers::EasingData> FlatBuffersSerialize::createEasingData(const tinyxml2::XMLElement *objectData)
  1049. {
  1050. if (!objectData)
  1051. {
  1052. return 0;
  1053. }
  1054. int type = -1;
  1055. std::vector<flatbuffers::Position> points;
  1056. const tinyxml2::XMLAttribute* attribute = objectData->FirstAttribute();
  1057. while (attribute)
  1058. {
  1059. std::string name = attribute->Name();
  1060. std::string value = attribute->Value();
  1061. if (name == "Type")
  1062. {
  1063. type = atoi(value.c_str());
  1064. break;
  1065. }
  1066. attribute = attribute->Next();
  1067. }
  1068. const tinyxml2::XMLElement* Points = objectData->FirstChildElement();
  1069. if (Points)
  1070. {
  1071. const tinyxml2::XMLElement* PointF = Points->FirstChildElement();
  1072. while (PointF)
  1073. {
  1074. Vec2 pointF;
  1075. attribute = PointF->FirstAttribute();
  1076. while (attribute)
  1077. {
  1078. std::string name = attribute->Name();
  1079. std::string value = attribute->Value();
  1080. if (name == "X")
  1081. {
  1082. pointF.x = atof(value.c_str());
  1083. }
  1084. else if (name == "Y")
  1085. {
  1086. pointF.y = atof(value.c_str());
  1087. }
  1088. attribute = attribute->Next();
  1089. }
  1090. flatbuffers::Position f_PointF(pointF.x, pointF.y);
  1091. points.push_back(f_PointF);
  1092. PointF = PointF->NextSiblingElement();
  1093. }
  1094. }
  1095. return CreateEasingData(*_builder,
  1096. type,
  1097. _builder->CreateVectorOfStructs(points));
  1098. }
  1099. /* create flat buffers with XML */
  1100. FlatBufferBuilder* FlatBuffersSerialize::createFlatBuffersWithXMLFileForSimulator(const std::string &xmlFileName)
  1101. {
  1102. std::string inFullpath = FileUtils::getInstance()->fullPathForFilename(xmlFileName);
  1103. // xml read
  1104. if (!FileUtils::getInstance()->isFileExist(inFullpath))
  1105. {
  1106. // CCLOG(".csd file does not exist.");
  1107. }
  1108. std::string content = FileUtils::getInstance()->getStringFromFile(inFullpath);
  1109. // xml parse
  1110. tinyxml2::XMLDocument* document = new (std::nothrow) tinyxml2::XMLDocument();
  1111. document->Parse(content.c_str());
  1112. const tinyxml2::XMLElement* rootElement = document->RootElement();// Root
  1113. // CCLOG("rootElement name = %s", rootElement->Name());
  1114. const tinyxml2::XMLElement* element = rootElement->FirstChildElement();
  1115. bool serializeEnabled = false;
  1116. std::string rootType = "";
  1117. while (element)
  1118. {
  1119. // CCLOG("entity name = %s", element->Name());
  1120. if (strcmp("PropertyGroup", element->Name()) == 0)
  1121. {
  1122. const tinyxml2::XMLAttribute* attribute = element->FirstAttribute();
  1123. while (attribute && strcmp("Version", attribute->Name()) != 0)
  1124. attribute = attribute->Next();
  1125. if (attribute)
  1126. _csdVersion = attribute->Value();
  1127. }
  1128. if (strcmp("Content", element->Name()) == 0)
  1129. {
  1130. const tinyxml2::XMLAttribute* attribute = element->FirstAttribute();
  1131. //
  1132. if (!attribute)
  1133. {
  1134. serializeEnabled = true;
  1135. rootType = "NodeObjectData";
  1136. }
  1137. }
  1138. if (serializeEnabled)
  1139. {
  1140. break;
  1141. }
  1142. const tinyxml2::XMLElement* child = element->FirstChildElement();
  1143. if (child)
  1144. {
  1145. element = child;
  1146. }
  1147. else
  1148. {
  1149. element = element->NextSiblingElement();
  1150. }
  1151. }
  1152. if (serializeEnabled)
  1153. {
  1154. _builder = new (std::nothrow) FlatBufferBuilder();
  1155. Offset<NodeTree> nodeTree;
  1156. Offset<NodeAction> aciton;
  1157. std::vector<Offset<flatbuffers::AnimationInfo> > animationInfos;
  1158. const tinyxml2::XMLElement* child = element->FirstChildElement();
  1159. while (child)
  1160. {
  1161. std::string name = child->Name();
  1162. if (name == "Animation") // action
  1163. {
  1164. const tinyxml2::XMLElement* animation = child;
  1165. aciton = createNodeAction(animation);
  1166. }
  1167. else if (name == "ObjectData") // nodeTree
  1168. {
  1169. const tinyxml2::XMLElement* objectData = child;
  1170. auto nameElem = objectData->FirstAttribute();
  1171. while (nameElem)
  1172. {
  1173. if (0 == strcmp("ctype", nameElem->Name()))
  1174. {
  1175. rootType = nameElem->Value();
  1176. break;
  1177. }
  1178. else
  1179. nameElem = nameElem->Next();
  1180. }
  1181. if (rootType == "GameNodeObjectData" || rootType == "GameLayerObjectData") // for adaptate old version
  1182. rootType = "NodeObjectData";
  1183. nodeTree = createNodeTreeForSimulator(objectData, rootType);
  1184. }
  1185. else if (name == "AnimationList") // animation list
  1186. {
  1187. const tinyxml2::XMLElement* animationinfoElement = child->FirstChildElement();
  1188. while (animationinfoElement)
  1189. {
  1190. auto animationinfo = createAnimationInfo(animationinfoElement);
  1191. animationInfos.push_back(animationinfo);
  1192. animationinfoElement = animationinfoElement->NextSiblingElement();
  1193. }
  1194. }
  1195. child = child->NextSiblingElement();
  1196. }
  1197. auto csparsebinary = CreateCSParseBinary(*_builder,
  1198. _builder->CreateString(_csdVersion),
  1199. _builder->CreateVector(_textures),
  1200. _builder->CreateVector(_texturePngs),
  1201. nodeTree,
  1202. aciton,
  1203. _builder->CreateVector(animationInfos));
  1204. _builder->Finish(csparsebinary);
  1205. _textures.clear();
  1206. _texturePngs.clear();
  1207. }
  1208. return _builder;
  1209. }
  1210. Offset<NodeTree> FlatBuffersSerialize::createNodeTreeForSimulator(const tinyxml2::XMLElement *objectData,
  1211. std::string classType)
  1212. {
  1213. std::string classname = classType.substr(0, classType.find("ObjectData"));
  1214. // CCLOG("classname = %s", classname.c_str());
  1215. std::string name = "";
  1216. Offset<Options> options;
  1217. std::vector<Offset<NodeTree>> children;
  1218. if (classname == "ProjectNode")
  1219. {
  1220. auto projectNodeOptions = createProjectNodeOptionsForSimulator(objectData);
  1221. options = CreateOptions(*_builder, *(Offset<WidgetOptions>*)(&projectNodeOptions));
  1222. }
  1223. else if (classname == "SimpleAudio")
  1224. {
  1225. auto reader = ComAudioReader::getInstance();
  1226. auto tempOptions = reader->createOptionsWithFlatBuffers(objectData, _builder);
  1227. options = CreateOptions(*_builder, *(Offset<WidgetOptions>*)(&tempOptions));
  1228. }
  1229. else
  1230. {
  1231. std::string readername = getGUIClassName(classname);
  1232. readername.append("Reader");
  1233. NodeReaderProtocol* reader = dynamic_cast<NodeReaderProtocol*>(ObjectFactory::getInstance()->createObject(readername));
  1234. if (reader != nullptr)
  1235. {
  1236. auto tempOptions = reader->createOptionsWithFlatBuffers(objectData, _builder);
  1237. options = CreateOptions(*_builder, *(Offset<WidgetOptions>*)(&tempOptions));
  1238. }
  1239. }
  1240. // children
  1241. bool containChildrenElement = false;
  1242. const tinyxml2::XMLElement* child = objectData->FirstChildElement();
  1243. while (child)
  1244. {
  1245. // CCLOG("child name = %s", child->Name());
  1246. if (strcmp("Children", child->Name()) == 0)
  1247. {
  1248. containChildrenElement = true;
  1249. break;
  1250. }
  1251. child = child->NextSiblingElement();
  1252. }
  1253. if (containChildrenElement)
  1254. {
  1255. child = child->FirstChildElement();
  1256. // CCLOG("element name = %s", child->Name());
  1257. while (child)
  1258. {
  1259. const tinyxml2::XMLAttribute* attribute = child->FirstAttribute();
  1260. bool bHasType = false;
  1261. while (attribute)
  1262. {
  1263. std::string attriname = attribute->Name();
  1264. std::string value = attribute->Value();
  1265. if (attriname == "ctype")
  1266. {
  1267. children.push_back(createNodeTreeForSimulator(child, value));
  1268. bHasType = true;
  1269. break;
  1270. }
  1271. attribute = attribute->Next();
  1272. }
  1273. if(!bHasType)
  1274. {
  1275. children.push_back(createNodeTreeForSimulator(child, "NodeObjectData"));
  1276. }
  1277. child = child->NextSiblingElement();
  1278. }
  1279. }
  1280. //
  1281. std::string customClassName = "";
  1282. const tinyxml2::XMLAttribute* attribute = objectData->FirstAttribute();
  1283. while (attribute)
  1284. {
  1285. std::string attriname = attribute->Name();
  1286. std::string value = attribute->Value();
  1287. if (attriname == "CustomClassName")
  1288. {
  1289. customClassName = value;
  1290. break;
  1291. }
  1292. attribute = attribute->Next();
  1293. }
  1294. return CreateNodeTree(*_builder,
  1295. _builder->CreateString(classname),
  1296. _builder->CreateVector(children),
  1297. options,
  1298. _builder->CreateString(customClassName));
  1299. }
  1300. Offset<ProjectNodeOptions> FlatBuffersSerialize::createProjectNodeOptionsForSimulator(const tinyxml2::XMLElement *objectData)
  1301. {
  1302. auto temp = NodeReader::getInstance()->createOptionsWithFlatBuffers(objectData, _builder);
  1303. auto nodeOptions = *(Offset<WidgetOptions>*)(&temp);
  1304. std::string filename = "";
  1305. float innerspeed = 1.0f;
  1306. const tinyxml2::XMLAttribute* objattri = objectData->FirstAttribute();
  1307. // inneraction speed
  1308. while (objattri)
  1309. {
  1310. std::string name = objattri->Name();
  1311. std::string value = objattri->Value();
  1312. if (name == "InnerActionSpeed")
  1313. {
  1314. innerspeed = atof(objattri->Value());
  1315. break;
  1316. }
  1317. objattri = objattri->Next();
  1318. }
  1319. // FileData
  1320. const tinyxml2::XMLElement* child = objectData->FirstChildElement();
  1321. while (child)
  1322. {
  1323. std::string name = child->Name();
  1324. if (name == "FileData")
  1325. {
  1326. const tinyxml2::XMLAttribute* attribute = child->FirstAttribute();
  1327. while (attribute)
  1328. {
  1329. name = attribute->Name();
  1330. std::string value = attribute->Value();
  1331. if (name == "Path")
  1332. {
  1333. filename = value;
  1334. }
  1335. attribute = attribute->Next();
  1336. }
  1337. }
  1338. child = child->NextSiblingElement();
  1339. }
  1340. return CreateProjectNodeOptions(*_builder,
  1341. nodeOptions,
  1342. _builder->CreateString(filename),
  1343. innerspeed);
  1344. }
  1345. /* Serialize language XML file to Flat Buffers file. */
  1346. std::string FlatBuffersSerialize::serializeFlatBuffersWithXMLFileForLanguageData(const std::string& xmlFilePath,
  1347. const std::string& flatBuffersFilePath,
  1348. const std::string& languageName)
  1349. {
  1350. //Read and parse XML data file.
  1351. if (!FileUtils::getInstance()->isFileExist(xmlFilePath))
  1352. return "Language XML file does not exist.";
  1353. std::string content = FileUtils::getInstance()->getStringFromFile(xmlFilePath);
  1354. tinyxml2::XMLDocument* document = new (std::nothrow) tinyxml2::XMLDocument();
  1355. document->Parse(content.c_str());
  1356. const tinyxml2::XMLElement* element = document->RootElement();
  1357. element = element->FirstChildElement();
  1358. //Create FlatBuffers file using the language data in XML file.
  1359. _builder = new (std::nothrow) FlatBufferBuilder();
  1360. std::vector<Offset<LanguageItem>> langItemList;
  1361. while (element)
  1362. {
  1363. if (strcmp("language", element->Name()) != 0)
  1364. {
  1365. element = element->NextSiblingElement();
  1366. continue;
  1367. }
  1368. //Read all of the Key-Values in the XML file.
  1369. std::string key = "";
  1370. std::string text = "";
  1371. bool hasKeyReaded = false;
  1372. bool hasTextReaded = false;
  1373. const tinyxml2::XMLElement* childElement = element->FirstChildElement();
  1374. while (childElement)
  1375. {
  1376. //Record language key.
  1377. if (strcmp("key", childElement->Name()) == 0)
  1378. {
  1379. key = childElement->GetText();
  1380. hasKeyReaded = true;
  1381. }
  1382. //Record corresponding text.
  1383. else if (strcmp(languageName.c_str(), childElement->Name()) == 0)
  1384. {
  1385. const char* langText = childElement->GetText();
  1386. if (langText && langText[0] != '\0')
  1387. text = langText;
  1388. else
  1389. text = key;
  1390. hasTextReaded = true;
  1391. }
  1392. if (hasKeyReaded && hasTextReaded)
  1393. break;
  1394. childElement = childElement->NextSiblingElement();
  1395. }
  1396. Offset<flatbuffers::LanguageItem> langItem = CreateLanguageItem(*_builder, _builder->CreateString(key), _builder->CreateString(text));
  1397. langItemList.push_back(langItem);
  1398. element = element->NextSiblingElement();
  1399. }
  1400. auto langSet = CreateLanguageSet(*_builder, _builder->CreateVector(langItemList));
  1401. _builder->Finish(langSet);
  1402. bool isSuccess = flatbuffers::SaveFile(flatBuffersFilePath.c_str(),
  1403. reinterpret_cast<const char *>(_builder->GetBufferPointer()),
  1404. _builder->GetSize(),
  1405. true);
  1406. if (isSuccess)
  1407. return "";
  1408. else
  1409. return "Failed to save language .csb file.";
  1410. }
  1411. }
  1412. /**/