Skip to content

Commit 3878059

Browse files
committed
Feat: Deserialize project name and materials
1 parent dd1dd9f commit 3878059

File tree

4 files changed

+117
-35
lines changed

4 files changed

+117
-35
lines changed

src/atta/file/manager.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <atta/file/manager.h>
1515
#include <atta/file/watchers/linuxFileWatcher.h>
1616
#include <atta/file/watchers/nullFileWatcher.h>
17+
#include <atta/resource/interface.h>
1718

1819
#ifdef ATTA_OS_WEB
1920
// Need to use "linux" build-in commands instead of std::filesystem
@@ -72,12 +73,19 @@ bool Manager::openProjectImpl(fs::path projectFile) {
7273
if (!fs::exists(projectFile))
7374
_projectSerializer->serialize();
7475

75-
// Clear components and read project file
76+
// Clear components before loading
7677
component::clear();
78+
// Clear resources before loading
79+
resource::destroyResources<resource::Material>();
7780

81+
// Load project
7882
event::ProjectBeforeDeserialize ed;
7983
event::publish(ed);
80-
_projectSerializer->deserialize();
84+
bool success = _projectSerializer->deserialize();
85+
if (!success) {
86+
LOG_ERROR("file::Manager", "Failed to open project [w]$0[]", fs::absolute(projectFile));
87+
return false;
88+
}
8189

8290
// Watch project directory file changes
8391
_fileWatcher->addWatch(_project->getDirectory());

src/atta/file/project/project.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class Project final {
3333
std::vector<fs::path> _resourceRootPaths;
3434

3535
friend Manager;
36+
friend class ProjectSerializer;
3637
};
3738

3839
} // namespace atta::file

src/atta/file/project/projectSerializer.cpp

Lines changed: 98 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -59,26 +59,37 @@ void ProjectSerializer::serialize() {
5959
LOG_SUCCESS("file::ProjectSerializer", "Project [w]$0[] was saved", _project->getName());
6060
}
6161

