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

@ -27,13 +27,13 @@ bool ObjLoaderT6::SupportsZone(Zone* zone)
return zone->m_game == &g_GameT6;
}
void ObjLoaderT6::LoadIPakForZone(std::string ipakName, Zone* zone)
void ObjLoaderT6::LoadIPakForZone(const std::string& ipakName, Zone* zone)
{
printf("Loading ipak '%s' for zone '%s'\n", ipakName.c_str(), zone->m_name.c_str());
// TODO
}
void ObjLoaderT6::LoadReferencedContainersForZone(Zone* zone)
void ObjLoaderT6::LoadReferencedContainersForZone(ISearchPath* searchPath, Zone* zone)
{
auto* assetPoolT6 = dynamic_cast<GameAssetPoolT6*>(zone->GetPools());
const int zoneNameHash = Com_HashKey(zone->m_name.c_str(), 64);
@ -55,6 +55,7 @@ void ObjLoaderT6::LoadReferencedContainersForZone(Zone* zone)
}
}
void ObjLoaderT6::LoadObjDataForZone(Zone* zone)
void ObjLoaderT6::LoadObjDataForZone(ISearchPath* searchPath, Zone* zone)
{
// TODO
}

View File

@ -8,10 +8,10 @@ class ObjLoaderT6 final : public IObjLoader
static const int GLOBAL_HASH;
static int Com_HashKey(const char* str, int maxLen);
static void LoadIPakForZone(std::string ipakName, Zone* zone);
static void LoadIPakForZone(const std::string& ipakName, Zone* zone);
public:
bool SupportsZone(Zone* zone) override;
void LoadReferencedContainersForZone(Zone* zone) override;
void LoadObjDataForZone(Zone* zone) override;
void LoadReferencedContainersForZone(ISearchPath* searchPath, Zone* zone) override;
void LoadObjDataForZone(ISearchPath* searchPath, Zone* zone) override;
};

View File

@ -1,5 +1,6 @@
#pragma once
#include "SearchPath/ISearchPath.h"
#include "Zone/Zone.h"
class IObjLoader
@ -8,6 +9,6 @@ public:
virtual ~IObjLoader() = default;
virtual bool SupportsZone(Zone* zone) = 0;
virtual void LoadReferencedContainersForZone(Zone* zone) = 0;
virtual void LoadObjDataForZone(Zone* zone) = 0;
virtual void LoadReferencedContainersForZone(ISearchPath* searchPath, Zone* zone) = 0;
virtual void LoadObjDataForZone(ISearchPath* searchPath, Zone* zone) = 0;
};

View File

@ -0,0 +1,32 @@
#include "IWD.h"
IWD::IWD(std::string path)
{
m_path = std::move(path);
}
FileAPI::IFile* IWD::Open(const std::string& fileName)
{
// TODO
return nullptr;
}
void IWD::FindAll(std::function<void(const std::string&)> callback)
{
// TODO
}
void IWD::FindAllOnDisk(std::function<void(const std::string&)> callback)
{
// Files inside an IWD are not on the disk's file system directly. Therefore do nothing here.
}
void IWD::FindByExtension(const std::string& extension, std::function<void(const std::string&)> callback)
{
// TODO
}
void IWD::FindOnDiskByExtension(const std::string& extension, std::function<void(const std::string&)> callback)
{
// Files inside an IWD are not on the disk's file system directly. Therefore do nothing here.
}

View File

@ -0,0 +1,17 @@
#pragma once
#include "SearchPath/ISearchPath.h"
class IWD final : public ISearchPath
{
std::string m_path;
public:
explicit IWD(std::string path);
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

@ -8,25 +8,25 @@ IObjLoader* objLoaders[]
new ObjLoaderT6()
};
void ObjLoading::LoadReferencedContainersForZone(Zone* zone)
void ObjLoading::LoadReferencedContainersForZone(ISearchPath* searchPath, Zone* zone)
{
for (auto* loader : objLoaders)
{
if (loader->SupportsZone(zone))
{
loader->LoadReferencedContainersForZone(zone);
loader->LoadReferencedContainersForZone(searchPath, zone);
return;
}
}
}
void ObjLoading::LoadObjDataForZone(Zone* zone)
void ObjLoading::LoadObjDataForZone(ISearchPath* searchPath, Zone* zone)
{
for (auto* loader : objLoaders)
{
if (loader->SupportsZone(zone))
{
loader->LoadObjDataForZone(zone);
loader->LoadObjDataForZone(searchPath, zone);
return;
}
}

View File

@ -1,11 +1,13 @@
#pragma once
#include "Zone/Zone.h"
#include "SearchPath/ISearchPath.h"
class ObjLoading
{
public:
static void LoadReferencedContainersForZone(Zone* zone);
static void LoadObjDataForZone(Zone* zone);
static void LoadReferencedContainersForZone(ISearchPath* searchPath, Zone* zone);
static void UnloadContainersOfZone(Zone* zone);
static void LoadObjDataForZone(ISearchPath* searchPath, Zone* zone);
};

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();