ResourceID.h 3.3 KB

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