-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SporeModManager: add update-modapi command
- Loading branch information
1 parent
0edb589
commit 7ec6899
Showing
6 changed files
with
179 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
* SporeModLoader - https://github.com/Rosalie241/SporeModLoader | ||
* Copyright (C) 2022 Rosalie Wanders <[email protected]> | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License version 3. | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
#include "Download.hpp" | ||
#include "UI.hpp" | ||
|
||
#include <iostream> | ||
#include <fstream> | ||
|
||
#ifdef _WIN32 | ||
// TODO: windows support | ||
#else | ||
#include <curl/curl.h> | ||
#endif // _WIN32 | ||
|
||
using namespace SporeModManagerHelpers; | ||
|
||
// | ||
// Local Variables | ||
// | ||
|
||
|
||
// | ||
// Local Functions | ||
// | ||
|
||
static size_t curl_write_data(char *data, size_t size, size_t nmemb, void* stream) | ||
{ | ||
std::ofstream* fileStream = (std::ofstream*)(stream); | ||
std::streamoff position = fileStream->tellp(); | ||
fileStream->write(data, size * nmemb); | ||
return fileStream->tellp() - position; | ||
} | ||
|
||
|
||
// | ||
// Exported Functions | ||
// | ||
|
||
bool Download::DownloadFile(std::string url, std::filesystem::path path) | ||
{ | ||
std::cout << "-> Downloading " << path.filename() << std::endl; | ||
|
||
if (UI::GetVerboseMode()) | ||
{ | ||
std::cout << "--> Downloading " << url << std::endl; | ||
} | ||
|
||
#ifdef _WIN32 | ||
// TODO: windows support | ||
#else | ||
CURL* curl = curl_easy_init(); | ||
if (curl == nullptr) | ||
{ | ||
std::cerr << "Error: failed to initialize cURL!" << std::endl;; | ||
return false; | ||
} | ||
|
||
std::ofstream fileStream(path, std::ios::binary); | ||
if (!fileStream.is_open()) | ||
{ | ||
curl_easy_cleanup(curl); | ||
std::cerr << "Error: failed to open " << path << std::endl; | ||
return false; | ||
} | ||
|
||
curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); | ||
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_write_data); | ||
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*)&fileStream); | ||
curl_easy_setopt(curl, CURLOPT_REDIR_PROTOCOLS_STR, "https"); | ||
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); | ||
|
||
if (curl_easy_perform(curl) != CURLE_OK) | ||
{ | ||
curl_easy_cleanup(curl); | ||
std::cerr << "Error: failed to download file!" << std::endl; | ||
return false; | ||
} | ||
|
||
curl_easy_cleanup(curl); | ||
return true; | ||
#endif // _WIN32 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* SporeModLoader - https://github.com/Rosalie241/SporeModLoader | ||
* Copyright (C) 2022 Rosalie Wanders <[email protected]> | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License version 3. | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
#ifndef SPOREMODMANAGERHELPERS_DOWNLOAD_HPP | ||
#define SPOREMODMANAGERHELPERS_DOWNLOAD_HPP | ||
|
||
#include <string> | ||
#include <filesystem> | ||
|
||
namespace SporeModManagerHelpers | ||
{ | ||
namespace Download | ||
{ | ||
/// <summary> | ||
/// Downloads url to path | ||
/// </summary> | ||
bool DownloadFile(std::string url, std::filesystem::path path); | ||
} | ||
} | ||
|
||
#endif // SPOREMODMANAGERHELPERS_DOWNLOAD_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters