metaforce/DataSpec/DNACommon/PAK.hpp

193 lines
6.9 KiB
C++
Raw Normal View History

2015-08-22 01:58:41 +00:00
#ifndef __DNACOMMON_PAK_HPP__
#define __DNACOMMON_PAK_HPP__
#include "DNACommon.hpp"
2016-04-01 04:25:00 +00:00
#include "boo/ThreadLocalPtr.hpp"
2015-08-22 01:58:41 +00:00
2016-02-13 09:02:47 +00:00
namespace DataSpec
2015-08-22 01:58:41 +00:00
{
/** PAK entry stream reader */
2016-03-04 23:04:53 +00:00
class PAKEntryReadStream : public athena::io::IStreamReader
2015-08-22 01:58:41 +00:00
{
std::unique_ptr<atUint8[]> m_buf;
atUint64 m_sz;
atUint64 m_pos;
public:
PAKEntryReadStream() {}
operator bool() const {return m_buf.operator bool();}
PAKEntryReadStream(const PAKEntryReadStream& other) = delete;
PAKEntryReadStream(PAKEntryReadStream&& other) = default;
PAKEntryReadStream& operator=(const PAKEntryReadStream& other) = delete;
PAKEntryReadStream& operator=(PAKEntryReadStream&& other) = default;
PAKEntryReadStream(std::unique_ptr<atUint8[]>&& buf, atUint64 sz, atUint64 pos)
: m_buf(std::move(buf)), m_sz(sz), m_pos(pos)
{
if (m_pos >= m_sz)
2016-03-04 23:04:53 +00:00
LogDNACommon.report(logvisor::Fatal, "PAK stream cursor overrun");
2015-08-22 01:58:41 +00:00
}
2016-03-04 23:04:53 +00:00
void seek(atInt64 pos, athena::SeekOrigin origin)
2015-08-22 01:58:41 +00:00
{
2016-03-04 23:04:53 +00:00
if (origin == athena::Begin)
2015-08-22 01:58:41 +00:00
m_pos = pos;
2016-03-04 23:04:53 +00:00
else if (origin == athena::Current)
2015-08-22 01:58:41 +00:00
m_pos += pos;
2016-03-04 23:04:53 +00:00
else if (origin == athena::End)
2015-08-22 01:58:41 +00:00
m_pos = m_sz + pos;
2015-10-26 02:31:09 +00:00
if (m_pos > m_sz)
2016-03-04 23:04:53 +00:00
LogDNACommon.report(logvisor::Fatal, "PAK stream cursor overrun");
2015-08-22 01:58:41 +00:00
}
2015-09-22 01:58:26 +00:00
atUint64 position() const {return m_pos;}
atUint64 length() const {return m_sz;}
const atUint8* data() const {return m_buf.get();}
atUint64 readUBytesToBuf(void* buf, atUint64 len)
2015-08-22 01:58:41 +00:00
{
atUint64 bufEnd = m_pos + len;
if (bufEnd > m_sz)
len -= bufEnd - m_sz;
memcpy(buf, m_buf.get() + m_pos, len);
m_pos += len;
return len;
}
};
struct UniqueResult
{
2015-11-21 01:16:07 +00:00
enum class Type
2015-08-22 01:58:41 +00:00
{
2015-11-21 01:16:07 +00:00
NotFound,
Pak,
Level,
Area,
Layer
} m_type = Type::NotFound;
2016-03-04 23:04:53 +00:00
const hecl::SystemString* m_levelName = nullptr;
const hecl::SystemString* m_areaName = nullptr;
const hecl::SystemString* m_layerName = nullptr;
2015-08-22 01:58:41 +00:00
UniqueResult() = default;
2015-09-24 03:57:45 +00:00
UniqueResult(Type tp) : m_type(tp) {}
template <class PAKBRIDGE>
void checkEntry(const PAKBRIDGE& pakBridge, const typename PAKBRIDGE::PAKType::Entry& entry);
2016-03-05 00:03:41 +00:00
hecl::ProjectPath uniquePath(const hecl::ProjectPath& pakPath) const;
2015-08-22 01:58:41 +00:00
};
template <class BRIDGETYPE>
class PAKRouter;
/** Resource extractor type */
2015-08-22 01:58:41 +00:00
template <class PAKBRIDGE>
struct ResExtractor
{
2016-03-04 23:04:53 +00:00
std::function<bool(PAKEntryReadStream&, const hecl::ProjectPath&)> func_a;
std::function<bool(const SpecBase&, PAKEntryReadStream&, const hecl::ProjectPath&, PAKRouter<PAKBRIDGE>&,
2016-04-01 04:25:00 +00:00
const typename PAKBRIDGE::PAKType::Entry&, bool, hecl::BlenderToken&,
2016-03-04 23:04:53 +00:00
std::function<void(const hecl::SystemChar*)>)> func_b;
const hecl::SystemChar* fileExts[4];
2015-08-22 01:58:41 +00:00
unsigned weight;
std::function<void(const SpecBase&, PAKEntryReadStream&, PAKRouter<PAKBRIDGE>&,
typename PAKBRIDGE::PAKType::Entry&)> func_name;
2015-08-22 01:58:41 +00:00
};
/** Level hierarchy representation */
template <class IDType>
struct Level
{
2016-03-04 23:04:53 +00:00
hecl::SystemString name;
struct Area
{
2016-03-04 23:04:53 +00:00
hecl::SystemString name;
struct Layer
{
2016-03-04 23:04:53 +00:00
hecl::SystemString name;
2015-10-12 21:10:38 +00:00
bool active;
std::unordered_set<IDType> resources;
};
std::vector<Layer> layers;
std::unordered_set<IDType> resources;
};
std::unordered_map<IDType, Area> areas;
};
/** PAKRouter (for detecting shared entry locations) */
2015-08-22 01:58:41 +00:00
template <class BRIDGETYPE>
class PAKRouter : public PAKRouterBase
2015-08-22 01:58:41 +00:00
{
public:
using PAKType = typename BRIDGETYPE::PAKType;
using IDType = typename PAKType::IDType;
using EntryType = typename PAKType::Entry;
using RigPair = std::pair<IDType, IDType>;
2015-08-22 01:58:41 +00:00
private:
const std::vector<BRIDGETYPE>* m_bridges = nullptr;
2016-03-04 23:04:53 +00:00
std::vector<std::pair<hecl::ProjectPath,hecl::ProjectPath>> m_bridgePaths;
2016-04-01 04:25:00 +00:00
ThreadLocalPtr<void> m_curBridgeIdx;
2016-03-04 23:04:53 +00:00
const hecl::ProjectPath& m_gameWorking;
const hecl::ProjectPath& m_gameCooked;
hecl::ProjectPath m_sharedWorking;
hecl::ProjectPath m_sharedCooked;
2016-04-01 04:25:00 +00:00
ThreadLocalPtr<const PAKType> m_pak;
ThreadLocalPtr<const nod::Node> m_node;
2015-08-22 01:58:41 +00:00
std::unordered_map<IDType, std::pair<size_t, EntryType*>> m_uniqueEntries;
std::unordered_map<IDType, std::pair<size_t, EntryType*>> m_sharedEntries;
std::unordered_map<IDType, RigPair> m_cmdlRigs;
2015-08-22 01:58:41 +00:00
public:
2016-03-04 23:04:53 +00:00
PAKRouter(const SpecBase& dataSpec, const hecl::ProjectPath& working, const hecl::ProjectPath& cooked)
: PAKRouterBase(dataSpec),
2015-08-22 01:58:41 +00:00
m_gameWorking(working), m_gameCooked(cooked),
m_sharedWorking(working, "Shared"), m_sharedCooked(cooked, "Shared") {}
2015-11-10 02:07:15 +00:00
void build(std::vector<BRIDGETYPE>& bridges, std::function<void(float)> progress);
void enterPAKBridge(const BRIDGETYPE& pakBridge);
2015-08-22 01:58:41 +00:00
2016-03-04 23:04:53 +00:00
hecl::ProjectPath getWorking(const EntryType* entry,
const ResExtractor<BRIDGETYPE>& extractor) const;
2016-03-05 00:03:41 +00:00
hecl::ProjectPath getWorking(const EntryType* entry) const;
hecl::ProjectPath getWorking(const IDType& id, bool silenceWarnings=false) const;
2016-03-05 00:03:41 +00:00
hecl::ProjectPath getCooked(const EntryType* entry) const;
hecl::ProjectPath getCooked(const IDType& id, bool silenceWarnings=false) const;
2016-04-01 04:25:00 +00:00
bool isShared()
{
const PAKType* pak = m_pak.get();
return pak ? !pak->m_noShare : false;
}
2015-10-16 00:35:40 +00:00
2016-03-05 00:03:41 +00:00
hecl::SystemString getResourceRelativePath(const EntryType& a, const IDType& b) const;
2015-10-16 00:35:40 +00:00
std::string getBestEntryName(const EntryType& entry) const;
std::string getBestEntryName(const IDType& entry) const;
2015-08-22 01:58:41 +00:00
bool extractResources(const BRIDGETYPE& pakBridge, bool force, hecl::BlenderToken& btok,
2016-03-05 00:03:41 +00:00
std::function<void(const hecl::SystemChar*, float)> progress);
2015-08-22 01:58:41 +00:00
const typename BRIDGETYPE::PAKType::Entry* lookupEntry(const IDType& entry,
2016-03-04 23:04:53 +00:00
const nod::Node** nodeOut=nullptr,
2015-10-16 00:35:40 +00:00
bool silenceWarnings=false,
bool currentPAK=false) const;
2015-08-22 01:58:41 +00:00
template <typename DNA>
bool lookupAndReadDNA(const IDType& id, DNA& out, bool silenceWarnings=false)
2015-08-22 01:58:41 +00:00
{
2016-03-04 23:04:53 +00:00
const nod::Node* node;
const EntryType* entry = lookupEntry(id, &node, silenceWarnings);
2015-08-22 01:58:41 +00:00
if (!entry)
return false;
PAKEntryReadStream rs = entry->beginReadStream(*node);
out.read(rs);
return true;
}
const RigPair* lookupCMDLRigPair(const IDType& id) const;
2015-09-24 00:59:36 +00:00
2016-03-05 00:03:41 +00:00
hecl::ProjectPath getAreaLayerWorking(const IDType& areaId, int layerIdx) const;
2016-08-10 02:52:00 +00:00
hecl::ProjectPath getAreaLayerWorking(const IDType& areaId, int layerIdx, bool& activeOut) const;
2016-03-05 00:03:41 +00:00
hecl::ProjectPath getAreaLayerCooked(const IDType& areaId, int layerIdx) const;
2016-08-10 02:52:00 +00:00
hecl::ProjectPath getAreaLayerCooked(const IDType& areaId, int layerIdx, bool& activeOut) const;
2015-08-22 01:58:41 +00:00
};
}
#endif // __DNACOMMON_PAK_HPP__