ResourceID.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #pragma once
  2. /*
  3. * ResourceID.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. /**
  12. * Specifies the resource type.
  13. *
  14. * Supported file extensions:
  15. *
  16. * Text: .txt .json
  17. * Animation: .def
  18. * Mask: .msk .msg
  19. * Campaign: .h3c
  20. * Map: .h3m
  21. * Font: .fnt
  22. * Image: .bmp, .jpg, .pcx, .png, .tga
  23. * Sound: .wav .82m
  24. * Video: .smk, .bik .mjpg .mpg
  25. * Music: .mp3, .ogg
  26. * Archive: .lod, .snd, .vid .pac .zip
  27. * Palette: .pal
  28. * Savegame: .v*gm1
  29. */
  30. namespace EResType
  31. {
  32. enum Type
  33. {
  34. TEXT,
  35. ANIMATION,
  36. MASK,
  37. CAMPAIGN,
  38. MAP,
  39. BMP_FONT,
  40. TTF_FONT,
  41. IMAGE,
  42. VIDEO,
  43. SOUND,
  44. MUSIC,
  45. ARCHIVE_VID,
  46. ARCHIVE_ZIP,
  47. ARCHIVE_SND,
  48. ARCHIVE_LOD,
  49. PALETTE,
  50. CLIENT_SAVEGAME,
  51. SERVER_SAVEGAME,
  52. DIRECTORY,
  53. ERM,
  54. ERT,
  55. ERS,
  56. OTHER
  57. };
  58. }
  59. /**
  60. * A struct which identifies a resource clearly.
  61. */
  62. class DLL_LINKAGE ResourceID
  63. {
  64. public:
  65. /**
  66. * Default c-tor.
  67. */
  68. ResourceID();
  69. /**
  70. * Ctor. Can be used to create identifier for resource loading using one parameter
  71. *
  72. * @param fullName The resource name including extension.
  73. */
  74. explicit ResourceID(std::string fullName);
  75. /**
  76. * Ctor.
  77. *
  78. * @param name The resource name.
  79. * @param type The resource type. A constant from the enumeration EResType.
  80. */
  81. ResourceID(std::string name, EResType::Type type);
  82. /**
  83. * Compares this object with a another resource identifier.
  84. *
  85. * @param other The other resource identifier.
  86. * @return Returns true if both are equally, false if not.
  87. */
  88. inline bool operator==(ResourceID const & other) const
  89. {
  90. return name == other.name && type == other.type;
  91. }
  92. std::string getName() const;
  93. EResType::Type getType() const;
  94. void setName(std::string name);
  95. void setType(EResType::Type type);
  96. private:
  97. /** Specifies the resource name. No extension so .pcx and .png can override each other, always in upper case. **/
  98. std::string name;
  99. /**
  100. * Specifies the resource type. EResType::OTHER if not initialized.
  101. * Required to prevent conflicts if files with different types (e.g. text and image) have the same name.
  102. */
  103. EResType::Type type;
  104. };
  105. namespace std
  106. {
  107. template <> struct hash<ResourceID>
  108. {
  109. size_t operator()(const ResourceID & resourceIdent) const
  110. {
  111. std::hash<int> intHasher;
  112. std::hash<std::string> stringHasher;
  113. return stringHasher(resourceIdent.getName()) ^ intHasher(static_cast<int>(resourceIdent.getType()));
  114. }
  115. };
  116. }
  117. /**
  118. * A helper class which provides a functionality to convert extension strings to EResTypes.
  119. */
  120. class DLL_LINKAGE EResTypeHelper
  121. {
  122. public:
  123. /**
  124. * Converts a extension string to a EResType enum object.
  125. *
  126. * @param extension The extension string e.g. .BMP, .PNG
  127. * @return Returns a EResType enum object
  128. */
  129. static EResType::Type getTypeFromExtension(std::string extension);
  130. /**
  131. * Gets the EResType as a string representation.
  132. *
  133. * @param type the EResType
  134. * @return the type as a string representation
  135. */
  136. static std::string getEResTypeAsString(EResType::Type type);
  137. };