Merge pull request #1046 from diath/fix_deprecated_physfs_funcs

Fix use of deprecated PhysFS functions
This commit is contained in:
Konrad Kuśnierz 2019-10-10 08:14:08 +02:00 committed by GitHub
commit dd0303feee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 28 deletions

View File

@ -68,7 +68,7 @@ void FileStream::cache()
PHYSFS_seek(m_fileHandle, 0); PHYSFS_seek(m_fileHandle, 0);
int size = PHYSFS_fileLength(m_fileHandle); int size = PHYSFS_fileLength(m_fileHandle);
m_data.resize(size); m_data.resize(size);
if(PHYSFS_read(m_fileHandle, m_data.data(), size, 1) == -1) if(PHYSFS_readBytes(m_fileHandle, m_data.data(), size) == -1)
throwError("unable to read file data", true); throwError("unable to read file data", true);
PHYSFS_close(m_fileHandle); PHYSFS_close(m_fileHandle);
m_fileHandle = nullptr; m_fileHandle = nullptr;
@ -97,7 +97,7 @@ void FileStream::flush()
if(!PHYSFS_seek(m_fileHandle, 0)) if(!PHYSFS_seek(m_fileHandle, 0))
throwError("flush seek failed", true); throwError("flush seek failed", true);
uint len = m_data.size(); uint len = m_data.size();
if(PHYSFS_write(m_fileHandle, m_data.data(), 1, len) != len) if(PHYSFS_writeBytes(m_fileHandle, m_data.data(), len) != len)
throwError("flush write failed", true); throwError("flush write failed", true);
} }
@ -109,7 +109,7 @@ void FileStream::flush()
int FileStream::read(void *buffer, uint32 size, uint32 nmemb) int FileStream::read(void *buffer, uint32 size, uint32 nmemb)
{ {
if(!m_caching) { if(!m_caching) {
int res = PHYSFS_read(m_fileHandle, buffer, size, nmemb); int res = PHYSFS_readBytes(m_fileHandle, buffer, size * nmemb);
if(res == -1) if(res == -1)
throwError("read failed", true); throwError("read failed", true);
return res; return res;
@ -130,7 +130,7 @@ int FileStream::read(void *buffer, uint32 size, uint32 nmemb)
void FileStream::write(const void *buffer, uint32 count) void FileStream::write(const void *buffer, uint32 count)
{ {
if(!m_caching) { if(!m_caching) {
if(PHYSFS_write(m_fileHandle, buffer, 1, count) != count) if(PHYSFS_writeBytes(m_fileHandle, buffer, count) != count)
throwError("write failed", true); throwError("write failed", true);
} else { } else {
m_data.grow(m_pos + count); m_data.grow(m_pos + count);
@ -184,7 +184,7 @@ uint8 FileStream::getU8()
{ {
uint8 v = 0; uint8 v = 0;
if(!m_caching) { if(!m_caching) {
if(PHYSFS_read(m_fileHandle, &v, 1, 1) != 1) if(PHYSFS_readBytes(m_fileHandle, &v, 1) != 1)
throwError("read failed", true); throwError("read failed", true);
} else { } else {
if(m_pos+1 > m_data.size()) if(m_pos+1 > m_data.size())
@ -247,7 +247,7 @@ int8 FileStream::get8()
{ {
int8 v = 0; int8 v = 0;
if(!m_caching) { if(!m_caching) {
if(PHYSFS_read(m_fileHandle, &v, 1, 1) != 1) if(PHYSFS_readBytes(m_fileHandle, &v, 1) != 1)
throwError("read failed", true); throwError("read failed", true);
} else { } else {
if(m_pos+1 > m_data.size()) if(m_pos+1 > m_data.size())
@ -313,7 +313,7 @@ std::string FileStream::getString()
if(len > 0 && len < 8192) { if(len > 0 && len < 8192) {
char buffer[8192]; char buffer[8192];
if(m_fileHandle) { if(m_fileHandle) {
if(PHYSFS_read(m_fileHandle, buffer, 1, len) == 0) if(PHYSFS_readBytes(m_fileHandle, buffer, len) == 0)
throwError("read failed", true); throwError("read failed", true);
else else
str = std::string(buffer, len); str = std::string(buffer, len);
@ -354,7 +354,7 @@ void FileStream::endNode()
void FileStream::addU8(uint8 v) void FileStream::addU8(uint8 v)
{ {
if(!m_caching) { if(!m_caching) {
if(PHYSFS_write(m_fileHandle, &v, 1, 1) != 1) if(PHYSFS_writeBytes(m_fileHandle, &v, 1) != 1)
throwError("write failed", true); throwError("write failed", true);
} else { } else {
m_data.add(v); m_data.add(v);
@ -401,7 +401,7 @@ void FileStream::addU64(uint64 v)
void FileStream::add8(int8 v) void FileStream::add8(int8 v)
{ {
if(!m_caching) { if(!m_caching) {
if(PHYSFS_write(m_fileHandle, &v, 1, 1) != 1) if(PHYSFS_writeBytes(m_fileHandle, &v, 1) != 1)
throwError("write failed", true); throwError("write failed", true);
} else { } else {
m_data.add(v); m_data.add(v);
@ -455,7 +455,7 @@ void FileStream::throwError(const std::string& message, bool physfsError)
{ {
std::string completeMessage = stdext::format("in file '%s': %s", m_name, message); std::string completeMessage = stdext::format("in file '%s': %s", m_name, message);
if(physfsError) if(physfsError)
completeMessage += std::string(": ") + PHYSFS_getLastError(); completeMessage += std::string(": ") + PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode());
stdext::throw_exception(completeMessage); stdext::throw_exception(completeMessage);
} }

View File

@ -29,6 +29,21 @@
#include <physfs.h> #include <physfs.h>
namespace {
bool isDirectory(const std::string &path)
{
PHYSFS_Stat stat = {};
int result = PHYSFS_stat(path.c_str(), &stat);
if (!result) {
return false;
}
return stat.filetype == PHYSFS_FILETYPE_DIRECTORY;
}
} // namespace <anonymous>
ResourceManager g_resources; ResourceManager g_resources;
void ResourceManager::init(const char *argv0) void ResourceManager::init(const char *argv0)
@ -52,7 +67,7 @@ bool ResourceManager::discoverWorkDir(const std::string& existentFile)
bool found = false; bool found = false;
for(const std::string& dir : possiblePaths) { for(const std::string& dir : possiblePaths) {
if(!PHYSFS_addToSearchPath(dir.c_str(), 0)) if(!PHYSFS_mount(dir.c_str(), nullptr, 0))
continue; continue;
if(PHYSFS_exists(existentFile.c_str())) { if(PHYSFS_exists(existentFile.c_str())) {
@ -61,7 +76,7 @@ bool ResourceManager::discoverWorkDir(const std::string& existentFile)
found = true; found = true;
break; break;
} }
PHYSFS_removeFromSearchPath(dir.c_str()); PHYSFS_unmount(dir.c_str());
} }
return found; return found;
@ -80,7 +95,7 @@ bool ResourceManager::setupUserWriteDir(const std::string& appWriteDirName)
if(!PHYSFS_setWriteDir(writeDir.c_str())) { if(!PHYSFS_setWriteDir(writeDir.c_str())) {
if(!PHYSFS_setWriteDir(userDir.c_str()) || !PHYSFS_mkdir(dirName.c_str())) { if(!PHYSFS_setWriteDir(userDir.c_str()) || !PHYSFS_mkdir(dirName.c_str())) {
g_logger.error(stdext::format("Unable to create write directory '%s': %s", writeDir, PHYSFS_getLastError())); g_logger.error(stdext::format("Unable to create write directory '%s': %s", writeDir, PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode())));
return false; return false;
} }
} }
@ -90,7 +105,7 @@ bool ResourceManager::setupUserWriteDir(const std::string& appWriteDirName)
bool ResourceManager::setWriteDir(const std::string& writeDir, bool create) bool ResourceManager::setWriteDir(const std::string& writeDir, bool create)
{ {
if(!PHYSFS_setWriteDir(writeDir.c_str())) { if(!PHYSFS_setWriteDir(writeDir.c_str())) {
g_logger.error(stdext::format("Unable to set write directory '%s': %s", writeDir, PHYSFS_getLastError())); g_logger.error(stdext::format("Unable to set write directory '%s': %s", writeDir, PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode())));
return false; return false;
} }
@ -108,11 +123,11 @@ bool ResourceManager::setWriteDir(const std::string& writeDir, bool create)
bool ResourceManager::addSearchPath(const std::string& path, bool pushFront) bool ResourceManager::addSearchPath(const std::string& path, bool pushFront)
{ {
std::string savePath = path; std::string savePath = path;
if(!PHYSFS_addToSearchPath(path.c_str(), pushFront ? 0 : 1)) { if(!PHYSFS_mount(path.c_str(), nullptr, pushFront ? 0 : 1)) {
bool found = false; bool found = false;
for(std::string searchPath : m_searchPaths) { for(std::string searchPath : m_searchPaths) {
std::string newPath = searchPath + path; std::string newPath = searchPath + path;
if(PHYSFS_addToSearchPath(newPath.c_str(), pushFront ? 0 : 1)) { if(PHYSFS_mount(newPath.c_str(), nullptr, pushFront ? 0 : 1)) {
savePath = newPath; savePath = newPath;
found = true; found = true;
break; break;
@ -120,7 +135,7 @@ bool ResourceManager::addSearchPath(const std::string& path, bool pushFront)
} }
if(!found) { if(!found) {
//g_logger.error(stdext::format("Could not add '%s' to directory search path. Reason %s", path, PHYSFS_getLastError())); //g_logger.error(stdext::format("Could not add '%s' to directory search path. Reason %s", path, PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode())));
return false; return false;
} }
} }
@ -133,7 +148,7 @@ bool ResourceManager::addSearchPath(const std::string& path, bool pushFront)
bool ResourceManager::removeSearchPath(const std::string& path) bool ResourceManager::removeSearchPath(const std::string& path)
{ {
if(!PHYSFS_removeFromSearchPath(path.c_str())) if(!PHYSFS_unmount(path.c_str()))
return false; return false;
auto it = std::find(m_searchPaths.begin(), m_searchPaths.end(), path); auto it = std::find(m_searchPaths.begin(), m_searchPaths.end(), path);
assert(it != m_searchPaths.end()); assert(it != m_searchPaths.end());
@ -150,18 +165,18 @@ void ResourceManager::searchAndAddPackages(const std::string& packagesDir, const
continue; continue;
std::string package = getRealDir(packagesDir) + "/" + file; std::string package = getRealDir(packagesDir) + "/" + file;
if(!addSearchPath(package, true)) if(!addSearchPath(package, true))
g_logger.error(stdext::format("Unable to read package '%s': %s", package, PHYSFS_getLastError())); g_logger.error(stdext::format("Unable to read package '%s': %s", package, PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode())));
} }
} }
bool ResourceManager::fileExists(const std::string& fileName) bool ResourceManager::fileExists(const std::string& fileName)
{ {
return (PHYSFS_exists(resolvePath(fileName).c_str()) && !PHYSFS_isDirectory(resolvePath(fileName).c_str())); return (PHYSFS_exists(resolvePath(fileName).c_str()) && !isDirectory(resolvePath(fileName)));
} }
bool ResourceManager::directoryExists(const std::string& directoryName) bool ResourceManager::directoryExists(const std::string& directoryName)
{ {
return (PHYSFS_isDirectory(resolvePath(directoryName).c_str())); return isDirectory(resolvePath(directoryName));
} }
void ResourceManager::readFileStream(const std::string& fileName, std::iostream& out) void ResourceManager::readFileStream(const std::string& fileName, std::iostream& out)
@ -182,11 +197,11 @@ std::string ResourceManager::readFileContents(const std::string& fileName)
PHYSFS_File* file = PHYSFS_openRead(fullPath.c_str()); PHYSFS_File* file = PHYSFS_openRead(fullPath.c_str());
if(!file) if(!file)
stdext::throw_exception(stdext::format("unable to open file '%s': %s", fullPath, PHYSFS_getLastError())); stdext::throw_exception(stdext::format("unable to open file '%s': %s", fullPath, PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode())));
int fileSize = PHYSFS_fileLength(file); int fileSize = PHYSFS_fileLength(file);
std::string buffer(fileSize, 0); std::string buffer(fileSize, 0);
PHYSFS_read(file, (void*)&buffer[0], 1, fileSize); PHYSFS_readBytes(file, (void*)&buffer[0], fileSize);
PHYSFS_close(file); PHYSFS_close(file);
return buffer; return buffer;
@ -196,11 +211,11 @@ bool ResourceManager::writeFileBuffer(const std::string& fileName, const uchar*
{ {
PHYSFS_file* file = PHYSFS_openWrite(fileName.c_str()); PHYSFS_file* file = PHYSFS_openWrite(fileName.c_str());
if(!file) { if(!file) {
g_logger.error(PHYSFS_getLastError()); g_logger.error(PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
return false; return false;
} }
PHYSFS_write(file, (void*)data, size, 1); PHYSFS_writeBytes(file, (void*)data, size);
PHYSFS_close(file); PHYSFS_close(file);
return true; return true;
} }
@ -229,7 +244,7 @@ FileStreamPtr ResourceManager::openFile(const std::string& fileName)
PHYSFS_File* file = PHYSFS_openRead(fullPath.c_str()); PHYSFS_File* file = PHYSFS_openRead(fullPath.c_str());
if(!file) if(!file)
stdext::throw_exception(stdext::format("unable to open file '%s': %s", fullPath, PHYSFS_getLastError())); stdext::throw_exception(stdext::format("unable to open file '%s': %s", fullPath, PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode())));
return FileStreamPtr(new FileStream(fullPath, file, false)); return FileStreamPtr(new FileStream(fullPath, file, false));
} }
@ -237,7 +252,7 @@ FileStreamPtr ResourceManager::appendFile(const std::string& fileName)
{ {
PHYSFS_File* file = PHYSFS_openAppend(fileName.c_str()); PHYSFS_File* file = PHYSFS_openAppend(fileName.c_str());
if(!file) if(!file)
stdext::throw_exception(stdext::format("failed to append file '%s': %s", fileName, PHYSFS_getLastError())); stdext::throw_exception(stdext::format("failed to append file '%s': %s", fileName, PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode())));
return FileStreamPtr(new FileStream(fileName, file, true)); return FileStreamPtr(new FileStream(fileName, file, true));
} }
@ -245,7 +260,7 @@ FileStreamPtr ResourceManager::createFile(const std::string& fileName)
{ {
PHYSFS_File* file = PHYSFS_openWrite(fileName.c_str()); PHYSFS_File* file = PHYSFS_openWrite(fileName.c_str());
if(!file) if(!file)
stdext::throw_exception(stdext::format("failed to create file '%s': %s", fileName, PHYSFS_getLastError())); stdext::throw_exception(stdext::format("failed to create file '%s': %s", fileName, PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode())));
return FileStreamPtr(new FileStream(fileName, file, true)); return FileStreamPtr(new FileStream(fileName, file, true));
} }