metaforce/Editor/ProjectResourceFactoryBase.hpp

164 lines
7.6 KiB
C++
Raw Normal View History

2018-10-07 03:42:33 +00:00
#pragma once
#include <list>
#include <memory>
#include <mutex>
#include <optional>
#include <thread>
#include <unordered_map>
#include "DataSpec/SpecBase.hpp"
#include "Runtime/CFactoryMgr.hpp"
#include "Runtime/CResFactory.hpp"
#include "Runtime/IFactory.hpp"
#include <hecl/ClientProcess.hpp>
#include <hecl/Database.hpp>
2021-04-10 08:42:06 +00:00
namespace metaforce {
2018-12-08 05:30:43 +00:00
class ProjectResourceFactoryBase : public IFactory {
friend class ProjectResourcePool;
hecl::ClientProcess& m_clientProc;
2016-08-08 04:48:18 +00:00
public:
2021-04-10 08:42:06 +00:00
struct AsyncTask : metaforce::IDvdRequest {
2018-12-08 05:30:43 +00:00
ProjectResourceFactoryBase& m_parent;
SObjectTag x0_tag;
// IDvdRequest* x8_dvdReq = nullptr;
std::unique_ptr<u8[]>* xc_targetDataPtr = nullptr;
u8* xc_targetDataRawPtr = nullptr;
std::unique_ptr<IObj>* xc_targetObjPtr = nullptr;
std::unique_ptr<u8[]> x10_loadBuffer;
u32 x14_resSize = UINT32_MAX;
u32 x14_resOffset = 0;
CVParamTransfer x18_cvXfer;
CObjectReference* m_selfRef = nullptr;
hecl::ProjectPath m_workingPath;
hecl::ProjectPath m_cookedPath;
std::shared_ptr<const hecl::ClientProcess::CookTransaction> m_cookTransaction;
std::shared_ptr<const hecl::ClientProcess::BufferTransaction> m_bufTransaction;
bool m_failed = false;
bool m_complete = false;
AsyncTask(ProjectResourceFactoryBase& parent, const SObjectTag& tag, std::unique_ptr<u8[]>& ptr)
: m_parent(parent), x0_tag(tag), xc_targetDataPtr(&ptr) {}
AsyncTask(ProjectResourceFactoryBase& parent, const SObjectTag& tag, std::unique_ptr<u8[]>& ptr, u32 size, u32 off)
: m_parent(parent), x0_tag(tag), xc_targetDataPtr(&ptr), x14_resSize(size), x14_resOffset(off) {}
AsyncTask(ProjectResourceFactoryBase& parent, const SObjectTag& tag, u8* ptr)
: m_parent(parent), x0_tag(tag), xc_targetDataRawPtr(ptr) {}
AsyncTask(ProjectResourceFactoryBase& parent, const SObjectTag& tag, u8* ptr, u32 size, u32 off)
: m_parent(parent), x0_tag(tag), xc_targetDataRawPtr(ptr), x14_resSize(size), x14_resOffset(off) {}
AsyncTask(ProjectResourceFactoryBase& parent, const SObjectTag& tag, std::unique_ptr<IObj>* ptr,
const CVParamTransfer& xfer, CObjectReference* selfRef)
: m_parent(parent), x0_tag(tag), xc_targetObjPtr(ptr), x18_cvXfer(xfer), m_selfRef(selfRef) {}
/* Cook only */
AsyncTask(ProjectResourceFactoryBase& parent, const SObjectTag& tag) : m_parent(parent), x0_tag(tag) {}
2021-04-10 08:42:06 +00:00
void EnsurePath(const metaforce::SObjectTag& tag, const hecl::ProjectPath& path);
2018-12-08 05:30:43 +00:00
void CookComplete();
bool AsyncPump();
void WaitUntilComplete() override;
bool IsComplete() override { return m_complete; }
void PostCancelRequest() override {}
EMediaType GetMediaType() const override { return EMediaType::Real; }
2018-12-08 05:30:43 +00:00
};
2016-08-08 04:48:18 +00:00
protected:
hecl::Database::Project* m_proj = nullptr;
2018-12-08 05:30:43 +00:00
const hecl::Database::DataSpecEntry* m_origSpec = nullptr;
const hecl::Database::DataSpecEntry* m_pcSpec = nullptr;
/* Used to resolve cooked paths */
std::unique_ptr<hecl::Database::IDataSpec> m_cookSpec;
2021-04-10 08:42:06 +00:00
metaforce::CFactoryMgr m_factoryMgr;
2018-12-08 05:30:43 +00:00
std::list<std::shared_ptr<AsyncTask>> m_asyncLoadList;
std::unordered_map<SObjectTag, std::list<std::shared_ptr<AsyncTask>>::iterator> m_asyncLoadMap;
std::shared_ptr<AsyncTask> _AddTask(const std::shared_ptr<AsyncTask>& ptr);
std::list<std::shared_ptr<AsyncTask>>::iterator _RemoveTask(std::list<std::shared_ptr<AsyncTask>>::iterator it);
std::unordered_map<SObjectTag, std::list<std::shared_ptr<AsyncTask>>::iterator>::iterator
_RemoveTask(std::unordered_map<SObjectTag, std::list<std::shared_ptr<AsyncTask>>::iterator>::iterator it);
std::unordered_map<SObjectTag, std::list<std::shared_ptr<AsyncTask>>::iterator>::iterator
_RemoveTask(const SObjectTag& tag);
static AsyncTask& _GetAsyncTask(std::list<std::shared_ptr<AsyncTask>>::iterator it) { return **it; }
static AsyncTask&
_GetAsyncTask(std::unordered_map<SObjectTag, std::list<std::shared_ptr<AsyncTask>>::iterator>::iterator it) {
return **it->second;
}
bool PrepForReadSync(const SObjectTag& tag, const hecl::ProjectPath& path,
std::optional<athena::io::FileReader>& fr);
2018-12-08 05:30:43 +00:00
2021-04-10 08:42:06 +00:00
bool WaitForTagReady(const metaforce::SObjectTag& tag, const hecl::ProjectPath*& pathOut) {
2018-12-08 05:30:43 +00:00
return static_cast<DataSpec::SpecBase&>(*m_cookSpec).waitForTagReady(tag, pathOut);
}
SObjectTag TagFromPath(const hecl::ProjectPath& path) const {
return static_cast<DataSpec::SpecBase&>(*m_cookSpec).tagFromPath(path);
2018-12-08 05:30:43 +00:00
}
SObjectTag BuildTagFromPath(const hecl::ProjectPath& path) const {
return static_cast<DataSpec::SpecBase&>(*m_cookSpec).buildTagFromPath(path);
2018-12-08 05:30:43 +00:00
}
void GetTagListForFile(const char* pakName, std::vector<SObjectTag>& out) const override {
2018-12-08 05:30:43 +00:00
return static_cast<DataSpec::SpecBase&>(*m_cookSpec).getTagListForFile(pakName, out);
}
void CancelBackgroundIndex() {
if (m_cookSpec)
return static_cast<DataSpec::SpecBase&>(*m_cookSpec).cancelBackgroundIndex();
}
void BeginBackgroundIndex(hecl::Database::Project& proj, const hecl::Database::DataSpecEntry& origSpec,
const hecl::Database::DataSpecEntry& pcSpec);
bool SyncCook(const hecl::ProjectPath& working);
CFactoryFnReturn BuildSync(const SObjectTag& tag, const hecl::ProjectPath& path, const CVParamTransfer& paramXfer,
CObjectReference* selfRef);
2021-04-10 08:42:06 +00:00
std::shared_ptr<AsyncTask> BuildAsyncInternal(const metaforce::SObjectTag&, const metaforce::CVParamTransfer&,
std::unique_ptr<metaforce::IObj>*, CObjectReference* selfRef);
public:
2018-12-08 05:30:43 +00:00
ProjectResourceFactoryBase(hecl::ClientProcess& clientProc) : m_clientProc(clientProc) {}
2021-04-10 08:42:06 +00:00
std::unique_ptr<metaforce::IObj> Build(const metaforce::SObjectTag&, const metaforce::CVParamTransfer&,
CObjectReference* selfRef) override;
2021-04-10 08:42:06 +00:00
void BuildAsync(const metaforce::SObjectTag&, const metaforce::CVParamTransfer&, std::unique_ptr<metaforce::IObj>*,
CObjectReference* selfRef) override;
2021-04-10 08:42:06 +00:00
void CancelBuild(const metaforce::SObjectTag&) override;
bool CanBuild(const metaforce::SObjectTag&) override;
const metaforce::SObjectTag* GetResourceIdByName(std::string_view) const override;
FourCC GetResourceTypeById(CAssetId id) const override;
2018-12-08 05:30:43 +00:00
hecl::ProjectPath GetCookedPath(const hecl::ProjectPath& working, bool pcTarget) const {
return static_cast<DataSpec::SpecBase&>(*m_cookSpec).getCookedPath(working, pcTarget);
}
void EnumerateResources(const std::function<bool(const SObjectTag&)>& lambda) const override;
void EnumerateNamedResources(const std::function<bool(std::string_view, const SObjectTag&)>& lambda) const override;
2018-12-08 05:30:43 +00:00
u32 ResourceSize(const SObjectTag& tag) override;
2021-04-10 08:42:06 +00:00
std::shared_ptr<metaforce::IDvdRequest> LoadResourceAsync(const metaforce::SObjectTag& tag, void* target) override;
std::shared_ptr<metaforce::IDvdRequest> LoadResourcePartAsync(const metaforce::SObjectTag& tag, u32 off, u32 size,
void* target) override;
2021-04-10 08:42:06 +00:00
std::unique_ptr<u8[]> LoadResourceSync(const metaforce::SObjectTag& tag) override;
std::unique_ptr<u8[]> LoadNewResourcePartSync(const metaforce::SObjectTag& tag, u32 off, u32 size) override;
2018-12-08 05:30:43 +00:00
2021-04-10 08:42:06 +00:00
std::shared_ptr<AsyncTask> CookResourceAsync(const metaforce::SObjectTag& tag);
2018-12-08 05:30:43 +00:00
template <typename ItType>
bool AsyncPumpTask(ItType& it);
void AsyncIdle() override;
2018-12-08 05:30:43 +00:00
void Shutdown() { CancelBackgroundIndex(); }
bool IsBusy() const { return m_asyncLoadMap.size() != 0; }
SObjectTag TagFromPath(hecl::SystemStringView path) const { return TagFromPath(hecl::ProjectPath(*m_proj, path)); }
2018-12-08 05:30:43 +00:00
~ProjectResourceFactoryBase() override { Shutdown(); }
};
2021-04-10 08:42:06 +00:00
} // namespace metaforce