MD5Parser.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (ASSIMP)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2008, ASSIMP Development Team
  6. All rights reserved.
  7. Redistribution and use of this software in source and binary forms,
  8. with or without modification, are permitted provided that the following
  9. conditions are met:
  10. * Redistributions of source code must retain the above
  11. copyright notice, this list of conditions and the
  12. following disclaimer.
  13. * Redistributions in binary form must reproduce the above
  14. copyright notice, this list of conditions and the
  15. following disclaimer in the documentation and/or other
  16. materials provided with the distribution.
  17. * Neither the name of the ASSIMP team, nor the names of its
  18. contributors may be used to endorse or promote products
  19. derived from this software without specific prior
  20. written permission of the ASSIMP Development Team.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. ---------------------------------------------------------------------------
  33. */
  34. /** @file Implementation of the MD5 parser class */
  35. #include "AssimpPCH.h"
  36. // internal headers
  37. #include "MD5Loader.h"
  38. #include "MaterialSystem.h"
  39. #include "fast_atof.h"
  40. #include "ParsingUtils.h"
  41. #include "StringComparison.h"
  42. using namespace Assimp;
  43. using namespace Assimp::MD5;
  44. #if _MSC_VER >= 1400
  45. # define sprintf sprintf_s
  46. #endif
  47. // ------------------------------------------------------------------------------------------------
  48. MD5Parser::MD5Parser(char* buffer, unsigned int fileSize)
  49. {
  50. ai_assert(NULL != buffer && 0 != fileSize);
  51. this->buffer = buffer;
  52. this->fileSize = fileSize;
  53. this->lineNumber = 0;
  54. DefaultLogger::get()->debug("MD5Parser begin");
  55. // parse the file header
  56. this->ParseHeader();
  57. // and read all sections until we're finished
  58. while (true)
  59. {
  60. this->mSections.push_back(Section());
  61. Section& sec = this->mSections.back();
  62. if(!this->ParseSection(sec))
  63. {
  64. break;
  65. }
  66. }
  67. if ( !DefaultLogger::isNullLogger())
  68. {
  69. char szBuffer[128]; // should be sufficiently large
  70. ::sprintf(szBuffer,"MD5Parser end. Parsed %i sections",(int)this->mSections.size());
  71. DefaultLogger::get()->debug(szBuffer);
  72. }
  73. }
  74. // ------------------------------------------------------------------------------------------------
  75. /*static*/ void MD5Parser::ReportError (const char* error, unsigned int line)
  76. {
  77. char szBuffer[1024]; // you, listen to me, you HAVE TO BE sufficiently large
  78. ::sprintf(szBuffer,"Line %i: %s",line,error);
  79. throw new ImportErrorException(szBuffer);
  80. }
  81. // ------------------------------------------------------------------------------------------------
  82. /*static*/ void MD5Parser::ReportWarning (const char* warn, unsigned int line)
  83. {
  84. char szBuffer[1024]; // you, listen to me, you HAVE TO BE sufficiently large
  85. ::sprintf(szBuffer,"Line %i: %s",line,warn);
  86. DefaultLogger::get()->warn(szBuffer);
  87. }
  88. // ------------------------------------------------------------------------------------------------
  89. void MD5Parser::ParseHeader()
  90. {
  91. // parse and validate the file version
  92. SkipSpaces();
  93. if (0 != ASSIMP_strincmp(buffer,"MD5Version",10) ||
  94. !IsSpace(*(buffer+=10)))
  95. {
  96. this->ReportError("Invalid MD5 file: MD5Version tag has not been found");
  97. }
  98. SkipSpaces();
  99. unsigned int iVer = ::strtol10(buffer,(const char**)&buffer);
  100. if (10 != iVer)
  101. {
  102. this->ReportWarning("MD5 version tag is unknown (10 is expected)");
  103. }
  104. this->SkipLine();
  105. // print the command line options to the console
  106. char* sz = buffer;
  107. while (!IsLineEnd( *buffer++));
  108. DefaultLogger::get()->info(std::string(sz,(uintptr_t)(buffer-sz)));
  109. this->SkipSpacesAndLineEnd();
  110. }
  111. // ------------------------------------------------------------------------------------------------
  112. bool MD5Parser::ParseSection(Section& out)
  113. {
  114. // store the current line number for use in error messages
  115. out.iLineNumber = this->lineNumber;
  116. // first parse the name of the section
  117. char* sz = buffer;
  118. while (!IsSpaceOrNewLine( *buffer))buffer++;
  119. out.mName = std::string(sz,(uintptr_t)(buffer-sz));
  120. SkipSpaces();
  121. while (true)
  122. {
  123. if ('{' == *buffer)
  124. {
  125. // it is a normal section so read all lines
  126. buffer++;
  127. while (true)
  128. {
  129. if (!SkipSpacesAndLineEnd())
  130. {
  131. return false; // seems this was the last section
  132. }
  133. if ('}' == *buffer)
  134. {
  135. buffer++;
  136. break;
  137. }
  138. out.mElements.push_back(Element());
  139. Element& elem = out.mElements.back();
  140. elem.iLineNumber = lineNumber;
  141. elem.szStart = buffer;
  142. // terminate the line with zero - remove all spaces at the end
  143. while (!IsLineEnd( *buffer))buffer++;
  144. //const char* end = buffer;
  145. do {buffer--;}
  146. while (IsSpace(*buffer));
  147. buffer++;
  148. *buffer++ = '\0';
  149. //if (*end) ++lineNumber;
  150. }
  151. break;
  152. }
  153. else if (!IsSpaceOrNewLine(*buffer))
  154. {
  155. // it is an element at global scope. Parse its value and go on
  156. // FIX: for MD5ANIm files - frame 0 {...} is allowed
  157. sz = buffer;
  158. while (!IsSpaceOrNewLine( *buffer++));
  159. out.mGlobalValue = std::string(sz,(uintptr_t)(buffer-sz));
  160. continue;
  161. }
  162. break;
  163. }
  164. return SkipSpacesAndLineEnd();
  165. }
  166. // ------------------------------------------------------------------------------------------------
  167. // skip all spaces ... handle EOL correctly
  168. #define AI_MD5_SKIP_SPACES() if(!SkipSpaces(&sz)) \
  169. MD5Parser::ReportWarning("Unexpected end of line",(*eit).iLineNumber);
  170. // read a triple float in brackets: (1.0 1.0 1.0)
  171. #define AI_MD5_READ_TRIPLE(vec) \
  172. AI_MD5_SKIP_SPACES(); \
  173. if ('(' != *sz++) \
  174. MD5Parser::ReportWarning("Unexpected token: ( was expected",(*eit).iLineNumber); \
  175. AI_MD5_SKIP_SPACES(); \
  176. sz = fast_atof_move(sz,(float&)vec.x); \
  177. AI_MD5_SKIP_SPACES(); \
  178. sz = fast_atof_move(sz,(float&)vec.y); \
  179. AI_MD5_SKIP_SPACES(); \
  180. sz = fast_atof_move(sz,(float&)vec.z); \
  181. AI_MD5_SKIP_SPACES(); \
  182. if (')' != *sz++) \
  183. MD5Parser::ReportWarning("Unexpected token: ) was expected",(*eit).iLineNumber);
  184. // parse a string, enclosed in quotation marks or not
  185. #define AI_MD5_PARSE_STRING(out) \
  186. bool bQuota = *sz == '\"'; \
  187. const char* szStart = sz; \
  188. while (!IsSpaceOrNewLine(*sz))++sz; \
  189. const char* szEnd = sz; \
  190. if (bQuota) \
  191. { \
  192. szStart++; \
  193. if ('\"' != *(szEnd-=1)) \
  194. { \
  195. MD5Parser::ReportWarning("Expected closing quotation marks in string", \
  196. (*eit).iLineNumber); \
  197. } \
  198. } \
  199. out.length = (size_t)(szEnd - szStart); \
  200. ::memcpy(out.data,szStart,out.length); \
  201. out.data[out.length] = '\0';
  202. // ------------------------------------------------------------------------------------------------
  203. MD5MeshParser::MD5MeshParser(SectionList& mSections)
  204. {
  205. DefaultLogger::get()->debug("MD5MeshParser begin");
  206. // now parse all sections
  207. for (SectionList::const_iterator
  208. iter = mSections.begin(), iterEnd = mSections.end();
  209. iter != iterEnd;++iter)
  210. {
  211. if ((*iter).mGlobalValue.length())
  212. {
  213. if ( !::strcmp("numMeshes",(*iter).mName.c_str()))
  214. {
  215. unsigned int iNumMeshes;
  216. if((iNumMeshes = ::strtol10((*iter).mGlobalValue.c_str())))
  217. {
  218. mMeshes.reserve(iNumMeshes);
  219. }
  220. }
  221. else if ( !::strcmp("numJoints",(*iter).mName.c_str()))
  222. {
  223. unsigned int iNumJoints;
  224. if((iNumJoints = ::strtol10((*iter).mGlobalValue.c_str())))
  225. {
  226. mJoints.reserve(iNumJoints);
  227. }
  228. }
  229. }
  230. else if (!::strcmp("joints",(*iter).mName.c_str()))
  231. {
  232. // now read all elements
  233. // "origin" -1 ( -0.000000 0.016430 -0.006044 ) ( 0.707107 0.000000 0.707107 )
  234. for (ElementList::const_iterator
  235. eit = (*iter).mElements.begin(), eitEnd = (*iter).mElements.end();
  236. eit != eitEnd; ++eit)
  237. {
  238. mJoints.push_back(BoneDesc());
  239. BoneDesc& desc = mJoints.back();
  240. const char* sz = (*eit).szStart;
  241. AI_MD5_PARSE_STRING(desc.mName);
  242. AI_MD5_SKIP_SPACES();
  243. // negative values can occur here ...
  244. bool bNeg = false;
  245. if ('-' == *sz){sz++;bNeg = true;}
  246. else if ('+' == *sz){sz++;}
  247. desc.mParentIndex = (int)::strtol10(sz,&sz);
  248. if (bNeg)desc.mParentIndex *= -1;
  249. AI_MD5_READ_TRIPLE(desc.mPositionXYZ);
  250. AI_MD5_READ_TRIPLE(desc.mRotationQuat); // normalized quaternion, so w is not there
  251. }
  252. }
  253. else if (!::strcmp("mesh",(*iter).mName.c_str()))
  254. {
  255. mMeshes.push_back(MeshDesc());
  256. MeshDesc& desc = mMeshes.back();
  257. // now read all elements
  258. for (ElementList::const_iterator
  259. eit = (*iter).mElements.begin(), eitEnd = (*iter).mElements.end();
  260. eit != eitEnd; ++eit)
  261. {
  262. const char* sz = (*eit).szStart;
  263. // shader attribute
  264. if (!ASSIMP_strincmp(sz,"shader",6) &&
  265. IsSpaceOrNewLine(*(sz+=6)++))
  266. {
  267. // don't expect quotation marks
  268. AI_MD5_SKIP_SPACES();
  269. AI_MD5_PARSE_STRING(desc.mShader);
  270. }
  271. // numverts attribute
  272. else if (!ASSIMP_strincmp(sz,"numverts",8) &&
  273. IsSpaceOrNewLine(*(sz+=8)++))
  274. {
  275. // reserve enough storage
  276. AI_MD5_SKIP_SPACES();
  277. unsigned int iNumVertices;
  278. if((iNumVertices = ::strtol10(sz)))
  279. desc.mVertices.resize(iNumVertices);
  280. }
  281. // numtris attribute
  282. else if (!ASSIMP_strincmp(sz,"numtris",7) &&
  283. IsSpaceOrNewLine(*(sz+=7)++))
  284. {
  285. // reserve enough storage
  286. AI_MD5_SKIP_SPACES();
  287. unsigned int iNumTris;
  288. if((iNumTris = ::strtol10(sz)))
  289. desc.mFaces.resize(iNumTris);
  290. }
  291. // numweights attribute
  292. else if (!ASSIMP_strincmp(sz,"numweights",10) &&
  293. IsSpaceOrNewLine(*(sz+=10)++))
  294. {
  295. // reserve enough storage
  296. AI_MD5_SKIP_SPACES();
  297. unsigned int iNumWeights;
  298. if((iNumWeights = ::strtol10(sz)))
  299. desc.mWeights.resize(iNumWeights);
  300. }
  301. // vert attribute
  302. // "vert 0 ( 0.394531 0.513672 ) 0 1"
  303. else if (!ASSIMP_strincmp(sz,"vert",4) &&
  304. IsSpaceOrNewLine(*(sz+=4)++))
  305. {
  306. AI_MD5_SKIP_SPACES();
  307. unsigned int idx = ::strtol10(sz,&sz);
  308. AI_MD5_SKIP_SPACES();
  309. if (idx >= desc.mVertices.size())
  310. desc.mVertices.resize(idx+1);
  311. VertexDesc& vert = desc.mVertices[idx];
  312. if ('(' != *sz++)
  313. MD5Parser::ReportWarning("Unexpected token: ( was expected",(*eit).iLineNumber);
  314. AI_MD5_SKIP_SPACES();
  315. sz = fast_atof_move(sz,(float&)vert.mUV.x);
  316. AI_MD5_SKIP_SPACES();
  317. sz = fast_atof_move(sz,(float&)vert.mUV.y);
  318. AI_MD5_SKIP_SPACES();
  319. if (')' != *sz++)
  320. MD5Parser::ReportWarning("Unexpected token: ) was expected",(*eit).iLineNumber);
  321. AI_MD5_SKIP_SPACES();
  322. vert.mFirstWeight = ::strtol10(sz,&sz);
  323. AI_MD5_SKIP_SPACES();
  324. vert.mNumWeights = ::strtol10(sz,&sz);
  325. }
  326. // tri attribute
  327. // "tri 0 15 13 12"
  328. else if (!ASSIMP_strincmp(sz,"tri",3) &&
  329. IsSpaceOrNewLine(*(sz+=3)++))
  330. {
  331. AI_MD5_SKIP_SPACES();
  332. unsigned int idx = ::strtol10(sz,&sz);
  333. if (idx >= desc.mFaces.size())
  334. desc.mFaces.resize(idx+1);
  335. aiFace& face = desc.mFaces[idx];
  336. face.mIndices = new unsigned int[face.mNumIndices = 3];
  337. for (unsigned int i = 0; i < 3;++i)
  338. {
  339. AI_MD5_SKIP_SPACES();
  340. face.mIndices[i] = ::strtol10(sz,&sz);
  341. }
  342. }
  343. // weight attribute
  344. // "weight 362 5 0.500000 ( -3.553583 11.893474 9.719339 )"
  345. else if (!ASSIMP_strincmp(sz,"weight",6) &&
  346. IsSpaceOrNewLine(*(sz+=6)++))
  347. {
  348. AI_MD5_SKIP_SPACES();
  349. unsigned int idx = ::strtol10(sz,&sz);
  350. AI_MD5_SKIP_SPACES();
  351. if (idx >= desc.mWeights.size())
  352. desc.mWeights.resize(idx+1);
  353. WeightDesc& weight = desc.mWeights[idx];
  354. weight.mBone = ::strtol10(sz,&sz);
  355. AI_MD5_SKIP_SPACES();
  356. sz = fast_atof_move(sz,weight.mWeight);
  357. AI_MD5_READ_TRIPLE(weight.vOffsetPosition);
  358. }
  359. }
  360. }
  361. }
  362. DefaultLogger::get()->debug("MD5MeshParser end");
  363. }
  364. // ------------------------------------------------------------------------------------------------
  365. MD5AnimParser::MD5AnimParser(SectionList& mSections)
  366. {
  367. DefaultLogger::get()->debug("MD5AnimParser begin");
  368. fFrameRate = 24.0f;
  369. mNumAnimatedComponents = 0xffffffff;
  370. // now parse all sections
  371. for (SectionList::const_iterator
  372. iter = mSections.begin(), iterEnd = mSections.end();
  373. iter != iterEnd;++iter)
  374. {
  375. if (!::strcmp("hierarchy",(*iter).mName.c_str()))
  376. {
  377. // now read all elements
  378. // "sheath" 0 63 6
  379. for (ElementList::const_iterator
  380. eit = (*iter).mElements.begin(), eitEnd = (*iter).mElements.end();
  381. eit != eitEnd; ++eit)
  382. {
  383. mAnimatedBones.push_back ( AnimBoneDesc () );
  384. AnimBoneDesc& desc = mAnimatedBones.back();
  385. const char* sz = (*eit).szStart;
  386. AI_MD5_PARSE_STRING(desc.mName);
  387. AI_MD5_SKIP_SPACES();
  388. // parent index
  389. // negative values can occur here ...
  390. bool bNeg = false;
  391. if ('-' == *sz){sz++;bNeg = true;}
  392. else if ('+' == *sz){sz++;}
  393. desc.mParentIndex = (int)::strtol10(sz,&sz);
  394. if (bNeg)desc.mParentIndex *= -1;
  395. // flags (highest is 2^6-1)
  396. AI_MD5_SKIP_SPACES();
  397. if(63 < (desc.iFlags = ::strtol10(sz,&sz)))
  398. {
  399. MD5Parser::ReportWarning("Invalid flag combination in hierarchy section",
  400. (*eit).iLineNumber);
  401. }
  402. AI_MD5_SKIP_SPACES();
  403. // index of the first animation keyframe component for this joint
  404. desc.iFirstKeyIndex = ::strtol10(sz,&sz);
  405. }
  406. }
  407. else if(!::strcmp("baseframe",(*iter).mName.c_str()))
  408. {
  409. // now read all elements
  410. // ( -0.000000 0.016430 -0.006044 ) ( 0.707107 0.000242 0.707107 )
  411. for (ElementList::const_iterator
  412. eit = (*iter).mElements.begin(), eitEnd = (*iter).mElements.end();
  413. eit != eitEnd; ++eit)
  414. {
  415. const char* sz = (*eit).szStart;
  416. mBaseFrames.push_back ( BaseFrameDesc () );
  417. BaseFrameDesc& desc = mBaseFrames.back();
  418. AI_MD5_READ_TRIPLE(desc.vPositionXYZ);
  419. AI_MD5_READ_TRIPLE(desc.vRotationQuat);
  420. }
  421. }
  422. else if(!::strcmp("frame",(*iter).mName.c_str()))
  423. {
  424. if (!(*iter).mGlobalValue.length())
  425. {
  426. MD5Parser::ReportWarning("A frame section must have a frame index",
  427. (*iter).iLineNumber);
  428. continue;
  429. }
  430. mFrames.push_back ( FrameDesc () );
  431. FrameDesc& desc = mFrames.back();
  432. desc.iIndex = ::strtol10((*iter).mGlobalValue.c_str());
  433. // we do already know how much storage we will presumably need
  434. if (0xffffffff != mNumAnimatedComponents)
  435. desc.mValues.reserve(mNumAnimatedComponents);
  436. // now read all elements
  437. // (continous list of float values)
  438. for (ElementList::const_iterator
  439. eit = (*iter).mElements.begin(), eitEnd = (*iter).mElements.end();
  440. eit != eitEnd; ++eit)
  441. {
  442. const char* sz = (*eit).szStart;
  443. while (SkipSpaces(sz,&sz))
  444. {
  445. float f;
  446. sz = fast_atof_move(sz,f);
  447. desc.mValues.push_back(f);
  448. }
  449. }
  450. }
  451. else if(!::strcmp("numFrames",(*iter).mName.c_str()))
  452. {
  453. unsigned int iNum;
  454. if((iNum = ::strtol10((*iter).mGlobalValue.c_str())))
  455. {
  456. mFrames.reserve(iNum);
  457. }
  458. }
  459. else if(!::strcmp("numJoints",(*iter).mName.c_str()))
  460. {
  461. unsigned int iNum;
  462. if((iNum = ::strtol10((*iter).mGlobalValue.c_str())))
  463. {
  464. mAnimatedBones.reserve(iNum);
  465. // try to guess the number of animated components if that element is not given
  466. if (0xffffffff == mNumAnimatedComponents)
  467. mNumAnimatedComponents = iNum * 6;
  468. }
  469. }
  470. else if(!::strcmp("numAnimatedComponents",(*iter).mName.c_str()))
  471. {
  472. unsigned int iNum;
  473. if((iNum = ::strtol10((*iter).mGlobalValue.c_str())))
  474. {
  475. mAnimatedBones.reserve(iNum);
  476. }
  477. }
  478. else if(!::strcmp("frameRate",(*iter).mName.c_str()))
  479. {
  480. fast_atof_move((*iter).mGlobalValue.c_str(),this->fFrameRate);
  481. }
  482. }
  483. DefaultLogger::get()->debug("MD5AnimParser end");
  484. }
  485. #undef AI_MD5_SKIP_SPACES
  486. #undef AI_MD5_READ_TRIPLE
  487. #undef AI_MD5_PARSE_STRING