/src/tools/map_extractor/loadlib.cpp

https://gitlab.com/tkrokli/TrinityCore_434 · C++ · 82 lines · 53 code · 11 blank · 18 comment · 8 complexity · 593f6b79cdb9b4f43290d9251d1f81df MD5 · raw file

  1. /*
  2. * Copyright (C) 2008-2015 TrinityCore <http://www.trinitycore.org/>
  3. * Copyright (C) 2005-2011 MaNGOS <http://getmangos.com/>
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the
  7. * Free Software Foundation; either version 2 of the License, or (at your
  8. * option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #define _CRT_SECURE_NO_DEPRECATE
  19. #include "loadlib.h"
  20. #include <cstdio>
  21. u_map_fcc MverMagic = { {'R','E','V','M'} };
  22. FileLoader::FileLoader()
  23. {
  24. data = 0;
  25. data_size = 0;
  26. version = 0;
  27. }
  28. FileLoader::~FileLoader()
  29. {
  30. free();
  31. }
  32. bool FileLoader::loadFile(HANDLE mpq, char* filename, bool log)
  33. {
  34. free();
  35. HANDLE file;
  36. if (!SFileOpenFileEx(mpq, filename, SFILE_OPEN_PATCHED_FILE, &file))
  37. {
  38. if (log)
  39. printf("No such file %s\n", filename);
  40. return false;
  41. }
  42. data_size = SFileGetFileSize(file, NULL);
  43. data = new uint8[data_size];
  44. SFileReadFile(file, data, data_size, NULL/*bytesRead*/, NULL);
  45. if (prepareLoadedData())
  46. {
  47. SFileCloseFile(file);
  48. return true;
  49. }
  50. printf("Error loading %s\n", filename);
  51. SFileCloseFile(file);
  52. free();
  53. return false;
  54. }
  55. bool FileLoader::prepareLoadedData()
  56. {
  57. // Check version
  58. version = (file_MVER *) data;
  59. if (version->fcc != MverMagic.fcc)
  60. return false;
  61. if (version->ver != FILE_FORMAT_VERSION)
  62. return false;
  63. return true;
  64. }
  65. void FileLoader::free()
  66. {
  67. if (data) delete[] data;
  68. data = 0;
  69. data_size = 0;
  70. version = 0;
  71. }