Skip to content

Commit e5fa752

Browse files
authored
Merge pull request #484 from scratchcpp/metadata_api
Add metadata API
2 parents 59b91a6 + 64bea02 commit e5fa752

File tree

10 files changed

+53
-0
lines changed

10 files changed

+53
-0
lines changed

include/scratchcpp/iengine.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,12 @@ class LIBSCRATCHCPP_EXPORT IEngine
358358

359359
/*! Returns the map of scripts (each top level block has a Script object). */
360360
virtual const std::unordered_map<std::shared_ptr<Block>, std::shared_ptr<Script>> &scripts() const = 0;
361+
362+
/*! Returns the user agent of the last person to edit the project. */
363+
virtual const std::string &userAgent() const = 0;
364+
365+
/*! Sets the user agent of the last person to edit the project. */
366+
virtual void setUserAgent(const std::string &agent) = 0;
361367
};
362368

363369
} // namespace libscratchcpp

src/engine/internal/engine.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1430,6 +1430,16 @@ BlockSectionContainer *Engine::blockSectionContainer(IBlockSection *section) con
14301430
return nullptr;
14311431
}
14321432

1433+
const std::string &Engine::userAgent() const
1434+
{
1435+
return m_userAgent;
1436+
}
1437+
1438+
void Engine::setUserAgent(const std::string &agent)
1439+
{
1440+
m_userAgent = agent;
1441+
}
1442+
14331443
void Engine::finalize()
14341444
{
14351445
m_eventLoopMutex.lock();

src/engine/internal/engine.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,9 @@ class Engine : public IEngine
157157
BlockSectionContainer *blockSectionContainer(const std::string &opcode) const;
158158
BlockSectionContainer *blockSectionContainer(IBlockSection *section) const;
159159

160+
const std::string &userAgent() const override;
161+
void setUserAgent(const std::string &agent) override;
162+
160163
IClock *m_clock = nullptr;
161164

162165
private:
@@ -227,6 +230,7 @@ class Engine : public IEngine
227230
std::unordered_map<std::shared_ptr<Block>, std::shared_ptr<Script>> m_scripts;
228231
std::vector<BlockFunc> m_functions;
229232
std::recursive_mutex m_eventLoopMutex;
233+
std::string m_userAgent;
230234

231235
std::unordered_map<Target *, std::vector<Script *>> m_greenFlagHats;
232236
std::unordered_map<Target *, std::vector<Script *>> m_backdropChangeHats;

src/internal/iprojectreader.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class IProjectReader
3535
virtual const std::vector<std::shared_ptr<Broadcast>> &broadcasts() = 0;
3636
virtual const std::vector<std::shared_ptr<Monitor>> &monitors() = 0;
3737
virtual const std::vector<std::string> &extensions() = 0;
38+
virtual const std::string &userAgent() const = 0;
3839

3940
protected:
4041
virtual void printErr(const std::string &errStr) final { std::cerr << "Failed to read project: " << errStr << std::endl; }

src/internal/scratch3reader.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,13 @@ bool Scratch3Reader::load()
453453
auto extensions = project["extensions"];
454454
for (auto extension : extensions)
455455
m_extensions.push_back(extension);
456+
457+
// meta
458+
READER_STEP(step, "meta");
459+
auto meta = project["meta"];
460+
READER_STEP(step, "meta -> agent");
461+
m_userAgent = meta["agent"];
462+
456463
} catch (std::exception &e) {
457464
if (strcmp(step, "") == 0)
458465
printErr("could not parse project JSON file", e.what());
@@ -522,6 +529,11 @@ const std::vector<std::string> &Scratch3Reader::extensions()
522529
return m_extensions;
523530
}
524531

532+
const std::string &Scratch3Reader::userAgent() const
533+
{
534+
return m_userAgent;
535+
}
536+
525537
void Scratch3Reader::read()
526538
{
527539
if (fileName().empty())

src/internal/scratch3reader.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class Scratch3Reader : public IProjectReader
2121
const std::vector<std::shared_ptr<Broadcast>> &broadcasts() override;
2222
const std::vector<std::shared_ptr<Monitor>> &monitors() override;
2323
const std::vector<std::string> &extensions() override;
24+
const std::string &userAgent() const override;
2425

2526
private:
2627
void read();
@@ -30,6 +31,7 @@ class Scratch3Reader : public IProjectReader
3031
std::vector<std::shared_ptr<Broadcast>> m_broadcasts;
3132
std::vector<std::shared_ptr<Monitor>> m_monitors;
3233
std::vector<std::string> m_extensions;
34+
std::string m_userAgent;
3335
};
3436

3537
} // namespace libscratchcpp

src/project_p.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ bool ProjectPrivate::load()
129129
engine->setBroadcasts(reader->broadcasts());
130130
engine->setMonitors(reader->monitors());
131131
engine->setExtensions(reader->extensions());
132+
engine->setUserAgent(reader->userAgent());
132133
engine->compile();
133134
return true;
134135
}

test/engine/engine_test.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1767,6 +1767,15 @@ TEST(EngineTest, StopOtherScriptsInSprite)
17671767
ASSERT_EQ(GET_VAR(stage, "l")->value().toInt(), 110);
17681768
}
17691769

1770+
TEST(EngineTest, UserAgent)
1771+
{
1772+
Engine engine;
1773+
ASSERT_TRUE(engine.userAgent().empty());
1774+
1775+
engine.setUserAgent("test");
1776+
ASSERT_EQ(engine.userAgent(), "test");
1777+
}
1778+
17701779
TEST(EngineTest, NoCrashAfterStop)
17711780
{
17721781
// Regtest for #186

test/load_project/load_project_test.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ TEST(LoadProjectTest, EmptyProject)
7272
backdrop->dataSize()),
7373
0);
7474

75+
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");
76+
7577
i++;
7678
}
7779
}
@@ -632,6 +634,9 @@ TEST(LoadProjectTest, LoadTestProject)
632634
ASSERT_EQ(monitor->y(), 280);
633635
ASSERT_TRUE(monitor->visible());
634636

637+
// User agent
638+
ASSERT_TRUE(engine->userAgent().empty());
639+
635640
i++;
636641
}
637642
}

test/mocks/enginemock.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,9 @@ class EngineMock : public IEngine
134134
MOCK_METHOD(void, setExtensions, (const std::vector<std::string> &), (override));
135135

136136
MOCK_METHOD(const ScriptMap &, scripts, (), (const, override));
137+
138+
MOCK_METHOD(const std::string &, userAgent, (), (const, override));
139+
MOCK_METHOD(void, setUserAgent, (const std::string &), (override));
137140
};
138141

139142
} // namespace libscratchcpp

0 commit comments

Comments
 (0)