metaforce/Runtime/CResFactory.cpp

126 lines
3.8 KiB
C++
Raw Normal View History

#include "CResFactory.hpp"
#include "CSimplePool.hpp"
2017-11-19 07:10:54 +00:00
#include "CStopwatch.hpp"
2016-03-04 23:04:53 +00:00
namespace urde
{
2017-10-27 10:10:32 +00:00
static logvisor::Module Log("CResFactory");
void CResFactory::AddToLoadList(SLoadingData&& data)
2015-08-22 01:58:41 +00:00
{
m_loadMap[data.x0_tag] = m_loadList.insert(m_loadList.end(), std::move(data));
2015-08-22 01:58:41 +00:00
}
CFactoryFnReturn CResFactory::BuildSync(const SObjectTag& tag, const CVParamTransfer& xfer, CObjectReference* selfRef)
{
2017-10-27 10:10:32 +00:00
CFactoryFnReturn ret;
if (x5c_factoryMgr.CanMakeMemory(tag))
{
std::unique_ptr<uint8_t[]> data;
2017-10-28 07:08:48 +00:00
int size = 0;
x4_loader.LoadMemResourceSync(tag, data, &size);
2017-10-28 07:08:48 +00:00
if (size)
ret = x5c_factoryMgr.MakeObjectFromMemory(tag, std::move(data), size,
x4_loader.GetResourceCompression(tag), xfer, selfRef);
}
else
{
2017-10-28 07:08:48 +00:00
if (auto rp = x4_loader.LoadNewResourceSync(tag, nullptr))
ret = x5c_factoryMgr.MakeObject(tag, *rp, xfer, selfRef);
}
2017-10-27 10:10:32 +00:00
Log.report(logvisor::Warning, "sync-built %.4s %08X", tag.type.getChars(), tag.id.Value());
return ret;
}
bool CResFactory::PumpResource(SLoadingData& data)
{
if (data.x8_dvdReq->IsComplete())
{
*data.xc_targetPtr =
x5c_factoryMgr.MakeObjectFromMemory(data.x0_tag, std::move(data.x10_loadBuffer),
data.x14_resSize, data.m_compressed, data.x18_cvXfer,
data.m_selfRef);
2017-10-27 10:10:32 +00:00
Log.report(logvisor::Info, "async-built %.4s %08X", data.x0_tag.type.getChars(), data.x0_tag.id.Value());
return true;
}
return false;
}
std::unique_ptr<IObj> CResFactory::Build(const SObjectTag& tag, const CVParamTransfer& xfer, CObjectReference* selfRef)
{
auto search = m_loadMap.find(tag);
if (search != m_loadMap.end())
{
while (!PumpResource(*search->second) || !search->second->xc_targetPtr) {}
2017-10-28 07:08:48 +00:00
std::unique_ptr<IObj> ret = std::move(*search->second->xc_targetPtr);
m_loadList.erase(search->second);
m_loadMap.erase(search);
2017-10-28 07:08:48 +00:00
return ret;
}
return BuildSync(tag, xfer, selfRef);
}
void CResFactory::BuildAsync(const SObjectTag& tag, const CVParamTransfer& xfer, std::unique_ptr<IObj>* target,
CObjectReference* selfRef)
2015-08-22 01:58:41 +00:00
{
auto search = m_loadMap.find(tag);
2017-10-28 07:08:48 +00:00
if (search == m_loadMap.end())
{
SLoadingData data(tag, target, xfer, x4_loader.GetResourceCompression(tag), selfRef);
2017-10-28 07:08:48 +00:00
data.x14_resSize = x4_loader.ResourceSize(tag);
if (data.x14_resSize)
{
data.x10_loadBuffer = std::unique_ptr<u8[]>(new u8[data.x14_resSize]);
data.x8_dvdReq = x4_loader.LoadResourceAsync(tag, data.x10_loadBuffer.get());
AddToLoadList(std::move(data));
}
}
2015-08-22 01:58:41 +00:00
}
void CResFactory::AsyncIdle()
{
2017-10-27 10:10:32 +00:00
if (m_loadList.empty())
return;
auto startTime = std::chrono::steady_clock::now();
while (std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::steady_clock::now() - startTime).count() < 2)
{
auto& task = m_loadList.front();
if (PumpResource(task))
{
m_loadMap.erase(task.x0_tag);
m_loadList.pop_front();
2017-10-28 07:08:48 +00:00
if (m_loadList.empty())
return;
}
}
}
void CResFactory::CancelBuild(const SObjectTag& tag)
{
auto search = m_loadMap.find(tag);
if (search != m_loadMap.end())
{
search->second->x8_dvdReq->PostCancelRequest();
m_loadList.erase(search->second);
m_loadMap.erase(search);
}
}
void CResFactory::LoadOriginalIDs(CSimplePool& sp)
{
m_origIds = sp.GetObj("MP1OriginalIDs");
}
CAssetId CResFactory::TranslateOriginalToNew(CAssetId id) const
{
return m_origIds->TranslateOriginalToNew(id);
}
CAssetId CResFactory::TranslateNewToOriginal(CAssetId id) const
2015-08-22 01:58:41 +00:00
{
return m_origIds->TranslateNewToOriginal(id);
2015-08-22 01:58:41 +00:00
}
}