Move code from Downloader::getLocalFileHash to Util::getLocalFileHash

This commit is contained in:
Sude 2016-05-18 14:11:37 +03:00
parent 4c02de5d24
commit 730c1ffe58
3 changed files with 33 additions and 24 deletions

View File

@ -88,6 +88,7 @@ namespace Util
unsigned int getOptionValue(const std::string& str, const std::vector<GlobalConstants::optionsStruct>& options);
std::string getOptionNameString(const unsigned int& value, const std::vector<GlobalConstants::optionsStruct>& options);
void parseOptionString(const std::string &option_string, std::vector<unsigned int> &priority, unsigned int &type, const std::vector<GlobalConstants::optionsStruct>& options);
std::string getLocalFileHash(const std::string& xml_dir, const std::string& filepath, const std::string& gamename = std::string());
}
#endif // UTIL_H

View File

@ -2555,33 +2555,12 @@ std::string Downloader::getLocalFileHash(const std::string& filepath, const std:
if (config.bAutomaticXMLCreation && !boost::filesystem::exists(local_xml_file) && boost::filesystem::exists(path))
{
std::string xml_directory = config.sXMLDirectory + "/" + gamename;
Util::createXML(filepath, config.iChunkSize, xml_directory);
std::string xml_directory = config.sXMLDirectory + "/" + gamename;
Util::createXML(filepath, config.iChunkSize, xml_directory);
}
if (boost::filesystem::exists(local_xml_file))
{
tinyxml2::XMLDocument local_xml;
local_xml.LoadFile(local_xml_file.string().c_str());
tinyxml2::XMLElement *fileElemLocal = local_xml.FirstChildElement("file");
if (!fileElemLocal && config.bAutomaticXMLCreation)
{
std::string xml_directory = config.sXMLDirectory + "/" + gamename;
Util::createXML(filepath, config.iChunkSize, xml_directory);
local_xml.LoadFile(local_xml_file.string().c_str());
fileElemLocal = local_xml.FirstChildElement("file");
}
if (fileElemLocal)
{
localHash = fileElemLocal->Attribute("md5");
return localHash;
}
}
localHash = Util::getFileHash(path.string(), RHASH_MD5);
if (boost::filesystem::exists(path) && boost::filesystem::is_regular_file(path))
{
localHash = Util::getFileHash(path.string(), RHASH_MD5);
}
return localHash;
}

View File

@ -540,3 +540,32 @@ void Util::parseOptionString(const std::string &option_string, std::vector<unsig
type |= value;
}
}
std::string Util::getLocalFileHash(const std::string& xml_dir, const std::string& filepath, const std::string& gamename)
{
std::string localHash;
boost::filesystem::path path = filepath;
boost::filesystem::path local_xml_file;
if (!gamename.empty())
local_xml_file = xml_dir + "/" + gamename + "/" + path.filename().string() + ".xml";
else
local_xml_file = xml_dir + "/" + path.filename().string() + ".xml";
if (boost::filesystem::exists(local_xml_file))
{
tinyxml2::XMLDocument local_xml;
local_xml.LoadFile(local_xml_file.string().c_str());
tinyxml2::XMLElement *fileElem = local_xml.FirstChildElement("file");
if (fileElem)
{
localHash = fileElem->Attribute("md5");
}
}
else if (boost::filesystem::exists(path) && boost::filesystem::is_regular_file(path))
{
localHash = Util::getFileHash(path.string(), RHASH_MD5);
}
return localHash;
}