Add AssetLoader basis

This commit is contained in:
Jan
2021-03-13 14:27:20 +01:00
parent 83080db991
commit a7860c7c76
34 changed files with 736 additions and 149 deletions

View File

@ -0,0 +1,45 @@
#include "AssetLoadingContext.h"
AssetLoadingContext::AssetLoadingContext(Zone* zone, ISearchPath* rawSearchPath, std::vector<Gdt*> gdtFiles)
: m_zone(zone),
m_raw_search_path(rawSearchPath),
m_gdt_files(std::move(gdtFiles))
{
BuildGdtEntryCache();
}
void AssetLoadingContext::BuildGdtEntryCache()
{
for(auto* gdt : m_gdt_files)
{
for(const auto& entry : gdt->m_entries)
{
auto gdfMapEntry = m_entries_by_gdf_and_by_name.find(entry->m_gdf_name);
if(gdfMapEntry == m_entries_by_gdf_and_by_name.end())
{
std::unordered_map<std::string, GdtEntry*> entryMap;
entryMap.emplace(std::make_pair(entry->m_name, entry.get()));
m_entries_by_gdf_and_by_name.emplace(std::make_pair(entry->m_gdf_name, std::move(entryMap)));
}
else
{
gdfMapEntry->second.emplace(std::make_pair(entry->m_name, entry.get()));
}
}
}
}
GdtEntry* AssetLoadingContext::GetGdtEntryByGdfAndName(const std::string& gdfName, const std::string& entryName)
{
const auto foundGdtMap = m_entries_by_gdf_and_by_name.find(gdfName);
if (foundGdtMap == m_entries_by_gdf_and_by_name.end())
return nullptr;
const auto foundGdtEntry = foundGdtMap->second.find(entryName);
if (foundGdtEntry == foundGdtMap->second.end())
return nullptr;
return foundGdtEntry->second;
}

View File

@ -0,0 +1,23 @@
#pragma once
#include <unordered_map>
#include "IGdtQueryable.h"
#include "Obj/Gdt/Gdt.h"
#include "SearchPath/ISearchPath.h"
#include "Zone/Zone.h"
class AssetLoadingContext final : public IGdtQueryable
{
std::unordered_map<std::string, std::unordered_map<std::string, GdtEntry*>> m_entries_by_gdf_and_by_name;
void BuildGdtEntryCache();
public:
Zone* const m_zone;
ISearchPath* const m_raw_search_path;
const std::vector<Gdt*> m_gdt_files;
AssetLoadingContext(Zone* zone, ISearchPath* rawSearchPath, std::vector<Gdt*> gdtFiles);
GdtEntry* GetGdtEntryByGdfAndName(const std::string& gdfName, const std::string& entryName) override;
};

View File

@ -0,0 +1,69 @@
#include "AssetLoadingManager.h"
#include <iostream>
AssetLoadingManager::AssetLoadingManager(const std::unordered_map<asset_type_t, std::unique_ptr<IAssetLoader>>& assetLoadersByType, AssetLoadingContext& context):
m_asset_loaders_by_type(assetLoadersByType),
m_context(context),
m_last_dependency_loaded(nullptr)
{
}
bool AssetLoadingManager::LoadAssetFromLoader(const asset_type_t assetType, const std::string& assetName)
{
return LoadDependency(assetType, assetName) != nullptr;
}
void AssetLoadingManager::AddAsset(const asset_type_t assetType, const std::string& assetName, void* asset, std::vector<XAssetInfoGeneric*>& dependencies)
{
m_last_dependency_loaded = m_context.m_zone->m_pools->AddAsset(assetType, assetName, asset, dependencies);
if (m_last_dependency_loaded == nullptr)
std::cout << "Failed to add asset of type \"" << m_context.m_zone->m_pools->GetAssetTypeName(assetType) << "\" to pool: \"" << assetName << "\"" << std::endl;
}
XAssetInfoGeneric* AssetLoadingManager::LoadDependency(const asset_type_t assetType, const std::string& assetName)
{
const auto loader = m_asset_loaders_by_type.find(assetType);
if (loader != m_asset_loaders_by_type.end())
{
if (loader->second->CanLoadFromGdt() && loader->second->LoadFromGdt(assetName, &m_context, m_context.m_zone->GetMemory(), this))
{
auto* lastDependency = m_last_dependency_loaded;
m_last_dependency_loaded = nullptr;
return lastDependency;
}
if (loader->second->CanLoadFromRaw() && loader->second->LoadFromRaw(assetName, m_context.m_raw_search_path, m_context.m_zone->GetMemory(), this))
{
auto* lastDependency = m_last_dependency_loaded;
m_last_dependency_loaded = nullptr;
return lastDependency;
}
auto* existingAsset = loader->second->LoadFromGlobalAssetPools(assetName);
if(existingAsset)
{
std::vector<XAssetInfoGeneric*> dependencies;
for (const auto* dependency : existingAsset->m_dependencies)
{
auto* newDependency = LoadDependency(dependency->m_type, dependency->m_name);
if (newDependency)
dependencies.push_back(newDependency);
else
return nullptr;
}
AddAsset(existingAsset->m_type, existingAsset->m_name, existingAsset->m_ptr, dependencies);
auto* lastDependency = m_last_dependency_loaded;
m_last_dependency_loaded = nullptr;
return lastDependency;
}
std::cout << "Failed to load asset of type \"" << m_context.m_zone->m_pools->GetAssetTypeName(assetType) << "\": \"" << assetName << "\"" << std::endl;
}
else
{
std::cout << "Failed to find loader for asset type \"" << m_context.m_zone->m_pools->GetAssetTypeName(assetType) << "\"" << std::endl;
}
return nullptr;
}

