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"
|
2016-09-19 01:04:26 +00:00
|
|
|
#include <array>
|
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
|
|
|
{
|
|
|
|
|
2016-03-01 03:08:25 +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;
|
2016-09-18 23:47:48 +00:00
|
|
|
memmove(buf, m_buf.get() + m_pos, len);
|
2015-08-22 01:58:41 +00:00
|
|
|
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) {}
|
|
|
|
|
2016-03-02 07:29:19 +00:00
|
|
|
template <class PAKBRIDGE>
|
|
|
|
void checkEntry(const PAKBRIDGE& pakBridge, const typename PAKBRIDGE::PAKType::Entry& entry);
|
2015-09-23 22:59:12 +00:00
|
|
|
|
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;
|
|
|
|
|
2016-03-01 03:08:25 +00:00
|
|
|
/** 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;
|
2016-09-18 23:47:48 +00:00
|
|
|
std::array<const hecl::SystemChar*, 6> fileExts = {};
|
|
|
|
unsigned weight = 0;
|
2015-10-27 00:19:03 +00:00
|
|
|
std::function<void(const SpecBase&, PAKEntryReadStream&, PAKRouter<PAKBRIDGE>&,
|
|
|
|
typename PAKBRIDGE::PAKType::Entry&)> func_name;
|
2016-09-18 23:47:48 +00:00
|
|
|
|
|
|
|
ResExtractor() = default;
|
|
|
|
|
2016-09-19 01:04:26 +00:00
|
|
|
ResExtractor(std::function<bool(PAKEntryReadStream&, const hecl::ProjectPath&)> func,
|
2016-09-18 23:47:48 +00:00
|
|
|
std::array<const hecl::SystemChar*, 6>&& fileExtsIn, unsigned weightin=0,
|
|
|
|
std::function<void(const SpecBase&, PAKEntryReadStream&, PAKRouter<PAKBRIDGE>&,
|
2016-09-19 01:04:26 +00:00
|
|
|
typename PAKBRIDGE::PAKType::Entry&)> nfunc={})
|
2016-09-18 23:47:48 +00:00
|
|
|
: func_a(std::move(func)), fileExts(std::move(fileExtsIn)), weight(weightin), func_name(std::move(nfunc)) {}
|
|
|
|
|
|
|
|
ResExtractor(std::function<bool(const SpecBase&, PAKEntryReadStream&, const hecl::ProjectPath&, PAKRouter<PAKBRIDGE>&,
|
|
|
|
const typename PAKBRIDGE::PAKType::Entry&, bool, hecl::BlenderToken&,
|
2016-09-19 01:04:26 +00:00
|
|
|
std::function<void(const hecl::SystemChar*)>)> func,
|
2016-09-18 23:47:48 +00:00
|
|
|
std::array<const hecl::SystemChar*, 6>&& fileExtsIn, unsigned weightin=0,
|
|
|
|
std::function<void(const SpecBase&, PAKEntryReadStream&, PAKRouter<PAKBRIDGE>&,
|
2016-09-19 01:04:26 +00:00
|
|
|
typename PAKBRIDGE::PAKType::Entry&)> nfunc={})
|
2016-09-18 23:47:48 +00:00
|
|
|
: func_b(std::move(func)), fileExts(std::move(fileExtsIn)), weight(weightin), func_name(std::move(nfunc)) {}
|
2015-08-22 01:58:41 +00:00
|
|
|
};
|
|
|
|
|
2016-03-01 03:08:25 +00:00
|
|
|
/** Level hierarchy representation */
|
2015-09-28 01:13:27 +00:00
|
|
|
template <class IDType>
|
|
|
|
struct Level
|
|
|
|
{
|
2016-03-04 23:04:53 +00:00
|
|
|
hecl::SystemString name;
|
2015-09-28 01:13:27 +00:00
|
|
|
struct Area
|
|
|
|
{
|
2016-03-04 23:04:53 +00:00
|
|
|
hecl::SystemString name;
|
2015-09-28 01:13:27 +00:00
|
|
|
struct Layer
|
|
|
|
{
|
2016-03-04 23:04:53 +00:00
|
|
|
hecl::SystemString name;
|
2015-10-12 21:10:38 +00:00
|
|
|
bool active;
|
2015-09-28 01:13:27 +00:00
|
|
|
std::unordered_set<IDType> resources;
|
|
|
|
};
|
|
|
|
std::vector<Layer> layers;
|
|
|
|
std::unordered_set<IDType> resources;
|
|
|
|
};
|
|
|
|
std::unordered_map<IDType, Area> areas;
|
|
|
|
};
|
|
|
|
|
2016-03-01 03:08:25 +00:00
|
|
|
/** PAKRouter (for detecting shared entry locations) */
|
2015-08-22 01:58:41 +00:00
|
|
|
template <class BRIDGETYPE>
|
2016-03-01 03:08:25 +00:00
|
|
|
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;
|
2015-09-06 05:34:30 +00:00
|
|
|
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;
|
2015-09-06 05:34:30 +00:00
|
|
|
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)
|
2016-03-01 03:08:25 +00:00
|
|
|
: 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
|
|
|
|
2016-03-02 07:29:19 +00:00
|
|
|
void build(std::vector<BRIDGETYPE>& bridges, std::function<void(float)> progress);
|
2016-03-02 01:49:21 +00:00
|
|
|
|
2016-03-02 07:29:19 +00:00
|
|
|
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,
|
2016-03-02 07:29:19 +00:00
|
|
|
const ResExtractor<BRIDGETYPE>& extractor) const;
|
2016-03-05 00:03:41 +00:00
|
|
|
hecl::ProjectPath getWorking(const EntryType* entry) const;
|
2016-04-06 02:04:15 +00:00
|
|
|
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;
|
2016-04-06 02:04:15 +00:00
|
|
|
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
|
|
|
|
2016-03-02 07:29:19 +00:00
|
|
|
std::string getBestEntryName(const EntryType& entry) const;
|
|
|
|
std::string getBestEntryName(const IDType& entry) const;
|
2015-08-22 01:58:41 +00:00
|
|
|
|
2016-04-02 00:18:35 +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
|
|
|
|
2015-09-06 05:34:30 +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,
|
2016-03-02 07:29:19 +00:00
|
|
|
bool currentPAK=false) const;
|
2015-08-22 01:58:41 +00:00
|
|
|
|
|
|
|
template <typename DNA>
|
2015-09-21 23:36:15 +00:00
|
|
|
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;
|
2015-09-21 23:36:15 +00:00
|
|
|
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;
|
|
|
|
}
|
2015-09-06 05:34:30 +00:00
|
|
|
|
2016-03-02 07:29:19 +00:00
|
|
|
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__
|