diff --git a/src/Linker/Linker.cpp b/src/Linker/Linker.cpp index 99cfdb75..de445d10 100644 --- a/src/Linker/Linker.cpp +++ b/src/Linker/Linker.cpp @@ -241,13 +241,13 @@ class Linker::Impl { const auto definitionFileName = source + ".zone"; const auto definitionStream = sourceSearchPath->Open(definitionFileName); - if (!definitionStream) + if (!definitionStream.IsOpen()) { std::cout << "Could not find zone definition file for zone \"" << source << "\"." << std::endl; return false; } - ZoneDefinitionInputStream zoneDefinitionInputStream(*definitionStream, definitionFileName, m_args.m_verbose); + ZoneDefinitionInputStream zoneDefinitionInputStream(*definitionStream.m_stream, definitionFileName, m_args.m_verbose); includeDefinition = zoneDefinitionInputStream.ReadDefinition(); } @@ -275,13 +275,13 @@ class Linker::Impl { const auto definitionFileName = zoneName + ".zone"; const auto definitionStream = sourceSearchPath->Open(definitionFileName); - if (!definitionStream) + if (!definitionStream.IsOpen()) { std::cout << "Could not find zone definition file for zone \"" << zoneName << "\"." << std::endl; return nullptr; } - ZoneDefinitionInputStream zoneDefinitionInputStream(*definitionStream, definitionFileName, m_args.m_verbose); + ZoneDefinitionInputStream zoneDefinitionInputStream(*definitionStream.m_stream, definitionFileName, m_args.m_verbose); zoneDefinition = zoneDefinitionInputStream.ReadDefinition(); } @@ -303,9 +303,9 @@ class Linker::Impl const auto assetListFileName = "assetlist/" + zoneName + ".csv"; const auto assetListStream = sourceSearchPath->Open(assetListFileName); - if (assetListStream) + if (assetListStream.IsOpen()) { - const AssetListInputStream stream(*assetListStream); + const AssetListInputStream stream(*assetListStream.m_stream); AssetListEntry entry; while (stream.NextEntry(entry)) @@ -405,13 +405,13 @@ class Linker::Impl for (auto i = rangeBegin; i != rangeEnd; ++i) { const auto gdtFile = gdtSearchPath->Open(i->second + ".gdt"); - if(!gdtFile) + if(!gdtFile.IsOpen()) { std::cout << "Failed to open file for gdt \"" << i->second << "\"" << std::endl; return false; } - GdtReader gdtReader(*gdtFile); + GdtReader gdtReader(*gdtFile.m_stream); auto gdt = std::make_unique(); if(!gdtReader.Read(*gdt)) { diff --git a/src/ObjLoading/Game/IW4/ObjLoaderIW4.cpp b/src/ObjLoading/Game/IW4/ObjLoaderIW4.cpp index 652a2416..31aadf48 100644 --- a/src/ObjLoading/Game/IW4/ObjLoaderIW4.cpp +++ b/src/ObjLoading/Game/IW4/ObjLoaderIW4.cpp @@ -47,9 +47,9 @@ namespace IW4 { const auto filePathImage = searchPath->Open(imageFileName); - if (filePathImage != nullptr) + if (filePathImage.IsOpen()) { - loadedTexture = loader.LoadIwi(*filePathImage); + loadedTexture = loader.LoadIwi(*filePathImage.m_stream); } } diff --git a/src/ObjLoading/Game/T6/ObjLoaderT6.cpp b/src/ObjLoading/Game/T6/ObjLoaderT6.cpp index 5311bb2a..51fcbc0e 100644 --- a/src/ObjLoading/Game/T6/ObjLoaderT6.cpp +++ b/src/ObjLoading/Game/T6/ObjLoaderT6.cpp @@ -36,9 +36,9 @@ namespace T6 const auto ipakFilename = ipakName + ".ipak"; auto file = searchPath->Open(ipakFilename); - if (file) + if (file.IsOpen()) { - auto ipak = std::make_unique(ipakFilename, std::move(file)); + auto ipak = std::make_unique(ipakFilename, std::move(file.m_stream)); if (ipak->Initialize()) { @@ -168,9 +168,9 @@ namespace T6 { const auto filePathImage = searchPath->Open(imageFileName); - if (filePathImage) + if (filePathImage.IsOpen()) { - loadedTexture = loader.LoadIwi(*filePathImage); + loadedTexture = loader.LoadIwi(*filePathImage.m_stream); } } } diff --git a/src/ObjLoading/ObjContainer/IWD/IWD.cpp b/src/ObjLoading/ObjContainer/IWD/IWD.cpp index 9bb31330..2ec1c3ba 100644 --- a/src/ObjLoading/ObjContainer/IWD/IWD.cpp +++ b/src/ObjLoading/ObjContainer/IWD/IWD.cpp @@ -240,11 +240,11 @@ public: return true; } - std::unique_ptr Open(const std::string& fileName) override + SearchPathOpenFile Open(const std::string& fileName) override { if (m_unz_file == nullptr) { - return nullptr; + return SearchPathOpenFile(); } auto iwdFilename = fileName; @@ -266,13 +266,13 @@ public: { auto result = std::make_unique(this, m_unz_file, iwdEntry->second.m_size); m_last_file = result.get(); - return std::make_unique(std::move(result)); + return SearchPathOpenFile(std::make_unique(std::move(result)), iwdEntry->second.m_size); } - return nullptr; + return SearchPathOpenFile(); } - return nullptr; + return SearchPathOpenFile(); } std::string GetPath() override @@ -342,7 +342,7 @@ bool IWD::Initialize() return m_impl->Initialize(); } -std::unique_ptr IWD::Open(const std::string& fileName) +SearchPathOpenFile IWD::Open(const std::string& fileName) { return m_impl->Open(fileName); } diff --git a/src/ObjLoading/ObjContainer/IWD/IWD.h b/src/ObjLoading/ObjContainer/IWD/IWD.h index a3b386d3..26a29680 100644 --- a/src/ObjLoading/ObjContainer/IWD/IWD.h +++ b/src/ObjLoading/ObjContainer/IWD/IWD.h @@ -16,7 +16,7 @@ public: static ObjContainerRepository Repository; IWD(std::string path, std::unique_ptr stream); - ~IWD(); + ~IWD() override; IWD(const IWD& other) = delete; IWD(IWD&& other) noexcept; @@ -29,7 +29,7 @@ public: */ bool Initialize(); - std::unique_ptr Open(const std::string& fileName) override; + SearchPathOpenFile Open(const std::string& fileName) override; std::string GetPath() override; std::string GetName() override; void Find(const SearchPathSearchOptions& options, const std::function& callback) override; diff --git a/src/ObjLoading/SearchPath/ISearchPath.cpp b/src/ObjLoading/SearchPath/ISearchPath.cpp new file mode 100644 index 00000000..425201d4 --- /dev/null +++ b/src/ObjLoading/SearchPath/ISearchPath.cpp @@ -0,0 +1,17 @@ +#include "ISearchPath.h" + +bool SearchPathOpenFile::IsOpen() const +{ + return m_stream != nullptr; +} + +SearchPathOpenFile::SearchPathOpenFile() + : m_length(0) +{ +} + +SearchPathOpenFile::SearchPathOpenFile(std::unique_ptr stream, const int64_t length) + : m_stream(std::move(stream)), + m_length(length) +{ +} diff --git a/src/ObjLoading/SearchPath/ISearchPath.h b/src/ObjLoading/SearchPath/ISearchPath.h index d5e31000..bc07cfc6 100644 --- a/src/ObjLoading/SearchPath/ISearchPath.h +++ b/src/ObjLoading/SearchPath/ISearchPath.h @@ -3,21 +3,40 @@ #include #include #include +#include -#include "Utils/ObjStream.h" +#include "Utils/ClassUtils.h" #include "SearchPathSearchOptions.h" +class SearchPathOpenFile +{ +public: + std::unique_ptr m_stream; + int64_t m_length; + + _NODISCARD bool IsOpen() const; + + SearchPathOpenFile(); + SearchPathOpenFile(std::unique_ptr stream, int64_t length); +}; + class ISearchPath { public: + ISearchPath() = default; virtual ~ISearchPath() = default; + ISearchPath(const ISearchPath& other) = default; + ISearchPath(ISearchPath&& other) noexcept = default; + ISearchPath& operator=(const ISearchPath& other) = default; + ISearchPath& operator=(ISearchPath&& other) noexcept = default; + /** * \brief Opens a file relative to the search path. * \param fileName The relative path to the file to open. * \return A pointer to an \c IFile object to read the found file or \c nullptr when no file could be found. */ - virtual std::unique_ptr Open(const std::string& fileName) = 0; + virtual SearchPathOpenFile Open(const std::string& fileName) = 0; /** * \brief Returns the path to the search path. diff --git a/src/ObjLoading/SearchPath/SearchPathFilesystem.cpp b/src/ObjLoading/SearchPath/SearchPathFilesystem.cpp index f0f92a89..3f587337 100644 --- a/src/ObjLoading/SearchPath/SearchPathFilesystem.cpp +++ b/src/ObjLoading/SearchPath/SearchPathFilesystem.cpp @@ -17,16 +17,17 @@ std::string SearchPathFilesystem::GetPath() return m_path; } -std::unique_ptr SearchPathFilesystem::Open(const std::string& fileName) +SearchPathOpenFile SearchPathFilesystem::Open(const std::string& fileName) { - auto file = std::make_unique(fs::path(m_path).append(fileName).string(), std::fstream::in | std::fstream::binary); + const auto filePath = fs::path(m_path).append(fileName); + std::ifstream file(filePath.string(), std::fstream::in | std::fstream::binary); - if (file->is_open()) + if (file.is_open()) { - return std::move(file); + return SearchPathOpenFile(std::make_unique(std::move(file)), static_cast(file_size(filePath))); } - return nullptr; + return SearchPathOpenFile(); } void SearchPathFilesystem::Find(const SearchPathSearchOptions& options, const std::function& callback) diff --git a/src/ObjLoading/SearchPath/SearchPathFilesystem.h b/src/ObjLoading/SearchPath/SearchPathFilesystem.h index 6a5f1e3c..d9997c85 100644 --- a/src/ObjLoading/SearchPath/SearchPathFilesystem.h +++ b/src/ObjLoading/SearchPath/SearchPathFilesystem.h @@ -11,7 +11,7 @@ class SearchPathFilesystem final : public ISearchPath public: explicit SearchPathFilesystem(std::string path); - std::unique_ptr Open(const std::string& fileName) override; + SearchPathOpenFile Open(const std::string& fileName) override; std::string GetPath() override; void Find(const SearchPathSearchOptions& options, const std::function& callback) override; }; \ No newline at end of file diff --git a/src/ObjLoading/SearchPath/SearchPaths.cpp b/src/ObjLoading/SearchPath/SearchPaths.cpp index 9c6a02b4..2065dea5 100644 --- a/src/ObjLoading/SearchPath/SearchPaths.cpp +++ b/src/ObjLoading/SearchPath/SearchPaths.cpp @@ -2,19 +2,19 @@ #include -std::unique_ptr SearchPaths::Open(const std::string& fileName) +SearchPathOpenFile SearchPaths::Open(const std::string& fileName) { for(auto* searchPathEntry : m_search_paths) { auto file = searchPathEntry->Open(fileName); - if(file) + if(file.IsOpen()) { - return std::move(file); + return file; } } - return nullptr; + return SearchPathOpenFile(); } std::string SearchPaths::GetPath() diff --git a/src/ObjLoading/SearchPath/SearchPaths.h b/src/ObjLoading/SearchPath/SearchPaths.h index 71d3c213..a503a526 100644 --- a/src/ObjLoading/SearchPath/SearchPaths.h +++ b/src/ObjLoading/SearchPath/SearchPaths.h @@ -15,7 +15,7 @@ public: SearchPaths() = default; ~SearchPaths() override = default; - std::unique_ptr Open(const std::string& fileName) override; + SearchPathOpenFile Open(const std::string& fileName) override; std::string GetPath() override; void Find(const SearchPathSearchOptions& options, const std::function& callback) override;