sfxFMODEventGroup.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  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
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "sfx/fmod/sfxFMODEventGroup.h"
  23. #include "sfx/fmod/sfxFMODDevice.h"
  24. #include "sfx/fmod/sfxFMODEvent.h"
  25. #include "sfx/fmod/sfxFMODProject.h"
  26. #include "core/stream/bitStream.h"
  27. #include "console/engineAPI.h"
  28. IMPLEMENT_CO_DATABLOCK_V1( SFXFMODEventGroup );
  29. ConsoleDocClass( SFXFMODEventGroup,
  30. "@brief A group of events in an imported FMOD Designer project.\n\n"
  31. ""
  32. "@note Instances of this class \n\n"
  33. "@ingroup SFXFMOD\n"
  34. "@ingroup Datablocks"
  35. );
  36. //-----------------------------------------------------------------------------
  37. SFXFMODEventGroup::SFXFMODEventGroup()
  38. : mProject( NULL ),
  39. mHandle( NULL ),
  40. mParent( NULL ),
  41. mChildren( NULL ),
  42. mSibling( NULL ),
  43. mLoadCount( 0 ),
  44. mEvents( NULL ),
  45. mNumEvents( 0 ),
  46. mNumGroups( 0 ),
  47. mParentId( 0 ),
  48. mProjectId( 0 )
  49. {
  50. }
  51. //-----------------------------------------------------------------------------
  52. SFXFMODEventGroup::SFXFMODEventGroup( SFXFMODProject* project, FMOD_EVENTGROUP* handle, SFXFMODEventGroup* parent )
  53. : mProject( project ),
  54. mHandle( handle ),
  55. mParent( parent ),
  56. mChildren( NULL ),
  57. mSibling( NULL ),
  58. mLoadCount( 0 ),
  59. mEvents( NULL ),
  60. mNumEvents( 0 ),
  61. mNumGroups( 0 ),
  62. mParentId( 0 ),
  63. mProjectId( 0 )
  64. {
  65. AssertFatal( project != NULL, "SFXFMODEventGroup::SFXFMODEventGroup - got a NULL project!" );
  66. AssertFatal( handle != NULL, "SFXFMODEventGroup::SFXFMODEventGroup - got a NULL group handle!" );
  67. // Fetch the name.
  68. int index;
  69. char* name = NULL;
  70. SFXFMODDevice::smFunc->FMOD_EventGroup_GetInfo( handle, &index, &name );
  71. mName = name;
  72. }
  73. //-----------------------------------------------------------------------------
  74. SFXFMODEventGroup::~SFXFMODEventGroup()
  75. {
  76. AssertFatal( mEvents == NULL, "SFXFMODEventGroup::~SFXFMODEventGroup - group still has events attached" );
  77. AssertFatal( mChildren == NULL, "SFXFMODEventGroup::~SFXFMODEventGroup - group still has subgroups attached" );
  78. }
  79. //-----------------------------------------------------------------------------
  80. void SFXFMODEventGroup::initPersistFields()
  81. {
  82. addGroup( "DO NOT MODIFY!!" );
  83. addField( "fmodProject", TYPEID< SFXFMODProject >(), Offset( mProject, SFXFMODEventGroup ), "DO NOT MODIFY!!" );
  84. addField( "fmodGroup", TYPEID< SFXFMODEventGroup >(), Offset( mParent, SFXFMODEventGroup ), "DO NOT MODIFY!!" );
  85. addField( "fmodName", TypeRealString, Offset( mName, SFXFMODEventGroup ), "DO NOT MODIFY!!" );
  86. endGroup( "DO NOT MODIFY!!" );
  87. Parent::initPersistFields();
  88. }
  89. //-----------------------------------------------------------------------------
  90. bool SFXFMODEventGroup::onAdd()
  91. {
  92. if( !Parent::onAdd() )
  93. return false;
  94. if( !mProject )
  95. {
  96. Con::errorf( "SFXFMODEventGroup - not part of a project" );
  97. return false;
  98. }
  99. if( mParent )
  100. mParent->_addGroup( this );
  101. mProject->_addGroup( this );
  102. return true;
  103. }
  104. //-----------------------------------------------------------------------------
  105. void SFXFMODEventGroup::onRemove()
  106. {
  107. Parent::onRemove();
  108. if( !mProject )
  109. return;
  110. release();
  111. while( mEvents )
  112. mEvents->deleteObject();
  113. while( mChildren )
  114. mChildren->deleteObject();
  115. if( mParent )
  116. mParent->_removeGroup( this );
  117. mProject->_removeGroup( this );
  118. }
  119. //-----------------------------------------------------------------------------
  120. bool SFXFMODEventGroup::preload( bool server, String& errorStr )
  121. {
  122. if( !Parent::preload( server, errorStr ) )
  123. return false;
  124. if( !server )
  125. {
  126. if( mParentId != 0 && !Sim::findObject( mParentId, mParent ) )
  127. {
  128. errorStr = String::ToString( "SFXFMODEventGroup - parent group '%i' does not exist", mParentId );
  129. return false;
  130. }
  131. if( !Sim::findObject( mProjectId, mProject ) )
  132. {
  133. errorStr = String::ToString( "SFXFMODEventGroup - project '%i' does not exist", mProjectId );
  134. return false;
  135. }
  136. }
  137. return true;
  138. }
  139. //-----------------------------------------------------------------------------
  140. void SFXFMODEventGroup::packData( BitStream* stream )
  141. {
  142. Parent::packData( stream );
  143. stream->write( mName );
  144. stream->writeRangedS32( mProject->getId(), DataBlockObjectIdFirst, DataBlockObjectIdLast );
  145. if( stream->writeFlag( mParent ) )
  146. stream->writeRangedS32( mParent->getId(), DataBlockObjectIdFirst, DataBlockObjectIdLast );
  147. }
  148. //-----------------------------------------------------------------------------
  149. void SFXFMODEventGroup::unpackData( BitStream* stream )
  150. {
  151. Parent::unpackData( stream );
  152. stream->read( &mName );
  153. mProjectId = stream->readRangedS32( DataBlockObjectIdFirst, DataBlockObjectIdLast );
  154. if( stream->readFlag() )
  155. mParentId = stream->readRangedS32( DataBlockObjectIdFirst, DataBlockObjectIdLast );
  156. else
  157. mParentId = 0;
  158. }
  159. //-----------------------------------------------------------------------------
  160. String SFXFMODEventGroup::getQualifiedName() const
  161. {
  162. if( mParent )
  163. return String::ToString( "%s/%s", mParent->getQualifiedName().c_str(), mName.c_str() );
  164. else
  165. return mName;
  166. }
  167. //-----------------------------------------------------------------------------
  168. bool SFXFMODEventGroup::isDataLoaded() const
  169. {
  170. // Check whether we or any of our parents has triggered a load.
  171. for( const SFXFMODEventGroup* group = this; group != NULL; group = group->mParent )
  172. if( group->mLoadCount > 0 )
  173. return true;
  174. return false;
  175. }
  176. //-----------------------------------------------------------------------------
  177. bool SFXFMODEventGroup::loadData( bool samples, bool streams )
  178. {
  179. if( !mHandle )
  180. acquire();
  181. if( !mLoadCount )
  182. {
  183. FMOD_EVENT_RESOURCE resource;
  184. if( samples && streams )
  185. resource = FMOD_EVENT_RESOURCE_STREAMS_AND_SAMPLES;
  186. else if( samples )
  187. resource = FMOD_EVENT_RESOURCE_SAMPLES;
  188. else if( streams )
  189. resource = FMOD_EVENT_RESOURCE_STREAMS;
  190. else
  191. return true;
  192. FMOD_RESULT result = SFXFMODDevice::smFunc->FMOD_EventGroup_LoadEventData( mHandle, resource, FMOD_EVENT_DEFAULT );
  193. if( result != FMOD_OK )
  194. {
  195. Con::errorf( "SFXFMODEventGroup::loadData - could not load data: %s", FMODResultToString( result ).c_str() );
  196. return false;
  197. }
  198. SFXFMODDevice::instance()->updateMemUsageStats();
  199. Con::printf( "SFXFMODProject - %s: Loaded data for group '%s'", mProject->getName(), getQualifiedName().c_str() );
  200. }
  201. mLoadCount ++;
  202. return true;
  203. }
  204. //-----------------------------------------------------------------------------
  205. void SFXFMODEventGroup::freeData( bool force )
  206. {
  207. bool isLoaded = ( mLoadCount > 0 );
  208. if( !isLoaded )
  209. isLoaded = ( mParent ? mParent->isDataLoaded() : false );
  210. else
  211. {
  212. if( force )
  213. mLoadCount = 0;
  214. else
  215. -- mLoadCount;
  216. }
  217. if( !mLoadCount && isLoaded )
  218. {
  219. FMOD_RESULT result = SFXFMODDevice::smFunc->FMOD_EventGroup_FreeEventData( mHandle, ( FMOD_EVENT* ) NULL, false );
  220. if( result != FMOD_OK )
  221. Con::errorf( "SFXFMODEventGroup - failed freeing event data: %s", FMODResultToString( result ).c_str() );
  222. SFXFMODDevice::instance()->updateMemUsageStats();
  223. Con::printf( "SFXFMODProject - %s: Cleared data for group '%s'", mProject->getName(), getQualifiedName().c_str() );
  224. }
  225. }
  226. //-----------------------------------------------------------------------------
  227. void SFXFMODEventGroup::acquire( bool recursive )
  228. {
  229. // Make sure the project is acquired.
  230. mProject->acquire();
  231. // Acquire the group.
  232. if( !mHandle )
  233. {
  234. if( mParent )
  235. {
  236. mParent->acquire();
  237. SFXFMODDevice::smFunc->FMOD_EventGroup_GetGroup( mParent->mHandle, mName, true, &mHandle );
  238. }
  239. else
  240. {
  241. mProject->acquire();
  242. SFXFMODDevice::smFunc->FMOD_EventProject_GetGroup( mProject->mHandle, mName, true, &mHandle );
  243. }
  244. }
  245. // Acquite events and subgroups.
  246. if( recursive )
  247. {
  248. for( SFXFMODEvent* event = mEvents; event != NULL; event = event->mSibling )
  249. event->acquire();
  250. for( SFXFMODEventGroup* group = mChildren; group != NULL; group = group->mSibling )
  251. group->acquire( true );
  252. }
  253. }
  254. //-----------------------------------------------------------------------------
  255. void SFXFMODEventGroup::release()
  256. {
  257. if( !mHandle )
  258. return;
  259. // Free the event data if we still have it loaded.
  260. if( isDataLoaded() )
  261. freeData( true );
  262. // Release events.
  263. for( SFXFMODEvent* event = mEvents; event != NULL; event = event->mSibling )
  264. event->release();
  265. // Release children.
  266. for( SFXFMODEventGroup* child = mChildren; child != NULL; child = child->mSibling )
  267. child->release();
  268. // Release our handle.
  269. freeData();
  270. mHandle = NULL;
  271. }
  272. //-----------------------------------------------------------------------------
  273. void SFXFMODEventGroup::_load()
  274. {
  275. // Make sure we have the group open.
  276. if( !mHandle )
  277. acquire();
  278. // Fetch info.
  279. int numEvents;
  280. int numGroups;
  281. SFXFMODDevice::smFunc->FMOD_EventGroup_GetNumEvents( mHandle, &numEvents );
  282. SFXFMODDevice::smFunc->FMOD_EventGroup_GetNumGroups( mHandle, &numGroups );
  283. // Load events.
  284. for( U32 i = 0; i < numEvents; ++ i )
  285. {
  286. FMOD_EVENT* handle;
  287. if( SFXFMODDevice::smFunc->FMOD_EventGroup_GetEventByIndex( mHandle, i, FMOD_EVENT_INFOONLY, &handle ) == FMOD_OK )
  288. {
  289. SFXFMODEvent* event = new SFXFMODEvent( this, handle );
  290. if( !isClientOnly() )
  291. event->assignId();
  292. event->registerObject( String::ToString( "%s_%s", getName(), FMODEventPathToTorqueName( event->getEventName() ).c_str() ) );
  293. if( isClientOnly() )
  294. Sim::getRootGroup()->addObject( event );
  295. }
  296. }
  297. // Load subgroups.
  298. for( U32 i = 0; i < numGroups; ++ i )
  299. {
  300. FMOD_EVENTGROUP* handle;
  301. if( SFXFMODDevice::smFunc->FMOD_EventGroup_GetGroupByIndex( mHandle, i, true, &handle ) == FMOD_OK )
  302. {
  303. SFXFMODEventGroup* group = new SFXFMODEventGroup( mProject, handle, this );
  304. if( !isClientOnly() )
  305. group->assignId();
  306. group->registerObject( String::ToString( "%s_%s", getName(), FMODEventPathToTorqueName( group->getGroupName() ).c_str() ) );
  307. if( isClientOnly() )
  308. Sim::getRootGroup()->addObject( group );
  309. group->_load();
  310. }
  311. }
  312. }
  313. //-----------------------------------------------------------------------------
  314. void SFXFMODEventGroup::_addEvent( SFXFMODEvent* event )
  315. {
  316. event->mSibling = mEvents;
  317. mEvents = event;
  318. mNumEvents ++;
  319. }
  320. //-----------------------------------------------------------------------------
  321. void SFXFMODEventGroup::_removeEvent( SFXFMODEvent* event )
  322. {
  323. if( mEvents == event )
  324. {
  325. mEvents = event->mSibling;
  326. event->mSibling = NULL;
  327. mNumEvents --;
  328. }
  329. else
  330. {
  331. SFXFMODEvent* p = mEvents;
  332. while( p != NULL && p->mSibling != event )
  333. p = p->mSibling;
  334. if( p )
  335. {
  336. p->mSibling = event->mSibling;
  337. event->mSibling = NULL;
  338. mNumEvents --;
  339. }
  340. }
  341. }
  342. //-----------------------------------------------------------------------------
  343. void SFXFMODEventGroup::_addGroup( SFXFMODEventGroup* group )
  344. {
  345. group->mSibling = mChildren;
  346. mChildren = group;
  347. mNumGroups ++;
  348. }
  349. //-----------------------------------------------------------------------------
  350. void SFXFMODEventGroup::_removeGroup( SFXFMODEventGroup* group )
  351. {
  352. if( mChildren == group )
  353. {
  354. mChildren = group->mSibling;
  355. group->mSibling = NULL;
  356. mNumGroups --;
  357. }
  358. else
  359. {
  360. SFXFMODEventGroup* p = mChildren;
  361. while( p != NULL && p->mSibling != group )
  362. p = p->mSibling;
  363. if( p )
  364. {
  365. p->mSibling = group->mSibling;
  366. group->mSibling = NULL;
  367. mNumGroups --;
  368. }
  369. }
  370. }
  371. //=============================================================================
  372. // Console Methods.
  373. //=============================================================================
  374. // MARK: ---- Console Methods ----
  375. //-----------------------------------------------------------------------------
  376. DefineEngineMethod( SFXFMODEventGroup, isDataLoaded, bool, (),,
  377. "Test whether the resource data for this group has been loaded.\n\n"
  378. "@return True if the resource data for this group is currently loaded.\n" )
  379. {
  380. return object->isDataLoaded();
  381. }
  382. //-----------------------------------------------------------------------------
  383. DefineEngineMethod( SFXFMODEventGroup, loadData, bool, ( bool loadStreams, bool loadSamples ), ( true, true ),
  384. "Load the resource data for this group, if it has not already been loaded (either directly "
  385. "or indirectly through a parent group).\n"
  386. "This method works recursively and thus data for direct and indirect child groups to this group will be "
  387. "loaded as well.\n\n"
  388. "@param loadStreams Whether to open streams.\n"
  389. "@param loadSamples Whether to load sample banks.\n"
  390. "@return True if the data has been successfully loaded; false otherwise.\n\n"
  391. "@see SFXFMODProject_resources" )
  392. {
  393. return object->loadData( loadSamples, loadStreams );
  394. }
  395. //-----------------------------------------------------------------------------
  396. DefineEngineMethod( SFXFMODEventGroup, freeData, void, (),,
  397. "Release the resource data for this group and its subgroups.\n\n"
  398. "@see SFXFMODProject_resources" )
  399. {
  400. object->freeData();
  401. }