1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
|
/*
* ResourcePath.h, part of VCMI engine
*
* Authors: listed in file AUTHORS in main folder
*
* License: GNU General Public License v2.0 or later
* Full text of license available in license.txt file, in main folder
*
*/
#pragma once
VCMI_LIB_NAMESPACE_BEGIN
class JsonNode;
class JsonSerializeFormat;
/**
* Specifies the resource type.
*
* Supported file extensions:
*
* Text: .txt
* Json: .json
* Animation: .def
* Mask: .msk .msg
* Campaign: .h3c
* Map: .h3m
* Font: .fnt
* Image: .bmp, .jpg, .pcx, .png, .tga
* Sound: .wav .82m
* Video: .smk, .bik .ogv .webm
* Music: .mp3, .ogg
* Archive: .lod, .snd, .vid .pac .zip
* Palette: .pal
* Savegame: .v*gm1
*/
enum class EResType
{
TEXT,
JSON,
ANIMATION,
MASK,
CAMPAIGN,
MAP,
BMP_FONT,
TTF_FONT,
IMAGE,
VIDEO,
VIDEO_LOW_QUALITY,
SOUND,
ARCHIVE_VID,
ARCHIVE_ZIP,
ARCHIVE_SND,
ARCHIVE_LOD,
PALETTE,
SAVEGAME,
DIRECTORY,
ERM,
ERT,
ERS,
LUA,
OTHER,
UNDEFINED,
};
/**
* A struct which identifies a resource clearly.
*/
class DLL_LINKAGE ResourcePath
{
protected:
/// Constructs resource path based on JsonNode and selected type. File extension is ignored
ResourcePath(const JsonNode & name, EResType type);
public:
/// Constructs resource path based on full name including extension
explicit ResourcePath(const std::string & fullName);
/// Constructs resource path based on filename and selected type. File extension is ignored
ResourcePath(const std::string & name, EResType type);
inline bool operator==(const ResourcePath & other) const
{
return name == other.name && type == other.type;
}
inline bool operator!=(const ResourcePath & other) const
{
return name != other.name || type != other.type;
}
inline bool operator<(const ResourcePath & other) const
{
if (type != other.type)
return type < other.type;
return name < other.name;
}
bool empty() const {return name.empty();}
std::string getName() const {return name;}
std::string getOriginalName() const {return originalName;}
EResType getType() const {return type;}
void serializeJson(JsonSerializeFormat & handler);
template <typename Handler> void serialize(Handler & h)
{
h & type;
h & name;
h & originalName;
}
protected:
/// Specifies the resource type. EResType::OTHER if not initialized.
/// Required to prevent conflicts if files with different types (e.g. text and image) have the same name.
EResType type;
/// Specifies the resource name. No extension so .pcx and .png can override each other, always in upper case.
std::string name;
/// name in original case
std::string originalName;
};
template<EResType Type>
class ResourcePathTempl : public ResourcePath
{
template <EResType>
friend class ResourcePathTempl;
ResourcePathTempl(const ResourcePath & copy)
:ResourcePath(copy)
{
type = Type;
}
ResourcePathTempl(const std::string & path)
:ResourcePath(path, Type)
{}
ResourcePathTempl(const JsonNode & name)
:ResourcePath(name, Type)
{}
public:
ResourcePathTempl()
:ResourcePath("", Type)
{}
static ResourcePathTempl fromResource(const ResourcePath & resource)
{
assert(Type == resource.getType());
return ResourcePathTempl(resource);
}
static ResourcePathTempl builtin(const std::string & filename)
{
return ResourcePathTempl(filename);
}
static ResourcePathTempl builtinTODO(const std::string & filename)
{
return ResourcePathTempl(filename);
}
static ResourcePathTempl fromJson(const JsonNode & path)
{
return ResourcePathTempl(path);
}
template<EResType Type2>
ResourcePathTempl<Type2> toType() const
{
ResourcePathTempl<Type2> result(static_cast<ResourcePath>(*this));
return result;
}
ResourcePathTempl addPrefix(const std::string & prefix) const
{
ResourcePathTempl result;
result.name = prefix + this->getName();
result.originalName = prefix + this->getOriginalName();
return result;
}
};
using AnimationPath = ResourcePathTempl<EResType::ANIMATION>;
using ImagePath = ResourcePathTempl<EResType::IMAGE>;
using TextPath = ResourcePathTempl<EResType::TEXT>;
using JsonPath = ResourcePathTempl<EResType::JSON>;
using VideoPath = ResourcePathTempl<EResType::VIDEO>;
using AudioPath = ResourcePathTempl<EResType::SOUND>;
namespace EResTypeHelper
{
/**
* Converts a extension string to a EResType enum object.
*
* @param extension The extension string e.g. .BMP, .PNG
* @return Returns a EResType enum object
*/
EResType getTypeFromExtension(std::string extension);
/**
* Gets the EResType as a string representation.
*
* @param type the EResType
* @return the type as a string representation
*/
std::string getEResTypeAsString(EResType type);
};
VCMI_LIB_NAMESPACE_END
namespace std
{
template <> struct hash<VCMI_LIB_WRAP_NAMESPACE(ResourcePath)>
{
size_t operator()(const VCMI_LIB_WRAP_NAMESPACE(ResourcePath) & resourceIdent) const
{
std::hash<int> intHasher;
std::hash<std::string> stringHasher;
return stringHasher(resourceIdent.getName()) ^ intHasher(static_cast<int>(resourceIdent.getType()));
}
};
}
|