QGearsHRCFile.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. -----------------------------------------------------------------------------
  3. The MIT License (MIT)
  4. Copyright (c) 2013-08-11 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/QGearsHRCFile.h"
  23. #include <OgreLogManager.h>
  24. #include <OgreMeshManager.h>
  25. #include <OgreSkeletonManager.h>
  26. //#include "common/FF7NameLookup.h"
  27. #include "../../SupportedGames/FinalFantasy7/include/common/FF7NameLookup.h"
  28. #include "common/QGearsStringUtil.h"
  29. #include "data/QGearsHRCFileSerializer.h"
  30. #include "data/QGearsHRCMeshLoader.h"
  31. #include "data/QGearsHRCSkeletonLoader.h"
  32. namespace QGears
  33. {
  34. //---------------------------------------------------------------------
  35. /*static*/ const String HRCFile::RESOURCE_TYPE( "QGearsHRCFile" );
  36. /*static*/ const float HRCFile::kDownScaler = 1.0f; //33.0f; TODO fix unit tests before changing me
  37. //---------------------------------------------------------------------
  38. HRCFile::HRCFile( Ogre::ResourceManager *creator
  39. ,const String &name, Ogre::ResourceHandle handle
  40. ,const String &group, bool isManual
  41. ,Ogre::ManualResourceLoader *loader ) :
  42. Ogre::Resource( creator, name, handle, group, isManual, loader )
  43. ,m_mesh_loader( nullptr )
  44. ,m_skeleton_loader( nullptr )
  45. {
  46. createParamDictionary( RESOURCE_TYPE );
  47. }
  48. //---------------------------------------------------------------------
  49. HRCFile::~HRCFile()
  50. {
  51. if( m_skeleton_loader )
  52. {
  53. Ogre::SkeletonManager::getSingleton().remove( m_skeleton->getHandle() );
  54. delete m_skeleton_loader;
  55. m_skeleton_loader = nullptr;
  56. }
  57. if( m_mesh_loader )
  58. {
  59. Ogre::MeshManager::getSingleton().remove( m_mesh->getHandle() );
  60. delete m_mesh_loader;
  61. m_mesh_loader = nullptr;
  62. }
  63. m_skeleton.setNull();
  64. m_mesh.setNull();
  65. unload();
  66. }
  67. //---------------------------------------------------------------------
  68. void
  69. HRCFile::loadImpl()
  70. {
  71. HRCFileSerializer serializer;
  72. Ogre::DataStreamPtr stream( Ogre::ResourceGroupManager::getSingleton().openResource( mName, mGroup, true, this ) );
  73. serializer.importHRCFile( stream, this );
  74. const String skeleton_file_name( getSkeletonFileName() );
  75. Ogre::SkeletonManager &skeleton_manager( Ogre::SkeletonManager::getSingleton() );
  76. m_skeleton = skeleton_manager.getByName( skeleton_file_name, mGroup );
  77. if( m_skeleton.isNull() )
  78. {
  79. assert( m_skeleton_loader == nullptr );
  80. m_skeleton_loader = new HRCSkeletonLoader( *this );
  81. m_skeleton = skeleton_manager.create( skeleton_file_name, mGroup, true, m_skeleton_loader );
  82. }
  83. const String mesh_file_name( getMeshFileName() );
  84. Ogre::LogManager::getSingleton().stream()
  85. << "HRC::loadImpl(): mesh_file_name: " << mesh_file_name;
  86. Ogre::MeshManager &mesh_manager( Ogre::MeshManager::getSingleton() );
  87. m_mesh = mesh_manager.getByName( mesh_file_name, mGroup );
  88. if( m_mesh.isNull() )
  89. {
  90. assert( m_mesh_loader == nullptr );
  91. m_mesh_loader = new HRCMeshLoader( *this );
  92. m_mesh = mesh_manager.create( mesh_file_name, mGroup, true, m_mesh_loader );
  93. }
  94. }
  95. //---------------------------------------------------------------------
  96. void
  97. HRCFile::unloadImpl()
  98. {
  99. m_skeleton_name.clear();
  100. m_bones.clear();
  101. }
  102. //---------------------------------------------------------------------
  103. size_t
  104. HRCFile::calculateSize( const Bone &bone ) const
  105. {
  106. size_t size_rsd_names( 0 );
  107. for( RSDNameList::const_iterator it( bone.rsd_names.begin() )
  108. ;it != bone.rsd_names.end()
  109. ;++it )
  110. {
  111. size_rsd_names += it->size();
  112. }
  113. return bone.name.size()
  114. +bone.parent.size()
  115. +sizeof( bone.length )
  116. +size_rsd_names;
  117. }
  118. //---------------------------------------------------------------------
  119. size_t
  120. HRCFile::calculateSize() const
  121. {
  122. size_t size_bones( 0 );
  123. for( BoneList::const_iterator it( m_bones.begin() )
  124. ;it != m_bones.end()
  125. ;++it )
  126. {
  127. size_bones += calculateSize( *it );
  128. }
  129. return m_skeleton_name.size() + size_bones;
  130. }
  131. //---------------------------------------------------------------------
  132. void
  133. HRCFile::setSkeletonName( const String &name )
  134. {
  135. m_skeleton_name = name;
  136. }
  137. //---------------------------------------------------------------------
  138. String
  139. HRCFile::getSkeletonFileName( void ) const
  140. {
  141. String path;
  142. StringUtil::splitPath( getName(), path );
  143. return path + m_skeleton_name + EXT_SKELETON;
  144. }
  145. //---------------------------------------------------------------------
  146. String
  147. HRCFile::getMeshFileName( void ) const
  148. {
  149. String base_name;
  150. StringUtil::splitBase( getName(), base_name );
  151. return FF7::NameLookup::model( base_name ) + EXT_MESH;
  152. }
  153. //---------------------------------------------------------------------
  154. Ogre::SkeletonPtr
  155. HRCFile::getSkeleton( void ) const
  156. {
  157. m_skeleton->load();
  158. return m_skeleton;
  159. }
  160. //---------------------------------------------------------------------
  161. }