Skip to content
This repository has been archived by the owner on Jun 7, 2018. It is now read-only.

Updated QuestFiles to use PhysFS 3.0 #1203

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 31 additions & 16 deletions src/core/QuestFiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ void set_solarus_write_dir(const std::string& solarus_write_dir) {
// First check that we can write in a directory.
if (!PHYSFS_setWriteDir(get_base_write_dir().c_str())) {
Debug::die(std::string("Cannot write in user directory '")
+ get_base_write_dir().c_str() + "': " + PHYSFS_getLastError());
+ get_base_write_dir().c_str() + "': " + PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
}

// Create the directory.
Expand All @@ -93,7 +93,7 @@ void set_solarus_write_dir(const std::string& solarus_write_dir) {
const std::string& full_write_dir = get_base_write_dir() + "/" + solarus_write_dir;
if (!PHYSFS_setWriteDir(full_write_dir.c_str())) {
Debug::die(std::string("Cannot set Solarus write directory to '")
+ full_write_dir + "': " + PHYSFS_getLastError());
+ full_write_dir + "': " + PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
}

// The quest subdirectory may be new, create it if needed.
Expand Down Expand Up @@ -134,12 +134,12 @@ SOLARUS_API bool open_quest(const std::string& program_name, const std::string&
std::string archive_quest_path_2 = quest_path + "/data.solarus.zip";

const std::string& base_dir = PHYSFS_getBaseDir();
PHYSFS_addToSearchPath(dir_quest_path.c_str(), 1); // data directory
PHYSFS_addToSearchPath(archive_quest_path_1.c_str(), 1); // data.solarus archive
PHYSFS_addToSearchPath(archive_quest_path_2.c_str(), 1); // data.solarus.zip archive
PHYSFS_addToSearchPath((base_dir + "/" + dir_quest_path).c_str(), 1);
PHYSFS_addToSearchPath((base_dir + "/" + archive_quest_path_1).c_str(), 1);
PHYSFS_addToSearchPath((base_dir + "/" + archive_quest_path_2).c_str(), 1);
PHYSFS_mount(dir_quest_path.c_str(), NULL, 1); // data directory
PHYSFS_mount(archive_quest_path_1.c_str(), NULL, 1); // data.solarus archive
PHYSFS_mount(archive_quest_path_2.c_str(), NULL, 1); // data.solarus.zip archive
PHYSFS_mount((base_dir + "/" + dir_quest_path).c_str(), NULL, 1);
PHYSFS_mount((base_dir + "/" + archive_quest_path_1).c_str(), NULL, 1);
PHYSFS_mount((base_dir + "/" + archive_quest_path_2).c_str(), NULL, 1);

// Set the engine root write directory.
set_solarus_write_dir(SOLARUS_WRITE_DIR);
Expand Down Expand Up @@ -266,7 +266,18 @@ SOLARUS_API bool data_file_exists(const std::string& file_name,
full_file_name = file_name;
}

return PHYSFS_exists(full_file_name.c_str()) && !PHYSFS_isDirectory(full_file_name.c_str());
if(!PHYSFS_exists(full_file_name.c_str())) {
return false;
}

PHYSFS_Stat stats;
if(!PHYSFS_stat(full_file_name.c_str(),&stats)) {
Debug::die(std::string("Cannot stat file '") + full_file_name
+ "': " + PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode())
);
}

return stats.filetype != PHYSFS_FILETYPE_DIRECTORY;
}

/**
Expand Down Expand Up @@ -305,7 +316,7 @@ SOLARUS_API std::string data_file_read(
size_t size = static_cast<size_t>(PHYSFS_fileLength(file));
std::vector<char> buffer(size);

PHYSFS_read(file, buffer.data(), 1, (PHYSFS_uint32) size);
PHYSFS_readBytes(file, buffer.data(), (PHYSFS_uint32) size);
PHYSFS_close(file);

return std::string(buffer.data(), size);
Expand All @@ -325,14 +336,14 @@ SOLARUS_API void data_file_save(
PHYSFS_file* file = PHYSFS_openWrite(file_name.c_str());
if (file == nullptr) {
Debug::die(std::string("Cannot open file '") + file_name
+ "' for writing: " + PHYSFS_getLastError()
+ "' for writing: " + PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode())
);
}

// save the memory buffer
if (PHYSFS_write(file, buffer.data(), (PHYSFS_uint32) buffer.size(), 1) == -1) {
if (PHYSFS_writeBytes(file, buffer.data(), (PHYSFS_uint32) buffer.size()) == -1) {
Debug::die(std::string("Cannot write file '") + file_name + "': "
+ PHYSFS_getLastError());
+ PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
}
PHYSFS_close(file);
}
Expand Down Expand Up @@ -403,7 +414,7 @@ SOLARUS_API void set_quest_write_dir(const std::string& quest_write_dir) {
if (!quest_write_dir_.empty()) {
// There was already a previous quest subdirectory: remove it from the
// search path.
PHYSFS_removeFromSearchPath(PHYSFS_getWriteDir());
PHYSFS_unmount(PHYSFS_getWriteDir());
}

quest_write_dir_ = quest_write_dir;
Expand All @@ -413,7 +424,7 @@ SOLARUS_API void set_quest_write_dir(const std::string& quest_write_dir) {
std::string full_write_dir = get_base_write_dir() + "/" + solarus_write_dir_;
if (!PHYSFS_setWriteDir(full_write_dir.c_str())) {
Debug::die(std::string("Cannot set Solarus write directory to '")
+ full_write_dir + "': " + PHYSFS_getLastError());
+ full_write_dir + "': " + PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
}

if (!quest_write_dir.empty()) {
Expand All @@ -426,7 +437,7 @@ SOLARUS_API void set_quest_write_dir(const std::string& quest_write_dir) {
PHYSFS_setWriteDir(full_write_dir.c_str());

// Also allow the quest to read savegames, settings and data files there.
PHYSFS_addToSearchPath(PHYSFS_getWriteDir(), 0);
PHYSFS_mount(PHYSFS_getWriteDir(),NULL, 0);
}
}

Expand All @@ -446,7 +457,11 @@ SOLARUS_API std::string get_base_write_dir() {
#if defined(SOLARUS_OSX) || defined(SOLARUS_IOS)
return get_user_application_support_directory();
#else
//Silence the deprecation warning since we must rely on getUserDir behaviour for compatibility
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
return std::string(PHYSFS_getUserDir());
#pragma GCC diagnostic pop
#endif
}

Expand Down