Skip to content
This repository was archived by the owner on Jan 5, 2024. It is now read-only.

Commit 86f502c

Browse files
committed
Fix SceneEditor failing to save
Refactor SceneEditor::SaveScene
1 parent f5fd5b4 commit 86f502c

File tree

2 files changed

+69
-96
lines changed

2 files changed

+69
-96
lines changed

Activities/SceneEditor.cpp

Lines changed: 62 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -647,95 +647,74 @@ void SceneEditor::Draw(BITMAP* pTargetBitmap, const Vector &targetPos)
647647
EditorActivity::Draw(pTargetBitmap, targetPos);
648648
}
649649

650+
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
650651

651-
//////////////////////////////////////////////////////////////////////////////////////////
652-
// Method: SaveScene
653-
//////////////////////////////////////////////////////////////////////////////////////////
654-
// Description: Saves the current scene to an appropriate ini file, and asks user if
655-
// they want to overwrite first if scene of this name exists.
652+
bool SceneEditor::SaveScene(const std::string &saveAsName, bool forceOverwrite) {
653+
Scene *editedScene = g_SceneMan.GetScene();
654+
editedScene->SetPresetName(saveAsName);
656655

657-
bool SceneEditor::SaveScene(std::string saveAsName, bool forceOverwrite)
658-
{
659-
// Set the name of the current scene in effect
660-
g_SceneMan.GetScene()->SetPresetName(saveAsName);
656+
std::string dataModuleName = g_PresetMan.GetDataModule(m_ModuleSpaceID)->GetFileName();
657+
bool savingToUserScenesModule = (dataModuleName == c_UserScenesModuleName);
661658

662-
if (g_PresetMan.GetDataModule(m_ModuleSpaceID)->GetFileName() == c_UserScenesModuleName)
663-
{
664-
std::string sceneFilePath(g_PresetMan.GetDataModule(m_ModuleSpaceID)->GetFileName() + "/" + saveAsName + ".ini");
665-
std::string previewFilePath(g_PresetMan.GetDataModule(m_ModuleSpaceID)->GetFileName() + "/" + saveAsName + ".preview.png");
666-
if (g_PresetMan.AddEntityPreset(g_SceneMan.GetScene(), m_ModuleSpaceID, forceOverwrite, sceneFilePath))
667-
{
668-
// Save preview
669-
g_SceneMan.GetScene()->SavePreview(previewFilePath);
670-
671-
// Does ini already exist? If yes, then no need to add it to a scenes.ini etc
672-
bool sceneFileExisted = System::PathExistsCaseSensitive(sceneFilePath.c_str());
673-
// Create the writer
674-
Writer sceneWriter(sceneFilePath.c_str(), false);
675-
sceneWriter.NewProperty("AddScene");
676-
// Write the scene out to the new ini
677-
sceneWriter << g_SceneMan.GetScene();
678-
return m_HasEverBeenSaved = true;
679-
}
680-
else
681-
{
682-
// Gotto ask if we can overwrite the existing scene
683-
m_PreviousMode = EditorActivity::SAVEDIALOG;
684-
m_EditorMode = EditorActivity::OVERWRITEDIALOG;
685-
m_ModeChange = true;
686-
}
659+
std::string dataModuleFullPath = g_PresetMan.GetFullModulePath(dataModuleName);
660+
std::string sceneSavePath;
661+
std::string previewSavePath;
662+
663+
if (savingToUserScenesModule) {
664+
sceneSavePath = dataModuleFullPath + "/" + saveAsName + ".ini";
665+
previewSavePath = dataModuleFullPath + "/" + saveAsName + ".preview.png";
666+
} else {
667+
sceneSavePath = dataModuleFullPath + "/Scenes/" + saveAsName + ".ini";
668+
previewSavePath = dataModuleFullPath + "/Scenes/" + saveAsName + ".preview.png";
687669
}
688-
else
689-
{
690-
// Try to save to the data module
691-
std::string sceneFilePath(g_PresetMan.GetDataModule(m_ModuleSpaceID)->GetFileName() + "/Scenes/" + saveAsName + ".ini");
692-
std::string previewFilePath(g_PresetMan.GetDataModule(m_ModuleSpaceID)->GetFileName() + "/Scenes/" + saveAsName + ".preview.png");
693-
if (g_PresetMan.AddEntityPreset(g_SceneMan.GetScene(), m_ModuleSpaceID, forceOverwrite, sceneFilePath))
694-
{
695-
// Save preview
696-
g_SceneMan.GetScene()->SavePreview(previewFilePath);
697-
698-
// Does ini already exist? If yes, then no need to add it to a scenes.ini etc
699-
bool sceneFileExisted = System::PathExistsCaseSensitive(sceneFilePath.c_str());
700-
// Create the writer
701-
Writer sceneWriter(sceneFilePath.c_str(), false);
702-
sceneWriter.NewProperty("AddScene");
703-
// TODO: Check if the ini file already exists, and then ask if overwrite
704-
// Write the scene out to the new ini
705-
sceneWriter << g_SceneMan.GetScene();
706-
707-
if (!sceneFileExisted)
708-
{
709-
// First find/create a .rte/Scenes.ini file to include the new .ini into
710-
std::string scenesFilePath(g_PresetMan.GetDataModule(m_ModuleSpaceID)->GetFileName() + "/Scenes.ini");
711-
bool scenesFileExisted = System::PathExistsCaseSensitive(scenesFilePath.c_str());
712-
Writer scenesWriter(scenesFilePath.c_str(), true);
713-
scenesWriter.NewProperty("\nIncludeFile");
714-
scenesWriter << sceneFilePath;
715-
716-
// Also add a line to the end of the modules' Index.ini to include the newly created Scenes.ini next startup
717-
// If it's already included, it doens't matter, the definitions will just bounce the second time
718-
if (!scenesFileExisted)
719-
{
720-
std::string indexFilePath(g_PresetMan.GetDataModule(m_ModuleSpaceID)->GetFileName() + "/Index.ini");
721-
Writer indexWriter(indexFilePath.c_str(), true);
722-
// Add extra tab since the DataModule has everything indented
723-
indexWriter.NewProperty("\tIncludeFile");
724-
indexWriter << scenesFilePath;
725-
}
726-
}
727-
return m_HasEverBeenSaved = true;
728-
}
729-
else
730-
{
731-
// Gotto ask if we can overwrite the existing scene
732-
m_PreviousMode = EditorActivity::SAVEDIALOG;
733-
m_EditorMode = EditorActivity::OVERWRITEDIALOG;
734-
m_ModeChange = true;
670+
671+
if (g_PresetMan.AddEntityPreset(editedScene, m_ModuleSpaceID, forceOverwrite, sceneSavePath)) {
672+
if (Writer sceneWriter(sceneSavePath, false); !sceneWriter.WriterOK()) {
673+
RTEError::ShowMessageBox("Failed to create Writer to path:\n\n" + sceneSavePath + "!\n\nTHE EDITED SCENE PRESET WAS NOT SAVED!!!");
674+
} else {
675+
// TODO: Check if the ini file already exists, and then ask if overwrite.
676+
sceneWriter.NewPropertyWithValue("AddScene", editedScene);
677+
sceneWriter.EndWrite();
678+
679+
editedScene->SavePreview(previewSavePath);
680+
m_HasEverBeenSaved = true;
681+
682+
if (!savingToUserScenesModule) {
683+
// First find/create a Scenes.ini file to include the new .ini into.
684+
std::string scenesFilePath(dataModuleFullPath + "/Scenes.ini");
685+
bool scenesFileExists = System::PathExistsCaseSensitive(scenesFilePath);
686+
687+
if (Writer scenesFileWriter(scenesFilePath, true); !scenesFileWriter.WriterOK()) {
688+
RTEError::ShowMessageBox("Failed to create Writer to path:\n\n" + scenesFilePath + "!\n\nThe edited Scene preset was saved but will not be loaded on next game start!\nPlease include the Scene preset manually!");
689+
} else {
690+
scenesFileWriter.NewPropertyWithValue("IncludeFile", sceneSavePath);
691+
scenesFileWriter.EndWrite();
692+
693+
// Append to the end of the modules' Index.ini to include the newly created Scenes.ini next startup.
694+
// If it's somehow already included without actually existing, it doesn't matter, the definitions will just bounce the second time.
695+
if (!scenesFileExists) {
696+
std::string indexFilePath = dataModuleFullPath + "/Index.ini";
697+
698+
if (Writer indexWriter(indexFilePath, true); !indexWriter.WriterOK()) {
699+
RTEError::ShowMessageBox("Failed to create Writer to path:\n\n" + indexFilePath + "!\n\nThe edited Scene preset was saved but will not be loaded on next game start!\nPlease include the Scene preset manually!");
700+
} else {
701+
// Add extra tab since the DataModule has everything indented.
702+
indexWriter.NewProperty("\tIncludeFile");
703+
indexWriter << scenesFilePath;
704+
indexWriter.EndWrite();
705+
}
706+
}
707+
}
708+
}
709+
return true;
735710
}
711+
} else {
712+
// Got to ask if we can overwrite the existing preset.
713+
m_PreviousMode = EditorMode::SAVEDIALOG;
714+
m_EditorMode = EditorMode::OVERWRITEDIALOG;
715+
m_ModeChange = true;
736716
}
737-
738-
return false;
717+
return false;
739718
}
740719

741720

Activities/SceneEditor.h

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -209,19 +209,13 @@ ClassInfoGetters;
209209

210210
protected:
211211

212-
213-
//////////////////////////////////////////////////////////////////////////////////////////
214-
// Method: SaveScene
215-
//////////////////////////////////////////////////////////////////////////////////////////
216-
// Description: Saves the current scene to an appropriate ini file, and asks user if
217-
// they want to overwrite first if scene of this name exists.
218-
// Arguments: The name of the new scene to be saved.
219-
// Whetehr to force any existing Scene of that name to be overwritten if
220-
// it already exists.
221-
// Return value: Whether actually managed to save. Will return false both if a scene
222-
// of this name already exists, or if other error.
223-
224-
bool SaveScene(std::string saveAsName, bool forceOverwrite = false);
212+
/// <summary>
213+
/// Saves the current Scene to an appropriate ini file, and asks user if they want to overwrite first if scene of this name exists.
214+
/// </summary>
215+
/// <param name="saveAsName">The name of the new Scene to be saved.</param>
216+
/// <param name="forceOverwrite">Whether to force any existing Scene of that name to be overwritten if it already exists.</param>
217+
/// <returns>Whether actually managed to save. Will return false both if a scene of this name already exists, or if other error.</returns>
218+
bool SaveScene(const std::string &saveAsName, bool forceOverwrite = false);
225219

226220

227221
//////////////////////////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)