MD5Parser.cpp 17 KB

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