Skip to content

Commit 94e8983

Browse files
committed
Created saveSceneFile used by saveSceneForPlayStartup and saveSceneToPath
1 parent 3443129 commit 94e8983

2 files changed

Lines changed: 52 additions & 71 deletions

File tree

editor/Project.cpp

Lines changed: 51 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -2523,10 +2523,9 @@ void editor::Project::saveScene(uint32_t sceneId) {
25232523
}
25242524
}
25252525

2526-
void editor::Project::saveSceneToPath(uint32_t sceneId, const std::filesystem::path& path) {
2527-
SceneProject* sceneProject = getScene(sceneId);
2528-
if (!sceneProject) {
2529-
return;
2526+
bool editor::Project::saveSceneFile(SceneProject* sceneProject, const std::filesystem::path& path) {
2527+
if (!sceneProject || path.empty()) {
2528+
return false;
25302529
}
25312530

25322531
fs::path fullPath = path;
@@ -2538,43 +2537,72 @@ void editor::Project::saveSceneToPath(uint32_t sceneId, const std::filesystem::p
25382537
fs::path relPath = fs::relative(fullPath, getProjectPath(), ec);
25392538
if (ec || relPath.empty()) {
25402539
Out::error("Scene filepath must be relative to project path: %s", path.string().c_str());
2541-
return;
2540+
return false;
25422541
}
25432542

2544-
// Check if this scene has entities in bundles and save them first
25452543
for (const auto& [filepath, bundle] : entityBundles) {
2546-
if (bundle.instances.find(sceneId) != bundle.instances.end()) {
2547-
if (bundle.isModified) {
2548-
saveEntityBundleToDisk(filepath);
2549-
}
2544+
if (bundle.instances.find(sceneProject->id) != bundle.instances.end() && bundle.isModified) {
2545+
saveEntityBundleToDisk(filepath);
25502546
}
25512547
}
25522548

2553-
calculateSceneMaxValues(sceneProject, sceneProject->maxValues);
2554-
collectSceneShaderKeys(sceneProject, sceneProject->shaderKeys);
2549+
SceneMaxValues maxValues;
2550+
calculateSceneMaxValues(sceneProject, maxValues);
2551+
sceneProject->maxValues = maxValues;
2552+
2553+
std::set<ShaderKey> shaderKeys;
2554+
collectSceneShaderKeys(sceneProject, shaderKeys);
2555+
sceneProject->shaderKeys = std::move(shaderKeys);
25552556

25562557
updateSceneCppScripts(sceneProject);
25572558
updateSceneBundles(sceneProject);
25582559

2559-
YAML::Node root = Stream::encodeSceneProject(this, sceneProject);
2560-
std::ofstream fout(fullPath.string());
2561-
fout << YAML::Dump(root);
2562-
fout.close();
2560+
try {
2561+
YAML::Node root = Stream::encodeSceneProject(this, sceneProject);
2562+
std::ofstream fout(fullPath.string());
2563+
if (!fout) {
2564+
Out::error("Failed to open scene file for writing: %s", fullPath.string().c_str());
2565+
return false;
2566+
}
2567+
2568+
fout << YAML::Dump(root);
2569+
fout.close();
2570+
2571+
sceneProject->filepath = relPath;
2572+
sceneProject->isModified = false;
2573+
TerrainEditWindow::cleanUnusedTerrainMaps(this);
2574+
} catch (const std::exception& e) {
2575+
Out::error("Failed to save scene file: %s", e.what());
2576+
return false;
2577+
}
2578+
2579+
return true;
2580+
}
2581+
2582+
void editor::Project::saveSceneToPath(uint32_t sceneId, const std::filesystem::path& path) {
2583+
SceneProject* sceneProject = getScene(sceneId);
2584+
if (!sceneProject) {
2585+
return;
2586+
}
2587+
2588+
fs::path fullPath = path;
2589+
if (fullPath.is_relative()) {
2590+
fullPath = getProjectPath() / fullPath;
2591+
}
25632592

25642593
std::string oldFilepath = sceneProject->filepath.string();
2565-
sceneProject->filepath = relPath;
2566-
sceneProject->isModified = false;
2594+
if (!saveSceneFile(sceneProject, path)) {
2595+
return;
2596+
}
25672597

25682598
// Update tabs: if filepath changed, update existing tab entry
25692599
if (sceneProject->opened) {
2570-
if (!oldFilepath.empty() && oldFilepath != relPath.string()) {
2600+
if (!oldFilepath.empty() && oldFilepath != sceneProject->filepath.string()) {
25712601
removeTab(TabType::SCENE, oldFilepath);
25722602
}
2573-
addTab(TabType::SCENE, relPath.string());
2603+
addTab(TabType::SCENE, sceneProject->filepath.string());
25742604
}
25752605

2576-
TerrainEditWindow::cleanUnusedTerrainMaps(this);
2577-
25782606
saveProject();
25792607

25802608
std::vector<BundleInstanceInfo> bundleInstances = generator.writeBundleSources(entityBundles, sceneId, getProjectPath(),getProjectInternalPath());
@@ -5011,55 +5039,7 @@ bool editor::Project::saveSceneForPlayStartup(SceneProject* sceneProject) {
50115039
return false;
50125040
}
50135041

5014-
fs::path fullPath = sceneProject->filepath;
5015-
if (fullPath.is_relative()) {
5016-
fullPath = getProjectPath() / fullPath;
5017-
}
5018-
5019-
std::error_code ec;
5020-
fs::path relPath = fs::relative(fullPath, getProjectPath(), ec);
5021-
if (ec || relPath.empty()) {
5022-
Out::error("Scene filepath must be relative to project path: %s", sceneProject->filepath.string().c_str());
5023-
return false;
5024-
}
5025-
5026-
for (const auto& [filepath, bundle] : entityBundles) {
5027-
if (bundle.instances.find(sceneProject->id) != bundle.instances.end() && bundle.isModified) {
5028-
saveEntityBundleToDisk(filepath);
5029-
}
5030-
}
5031-
5032-
SceneMaxValues maxValues;
5033-
calculateSceneMaxValues(sceneProject, maxValues);
5034-
sceneProject->maxValues = maxValues;
5035-
5036-
std::set<ShaderKey> shaderKeys;
5037-
collectSceneShaderKeys(sceneProject, shaderKeys);
5038-
sceneProject->shaderKeys = std::move(shaderKeys);
5039-
5040-
updateSceneCppScripts(sceneProject);
5041-
updateSceneBundles(sceneProject);
5042-
5043-
try {
5044-
YAML::Node root = Stream::encodeSceneProject(this, sceneProject);
5045-
std::ofstream fout(fullPath.string());
5046-
if (!fout) {
5047-
Out::error("Failed to open scene file for writing: %s", fullPath.string().c_str());
5048-
return false;
5049-
}
5050-
5051-
fout << YAML::Dump(root);
5052-
fout.close();
5053-
5054-
sceneProject->filepath = relPath;
5055-
sceneProject->isModified = false;
5056-
TerrainEditWindow::cleanUnusedTerrainMaps(this);
5057-
} catch (const std::exception& e) {
5058-
Out::error("Failed to save scene for play startup: %s", e.what());
5059-
return false;
5060-
}
5061-
5062-
return true;
5042+
return saveSceneFile(sceneProject, sceneProject->filepath);
50635043
}
50645044

50655045
void editor::Project::runPlayStartup(const std::shared_ptr<PlaySession>& session, uint32_t sceneId) {

editor/Project.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ namespace doriax::editor{
243243
void runPlayStartup(const std::shared_ptr<PlaySession>& session, uint32_t sceneId);
244244
void failPlayStartup(const std::shared_ptr<PlaySession>& session, uint32_t sceneId, const std::string& message,
245245
const std::string& alertTitle = "", const std::string& alertMessage = "");
246+
bool saveSceneFile(SceneProject* sceneProject, const std::filesystem::path& path);
246247
bool saveSceneForPlayStartup(SceneProject* sceneProject);
247248

248249
void collectInvolvedScenes(uint32_t sceneId, std::vector<uint32_t>& involvedSceneIds);

0 commit comments

Comments
 (0)