ResourceID.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #include "StdInc.h"
  2. #include "ResourceID.h"
  3. #include "CFileInfo.h"
  4. ResourceID::ResourceID()
  5. :type(EResType::OTHER)
  6. {
  7. }
  8. ResourceID::ResourceID(std::string name)
  9. {
  10. CFileInfo info(std::move(name));
  11. setName(info.getStem());
  12. setType(info.getType());
  13. }
  14. ResourceID::ResourceID(std::string name, EResType::Type type)
  15. {
  16. setName(std::move(name));
  17. setType(type);
  18. }
  19. std::string ResourceID::getName() const
  20. {
  21. return name;
  22. }
  23. EResType::Type ResourceID::getType() const
  24. {
  25. return type;
  26. }
  27. void ResourceID::setName(std::string name)
  28. {
  29. this->name = std::move(name);
  30. size_t dotPos = this->name.find_last_of("/.");
  31. if(dotPos != std::string::npos && this->name[dotPos] == '.')
  32. this->name.erase(dotPos);
  33. // strangely enough but this line takes 40-50% of filesystem loading time
  34. boost::to_upper(this->name);
  35. }
  36. void ResourceID::setType(EResType::Type type)
  37. {
  38. this->type = type;
  39. }
  40. EResType::Type EResTypeHelper::getTypeFromExtension(std::string extension)
  41. {
  42. boost::to_upper(extension);
  43. static const std::map<std::string, EResType::Type> stringToRes =
  44. boost::assign::map_list_of
  45. (".TXT", EResType::TEXT)
  46. (".JSON", EResType::TEXT)
  47. (".DEF", EResType::ANIMATION)
  48. (".MSK", EResType::MASK)
  49. (".MSG", EResType::MASK)
  50. (".H3C", EResType::CAMPAIGN)
  51. (".H3M", EResType::MAP)
  52. (".FNT", EResType::BMP_FONT)
  53. (".TTF", EResType::TTF_FONT)
  54. (".BMP", EResType::IMAGE)
  55. (".JPG", EResType::IMAGE)
  56. (".PCX", EResType::IMAGE)
  57. (".PNG", EResType::IMAGE)
  58. (".TGA", EResType::IMAGE)
  59. (".WAV", EResType::SOUND)
  60. (".82M", EResType::SOUND)
  61. (".SMK", EResType::VIDEO)
  62. (".BIK", EResType::VIDEO)
  63. (".MJPG", EResType::VIDEO)
  64. (".MPG", EResType::VIDEO)
  65. (".AVI", EResType::VIDEO)
  66. (".MP3", EResType::MUSIC)
  67. (".OGG", EResType::MUSIC)
  68. (".ZIP", EResType::ARCHIVE_ZIP)
  69. (".LOD", EResType::ARCHIVE_LOD)
  70. (".PAC", EResType::ARCHIVE_LOD)
  71. (".VID", EResType::ARCHIVE_VID)
  72. (".SND", EResType::ARCHIVE_SND)
  73. (".PAL", EResType::PALETTE)
  74. (".VCGM1", EResType::CLIENT_SAVEGAME)
  75. (".VSGM1", EResType::SERVER_SAVEGAME)
  76. (".ERM", EResType::ERM)
  77. (".ERT", EResType::ERT)
  78. (".ERS", EResType::ERS);
  79. auto iter = stringToRes.find(extension);
  80. if (iter == stringToRes.end())
  81. return EResType::OTHER;
  82. return iter->second;
  83. }
  84. std::string EResTypeHelper::getEResTypeAsString(EResType::Type type)
  85. {
  86. #define MAP_ENUM(value) (EResType::value, #value)
  87. static const std::map<EResType::Type, std::string> stringToRes = boost::assign::map_list_of
  88. MAP_ENUM(TEXT)
  89. MAP_ENUM(ANIMATION)
  90. MAP_ENUM(MASK)
  91. MAP_ENUM(CAMPAIGN)
  92. MAP_ENUM(MAP)
  93. MAP_ENUM(BMP_FONT)
  94. MAP_ENUM(TTF_FONT)
  95. MAP_ENUM(IMAGE)
  96. MAP_ENUM(VIDEO)
  97. MAP_ENUM(SOUND)
  98. MAP_ENUM(MUSIC)
  99. MAP_ENUM(ARCHIVE_ZIP)
  100. MAP_ENUM(ARCHIVE_LOD)
  101. MAP_ENUM(ARCHIVE_SND)
  102. MAP_ENUM(ARCHIVE_VID)
  103. MAP_ENUM(PALETTE)
  104. MAP_ENUM(CLIENT_SAVEGAME)
  105. MAP_ENUM(SERVER_SAVEGAME)
  106. MAP_ENUM(DIRECTORY)
  107. MAP_ENUM(ERM)
  108. MAP_ENUM(ERT)
  109. MAP_ENUM(ERS)
  110. MAP_ENUM(OTHER);
  111. #undef MAP_ENUM
  112. auto iter = stringToRes.find(type);
  113. assert(iter != stringToRes.end());
  114. return iter->second;
  115. }