mirror of
https://github.com/Laupetin/OpenAssetTools.git
synced 2025-06-10 23:08:05 -05:00
Add AssetLoader basis
This commit is contained in:
45
src/ObjLoading/AssetLoading/AssetLoadingContext.cpp
Normal file
45
src/ObjLoading/AssetLoading/AssetLoadingContext.cpp
Normal 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;
|
||||
}
|
23
src/ObjLoading/AssetLoading/AssetLoadingContext.h
Normal file
23
src/ObjLoading/AssetLoading/AssetLoadingContext.h
Normal 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;
|
||||
};
|
69
src/ObjLoading/AssetLoading/AssetLoadingManager.cpp
Normal file
69
src/ObjLoading/AssetLoading/AssetLoadingManager.cpp
Normal 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;
|
||||
}
|
20
src/ObjLoading/AssetLoading/AssetLoadingManager.h
Normal file
20
src/ObjLoading/AssetLoading/AssetLoadingManager.h
Normal 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;
|
||||
};
|
18
src/ObjLoading/AssetLoading/BasicAssetLoader.h
Normal file
18
src/ObjLoading/AssetLoading/BasicAssetLoader.h
Normal 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);
|
||||
}
|
||||
};
|
42
src/ObjLoading/AssetLoading/IAssetLoader.h
Normal file
42
src/ObjLoading/AssetLoading/IAssetLoader.h
Normal 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;
|
||||
}
|
||||
};
|
24
src/ObjLoading/AssetLoading/IAssetLoadingManager.h
Normal file
24
src/ObjLoading/AssetLoading/IAssetLoadingManager.h
Normal 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;
|
||||
};
|
17
src/ObjLoading/AssetLoading/IGdtQueryable.h
Normal file
17
src/ObjLoading/AssetLoading/IGdtQueryable.h
Normal 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;
|
||||
};
|
Reference in New Issue
Block a user