1
0
Fork 0
mirror of https://github.com/TerryCavanagh/VVVVVV.git synced 2024-12-23 01:59:43 +01:00

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.
This commit is contained in:
Misa 2020-11-03 09:55:26 -08:00 committed by Ethan Lee
parent 3f954a169a
commit 5eab074e4d
2 changed files with 15 additions and 4 deletions

View file

@ -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);
}

View file

@ -191,6 +191,7 @@ class editorclass{
std::string note;
std::string keybuffer;
std::string filename;
std::string loaded_filepath;
int drawmode;
int tilex, tiley;