Unlinker/ObjLoading: Add skeleton for dynamically loading search paths based on current zone

This commit is contained in:
Jan
2019-12-30 23:52:33 +01:00
parent 5f833969f9
commit 153f8f2e89
15 changed files with 727 additions and 178 deletions

View File

@ -2,11 +2,43 @@
#include "Utils/FileAPI.h"
#include <string>
#include <functional>
class ISearchPath
{
public:
virtual ~ISearchPath() = 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 FileAPI::IFile* Open(const std::string& fileName) = 0;
/**
* \brief Iterates through all files of the search path.
* \param callback The callback to call for each found file with it's path relative to the search path.
*/
virtual void FindAll(std::function<void(const std::string&)> callback) = 0;
/**
* \brief Iterates through all files available through the OS file system.
* \param callback The callback to call for each found file with it's full path.
*/
virtual void FindAllOnDisk(std::function<void(const std::string&)> callback) = 0;
/**
* \brief Iterates through all files of the search path with the specified extension.
* \param extension The extension of all files to find.
* \param callback The callback to call for each found file with it's path relative to the search path.
*/
virtual void FindByExtension(const std::string& extension, std::function<void(const std::string&)> callback) = 0;
/**
* \brief Iterates through all files available through the OS file system with the specified extension.
* \param extension The extension of all files to find.
* \param callback The callback to call for each found file with it's full path.
*/
virtual void FindOnDiskByExtension(const std::string& extension, std::function<void(const std::string&)> callback) = 0;
};

View File

@ -0,0 +1,79 @@
#include "SearchPathFilesystem.h"
#include "Utils/PathUtils.h"
#include <filesystem>
SearchPathFilesystem::SearchPathFilesystem(std::string path)
{
m_path = std::move(path);
}
const std::string& SearchPathFilesystem::GetPath() const
{
return m_path;
}
FileAPI::IFile* SearchPathFilesystem::Open(const std::string& fileName)
{
FileAPI::File file = FileAPI::Open(utils::Path::Combine(m_path, fileName), FileAPI::Mode::MODE_READ);
if (file.IsOpen())
{
return new FileAPI::File(std::move(file));
}
return nullptr;
}
void SearchPathFilesystem::FindAll(const std::function<void(const std::string&)> callback)
{
std::filesystem::recursive_directory_iterator iterator(m_path);
for (const auto entry = begin(iterator); iterator != end(iterator); ++iterator)
{
callback(entry->path().string());
}
}
void SearchPathFilesystem::FindAllOnDisk(const std::function<void(const std::string&)> callback)
{
std::filesystem::recursive_directory_iterator iterator(m_path);
for (const auto entry = begin(iterator); iterator != end(iterator); ++iterator)
{
callback(absolute(entry->path()).string());
}
}
void SearchPathFilesystem::FindByExtension(const std::string& extension,
const std::function<void(const std::string&)> callback)
{
std::filesystem::recursive_directory_iterator iterator(m_path);
for (const auto entry = begin(iterator); iterator != end(iterator); ++iterator)
{
auto entryPath = entry->path();
if (entryPath.extension().string() == extension)
{
callback(entryPath.string());
}
}
}
void SearchPathFilesystem::FindOnDiskByExtension(const std::string& extension,
const std::function<void(const std::string&)> callback)
{
std::filesystem::recursive_directory_iterator iterator(m_path);
for (const auto entry = begin(iterator); iterator != end(iterator); ++iterator)
{
auto entryPath = entry->path();
if (entryPath.extension().string() == extension)
{
callback(absolute(entryPath).string());
}
}
}

View File

@ -0,0 +1,19 @@
#pragma once
#include "ISearchPath.h"
#include <string>
class SearchPathFilesystem final : public ISearchPath
{
std::string m_path;
public:
explicit SearchPathFilesystem(std::string path);
const std::string& GetPath() const;
FileAPI::IFile* Open(const std::string& fileName) override;
void FindAll(std::function<void(const std::string&)> callback) override;
void FindAllOnDisk(std::function<void(const std::string&)> callback) override;
void FindByExtension(const std::string& extension, std::function<void(const std::string&)> callback) override;
void FindOnDiskByExtension(const std::string& extension, std::function<void(const std::string&)> callback) override;
};

