CLodHandler.h 3.5 KB

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