1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-04 09:42:40 +02:00
vcmi/lib/filesystem/ResourcePath.h

228 lines
4.9 KiB
C++
Raw Normal View History

/*
* 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
2023-11-16 03:58:43 +02:00
* Video: .smk, .bik .mjpg .mpg .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,
2024-05-03 19:08:29 +02:00
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;
}
2023-09-01 23:57:25 +02:00
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>
2023-09-04 21:44:01 +02:00
class ResourcePathTempl : public ResourcePath
{
template <EResType>
friend class ResourcePathTempl;
ResourcePathTempl(const ResourcePath & copy)
:ResourcePath(copy)
{
type = Type;
}
2023-09-04 13:29:02 +02:00
ResourcePathTempl(const std::string & path)
:ResourcePath(path, Type)
{}
2023-09-04 13:29:02 +02:00
ResourcePathTempl(const JsonNode & name)
:ResourcePath(name, Type)
{}
public:
ResourcePathTempl()
:ResourcePath("", Type)
{}
2023-09-04 12:03:15 +02:00
static ResourcePathTempl fromResource(const ResourcePath & resource)
{
assert(Type == resource.getType());
return ResourcePathTempl(resource);
}
static ResourcePathTempl builtin(const std::string & filename)
{
2023-09-04 13:29:02 +02:00
return ResourcePathTempl(filename);
}
static ResourcePathTempl builtinTODO(const std::string & filename)
{
2023-09-04 13:29:02 +02:00
return ResourcePathTempl(filename);
}
static ResourcePathTempl fromJson(const JsonNode & path)
{
2023-09-04 13:29:02 +02:00
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>;
2023-09-01 23:57:25 +02:00
using VideoPath = ResourcePathTempl<EResType::VIDEO>;
2023-09-04 12:03:15 +02:00
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()));
}
};
}