2023-08-23 14:07:50 +02:00
|
|
|
/*
|
|
|
|
* 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:
|
|
|
|
*
|
2023-09-01 23:26:14 +02:00
|
|
|
* Text: .txt
|
|
|
|
* Json: .json
|
2023-08-23 14:07:50 +02:00
|
|
|
* 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
|
2023-08-23 14:07:50 +02:00
|
|
|
* Music: .mp3, .ogg
|
|
|
|
* Archive: .lod, .snd, .vid .pac .zip
|
|
|
|
* Palette: .pal
|
|
|
|
* Savegame: .v*gm1
|
|
|
|
*/
|
|
|
|
enum class EResType
|
|
|
|
{
|
|
|
|
TEXT,
|
2023-09-01 23:26:14 +02:00
|
|
|
JSON,
|
2023-08-23 14:07:50 +02:00
|
|
|
ANIMATION,
|
|
|
|
MASK,
|
|
|
|
CAMPAIGN,
|
|
|
|
MAP,
|
|
|
|
BMP_FONT,
|
|
|
|
TTF_FONT,
|
|
|
|
IMAGE,
|
|
|
|
VIDEO,
|
2024-05-03 19:08:29 +02:00
|
|
|
VIDEO_LOW_QUALITY,
|
2023-08-23 14:07:50 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-08-23 14:07:50 +02:00
|
|
|
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);
|
|
|
|
|
2024-01-20 20:34:51 +02:00
|
|
|
template <typename Handler> void serialize(Handler & h)
|
2023-08-23 14:07:50 +02:00
|
|
|
{
|
|
|
|
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
|
2023-08-23 14:07:50 +02:00
|
|
|
{
|
|
|
|
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-08-23 14:07:50 +02:00
|
|
|
|
2023-09-04 13:29:02 +02:00
|
|
|
ResourcePathTempl(const JsonNode & name)
|
|
|
|
:ResourcePath(name, Type)
|
|
|
|
{}
|
|
|
|
|
|
|
|
public:
|
2023-08-23 14:07:50 +02:00
|
|
|
ResourcePathTempl()
|
|
|
|
:ResourcePath("", Type)
|
|
|
|
{}
|
|
|
|
|
2023-09-04 12:03:15 +02:00
|
|
|
static ResourcePathTempl fromResource(const ResourcePath & resource)
|
|
|
|
{
|
|
|
|
assert(Type == resource.getType());
|
|
|
|
return ResourcePathTempl(resource);
|
|
|
|
}
|
|
|
|
|
2023-08-23 14:07:50 +02:00
|
|
|
static ResourcePathTempl builtin(const std::string & filename)
|
|
|
|
{
|
2023-09-04 13:29:02 +02:00
|
|
|
return ResourcePathTempl(filename);
|
2023-08-23 14:07:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static ResourcePathTempl builtinTODO(const std::string & filename)
|
|
|
|
{
|
2023-09-04 13:29:02 +02:00
|
|
|
return ResourcePathTempl(filename);
|
2023-08-23 14:07:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static ResourcePathTempl fromJson(const JsonNode & path)
|
|
|
|
{
|
2023-09-04 13:29:02 +02:00
|
|
|
return ResourcePathTempl(path);
|
2023-08-23 14:07:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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>;
|
2023-09-01 23:26:14 +02:00
|
|
|
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>;
|
2023-08-23 14:07:50 +02:00
|
|
|
|
|
|
|
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()));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|