Skip to content

Commit 6037520

Browse files
committed
Add API for unsupported blocks to IEngine
1 parent f3bc87f commit 6037520

File tree

6 files changed

+42
-2
lines changed

6 files changed

+42
-2
lines changed

include/scratchcpp/iengine.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <memory>
66
#include <vector>
77
#include <unordered_map>
8+
#include <unordered_set>
89
#include <functional>
910

1011
#include "global.h"
@@ -390,6 +391,9 @@ class LIBSCRATCHCPP_EXPORT IEngine
390391

391392
/*! Sets the user agent of the last person to edit the project. */
392393
virtual void setUserAgent(const std::string &agent) = 0;
394+
395+
/*! Returns the unsupported block opcodes which were found when compiling. */
396+
virtual const std::unordered_set<std::string> &unsupportedBlocks() const = 0;
393397
};
394398

395399
} // namespace libscratchcpp

src/engine/internal/engine.cpp

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ void Engine::clear()
9595
m_edgeActivatedHatValues.clear();
9696

9797
m_running = false;
98+
99+
m_unsupportedBlocks.clear();
98100
}
99101

100102
// Resolves ID references and sets pointers of entities.
@@ -280,8 +282,10 @@ void Engine::compile()
280282
auto b = block->inputAt(block->findInput("custom_block"))->valueBlock();
281283
procedureBytecodeMap[b->mutationPrototype()->procCode()] = script->bytecode();
282284
}
283-
} else
285+
} else {
284286
std::cout << "warning: unsupported top level block: " << block->opcode() << std::endl;
287+
m_unsupportedBlocks.insert(block->opcode());
288+
}
285289
}
286290
}
287291

@@ -298,6 +302,11 @@ void Engine::compile()
298302
m_scripts[block]->setLists(compiler.lists());
299303
}
300304
}
305+
306+
const auto &unsupportedBlocks = compiler.unsupportedBlocks();
307+
308+
for (const std::string &opcode : unsupportedBlocks)
309+
m_unsupportedBlocks.insert(opcode);
301310
}
302311

303312
// Compile monitor blocks to bytecode
@@ -343,8 +352,15 @@ void Engine::compile()
343352
script->setConstValues(compiler.constValues());
344353
script->setVariables(compiler.variables());
345354
script->setLists(compiler.lists());
346-
} else
355+
} else {
347356
std::cout << "warning: unsupported monitor block: " << block->opcode() << std::endl;
357+
m_unsupportedBlocks.insert(block->opcode());
358+
}
359+
360+
const auto &unsupportedBlocks = compiler.unsupportedBlocks();
361+
362+
for (const std::string &opcode : unsupportedBlocks)
363+
m_unsupportedBlocks.insert(opcode);
348364
}
349365
}
350366

@@ -1622,6 +1638,11 @@ void Engine::setUserAgent(const std::string &agent)
16221638
m_userAgent = agent;
16231639
}
16241640

1641+
const std::unordered_set<std::string> &Engine::unsupportedBlocks() const
1642+
{
1643+
return m_unsupportedBlocks;
1644+
}
1645+
16251646
void Engine::finalize()
16261647
{
16271648
m_eventLoopMutex.lock();

src/engine/internal/engine.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,8 @@ class Engine : public IEngine
168168
const std::string &userAgent() const override;
169169
void setUserAgent(const std::string &agent) override;
170170

171+
const std::unordered_set<std::string> &unsupportedBlocks() const override;
172+
171173
IClock *m_clock = nullptr;
172174
IAudioEngine *m_audioEngine = nullptr;
173175

@@ -287,6 +289,8 @@ class Engine : public IEngine
287289
sigslot::signal<const std::string &> m_questionAsked;
288290
sigslot::signal<> m_questionAborted;
289291
sigslot::signal<const std::string &> m_questionAnswered;
292+
293+
std::unordered_set<std::string> m_unsupportedBlocks;
290294
};
291295

292296
} // namespace libscratchcpp

test/engine/engine_test.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2133,6 +2133,15 @@ TEST(EngineTest, UserAgent)
21332133
ASSERT_EQ(engine.userAgent(), "test");
21342134
}
21352135

2136+
TEST(EngineTest, UnsupportedBlocks)
2137+
{
2138+
Project p("unsupported_blocks.sb3");
2139+
ASSERT_TRUE(p.load());
2140+
ASSERT_EQ(
2141+
p.engine()->unsupportedBlocks(),
2142+
std::unordered_set<std::string>({ "ev3_motorTurnClockwise", "ev3_motorSetPower", "ev3_getMotorPosition", "ev3_whenButtonPressed", "ev3_getBrightness", "ev3_getDistance" }));
2143+
}
2144+
21362145
TEST(EngineTest, NoCrashAfterStop)
21372146
{
21382147
// Regtest for #186

test/mocks/enginemock.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@ class EngineMock : public IEngine
144144

145145
MOCK_METHOD(const std::string &, userAgent, (), (const, override));
146146
MOCK_METHOD(void, setUserAgent, (const std::string &), (override));
147+
148+
MOCK_METHOD(const std::unordered_set<std::string> &, unsupportedBlocks, (), (const, override));
147149
};
148150

149151
} // namespace libscratchcpp

test/unsupported_blocks.sb3

2.06 KB
Binary file not shown.

0 commit comments

Comments
 (0)