Per-level custom asset loading functionality (#262)

This commit is contained in:
Matt Aaldenberg
2020-05-31 19:31:02 -04:00
committed by GitHub
parent cfcfccf58b
commit b217fec3aa
6 changed files with 127 additions and 20 deletions

View File

@@ -18,6 +18,7 @@
#include <string>
#include <utf8/unchecked.h>
#include <physfs.h>
edlevelclass::edlevelclass()
{
@@ -78,6 +79,10 @@ bool compare_nocase (std::string first, std::string second)
return false;
}
static bool endsWith(const std::string& str, const std::string& suffix) {
return str.size() >= suffix.size() && 0 == str.compare(str.size()-suffix.size(), suffix.size(), suffix);
}
void replace_all(std::string& str, const std::string& from, const std::string& to)
{
if (from.empty())
@@ -1640,6 +1645,38 @@ void editorclass::load(std::string& _path)
_path = levelDir + _path;
}
FILESYSTEM_unmountassets();
std::string zippath = "levels/" + _path.substr(7,_path.size()-14) + ".data.zip";
std::string dirpath = "levels/" + _path.substr(7,_path.size()-14) + "/";
std::string zip_path;
const char* cstr = PHYSFS_getRealDir(_path.c_str());
if (cstr) zip_path = cstr;
if (cstr && FILESYSTEM_directoryExists(zippath.c_str())) {
printf("Custom asset directory exists at %s\n",zippath.c_str());
FILESYSTEM_mount(zippath.c_str());
graphics.reloadresources();
music.init();
} else if (zip_path != "data.zip" && !endsWith(zip_path, "/data.zip") && endsWith(zip_path, ".zip")) {
printf("Custom asset directory is .zip at %s\n", zip_path.c_str());
PHYSFS_File* zip = PHYSFS_openRead(zip_path.c_str());
zip_path += ".data.zip";
if (zip == NULL) {
printf("error loading .zip: %s\n", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
} else if (PHYSFS_mountHandle(zip, zip_path.c_str(), "/", 0) == 0) {
printf("error mounting .zip: %s\n", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
} else {
graphics.assetdir = zip_path;
}
graphics.reloadresources();
} else if (FILESYSTEM_directoryExists(dirpath.c_str())) {
printf("Custom asset directory exists at %s\n",dirpath.c_str());
FILESYSTEM_mount(dirpath.c_str());
graphics.reloadresources();
} else {
printf("Custom asset directory does not exist\n");
}
TiXmlDocument doc;
if (!FILESYSTEM_loadTiXmlDocument(_path.c_str(), &doc))
{
@@ -3661,6 +3698,13 @@ void editorinput()
game.press_action = true;
};
if (key.keymap[SDLK_F9] && (ed.keydelay==0)) {
ed.keydelay = 30;
ed.note="Reloaded resources";
ed.notedelay=45;
graphics.reloadresources();
}
if (key.isDown(KEYBOARD_ENTER)) game.press_map = true;
if (key.isDown(27) && !ed.settingskey)
{