ContainerUMX.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * ContainerUMX.cpp
  3. * ----------------
  4. * Purpose: UMX (Unreal Music) module ripper
  5. * Notes : Obviously, this code only rips modules from older Unreal Engine games, such as Unreal 1, Unreal Tournament 1 and Deus Ex.
  6. * Authors: OpenMPT Devs (inspired by code from http://wiki.beyondunreal.com/Legacy:Package_File_Format)
  7. * The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
  8. */
  9. #include "stdafx.h"
  10. #include "Loaders.h"
  11. #include "UMXTools.h"
  12. #include "Container.h"
  13. #include "Sndfile.h"
  14. OPENMPT_NAMESPACE_BEGIN
  15. CSoundFile::ProbeResult CSoundFile::ProbeFileHeaderUMX(MemoryFileReader file, const uint64 *pfilesize)
  16. {
  17. return UMX::ProbeFileHeader(file, pfilesize, "music");
  18. }
  19. bool UnpackUMX(std::vector<ContainerItem> &containerItems, FileReader &file, ContainerLoadingFlags loadFlags)
  20. {
  21. file.Rewind();
  22. containerItems.clear();
  23. UMX::FileHeader fileHeader;
  24. if(!file.ReadStruct(fileHeader) || !fileHeader.IsValid())
  25. return false;
  26. // Note that this can be a false positive, e.g. Unreal maps will have music and sound
  27. // in their name table because they usually import such files. However, it spares us
  28. // from wildly seeking through the file, as the name table is usually right at the
  29. // start of the file, so it is hopefully a good enough heuristic for our purposes.
  30. if(!UMX::FindNameTableEntry(file, fileHeader, "music"))
  31. return false;
  32. else if(!file.CanRead(fileHeader.GetMinimumAdditionalFileSize()))
  33. return false;
  34. else if(loadFlags == ContainerOnlyVerifyHeader)
  35. return true;
  36. const std::vector<std::string> names = UMX::ReadNameTable(file, fileHeader);
  37. const std::vector<int32> classes = UMX::ReadImportTable(file, fileHeader, names);
  38. // Read export table
  39. file.Seek(fileHeader.exportOffset);
  40. for(uint32 i = 0; i < fileHeader.exportCount && file.CanRead(8); i++)
  41. {
  42. auto [fileChunk, objName] = UMX::ReadExportTableEntry(file, fileHeader, classes, names, "music");
  43. if(!fileChunk.IsValid())
  44. continue;
  45. ContainerItem item;
  46. if(objName >= 0 && static_cast<std::size_t>(objName) < names.size())
  47. {
  48. item.name = mpt::ToUnicode(mpt::Charset::Windows1252, names[objName]);
  49. }
  50. item.file = fileChunk;
  51. containerItems.push_back(std::move(item));
  52. }
  53. return !containerItems.empty();
  54. }
  55. OPENMPT_NAMESPACE_END