CSndHandler.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. #include "../stdafx.h"
  2. #include <fstream>
  3. #include "CSndHandler.h"
  4. #include <boost/iostreams/device/mapped_file.hpp>
  5. #include <SDL_endian.h>
  6. /*
  7. * CSndHandler.cpp, part of VCMI engine
  8. *
  9. * Authors: listed in file AUTHORS in main folder
  10. *
  11. * License: GNU General Public License v2.0 or later
  12. * Full text of license available in license.txt file, in main folder
  13. *
  14. */
  15. /* Media file are kept in container files. We map these files in
  16. * memory, parse them and create an index of them to easily retrieve
  17. * the data+size of the objects. */
  18. CMediaHandler::~CMediaHandler()
  19. {
  20. std::vector<boost::iostreams::mapped_file_source *>::iterator it;
  21. entries.clear();
  22. fimap.clear();
  23. for (it=mfiles.begin() ; it < mfiles.end(); it++ ) {
  24. (*it)->close();
  25. delete *it;
  26. }
  27. }
  28. boost::iostreams::mapped_file_source *CMediaHandler::add_file(std::string fname)
  29. {
  30. boost::iostreams::mapped_file_source *mfile;
  31. try //c-tor of mapped_file_source throws exception on failure
  32. {
  33. mfile = new boost::iostreams::mapped_file_source(fname);
  34. } HANDLE_EXCEPTIONC(tlog1 << "Cannot open " << fname << std::endl)
  35. if (!mfile->is_open()) //just in case
  36. {
  37. tlog1 << "Cannot open " << fname << std::endl;
  38. throw std::string("Cannot open ")+fname;
  39. return NULL;
  40. } else {
  41. mfiles.push_back(mfile);
  42. return mfile;
  43. }
  44. }
  45. void CMediaHandler::extract(int index, std::string dstfile) //saves selected file
  46. {
  47. std::ofstream out(dstfile.c_str(),std::ios_base::binary);
  48. Entry &entry = entries[index];
  49. out.write(entry.data, entry.size);
  50. out.close();
  51. }
  52. void CMediaHandler::extract(std::string srcfile, std::string dstfile, bool caseSens) //saves selected file
  53. {
  54. srcfile.erase(srcfile.find_last_of('.'));
  55. if (caseSens)
  56. {
  57. for (size_t i=0;i<entries.size();++i)
  58. {
  59. if (entries[i].name==srcfile)
  60. extract(i,dstfile);
  61. }
  62. }
  63. else
  64. {
  65. std::transform(srcfile.begin(),srcfile.end(),srcfile.begin(),tolower);
  66. for (size_t i=0;i<entries.size();++i)
  67. {
  68. if (entries[i].name==srcfile)
  69. {
  70. std::string por = entries[i].name;
  71. std::transform(por.begin(),por.end(),por.begin(),tolower);
  72. if (por==srcfile)
  73. extract(i,dstfile);
  74. }
  75. }
  76. }
  77. }
  78. #if 0
  79. // unused and not sure what it's supposed to do
  80. MemberFile CMediaHandler::getFile(std::string name)
  81. {
  82. MemberFile ret;
  83. std::transform(name.begin(),name.end(),name.begin(),tolower);
  84. for (size_t i=0;i<entries.size();++i)
  85. {
  86. if (entries[i].name==name)
  87. {
  88. std::string por = entries[i].name;
  89. std::transform(por.begin(),por.end(),por.begin(),tolower);
  90. if (por==name)
  91. {
  92. ret.length=entries[i].size;
  93. file.seekg(entries[i].offset,std::ios_base::beg);
  94. ret.ifs=&file;
  95. return ret;
  96. }
  97. }
  98. }
  99. return ret;
  100. }
  101. #endif
  102. const char * CMediaHandler::extract (int index, int & size)
  103. {
  104. Entry &entry = entries[index];
  105. size = entry.size;
  106. return entry.data;
  107. }
  108. const char * CMediaHandler::extract (std::string srcName, int &size)
  109. {
  110. int index;
  111. srcName.erase(srcName.find_last_of('.'));
  112. std::map<std::string, int>::iterator fit;
  113. if ((fit = fimap.find(srcName)) != fimap.end())
  114. {
  115. index = fit->second;
  116. return this->extract(index, size);
  117. }
  118. size = 0;
  119. return NULL;
  120. }
  121. void CSndHandler::add_file(std::string fname)
  122. {
  123. boost::iostreams::mapped_file_source *mfile = CMediaHandler::add_file(fname);
  124. if (!mfile)
  125. /* File doesn't exist. Silently skip it.*/
  126. return;
  127. const char *data = mfile->data();
  128. unsigned int numFiles = SDL_SwapLE32(*(Uint32 *)&data[0]);
  129. struct soundEntry *se = (struct soundEntry *)&data[4];
  130. for (unsigned int i=0; i<numFiles; i++, se++)
  131. {
  132. Entry entry;
  133. entry.name = se->filename;
  134. entry.offset = SDL_SwapLE32(se->offset);
  135. entry.size = SDL_SwapLE32(se->size);
  136. entry.data = mfile->data() + entry.offset;
  137. entries.push_back(entry);
  138. fimap[entry.name] = i;
  139. }
  140. }
  141. void CVidHandler::add_file(std::string fname)
  142. {
  143. boost::iostreams::mapped_file_source *mfile = CMediaHandler::add_file(fname);
  144. if (!mfile)
  145. /* File doesn't exist. Silently skip it.*/
  146. return;
  147. if(mfile->size() < 48)
  148. {
  149. tlog1 << fname << " doesn't contain needed data!\n";
  150. return;
  151. }
  152. const unsigned char *data = (const unsigned char *)mfile->data();
  153. unsigned int numFiles = SDL_SwapLE32(*(Uint32 *)&data[0]);
  154. struct videoEntry *ve = (struct videoEntry *)&data[4];
  155. for (unsigned int i=0; i<numFiles; i++, ve++)
  156. {
  157. Entry entry;
  158. entry.name = ve->filename;
  159. entry.offset = SDL_SwapLE32(ve->offset);
  160. entry.name.erase(entry.name.find_last_of('.'));
  161. // There is no size, so check where the next file is
  162. if (i == numFiles - 1) {
  163. entry.size = mfile->size() - entry.offset;
  164. } else {
  165. struct videoEntry *ve_next = ve+1;
  166. entry.size = SDL_SwapLE32(ve_next->offset) - entry.offset;
  167. }
  168. entry.data = mfile->data() + entry.offset;
  169. entries.push_back(entry);
  170. fimap[entry.name] = i;
  171. }
  172. }