62-
void ProjectSerializer::deserialize() {
63-
Section section("Section");
62+
bool ProjectSerializer::deserialize() {
6463
fs::path attaFile = _project->getFile();
64+
Serializer serializer;
65+
serializer.deserialize(attaFile);
6566

66-
std::ifstream is(attaFile, std::ifstream::in | std::ifstream::binary);
67-
68-
// Deserialize version
69-
// std::string version;
70-
// read(is, version);
71-
// section.deserialize(is);
72-
// is.close();
73-
74-
// Deserialize data
75-
// deserializeHeader(section["header"]);
76-
// deserializeConfig(section["config"]);
77-
// deserializeComponentModule(section["componentModule"]);
78-
// deserializeGraphicsModule(section["graphicsModule"]);
79-
// deserializeResourceModule(section["resourceModule"]);
80-
// deserializePhysicsModule(section["physicsModule"]);
81-
// deserializeSensorModule(section["sensorModule"]);
67+
// Make sure atta versions match
68+
bool canLoadProject = false;
69+
for (const Section& section : serializer.getSections()) {
70+
if (section.getName() == "project") {
71+
canLoadProject = deserializeProject(section);
72+
break;
73+
}
74+
}
75+
if (!canLoadProject)
76+
return false;
77+
78+
for (const Section& section : serializer.getSections()) {
79+
if (section.getName() == "config")
80+
deserializeConfig(section);
81+
else if (section.getName() == "graphics")
82+
deserializeGraphicsModule(section);
83+
else if (section.getName() == "physics")
84+
deserializePhysicsModule(section);
85+
else if (section.getName() == "sensor")
86+
deserializeSensorModule(section);
87+
else if (section.getName() == "material")
88+
deserializeMaterial(section);
89+
else if (section.getName() == "node")
90+
deserializeNode(section);
91+
}
92+
return true;
8293
}
8394

8495
Section ProjectSerializer::serializeProject() {
@@ -290,12 +301,74 @@ std::vector<Section> ProjectSerializer::serializeNodes() {
290301
return sections;
291302
}
292303

293-
void ProjectSerializer::deserializeProject(Section& section) {}
294-
void ProjectSerializer::deserializeConfig(Section& section) {}
295-
void ProjectSerializer::deserializeGraphicsModule(Section& section) {}
296-
void ProjectSerializer::deserializeResourceModule(Section& section) {}
297-
void ProjectSerializer::deserializePhysicsModule(Section& section) {}
298-
void ProjectSerializer::deserializeSensorModule(Section& section) {}
299-
void ProjectSerializer::deserializeNode(Section& section) {}
304+
bool ProjectSerializer::deserializeProject(const Section& section) {
305+
if (!section.contains("attaVersion")) {
306+
LOG_WARN("file::ProjectSerializer", "Project [w]$0[] does not have an atta version. The project will not be loaded", _project->getName());
307+
return false;
308+
}
309+
std::string projectAttaVersion = std::string(section["attaVersion"]);
310+
if (projectAttaVersion != ATTA_VERSION) {
311+
LOG_WARN(
312+
"file::ProjectSerializer",
313+
"Project [w]$0[] was created with atta version [w]$1[], which does not match this atta version [w]$2[]. The project will not be loaded",
314+
_project->getName(), projectAttaVersion, ATTA_VERSION);
315+
return false;
316+
}
317+
318+
// Load project name
319+
if (section.contains("name"))
320+
_project->_name = std::string(section["name"]);
321+
return true;
322+
}
323+
324+
void ProjectSerializer::deserializeConfig(const Section& section) {
325+
if (section.contains("dt"))
326+
Config::setDt(float(section["dt"]));
327+
if (section.contains("desiredStepSpeed"))
328+
Config::setDesiredStepSpeed(float(section["desiredStepSpeed"]));
329+
}
330+
331+
void ProjectSerializer::deserializeGraphicsModule(const Section& section) {
332+
if (section.contains("graphicsFPS"))
333+
gfx::setGraphicsFPS(float(section["graphicsFPS"]));
334+
if (section.contains("viewportRendering"))
335+
ui::setViewportRendering(bool(section["viewportRendering"]));
336+
}
337+
338+
void ProjectSerializer::deserializePhysicsModule(const Section& section) {}
339+
340+
void ProjectSerializer::deserializeSensorModule(const Section& section) {}
341+
342+
void ProjectSerializer::deserializeMaterial(const Section& section) {
343+
res::Material::CreateInfo material{};
344+
if (!section.contains("id")) {
345+
LOG_WARN("file::ProjectSerializer", "Material section does not have an id. The material will not be loaded");
346+
return;
347+
}
348+
std::string id = std::string(section["id"]);
349+
350+
if (section.contains("color"))
351+
material.color = vec3f(section["color"]);
352+
if (section.contains("colorImage"))
353+
material.colorImage = StringId(std::string(section["colorImage"]));
354+
if (section.contains("metallic"))
355+
material.metallic = float(section["metallic"]);
356+
if (section.contains("metallicImage"))
357+
material.metallicImage = StringId(std::string(section["metallicImage"]));
358+
if (section.contains("roughness"))
359+
material.roughness = float(section["roughness"]);
360+
if (section.contains("roughnessImage"))
361+
material.roughnessImage = StringId(std::string(section["roughnessImage"]));
362+
if (section.contains("ao"))
363+
material.ao = float(section["ao"]);
364+
if (section.contains("aoImage"))
365+
material.aoImage = StringId(std::string(section["aoImage"]));
366+
if (section.contains("normalImage"))
367+
material.normalImage = StringId(std::string(section["normalImage"]));
368+
369+
res::create<res::Material>(id, material);
370+
}
371+
372+
void ProjectSerializer::deserializeNode(const Section& section) {}
300373

301374
} // namespace atta::file

src/atta/file/project/projectSerializer.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class ProjectSerializer final {
1717
~ProjectSerializer();
1818

1919
void serialize();
20-
void deserialize();
20+
bool deserialize();
2121

2222
private:
2323
Section serializeProject();
@@ -28,13 +28,13 @@ class ProjectSerializer final {
2828
std::vector<Section> serializeResources();
2929
std::vector<Section> serializeNodes();
3030

31-
void deserializeProject(Section& section);
32-
void deserializeConfig(Section& section);
33-
void deserializeGraphicsModule(Section& section);
34-
void deserializeResourceModule(Section& section);
35-
void deserializePhysicsModule(Section& section);
36-
void deserializeSensorModule(Section& section);
37-
void deserializeNode(Section& section);
31+
bool deserializeProject(const Section& section);
32+
void deserializeConfig(const Section& section);
33+
void deserializeGraphicsModule(const Section& section);
34+
void deserializePhysicsModule(const Section& section);
35+
void deserializeSensorModule(const Section& section);
36+
void deserializeMaterial(const Section& section);
37+
void deserializeNode(const Section& section);
3838

3939
std::shared_ptr<Project> _project;
4040
};

0 commit comments

Comments
 (0)