CLodHandler.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. DLL_EXPORT int readNormalNr (const unsigned char * bufor, int pos, int bytCon = 4, bool cyclic = false);
  35. DLL_EXPORT char readChar(const unsigned char * bufor, int &i);
  36. DLL_EXPORT std::string readString(const unsigned char * bufor, int &i);
  37. enum LodFileType{
  38. FILE_ANY,
  39. FILE_TEXT,
  40. FILE_ANIMATION,
  41. FILE_MASK,
  42. FILE_CAMPAIGN,
  43. FILE_MAP,
  44. FILE_FONT,
  45. FILE_GRAPHICS,
  46. FILE_OTHER
  47. };
  48. struct Entry
  49. {
  50. // Info extracted from LOD file
  51. std::string name,
  52. realName;//for external files - case\extension may not match
  53. int offset, //from beginning
  54. realSize, //size without compression
  55. size; //and with
  56. LodFileType type;// file type determined by extension
  57. bool operator == (const Entry & comp) const
  58. {
  59. return (type==comp.type || comp.type== FILE_ANY) && name==comp.name;
  60. }
  61. Entry(std::string con, LodFileType TYPE): name(con), type(TYPE){};
  62. Entry(std::string con): name(con){};
  63. Entry(){};
  64. };
  65. namespace boost
  66. {
  67. template<>
  68. struct hash<Entry> : public std::unary_function<Entry, std::size_t>
  69. {
  70. private:
  71. hash<std::string> stringHasher;
  72. public:
  73. std::size_t operator()(Entry const& en) const
  74. {
  75. //do NOT improve this hash function as we need same-name hash collisions for find to work properly
  76. return stringHasher(en.name);
  77. }
  78. };
  79. }
  80. class DLL_EXPORT CLodHandler
  81. {
  82. std::map<std::string, LodFileType> extMap;// to convert extensions to file type
  83. std::ifstream LOD;
  84. unsigned int totalFiles;
  85. boost::mutex *mutex;
  86. std::string myDir; //load files from this dir instead of .lod file
  87. void initEntry(Entry &e, const std::string name);
  88. int infs2(unsigned char * in, int size, int realSize, unsigned char*& out, int wBits=15); //zlib fast handler
  89. public:
  90. //convert string to upper case and remove extension. If extension!=NULL -> it will be copied here (including dot)
  91. void convertName(std::string &filename, std::string *extension=NULL);
  92. boost::unordered_set<Entry> entries;
  93. CLodHandler();
  94. ~CLodHandler();
  95. void init(const std::string lodFile, const std::string dirName);
  96. std::string getFileName(std::string lodFile, LodFileType type=FILE_ANY);
  97. 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!
  98. bool haveFile(std::string name, LodFileType type=FILE_ANY);//check if file is present in lod
  99. std::string getTextFile(std::string name, LodFileType type=FILE_TEXT); //extracts one file
  100. void extractFile(const std::string FName, const std::string name); //extracts a specific file
  101. static unsigned char * getUnpackedFile(const std::string & path, int * sizeOut); //loads given file, decompresses and returns
  102. };
  103. #endif // __CLODHANDLER_H__