CSndHandler.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. CMediaHandler::~CMediaHandler()
  16. {
  17. entries.clear();
  18. fimap.clear();
  19. mfile->close();
  20. delete mfile;
  21. }
  22. CMediaHandler::CMediaHandler(std::string fname)
  23. {
  24. try //c-tor of mapped_file_source throws exception on failure
  25. {
  26. mfile = new boost::iostreams::mapped_file_source(fname);
  27. } HANDLE_EXCEPTIONC(tlog1 << "Cannot open " << fname << std::endl)
  28. if (!mfile->is_open()) //just in case
  29. {
  30. tlog1 << "Cannot open " << fname << std::endl;
  31. throw std::string("Cannot open ")+fname;
  32. }
  33. }
  34. void CMediaHandler::extract(int index, std::string dstfile) //saves selected file
  35. {
  36. std::ofstream out(dstfile.c_str(),std::ios_base::binary);
  37. const char *data = mfile->data();
  38. out.write(&data[entries[index].offset], entries[index].size);
  39. out.close();
  40. }
  41. void CMediaHandler::extract(std::string srcfile, std::string dstfile, bool caseSens) //saves selected file
  42. {
  43. if (caseSens)
  44. {
  45. for (size_t i=0;i<entries.size();++i)
  46. {
  47. if (entries[i].name==srcfile)
  48. extract(i,dstfile);
  49. }
  50. }
  51. else
  52. {
  53. std::transform(srcfile.begin(),srcfile.end(),srcfile.begin(),tolower);
  54. for (size_t i=0;i<entries.size();++i)
  55. {
  56. if (entries[i].name==srcfile)
  57. {
  58. std::string por = entries[i].name;
  59. std::transform(por.begin(),por.end(),por.begin(),tolower);
  60. if (por==srcfile)
  61. extract(i,dstfile);
  62. }
  63. }
  64. }
  65. }
  66. #if 0
  67. // unused and not sure what it's supposed to do
  68. MemberFile CMediaHandler::getFile(std::string name)
  69. {
  70. MemberFile ret;
  71. std::transform(name.begin(),name.end(),name.begin(),tolower);
  72. for (size_t i=0;i<entries.size();++i)
  73. {
  74. if (entries[i].name==name)
  75. {
  76. std::string por = entries[i].name;
  77. std::transform(por.begin(),por.end(),por.begin(),tolower);
  78. if (por==name)
  79. {
  80. ret.length=entries[i].size;
  81. file.seekg(entries[i].offset,std::ios_base::beg);
  82. ret.ifs=&file;
  83. return ret;
  84. }
  85. }
  86. }
  87. return ret;
  88. }
  89. #endif
  90. const char * CMediaHandler::extract (int index, int & size)
  91. {
  92. size = entries[index].size;
  93. const char *data = mfile->data();
  94. return &data[entries[index].offset];
  95. }
  96. const char * CMediaHandler::extract (std::string srcName, int &size)
  97. {
  98. int index;
  99. std::map<std::string, int>::iterator fit;
  100. if ((fit = fimap.find(srcName)) != fimap.end())
  101. {
  102. index = fit->second;
  103. return this->extract(index, size);
  104. }
  105. size = 0;
  106. return NULL;
  107. }
  108. CSndHandler::CSndHandler(std::string fname) : CMediaHandler(fname)
  109. {
  110. const unsigned char *data = (const unsigned char *)mfile->data();
  111. unsigned int numFiles = SDL_SwapLE32(*(Uint32 *)&data[0]);
  112. struct soundEntry *se = (struct soundEntry *)&data[4];
  113. for (unsigned int i=0; i<numFiles; i++, se++)
  114. {
  115. Entry entry;
  116. char *p;
  117. // Reassemble the filename and its extension
  118. entry.name = se->filename;
  119. entry.name += '.';
  120. p = se->filename;
  121. while(*p) p++;
  122. p++;
  123. entry.name += p;
  124. entry.offset = SDL_SwapLE32(se->offset);
  125. entry.size = SDL_SwapLE32(se->size);
  126. entries.push_back(entry);
  127. fimap[entry.name] = i;
  128. }
  129. }
  130. CVidHandler::CVidHandler(std::string fname) : CMediaHandler(fname)
  131. {
  132. const unsigned char *data = (const unsigned char *)mfile->data();
  133. unsigned int numFiles = SDL_SwapLE32(*(Uint32 *)&data[0]);
  134. struct videoEntry *ve = (struct videoEntry *)&data[4];
  135. for (unsigned int i=0; i<numFiles; i++, ve++)
  136. {
  137. Entry entry;
  138. entry.name = ve->filename;
  139. entry.offset = SDL_SwapLE32(ve->offset);
  140. // There is no size, so check where the next file is
  141. if (i == numFiles - 1) {
  142. entry.size = mfile->size() - entry.offset;
  143. } else {
  144. struct videoEntry *ve_next = ve+1;
  145. entry.size = SDL_SwapLE32(ve_next->offset) - entry.offset;
  146. }
  147. entries.push_back(entry);
  148. fimap[entry.name] = i;
  149. }
  150. }