SoundBanks.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. #include "SoundBanks.h"
  2. #include "..\..\common_h\FileService.h"
  3. SoundBanks::SoundBank::SoundBank()
  4. {
  5. header = null;
  6. data = null;
  7. dataSize = 0;
  8. refCount = 0;
  9. nameHash = 0;
  10. }
  11. SoundBanks::SoundBank::~SoundBank()
  12. {
  13. #ifdef _XBOX
  14. XPhysicalFree(data);
  15. #else
  16. delete data;
  17. #endif
  18. data = null;
  19. }
  20. SoundBanks::SoundBanks() : soundBank(_FL_)
  21. {
  22. fs = (IFileService *)api->GetService("FileService");
  23. Assert(fs);
  24. SoundBankFileId::CheckMachine();
  25. }
  26. SoundBanks::~SoundBanks()
  27. {
  28. soundBank.DelAllWithPointers();
  29. }
  30. //Загрузить звуковой банк
  31. bool SoundBanks::LoadSoundBank(const char * path)
  32. {
  33. if(string::IsEmpty(path))
  34. {
  35. return false;
  36. }
  37. //Вычленяем имя файла
  38. strBufferPath = path;
  39. strBufferTitle.Empty();
  40. strBufferTitle.GetFileTitle(strBufferPath);
  41. strBufferTitle.Lower();
  42. dword hash = string::HashNoCase(strBufferTitle.c_str());
  43. //Ищем среди загруженных
  44. for(long i = 0; i < soundBank; i++)
  45. {
  46. SoundBank * sb = soundBank[i];
  47. if(sb->nameHash == hash)
  48. {
  49. if(sb->name == strBufferTitle)
  50. {
  51. sb->refCount++;
  52. return true;
  53. }
  54. }
  55. }
  56. //Нужно загружать с диска
  57. #ifndef _XBOX
  58. strBufferPath = "Resource\\sounds\\";
  59. #else
  60. strBufferPath = "";
  61. #endif
  62. strBufferPath += path;
  63. strBufferPath.AddExtention(".ssb");
  64. IDataFile * file = fs->OpenDataFile(strBufferPath.c_str(), file_open_any, _FL_);
  65. if(!file)
  66. {
  67. api->Trace("SoundService: Can't open sound bank file: %s", strBufferPath.c_str());
  68. return false;
  69. }
  70. dword fileSize = file->Size();
  71. if(fileSize < sizeof(SoundBankFileId) + sizeof(SoundBankFileHeader) + sizeof(SoundBankFileSound))
  72. {
  73. api->Trace("SoundService: Can't load sound bank file: %s,\n incorrect file size...", strBufferPath.c_str());
  74. file->Release();
  75. return false;
  76. }
  77. #ifndef GAME_RUSSIAN
  78. //Загружаем идентификационный заголовок
  79. SoundBankFileId sbfId;
  80. if(file->Read(&sbfId, sizeof(sbfId)) != sizeof(sbfId))
  81. {
  82. api->Trace("SoundService: Can't load sound bank file: %s,\n io error...", strBufferPath.c_str());
  83. file->Release();
  84. return false;
  85. }
  86. //Проверяем идентификатор
  87. if(!sbfId.CheckId())
  88. {
  89. api->Trace("SoundService: Invalidate sound bank file id: %s", strBufferPath.c_str());
  90. file->Release();
  91. return false;
  92. }
  93. if(!sbfId.CheckVer())
  94. {
  95. api->Trace("SoundService: Invalidate sound bank file version: %s", strBufferPath.c_str());
  96. file->Release();
  97. return false;
  98. }
  99. if(sbfId.GetPCChankOffset() < sizeof(SoundBankFileId) + sizeof(SoundBankFileHeader) + sizeof(SoundBankFileSound))
  100. {
  101. api->Trace("SoundService: Can't load sound bank file: %s,\n incorrect file size...", strBufferPath.c_str());
  102. file->Release();
  103. return false;
  104. }
  105. //В зависимости от платформы определяем что сколько и как загружать
  106. #ifdef _XBOX
  107. //Загружаем файл куском в память
  108. fileSize = sbfId.GetXboxChankSize();
  109. byte * data = (byte *)XPhysicalAlloc(fileSize, MAXULONG_PTR, 2048, PAGE_READWRITE);
  110. Assert(data != null);
  111. #else
  112. //Определяем сколько нужно прочитать инфы
  113. dword skipBytes = sbfId.GetXboxChankSize();
  114. if(file->Read(null, skipBytes) != skipBytes)
  115. {
  116. api->Trace("SoundService: Can't load sound bank file: %s,\n io error...", strBufferPath.c_str());
  117. file->Release();
  118. return false;
  119. }
  120. fileSize = sbfId.GetPCChankSize(fileSize);
  121. byte * data = NEW byte[fileSize];
  122. #endif
  123. #else
  124. byte * data = NEW byte[fileSize];
  125. #endif
  126. if(file->Read(data, fileSize) != fileSize)
  127. {
  128. #ifdef _XBOX
  129. XPhysicalFree(data);
  130. #else
  131. delete data;
  132. #endif
  133. api->Trace("SoundService: Can't load sound bank file: %s,\n io error...", strBufferPath.c_str());
  134. file->Release();
  135. return false;
  136. }
  137. //Пытаемся подготовить данные к использованию
  138. SoundBankFileHeader * header = (SoundBankFileHeader *)data;
  139. if(!header->Restore(fileSize))
  140. {
  141. #ifdef _XBOX
  142. XPhysicalFree(data);
  143. #else
  144. delete data;
  145. #endif
  146. api->Trace("SoundService: Sound bank file is damaged: %s", strBufferPath.c_str());
  147. file->Release();
  148. return false;
  149. }
  150. file->Release();
  151. file = null;
  152. //Декодировать волны
  153. #ifdef GAME_RUSSIAN
  154. for(dword i = 0; i < header->wavesCount; i++)
  155. {
  156. header->waves[i].DecodeWaveData();
  157. }
  158. #endif
  159. //Добавляем новую учётную запись
  160. SoundBank * sb = NEW SoundBank();
  161. sb->header = header;
  162. sb->data = data;
  163. sb->dataSize = fileSize;
  164. sb->refCount = 1;
  165. sb->nameHash = hash;
  166. sb->name = strBufferTitle;
  167. soundBank.Add(sb);
  168. return true;
  169. }
  170. //Удалить звуковой банк
  171. void SoundBanks::ReleaseSoundBank(const char * path)
  172. {
  173. path = GetSoundBankNativeName(path);
  174. for(dword i = 0; i < soundBank.Size(); i++)
  175. {
  176. SoundBank * sb = soundBank[i];
  177. if(sb->name.c_str() == path)
  178. {
  179. sb->refCount--;
  180. if(sb->refCount <= 0)
  181. {
  182. //Удаляем описание
  183. delete sb;
  184. //Удаляем запись
  185. soundBank.DelIndex(i);
  186. return;
  187. }
  188. }
  189. }
  190. }
  191. //Получить внутренний указатель на имя звукового банка, который сохранён в звуках
  192. const char * SoundBanks::GetSoundBankNativeName(const char * path)
  193. {
  194. //Вычленяем имя файла
  195. strBufferPath = path;
  196. strBufferTitle.Empty();
  197. strBufferTitle.GetFileTitle(strBufferPath);
  198. strBufferTitle.Lower();
  199. dword hash = string::HashNoCase(strBufferTitle.c_str());
  200. //Ищем среди загруженных
  201. for(dword i = 0; i < soundBank.Size(); i++)
  202. {
  203. SoundBank * sb = soundBank[i];
  204. if(sb->nameHash == hash)
  205. {
  206. if(sb->name == strBufferTitle)
  207. {
  208. return sb->name.c_str();
  209. }
  210. }
  211. }
  212. return null;
  213. }
  214. //Найти звук по имени
  215. SoundBankFileSound * SoundBanks::FindSound(const ConstString & name, const char ** sbankName)
  216. {
  217. if(name.IsEmpty())
  218. {
  219. return null;
  220. }
  221. return FindSound(name.c_str(), name.Hash(), name.Len(), null, sbankName);
  222. }
  223. //Найти звук по имени с расчитаными параметрами имени
  224. SoundBankFileSound * SoundBanks::FindSound(const char * name, dword nameHash, dword nameLen, SoundBankFileHeader ** header, const char ** sbankName)
  225. {
  226. for(long i = 0; i < soundBank; i++)
  227. {
  228. SoundBank * sb = soundBank[i];
  229. dword entryIndex = nameHash & sb->header->mask;
  230. SoundBankFileSound * snd = sb->header->entry[entryIndex];
  231. for(; snd; snd = snd->next)
  232. {
  233. if(snd->nameHash == nameHash && snd->nameLen == nameLen)
  234. {
  235. if(string::IsEqual(snd->name, name))
  236. {
  237. if(header)
  238. {
  239. *header = sb->header;
  240. }
  241. if(sbankName)
  242. {
  243. *sbankName = sb->name.c_str();
  244. }
  245. return snd;
  246. }
  247. }
  248. }
  249. }
  250. return null;
  251. }