CCPUScriptTranslator.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. /****************************************************************************
  2. Copyright (C) 2013 Henry van Merode. All rights reserved.
  3. Copyright (c) 2015-2016 Chukong Technologies Inc.
  4. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  5. http://www.cocos2d-x.org
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. ****************************************************************************/
  22. #include "CCPUScriptTranslator.h"
  23. #include "extensions/Particle3D/PU/CCPUTranslateManager.h"
  24. NS_CC_BEGIN
  25. PUScriptTranslator::PUScriptTranslator(void)
  26. {
  27. }
  28. PUScriptTranslator::~PUScriptTranslator(void)
  29. {
  30. }
  31. //-------------------------------------------------------------------------
  32. //-------------------------------------------------------------------------
  33. bool PUScriptTranslator::getFloat(const PUAbstractNode &node, float *result)
  34. {
  35. if(node.type != ANT_ATOM)
  36. return false;
  37. PUAtomAbstractNode *atom = (PUAtomAbstractNode*)(&node);
  38. int n = sscanf(atom->value.c_str(), "%f", result);
  39. if(n == 0 || n == EOF)
  40. return false; // Conversion failed
  41. return true;
  42. }
  43. bool PUScriptTranslator::getInt(const PUAbstractNode &node, int *result)
  44. {
  45. if(node.type != ANT_ATOM)
  46. return false;
  47. PUAtomAbstractNode *atom = (PUAtomAbstractNode*)&node;
  48. int n = sscanf(atom->value.c_str(), "%d", result);
  49. if(n == 0 || n == EOF)
  50. return false; // Conversion failed
  51. return true;
  52. }
  53. bool PUScriptTranslator::getUInt(const PUAbstractNode &node, unsigned int *result)
  54. {
  55. if(node.type != ANT_ATOM)
  56. return false;
  57. PUAtomAbstractNode *atom = (PUAtomAbstractNode*)&node;
  58. int n = sscanf(atom->value.c_str(), "%u", result);
  59. if(n == 0 || n == EOF)
  60. return false; // Conversion failed
  61. return true;
  62. }
  63. bool PUScriptTranslator::getBoolean(const PUAbstractNode &node, bool *result)
  64. {
  65. if(node.type != ANT_ATOM)
  66. return false;
  67. PUAtomAbstractNode *atom = (PUAtomAbstractNode*)&node;
  68. //if(atom->id != 1 && atom->id != 2)
  69. // return false;
  70. *result = atom->value == "true" ? true : false;
  71. return true;
  72. }
  73. bool PUScriptTranslator::getString( const PUAbstractNode &node, std::string *result )
  74. {
  75. if(node.type != ANT_ATOM)
  76. return false;
  77. PUAtomAbstractNode *atom = (PUAtomAbstractNode*)&node;
  78. *result = atom->value;
  79. return true;
  80. }
  81. bool PUScriptTranslator::getVector2(PUAbstractNodeList::const_iterator i,
  82. PUAbstractNodeList::const_iterator end,
  83. Vec2* result,
  84. int maxEntries)
  85. {
  86. int n = 0;
  87. while(i != end && n < maxEntries)
  88. {
  89. float v = 0;
  90. if(getFloat(**i, &v))
  91. {
  92. switch(n)
  93. {
  94. case 0:
  95. result->x = v;
  96. break;
  97. case 1:
  98. result->y = v;
  99. break;
  100. }
  101. }
  102. else
  103. {
  104. return false;
  105. }
  106. ++n;
  107. ++i;
  108. }
  109. // return error if we found less than xy before end, unless constrained
  110. return (n >= 2 || n == maxEntries);
  111. }
  112. //-------------------------------------------------------------------------
  113. bool PUScriptTranslator::getVector3(PUAbstractNodeList::const_iterator i,
  114. PUAbstractNodeList::const_iterator end,
  115. Vec3* result,
  116. int maxEntries)
  117. {
  118. int n = 0;
  119. while(i != end && n < maxEntries)
  120. {
  121. float v = 0;
  122. if(getFloat(**i, &v))
  123. {
  124. switch(n)
  125. {
  126. case 0:
  127. result->x = v;
  128. break;
  129. case 1:
  130. result->y = v;
  131. break;
  132. case 2:
  133. result->z = v;
  134. break;
  135. }
  136. }
  137. else
  138. {
  139. return false;
  140. }
  141. ++n;
  142. ++i;
  143. }
  144. // return error if we found less than xyz before end, unless constrained
  145. return (n >= 3 || n == maxEntries);
  146. }
  147. //-------------------------------------------------------------------------
  148. bool PUScriptTranslator::getVector4(PUAbstractNodeList::const_iterator i,
  149. PUAbstractNodeList::const_iterator end,
  150. Vec4* result,
  151. int maxEntries)
  152. {
  153. int n = 0;
  154. while(i != end && n < maxEntries)
  155. {
  156. float v = 0;
  157. if(getFloat(**i, &v))
  158. {
  159. switch(n)
  160. {
  161. case 0:
  162. result->x = v;
  163. break;
  164. case 1:
  165. result->y = v;
  166. break;
  167. case 2:
  168. result->z = v;
  169. break;
  170. case 3:
  171. result->w = v;
  172. break;
  173. }
  174. }
  175. else
  176. {
  177. return false;
  178. }
  179. ++n;
  180. ++i;
  181. }
  182. // return error if we found less than xyz before end, unless constrained
  183. return (n >= 4 || n == maxEntries);
  184. }
  185. //-------------------------------------------------------------------------
  186. bool PUScriptTranslator::getQuaternion(PUAbstractNodeList::const_iterator i,
  187. PUAbstractNodeList::const_iterator end,
  188. Quaternion* result,
  189. int maxEntries)
  190. {
  191. int n = 0;
  192. while(i != end && n < maxEntries)
  193. {
  194. float v = 0;
  195. if(getFloat(**i, &v))
  196. {
  197. switch(n)
  198. {
  199. case 0:
  200. result->w = v;
  201. break;
  202. case 1:
  203. result->x = v;
  204. break;
  205. case 2:
  206. result->y = v;
  207. break;
  208. case 3:
  209. result->z = v;
  210. break;
  211. }
  212. }
  213. else
  214. {
  215. return false;
  216. }
  217. ++n;
  218. ++i;
  219. }
  220. // return error if we found less than xyz before end, unless constrained
  221. return (n >= 4 || n == maxEntries);
  222. }
  223. //-------------------------------------------------------------------------
  224. bool PUScriptTranslator::passValidateProperty(PUScriptCompiler* compiler,
  225. PUPropertyAbstractNode* prop,
  226. const std::string& token2,
  227. ValidationType validationType)
  228. {
  229. if (!passValidatePropertyNoValues(compiler, prop, token2))
  230. {
  231. return false;
  232. }
  233. bool ret = true;
  234. switch(validationType)
  235. {
  236. case VAL_BOOL:
  237. {
  238. ret = passValidatePropertyNumberOfValues(compiler, prop, token2, 1) && passValidatePropertyValidBool(compiler, prop);
  239. }
  240. break;
  241. case VAL_COLOURVALUE:
  242. {
  243. ret = passValidatePropertyNumberOfValuesRange(compiler, prop, token2, 3, 4);
  244. }
  245. break;
  246. case VAL_INT:
  247. {
  248. ret = passValidatePropertyNumberOfValues(compiler, prop, token2, 1) && passValidatePropertyValidInt(compiler, prop);
  249. }
  250. break;
  251. case VAL_QUATERNION:
  252. {
  253. ret = passValidatePropertyNumberOfValues(compiler, prop, token2, 4) && passValidatePropertyValidQuaternion(compiler, prop);
  254. }
  255. break;
  256. case VAL_REAL:
  257. {
  258. ret = passValidatePropertyNumberOfValues(compiler, prop, token2, 1) && passValidatePropertyValidReal(compiler, prop);
  259. }
  260. break;
  261. case VAL_STRING:
  262. {
  263. ret = passValidatePropertyNumberOfValues(compiler, prop, token2, 1);
  264. }
  265. break;
  266. case VAL_UINT:
  267. {
  268. ret = passValidatePropertyNumberOfValues(compiler, prop, token2, 1) && passValidatePropertyValidUint(compiler, prop);
  269. }
  270. break;
  271. case VAL_VECTOR2:
  272. {
  273. ret = passValidatePropertyNumberOfValues(compiler, prop, token2, 2) && passValidatePropertyValidVector2(compiler, prop);
  274. }
  275. break;
  276. case VAL_VECTOR3:
  277. {
  278. ret = passValidatePropertyNumberOfValues(compiler, prop, token2, 3) && passValidatePropertyValidVector3(compiler, prop);
  279. }
  280. break;
  281. case VAL_VECTOR4:
  282. {
  283. ret = passValidatePropertyNumberOfValues(compiler, prop, token2, 4) && passValidatePropertyValidVector4(compiler, prop);
  284. }
  285. break;
  286. }
  287. return ret;
  288. }
  289. //-------------------------------------------------------------------------
  290. bool PUScriptTranslator::passValidatePropertyNoValues(PUScriptCompiler* /*compiler*/,
  291. PUPropertyAbstractNode* prop,
  292. const std::string& /*token2*/)
  293. {
  294. if(prop->values.empty())
  295. {
  296. // compiler->addError(ScriptCompiler::CE_INVALIDPARAMETERS, prop->file, prop->line, "PU Compiler: No values found for " + token2 + ".");
  297. return false;
  298. }
  299. return true;
  300. }
  301. //-------------------------------------------------------------------------
  302. bool PUScriptTranslator::passValidatePropertyNumberOfValues(PUScriptCompiler* /*compiler*/,
  303. PUPropertyAbstractNode* prop,
  304. const std::string& /*token2*/,
  305. unsigned short numberOfValues)
  306. {
  307. if(prop->values.size() > numberOfValues)
  308. {
  309. return false;
  310. }
  311. return true;
  312. }
  313. //-------------------------------------------------------------------------
  314. bool PUScriptTranslator::passValidatePropertyNumberOfValuesRange(PUScriptCompiler* /*compiler*/,
  315. PUPropertyAbstractNode* prop,
  316. const std::string& /*token2*/,
  317. unsigned short minNumberOfValues,
  318. unsigned short maxNumberOfValues)
  319. {
  320. if(prop->values.size() < minNumberOfValues || prop->values.size() > maxNumberOfValues)
  321. {
  322. return false;
  323. }
  324. return true;
  325. }
  326. //-------------------------------------------------------------------------
  327. bool PUScriptTranslator::passValidatePropertyValidReal(PUScriptCompiler* /*compiler*/,
  328. PUPropertyAbstractNode* prop)
  329. {
  330. float val = 0.0f;
  331. if(getFloat(*(prop->values.front()), &val))
  332. {
  333. return true;
  334. }
  335. // compiler->addError(ScriptCompiler::CE_INVALIDPARAMETERS, prop->file, prop->line,
  336. // "PU Compiler: " + prop->values.front()->getValue() + " is not a valid Real");
  337. return false;
  338. }
  339. //-------------------------------------------------------------------------
  340. bool PUScriptTranslator::passValidatePropertyValidInt(PUScriptCompiler* /*compiler*/,
  341. PUPropertyAbstractNode* prop)
  342. {
  343. int val = 0;
  344. if(getInt(*prop->values.front(), &val))
  345. {
  346. return true;
  347. }
  348. // compiler->addError(ScriptCompiler::CE_INVALIDPARAMETERS, prop->file, prop->line,
  349. // "PU Compiler: " + prop->values.front()->getValue() + " is not a valid int");
  350. return false;
  351. }
  352. //-------------------------------------------------------------------------
  353. bool PUScriptTranslator::passValidatePropertyValidUint(PUScriptCompiler* /*compiler*/,
  354. PUPropertyAbstractNode* prop)
  355. {
  356. unsigned int val = 0;
  357. if(getUInt(*prop->values.front(), &val))
  358. {
  359. return true;
  360. }
  361. // compiler->addError(ScriptCompiler::CE_INVALIDPARAMETERS, prop->file, prop->line,
  362. // "PU Compiler: " + prop->values.front()->getValue() + " is not a valid uint");
  363. return false;
  364. }
  365. //-------------------------------------------------------------------------
  366. bool PUScriptTranslator::passValidatePropertyValidBool(PUScriptCompiler* /*compiler*/,
  367. PUPropertyAbstractNode* prop)
  368. {
  369. bool val;
  370. if(getBoolean(*prop->values.front(), &val))
  371. {
  372. return true;
  373. }
  374. // compiler->addError(ScriptCompiler::CE_INVALIDPARAMETERS, prop->file, prop->line,
  375. // "PU Compiler: " + prop->values.front()->getValue() + " is not a valid bool");
  376. return false;
  377. }
  378. //-------------------------------------------------------------------------
  379. bool PUScriptTranslator::passValidatePropertyValidVector2(PUScriptCompiler* /*compiler*/,
  380. PUPropertyAbstractNode* prop)
  381. {
  382. Vec2 val;
  383. if(getVector2(prop->values.begin(), prop->values.end(), &val))
  384. {
  385. return true;
  386. }
  387. // compiler->addError(ScriptCompiler::CE_INVALIDPARAMETERS, prop->file, prop->line,
  388. // "PU Compiler: " + prop->values.front()->getValue() + " is not a valid Vector2");
  389. return false;
  390. }
  391. //-------------------------------------------------------------------------
  392. bool PUScriptTranslator::passValidatePropertyValidVector3(PUScriptCompiler* /*compiler*/,
  393. PUPropertyAbstractNode* prop)
  394. {
  395. Vec3 val;
  396. if(getVector3(prop->values.begin(), prop->values.end(), &val))
  397. {
  398. return true;
  399. }
  400. // compiler->addError(ScriptCompiler::CE_INVALIDPARAMETERS, prop->file, prop->line,
  401. // "PU Compiler: " + prop->values.front()->getValue() + " is not a valid Vector3");
  402. return false;
  403. }
  404. //-------------------------------------------------------------------------
  405. bool PUScriptTranslator::passValidatePropertyValidVector4(PUScriptCompiler* /*compiler*/,
  406. PUPropertyAbstractNode* prop)
  407. {
  408. Vec4 val;
  409. if(getVector4(prop->values.begin(), prop->values.end(), &val))
  410. {
  411. return true;
  412. }
  413. // compiler->addError(ScriptCompiler::CE_INVALIDPARAMETERS, prop->file, prop->line,
  414. // "PU Compiler: " + prop->values.front()->getValue() + " is not a valid Vector4");
  415. return false;
  416. }
  417. //-------------------------------------------------------------------------
  418. bool PUScriptTranslator::passValidatePropertyValidQuaternion(PUScriptCompiler* /*compiler*/,
  419. PUPropertyAbstractNode* prop)
  420. {
  421. Quaternion val;
  422. if(getQuaternion(prop->values.begin(), prop->values.end(), &val))
  423. {
  424. return true;
  425. }
  426. // compiler->addError(ScriptCompiler::CE_INVALIDPARAMETERS, prop->file, prop->line,
  427. // "PU Compiler: " + prop->values.front()->getValue() + " is not a valid Quaternion");
  428. return false;
  429. }
  430. //-------------------------------------------------------------------------
  431. void PUScriptTranslator::errorUnexpectedToken(PUScriptCompiler* /*compiler*/,
  432. PUAbstractNode* /*token2*/)
  433. {
  434. // CCLOGERROR("PU Compiler: token2 is not recognized tokenFile:%s tokenLine:%s",)
  435. // printf()
  436. // compiler->addError(ScriptCompiler::CE_UNEXPECTEDTOKEN, token.getPointer()->file, token.getPointer()->line,
  437. // );
  438. }
  439. //-------------------------------------------------------------------------
  440. void PUScriptTranslator::errorUnexpectedProperty(PUScriptCompiler* /*compiler*/,
  441. PUPropertyAbstractNode* /*prop*/)
  442. {
  443. // compiler->addError(ScriptCompiler::CE_UNEXPECTEDTOKEN, prop->file, prop->line,
  444. // "PU Compiler: token \"" + prop->name + "\" is not recognized");
  445. }
  446. void PUScriptTranslator::processNode( PUScriptCompiler *compiler, PUAbstractNode *node )
  447. {
  448. if(node->type != ANT_OBJECT)
  449. return;
  450. // Abstract objects are completely skipped
  451. if((reinterpret_cast<PUObjectAbstractNode*>(node))->abstract)
  452. return;
  453. // Retrieve the translator to use
  454. PUScriptTranslator *translator = PUTranslateManager::Instance()->getTranslator(node);
  455. if(translator)
  456. translator->translate(compiler, node);
  457. }
  458. //-------------------------------------------------------------------------
  459. //-------------------------------------------------------------------------
  460. //-------------------------------------------------------------------------
  461. NS_CC_END