QGearsFLevelFileSerializer.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /*
  2. -----------------------------------------------------------------------------
  3. The MIT License (MIT)
  4. Copyright (c) 2013-08-24 Tobias Peters <tobias.peters@kreativeffekt.at>
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. -----------------------------------------------------------------------------
  21. */
  22. #include "data/QGearsFLevelFileSerializer.h"
  23. #include <OgreLogManager.h>
  24. #include <OgreException.h>
  25. #include "FF7Common.h"
  26. #include "common/QGearsStringUtil.h"
  27. #include "data/QGearsBackgroundFileManager.h"
  28. #include "data/QGearsBackgroundFileSerializer.h"
  29. #include "data/QGearsCameraMatrixFileManager.h"
  30. #include "data/QGearsCameraMatrixFileSerializer.h"
  31. #include "data/FF7ModelListFileManager.h"
  32. #include "data/FF7ModelListFileSerializer.h"
  33. #include "data/QGearsPaletteFileManager.h"
  34. #include "data/QGearsPaletteFileSerializer.h"
  35. #include "map/QGearsWalkmeshFileManager.h"
  36. #include "map/FF7WalkmeshFileSerializer.h"
  37. namespace QGears
  38. {
  39. // TODO: move flevel stuff to ff7 as it is ff7 related
  40. using FF7::ModelListFileManager;
  41. using FF7::ModelListFileSerializer;
  42. using FF7::WalkmeshFileSerializer;
  43. //--------------------------------------------------------------------------
  44. const String FLevelFileSerializer::TAG_FILE_END( "FINAL FANTASY7" );
  45. //--------------------------------------------------------------------------
  46. FLevelFileSerializer::FLevelFileSerializer() :
  47. Serializer()
  48. {
  49. }
  50. //--------------------------------------------------------------------------
  51. FLevelFileSerializer::~FLevelFileSerializer()
  52. {
  53. }
  54. //--------------------------------------------------------------------------
  55. void
  56. FLevelFileSerializer::importFLevelFile(Ogre::DataStreamPtr &stream, FLevelFile* pDest)
  57. {
  58. // Read header
  59. const size_t start_position(stream->tell());
  60. readFileHeader(stream);
  61. // Read section offsets
  62. std::vector<uint32> section_offsetsVec(m_header.section_count);
  63. std::vector<uint32> section_sizesVec(m_header.section_count);
  64. readInts(stream, section_offsetsVec.data(), m_header.section_count);
  65. // Now calculate section sizes
  66. size_t fileSize = stream->size();
  67. for (size_t i = 0; i < section_offsetsVec.size(); i++)
  68. {
  69. if (i+1 == section_offsetsVec.size())
  70. {
  71. // We need to know the file size to calculate this, since we don't have it we
  72. // just read till end of stream on the final section.
  73. section_sizesVec[i] = 0;
  74. }
  75. else
  76. {
  77. section_sizesVec[i] = section_offsetsVec[i + 1] - section_offsetsVec[i] - 4;
  78. }
  79. }
  80. Ogre::DataStreamPtr section;
  81. for (size_t i(0); i < m_header.section_count; ++i)
  82. {
  83. const size_t current_offset(stream->tell() - start_position);
  84. const size_t section_gap(section_offsetsVec[i] - current_offset);
  85. if (current_offset > section_offsetsVec[i])
  86. {
  87. OGRE_EXCEPT(Ogre::Exception::ERR_INVALIDPARAMS
  88. , "FLevel sections overlap"
  89. , "FLevelFileSerializer::importFLevelFile");
  90. }
  91. else if (section_gap)
  92. {
  93. stream->skip(section_gap);
  94. Ogre::LogManager::getSingleton().stream()
  95. << "Warning: skiping gap in front of section " << i
  96. << " gap size " << section_gap
  97. << " FLevelFileSerializer::importFLevelFile";
  98. }
  99. readSectionData(stream, section, section_sizesVec[i]);
  100. readSection(section, pDest, i);
  101. section->close();
  102. section.setNull();
  103. }
  104. readEnd(stream);
  105. }
  106. //--------------------------------------------------------------------------
  107. void
  108. FLevelFileSerializer::readFileHeader( Ogre::DataStreamPtr &stream )
  109. {
  110. // Read version, check its zero
  111. readShort( stream, m_header.version );
  112. if( m_header.version != 0 )
  113. {
  114. OGRE_EXCEPT(Ogre::Exception::ERR_INVALIDPARAMS
  115. ,"FLevel has unsupported version"
  116. ,"FLevelFileSerializer::importFLevelFile" );
  117. }
  118. // Read number of sections, check its 9
  119. readUInt32( stream, m_header.section_count );
  120. if( m_header.section_count != SECTION_COUNT )
  121. {
  122. Ogre::LogManager::getSingleton().stream()
  123. << "Warning: FLevel section_count has unexpected value "
  124. << m_header.section_count << " should be " << SECTION_COUNT
  125. << " FLevelFileSerializer::importFLevelFile";
  126. }
  127. }
  128. //--------------------------------------------------------------------------
  129. void
  130. FLevelFileSerializer::readSectionData( Ogre::DataStreamPtr &stream
  131. ,Ogre::DataStreamPtr &out_buffer,
  132. size_t sectionSize )
  133. {
  134. // This can be wrong, we read it so we're at the correct stream position
  135. // but use our manually calculated section size instead.
  136. uint32 length( 0 );
  137. readUInt32( stream, length );
  138. // TODO: implement SubDataStream class to restrict access size etc
  139. // so we don't have to copy the whole memory
  140. if (sectionSize == 0)
  141. {
  142. // TODO FIX ME - read to EOS
  143. // Hope this is actually correct :)
  144. sectionSize = length;
  145. }
  146. Ogre::MemoryDataStream *buffer(new Ogre::MemoryDataStream(sectionSize));
  147. stream->read(buffer->getPtr(), sectionSize);
  148. out_buffer = Ogre::DataStreamPtr( buffer );
  149. }
  150. //--------------------------------------------------------------------------
  151. void
  152. FLevelFileSerializer::readSection( Ogre::DataStreamPtr &stream
  153. ,FLevelFile *pDest
  154. ,const size_t section_index )
  155. {
  156. switch (section_index)
  157. {
  158. case SECTION_SCRIPT:
  159. {
  160. std::vector<u8> buffer(stream->size());
  161. stream->read(buffer.data(), buffer.size());
  162. pDest->setRawScript(buffer);
  163. }
  164. break;
  165. case SECTION_CAMERA_MATRIX:
  166. readCameraMatrix( stream, pDest );
  167. break;
  168. case SECTION_MODEL_LOADER:
  169. readModelList( stream, pDest );
  170. break;
  171. case SECTION_PALETTE:
  172. readPalette( stream, pDest );
  173. break;
  174. case SECTION_WALKMESH:
  175. readWalkmesh( stream, pDest );
  176. break;
  177. case SECTION_BACKGROUND:
  178. readBackground( stream, pDest );
  179. break;
  180. }
  181. }
  182. //--------------------------------------------------------------------------
  183. template<typename ResourceManagerType>
  184. Ogre::ResourcePtr
  185. FLevelFileSerializer::createResource( FLevelFile *pDest, const String &extension )
  186. {
  187. assert( pDest );
  188. Ogre::ResourceManager &mgr( ResourceManagerType::getSingleton() );
  189. String name( getBaseName( pDest ) + extension );
  190. Ogre::ResourcePtr resource = mgr.createResource( name, pDest->getGroup(), true );
  191. resource->_notifyOrigin( pDest->getName() );
  192. return resource;
  193. }
  194. //--------------------------------------------------------------------------
  195. void
  196. FLevelFileSerializer::readCameraMatrix( Ogre::DataStreamPtr &stream, FLevelFile *pDest )
  197. {
  198. CameraMatrixFilePtr camera_matrix = createResource<CameraMatrixFileManager>( pDest, EXT_CAMERA_MATRIX ).staticCast<CameraMatrixFile>();
  199. CameraMatrixFileSerializer ser;
  200. ser.importCameraMatrixFile( stream, camera_matrix.getPointer() );
  201. pDest->setCameraMatrix( camera_matrix );
  202. }
  203. //--------------------------------------------------------------------------
  204. void
  205. FLevelFileSerializer::readModelList( Ogre::DataStreamPtr &stream, FLevelFile *pDest )
  206. {
  207. ModelListFilePtr model_list = createResource<ModelListFileManager>( pDest, EXT_MODEL_LIST ).staticCast<ModelListFile>();
  208. ModelListFileSerializer ser;
  209. ser.importModelListFile( stream, model_list.getPointer() );
  210. pDest->setModelList( model_list );
  211. }
  212. //--------------------------------------------------------------------------
  213. void
  214. FLevelFileSerializer::readPalette( Ogre::DataStreamPtr &stream, FLevelFile *pDest )
  215. {
  216. PaletteFilePtr palette = createResource<PaletteFileManager>( pDest, EXT_PALETTE ).staticCast<PaletteFile>();
  217. PaletteFileSerializer ser;
  218. ser.importPaletteFile( stream, palette.getPointer() );
  219. pDest->setPalette( palette );
  220. }
  221. //--------------------------------------------------------------------------
  222. void
  223. FLevelFileSerializer::readWalkmesh( Ogre::DataStreamPtr &stream, FLevelFile *pDest )
  224. {
  225. WalkmeshFilePtr walkmesh = createResource<WalkmeshFileManager>( pDest, EXT_WALKMESH ).staticCast<WalkmeshFile>();
  226. WalkmeshFileSerializer ser;
  227. ser.importWalkmeshFile( stream, walkmesh.getPointer() );
  228. WalkmeshFile::TriangleList& triangles( walkmesh->getTriangles() );
  229. WalkmeshFile::TriangleList::iterator it( triangles.begin() );
  230. WalkmeshFile::TriangleList::const_iterator it_end( triangles.end() );
  231. Ogre::Real scale = static_cast<Ogre::Real>(pDest->getCameraMatrix()->getCount() );
  232. scale *= FF7::FIELD_POSITION_SCALE;
  233. while( it != it_end )
  234. {
  235. it->a /= scale;
  236. it->b /= scale;
  237. it->c /= scale;
  238. ++it;
  239. }
  240. pDest->setWalkmesh( walkmesh );
  241. }
  242. //--------------------------------------------------------------------------
  243. void
  244. FLevelFileSerializer::readBackground( Ogre::DataStreamPtr &stream, FLevelFile *pDest )
  245. {
  246. BackgroundFilePtr background = createResource<BackgroundFileManager>( pDest, EXT_BACKGROUND ).staticCast<BackgroundFile>();
  247. BackgroundFileSerializer ser;
  248. ser.importBackgroundFile( stream, background.getPointer() );
  249. pDest->setBackground( background );
  250. }
  251. //--------------------------------------------------------------------------
  252. String
  253. FLevelFileSerializer::getBaseName( const FLevelFile *pDest ) const
  254. {
  255. String base_name;
  256. StringUtil::splitBase( pDest->getName(), base_name );
  257. return base_name;
  258. }
  259. //--------------------------------------------------------------------------
  260. void
  261. FLevelFileSerializer::readEnd( Ogre::DataStreamPtr &stream )
  262. {
  263. readEndString( stream, TAG_FILE_END );
  264. }
  265. //----------------------------------------------------------------------
  266. template<typename ValueType>
  267. void
  268. FLevelFileSerializer::readVector( Ogre::DataStreamPtr &stream
  269. ,std::vector<ValueType> &pDest
  270. ,size_t count )
  271. {
  272. pDest.clear();
  273. pDest.reserve( count );
  274. for( size_t i( count ); i--; )
  275. {
  276. ValueType in_tmp;
  277. readObject( stream, in_tmp );
  278. pDest.push_back( in_tmp );
  279. }
  280. }
  281. //--------------------------------------------------------------------------
  282. }