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

@ -5,6 +5,27 @@
using namespace IW4;
ZoneCreator::ZoneCreator()
{
for (auto assetType = 0; assetType < ASSET_TYPE_COUNT; assetType++)
{
AddAssetTypeName(assetType, GameAssetPoolIW4::AssetTypeNameByType(assetType));
}
}
void ZoneCreator::AddAssetTypeName(asset_type_t assetType, std::string name)
{
m_asset_types_by_name.emplace(std::make_pair(std::move(name), assetType));
}
void ZoneCreator::CreateZoneAssetPools(Zone* zone) const
{
zone->m_pools = std::make_unique<GameAssetPoolIW4>(zone, zone->m_priority);
for (auto assetType = 0; assetType < ASSET_TYPE_COUNT; assetType++)
zone->m_pools->InitPoolDynamic(assetType);
}
bool ZoneCreator::SupportsGame(const std::string& gameName) const
{
return gameName == g_GameIW4.GetShortName();

View File

@ -1,11 +1,22 @@
#pragma once
#include <unordered_map>
#include <string>
#include "Zone/ZoneTypes.h"
#include "ZoneCreation/IZoneCreator.h"
namespace IW4
{
class ZoneCreator final : public IZoneCreator
{
std::unordered_map<std::string, asset_type_t> m_asset_types_by_name;
void AddAssetTypeName(asset_type_t assetType, std::string name);
void CreateZoneAssetPools(Zone* zone) const;
public:
ZoneCreator();
_NODISCARD bool SupportsGame(const std::string& gameName) const override;
_NODISCARD std::unique_ptr<Zone> CreateZoneForDefinition(ZoneCreationContext& context) const override;
};

View File

@ -1,49 +1,33 @@
#include "ZoneCreatorT6.h"
#include <iostream>
#include "ObjLoading.h"
#include "Game/T6/T6.h"
#include "Game/T6/GameT6.h"
#include "Game/T6/GameAssetPoolT6.h"
using namespace T6;
namespace T6
ZoneCreator::ZoneCreator()
{
class SpecifiedAsset
for(auto assetType = 0; assetType < ASSET_TYPE_COUNT; assetType++)
{
public:
XAssetType m_type;
std::string m_name;
bool m_reference;
};
AddAssetTypeName(assetType, GameAssetPoolT6::AssetTypeNameByType(assetType));
}
}
class AssetLoaderImpl
{
ZoneCreationContext& m_context;
void ZoneCreator::AddAssetTypeName(asset_type_t assetType, std::string name)
{
m_asset_types_by_name.emplace(std::make_pair(std::move(name), assetType));
}
void CreateZoneAssetPools(Zone* zone) const
{
zone->m_pools = std::make_unique<GameAssetPoolT6>(zone, zone->m_priority);
void ZoneCreator::CreateZoneAssetPools(Zone* zone) const
{
zone->m_pools = std::make_unique<GameAssetPoolT6>(zone, zone->m_priority);
for (auto assetType = 0; assetType < ASSET_TYPE_COUNT; assetType++)
zone->m_pools->InitPoolDynamic(assetType);
}
public:
explicit AssetLoaderImpl(ZoneCreationContext& context)
: m_context(context)
{
}
std::unique_ptr<Zone> CreateZoneForDefinition()
{
auto zone = std::make_unique<Zone>(m_context.m_zone_name, 0, &g_GameT6);
CreateZoneAssetPools(zone.get());
std::vector<SpecifiedAsset> specifiedAssets;
return zone;
}
};
for (auto assetType = 0; assetType < ASSET_TYPE_COUNT; assetType++)
zone->m_pools->InitPoolDynamic(assetType);
}
bool ZoneCreator::SupportsGame(const std::string& gameName) const
@ -53,6 +37,30 @@ bool ZoneCreator::SupportsGame(const std::string& gameName) const
std::unique_ptr<Zone> ZoneCreator::CreateZoneForDefinition(ZoneCreationContext& context) const
{
AssetLoaderImpl impl(context);
return impl.CreateZoneForDefinition();
auto zone = std::make_unique<Zone>(context.m_zone_name, 0, &g_GameT6);
CreateZoneAssetPools(zone.get());
std::vector<Gdt*> gdtList;
gdtList.reserve(context.m_gdt_files.size());
for (const auto& gdt : context.m_gdt_files)
gdtList.push_back(gdt.get());
const auto assetLoadingContext = std::make_unique<AssetLoadingContext>(zone.get(), context.m_asset_search_path, std::move(gdtList));
for(const auto& assetEntry : context.m_definition->m_assets)
{
if (assetEntry.m_is_reference)
continue;
const auto foundAssetTypeEntry = m_asset_types_by_name.find(assetEntry.m_asset_type);
if(foundAssetTypeEntry == m_asset_types_by_name.end())
{
std::cout << "Unknown asset type \"" << assetEntry.m_asset_type << "\"" << std::endl;
return nullptr;
}
if (!ObjLoading::LoadAssetForZone(assetLoadingContext.get(), foundAssetTypeEntry->second, assetEntry.m_asset_name))
return nullptr;
}
return zone;
}

View File

@ -1,11 +1,22 @@
#pragma once
#include <unordered_map>
#include <string>
#include "Zone/ZoneTypes.h"
#include "ZoneCreation/IZoneCreator.h"
namespace T6
{
class ZoneCreator final : public IZoneCreator
{
std::unordered_map<std::string, asset_type_t> m_asset_types_by_name;
void AddAssetTypeName(asset_type_t assetType, std::string name);
void CreateZoneAssetPools(Zone* zone) const;
public:
ZoneCreator();
_NODISCARD bool SupportsGame(const std::string& gameName) const override;
_NODISCARD std::unique_ptr<Zone> CreateZoneForDefinition(ZoneCreationContext& context) const override;
};

View File

@ -427,12 +427,12 @@ class Linker::Impl
std::unique_ptr<Zone> CreateZoneForDefinition(const std::string& zoneName, ZoneDefinition& zoneDefinition, ISearchPath* assetSearchPath, ISearchPath* gdtSearchPath) const
{
auto context = std::make_unique<ZoneCreationContext>(zoneName, assetSearchPath);
auto context = std::make_unique<ZoneCreationContext>(zoneName, assetSearchPath, &zoneDefinition);
if (!GetGameNameFromZoneDefinition(context->m_game_name, zoneName, zoneDefinition))
return nullptr;
if (!LoadGdtFilesFromZoneDefinition(context->m_gdt_files, zoneName, zoneDefinition, gdtSearchPath))
return nullptr;
for(const auto* assetLoader : ZONE_CREATORS)
{
if(assetLoader->SupportsGame(context->m_game_name))
@ -523,6 +523,8 @@ public:
{
printf("Loaded zone \"%s\"\n", zone->m_name.c_str());
}
zones.emplace_back(std::move(zone));
}
auto result = true;

View File

@ -1,12 +1,14 @@
#include "ZoneCreationContext.h"
ZoneCreationContext::ZoneCreationContext()
: m_asset_search_path(nullptr)
: m_asset_search_path(nullptr),
m_definition(nullptr)
{
}
ZoneCreationContext::ZoneCreationContext(std::string zoneName, ISearchPath* assetSearchPath)
: m_asset_search_path(assetSearchPath),
m_zone_name(std::move(zoneName))
ZoneCreationContext::ZoneCreationContext(std::string zoneName, ISearchPath* assetSearchPath, ZoneDefinition* definition)
: m_zone_name(std::move(zoneName)),
m_asset_search_path(assetSearchPath),
m_definition(definition)
{
}

View File

@ -5,15 +5,17 @@
#include "SearchPath/ISearchPath.h"
#include "Obj/Gdt/Gdt.h"
#include "Zone/Definition/ZoneDefinition.h"
class ZoneCreationContext
{
public:
ISearchPath* m_asset_search_path;
std::string m_zone_name;
std::string m_game_name;
ISearchPath* m_asset_search_path;
ZoneDefinition* m_definition;
std::vector<std::unique_ptr<Gdt>> m_gdt_files;
ZoneCreationContext();
ZoneCreationContext(std::string zoneName, ISearchPath* assetSearchPath);
ZoneCreationContext(std::string zoneName, ISearchPath* assetSearchPath, ZoneDefinition* definition);
};