Skip to content

Commit f5a264f

Browse files
committed
Load costume data
1 parent d7ddd1e commit f5a264f

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

src/internal/scratch3reader.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
#include "scratch3reader.h"
1616
#include "reader_common.h"
17-
#include "zipreader.h"
1817

1918
using namespace libscratchcpp;
2019
using json = nlohmann::json;
@@ -211,6 +210,11 @@ bool Scratch3Reader::load()
211210
costume->setRotationCenterX(jsonCostume["rotationCenterX"]);
212211
READER_STEP(step, "target -> costume -> rotationCenterY");
213212
costume->setRotationCenterY(jsonCostume["rotationCenterY"]);
213+
214+
void *data;
215+
m_zipReader->readFile(costume->fileName(), &data);
216+
costume->setData(static_cast<char *>(data));
217+
214218
target->addCostume(costume);
215219
}
216220

@@ -360,11 +364,11 @@ std::vector<std::string> Scratch3Reader::extensions()
360364
void Scratch3Reader::read()
361365
{
362366
// Read project.json
363-
ZipReader reader(fileName());
364-
if (reader.open()) {
367+
m_zipReader = std::make_unique<ZipReader>(fileName());
368+
if (m_zipReader->open()) {
365369
// Parse the JSON
366370
try {
367-
m_json = json::parse(reader.readFileToString("project.json"));
371+
m_json = json::parse(m_zipReader->readFileToString("project.json"));
368372
} catch (std::exception &e) {
369373
printErr("invalid JSON file", e.what());
370374
}

src/internal/scratch3reader.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#include "iprojectreader.h"
66
#include <nlohmann/json.hpp>
77

8+
#include "zipreader.h"
9+
810
namespace libscratchcpp
911
{
1012

@@ -20,6 +22,7 @@ class Scratch3Reader : public IProjectReader
2022

2123
private:
2224
void read();
25+
std::unique_ptr<ZipReader> m_zipReader;
2326
nlohmann::json m_json = "";
2427
std::vector<std::shared_ptr<Target>> m_targets;
2528
std::vector<std::shared_ptr<Broadcast>> m_broadcasts;

test/load_project/load_project_test.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@ TEST(LoadProjectTest, EmptyProject)
5454
ASSERT_FALSE(backdrop->id().empty());
5555
ASSERT_EQ(backdrop->fileName(), backdrop->id() + ".svg");
5656
ASSERT_EQ(backdrop->dataFormat(), "svg");
57+
ASSERT_TRUE(backdrop->data());
58+
ASSERT_EQ(
59+
strcmp(
60+
backdrop->data(),
61+
"<svg version=\"1.1\" width=\"2\" height=\"2\" viewBox=\"-1 -1 2 2\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n <!-- Exported by Scratch - "
62+
"http://scratch.mit.edu/ -->\n</svg>"),
63+
0);
5764

5865
i++;
5966
}

0 commit comments

Comments
 (0)