Skip to content

Add API for stopping project loading #619

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/scratchcpp/project.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class LIBSCRATCHCPP_EXPORT Project
Project(const Project &) = delete;

bool load();
void stopLoading();

void start();
void run();
Expand Down
21 changes: 3 additions & 18 deletions src/internal/projectdownloader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,11 @@ static const std::string ASSET_PREFIX = "https://assets.scratch.mit.edu/internal
static const std::string ASSET_SUFFIX = "/get";

#define CHECK_CANCEL() \
m_cancelMutex.lock(); \
if (m_cancel) { \
m_downloadedAssetCount = 0; \
m_cancelMutex.unlock(); \
std::cout << "Download aborted!" << std::endl; \
return false; \
} \
m_cancelMutex.unlock()
}

ProjectDownloader::ProjectDownloader(IDownloaderFactory *downloaderFactory) :
m_downloaderFactory(downloaderFactory)
Expand All @@ -38,9 +35,7 @@ ProjectDownloader::ProjectDownloader(IDownloaderFactory *downloaderFactory) :

bool ProjectDownloader::downloadJson(const std::string &projectId)
{
m_cancelMutex.lock();
m_cancel = false;
m_cancelMutex.unlock();

// Get project token
std::cout << "Fetching project info of " << projectId << std::endl;
Expand Down Expand Up @@ -89,9 +84,7 @@ bool ProjectDownloader::downloadJson(const std::string &projectId)

bool ProjectDownloader::downloadAssets(const std::vector<std::string> &assetIds)
{
m_cancelMutex.lock();
m_cancel = false;
m_cancelMutex.unlock();

auto count = assetIds.size();
// unsigned int threadCount = std::thread::hardware_concurrency();
Expand Down Expand Up @@ -119,20 +112,14 @@ bool ProjectDownloader::downloadAssets(const std::vector<std::string> &assetIds)

// Download assets
auto f = [this, count](std::shared_ptr<IDownloader> downloader, int index, const std::string &id) {
m_cancelMutex.lock();

if (m_cancel)
return;

m_cancelMutex.unlock();

bool ret = downloader->download(ASSET_PREFIX + id + ASSET_SUFFIX);

if (!ret) {
std::cerr << "Failed to download asset: " << id << std::endl;
m_cancelMutex.lock();
m_cancel = true;
m_cancelMutex.unlock();
return;
}

Expand Down Expand Up @@ -178,7 +165,7 @@ bool ProjectDownloader::downloadAssets(const std::vector<std::string> &assetIds)

m_assetsMutex.lock();

if (m_downloadedAssetCount != lastCount) {
if (m_downloadedAssetCount != lastCount && !m_cancel) {
std::cout << "Downloaded assets: " << m_downloadedAssetCount << " of " << count << std::endl;
lastCount = m_downloadedAssetCount;
}
Expand All @@ -197,7 +184,7 @@ bool ProjectDownloader::downloadAssets(const std::vector<std::string> &assetIds)
threads.erase(index);
}

if (done) {
if (done || m_cancel) {
for (auto &[index, info] : threads)
info.first.join();

Expand All @@ -212,9 +199,7 @@ bool ProjectDownloader::downloadAssets(const std::vector<std::string> &assetIds)

void ProjectDownloader::cancel()
{
m_cancelMutex.lock();
m_cancel = true;
m_cancelMutex.unlock();
m_downloadedAssetCount = 0;
}

Expand Down
3 changes: 1 addition & 2 deletions src/internal/projectdownloader.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ class ProjectDownloader : public IProjectDownloader
std::vector<std::string> m_assets;
std::mutex m_assetsMutex;
std::atomic<unsigned int> m_downloadedAssetCount = 0;
bool m_cancel = false;
std::mutex m_cancelMutex;
std::atomic<bool> m_cancel = false;
sigslot::signal<unsigned int, unsigned int> m_downloadProgressChanged;
};

Expand Down
10 changes: 10 additions & 0 deletions src/project.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// SPDX-License-Identifier: Apache-2.0

#include <scratchcpp/project.h>
#include <iostream>

#include "project_p.h"
#include "internal/projectdownloader.h"

using namespace libscratchcpp;

Expand All @@ -27,6 +29,14 @@ bool Project::load()
return impl->load();
}

/*! Cancels project loading if loading in another thread. */
void Project::stopLoading()
{
std::cout << "Aborting project loading..." << std::endl;
impl->stopLoading = true;
impl->downloader->cancel();
}

/*!
* Calls all "when green flag clicked" blocks.
* \note Nothing will happen until run() or frame() is called.
Expand Down
43 changes: 42 additions & 1 deletion src/project_p.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ bool ProjectPrivate::load()

bool ProjectPrivate::tryLoad(IProjectReader *reader)
{
stopLoading = false;

// Load from URL
ProjectUrl url(fileName);

Expand All @@ -57,8 +59,18 @@ bool ProjectPrivate::tryLoad(IProjectReader *reader)
return false;
}

if (stopLoading) {
loadingAborted();
return false;
}

bool ret = reader->loadData(downloader->json());

if (stopLoading) {
loadingAborted();
return false;
}

if (!ret)
return false;

Expand Down Expand Up @@ -91,7 +103,14 @@ bool ProjectPrivate::tryLoad(IProjectReader *reader)
}

// Download assets
if (!downloader->downloadAssets(assetNames)) {
ret = downloader->downloadAssets(assetNames);

if (stopLoading) {
loadingAborted();
return false;
}

if (!ret) {
std::cerr << "Failed to download the project assets." << std::endl;
return false;
}
Expand All @@ -113,7 +132,18 @@ bool ProjectPrivate::tryLoad(IProjectReader *reader)
return false;
}

if (stopLoading) {
loadingAborted();
return false;
}

bool ret = reader->load();

if (stopLoading) {
loadingAborted();
return false;
}

if (!ret)
return false;
}
Expand All @@ -125,9 +155,20 @@ bool ProjectPrivate::tryLoad(IProjectReader *reader)
engine->setExtensions(reader->extensions());
engine->setUserAgent(reader->userAgent());
engine->compile();

if (stopLoading) {
loadingAborted();
return false;
}

return true;
}

void ProjectPrivate::loadingAborted()
{
std::cout << "Loading aborted." << std::endl;
}

void ProjectPrivate::start()
{
engine->start();
Expand Down
2 changes: 2 additions & 0 deletions src/project_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ struct ProjectPrivate

bool load();
bool tryLoad(IProjectReader *reader);
void loadingAborted();

void start();
void run();
Expand All @@ -29,6 +30,7 @@ struct ProjectPrivate
sigslot::signal<unsigned int, unsigned int> &downloadProgressChanged();

std::string fileName;
std::atomic<bool> stopLoading = false;
std::shared_ptr<IEngine> engine = nullptr;

static IProjectDownloaderFactory *downloaderFactory;
Expand Down
14 changes: 14 additions & 0 deletions test/project/project_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,17 @@ TEST(LoadProjectTest, DownloadProgressChanged)
EXPECT_CALL(*downloader, downloadProgressChanged).WillOnce(ReturnRef(signal));
ASSERT_EQ(&p.downloadProgressChanged(), &signal);
}

TEST(LoadProjectTest, AbortDownload)
{
ProjectDownloaderFactoryMock factory;
auto downloader = std::make_shared<ProjectDownloaderMock>();
ProjectPrivate::downloaderFactory = &factory;

EXPECT_CALL(factory, create()).WillOnce(Return(downloader));
Project p;
ProjectPrivate::downloaderFactory = nullptr;

EXPECT_CALL(*downloader, cancel());
p.stopLoading();
}
Loading