1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-11-25 22:42:04 +02:00

- vcmi can now calculate crc32 checksum of a file

- reorganized internal filesystem structure - all files from one mod are
now grouped in same FS node
- modhandler will now calculate crc32 checksum for each mod
- modhandler now knows validation status of each mod

todo - use checksum to determine mods that have not changed since last
start and disable validation for them.
This commit is contained in:
Ivan Savenko
2013-11-08 20:36:26 +00:00
parent ee6cdbeffe
commit dd73573c5c
21 changed files with 575 additions and 400 deletions

View File

@@ -17,50 +17,97 @@
CFilesystemList * CResourceHandler::resourceLoader = nullptr;
CFilesystemList * CResourceHandler::initialLoader = nullptr;
ResourceID::ResourceID()
:type(EResType::OTHER)
CFilesystemGenerator::CFilesystemGenerator(std::string prefix):
filesystem(new CFilesystemList()),
prefix(prefix)
{
}
ResourceID::ResourceID(std::string name)
CFilesystemGenerator::TLoadFunctorMap CFilesystemGenerator::genFunctorMap()
{
CFileInfo info(std::move(name));
setName(info.getStem());
setType(info.getType());
TLoadFunctorMap map;
map["map"] = boost::bind(&CFilesystemGenerator::loadJsonMap, this, _1, _2);
map["dir"] = boost::bind(&CFilesystemGenerator::loadDirectory, this, _1, _2);
map["lod"] = boost::bind(&CFilesystemGenerator::loadArchive<EResType::ARCHIVE_LOD>, this, _1, _2);
map["snd"] = boost::bind(&CFilesystemGenerator::loadArchive<EResType::ARCHIVE_SND>, this, _1, _2);
map["vid"] = boost::bind(&CFilesystemGenerator::loadArchive<EResType::ARCHIVE_VID>, this, _1, _2);
map["zip"] = boost::bind(&CFilesystemGenerator::loadZipArchive, this, _1, _2);
return map;
}
ResourceID::ResourceID(std::string name, EResType::Type type)
void CFilesystemGenerator::loadConfig(const JsonNode & config)
{
setName(std::move(name));
setType(type);
for(auto & mountPoint : config.Struct())
{
for(auto & entry : mountPoint.second.Vector())
{
CStopWatch timer;
logGlobal->debugStream() << "\t\tLoading resource at " << prefix + entry["path"].String();
auto map = genFunctorMap();
auto functor = map.find(entry["type"].String());
if (functor != map.end())
{
functor->second(mountPoint.first, entry);
logGlobal->debugStream() << "Resource loaded in " << timer.getDiff() << " ms.";
}
else
{
logGlobal->errorStream() << "Unknown filesystem format: " << functor->first;
}
}
}
}
std::string ResourceID::getName() const
CFilesystemList * CFilesystemGenerator::getFilesystem()
{
return name;
return filesystem;
}
EResType::Type ResourceID::getType() const
void CFilesystemGenerator::loadDirectory(const std::string &mountPoint, const JsonNode & config)
{
return type;
std::string URI = prefix + config["path"].String();
int depth = 16;
if (!config["depth"].isNull())
depth = config["depth"].Float();
ResourceID resID(URI, EResType::DIRECTORY);
for(auto & loader : CResourceHandler::getInitial()->getResourcesWithName(resID))
{
auto filename = loader->getResourceName(resID);
filesystem->addLoader(new CFilesystemLoader(mountPoint, *filename, depth), false);
}
}
void ResourceID::setName(std::string name)
void CFilesystemGenerator::loadZipArchive(const std::string &mountPoint, const JsonNode & config)
{
this->name = std::move(name);
size_t dotPos = this->name.find_last_of("/.");
if(dotPos != std::string::npos && this->name[dotPos] == '.')
this->name.erase(dotPos);
// strangely enough but this line takes 40-50% of filesystem loading time
boost::to_upper(this->name);
std::string URI = prefix + config["path"].String();
auto filename = CResourceHandler::getInitial()->getResourceName(ResourceID(URI, EResType::ARCHIVE_ZIP));
if (filename)
filesystem->addLoader(new CZipLoader(mountPoint, *filename), false);
}
void ResourceID::setType(EResType::Type type)
template<EResType::Type archiveType>
void CFilesystemGenerator::loadArchive(const std::string &mountPoint, const JsonNode & config)
{
this->type = type;
std::string URI = prefix + config["path"].String();
auto filename = CResourceHandler::getInitial()->getResourceName(ResourceID(URI, archiveType));
if (filename)
filesystem->addLoader(new CArchiveLoader(mountPoint, *filename), false);
}
void CFilesystemGenerator::loadJsonMap(const std::string &mountPoint, const JsonNode & config)
{
std::string URI = prefix + config["path"].String();
auto filename = CResourceHandler::getInitial()->getResourceName(ResourceID(URI, EResType::TEXT));
if (filename)
{
auto configData = CResourceHandler::getInitial()->load(ResourceID(URI, EResType::TEXT))->readAll();
const JsonNode config((char*)configData.first.get(), configData.second);
filesystem->addLoader(new CMappedFileLoader(mountPoint, config), false);
}
}
void CResourceHandler::clear()
@@ -69,90 +116,6 @@ void CResourceHandler::clear()
delete initialLoader;
}
EResType::Type EResTypeHelper::getTypeFromExtension(std::string extension)
{
boost::to_upper(extension);
static const std::map<std::string, EResType::Type> stringToRes =
boost::assign::map_list_of
(".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)
(".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)
(".ERS", EResType::ERS);
auto iter = stringToRes.find(extension);
if (iter == stringToRes.end())
return EResType::OTHER;
return iter->second;
}
std::string EResTypeHelper::getEResTypeAsString(EResType::Type type)
{
#define MAP_ENUM(value) (EResType::value, #value)
static const std::map<EResType::Type, std::string> stringToRes = boost::assign::map_list_of
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)
MAP_ENUM(OTHER);
#undef MAP_ENUM
auto iter = stringToRes.find(type);
assert(iter != stringToRes.end());
return iter->second;
}
void CResourceHandler::initialize()
{
//recurse only into specific directories
@@ -174,7 +137,6 @@ void CResourceHandler::initialize()
//temporary filesystem that will be used to initialize main one.
//used to solve several case-sensivity issues like Mp3 vs MP3
initialLoader = new CFilesystemList;
resourceLoader = new CFilesystemList;
for (auto & path : VCMIDirs::get().dataPaths())
initialLoader->addLoader(new CFilesystemLoader("", path, 0, true), false);
@@ -188,89 +150,36 @@ void CResourceHandler::initialize()
recurseInDir("MODS", 2); // look for mods. Depth 2 is required for now but won't cause speed issues if no mods present
}
void CResourceHandler::loadDirectory(const std::string &prefix, const std::string &mountPoint, const JsonNode & config)
CFilesystemList * CResourceHandler::get()
{
std::string URI = prefix + config["path"].String();
int depth = 16;
if (!config["depth"].isNull())
depth = config["depth"].Float();
ResourceID resID(URI, EResType::DIRECTORY);
for(auto & loader : initialLoader->getResourcesWithName(resID))
{
auto filename = loader->getResourceName(resID);
resourceLoader->addLoader(new CFilesystemLoader(mountPoint, *filename, depth), false);
}
assert(resourceLoader);
return resourceLoader;
}
void CResourceHandler::loadZipArchive(const std::string &prefix, const std::string &mountPoint, const JsonNode & config)
CFilesystemList * CResourceHandler::getInitial()
{
std::string URI = prefix + config["path"].String();
auto filename = initialLoader->getResourceName(ResourceID(URI, EResType::ARCHIVE_ZIP));
if (filename)
resourceLoader->addLoader(new CZipLoader(mountPoint, *filename), false);
assert(initialLoader);
return initialLoader;
}
void CResourceHandler::loadArchive(const std::string &prefix, const std::string &mountPoint, const JsonNode & config, EResType::Type archiveType)
{
std::string URI = prefix + config["path"].String();
auto filename = initialLoader->getResourceName(ResourceID(URI, archiveType));
if (filename)
resourceLoader->addLoader(new CArchiveLoader(mountPoint, *filename), false);
}
void CResourceHandler::loadJsonMap(const std::string &prefix, const std::string &mountPoint, const JsonNode & config)
{
std::string URI = prefix + config["path"].String();
auto filename = initialLoader->getResourceName(ResourceID(URI, EResType::TEXT));
if (filename)
{
auto configData = initialLoader->load(ResourceID(URI, EResType::TEXT))->readAll();
const JsonNode config((char*)configData.first.get(), configData.second);
resourceLoader->addLoader(new CMappedFileLoader(mountPoint, config), false);
}
}
void CResourceHandler::loadMainFileSystem(const std::string &fsConfigURI)
void CResourceHandler::load(const std::string &fsConfigURI)
{
auto fsConfigData = initialLoader->load(ResourceID(fsConfigURI, EResType::TEXT))->readAll();
const JsonNode fsConfig((char*)fsConfigData.first.get(), fsConfigData.second);
loadModFileSystem("", fsConfig["filesystem"]);
resourceLoader = createFileSystem("", fsConfig["filesystem"]);
// hardcoded system-specific path, may not be inside any of data directories
resourceLoader->addLoader(new CFilesystemLoader("SAVES/", VCMIDirs::get().userSavePath()), true);
resourceLoader->addLoader(new CFilesystemLoader("CONFIG/", VCMIDirs::get().userConfigPath()), true);
}
void CResourceHandler::loadModFileSystem(const std::string & prefix, const JsonNode &fsConfig)
CFilesystemList * CResourceHandler::createFileSystem(const std::string & prefix, const JsonNode &fsConfig)
{
for(auto & mountPoint : fsConfig.Struct())
{
for(auto & entry : mountPoint.second.Vector())
{
CStopWatch timer;
logGlobal->debugStream() << "\t\tLoading resource at " << prefix + entry["path"].String();
//TODO: replace if's with string->functor map?
if (entry["type"].String() == "map")
loadJsonMap(prefix, mountPoint.first, entry);
if (entry["type"].String() == "dir")
loadDirectory(prefix, mountPoint.first, entry);
if (entry["type"].String() == "lod")
loadArchive(prefix, mountPoint.first, entry, EResType::ARCHIVE_LOD);
if (entry["type"].String() == "snd")
loadArchive(prefix, mountPoint.first, entry, EResType::ARCHIVE_SND);
if (entry["type"].String() == "vid")
loadArchive(prefix, mountPoint.first, entry, EResType::ARCHIVE_VID);
if (entry["type"].String() == "zip")
loadZipArchive(prefix, mountPoint.first, entry);
logGlobal->debugStream() << "Resource loaded in " << timer.getDiff() << " ms.";
}
}
CFilesystemGenerator generator(prefix);
generator.loadConfig(fsConfig);
return generator.getFilesystem();
}
std::vector<std::string> CResourceHandler::getAvailableMods()
@@ -306,27 +215,3 @@ std::vector<std::string> CResourceHandler::getAvailableMods()
}
return foundMods;
}
void CResourceHandler::setActiveMods(std::vector<std::string> enabledMods)
{
// default FS config for mods: directory "Content" that acts as H3 root directory
JsonNode defaultFS;
defaultFS[""].Vector().resize(2);
defaultFS[""].Vector()[0]["type"].String() = "zip";
defaultFS[""].Vector()[0]["path"].String() = "/Content.zip";
defaultFS[""].Vector()[1]["type"].String() = "dir";
defaultFS[""].Vector()[1]["path"].String() = "/Content";
for(std::string & modName : enabledMods)
{
ResourceID modConfFile("mods/" + modName + "/mod", EResType::TEXT);
auto fsConfigData = initialLoader->load(modConfFile)->readAll();
const JsonNode fsConfig((char*)fsConfigData.first.get(), fsConfigData.second);
if (!fsConfig["filesystem"].isNull())
loadModFileSystem("mods/" + modName, fsConfig["filesystem"]);
else
loadModFileSystem("mods/" + modName, defaultFS);
}
}