2015-07-13 06:29:12 +00:00
|
|
|
#include "DNACommon.hpp"
|
2016-03-01 03:08:25 +00:00
|
|
|
#include "PAK.hpp"
|
2016-04-10 04:49:02 +00:00
|
|
|
#include "boo/ThreadLocalPtr.hpp"
|
2015-07-13 06:29:12 +00:00
|
|
|
|
2016-02-13 09:02:47 +00:00
|
|
|
namespace DataSpec
|
2015-07-13 06:29:12 +00:00
|
|
|
{
|
|
|
|
|
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;
|
2018-03-28 08:09:41 +00:00
|
|
|
ThreadLocalPtr<IDRestorer<UniqueID32>> UniqueIDBridge::s_restorer32;
|
|
|
|
ThreadLocalPtr<IDRestorer<UniqueID64>> UniqueIDBridge::s_restorer64;
|
|
|
|
ThreadLocalPtr<IDRestorer<UniqueID128>> UniqueIDBridge::s_restorer128;
|
2016-04-06 01:44:07 +00:00
|
|
|
UniqueID32 UniqueID32::kInvalidId;
|
2015-07-16 01:57:34 +00:00
|
|
|
|
2016-10-07 18:44:45 +00:00
|
|
|
template <class IDType>
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Try project cache second (populated with paths read from YAML resources) */
|
|
|
|
hecl::Database::Project* project = s_Project.get();
|
|
|
|
if (!project)
|
|
|
|
{
|
|
|
|
if (pakRouter)
|
|
|
|
{
|
2017-03-10 18:37:12 +00:00
|
|
|
if (hecl::VerbosityLevel >= 1 && !silenceWarnings && id)
|
2016-10-07 18:44:45 +00:00
|
|
|
LogDNACommon.report(logvisor::Warning,
|
|
|
|
"unable to translate %s to path", id.toString().c_str());
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
LogDNACommon.report(logvisor::Fatal,
|
|
|
|
"g_PakRouter or s_Project must be set to non-null before "
|
|
|
|
"calling UniqueIDBridge::TranslatePakIdToPath");
|
2018-03-28 08:09:41 +00:00
|
|
|
return {};
|
2016-10-07 18:44:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const hecl::ProjectPath* search = project->lookupBridgePath(id.toUint64());
|
|
|
|
if (!search)
|
|
|
|
{
|
2018-03-28 08:09:41 +00:00
|
|
|
if (IDRestorer<IDType>* restorer = GetIDRestorer<IDType>())
|
|
|
|
if (IDType newId = restorer->originalToNew(id))
|
|
|
|
if (const hecl::ProjectPath* newSearch = project->lookupBridgePath(newId.toUint64()))
|
|
|
|
return *newSearch;
|
2017-03-10 18:37:12 +00:00
|
|
|
if (hecl::VerbosityLevel >= 1 && !silenceWarnings && id)
|
2016-10-07 18:44:45 +00:00
|
|
|
LogDNACommon.report(logvisor::Warning,
|
|
|
|
"unable to translate %s to path", id.toString().c_str());
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
return *search;
|
|
|
|
}
|
|
|
|
template
|
|
|
|
hecl::ProjectPath UniqueIDBridge::TranslatePakIdToPath(const UniqueID32& id, bool silenceWarnings);
|
|
|
|
template
|
|
|
|
hecl::ProjectPath UniqueIDBridge::TranslatePakIdToPath(const UniqueID64& id, bool silenceWarnings);
|
2018-03-28 08:09:41 +00:00
|
|
|
template
|
|
|
|
hecl::ProjectPath UniqueIDBridge::TranslatePakIdToPath(const UniqueID128& id, bool silenceWarnings);
|
2016-10-07 18:44:45 +00:00
|
|
|
|
|
|
|
template <class IDType>
|
2017-11-13 06:19:18 +00:00
|
|
|
hecl::ProjectPath UniqueIDBridge::MakePathFromString(std::string_view str)
|
2016-10-07 18:44:45 +00:00
|
|
|
{
|
2016-10-08 03:41:08 +00:00
|
|
|
if (str.empty())
|
|
|
|
return {};
|
2016-10-07 18:44:45 +00:00
|
|
|
hecl::Database::Project* project = s_Project.get();
|
|
|
|
if (!project)
|
|
|
|
LogDNACommon.report(logvisor::Fatal,
|
|
|
|
"UniqueIDBridge::setGlobalProject must be called before MakePathFromString");
|
|
|
|
hecl::ProjectPath path = hecl::ProjectPath(*project, str);
|
2016-10-08 03:41:08 +00:00
|
|
|
project->addBridgePathToCache(IDType(path).toUint64(), path);
|
2016-10-07 18:44:45 +00:00
|
|
|
return path;
|
|
|
|
}
|
|
|
|
template
|
2017-11-13 06:19:18 +00:00
|
|
|
hecl::ProjectPath UniqueIDBridge::MakePathFromString<UniqueID32>(std::string_view str);
|
2016-10-07 18:44:45 +00:00
|
|
|
template
|
2017-11-13 06:19:18 +00:00
|
|
|
hecl::ProjectPath UniqueIDBridge::MakePathFromString<UniqueID64>(std::string_view str);
|
2016-10-07 18:44:45 +00:00
|
|
|
|
|
|
|
template <class IDType>
|
|
|
|
void UniqueIDBridge::TransformOldHashToNewHash(IDType& id)
|
|
|
|
{
|
|
|
|
id = TranslatePakIdToPath(id);
|
|
|
|
}
|
|
|
|
template
|
|
|
|
void UniqueIDBridge::TransformOldHashToNewHash(UniqueID32& id);
|
|
|
|
template
|
|
|
|
void UniqueIDBridge::TransformOldHashToNewHash(UniqueID64& id);
|
|
|
|
|
2018-03-28 08:09:41 +00:00
|
|
|
void UniqueIDBridge::SetThreadProject(hecl::Database::Project& project)
|
2016-10-07 18:44:45 +00:00
|
|
|
{
|
|
|
|
s_Project.reset(&project);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** PAK 32-bit Unique ID */
|
2018-03-28 08:09:41 +00:00
|
|
|
void UniqueID32::assign(uint32_t id, bool noOriginal)
|
|
|
|
{
|
|
|
|
m_id = id ? id : 0xffffffff;
|
|
|
|
if (!noOriginal)
|
|
|
|
if (IDRestorer<UniqueID32>* restorer = UniqueIDBridge::GetIDRestorer<UniqueID32>())
|
|
|
|
if (UniqueID32 origId = restorer->newToOriginal(*this))
|
|
|
|
*this = origId;
|
|
|
|
}
|
|
|
|
|
2018-02-22 07:24:51 +00:00
|
|
|
template <>
|
|
|
|
void UniqueID32::Enumerate<BigDNA::Read>(typename Read::StreamT& reader)
|
2017-03-20 05:09:53 +00:00
|
|
|
{assign(reader.readUint32Big());}
|
2018-02-22 07:24:51 +00:00
|
|
|
template <>
|
|
|
|
void UniqueID32::Enumerate<BigDNA::Write>(typename Write::StreamT& writer)
|
2016-10-07 18:44:45 +00:00
|
|
|
{writer.writeUint32Big(m_id);}
|
2018-02-22 07:24:51 +00:00
|
|
|
template <>
|
|
|
|
void UniqueID32::Enumerate<BigDNA::ReadYaml>(typename ReadYaml::StreamT& reader)
|
2016-10-07 18:44:45 +00:00
|
|
|
{
|
|
|
|
*this = UniqueIDBridge::MakePathFromString<UniqueID32>(reader.readString(nullptr));
|
|
|
|
}
|
2018-02-22 07:24:51 +00:00
|
|
|
template <>
|
|
|
|
void UniqueID32::Enumerate<BigDNA::WriteYaml>(typename WriteYaml::StreamT& writer)
|
2016-10-07 18:44:45 +00:00
|
|
|
{
|
|
|
|
if (!operator bool())
|
|
|
|
return;
|
|
|
|
hecl::ProjectPath path = UniqueIDBridge::TranslatePakIdToPath(*this);
|
|
|
|
if (!path)
|
|
|
|
return;
|
2016-10-08 03:41:08 +00:00
|
|
|
writer.writeString(nullptr, path.getAuxInfo().size() ?
|
2017-11-13 06:19:18 +00:00
|
|
|
(std::string(path.getRelativePathUTF8()) + '|' + path.getAuxInfoUTF8().data()) :
|
2016-10-08 03:41:08 +00:00
|
|
|
path.getRelativePathUTF8());
|
2016-10-07 18:44:45 +00:00
|
|
|
}
|
2018-02-22 07:24:51 +00:00
|
|
|
template <>
|
|
|
|
void UniqueID32::Enumerate<BigDNA::BinarySize>(typename BinarySize::StreamT& s)
|
|
|
|
{s += 4;}
|
2016-10-07 18:44:45 +00:00
|
|
|
|
|
|
|
std::string UniqueID32::toString() const
|
|
|
|
{
|
|
|
|
char buf[9];
|
|
|
|
snprintf(buf, 9, "%08X", m_id);
|
|
|
|
return std::string(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
AuxiliaryID32& AuxiliaryID32::operator=(const hecl::ProjectPath& path)
|
|
|
|
{
|
2017-03-20 05:09:53 +00:00
|
|
|
assign(path.ensureAuxInfo(m_auxStr).hash().val32());
|
2016-10-07 18:44:45 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
AuxiliaryID32& AuxiliaryID32::operator=(const UniqueID32& id)
|
|
|
|
{
|
|
|
|
m_baseId = id;
|
|
|
|
hecl::ProjectPath path = UniqueIDBridge::TranslatePakIdToPath(id);
|
|
|
|
if (path)
|
|
|
|
{
|
|
|
|
if (m_addExtension)
|
|
|
|
path = path.getWithExtension(m_addExtension);
|
|
|
|
*this = path;
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2018-02-22 07:24:51 +00:00
|
|
|
template <>
|
|
|
|
void AuxiliaryID32::Enumerate<BigDNA::Read>(typename Read::StreamT& reader)
|
2016-10-07 18:44:45 +00:00
|
|
|
{
|
2017-03-20 05:09:53 +00:00
|
|
|
assign(reader.readUint32Big());
|
2016-10-07 18:44:45 +00:00
|
|
|
m_baseId = *this;
|
|
|
|
}
|
2018-02-22 07:24:51 +00:00
|
|
|
template <>
|
|
|
|
void AuxiliaryID32::Enumerate<BigDNA::Write>(typename Write::StreamT& writer)
|
2016-10-07 18:44:45 +00:00
|
|
|
{
|
|
|
|
writer.writeUint32Big(m_id);
|
|
|
|
}
|
2018-02-22 07:24:51 +00:00
|
|
|
template <>
|
|
|
|
void AuxiliaryID32::Enumerate<BigDNA::ReadYaml>(typename ReadYaml::StreamT& reader)
|
2016-10-07 18:44:45 +00:00
|
|
|
{
|
|
|
|
hecl::ProjectPath readPath = UniqueIDBridge::MakePathFromString<UniqueID32>(reader.readString(nullptr));
|
|
|
|
*this = readPath.ensureAuxInfo(m_auxStr);
|
|
|
|
}
|
2018-02-22 07:24:51 +00:00
|
|
|
template <>
|
|
|
|
void AuxiliaryID32::Enumerate<BigDNA::WriteYaml>(typename WriteYaml::StreamT& writer)
|
2016-10-07 18:44:45 +00:00
|
|
|
{
|
|
|
|
if (!operator bool())
|
|
|
|
return;
|
2018-03-28 08:09:41 +00:00
|
|
|
hecl::ProjectPath path = UniqueIDBridge::TranslatePakIdToPath<UniqueID32>(*this, true);
|
2016-10-07 18:44:45 +00:00
|
|
|
if (!path)
|
|
|
|
path = UniqueIDBridge::TranslatePakIdToPath(m_baseId);
|
|
|
|
if (!path)
|
|
|
|
return;
|
|
|
|
if (m_addExtension)
|
|
|
|
path = path.getWithExtension(m_addExtension);
|
2017-11-13 06:19:18 +00:00
|
|
|
hecl::SystemUTF8Conv ufx8AuxStr(m_auxStr);
|
|
|
|
writer.writeString(nullptr, std::string(path.getRelativePathUTF8()) + '|' + ufx8AuxStr);
|
2016-10-07 18:44:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** PAK 64-bit Unique ID */
|
2018-03-28 08:09:41 +00:00
|
|
|
void UniqueID64::assign(uint64_t id, bool noOriginal)
|
|
|
|
{
|
|
|
|
m_id = id ? id : 0xffffffffffffffff;
|
|
|
|
if (!noOriginal)
|
|
|
|
if (IDRestorer<UniqueID64>* restorer = UniqueIDBridge::GetIDRestorer<UniqueID64>())
|
|
|
|
if (UniqueID64 origId = restorer->newToOriginal(*this))
|
|
|
|
*this = origId;
|
|
|
|
}
|
|
|
|
|
2018-02-22 07:24:51 +00:00
|
|
|
template <>
|
|
|
|
void UniqueID64::Enumerate<BigDNA::Read>(typename Read::StreamT& reader)
|
2017-03-20 05:09:53 +00:00
|
|
|
{assign(reader.readUint64Big());}
|
2018-02-22 07:24:51 +00:00
|
|
|
template <>
|
|
|
|
void UniqueID64::Enumerate<BigDNA::Write>(typename Write::StreamT& writer)
|
2016-10-07 18:44:45 +00:00
|
|
|
{writer.writeUint64Big(m_id);}
|
2018-02-22 07:24:51 +00:00
|
|
|
template <>
|
|
|
|
void UniqueID64::Enumerate<BigDNA::ReadYaml>(typename ReadYaml::StreamT& reader)
|
2016-10-07 18:44:45 +00:00
|
|
|
{
|
|
|
|
*this = UniqueIDBridge::MakePathFromString<UniqueID64>(reader.readString(nullptr));
|
|
|
|
}
|
2018-02-22 07:24:51 +00:00
|
|
|
template <>
|
|
|
|
void UniqueID64::Enumerate<BigDNA::WriteYaml>(typename WriteYaml::StreamT& writer)
|
2016-10-07 18:44:45 +00:00
|
|
|
{
|
|
|
|
if (!operator bool())
|
|
|
|
return;
|
|
|
|
hecl::ProjectPath path = UniqueIDBridge::TranslatePakIdToPath(*this);
|
|
|
|
if (!path)
|
|
|
|
return;
|
2016-10-08 03:41:08 +00:00
|
|
|
writer.writeString(nullptr, path.getAuxInfo().size() ?
|
2018-02-22 07:24:51 +00:00
|
|
|
(std::string(path.getRelativePathUTF8()) + '|' + path.getAuxInfoUTF8().data()) :
|
|
|
|
path.getRelativePathUTF8());
|
2016-10-07 18:44:45 +00:00
|
|
|
}
|
2018-02-22 07:24:51 +00:00
|
|
|
template <>
|
|
|
|
void UniqueID64::Enumerate<BigDNA::BinarySize>(typename BinarySize::StreamT& s)
|
|
|
|
{s += 8;}
|
2016-10-07 18:44:45 +00:00
|
|
|
|
|
|
|
std::string UniqueID64::toString() const
|
|
|
|
{
|
|
|
|
char buf[17];
|
|
|
|
snprintf(buf, 17, "%016" PRIX64, m_id);
|
|
|
|
return std::string(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** PAK 128-bit Unique ID */
|
2018-02-22 07:24:51 +00:00
|
|
|
template <>
|
|
|
|
void UniqueID128::Enumerate<BigDNA::Read>(typename Read::StreamT& reader)
|
2016-10-07 18:44:45 +00:00
|
|
|
{
|
2018-03-28 08:09:41 +00:00
|
|
|
m_id.id[0] = reader.readUint64Big();
|
|
|
|
m_id.id[1] = reader.readUint64Big();
|
2016-10-07 18:44:45 +00:00
|
|
|
}
|
2018-02-22 07:24:51 +00:00
|
|
|
template <>
|
|
|
|
void UniqueID128::Enumerate<BigDNA::Write>(typename Write::StreamT& writer)
|
2016-10-07 18:44:45 +00:00
|
|
|
{
|
2018-03-28 08:09:41 +00:00
|
|
|
writer.writeUint64Big(m_id.id[0]);
|
|
|
|
writer.writeUint64Big(m_id.id[1]);
|
2016-10-07 18:44:45 +00:00
|
|
|
}
|
2018-02-22 07:24:51 +00:00
|
|
|
template <>
|
|
|
|
void UniqueID128::Enumerate<BigDNA::ReadYaml>(typename ReadYaml::StreamT& reader)
|
2016-10-07 18:44:45 +00:00
|
|
|
{
|
|
|
|
*this = UniqueIDBridge::MakePathFromString<UniqueID128>(reader.readString(nullptr));
|
|
|
|
}
|
2018-02-22 07:24:51 +00:00
|
|
|
template <>
|
|
|
|
void UniqueID128::Enumerate<BigDNA::WriteYaml>(typename WriteYaml::StreamT& writer)
|
2016-10-07 18:44:45 +00:00
|
|
|
{
|
|
|
|
if (!operator bool())
|
|
|
|
return;
|
|
|
|
hecl::ProjectPath path = UniqueIDBridge::TranslatePakIdToPath(*this);
|
|
|
|
if (!path)
|
|
|
|
return;
|
2016-10-08 03:41:08 +00:00
|
|
|
writer.writeString(nullptr, path.getAuxInfo().size() ?
|
2018-02-22 07:24:51 +00:00
|
|
|
(std::string(path.getRelativePathUTF8()) + '|' + path.getAuxInfoUTF8().data()) :
|
|
|
|
path.getRelativePathUTF8());
|
2016-10-07 18:44:45 +00:00
|
|
|
}
|
2018-02-22 07:24:51 +00:00
|
|
|
template <>
|
|
|
|
void UniqueID128::Enumerate<BigDNA::BinarySize>(typename BinarySize::StreamT& s)
|
|
|
|
{s += 16;}
|
2016-10-07 18:44:45 +00:00
|
|
|
|
|
|
|
std::string UniqueID128::toString() const
|
|
|
|
{
|
|
|
|
char buf[33];
|
2018-03-28 08:09:41 +00:00
|
|
|
snprintf(buf, 33, "%016" PRIX64 "%016" PRIX64, m_id.id[0], m_id.id[1]);
|
2016-10-07 18:44:45 +00:00
|
|
|
return std::string(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** Word Bitmap reader/writer */
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
void WordBitmap::write(athena::io::IStreamWriter& writer) const
|
|
|
|
{
|
|
|
|
for (atUint32 word : m_words)
|
|
|
|
writer.writeUint32Big(word);
|
|
|
|
}
|
2018-03-04 06:15:40 +00:00
|
|
|
void WordBitmap::binarySize(size_t& __isz) const
|
2016-10-07 18:44:45 +00:00
|
|
|
{
|
2018-03-04 06:15:40 +00:00
|
|
|
__isz += m_words.size() * 4;
|
2016-10-07 18:44:45 +00:00
|
|
|
}
|
|
|
|
|
2015-07-13 06:29:12 +00:00
|
|
|
}
|