View File

@ -0,0 +1,20 @@
#pragma once
#include <unordered_map>
#include "AssetLoadingContext.h"
#include "IAssetLoader.h"
#include "IAssetLoadingManager.h"
class AssetLoadingManager final : public IAssetLoadingManager
{
const std::unordered_map<asset_type_t, std::unique_ptr<IAssetLoader>>& m_asset_loaders_by_type;
AssetLoadingContext& m_context;
XAssetInfoGeneric* m_last_dependency_loaded;
public:
AssetLoadingManager(const std::unordered_map<asset_type_t, std::unique_ptr<IAssetLoader>>& assetLoadersByType, AssetLoadingContext& context);
bool LoadAssetFromLoader(asset_type_t assetType, const std::string& assetName);
void AddAsset(asset_type_t assetType, const std::string& assetName, void* asset, std::vector<XAssetInfoGeneric*>& dependencies) override;
XAssetInfoGeneric* LoadDependency(asset_type_t assetType, const std::string& assetName) override;
};

View File

@ -0,0 +1,18 @@
#pragma once
#include "IAssetLoader.h"
#include "Pool/GlobalAssetPool.h"
template<asset_type_t AssetType, typename T>
class BasicAssetLoader : public IAssetLoader
{
public:
_NODISCARD asset_type_t GetHandlingAssetType() const override
{
return AssetType;
}
_NODISCARD XAssetInfoGeneric* LoadFromGlobalAssetPools(const std::string& assetName) const override
{
return GlobalAssetPool<T>::GetAssetByName(assetName);
}
};

View File

@ -0,0 +1,42 @@
#pragma once
#include <string>
#include "Utils/ClassUtils.h"
#include "SearchPath/ISearchPath.h"
#include "IAssetLoadingManager.h"
#include "IGdtQueryable.h"
#include "Zone/ZoneTypes.h"
class IAssetLoader
{
public:
IAssetLoader() = default;
virtual ~IAssetLoader() = default;
IAssetLoader(const IAssetLoader& other) = default;
IAssetLoader(IAssetLoader&& other) noexcept = default;
IAssetLoader& operator=(const IAssetLoader& other) = default;
IAssetLoader& operator=(IAssetLoader&& other) noexcept = default;
_NODISCARD virtual asset_type_t GetHandlingAssetType() const = 0;
_NODISCARD virtual XAssetInfoGeneric* LoadFromGlobalAssetPools(const std::string& assetName) const = 0;
_NODISCARD virtual bool CanLoadFromGdt() const
{
return false;
}
_NODISCARD virtual bool CanLoadFromRaw() const
{
return false;
}
virtual bool LoadFromGdt(const std::string& assetName, IGdtQueryable* gdtQueryable, MemoryManager* memory, IAssetLoadingManager* manager) const
{
return false;
}
virtual bool LoadFromRaw(const std::string& assetName, ISearchPath* searchPath, MemoryManager* memory, IAssetLoadingManager* manager) const
{
return false;
}
};

View File

@ -0,0 +1,24 @@
#pragma once
#include <string>
#include "Pool/XAssetInfo.h"
#include "Zone/ZoneTypes.h"
class IAssetLoadingManager
{
public:
IAssetLoadingManager() = default;
virtual ~IAssetLoadingManager() = default;
IAssetLoadingManager(const IAssetLoadingManager& other) = default;
IAssetLoadingManager(IAssetLoadingManager&& other) noexcept = default;
IAssetLoadingManager& operator=(const IAssetLoadingManager& other) = default;
IAssetLoadingManager& operator=(IAssetLoadingManager&& other) noexcept = default;
virtual void AddAsset(asset_type_t assetType, const std::string& assetName, void* asset, std::vector<XAssetInfoGeneric*>& dependencies) = 0;
void AddAsset(const asset_type_t assetType, const std::string& assetName, void* asset)
{
std::vector<XAssetInfoGeneric*> dependencies;
AddAsset(assetType, assetName, asset, dependencies);
}
virtual XAssetInfoGeneric* LoadDependency(asset_type_t assetType, const std::string& assetName) = 0;
};

View File

@ -0,0 +1,17 @@
#pragma once
#include <string>
#include "Obj/Gdt/GdtEntry.h"
class IGdtQueryable
{
public:
IGdtQueryable() = default;
virtual ~IGdtQueryable() = default;
IGdtQueryable(const IGdtQueryable& other) = default;
IGdtQueryable(IGdtQueryable&& other) noexcept = default;
IGdtQueryable& operator=(const IGdtQueryable& other) = default;
IGdtQueryable& operator=(IGdtQueryable&& other) noexcept = default;
virtual GdtEntry* GetGdtEntryByGdfAndName(const std::string& gdfName, const std::string& entryName) = 0;
};