metaforce/DataSpec/DNACommon/DNACommon.cpp

206 lines
7.1 KiB
C++
Raw Normal View History

#include "DNACommon.hpp"
#include "PAK.hpp"
2016-04-10 04:49:02 +00:00
#include "boo/ThreadLocalPtr.hpp"
2018-12-08 05:30:43 +00:00
namespace DataSpec {
2016-03-04 23:04:53 +00:00
logvisor::Module LogDNACommon("urde::DNACommon");
2016-04-10 04:49:02 +00:00
ThreadLocalPtr<SpecBase> g_curSpec;
ThreadLocalPtr<PAKRouterBase> g_PakRouter;
2017-12-29 08:08:12 +00:00
ThreadLocalPtr<hecl::blender::Token> g_ThreadBlenderToken;
2016-04-10 04:49:02 +00:00
ThreadLocalPtr<hecl::Database::Project> UniqueIDBridge::s_Project;
UniqueID32 UniqueID32::kInvalidId;
2015-07-16 01:57:34 +00:00
template <class IDType>
2018-12-08 05:30:43 +00:00
hecl::ProjectPath UniqueIDBridge::TranslatePakIdToPath(const IDType& id, bool silenceWarnings) {
/* Try PAKRouter first (only available at extract) */
PAKRouterBase* pakRouter = g_PakRouter.get();
if (pakRouter) {
hecl::ProjectPath path = pakRouter->getWorking(id, silenceWarnings);
if (path)
return path;
}
2018-12-08 05:30:43 +00:00
/* Try project cache second (populated with paths read from YAML resources) */
hecl::Database::Project* project = s_Project.get();
if (!project) {
if (pakRouter) {
2019-07-20 04:27:21 +00:00
if (hecl::VerbosityLevel >= 1 && !silenceWarnings && id.isValid())
2020-04-11 22:51:39 +00:00
LogDNACommon.report(logvisor::Warning, FMT_STRING("unable to translate {} to path"), id);
2018-12-08 05:30:43 +00:00
return {};
}
2018-12-08 05:30:43 +00:00
LogDNACommon.report(logvisor::Fatal,
2020-04-11 22:51:39 +00:00
FMT_STRING("g_PakRouter or s_Project must be set to non-null before "
2019-07-20 04:27:21 +00:00
"calling UniqueIDBridge::TranslatePakIdToPath"));
2018-12-08 05:30:43 +00:00
return {};
}
2018-12-08 05:30:43 +00:00
const hecl::ProjectPath* search = project->lookupBridgePath(id.toUint64());
if (!search) {
2019-07-20 04:27:21 +00:00
if (hecl::VerbosityLevel >= 1 && !silenceWarnings && id.isValid())
2020-04-11 22:51:39 +00:00
LogDNACommon.report(logvisor::Warning, FMT_STRING("unable to translate {} to path"), id);
2018-12-08 05:30:43 +00:00
return {};
}
return *search;
}
template hecl::ProjectPath UniqueIDBridge::TranslatePakIdToPath(const UniqueID32& id, bool silenceWarnings);
template hecl::ProjectPath UniqueIDBridge::TranslatePakIdToPath(const UniqueID64& id, bool silenceWarnings);
template hecl::ProjectPath UniqueIDBridge::TranslatePakIdToPath(const UniqueID128& id, bool silenceWarnings);
template <class IDType>
2018-12-08 05:30:43 +00:00
hecl::ProjectPath UniqueIDBridge::MakePathFromString(std::string_view str) {
if (str.empty())
return {};
hecl::Database::Project* project = s_Project.get();
if (!project)
2020-04-11 22:51:39 +00:00
LogDNACommon.report(logvisor::Fatal, FMT_STRING("UniqueIDBridge::setGlobalProject must be called before MakePathFromString"));
2018-12-08 05:30:43 +00:00
hecl::ProjectPath path = hecl::ProjectPath(*project, str);
project->addBridgePathToCache(IDType(path).toUint64(), path);
return path;
}
template hecl::ProjectPath UniqueIDBridge::MakePathFromString<UniqueID32>(std::string_view str);
template hecl::ProjectPath UniqueIDBridge::MakePathFromString<UniqueID64>(std::string_view str);
2018-12-08 05:30:43 +00:00
void UniqueIDBridge::SetThreadProject(hecl::Database::Project& project) { s_Project.reset(&project); }
/** PAK 32-bit Unique ID */
2018-02-22 07:24:51 +00:00
template <>
2018-12-08 05:30:43 +00:00
void UniqueID32::Enumerate<BigDNA::Read>(typename Read::StreamT& reader) {
assign(reader.readUint32Big());
}
2018-02-22 07:24:51 +00:00
template <>
2018-12-08 05:30:43 +00:00
void UniqueID32::Enumerate<BigDNA::Write>(typename Write::StreamT& writer) {
writer.writeUint32Big(m_id);
}
2018-02-22 07:24:51 +00:00
template <>
2018-12-08 05:30:43 +00:00
void UniqueID32::Enumerate<BigDNA::ReadYaml>(typename ReadYaml::StreamT& reader) {
2019-10-01 07:38:03 +00:00
*this = UniqueIDBridge::MakePathFromString<UniqueID32>(reader.readString());
}
2018-02-22 07:24:51 +00:00
template <>
2018-12-08 05:30:43 +00:00
void UniqueID32::Enumerate<BigDNA::WriteYaml>(typename WriteYaml::StreamT& writer) {
2019-07-20 04:27:21 +00:00
if (!isValid())
2018-12-08 05:30:43 +00:00
return;
hecl::ProjectPath path = UniqueIDBridge::TranslatePakIdToPath(*this);
if (!path)
return;
2019-10-01 07:38:03 +00:00
writer.writeString(path.getEncodableStringUTF8());
}
2018-02-22 07:24:51 +00:00
template <>
2018-12-08 05:30:43 +00:00
void UniqueID32::Enumerate<BigDNA::BinarySize>(typename BinarySize::StreamT& s) {
s += 4;
}
2018-12-08 05:30:43 +00:00
std::string UniqueID32::toString() const {
2020-04-11 22:51:39 +00:00
return fmt::format(FMT_STRING("{}"), *this);
}
2018-04-02 04:27:24 +00:00
template <>
2018-12-08 05:30:43 +00:00
void UniqueID32Zero::Enumerate<BigDNA::Read>(typename Read::StreamT& reader) {
UniqueID32::Enumerate<BigDNA::Read>(reader);
}
2018-04-02 04:27:24 +00:00
template <>
2018-12-08 05:30:43 +00:00
void UniqueID32Zero::Enumerate<BigDNA::Write>(typename Write::StreamT& writer) {
2019-07-20 04:27:21 +00:00
writer.writeUint32Big(isValid() ? m_id : 0);
2018-12-08 05:30:43 +00:00
}
2018-04-02 04:27:24 +00:00
template <>
2018-12-08 05:30:43 +00:00
void UniqueID32Zero::Enumerate<BigDNA::ReadYaml>(typename ReadYaml::StreamT& reader) {
UniqueID32::Enumerate<BigDNA::ReadYaml>(reader);
}
2018-04-02 04:27:24 +00:00
template <>
2018-12-08 05:30:43 +00:00
void UniqueID32Zero::Enumerate<BigDNA::WriteYaml>(typename WriteYaml::StreamT& writer) {
UniqueID32::Enumerate<BigDNA::WriteYaml>(writer);
}
2018-04-02 04:27:24 +00:00
template <>
2018-12-08 05:30:43 +00:00
void UniqueID32Zero::Enumerate<BigDNA::BinarySize>(typename BinarySize::StreamT& s) {
UniqueID32::Enumerate<BigDNA::BinarySize>(s);
}
2018-04-02 04:27:24 +00:00
/** PAK 64-bit Unique ID */
2018-02-22 07:24:51 +00:00
template <>
2018-12-08 05:30:43 +00:00
void UniqueID64::Enumerate<BigDNA::Read>(typename Read::StreamT& reader) {
assign(reader.readUint64Big());
}
2018-02-22 07:24:51 +00:00
template <>
2018-12-08 05:30:43 +00:00
void UniqueID64::Enumerate<BigDNA::Write>(typename Write::StreamT& writer) {
writer.writeUint64Big(m_id);
}
2018-02-22 07:24:51 +00:00
template <>
2018-12-08 05:30:43 +00:00
void UniqueID64::Enumerate<BigDNA::ReadYaml>(typename ReadYaml::StreamT& reader) {
2019-10-01 07:38:03 +00:00
*this = UniqueIDBridge::MakePathFromString<UniqueID64>(reader.readString());
}
2018-02-22 07:24:51 +00:00
template <>
2018-12-08 05:30:43 +00:00
void UniqueID64::Enumerate<BigDNA::WriteYaml>(typename WriteYaml::StreamT& writer) {
2019-07-20 04:27:21 +00:00
if (!isValid())
2018-12-08 05:30:43 +00:00
return;
hecl::ProjectPath path = UniqueIDBridge::TranslatePakIdToPath(*this);
if (!path)
return;
2019-10-01 07:38:03 +00:00
writer.writeString(path.getEncodableStringUTF8());
}
2018-02-22 07:24:51 +00:00
template <>
2018-12-08 05:30:43 +00:00
void UniqueID64::Enumerate<BigDNA::BinarySize>(typename BinarySize::StreamT& s) {
s += 8;
}
2018-12-08 05:30:43 +00:00
std::string UniqueID64::toString() const {
2020-04-11 22:51:39 +00:00
return fmt::format(FMT_STRING("{}"), *this);
}
/** PAK 128-bit Unique ID */
2018-02-22 07:24:51 +00:00
template <>
2018-12-08 05:30:43 +00:00
void UniqueID128::Enumerate<BigDNA::Read>(typename Read::StreamT& reader) {
m_id.id[0] = reader.readUint64Big();
m_id.id[1] = reader.readUint64Big();
}
2018-02-22 07:24:51 +00:00
template <>
2018-12-08 05:30:43 +00:00
void UniqueID128::Enumerate<BigDNA::Write>(typename Write::StreamT& writer) {
writer.writeUint64Big(m_id.id[0]);
writer.writeUint64Big(m_id.id[1]);
}
2018-02-22 07:24:51 +00:00
template <>
2018-12-08 05:30:43 +00:00
void UniqueID128::Enumerate<BigDNA::ReadYaml>(typename ReadYaml::StreamT& reader) {
2019-10-01 07:38:03 +00:00
*this = UniqueIDBridge::MakePathFromString<UniqueID128>(reader.readString());
}
2018-02-22 07:24:51 +00:00
template <>
2018-12-08 05:30:43 +00:00
void UniqueID128::Enumerate<BigDNA::WriteYaml>(typename WriteYaml::StreamT& writer) {
2019-07-20 04:27:21 +00:00
if (!isValid())
2018-12-08 05:30:43 +00:00
return;
hecl::ProjectPath path = UniqueIDBridge::TranslatePakIdToPath(*this);
if (!path)
return;
2019-10-01 07:38:03 +00:00
writer.writeString(path.getEncodableStringUTF8());
}
2018-02-22 07:24:51 +00:00
template <>
2018-12-08 05:30:43 +00:00
void UniqueID128::Enumerate<BigDNA::BinarySize>(typename BinarySize::StreamT& s) {
s += 16;
}
2018-12-08 05:30:43 +00:00
std::string UniqueID128::toString() const {
2020-04-11 22:51:39 +00:00
return fmt::format(FMT_STRING("{}"), *this);
2018-12-08 05:30:43 +00:00
}
/** Word Bitmap reader/writer */
2018-12-08 05:30:43 +00:00
void WordBitmap::read(athena::io::IStreamReader& reader, size_t bitCount) {
m_bitCount = bitCount;
size_t wordCount = (bitCount + 31) / 32;
m_words.clear();
m_words.reserve(wordCount);
for (size_t w = 0; w < wordCount; ++w)
m_words.push_back(reader.readUint32Big());
}
2018-12-08 05:30:43 +00:00
void WordBitmap::write(athena::io::IStreamWriter& writer) const {
for (atUint32 word : m_words)
writer.writeUint32Big(word);
}
2018-12-08 05:30:43 +00:00
void WordBitmap::binarySize(size_t& __isz) const { __isz += m_words.size() * 4; }
2019-10-01 07:38:03 +00:00
hecl::ProjectPath GetPathBeginsWith(const hecl::DirectoryEnumerator& dEnum, const hecl::ProjectPath& parentPath,
hecl::SystemStringView test) {
for (const auto& ent : dEnum)
if (hecl::StringUtils::BeginsWith(ent.m_name, test))
return hecl::ProjectPath(parentPath, ent.m_name);
return {};
}
2018-12-08 05:30:43 +00:00
} // namespace DataSpec