2017-07-13 10:26:03 +02:00
|
|
|
/*
|
2023-08-23 14:07:50 +02:00
|
|
|
* ResourcePath.cpp, part of VCMI engine
|
2017-07-13 10:26:03 +02:00
|
|
|
*
|
|
|
|
* 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"
|
2023-08-23 14:07:50 +02:00
|
|
|
#include "ResourcePath.h"
|
2016-01-26 15:51:38 +02:00
|
|
|
#include "FileInfo.h"
|
2013-11-08 23:36:26 +03:00
|
|
|
|
2023-08-23 14:07:50 +02:00
|
|
|
#include "../serializer/JsonDeserializer.h"
|
|
|
|
#include "../serializer/JsonSerializer.h"
|
2013-11-14 16:21:09 +03:00
|
|
|
|
2023-08-23 14:07:50 +02:00
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
2013-11-14 16:21:09 +03:00
|
|
|
|
2016-01-26 15:51:38 +02:00
|
|
|
static inline void toUpper(std::string & string)
|
|
|
|
{
|
|
|
|
boost::to_upper(string);
|
|
|
|
}
|
2013-11-14 16:21:09 +03:00
|
|
|
|
2023-08-23 14:07:50 +02:00
|
|
|
static inline EResType readType(const std::string& name)
|
2016-01-26 15:51:38 +02:00
|
|
|
{
|
|
|
|
return EResTypeHelper::getTypeFromExtension(FileInfo::GetExtension(name).to_string());
|
|
|
|
}
|
|
|
|
|
2023-08-11 23:56:20 +02:00
|
|
|
static inline std::string readName(std::string name, bool uppercase)
|
2016-01-26 15:51:38 +02:00
|
|
|
{
|
|
|
|
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
|
|
|
|
2023-08-11 23:56:20 +02:00
|
|
|
if(uppercase)
|
2023-08-11 01:46:22 +02:00
|
|
|
toUpper(name);
|
2016-01-26 15:51:38 +02:00
|
|
|
|
|
|
|
return name;
|
|
|
|
}
|
2013-11-14 16:21:09 +03:00
|
|
|
|
2023-08-23 14:07:50 +02:00
|
|
|
ResourcePath::ResourcePath(const std::string & name_):
|
|
|
|
type(readType(name_)),
|
|
|
|
name(readName(name_, true)),
|
|
|
|
originalName(readName(name_, false))
|
2016-01-29 17:08:44 +02:00
|
|
|
{}
|
2013-11-08 23:36:26 +03:00
|
|
|
|
2023-08-23 14:07:50 +02:00
|
|
|
ResourcePath::ResourcePath(const std::string & name_, EResType type_):
|
|
|
|
type(type_),
|
|
|
|
name(readName(name_, true)),
|
|
|
|
originalName(readName(name_, false))
|
2016-01-29 17:08:44 +02:00
|
|
|
{}
|
2013-11-08 23:36:26 +03:00
|
|
|
|
2023-08-23 14:07:50 +02:00
|
|
|
ResourcePath::ResourcePath(const JsonNode & name, EResType type):
|
|
|
|
type(type),
|
|
|
|
name(readName(name.String(), true)),
|
|
|
|
originalName(readName(name.String(), false))
|
2013-11-08 23:36:26 +03:00
|
|
|
{
|
|
|
|
}
|
2016-01-29 16:16:14 +02:00
|
|
|
|
2023-08-23 14:07:50 +02:00
|
|
|
void ResourcePath::serializeJson(JsonSerializeFormat & handler)
|
2013-11-08 23:36:26 +03:00
|
|
|
{
|
2023-08-23 14:07:50 +02:00
|
|
|
if (!handler.saving)
|
2015-01-26 15:40:49 +02:00
|
|
|
{
|
2023-08-23 14:07:50 +02:00
|
|
|
JsonNode const & node = handler.getCurrent();
|
|
|
|
|
|
|
|
if (node.isString())
|
|
|
|
{
|
|
|
|
name = readName(node.String(), true);
|
|
|
|
originalName = readName(node.String(), false);
|
|
|
|
return;
|
|
|
|
}
|
2015-01-26 15:40:49 +02:00
|
|
|
}
|
2013-11-08 23:36:26 +03:00
|
|
|
|
2023-08-23 14:07:50 +02:00
|
|
|
handler.serializeInt("type", type);
|
|
|
|
handler.serializeString("name", name);
|
|
|
|
handler.serializeString("originalName", originalName);
|
2013-11-08 23:36:26 +03:00
|
|
|
}
|
|
|
|
|
2023-08-23 14:07:50 +02:00
|
|
|
EResType EResTypeHelper::getTypeFromExtension(std::string extension)
|
2013-11-08 23:36:26 +03:00
|
|
|
{
|
2013-11-14 16:21:09 +03:00
|
|
|
toUpper(extension);
|
2013-11-08 23:36:26 +03:00
|
|
|
|
2023-08-23 14:07:50 +02:00
|
|
|
static const std::map<std::string, EResType> stringToRes =
|
2014-10-02 18:43:46 +03:00
|
|
|
{
|
|
|
|
{".TXT", EResType::TEXT},
|
2023-09-04 12:03:15 +02:00
|
|
|
{".JSON", EResType::JSON},
|
2014-10-02 18:43:46 +03:00
|
|
|
{".DEF", EResType::ANIMATION},
|
|
|
|
{".MSK", EResType::MASK},
|
|
|
|
{".MSG", EResType::MASK},
|
|
|
|
{".H3C", EResType::CAMPAIGN},
|
|
|
|
{".H3M", EResType::MAP},
|
2023-08-09 01:54:09 +02:00
|
|
|
{".TUT", EResType::MAP},
|
2014-10-02 18:43:46 +03:00
|
|
|
{".FNT", EResType::BMP_FONT},
|
|
|
|
{".TTF", EResType::TTF_FONT},
|
|
|
|
{".BMP", EResType::IMAGE},
|
2019-02-04 11:14:59 +02:00
|
|
|
{".GIF", EResType::IMAGE},
|
2014-10-02 18:43:46 +03:00
|
|
|
{".JPG", EResType::IMAGE},
|
|
|
|
{".PCX", EResType::IMAGE},
|
|
|
|
{".PNG", EResType::IMAGE},
|
|
|
|
{".TGA", EResType::IMAGE},
|
|
|
|
{".WAV", EResType::SOUND},
|
|
|
|
{".82M", EResType::SOUND},
|
2023-05-10 20:18:20 +02:00
|
|
|
{".MP3", EResType::SOUND},
|
|
|
|
{".OGG", EResType::SOUND},
|
|
|
|
{".FLAC", EResType::SOUND},
|
2014-10-02 18:43:46 +03:00
|
|
|
{".SMK", EResType::VIDEO},
|
|
|
|
{".BIK", EResType::VIDEO},
|
|
|
|
{".MJPG", EResType::VIDEO},
|
|
|
|
{".MPG", EResType::VIDEO},
|
|
|
|
{".AVI", EResType::VIDEO},
|
2023-11-16 03:58:43 +02:00
|
|
|
{".WEBM", EResType::VIDEO},
|
2014-10-02 18:43:46 +03:00
|
|
|
{".ZIP", EResType::ARCHIVE_ZIP},
|
|
|
|
{".LOD", EResType::ARCHIVE_LOD},
|
|
|
|
{".PAC", EResType::ARCHIVE_LOD},
|
|
|
|
{".VID", EResType::ARCHIVE_VID},
|
|
|
|
{".SND", EResType::ARCHIVE_SND},
|
|
|
|
{".PAL", EResType::PALETTE},
|
2023-07-09 14:30:47 +02:00
|
|
|
{".VSGM1", EResType::SAVEGAME},
|
2014-10-02 18:43:46 +03:00
|
|
|
{".ERM", EResType::ERM},
|
|
|
|
{".ERT", EResType::ERT},
|
2016-02-09 19:20:03 +02:00
|
|
|
{".ERS", EResType::ERS},
|
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
|
|
|
{".VMAP", EResType::MAP},
|
2023-04-09 01:24:40 +02:00
|
|
|
{".VCMP", EResType::CAMPAIGN},
|
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
|
|
|
{".VERM", EResType::ERM},
|
|
|
|
{".LUA", EResType::LUA}
|
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;
|
|
|
|
}
|
|
|
|
|
2023-08-23 14:07:50 +02:00
|
|
|
std::string EResTypeHelper::getEResTypeAsString(EResType type)
|
2013-11-08 23:36:26 +03:00
|
|
|
{
|
2014-10-02 18:43:46 +03:00
|
|
|
#define MAP_ENUM(value) {EResType::value, #value},
|
2013-11-08 23:36:26 +03:00
|
|
|
|
2023-08-23 14:07:50 +02:00
|
|
|
static const std::map<EResType, std::string> stringToRes =
|
2014-10-02 18:43:46 +03:00
|
|
|
{
|
2013-11-08 23:36:26 +03:00
|
|
|
MAP_ENUM(TEXT)
|
2023-09-04 12:03:15 +02:00
|
|
|
MAP_ENUM(JSON)
|
2013-11-08 23:36:26 +03:00
|
|
|
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(ARCHIVE_ZIP)
|
|
|
|
MAP_ENUM(ARCHIVE_LOD)
|
|
|
|
MAP_ENUM(ARCHIVE_SND)
|
|
|
|
MAP_ENUM(ARCHIVE_VID)
|
|
|
|
MAP_ENUM(PALETTE)
|
2023-07-09 14:30:47 +02:00
|
|
|
MAP_ENUM(SAVEGAME)
|
2013-11-08 23:36:26 +03:00
|
|
|
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;
|
|
|
|
}
|
2022-07-26 15:07:42 +02:00
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_END
|