Parse techset files for IW4

This commit is contained in:
Jan
2022-03-26 18:47:43 +01:00
parent 2fda10f133
commit eb5312899f
11 changed files with 345 additions and 2 deletions

View File

@ -1,10 +1,14 @@
#include "AssetLoaderTechniqueSet.h"
#include <cstring>
#include <sstream>
#include <type_traits>
#include "ObjLoading.h"
#include "Game/IW4/IW4.h"
#include "Game/IW4/TechsetConstantsIW4.h"
#include "Pool/GlobalAssetPool.h"
#include "Techset/TechsetFileReader.h"
using namespace IW4;
@ -15,3 +19,68 @@ void* AssetLoaderTechniqueSet::CreateEmptyAsset(const std::string& assetName, Me
techset->name = memory->Dup(assetName.c_str());
return techset;
}
std::string AssetLoaderTechniqueSet::GetTechniqueFileName(const std::string& techniqueName)
{
std::ostringstream ss;
ss << "techniques/" << techniqueName << ".tech";
return ss.str();
}
std::string AssetLoaderTechniqueSet::GetTechsetFileName(const std::string& techsetAssetName)
{
std::ostringstream ss;
ss << "techsets/" << techsetAssetName << ".techset";
return ss.str();
}
MaterialTechnique* AssetLoaderTechniqueSet::LoadTechniqueWithName(const std::string& techniqueName, ISearchPath* searchPath, MemoryManager* memory, IAssetLoadingManager* manager)
{
// TODO: Load technique or use previously loaded one
return nullptr;
}
bool AssetLoaderTechniqueSet::CreateTechsetFromDefinition(const std::string& assetName, const techset::TechsetDefinition& definition, ISearchPath* searchPath, MemoryManager* memory,
IAssetLoadingManager* manager)
{
auto* techset = memory->Create<MaterialTechniqueSet>();
memset(techset, 0, sizeof(MaterialTechniqueSet));
techset->name = memory->Dup(assetName.c_str());
for(auto i = 0u; i < std::extent_v<decltype(MaterialTechniqueSet::techniques)>; i++)
{
std::string techniqueName;
if(definition.GetTechniqueByIndex(i, techniqueName))
{
auto* technique = LoadTechniqueWithName(techniqueName, searchPath, memory, manager);
if (technique == nullptr)
return false;
techset->techniques[i] = technique;
}
}
manager->AddAsset(ASSET_TYPE_TECHNIQUE_SET, assetName, techset);
return true;
}
bool AssetLoaderTechniqueSet::CanLoadFromRaw() const
{
return true;
}
bool AssetLoaderTechniqueSet::LoadFromRaw(const std::string& assetName, ISearchPath* searchPath, MemoryManager* memory, IAssetLoadingManager* manager, Zone* zone) const
{
const auto techsetFileName = GetTechsetFileName(assetName);
const auto file = searchPath->Open(techsetFileName);
if (!file.IsOpen())
return false;
const TechsetFileReader reader(*file.m_stream, techsetFileName, techniqueTypeNames, std::extent_v<decltype(techniqueTypeNames)>);
const auto techsetDefinition = reader.ReadTechsetDefinition();
if(techsetDefinition)
return CreateTechsetFromDefinition(assetName, *techsetDefinition, searchPath, memory, manager);
return false;
}

View File

@ -3,12 +3,21 @@
#include "Game/IW4/IW4.h"
#include "AssetLoading/BasicAssetLoader.h"
#include "SearchPath/ISearchPath.h"
#include "Techset/TechsetDefinition.h"
namespace IW4
{
class AssetLoaderTechniqueSet final : public BasicAssetLoader<ASSET_TYPE_TECHNIQUE_SET, MaterialTechniqueSet>
{
static std::string GetTechniqueFileName(const std::string& techniqueName);
static std::string GetTechsetFileName(const std::string& techsetAssetName);
static MaterialTechnique* LoadTechniqueWithName(const std::string& techniqueName, ISearchPath* searchPath, MemoryManager* memory, IAssetLoadingManager* manager);
static bool CreateTechsetFromDefinition(const std::string& assetName, const techset::TechsetDefinition& definition, ISearchPath* searchPath, MemoryManager* memory,
IAssetLoadingManager* manager);
public:
_NODISCARD void* CreateEmptyAsset(const std::string& assetName, MemoryManager* memory) override;
_NODISCARD bool CanLoadFromRaw() const override;
bool LoadFromRaw(const std::string& assetName, ISearchPath* searchPath, MemoryManager* memory, IAssetLoadingManager* manager, Zone* zone) const override;
};
}