Skip to content
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
6 changes: 6 additions & 0 deletions include/scratchcpp/iengine.h
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,12 @@ class LIBSCRATCHCPP_EXPORT IEngine

/*! Returns the map of scripts (each top level block has a Script object). */
virtual const std::unordered_map<std::shared_ptr<Block>, std::shared_ptr<Script>> &scripts() const = 0;

/*! Returns the user agent of the last person to edit the project. */
virtual const std::string &userAgent() const = 0;

/*! Sets the user agent of the last person to edit the project. */
virtual void setUserAgent(const std::string &agent) = 0;
};

} // namespace libscratchcpp
10 changes: 10 additions & 0 deletions src/engine/internal/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1430,6 +1430,16 @@ BlockSectionContainer *Engine::blockSectionContainer(IBlockSection *section) con
return nullptr;
}

const std::string &Engine::userAgent() const
{
return m_userAgent;
}

void Engine::setUserAgent(const std::string &agent)
{
m_userAgent = agent;
}

void Engine::finalize()
{
m_eventLoopMutex.lock();
Expand Down
4 changes: 4 additions & 0 deletions src/engine/internal/engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,9 @@ class Engine : public IEngine
BlockSectionContainer *blockSectionContainer(const std::string &opcode) const;
BlockSectionContainer *blockSectionContainer(IBlockSection *section) const;

const std::string &userAgent() const override;
void setUserAgent(const std::string &agent) override;

IClock *m_clock = nullptr;

private:
Expand Down Expand Up @@ -227,6 +230,7 @@ class Engine : public IEngine
std::unordered_map<std::shared_ptr<Block>, std::shared_ptr<Script>> m_scripts;
std::vector<BlockFunc> m_functions;
std::recursive_mutex m_eventLoopMutex;
std::string m_userAgent;

std::unordered_map<Target *, std::vector<Script *>> m_greenFlagHats;
std::unordered_map<Target *, std::vector<Script *>> m_backdropChangeHats;
Expand Down
1 change: 1 addition & 0 deletions src/internal/iprojectreader.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class IProjectReader
virtual const std::vector<std::shared_ptr<Broadcast>> &broadcasts() = 0;
virtual const std::vector<std::shared_ptr<Monitor>> &monitors() = 0;
virtual const std::vector<std::string> &extensions() = 0;
virtual const std::string &userAgent() const = 0;

protected:
virtual void printErr(const std::string &errStr) final { std::cerr << "Failed to read project: " << errStr << std::endl; }
Expand Down
12 changes: 12 additions & 0 deletions src/internal/scratch3reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,13 @@ bool Scratch3Reader::load()
auto extensions = project["extensions"];
for (auto extension : extensions)
m_extensions.push_back(extension);

// meta
READER_STEP(step, "meta");
auto meta = project["meta"];
READER_STEP(step, "meta -> agent");
m_userAgent = meta["agent"];

} catch (std::exception &e) {
if (strcmp(step, "") == 0)
printErr("could not parse project JSON file", e.what());
Expand Down Expand Up @@ -522,6 +529,11 @@ const std::vector<std::string> &Scratch3Reader::extensions()
return m_extensions;
}

const std::string &Scratch3Reader::userAgent() const
{
return m_userAgent;
}

void Scratch3Reader::read()
{
if (fileName().empty())
Expand Down
2 changes: 2 additions & 0 deletions src/internal/scratch3reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class Scratch3Reader : public IProjectReader
const std::vector<std::shared_ptr<Broadcast>> &broadcasts() override;
const std::vector<std::shared_ptr<Monitor>> &monitors() override;
const std::vector<std::string> &extensions() override;
const std::string &userAgent() const override;

private:
void read();
Expand All @@ -30,6 +31,7 @@ class Scratch3Reader : public IProjectReader
std::vector<std::shared_ptr<Broadcast>> m_broadcasts;
std::vector<std::shared_ptr<Monitor>> m_monitors;
std::vector<std::string> m_extensions;
std::string m_userAgent;
};

} // namespace libscratchcpp
1 change: 1 addition & 0 deletions src/project_p.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ bool ProjectPrivate::load()
engine->setBroadcasts(reader->broadcasts());
engine->setMonitors(reader->monitors());
engine->setExtensions(reader->extensions());
engine->setUserAgent(reader->userAgent());
engine->compile();
return true;
}
Expand Down
9 changes: 9 additions & 0 deletions test/engine/engine_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1767,6 +1767,15 @@ TEST(EngineTest, StopOtherScriptsInSprite)
ASSERT_EQ(GET_VAR(stage, "l")->value().toInt(), 110);
}

TEST(EngineTest, UserAgent)
{
Engine engine;
ASSERT_TRUE(engine.userAgent().empty());

engine.setUserAgent("test");
ASSERT_EQ(engine.userAgent(), "test");
}

TEST(EngineTest, NoCrashAfterStop)
{
// Regtest for #186
Expand Down
5 changes: 5 additions & 0 deletions test/load_project/load_project_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ TEST(LoadProjectTest, EmptyProject)
backdrop->dataSize()),
0);

ASSERT_EQ(engine->userAgent(), "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Scratux/1.4.1 Chrome/76.0.3809.146 Electron/6.1.7 Safari/537.36");

i++;
}
}
Expand Down Expand Up @@ -632,6 +634,9 @@ TEST(LoadProjectTest, LoadTestProject)
ASSERT_EQ(monitor->y(), 280);
ASSERT_TRUE(monitor->visible());

// User agent
ASSERT_TRUE(engine->userAgent().empty());

i++;
}
}
Expand Down
3 changes: 3 additions & 0 deletions test/mocks/enginemock.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ class EngineMock : public IEngine
MOCK_METHOD(void, setExtensions, (const std::vector<std::string> &), (override));

MOCK_METHOD(const ScriptMap &, scripts, (), (const, override));

MOCK_METHOD(const std::string &, userAgent, (), (const, override));
MOCK_METHOD(void, setUserAgent, (const std::string &), (override));
};

} // namespace libscratchcpp