CLodHandler.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #pragma once
  2. /*
  3. * CLodhandler.h, part of VCMI engine
  4. *
  5. * Authors: listed in file AUTHORS in main folder
  6. *
  7. * License: GNU General Public License v2.0 or later
  8. * Full text of license available in license.txt file, in main folder
  9. *
  10. */
  11. namespace boost
  12. {class mutex;}
  13. namespace NLoadHandlerHelp
  14. {
  15. const int dmHelp=0, dmNoExtractingMask=1;
  16. //std::string P1,P2,CurDir;
  17. const int fCHUNK = 50000;
  18. }
  19. struct LodEntry {
  20. char filename[16];
  21. ui32 offset; /* little endian */
  22. ui32 uncompressedSize; /* little endian */
  23. ui32 unused; /* little endian */
  24. ui32 size; /* little endian */
  25. };
  26. static inline char readChar(const ui8 * bufor, int &i)
  27. {
  28. return bufor[i++];
  29. }
  30. DLL_LINKAGE std::string readString(const ui8 * bufor, int &i);
  31. enum LodFileType{
  32. FILE_ANY,
  33. FILE_TEXT,
  34. FILE_ANIMATION,
  35. FILE_MASK,
  36. FILE_CAMPAIGN,
  37. FILE_MAP,
  38. FILE_FONT,
  39. FILE_GRAPHICS,
  40. FILE_OTHER
  41. };
  42. struct Entry
  43. {
  44. // Info extracted from LOD file
  45. std::string name,
  46. realName;//for external files - case\extension may not match
  47. int offset, //from beginning
  48. realSize, //size without compression
  49. size; //and with
  50. LodFileType type;// file type determined by extension
  51. bool operator == (const Entry & comp) const
  52. {
  53. return (type==comp.type || comp.type== FILE_ANY) && name==comp.name;
  54. }
  55. Entry(std::string con, LodFileType TYPE): name(con), type(TYPE){};
  56. Entry(std::string con): name(con){};
  57. Entry(){};
  58. };
  59. namespace boost
  60. {
  61. template<>
  62. struct hash<Entry> : public std::unary_function<Entry, std::size_t>
  63. {
  64. private:
  65. hash<std::string> stringHasher;
  66. public:
  67. std::size_t operator()(Entry const& en) const
  68. {
  69. //do NOT improve this hash function as we need same-name hash collisions for find to work properly
  70. return stringHasher(en.name);
  71. }
  72. };
  73. }
  74. class DLL_LINKAGE CLodHandler
  75. {
  76. std::map<std::string, LodFileType> extMap;// to convert extensions to file type
  77. std::ifstream LOD;
  78. ui32 totalFiles;
  79. boost::mutex *mutex;
  80. std::string myDir; //load files from this dir instead of .lod file
  81. void initEntry(Entry &e, const std::string name);
  82. int infs2(ui8 * in, int size, int realSize, ui8*& out, int wBits=15); //zlib fast handler
  83. public:
  84. //convert string to upper case and remove extension. If extension!=NULL -> it will be copied here (including dot)
  85. void convertName(std::string &filename, std::string *extension=NULL);
  86. boost::unordered_set<Entry> entries;
  87. CLodHandler();
  88. ~CLodHandler();
  89. void init(const std::string lodFile, const std::string dirName);
  90. std::string getFileName(std::string lodFile, LodFileType type=FILE_ANY);
  91. ui8 * giveFile(std::string defName, LodFileType type=FILE_ANY, int * length=NULL); //returns pointer to the decompressed data - it must be deleted when no longer needed!
  92. bool haveFile(std::string name, LodFileType type=FILE_ANY);//check if file is present in lod
  93. std::string getTextFile(std::string name, LodFileType type=FILE_TEXT); //extracts one file
  94. void extractFile(const std::string FName, const std::string name, LodFileType type=FILE_ANY); //extracts a specific file
  95. //unpack data from memory, input data will be deleted
  96. static ui8 * getUnpackedData(ui8 *data, size_t inputSize, int * outputSize);
  97. //loads given file, decompresses and returns
  98. static ui8 * getUnpackedFile(const std::string & path, int * sizeOut);
  99. };