mirror of
https://github.com/Laupetin/OpenAssetTools.git
synced 2025-06-10 23:08:05 -05:00
ObjLoading: Be able to load and index IWD files
This commit is contained in:
@ -1,7 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "SearchPathSearchOptions.h"
|
||||
#include "Utils/FileAPI.h"
|
||||
#include <string>
|
||||
#include <functional>
|
||||
|
||||
class ISearchPath
|
||||
@ -16,29 +16,25 @@ public:
|
||||
*/
|
||||
virtual FileAPI::IFile* Open(const std::string& fileName) = 0;
|
||||
|
||||
/**
|
||||
* \brief Returns the path to the search path.
|
||||
* \return The path to the search path.
|
||||
*/
|
||||
virtual std::string GetPath() = 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.
|
||||
* \param options Options that modify the search.
|
||||
*/
|
||||
virtual void Find(const SearchPathSearchOptions& options, const std::function<void(const std::string&)>& callback) = 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;
|
||||
void Find(const std::function<void(const std::string&)>& callback)
|
||||
{
|
||||
Find(SearchPathSearchOptions(), callback);
|
||||
}
|
||||
};
|
||||
|
@ -8,7 +8,7 @@ SearchPathFilesystem::SearchPathFilesystem(std::string path)
|
||||
m_path = std::move(path);
|
||||
}
|
||||
|
||||
const std::string& SearchPathFilesystem::GetPath() const
|
||||
std::string SearchPathFilesystem::GetPath()
|
||||
{
|
||||
return m_path;
|
||||
}
|
||||
@ -25,55 +25,28 @@ FileAPI::IFile* SearchPathFilesystem::Open(const std::string& fileName)
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
|
||||
void SearchPathFilesystem::FindAll(const std::function<void(const std::string&)> callback)
|
||||
void SearchPathFilesystem::Find(const SearchPathSearchOptions& options, 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)
|
||||
if (options.m_should_include_subdirectories)
|
||||
{
|
||||
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)
|
||||
std::filesystem::recursive_directory_iterator iterator(m_path);
|
||||
for (const auto entry = begin(iterator); iterator != end(iterator); ++iterator)
|
||||
{
|
||||
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());
|
||||
auto path = entry->path();
|
||||
if (options.m_filter_extensions && path.extension().string() != options.m_extension)
|
||||
continue;
|
||||
callback(options.m_absolute_paths ? absolute(path).string() : path.string());
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
std::filesystem::directory_iterator iterator(m_path);
|
||||
for (const auto entry = begin(iterator); iterator != end(iterator); ++iterator)
|
||||
{
|
||||
auto path = entry->path();
|
||||
if (options.m_filter_extensions && path.extension().string() != options.m_extension)
|
||||
continue;
|
||||
callback(options.m_absolute_paths ? absolute(path).string() : path.string());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -9,11 +9,8 @@ class SearchPathFilesystem final : public ISearchPath
|
||||
|
||||
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;
|
||||
std::string GetPath() override;
|
||||
void Find(const SearchPathSearchOptions& options, const std::function<void(const std::string&)>& callback) override;
|
||||
};
|
41
src/ObjLoading/SearchPath/SearchPathSearchOptions.cpp
Normal file
41
src/ObjLoading/SearchPath/SearchPathSearchOptions.cpp
Normal file
@ -0,0 +1,41 @@
|
||||
#include "SearchPathSearchOptions.h"
|
||||
|
||||
SearchPathSearchOptions::SearchPathSearchOptions()
|
||||
{
|
||||
m_should_include_subdirectories = true;
|
||||
m_disk_files_only = false;
|
||||
m_absolute_paths = false;
|
||||
m_filter_extensions = false;
|
||||
}
|
||||
|
||||
SearchPathSearchOptions& SearchPathSearchOptions::IncludeSubdirectories(const bool value)
|
||||
{
|
||||
m_should_include_subdirectories = value;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
SearchPathSearchOptions& SearchPathSearchOptions::OnlyDiskFiles(const bool value)
|
||||
{
|
||||
m_disk_files_only = value;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
SearchPathSearchOptions& SearchPathSearchOptions::AbsolutePaths(const bool value)
|
||||
{
|
||||
m_absolute_paths = value;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
SearchPathSearchOptions& SearchPathSearchOptions::FilterExtensions(std::string extension)
|
||||
{
|
||||
m_extension = std::move(extension);
|
||||
m_filter_extensions = true;
|
||||
|
||||
if(m_extension[0] != '.')
|
||||
m_extension = "." + m_extension;
|
||||
|
||||
return *this;
|
||||
}
|
21
src/ObjLoading/SearchPath/SearchPathSearchOptions.h
Normal file
21
src/ObjLoading/SearchPath/SearchPathSearchOptions.h
Normal file
@ -0,0 +1,21 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
class SearchPathSearchOptions
|
||||
{
|
||||
public:
|
||||
bool m_should_include_subdirectories;
|
||||
bool m_disk_files_only;
|
||||
bool m_absolute_paths;
|
||||
|
||||
bool m_filter_extensions;
|
||||
std::string m_extension;
|
||||
|
||||
SearchPathSearchOptions();
|
||||
|
||||
SearchPathSearchOptions& IncludeSubdirectories(bool value);
|
||||
SearchPathSearchOptions& OnlyDiskFiles(bool value);
|
||||
SearchPathSearchOptions& AbsolutePaths(bool value);
|
||||
SearchPathSearchOptions& FilterExtensions(std::string extension);
|
||||
};
|
@ -56,36 +56,16 @@ FileAPI::IFile* SearchPaths::Open(const std::string& fileName)
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
|
||||
void SearchPaths::FindAll(const std::function<void(const std::string&)> callback)
|
||||
std::string SearchPaths::GetPath()
|
||||
{
|
||||
for (auto searchPathEntry : m_search_paths)
|
||||
{
|
||||
searchPathEntry->FindAll(callback);
|
||||
}
|
||||
return "SearchPaths: " + std::to_string(m_search_paths.size()) + " entries";
|
||||
}
|
||||
|
||||
void SearchPaths::FindAllOnDisk(const std::function<void(const std::string&)> callback)
|
||||
void SearchPaths::Find(const SearchPathSearchOptions& options, 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);
|
||||
searchPathEntry->Find(options, callback);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -15,10 +15,8 @@ public:
|
||||
~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;
|
||||
std::string GetPath() override;
|
||||
void Find(const SearchPathSearchOptions& options, const std::function<void(const std::string&)>& callback) override;
|
||||
|
||||
SearchPaths(const SearchPaths& other);
|
||||
SearchPaths(SearchPaths&& other) noexcept;
|
||||
|
Reference in New Issue
Block a user