From 5eab074e4da880964c882fbcf790a0021415e997 Mon Sep 17 00:00:00 2001 From: Misa Date: Tue, 3 Nov 2020 09:55:26 -0800 Subject: [PATCH] Remember loaded custom level and read from it when saving My previous custom level forwards compatibility would only work if you saved to the same filename as you read from. But what if you saved to a new filename? Well, your extra XML is lost. Not to worry, I've introduced a variable that remembers the filepath of the currently-loaded level (the existing `filename` attribute is kind of weird and not really suited for this purpose, so). I tried to make it a simple `const char*`, but it turns out that's bad when you take the `c_str()` of an `std::string` that then gets destroyed, so it's best to use `std::string` in an `std::string` world. So now when you save a level, it'll attempt to open the original file, and if that doesn't exist then your extra XML gets lost anyway, but I shouldn't be expected to keep your XML if you delete the original file. --- desktop_version/src/editor.cpp | 18 ++++++++++++++---- desktop_version/src/editor.h | 1 + 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/desktop_version/src/editor.cpp b/desktop_version/src/editor.cpp index f9c6f91d..619f9f2f 100644 --- a/desktop_version/src/editor.cpp +++ b/desktop_version/src/editor.cpp @@ -383,6 +383,8 @@ void editorclass::reset() currentghosts = 0; onewaycol_override = false; + + loaded_filepath = ""; } void editorclass::gethooks() @@ -1730,6 +1732,7 @@ bool editorclass::load(std::string& _path) return false; } + loaded_filepath = _path; tinyxml2::XMLHandle hDoc(&doc); tinyxml2::XMLElement* pElem; @@ -2018,11 +2021,18 @@ bool editorclass::load(std::string& _path) bool editorclass::save(std::string& _path) { tinyxml2::XMLDocument doc; - bool already_exists = FILESYSTEM_loadTiXml2Document(("levels/" + _path).c_str(), doc); - if (!already_exists) + + std::string newpath("levels/" + _path); + + // Try to preserve the XML of the currently-loaded one + bool already_exists = !loaded_filepath.empty() && FILESYSTEM_loadTiXml2Document(loaded_filepath.c_str(), doc); + if (!already_exists && !loaded_filepath.empty()) { - printf("No %s found. Creating new file\n", _path.c_str()); + printf("Currently-loaded %s not found\n", loaded_filepath.c_str()); } + + loaded_filepath = newpath; + tinyxml2::XMLElement* msg; xml::update_declaration(doc); @@ -2150,7 +2160,7 @@ bool editorclass::save(std::string& _path) } xml::update_tag(data, "script", scriptString.c_str()); - return FILESYSTEM_saveTiXml2Document(("levels/" + _path).c_str(), doc); + return FILESYSTEM_saveTiXml2Document(newpath.c_str(), doc); } diff --git a/desktop_version/src/editor.h b/desktop_version/src/editor.h index 4986ddcf..d07e05e1 100644 --- a/desktop_version/src/editor.h +++ b/desktop_version/src/editor.h @@ -191,6 +191,7 @@ class editorclass{ std::string note; std::string keybuffer; std::string filename; + std::string loaded_filepath; int drawmode; int tilex, tiley;