From f978c1bed85961e102951340669ca873328bcef3 Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Sat, 11 Feb 2017 17:13:02 +0300 Subject: Add VirtFile struct and replace all Phyfs_file usage to this struct. --- src/CMakeLists.txt | 8 +++ src/Makefile.am | 4 ++ src/integrity_unittest.cc | 2 + src/maingui.cpp | 1 + src/resources/atlas/atlasmanager.cpp | 1 + src/resources/loaders/imageloader.cpp | 1 + src/resources/loaders/musicloader.cpp | 1 + src/resources/loaders/soundloader.cpp | 1 + src/resources/resourcemanager/resourcemanager.cpp | 1 + src/utils/virtfile.cpp | 36 +++++++++++ src/utils/virtfile.h | 39 ++++++++++++ src/utils/virtfileprivate.cpp | 42 ++++++++++++ src/utils/virtfileprivate.h | 44 +++++++++++++ src/utils/virtfs.cpp | 78 +++++++++++++++++------ src/utils/virtfs.h | 29 ++++----- src/utils/virtfsrwops.cpp | 21 +++--- src/utils/virtfsrwops.h | 4 +- 17 files changed, 265 insertions(+), 48 deletions(-) create mode 100644 src/utils/virtfile.cpp create mode 100644 src/utils/virtfile.h create mode 100644 src/utils/virtfileprivate.cpp create mode 100644 src/utils/virtfileprivate.h (limited to 'src') diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 6788c4512..8aa75a4d8 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -906,6 +906,10 @@ SET(SRCS utils/stringvector.h utils/timer.cpp utils/timer.h + utils/virtfile.cpp + utils/virtfile.h + utils/virtfileprivate.cpp + utils/virtfileprivate.h utils/mutex.h utils/naclmessages.cpp utils/naclmessages.h @@ -1780,6 +1784,10 @@ SET(DYE_CMD_SRCS utils/stringutils.h utils/timer.cpp utils/timer.h + utils/virtfile.cpp + utils/virtfile.h + utils/virtfileprivate.cpp + utils/virtfileprivate.h utils/xml.cpp utils/xml.h utils/xml.inc diff --git a/src/Makefile.am b/src/Makefile.am index 66bb691f3..a9722e9b4 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -568,6 +568,10 @@ BASE_SRC += events/actionevent.h \ utils/stringvector.h \ utils/timer.cpp \ utils/timer.h \ + utils/virtfile.cpp \ + utils/virtfile.h \ + utils/virtfileprivate.cpp \ + utils/virtfileprivate.h \ utils/mutex.h \ utils/naclmessages.cpp \ utils/naclmessages.h \ diff --git a/src/integrity_unittest.cc b/src/integrity_unittest.cc index a907ca5fb..67ab1c027 100644 --- a/src/integrity_unittest.cc +++ b/src/integrity_unittest.cc @@ -39,8 +39,10 @@ #include "resources/resourcemanager/resourcemanager.h" #include "utils/env.h" +#include "utils/virtfs.h" #include "utils/virtfsrwops.h" +#include #include #include "debug.h" diff --git a/src/maingui.cpp b/src/maingui.cpp index a8a06cf9a..c94c456a5 100644 --- a/src/maingui.cpp +++ b/src/maingui.cpp @@ -31,6 +31,7 @@ #include "utils/mkdir.h" #include "utils/paths.h" #endif // ANDROID +#include "utils/virtfs.h" #include "utils/virtfsrwops.h" #include "utils/process.h" #include "utils/xml.h" diff --git a/src/resources/atlas/atlasmanager.cpp b/src/resources/atlas/atlasmanager.cpp index ecd00f3b7..6a141df27 100644 --- a/src/resources/atlas/atlasmanager.cpp +++ b/src/resources/atlas/atlasmanager.cpp @@ -29,6 +29,7 @@ #endif // DEBUG_IMAGES #include "utils/mathutils.h" +#include "utils/virtfs.h" #include "utils/virtfsrwops.h" #include "utils/sdlcheckutils.h" diff --git a/src/resources/loaders/imageloader.cpp b/src/resources/loaders/imageloader.cpp index 54f987bab..16a5022c3 100644 --- a/src/resources/loaders/imageloader.cpp +++ b/src/resources/loaders/imageloader.cpp @@ -31,6 +31,7 @@ #include "resources/dye/dye.h" #include "utils/checkutils.h" +#include "utils/virtfs.h" #include "utils/virtfsrwops.h" #include "debug.h" diff --git a/src/resources/loaders/musicloader.cpp b/src/resources/loaders/musicloader.cpp index c22843d39..8c6f483db 100644 --- a/src/resources/loaders/musicloader.cpp +++ b/src/resources/loaders/musicloader.cpp @@ -27,6 +27,7 @@ #include "resources/resourcemanager/resourcemanager.h" #include "utils/checkutils.h" +#include "utils/virtfs.h" #include "utils/virtfsrwops.h" #include "debug.h" diff --git a/src/resources/loaders/soundloader.cpp b/src/resources/loaders/soundloader.cpp index 71ceca8bd..a70d188fa 100644 --- a/src/resources/loaders/soundloader.cpp +++ b/src/resources/loaders/soundloader.cpp @@ -27,6 +27,7 @@ #include "resources/resourcemanager/resourcemanager.h" #include "utils/checkutils.h" +#include "utils/virtfs.h" #include "utils/virtfsrwops.h" #include "debug.h" diff --git a/src/resources/resourcemanager/resourcemanager.cpp b/src/resources/resourcemanager/resourcemanager.cpp index 2a2fa42e2..322b119a1 100644 --- a/src/resources/resourcemanager/resourcemanager.cpp +++ b/src/resources/resourcemanager/resourcemanager.cpp @@ -36,6 +36,7 @@ #include "utils/checkutils.h" #include "utils/delete2.h" +#include "utils/virtfs.h" #include "utils/virtfsrwops.h" #ifndef USE_OPENGL diff --git a/src/utils/virtfile.cpp b/src/utils/virtfile.cpp new file mode 100644 index 000000000..b7e6cce1f --- /dev/null +++ b/src/utils/virtfile.cpp @@ -0,0 +1,36 @@ +/* + * The ManaPlus Client + * Copyright (C) 2017 The ManaPlus Developers + * + * This file is part of The ManaPlus Client. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "utils/virtfile.h" + +#include "utils/delete2.h" +#include "utils/virtfileprivate.h" + +#include "debug.h" + +VirtFile::VirtFile() : + mPrivate(nullptr) +{ +} + +VirtFile::~VirtFile() +{ + delete2(mPrivate); +} diff --git a/src/utils/virtfile.h b/src/utils/virtfile.h new file mode 100644 index 000000000..cf7ded1a4 --- /dev/null +++ b/src/utils/virtfile.h @@ -0,0 +1,39 @@ +/* + * The ManaPlus Client + * Copyright (C) 2017 The ManaPlus Developers + * + * This file is part of The ManaPlus Client. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef UTILS_VIRTFILE_H +#define UTILS_VIRTFILE_H + +#include "localconsts.h" + +struct VirtFilePrivate; + +struct VirtFile final +{ + VirtFile(); + + A_DELETE_COPY(VirtFile) + + ~VirtFile(); + + VirtFilePrivate *mPrivate; +}; + +#endif // UTILS_VIRTFILE_H diff --git a/src/utils/virtfileprivate.cpp b/src/utils/virtfileprivate.cpp new file mode 100644 index 000000000..e1a5885f7 --- /dev/null +++ b/src/utils/virtfileprivate.cpp @@ -0,0 +1,42 @@ +/* + * The ManaPlus Client + * Copyright (C) 2017 The ManaPlus Developers + * + * This file is part of The ManaPlus Client. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "utils/virtfileprivate.h" + +#include "debug.h" + +VirtFilePrivate::VirtFilePrivate() : + mFile(nullptr) +{ +} + +VirtFilePrivate::VirtFilePrivate(PHYSFS_file *const file) : + mFile(file) +{ +} + +VirtFilePrivate::~VirtFilePrivate() +{ + if (mFile != nullptr) + { + PHYSFS_close(mFile); + mFile = nullptr; + } +} diff --git a/src/utils/virtfileprivate.h b/src/utils/virtfileprivate.h new file mode 100644 index 000000000..59670d858 --- /dev/null +++ b/src/utils/virtfileprivate.h @@ -0,0 +1,44 @@ +/* + * The ManaPlus Client + * Copyright (C) 2017 The ManaPlus Developers + * + * This file is part of The ManaPlus Client. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef UTILS_VIRTFILEPRIVATE_H +#define UTILS_VIRTFILEPRIVATE_H + +#include "localconsts.h" + +PRAGMA45(GCC diagnostic push) +PRAGMA45(GCC diagnostic ignored "-Wlong-long") +#include +PRAGMA45(GCC diagnostic pop) + +struct VirtFilePrivate final +{ + VirtFilePrivate(); + + VirtFilePrivate(PHYSFS_file *const file); + + A_DELETE_COPY(VirtFilePrivate) + + ~VirtFilePrivate(); + + PHYSFS_file *mFile; +}; + +#endif // UTILS_VIRTFILEPRIVATE_H diff --git a/src/utils/virtfs.cpp b/src/utils/virtfs.cpp index af000376a..6d3d18ba9 100644 --- a/src/utils/virtfs.cpp +++ b/src/utils/virtfs.cpp @@ -22,6 +22,9 @@ #include "logger.h" +#include "utils/virtfile.h" +#include "utils/virtfileprivate.h" + #include #include @@ -98,19 +101,34 @@ namespace VirtFs PHYSFS_freeList(listVar); } - PHYSFS_file *openRead(const char *const filename) + VirtFile *openRead(const char *const filename) { - return PHYSFS_openRead(filename); + PHYSFS_file *const handle = PHYSFS_openRead(filename); + if (!handle) + return nullptr; + VirtFile *const file = new VirtFile; + file->mPrivate = new VirtFilePrivate(handle); + return file; } - PHYSFS_file *openWrite(const char *const filename) + VirtFile *openWrite(const char *const filename) { - return PHYSFS_openWrite(filename); + PHYSFS_file *const handle = PHYSFS_openWrite(filename); + if (!handle) + return nullptr; + VirtFile *const file = new VirtFile; + file->mPrivate = new VirtFilePrivate(handle); + return file; } - PHYSFS_file *openAppend(const char *const filename) + VirtFile *openAppend(const char *const filename) { - return PHYSFS_openAppend(filename); + PHYSFS_file *const handle = PHYSFS_openAppend(filename); + if (!handle) + return nullptr; + VirtFile *const file = new VirtFile; + file->mPrivate = new VirtFilePrivate(handle); + return file; } bool setWriteDir(const char *const newDir) @@ -141,7 +159,7 @@ namespace VirtFs void *loadFile(const std::string &fileName, int &fileSize) { // Attempt to open the specified file using PhysicsFS - PHYSFS_file *const file = VirtFs::openRead(fileName.c_str()); + VirtFile *const file = VirtFs::openRead(fileName.c_str()); if (!file) { @@ -185,45 +203,63 @@ namespace VirtFs return PHYSFS_getLastError(); } - int close(PHYSFS_file *const file) + int close(VirtFile *const file) { - return PHYSFS_close(file); + if (file == nullptr) + return 0; + delete file; + return 1; } - int64_t read(PHYSFS_File *const file, + int64_t read(VirtFile *const file, void *const buffer, const uint32_t objSize, const uint32_t objCount) { - return PHYSFS_read(file, buffer, objSize, objCount); + if (file == nullptr) + return 0; + return PHYSFS_read(file->mPrivate->mFile, + buffer, + objSize, + objCount); } - int64_t write(PHYSFS_File *const file, + int64_t write(VirtFile *const file, const void *const buffer, const uint32_t objSize, const uint32_t objCount) { - return PHYSFS_write(file, buffer, objSize, objCount); + if (file == nullptr) + return 0; + return PHYSFS_write(file->mPrivate->mFile, + buffer, + objSize, + objCount); } - int64_t fileLength(PHYSFS_File *const file) + int64_t fileLength(VirtFile *const file) { - return PHYSFS_fileLength(file); + if (file == nullptr) + return -1; + return PHYSFS_fileLength(file->mPrivate->mFile); } - int64_t tell(PHYSFS_File *const file) + int64_t tell(VirtFile *const file) { - return PHYSFS_tell(file); + if (file == nullptr) + return -1; + return PHYSFS_tell(file->mPrivate->mFile); } - int seek(PHYSFS_File *const file, + int seek(VirtFile *const file, const uint64_t pos) { - return PHYSFS_seek(file, pos); + return PHYSFS_seek(file->mPrivate->mFile, + pos); } - int eof(PHYSFS_File *const file) + int eof(VirtFile *const file) { - return PHYSFS_eof(file); + return PHYSFS_eof(file->mPrivate->mFile); } } // namespace PhysFs diff --git a/src/utils/virtfs.h b/src/utils/virtfs.h index 66149c903..e2db24e44 100644 --- a/src/utils/virtfs.h +++ b/src/utils/virtfs.h @@ -23,13 +23,10 @@ #include "localconsts.h" -PRAGMA45(GCC diagnostic push) -PRAGMA45(GCC diagnostic ignored "-Wlong-long") -#include -PRAGMA45(GCC diagnostic pop) - #include +struct VirtFile; + namespace VirtFs { void init(const char *const name); @@ -41,9 +38,9 @@ namespace VirtFs char **enumerateFiles(const char *const dir); bool isDirectory(const char *const fname); void freeList(void *const listVar); - PHYSFS_file *openRead(const char *const filename); - PHYSFS_file *openWrite(const char *const filename); - PHYSFS_file *openAppend(const char *const filename); + VirtFile *openRead(const char *const filename); + VirtFile *openWrite(const char *const filename); + VirtFile *openAppend(const char *const filename); bool setWriteDir(const char *const newDir); bool addToSearchPath(const char *const newDir, const int appendToPath); bool removeFromSearchPath(const char *const oldDir); @@ -53,21 +50,21 @@ namespace VirtFs bool deinit(); void permitLinks(const bool val); const char *getLastError(); - int64_t read(PHYSFS_File *const handle, + int64_t read(VirtFile *const handle, void *const buffer, const uint32_t objSize, const uint32_t objCount); - int64_t write(PHYSFS_File *const file, + int64_t write(VirtFile *const file, const void *const buffer, const uint32_t objSize, const uint32_t objCount); - int close(PHYSFS_file *const file); - int64_t fileLength(PHYSFS_File *const file); - int64_t tell(PHYSFS_File *const file); - int seek(PHYSFS_File *const file, + int close(VirtFile *const file); + int64_t fileLength(VirtFile *const file); + int64_t tell(VirtFile *const file); + int seek(VirtFile *const file, const uint64_t pos); - int eof(PHYSFS_File *const file); -} // namespace PhysFs + int eof(VirtFile *const file); +} // namespace VirtFs extern const char *dirSeparator; diff --git a/src/utils/virtfsrwops.cpp b/src/utils/virtfsrwops.cpp index 731eb12a8..ce4f0cec9 100644 --- a/src/utils/virtfsrwops.cpp +++ b/src/utils/virtfsrwops.cpp @@ -53,6 +53,9 @@ #endif // DEBUG_VIRTFS #include "utils/fuzzer.h" +#include "utils/virtfile.h" +#include "utils/virtfileprivate.h" +#include "utils/virtfs.h" #include "debug.h" @@ -138,7 +141,7 @@ static RWOPSINT virtfsrwops_seek(SDL_RWops *const rw, { if (!rw) return -1; - PHYSFS_file *const handle = static_cast( + VirtFile *const handle = static_cast( rw->hidden.unknown.data1); RWOPSINT pos = 0; @@ -220,7 +223,7 @@ static RWOPSSIZE virtfsrwops_read(SDL_RWops *const rw, { if (!rw) return 0; - PHYSFS_file *const handle = static_cast( + VirtFile *const handle = static_cast( rw->hidden.unknown.data1); const int64_t rc = VirtFs::read(handle, ptr, CAST_U32(size), @@ -244,7 +247,7 @@ static RWOPSSIZE virtfsrwops_write(SDL_RWops *const rw, { if (!rw) return 0; - PHYSFS_file *const handle = static_cast( + VirtFile *const handle = static_cast( rw->hidden.unknown.data1); const int64_t rc = VirtFs::write(handle, ptr, CAST_U32(size), @@ -262,7 +265,7 @@ static int virtfsrwops_close(SDL_RWops *const rw) { if (!rw) return 0; - PHYSFS_file *const handle = static_cast( + VirtFile *const handle = static_cast( rw->hidden.unknown.data1); if (!VirtFs::close(handle)) { @@ -287,17 +290,17 @@ static int virtfsrwops_close(SDL_RWops *const rw) #ifdef USE_SDL2 static RWOPSINT virtfsrwops_size(SDL_RWops *const rw) { - PHYSFS_file *const handle = static_cast( + VirtFile *const handle = static_cast( rw->hidden.unknown.data1); return VirtFs::fileLength(handle); } /* virtfsrwops_size */ #endif // USE_SDL2 -static SDL_RWops *create_rwops(PHYSFS_file *const handle) +static SDL_RWops *create_rwops(VirtFile *const file) { SDL_RWops *retval = nullptr; - if (!handle) + if (!file) { logger->assertLog("virtfsrwops_seek: create rwops error: %s", VirtFs::getLastError()); @@ -315,7 +318,7 @@ static SDL_RWops *create_rwops(PHYSFS_file *const handle) retval->read = &virtfsrwops_read; retval->write = &virtfsrwops_write; retval->close = &virtfsrwops_close; - retval->hidden.unknown.data1 = handle; + retval->hidden.unknown.data1 = file; } /* if */ #ifdef DUMP_LEAKED_RESOURCES openedRWops ++; @@ -325,7 +328,7 @@ static SDL_RWops *create_rwops(PHYSFS_file *const handle) return retval; } /* create_rwops */ -SDL_RWops *VirtFs::MakeRWops(PHYSFS_file *const handle) +SDL_RWops *VirtFs::MakeRWops(VirtFile *const handle) { SDL_RWops *retval = nullptr; if (!handle) diff --git a/src/utils/virtfsrwops.h b/src/utils/virtfsrwops.h index c937fe543..5a8871377 100644 --- a/src/utils/virtfsrwops.h +++ b/src/utils/virtfsrwops.h @@ -49,7 +49,7 @@ #include -#include "utils/virtfs.h" +struct VirtFile; namespace VirtFs { @@ -64,7 +64,7 @@ namespace VirtFs SDL_RWops *RWopsOpenWrite(const char *const fname) A_WARN_UNUSED; SDL_RWops *RWopsOpenAppend(const char *const fname) A_WARN_UNUSED; - SDL_RWops *MakeRWops(PHYSFS_file *const handle) A_WARN_UNUSED; + SDL_RWops *MakeRWops(VirtFile *const handle) A_WARN_UNUSED; #ifdef DUMP_LEAKED_RESOURCES void reportRWops(); #endif // DUMP_LEAKED_RESOURCES -- cgit v1.2.3-60-g2f50