CLoadFile.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * CLoadFile.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "CLoadFile.h"
  12. VCMI_LIB_NAMESPACE_BEGIN
  13. CLoadFile::CLoadFile(const boost::filesystem::path & fname, ESerializationVersion minimalVersion)
  14. : serializer(this)
  15. {
  16. openNextFile(fname, minimalVersion);
  17. }
  18. //must be instantiated in .cpp file for access to complete types of all member fields
  19. CLoadFile::~CLoadFile() = default;
  20. int CLoadFile::read(std::byte * data, unsigned size)
  21. {
  22. sfile->read(reinterpret_cast<char *>(data), size);
  23. return size;
  24. }
  25. void CLoadFile::openNextFile(const boost::filesystem::path & fname, ESerializationVersion minimalVersion)
  26. {
  27. assert(!serializer.reverseEndianess);
  28. assert(minimalVersion <= ESerializationVersion::CURRENT);
  29. try
  30. {
  31. fName = fname.string();
  32. sfile = std::make_unique<std::fstream>(fname.c_str(), std::ios::in | std::ios::binary);
  33. sfile->exceptions(std::ifstream::failbit | std::ifstream::badbit); //we throw a lot anyway
  34. if(!(*sfile))
  35. THROW_FORMAT("Error: cannot open to read %s!", fName);
  36. //we can read
  37. char buffer[4];
  38. sfile->read(buffer, 4);
  39. if(std::memcmp(buffer, "VCMI", 4) != 0)
  40. THROW_FORMAT("Error: not a VCMI file(%s)!", fName);
  41. serializer & serializer.version;
  42. if(serializer.version < minimalVersion)
  43. THROW_FORMAT("Error: too old file format (%s)!", fName);
  44. if(serializer.version > ESerializationVersion::CURRENT)
  45. {
  46. logGlobal->warn("Warning format version mismatch: found %d when current is %d! (file %s)\n", vstd::to_underlying(serializer.version), vstd::to_underlying(ESerializationVersion::CURRENT), fName);
  47. auto * versionptr = reinterpret_cast<char *>(&serializer.version);
  48. std::reverse(versionptr, versionptr + 4);
  49. logGlobal->warn("Version number reversed is %x, checking...", vstd::to_underlying(serializer.version));
  50. if(serializer.version == ESerializationVersion::CURRENT)
  51. {
  52. logGlobal->warn("%s seems to have different endianness! Entering reversing mode.", fname.string());
  53. serializer.reverseEndianess = true;
  54. }
  55. else
  56. THROW_FORMAT("Error: too new file format (%s)!", fName);
  57. }
  58. }
  59. catch(...)
  60. {
  61. clear(); //if anything went wrong, we delete file and rethrow
  62. throw;
  63. }
  64. }
  65. void CLoadFile::reportState(vstd::CLoggerBase * out)
  66. {
  67. out->debug("CLoadFile");
  68. if(!!sfile && *sfile)
  69. out->debug("\tOpened %s Position: %d", fName, sfile->tellg());
  70. }
  71. void CLoadFile::clear()
  72. {
  73. sfile = nullptr;
  74. fName.clear();
  75. serializer.version = ESerializationVersion::NONE;
  76. }
  77. void CLoadFile::checkMagicBytes(const std::string &text)
  78. {
  79. std::string loaded = text;
  80. read(reinterpret_cast<std::byte*>(loaded.data()), text.length());
  81. if(loaded != text)
  82. throw std::runtime_error("Magic bytes doesn't match!");
  83. }
  84. VCMI_LIB_NAMESPACE_END