View File

@ -1,11 +1,17 @@
#include "SearchPaths.h"
#include <filesystem>
SearchPaths::SearchPaths() = default;
SearchPaths::~SearchPaths()
{
for(auto searchPath : m_search_paths)
for(auto searchPathToFree : m_to_free)
{
delete searchPath;
delete searchPathToFree;
}
m_to_free.clear();
m_search_paths.clear();
}
@ -37,9 +43,9 @@ SearchPaths& SearchPaths::operator=(SearchPaths&& other) noexcept
FileAPI::IFile* SearchPaths::Open(const std::string& fileName)
{
for(auto searchPath : m_search_paths)
for(auto searchPathEntry : m_search_paths)
{
auto* file = searchPath->Open(fileName);
auto* file = searchPathEntry->Open(fileName);
if(file != nullptr)
{
@ -50,7 +56,46 @@ FileAPI::IFile* SearchPaths::Open(const std::string& fileName)
return nullptr;
}
void SearchPaths::AddSearchPath(ISearchPath* searchPath)
void SearchPaths::FindAll(const std::function<void(const std::string&)> callback)
{
for (auto searchPathEntry : m_search_paths)
{
searchPathEntry->FindAll(callback);
}
}
void SearchPaths::FindAllOnDisk(const std::function<void(const std::string&)> callback)
{
for (auto searchPathEntry : m_search_paths)
{
searchPathEntry->FindAllOnDisk(callback);
}
}
void SearchPaths::FindByExtension(const std::string& extension, const std::function<void(const std::string&)> callback)
{
for (auto searchPathEntry : m_search_paths)
{
searchPathEntry->FindByExtension(extension, callback);
}
}
void SearchPaths::FindOnDiskByExtension(const std::string& extension, const std::function<void(const std::string&)> callback)
{
for (auto searchPathEntry : m_search_paths)
{
searchPathEntry->FindOnDiskByExtension(extension, callback);
}
}
void SearchPaths::CommitSearchPath(ISearchPath* searchPath)
{
m_search_paths.push_back(searchPath);
m_to_free.push_back(searchPath);
}
void SearchPaths::IncludeSearchPath(ISearchPath* searchPath)
{
m_search_paths.push_back(searchPath);
}

View File

@ -6,19 +6,41 @@
class SearchPaths final : public ISearchPath
{
std::vector<ISearchPath*> m_search_paths;
std::vector<ISearchPath*> m_to_free;
public:
using iterator = std::vector<ISearchPath*>::iterator;
SearchPaths();
~SearchPaths() override;
FileAPI::IFile* Open(const std::string& fileName) override;
void FindAll(std::function<void(const std::string&)> callback) override;
void FindAllOnDisk(std::function<void(const std::string&)> callback) override;
void FindByExtension(const std::string& extension, std::function<void(const std::string&)> callback) override;
void FindOnDiskByExtension(const std::string& extension, std::function<void(const std::string&)> callback) override;
SearchPaths(const SearchPaths& other);
SearchPaths(SearchPaths&& other) noexcept;
SearchPaths& operator=(const SearchPaths& other);
SearchPaths& operator=(SearchPaths&& other) noexcept;
void AddSearchPath(ISearchPath* searchPath);
/**
* \brief Adds a search path that gets deleted upon destruction of the \c SearchPaths object.
* \param searchPath The search path to add.
*/
void CommitSearchPath(ISearchPath* searchPath);
/**
* \brief Adds a search path that does \b NOT get deleted upon destruction of the \c SearchPaths object.
* \param searchPath The search path to add.
*/
void IncludeSearchPath(ISearchPath* searchPath);
/**
* \brief Removes a search path from the \c SearchPaths object. If the search path was committed then it will \b NOT be deleted when destructing the \c SearchPaths object.
* \param searchPath The search path to remove.
*/
void RemoveSearchPath(ISearchPath* searchPath);
iterator begin();