2017-07-13 10:26:03 +02:00
|
|
|
/*
|
|
|
|
* ResourceID.cpp, 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
|
|
|
|
*
|
|
|
|
*/
|
2013-11-08 23:36:26 +03:00
|
|
|
#include "StdInc.h"
|
|
|
|
#include "ResourceID.h"
|
2016-01-26 15:51:38 +02:00
|
|
|
#include "FileInfo.h"
|
2013-11-08 23:36:26 +03:00
|
|
|
|
2013-11-14 16:21:09 +03:00
|
|
|
// trivial to_upper that completely ignores localization and only work with ASCII
|
|
|
|
// Technically not a problem since
|
|
|
|
// 1) Right now VCMI does not supports unicode in filenames on Win
|
|
|
|
// 2) Filesystem case-sensivity is only problem for H3 data which uses ASCII-only symbols
|
|
|
|
// for me (Ivan) this define gives notable decrease in loading times
|
|
|
|
// #define ENABLE_TRIVIAL_TOUPPER
|
|
|
|
|
|
|
|
#ifdef ENABLE_TRIVIAL_TOUPPER
|
|
|
|
static inline void toUpper(char & symbol)
|
|
|
|
{
|
|
|
|
static const int diff = 'a' - 'A';
|
|
|
|
if (symbol >= 'a' && symbol <= 'z')
|
|
|
|
symbol -= diff;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void toUpper(std::string & string)
|
|
|
|
{
|
|
|
|
for (char & symbol : string)
|
|
|
|
toUpper(symbol);
|
|
|
|
}
|
2016-01-26 15:51:38 +02:00
|
|
|
#else
|
|
|
|
static inline void toUpper(std::string & string)
|
|
|
|
{
|
|
|
|
boost::to_upper(string);
|
|
|
|
}
|
2013-11-14 16:21:09 +03:00
|
|
|
#endif
|
|
|
|
|
2016-01-26 15:51:38 +02:00
|
|
|
static inline EResType::Type readType(const std::string& name)
|
|
|
|
{
|
|
|
|
return EResTypeHelper::getTypeFromExtension(FileInfo::GetExtension(name).to_string());
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline std::string readName(std::string name)
|
|
|
|
{
|
|
|
|
const auto dotPos = name.find_last_of('.');
|
|
|
|
|
2016-09-11 15:02:45 +02:00
|
|
|
//do not cut "extension" of directory name
|
|
|
|
auto delimPos = name.find_last_of('/');
|
|
|
|
if(delimPos == std::string::npos)
|
|
|
|
delimPos = name.find_last_of('\\');
|
|
|
|
|
|
|
|
if((delimPos == std::string::npos || delimPos < dotPos) && dotPos != std::string::npos)
|
2016-10-02 11:39:25 +02:00
|
|
|
{
|
|
|
|
auto type = EResTypeHelper::getTypeFromExtension(name.substr(dotPos));
|
|
|
|
if(type != EResType::OTHER)
|
|
|
|
name.resize(dotPos);
|
|
|
|
}
|
2016-01-26 15:51:38 +02:00
|
|
|
|
|
|
|
toUpper(name);
|
|
|
|
|
|
|
|
return name;
|
|
|
|
}
|
2013-11-14 16:21:09 +03:00
|
|
|
|
2016-01-29 16:16:14 +02:00
|
|
|
#if 0
|
2013-11-08 23:36:26 +03:00
|
|
|
ResourceID::ResourceID()
|
|
|
|
:type(EResType::OTHER)
|
|
|
|
{
|
|
|
|
}
|
2016-01-29 16:16:14 +02:00
|
|
|
#endif
|
2013-11-08 23:36:26 +03:00
|
|
|
|
2016-01-29 17:08:44 +02:00
|
|
|
ResourceID::ResourceID(std::string name_):
|
|
|
|
type{readType(name_)},
|
2016-01-26 15:51:38 +02:00
|
|
|
name{readName(std::move(name_))}
|
2016-01-29 17:08:44 +02:00
|
|
|
{}
|
2013-11-08 23:36:26 +03:00
|
|
|
|
2016-01-29 17:08:44 +02:00
|
|
|
ResourceID::ResourceID(std::string name_, EResType::Type type_):
|
|
|
|
type{type_},
|
2016-01-26 15:51:38 +02:00
|
|
|
name{readName(std::move(name_))}
|
2016-01-29 17:08:44 +02:00
|
|
|
{}
|
2016-01-29 16:16:14 +02:00
|
|
|
#if 0
|
2013-11-08 23:36:26 +03:00
|
|
|
std::string ResourceID::getName() const
|
|
|
|
{
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
EResType::Type ResourceID::getType() const
|
|
|
|
{
|
|
|
|
return type;
|
|
|
|
}
|
2016-01-29 16:16:14 +02:00
|
|
|
|
2013-11-08 23:36:26 +03:00
|
|
|
void ResourceID::setName(std::string name)
|
|
|
|
{
|
2015-02-06 09:51:32 +02:00
|
|
|
// setName shouldn't be used if type is UNDEFINED
|
|
|
|
assert(type != EResType::UNDEFINED);
|
|
|
|
|
2013-11-08 23:36:26 +03:00
|
|
|
this->name = std::move(name);
|
|
|
|
|
|
|
|
size_t dotPos = this->name.find_last_of("/.");
|
|
|
|
|
2015-01-26 15:40:49 +02:00
|
|
|
if(dotPos != std::string::npos && this->name[dotPos] == '.'
|
|
|
|
&& this->type == EResTypeHelper::getTypeFromExtension(this->name.substr(dotPos)))
|
|
|
|
{
|
2013-11-08 23:36:26 +03:00
|
|
|
this->name.erase(dotPos);
|
2015-01-26 15:40:49 +02:00
|
|
|
}
|
2013-11-08 23:36:26 +03:00
|
|
|
|
2013-11-14 16:21:09 +03:00
|
|
|
toUpper(this->name);
|
2013-11-08 23:36:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void ResourceID::setType(EResType::Type type)
|
|
|
|
{
|
|
|
|
this->type = type;
|
|
|
|
}
|
2016-01-26 15:51:38 +02:00
|
|
|
#endif
|
2013-11-08 23:36:26 +03:00
|
|
|
EResType::Type EResTypeHelper::getTypeFromExtension(std::string extension)
|
|
|
|
{
|
2013-11-14 16:21:09 +03:00
|
|
|
toUpper(extension);
|
2013-11-08 23:36:26 +03:00
|
|
|
|
|
|
|
static const std::map<std::string, EResType::Type> stringToRes =
|
2014-10-02 18:43:46 +03:00
|
|
|
{
|
|
|
|
{".TXT", EResType::TEXT},
|
|
|
|
{".JSON", EResType::TEXT},
|
|
|
|
{".DEF", EResType::ANIMATION},
|
|
|
|
{".MSK", EResType::MASK},
|
|
|
|
{".MSG", EResType::MASK},
|
|
|
|
{".H3C", EResType::CAMPAIGN},
|
|
|
|
{".H3M", EResType::MAP},
|
|
|
|
{".FNT", EResType::BMP_FONT},
|
|
|
|
{".TTF", EResType::TTF_FONT},
|
|
|
|
{".BMP", EResType::IMAGE},
|
|
|
|
{".JPG", EResType::IMAGE},
|
|
|
|
{".PCX", EResType::IMAGE},
|
|
|
|
{".PNG", EResType::IMAGE},
|
|
|
|
{".TGA", EResType::IMAGE},
|
|
|
|
{".WAV", EResType::SOUND},
|
|
|
|
{".82M", EResType::SOUND},
|
|
|
|
{".SMK", EResType::VIDEO},
|
|
|
|
{".BIK", EResType::VIDEO},
|
|
|
|
{".MJPG", EResType::VIDEO},
|
|
|
|
{".MPG", EResType::VIDEO},
|
|
|
|
{".AVI", EResType::VIDEO},
|
|
|
|
{".MP3", EResType::MUSIC},
|
|
|
|
{".OGG", EResType::MUSIC},
|
|
|
|
{".FLAC", EResType::MUSIC},
|
|
|
|
{".ZIP", EResType::ARCHIVE_ZIP},
|
|
|
|
{".LOD", EResType::ARCHIVE_LOD},
|
|
|
|
{".PAC", EResType::ARCHIVE_LOD},
|
|
|
|
{".VID", EResType::ARCHIVE_VID},
|
|
|
|
{".SND", EResType::ARCHIVE_SND},
|
|
|
|
{".PAL", EResType::PALETTE},
|
|
|
|
{".VCGM1", EResType::CLIENT_SAVEGAME},
|
|
|
|
{".VSGM1", EResType::SERVER_SAVEGAME},
|
|
|
|
{".ERM", EResType::ERM},
|
|
|
|
{".ERT", EResType::ERT},
|
2016-02-09 19:20:03 +02:00
|
|
|
{".ERS", EResType::ERS},
|
|
|
|
{".VMAP", EResType::MAP}
|
2014-10-02 18:43:46 +03:00
|
|
|
};
|
2013-11-08 23:36:26 +03:00
|
|
|
|
|
|
|
auto iter = stringToRes.find(extension);
|
|
|
|
if (iter == stringToRes.end())
|
|
|
|
return EResType::OTHER;
|
|
|
|
return iter->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string EResTypeHelper::getEResTypeAsString(EResType::Type type)
|
|
|
|
{
|
2014-10-02 18:43:46 +03:00
|
|
|
#define MAP_ENUM(value) {EResType::value, #value},
|
2013-11-08 23:36:26 +03:00
|
|
|
|
2014-10-02 18:43:46 +03:00
|
|
|
static const std::map<EResType::Type, std::string> stringToRes =
|
|
|
|
{
|
2013-11-08 23:36:26 +03:00
|
|
|
MAP_ENUM(TEXT)
|
|
|
|
MAP_ENUM(ANIMATION)
|
|
|
|
MAP_ENUM(MASK)
|
|
|
|
MAP_ENUM(CAMPAIGN)
|
|
|
|
MAP_ENUM(MAP)
|
|
|
|
MAP_ENUM(BMP_FONT)
|
|
|
|
MAP_ENUM(TTF_FONT)
|
|
|
|
MAP_ENUM(IMAGE)
|
|
|
|
MAP_ENUM(VIDEO)
|
|
|
|
MAP_ENUM(SOUND)
|
|
|
|
MAP_ENUM(MUSIC)
|
|
|
|
MAP_ENUM(ARCHIVE_ZIP)
|
|
|
|
MAP_ENUM(ARCHIVE_LOD)
|
|
|
|
MAP_ENUM(ARCHIVE_SND)
|
|
|
|
MAP_ENUM(ARCHIVE_VID)
|
|
|
|
MAP_ENUM(PALETTE)
|
|
|
|
MAP_ENUM(CLIENT_SAVEGAME)
|
|
|
|
MAP_ENUM(SERVER_SAVEGAME)
|
|
|
|
MAP_ENUM(DIRECTORY)
|
|
|
|
MAP_ENUM(ERM)
|
|
|
|
MAP_ENUM(ERT)
|
|
|
|
MAP_ENUM(ERS)
|
2014-10-02 18:43:46 +03:00
|
|
|
MAP_ENUM(OTHER)
|
|
|
|
};
|
2013-11-08 23:36:26 +03:00
|
|
|
|
|
|
|
#undef MAP_ENUM
|
|
|
|
|
|
|
|
auto iter = stringToRes.find(type);
|
|
|
|
assert(iter != stringToRes.end());
|
|
|
|
|
|
|
|
return iter->second;
|
|
|
|
}
|