Skip to content

Commit f7065dd

Browse files
committed
Add a way to load project data without setting the file name
1 parent 669c9eb commit f7065dd

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

src/internal/iprojectreader.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class IProjectReader
2727
virtual void setFileName(const std::string &fileName) final { m_fileName = fileName; }
2828

2929
virtual bool load() = 0;
30+
virtual bool loadData(const std::string &data) = 0;
3031
virtual bool isValid() = 0;
3132
virtual void clear() = 0;
3233
virtual std::vector<std::shared_ptr<Target>> targets() = 0;

src/internal/scratch3reader.cpp

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -235,9 +235,11 @@ bool Scratch3Reader::load()
235235
READER_STEP(step, "target -> costume -> rotationCenterY");
236236
costume->setRotationCenterY(jsonCostume["rotationCenterY"]);
237237

238-
void *data;
239-
unsigned int size = m_zipReader->readFile(costume->fileName(), &data);
240-
costume->setData(size, data);
238+
if (m_zipReader && !costume->data()) {
239+
void *data;
240+
unsigned int size = m_zipReader->readFile(costume->fileName(), &data);
241+
costume->setData(size, data);
242+
}
241243

242244
target->addCostume(costume);
243245
}
@@ -349,6 +351,18 @@ bool Scratch3Reader::load()
349351
return true;
350352
}
351353

354+
bool Scratch3Reader::loadData(const std::string &data)
355+
{
356+
try {
357+
m_json = json::parse(data);
358+
} catch (std::exception &e) {
359+
printErr("invalid JSON file", e.what());
360+
return false;
361+
}
362+
363+
return load();
364+
}
365+
352366
bool Scratch3Reader::isValid()
353367
{
354368
if (m_json == "")
@@ -391,6 +405,9 @@ std::vector<std::string> Scratch3Reader::extensions()
391405

392406
void Scratch3Reader::read()
393407
{
408+
if (fileName().empty())
409+
return;
410+
394411
// Read project.json
395412
m_zipReader = std::make_unique<ZipReader>(fileName());
396413
if (m_zipReader->open()) {

src/internal/scratch3reader.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class Scratch3Reader : public IProjectReader
1414
{
1515
public:
1616
bool load() override;
17+
bool loadData(const std::string &data) override;
1718
bool isValid() override;
1819
void clear() override;
1920
std::vector<std::shared_ptr<Target>> targets() override;

0 commit comments

Comments
 (0)