mirror of
https://github.com/Laupetin/OpenAssetTools.git
synced 2025-04-20 00:02:55 +00:00
Include filesize when opening a file with searchpaths
This commit is contained in:
parent
62247cecda
commit
83080db991
@ -241,13 +241,13 @@ class Linker::Impl
|
|||||||
{
|
{
|
||||||
const auto definitionFileName = source + ".zone";
|
const auto definitionFileName = source + ".zone";
|
||||||
const auto definitionStream = sourceSearchPath->Open(definitionFileName);
|
const auto definitionStream = sourceSearchPath->Open(definitionFileName);
|
||||||
if (!definitionStream)
|
if (!definitionStream.IsOpen())
|
||||||
{
|
{
|
||||||
std::cout << "Could not find zone definition file for zone \"" << source << "\"." << std::endl;
|
std::cout << "Could not find zone definition file for zone \"" << source << "\"." << std::endl;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
ZoneDefinitionInputStream zoneDefinitionInputStream(*definitionStream, definitionFileName, m_args.m_verbose);
|
ZoneDefinitionInputStream zoneDefinitionInputStream(*definitionStream.m_stream, definitionFileName, m_args.m_verbose);
|
||||||
includeDefinition = zoneDefinitionInputStream.ReadDefinition();
|
includeDefinition = zoneDefinitionInputStream.ReadDefinition();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -275,13 +275,13 @@ class Linker::Impl
|
|||||||
{
|
{
|
||||||
const auto definitionFileName = zoneName + ".zone";
|
const auto definitionFileName = zoneName + ".zone";
|
||||||
const auto definitionStream = sourceSearchPath->Open(definitionFileName);
|
const auto definitionStream = sourceSearchPath->Open(definitionFileName);
|
||||||
if (!definitionStream)
|
if (!definitionStream.IsOpen())
|
||||||
{
|
{
|
||||||
std::cout << "Could not find zone definition file for zone \"" << zoneName << "\"." << std::endl;
|
std::cout << "Could not find zone definition file for zone \"" << zoneName << "\"." << std::endl;
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
ZoneDefinitionInputStream zoneDefinitionInputStream(*definitionStream, definitionFileName, m_args.m_verbose);
|
ZoneDefinitionInputStream zoneDefinitionInputStream(*definitionStream.m_stream, definitionFileName, m_args.m_verbose);
|
||||||
zoneDefinition = zoneDefinitionInputStream.ReadDefinition();
|
zoneDefinition = zoneDefinitionInputStream.ReadDefinition();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -303,9 +303,9 @@ class Linker::Impl
|
|||||||
const auto assetListFileName = "assetlist/" + zoneName + ".csv";
|
const auto assetListFileName = "assetlist/" + zoneName + ".csv";
|
||||||
const auto assetListStream = sourceSearchPath->Open(assetListFileName);
|
const auto assetListStream = sourceSearchPath->Open(assetListFileName);
|
||||||
|
|
||||||
if (assetListStream)
|
if (assetListStream.IsOpen())
|
||||||
{
|
{
|
||||||
const AssetListInputStream stream(*assetListStream);
|
const AssetListInputStream stream(*assetListStream.m_stream);
|
||||||
AssetListEntry entry;
|
AssetListEntry entry;
|
||||||
|
|
||||||
while (stream.NextEntry(entry))
|
while (stream.NextEntry(entry))
|
||||||
@ -405,13 +405,13 @@ class Linker::Impl
|
|||||||
for (auto i = rangeBegin; i != rangeEnd; ++i)
|
for (auto i = rangeBegin; i != rangeEnd; ++i)
|
||||||
{
|
{
|
||||||
const auto gdtFile = gdtSearchPath->Open(i->second + ".gdt");
|
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;
|
std::cout << "Failed to open file for gdt \"" << i->second << "\"" << std::endl;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
GdtReader gdtReader(*gdtFile);
|
GdtReader gdtReader(*gdtFile.m_stream);
|
||||||
auto gdt = std::make_unique<Gdt>();
|
auto gdt = std::make_unique<Gdt>();
|
||||||
if(!gdtReader.Read(*gdt))
|
if(!gdtReader.Read(*gdt))
|
||||||
{
|
{
|
||||||
|
@ -47,9 +47,9 @@ namespace IW4
|
|||||||
|
|
||||||
{
|
{
|
||||||
const auto filePathImage = searchPath->Open(imageFileName);
|
const auto filePathImage = searchPath->Open(imageFileName);
|
||||||
if (filePathImage != nullptr)
|
if (filePathImage.IsOpen())
|
||||||
{
|
{
|
||||||
loadedTexture = loader.LoadIwi(*filePathImage);
|
loadedTexture = loader.LoadIwi(*filePathImage.m_stream);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -36,9 +36,9 @@ namespace T6
|
|||||||
const auto ipakFilename = ipakName + ".ipak";
|
const auto ipakFilename = ipakName + ".ipak";
|
||||||
|
|
||||||
auto file = searchPath->Open(ipakFilename);
|
auto file = searchPath->Open(ipakFilename);
|
||||||
if (file)
|
if (file.IsOpen())
|
||||||
{
|
{
|
||||||
auto ipak = std::make_unique<IPak>(ipakFilename, std::move(file));
|
auto ipak = std::make_unique<IPak>(ipakFilename, std::move(file.m_stream));
|
||||||
|
|
||||||
if (ipak->Initialize())
|
if (ipak->Initialize())
|
||||||
{
|
{
|
||||||
@ -168,9 +168,9 @@ namespace T6
|
|||||||
|
|
||||||
{
|
{
|
||||||
const auto filePathImage = searchPath->Open(imageFileName);
|
const auto filePathImage = searchPath->Open(imageFileName);
|
||||||
if (filePathImage)
|
if (filePathImage.IsOpen())
|
||||||
{
|
{
|
||||||
loadedTexture = loader.LoadIwi(*filePathImage);
|
loadedTexture = loader.LoadIwi(*filePathImage.m_stream);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -240,11 +240,11 @@ public:
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::unique_ptr<std::istream> Open(const std::string& fileName) override
|
SearchPathOpenFile Open(const std::string& fileName) override
|
||||||
{
|
{
|
||||||
if (m_unz_file == nullptr)
|
if (m_unz_file == nullptr)
|
||||||
{
|
{
|
||||||
return nullptr;
|
return SearchPathOpenFile();
|
||||||
}
|
}
|
||||||
|
|
||||||
auto iwdFilename = fileName;
|
auto iwdFilename = fileName;
|
||||||
@ -266,13 +266,13 @@ public:
|
|||||||
{
|
{
|
||||||
auto result = std::make_unique<IWDFile>(this, m_unz_file, iwdEntry->second.m_size);
|
auto result = std::make_unique<IWDFile>(this, m_unz_file, iwdEntry->second.m_size);
|
||||||
m_last_file = result.get();
|
m_last_file = result.get();
|
||||||
return std::make_unique<iobjstream>(std::move(result));
|
return SearchPathOpenFile(std::make_unique<iobjstream>(std::move(result)), iwdEntry->second.m_size);
|
||||||
}
|
}
|
||||||
|
|
||||||
return nullptr;
|
return SearchPathOpenFile();
|
||||||
}
|
}
|
||||||
|
|
||||||
return nullptr;
|
return SearchPathOpenFile();
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string GetPath() override
|
std::string GetPath() override
|
||||||
@ -342,7 +342,7 @@ bool IWD::Initialize()
|
|||||||
return m_impl->Initialize();
|
return m_impl->Initialize();
|
||||||
}
|
}
|
||||||
|
|
||||||
std::unique_ptr<std::istream> IWD::Open(const std::string& fileName)
|
SearchPathOpenFile IWD::Open(const std::string& fileName)
|
||||||
{
|
{
|
||||||
return m_impl->Open(fileName);
|
return m_impl->Open(fileName);
|
||||||
}
|
}
|
||||||
|
@ -16,7 +16,7 @@ public:
|
|||||||
static ObjContainerRepository<IWD, ISearchPath> Repository;
|
static ObjContainerRepository<IWD, ISearchPath> Repository;
|
||||||
|
|
||||||
IWD(std::string path, std::unique_ptr<std::istream> stream);
|
IWD(std::string path, std::unique_ptr<std::istream> stream);
|
||||||
~IWD();
|
~IWD() override;
|
||||||
|
|
||||||
IWD(const IWD& other) = delete;
|
IWD(const IWD& other) = delete;
|
||||||
IWD(IWD&& other) noexcept;
|
IWD(IWD&& other) noexcept;
|
||||||
@ -29,7 +29,7 @@ public:
|
|||||||
*/
|
*/
|
||||||
bool Initialize();
|
bool Initialize();
|
||||||
|
|
||||||
std::unique_ptr<std::istream> Open(const std::string& fileName) override;
|
SearchPathOpenFile Open(const std::string& fileName) override;
|
||||||
std::string GetPath() override;
|
std::string GetPath() override;
|
||||||
std::string GetName() override;
|
std::string GetName() override;
|
||||||
void Find(const SearchPathSearchOptions& options, const std::function<void(const std::string&)>& callback) override;
|
void Find(const SearchPathSearchOptions& options, const std::function<void(const std::string&)>& callback) override;
|
||||||
|
17
src/ObjLoading/SearchPath/ISearchPath.cpp
Normal file
17
src/ObjLoading/SearchPath/ISearchPath.cpp
Normal file
@ -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<std::istream> stream, const int64_t length)
|
||||||
|
: m_stream(std::move(stream)),
|
||||||
|
m_length(length)
|
||||||
|
{
|
||||||
|
}
|
@ -3,21 +3,40 @@
|
|||||||
#include <functional>
|
#include <functional>
|
||||||
#include <istream>
|
#include <istream>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <cstdint>
|
||||||
|
|
||||||
#include "Utils/ObjStream.h"
|
#include "Utils/ClassUtils.h"
|
||||||
#include "SearchPathSearchOptions.h"
|
#include "SearchPathSearchOptions.h"
|
||||||
|
|
||||||
|
class SearchPathOpenFile
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
std::unique_ptr<std::istream> m_stream;
|
||||||
|
int64_t m_length;
|
||||||
|
|
||||||
|
_NODISCARD bool IsOpen() const;
|
||||||
|
|
||||||
|
SearchPathOpenFile();
|
||||||
|
SearchPathOpenFile(std::unique_ptr<std::istream> stream, int64_t length);
|
||||||
|
};
|
||||||
|
|
||||||
class ISearchPath
|
class ISearchPath
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
ISearchPath() = default;
|
||||||
virtual ~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.
|
* \brief Opens a file relative to the search path.
|
||||||
* \param fileName The relative path to the file to open.
|
* \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.
|
* \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<std::istream> Open(const std::string& fileName) = 0;
|
virtual SearchPathOpenFile Open(const std::string& fileName) = 0;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \brief Returns the path to the search path.
|
* \brief Returns the path to the search path.
|
||||||
|
@ -17,16 +17,17 @@ std::string SearchPathFilesystem::GetPath()
|
|||||||
return m_path;
|
return m_path;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::unique_ptr<std::istream> SearchPathFilesystem::Open(const std::string& fileName)
|
SearchPathOpenFile SearchPathFilesystem::Open(const std::string& fileName)
|
||||||
{
|
{
|
||||||
auto file = std::make_unique<std::ifstream>(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::ifstream>(std::move(file)), static_cast<int64_t>(file_size(filePath)));
|
||||||
}
|
}
|
||||||
|
|
||||||
return nullptr;
|
return SearchPathOpenFile();
|
||||||
}
|
}
|
||||||
|
|
||||||
void SearchPathFilesystem::Find(const SearchPathSearchOptions& options, const std::function<void(const std::string&)>& callback)
|
void SearchPathFilesystem::Find(const SearchPathSearchOptions& options, const std::function<void(const std::string&)>& callback)
|
||||||
|
@ -11,7 +11,7 @@ class SearchPathFilesystem final : public ISearchPath
|
|||||||
public:
|
public:
|
||||||
explicit SearchPathFilesystem(std::string path);
|
explicit SearchPathFilesystem(std::string path);
|
||||||
|
|
||||||
std::unique_ptr<std::istream> Open(const std::string& fileName) override;
|
SearchPathOpenFile Open(const std::string& fileName) override;
|
||||||
std::string GetPath() override;
|
std::string GetPath() override;
|
||||||
void Find(const SearchPathSearchOptions& options, const std::function<void(const std::string&)>& callback) override;
|
void Find(const SearchPathSearchOptions& options, const std::function<void(const std::string&)>& callback) override;
|
||||||
};
|
};
|
@ -2,19 +2,19 @@
|
|||||||
|
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
|
|
||||||
std::unique_ptr<std::istream> SearchPaths::Open(const std::string& fileName)
|
SearchPathOpenFile SearchPaths::Open(const std::string& fileName)
|
||||||
{
|
{
|
||||||
for(auto* searchPathEntry : m_search_paths)
|
for(auto* searchPathEntry : m_search_paths)
|
||||||
{
|
{
|
||||||
auto file = searchPathEntry->Open(fileName);
|
auto file = searchPathEntry->Open(fileName);
|
||||||
|
|
||||||
if(file)
|
if(file.IsOpen())
|
||||||
{
|
{
|
||||||
return std::move(file);
|
return file;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nullptr;
|
return SearchPathOpenFile();
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string SearchPaths::GetPath()
|
std::string SearchPaths::GetPath()
|
||||||
|
@ -15,7 +15,7 @@ public:
|
|||||||
SearchPaths() = default;
|
SearchPaths() = default;
|
||||||
~SearchPaths() override = default;
|
~SearchPaths() override = default;
|
||||||
|
|
||||||
std::unique_ptr<std::istream> Open(const std::string& fileName) override;
|
SearchPathOpenFile Open(const std::string& fileName) override;
|
||||||
std::string GetPath() override;
|
std::string GetPath() override;
|
||||||
void Find(const SearchPathSearchOptions& options, const std::function<void(const std::string&)>& callback) override;
|
void Find(const SearchPathSearchOptions& options, const std::function<void(const std::string&)>& callback) override;
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user