From 2a9170a2ea6c1d146bc6f6c701b2e6fef87461d0 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Fri, 14 May 2021 22:46:03 -0700 Subject: [PATCH] http: Use Path for the download file. --- Common/Buffer.cpp | 6 ++++-- Common/Buffer.h | 4 +++- Common/Net/HTTPClient.cpp | 14 +++++++------- Common/Net/HTTPClient.h | 11 ++++++----- Core/Config.cpp | 2 +- Core/Util/GameManager.cpp | 8 ++++---- Core/Util/GameManager.h | 2 +- UI/DevScreens.cpp | 2 +- UI/Store.cpp | 4 ++-- 9 files changed, 29 insertions(+), 24 deletions(-) diff --git a/Common/Buffer.cpp b/Common/Buffer.cpp index fd25b0ad97..6dc6b74c5a 100644 --- a/Common/Buffer.cpp +++ b/Common/Buffer.cpp @@ -3,6 +3,8 @@ #include #include "Common/Buffer.h" +#include "Common/File/FileUtil.h" +#include "Common/File/Path.h" #include "Common/Log.h" Buffer::Buffer() { } @@ -115,8 +117,8 @@ void Buffer::Printf(const char *fmt, ...) { memcpy(ptr, buffer, retval); } -bool Buffer::FlushToFile(const char *filename) { - FILE *f = fopen(filename, "wb"); +bool Buffer::FlushToFile(const Path &filename) { + FILE *f = File::OpenCFile(filename, "wb"); if (!f) return false; if (data_.size()) { diff --git a/Common/Buffer.h b/Common/Buffer.h index 2e92669b29..4d63a8e5ad 100644 --- a/Common/Buffer.h +++ b/Common/Buffer.h @@ -5,6 +5,8 @@ #include "Common/Common.h" +class Path; + // Acts as a queue. Intended to be as fast as possible for most uses. // Does not do synchronization, must use external mutexes. class Buffer { @@ -66,7 +68,7 @@ public: // Writes the entire buffer to the file descriptor. Also resets the // size to zero. On failure, data remains in buffer and nothing is // written. - bool FlushToFile(const char *filename); + bool FlushToFile(const Path &filename); // Utilities. Try to avoid checking for size. size_t size() const { return data_.size(); } diff --git a/Common/Net/HTTPClient.cpp b/Common/Net/HTTPClient.cpp index f06072cc7d..e1b25bb073 100644 --- a/Common/Net/HTTPClient.cpp +++ b/Common/Net/HTTPClient.cpp @@ -457,7 +457,7 @@ int Client::ReadResponseEntity(net::Buffer *readbuf, const std::vector Downloader::StartDownload(const std::string &url, const std::string &outfile) { +std::shared_ptr Downloader::StartDownload(const std::string &url, const Path &outfile) { std::shared_ptr dl(new Download(url, outfile)); downloads_.push_back(dl); dl->Start(); @@ -578,7 +578,7 @@ std::shared_ptr Downloader::StartDownload(const std::string &url, cons std::shared_ptr Downloader::StartDownloadWithCallback( const std::string &url, - const std::string &outfile, + const Path &outfile, std::function callback) { std::shared_ptr dl(new Download(url, outfile)); dl->SetCallback(callback); diff --git a/Common/Net/HTTPClient.h b/Common/Net/HTTPClient.h index 2087f1ea71..0924d4b9b7 100644 --- a/Common/Net/HTTPClient.h +++ b/Common/Net/HTTPClient.h @@ -5,6 +5,7 @@ #include #include +#include "Common/File/Path.h" #include "Common/Net/NetBuffer.h" #include "Common/Net/Resolve.h" @@ -90,7 +91,7 @@ protected: // Not particularly efficient, but hey - it's a background download, that's pretty cool :P class Download { public: - Download(const std::string &url, const std::string &outfile); + Download(const std::string &url, const Path &outfile); ~Download(); void Start(); @@ -108,7 +109,7 @@ public: int ResultCode() const { return resultCode_; } std::string url() const { return url_; } - std::string outfile() const { return outfile_; } + const Path &outfile() const { return outfile_; } // If not downloading to a file, access this to get the result. Buffer &buffer() { return buffer_; } @@ -147,7 +148,7 @@ private: Buffer buffer_; std::vector responseHeaders_; std::string url_; - std::string outfile_; + Path outfile_; std::thread thread_; int resultCode_ = 0; bool completed_ = false; @@ -166,11 +167,11 @@ public: CancelAll(); } - std::shared_ptr StartDownload(const std::string &url, const std::string &outfile); + std::shared_ptr StartDownload(const std::string &url, const Path &outfile); std::shared_ptr StartDownloadWithCallback( const std::string &url, - const std::string &outfile, + const Path &outfile, std::function callback); // Drops finished downloads from the list. diff --git a/Core/Config.cpp b/Core/Config.cpp index e365673778..5664828011 100644 --- a/Core/Config.cpp +++ b/Core/Config.cpp @@ -1301,7 +1301,7 @@ void Config::Load(const char *iniFileName, const char *controllerIniFilename) { // upgrade number in the ini. if (iRunCount % 10 == 0 && bCheckForNewVersion) { std::shared_ptr dl = g_DownloadManager.StartDownloadWithCallback( - "http://www.ppsspp.org/version.json", "", &DownloadCompletedCallback); + "http://www.ppsspp.org/version.json", Path(), &DownloadCompletedCallback); dl->SetHidden(true); } diff --git a/Core/Util/GameManager.cpp b/Core/Util/GameManager.cpp index 50280938c1..f72977d67f 100644 --- a/Core/Util/GameManager.cpp +++ b/Core/Util/GameManager.cpp @@ -52,15 +52,15 @@ GameManager g_GameManager; GameManager::GameManager() { } -std::string GameManager::GetTempFilename() const { +Path GameManager::GetTempFilename() const { #ifdef _WIN32 wchar_t tempPath[MAX_PATH]; GetTempPath(MAX_PATH, tempPath); wchar_t buffer[MAX_PATH]; GetTempFileName(tempPath, L"PSP", 1, buffer); - return ConvertWStringToUTF8(buffer); + return Path(buffer); #else - return (g_Config.memStickDirectory / "ppsspp.dl").ToString(); + return g_Config.memStickDirectory / "ppsspp.dl"; #endif } @@ -79,7 +79,7 @@ bool GameManager::DownloadAndInstall(std::string storeFileUrl) { return false; } - std::string filename = GetTempFilename(); + Path filename = GetTempFilename(); curDownload_ = g_DownloadManager.StartDownload(storeFileUrl, filename); return true; } diff --git a/Core/Util/GameManager.h b/Core/Util/GameManager.h index 382038c3fd..4f8826bc88 100644 --- a/Core/Util/GameManager.h +++ b/Core/Util/GameManager.h @@ -85,7 +85,7 @@ private: bool DetectTexturePackDest(struct zip *z, int iniIndex, Path &dest); void SetInstallError(const std::string &err); - std::string GetTempFilename() const; + Path GetTempFilename() const; std::string GetGameID(const Path &path) const; std::string GetPBPGameID(FileLoader *loader) const; std::string GetISOGameID(FileLoader *loader) const; diff --git a/UI/DevScreens.cpp b/UI/DevScreens.cpp index f04b7d4a9e..8698b16005 100644 --- a/UI/DevScreens.cpp +++ b/UI/DevScreens.cpp @@ -1192,7 +1192,7 @@ void FrameDumpTestScreen::update() { UIScreen::update(); if (!listing_) { - listing_ = g_DownloadManager.StartDownload(framedumpsBaseUrl, ""); + listing_ = g_DownloadManager.StartDownload(framedumpsBaseUrl, Path()); } if (listing_ && listing_->Done() && files_.empty()) { diff --git a/UI/Store.cpp b/UI/Store.cpp index fb83b3a860..111b2b2731 100644 --- a/UI/Store.cpp +++ b/UI/Store.cpp @@ -134,7 +134,7 @@ void HttpImageFileView::DownloadCompletedCallback(http::Download &download) { void HttpImageFileView::Draw(UIContext &dc) { using namespace Draw; if (!texture_ && !textureFailed_ && !path_.empty() && !download_) { - download_ = downloader_->StartDownloadWithCallback(path_, "", std::bind(&HttpImageFileView::DownloadCompletedCallback, this, std::placeholders::_1)); + download_ = downloader_->StartDownloadWithCallback(path_, Path(), std::bind(&HttpImageFileView::DownloadCompletedCallback, this, std::placeholders::_1)); download_->SetHidden(true); } @@ -367,7 +367,7 @@ StoreScreen::StoreScreen() { std::string indexPath = storeBaseUrl + "index.json"; - listing_ = g_DownloadManager.StartDownload(indexPath, ""); + listing_ = g_DownloadManager.StartDownload(indexPath, Path()); } StoreScreen::~StoreScreen() {