Add ability to reference resources by name

This commit is contained in:
Phillip Stephens 2016-02-20 04:40:58 -08:00
parent a37f536ae9
commit 764d75f38e
5 changed files with 78 additions and 29 deletions

View File

@ -17,31 +17,26 @@ ProjectResourceFactory::ProjectResourceFactory()
m_factoryMgr.AddFactory(HECL::FOURCC('PART'), pshag::FParticleFactory);
}
void ProjectResourceFactory::RecursiveAddDirObjects(const HECL::ProjectPath& path)
{
HECL::DirectoryEnumerator de = path.enumerateDir();
for (const HECL::DirectoryEnumerator::Entry& ent : de)
{
if (ent.m_isDir)
RecursiveAddDirObjects(HECL::ProjectPath(path, ent.m_name));
if (ent.m_name.size() == 13 && ent.m_name[4] == _S('_'))
{
HECL::SystemUTF8View entu8(ent.m_name);
u32 id = strtoul(entu8.c_str() + 5, nullptr, 16);
if (id)
{
pshag::SObjectTag objTag = {HECL::FourCC(entu8.c_str()), id};
if (m_resPaths.find(objTag) == m_resPaths.end())
m_resPaths[objTag] = HECL::ProjectPath(path, ent.m_name);
}
}
}
}
void ProjectResourceFactory::BuildObjectMap(const HECL::Database::Project::ProjectDataSpec &spec)
{
m_resPaths.clear();
RecursiveAddDirObjects(spec.cookedPath);
m_namedResources.clear();
HECL::SystemString catalogPath = HECL::ProjectPath(spec.cookedPath, std::string(spec.spec.m_name) + "/catalog.yaml").getAbsolutePath();
FILE* catalogFile = HECL::Fopen(catalogPath.c_str(), _S("r"));
if (!HECL::StrCmp(spec.spec.m_name, "MP3"))
{
DataSpec::NamedResourceCatalog<DataSpec::UniqueID64> catalog;
if (catalogFile)
catalog.fromYAMLFile(catalogFile);
RecursiveAddDirObjects(spec.cookedPath, catalog);
}
else
{
DataSpec::NamedResourceCatalog<DataSpec::UniqueID32> catalog;
if (catalogFile)
catalog.fromYAMLFile(catalogFile);
RecursiveAddDirObjects(spec.cookedPath, catalog);
}
}
std::unique_ptr<pshag::IObj> ProjectResourceFactory::Build(const pshag::SObjectTag& tag,
@ -83,9 +78,12 @@ bool ProjectResourceFactory::CanBuild(const pshag::SObjectTag& tag)
return true;
}
const pshag::SObjectTag* ProjectResourceFactory::GetResourceIdByName(const char*) const
const pshag::SObjectTag* ProjectResourceFactory::GetResourceIdByName(const char* name) const
{
if (m_namedResources.find(name) == m_namedResources.end())
return nullptr;
const pshag::SObjectTag& tag = m_namedResources.at(name);
return &tag;
}
}

View File

@ -3,6 +3,7 @@
#include "Runtime/IFactory.hpp"
#include "Runtime/CFactoryMgr.hpp"
#include "DataSpec/DNACommon/NamedResourceCatalog.hpp"
namespace URDE
{
@ -10,8 +11,57 @@ namespace URDE
class ProjectResourceFactory : public pshag::IFactory
{
std::unordered_map<pshag::SObjectTag, HECL::ProjectPath> m_resPaths;
std::unordered_map<std::string, pshag::SObjectTag> m_namedResources;
pshag::CFactoryMgr m_factoryMgr;
void RecursiveAddDirObjects(const HECL::ProjectPath& path);
template <class IDType>
void RecursiveAddDirObjects(const HECL::ProjectPath& path, const DataSpec::NamedResourceCatalog<IDType>& catalog)
{
HECL::DirectoryEnumerator de = path.enumerateDir();
const int idLen = 5 + (IDType::BinarySize() * 2);
for (const HECL::DirectoryEnumerator::Entry& ent : de)
{
if (ent.m_isDir)
RecursiveAddDirObjects(HECL::ProjectPath(path, ent.m_name), catalog);
if (ent.m_name.size() == idLen && ent.m_name[4] == _S('_'))
{
HECL::SystemUTF8View entu8(ent.m_name);
u64 id = strtouq(entu8.c_str() + 5, nullptr, 16);
if (id)
{
pshag::SObjectTag objTag = {HECL::FourCC(entu8.c_str()), id};
if (m_resPaths.find(objTag) == m_resPaths.end())
m_resPaths[objTag] = HECL::ProjectPath(path, ent.m_name);
}
}
else
{
HECL::SystemUTF8View nameView(ent.m_name);
auto it = std::find_if(catalog.namedResources.begin(), catalog.namedResources.end(),
[&nameView](const typename DataSpec::NamedResourceCatalog<IDType>::NamedResource& res) -> bool
{ return res.name == nameView.str(); });
if (it == catalog.namedResources.end())
continue;
const typename DataSpec::NamedResourceCatalog<IDType>::NamedResource& nr = *it;
pshag::SObjectTag objTag = GetTag<IDType>(nr);
m_namedResources[nr.name.c_str()] = objTag;
m_resPaths[objTag] = HECL::ProjectPath(path, ent.m_name);
}
}
}
template <class IDType>
pshag::SObjectTag GetTag(const DataSpec::NamedResourceCatalog<DataSpec::UniqueID32>::NamedResource &nr,
typename std::enable_if<std::is_same<IDType, DataSpec::UniqueID32>::value>::type* = 0)
{ return { nr.type, nr.uid.toUint32() }; }
template <class IDType>
pshag::SObjectTag GetTag(const typename DataSpec::NamedResourceCatalog<IDType>::NamedResource& nr,
typename std::enable_if<std::is_same<IDType, DataSpec::UniqueID64>::value>::type* = 0)
{ return { nr.type, nr.uid.toUint64() }; }
public:
ProjectResourceFactory();
void BuildObjectMap(const HECL::Database::Project::ProjectDataSpec& spec);

View File

@ -20,7 +20,8 @@ namespace URDE
void ViewManager::BuildTestPART(pshag::IObjectStore& objStore)
{
m_partGenDesc = objStore.GetObj({HECL::FOURCC('PART'), 0x972A5CD2});
//m_partGenDesc = objStore.GetObj({HECL::FOURCC('PART'), 0x972A5CD2});
m_partGenDesc = objStore.GetObj("WallSpark");
m_partGen.reset(new pshag::CElementGen(m_partGenDesc,
pshag::CElementGen::EModelOrientationType::Normal,
pshag::CElementGen::EOptionalSystemFlags::None));

View File

@ -244,7 +244,6 @@ int CMain::appMain(boo::IApplication* app)
while (!xe8_b24_finished)
{
mainWindow->waitForRetrace();
xe8_b24_finished = archSupport->Update();
if (archSupport->isRectDirty())
@ -259,6 +258,7 @@ int CMain::appMain(boo::IApplication* app)
gfxQ->clearTarget();
gfxQ->resolveDisplay(renderTex);
gfxQ->execute();
mainWindow->waitForRetrace();
}
return 0;
}

View File

@ -12,7 +12,7 @@ namespace pshag
{
using FourCC = HECL::FourCC;
using TResId = u32;
using TResId = u64;
struct SObjectTag
{