CLodHandler.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. template<>
  66. struct boost::hash<Entry> : public std::unary_function<Entry, std::size_t> {
  67. private:
  68. boost::hash<std::string> stringHasher;
  69. public:
  70. std::size_t operator()(Entry const& en) const
  71. {
  72. //do NOT improve this hash function as we need same-name hash collisions for find to work properly
  73. return stringHasher(en.name);
  74. }
  75. };
  76. class DLL_EXPORT CLodHandler
  77. {
  78. std::map<std::string, LodFileType> extMap;// to convert extensions to file type
  79. std::ifstream LOD;
  80. unsigned int totalFiles;
  81. boost::mutex *mutex;
  82. std::string myDir; //load files from this dir instead of .lod file
  83. void initEntry(Entry &e, const std::string name);
  84. int infs2(unsigned char * in, int size, int realSize, unsigned char*& out, int wBits=15); //zlib fast handler
  85. public:
  86. boost::unordered_set<Entry> entries;
  87. CLodHandler();
  88. ~CLodHandler();
  89. void init(const std::string lodFile, const std::string dirName);
  90. unsigned char * giveFile(const std::string defName, LodFileType type=FILE_ANY, int * length=NULL); //returns pointer to the decompressed data - it must be deleted when no longer needed!
  91. bool haveFile(const std::string name, LodFileType type=FILE_ANY);//check if file is present in lod
  92. std::string getTextFile(const std::string name, LodFileType type=FILE_TEXT); //extracts one file
  93. void extractFile(const std::string FName, const std::string name); //extracts a specific file
  94. static unsigned char * getUnpackedFile(const std::string & path, int * sizeOut); //loads given file, decompresses and returns
  95. };
  96. #endif // __CLODHANDLER_H__