Galaxy: Fix MojoSetup hack for files that have missing XML data

Try to get file size using Content-Length header if XML data is missing
This commit is contained in:
Sude 2022-10-01 20:28:03 +03:00
parent 6ae6ab2469
commit 626aa681c9

View File

@ -6002,15 +6002,18 @@ int Downloader::mojoSetupGetFileVector(const gameFile& gf, std::vector<zipFileEn
// Get XML data // Get XML data
curl_off_t file_size = 0;
bool bMissingXML = false;
bool bXMLParsingError = false;
std::string xml_data = gogGalaxy->getResponse(xml_url); std::string xml_data = gogGalaxy->getResponse(xml_url);
if (xml_data.empty()) if (xml_data.empty())
{ {
std::cerr << "Failed to get XML data" << std::endl; std::cerr << "Failed to get XML data" << std::endl;
return 1; bMissingXML = true;
} }
if (!bMissingXML)
std::uintmax_t file_size = 0; {
tinyxml2::XMLDocument xml; tinyxml2::XMLDocument xml;
xml.Parse(xml_data.c_str()); xml.Parse(xml_data.c_str());
tinyxml2::XMLElement *fileElem = xml.FirstChildElement("file"); tinyxml2::XMLElement *fileElem = xml.FirstChildElement("file");
@ -6018,7 +6021,7 @@ int Downloader::mojoSetupGetFileVector(const gameFile& gf, std::vector<zipFileEn
if (!fileElem) if (!fileElem)
{ {
std::cerr << "Failed to parse XML data" << std::endl; std::cerr << "Failed to parse XML data" << std::endl;
return 1; bXMLParsingError = true;
} }
else else
{ {
@ -6031,11 +6034,6 @@ int Downloader::mojoSetupGetFileVector(const gameFile& gf, std::vector<zipFileEn
{ {
file_size = 0; file_size = 0;
} }
if (file_size == 0)
{
std::cerr << "Failed to get file size" << std::endl;
return 1;
} }
} }
@ -6046,6 +6044,28 @@ int Downloader::mojoSetupGetFileVector(const gameFile& gf, std::vector<zipFileEn
return 1; return 1;
} }
if (bXMLParsingError || bMissingXML || file_size == 0)
{
std::cerr << "Failed to get file size from XML data, trying to get Content-Length header" << std::endl;
std::stringstream header;
curl_easy_setopt(curlhandle, CURLOPT_URL, installer_url.c_str());
curl_easy_setopt(curlhandle, CURLOPT_HEADER, 1);
curl_easy_setopt(curlhandle, CURLOPT_NOBODY, 1);
curl_easy_setopt(curlhandle, CURLOPT_NOPROGRESS, 1);
curl_easy_setopt(curlhandle, CURLOPT_WRITEFUNCTION, Util::CurlWriteMemoryCallback);
curl_easy_setopt(curlhandle, CURLOPT_WRITEDATA, &header);
curl_easy_perform(curlhandle);
curl_easy_setopt(curlhandle, CURLOPT_HEADER, 0);
curl_easy_setopt(curlhandle, CURLOPT_NOBODY, 0);
curl_easy_getinfo(curlhandle, CURLINFO_CONTENT_LENGTH_DOWNLOAD_T, &file_size);
}
if (file_size <= 0)
{
std::cerr << "Failed to get file size" << std::endl;
return 1;
}
off_t head_size = 100 << 10; // 100 kB off_t head_size = 100 << 10; // 100 kB
off_t tail_size = 200 << 10; // 200 kB off_t tail_size = 200 << 10; // 200 kB
std::string head_range = "0-" + std::to_string(head_size); std::string head_range = "0-" + std::to_string(head_size);