metaforce/DataSpec/SpecBase.cpp

1483 lines
53 KiB
C++
Raw Normal View History

2015-11-27 14:21:19 -08:00
#if _WIN32
#define _CRT_RAND_S
2017-12-29 00:08:12 -08:00
#include <cstdlib>
2015-11-27 14:21:19 -08:00
#endif
2015-07-01 16:50:39 -07:00
#include "SpecBase.hpp"
#include "Blender/BlenderSupport.hpp"
2015-11-09 18:07:15 -08:00
#include "DNACommon/DNACommon.hpp"
#include "DNACommon/TXTR.hpp"
2017-07-13 04:39:52 -07:00
#include "AssetNameMap.hpp"
#include "hecl/ClientProcess.hpp"
2017-12-29 00:08:12 -08:00
#include "nod/nod.hpp"
#include "hecl/Blender/Connection.hpp"
#include "hecl/Blender/SDNARead.hpp"
2018-03-23 14:56:17 -07:00
#include "hecl/MultiProgressPrinter.hpp"
2015-07-01 16:50:39 -07:00
2017-05-31 22:34:24 -07:00
#include <png.h>
2015-11-21 21:16:40 -08:00
#define DUMP_CACHE_FILL 1
2016-02-13 01:02:47 -08:00
namespace DataSpec
2015-07-01 16:50:39 -07:00
{
2016-03-04 15:04:53 -08:00
static logvisor::Module Log("urde::SpecBase");
2016-03-04 15:04:53 -08:00
static const hecl::SystemChar* MomErr[] =
2015-11-21 21:16:40 -08:00
{
2018-10-14 13:16:21 -07:00
_SYS_STR("Your metroid is in another castle"),
_SYS_STR("HECL is experiencing a PTSD attack"),
_SYS_STR("Unable to freeze metroids"),
_SYS_STR("Ridley ate your homework"),
_SYS_STR("Expected 0 maternal symbolisms, found 2147483647"),
_SYS_STR("Contradictive narratives unsupported"),
_SYS_STR("Wiimote profile \"NES + Zapper\" not recognized"),
_SYS_STR("Unable to find Waldo"),
_SYS_STR("Expected Ridley, found furby"),
_SYS_STR("Adam has not authorized this, please do not bug the developers"),
_SYS_STR("Lady returned objection"),
_SYS_STR("Unterminated plot thread 'Deleter' detected")
2015-11-21 21:16:40 -08:00
};
constexpr uint32_t MomErrCount = std::extent<decltype(MomErr)>::value;
SpecBase::SpecBase(const hecl::Database::DataSpecEntry* specEntry, hecl::Database::Project& project, bool pc)
: hecl::Database::IDataSpec(specEntry), m_project(project), m_pc(pc),
m_masterShader(project.getProjectWorkingPath(), ".hecl/RetroMasterShader.blend")
{
2017-07-13 04:39:52 -07:00
AssetNameMap::InitAssetNameMap();
SpecBase::setThreadProject();
}
2015-11-09 18:07:15 -08:00
SpecBase::~SpecBase()
{
cancelBackgroundIndex();
}
2018-10-14 13:16:21 -07:00
static const hecl::SystemString regNONE = _SYS_STR("");
static const hecl::SystemString regE = _SYS_STR("NTSC");
static const hecl::SystemString regJ = _SYS_STR("NTSC-J");
static const hecl::SystemString regP = _SYS_STR("PAL");
void SpecBase::setThreadProject()
{
UniqueIDBridge::SetThreadProject(m_project);
}
template <typename IDType>
IDRestorer<IDType>::IDRestorer(const hecl::ProjectPath& yamlPath, const hecl::Database::Project& project)
{
using ValType = typename IDType::value_type;
if (!yamlPath.isFile())
return;
athena::io::YAMLDocReader r;
athena::io::FileReader fr(yamlPath.getAbsolutePath());
if (!fr.isOpen() || !r.parse(&fr))
return;
m_newToOrig.reserve(r.getRootNode()->m_mapChildren.size());
m_origToNew.reserve(r.getRootNode()->m_mapChildren.size());
for (const auto& node : r.getRootNode()->m_mapChildren)
{
char* end = const_cast<char*>(node.first.c_str());
ValType id = strtoull(end, &end, 16);
if (end != node.first.c_str() + sizeof(ValType) * 2)
continue;
hecl::ProjectPath path(project.getProjectWorkingPath(), node.second->m_scalarString.c_str());
m_newToOrig.push_back(std::make_pair(IDType{path.hash().valT<ValType>(), true}, IDType{id, true}));
m_origToNew.push_back(std::make_pair(IDType{id, true}, IDType{path.hash().valT<ValType>(), true}));
}
std::sort(m_newToOrig.begin(), m_newToOrig.end(),
[](const std::pair<IDType, IDType>& a, const std::pair<IDType, IDType>& b) {
return a.first < b.first;
});
std::sort(m_origToNew.begin(), m_origToNew.end(),
[](const std::pair<IDType, IDType>& a, const std::pair<IDType, IDType>& b) {
return a.first < b.first;
});
2018-10-14 13:16:21 -07:00
Log.report(logvisor::Info, _SYS_STR("Loaded Original IDs '%s'"), yamlPath.getRelativePath().data());
}
template <typename IDType>
IDType IDRestorer<IDType>::newToOriginal(IDType id) const
{
if (!id)
return {};
auto search = rstl::binary_find(m_newToOrig.cbegin(), m_newToOrig.cend(), id,
[](const auto& id) { return id.first; });
if (search == m_newToOrig.cend())
return {};
return search->second;
}
template <typename IDType>
IDType IDRestorer<IDType>::originalToNew(IDType id) const
{
if (!id)
return {};
auto search = rstl::binary_find(m_origToNew.cbegin(), m_origToNew.cend(), id,
[](const auto& id) { return id.first; });
if (search == m_origToNew.cend())
return {};
return search->second;
}
template class IDRestorer<UniqueID32>;
template class IDRestorer<UniqueID64>;
template class IDRestorer<UniqueID128>;
2015-10-03 22:08:56 -07:00
bool SpecBase::canExtract(const ExtractPassInfo& info, std::vector<ExtractReport>& reps)
2015-07-01 16:50:39 -07:00
{
2017-11-12 22:19:18 -08:00
m_disc = nod::OpenDiscFromImage(info.srcpath, m_isWii);
2015-07-15 18:57:34 -07:00
if (!m_disc)
2015-07-02 13:41:40 -07:00
return false;
2016-01-19 19:18:30 -08:00
const char* gameID = m_disc->getHeader().m_gameID;
2015-07-02 13:41:40 -07:00
2015-11-21 21:16:40 -08:00
if (!memcmp(gameID, "R3O", 3))
{
2017-12-29 17:09:45 -08:00
std::srand(std::time(0));
int r = std::rand() % MomErrCount;
2016-03-04 15:04:53 -08:00
Log.report(logvisor::Fatal, MomErr[r]);
2015-11-21 21:16:40 -08:00
}
m_standalone = true;
if (m_isWii && (!memcmp(gameID, "R3M", 3) || !memcmp(gameID, "R3I", 3) || !memcmp(gameID, "R32", 3)))
m_standalone = false;
2015-07-01 16:50:39 -07:00
if (m_standalone && !checkStandaloneID(gameID))
2015-07-09 22:28:08 -07:00
return false;
2016-01-19 19:18:30 -08:00
char region = m_disc->getHeader().m_gameID[3];
2016-03-04 15:04:53 -08:00
const hecl::SystemString* regstr = &regNONE;
2015-07-12 11:07:58 -07:00
switch (region)
{
case 'E':
regstr = &regE;
break;
case 'J':
regstr = &regJ;
break;
case 'P':
regstr = &regP;
break;
}
if (m_standalone)
2015-08-05 14:46:07 -07:00
return checkFromStandaloneDisc(*m_disc, *regstr, info.extractArgs, reps);
2015-07-09 22:28:08 -07:00
else
2015-08-05 14:46:07 -07:00
return checkFromTrilogyDisc(*m_disc, *regstr, info.extractArgs, reps);
2015-07-01 16:50:39 -07:00
}
2018-03-23 14:56:17 -07:00
void SpecBase::doExtract(const ExtractPassInfo& info, const hecl::MultiProgressPrinter& progress)
2015-07-01 16:50:39 -07:00
{
setThreadProject();
2016-04-09 21:49:02 -07:00
DataSpec::g_curSpec.reset(this);
2015-08-05 14:46:07 -07:00
if (!Blender::BuildMasterShader(m_masterShader))
2016-03-04 15:04:53 -08:00
Log.report(logvisor::Fatal, "Unable to build master shader blend");
if (m_isWii)
{
/* Extract root files for repacking later */
2018-10-14 13:16:21 -07:00
hecl::ProjectPath outDir(m_project.getProjectWorkingPath(), _SYS_STR("out"));
outDir.makeDirChain(true);
2017-07-01 16:37:59 -07:00
nod::ExtractionContext ctx = {info.force, nullptr};
2015-09-28 20:49:31 -07:00
if (!m_standalone)
{
2018-10-14 13:16:21 -07:00
progress.print(_SYS_STR("Trilogy Files"), _SYS_STR(""), 0.0);
2017-12-29 00:08:12 -08:00
nod::IPartition* data = m_disc->getDataPartition();
2016-03-04 15:04:53 -08:00
const nod::Node& root = data->getFSTRoot();
for (const nod::Node& child : root)
if (child.getKind() == nod::Node::Kind::File)
child.extractToDirectory(outDir.getAbsolutePath(), ctx);
2018-10-14 13:16:21 -07:00
progress.print(_SYS_STR("Trilogy Files"), _SYS_STR(""), 1.0);
}
}
2015-08-05 14:46:07 -07:00
extractFromDisc(*m_disc, info.force, progress);
2015-07-01 16:50:39 -07:00
}
2016-09-18 16:47:48 -07:00
static bool IsPathAudioGroup(const hecl::ProjectPath& path)
{
return (path.getPathType() == hecl::ProjectPath::Type::Directory &&
2018-10-14 13:16:21 -07:00
hecl::ProjectPath(path, _SYS_STR("!project.yaml")).isFile() &&
hecl::ProjectPath(path, _SYS_STR("!pool.yaml")).isFile());
2016-09-18 16:47:48 -07:00
}
static bool IsPathSong(const hecl::ProjectPath& path)
{
if (path.getPathType() != hecl::ProjectPath::Type::Glob ||
2018-10-14 13:16:21 -07:00
!path.getWithExtension(_SYS_STR(".mid"), true).isFile() ||
!path.getWithExtension(_SYS_STR(".yaml"), true).isFile())
{
if (path.isFile() &&
2018-10-14 13:16:21 -07:00
!hecl::StrCmp(_SYS_STR("mid"), path.getLastComponentExt().data()) &&
path.getWithExtension(_SYS_STR(".yaml"), true).isFile())
return true;
2016-09-18 16:47:48 -07:00
return false;
}
2016-09-18 16:47:48 -07:00
return true;
}
2018-04-01 21:27:24 -07:00
bool SpecBase::canCook(const hecl::ProjectPath& path, hecl::blender::Token& btok, int cookPass)
2015-07-01 16:50:39 -07:00
{
2015-10-03 22:08:56 -07:00
if (!checkPathPrefix(path))
return false;
2016-09-23 11:56:42 -07:00
hecl::ProjectPath asBlend;
if (path.getPathType() == hecl::ProjectPath::Type::Glob)
2018-10-14 13:16:21 -07:00
asBlend = path.getWithExtension(_SYS_STR(".blend"), true);
2016-09-23 11:56:42 -07:00
else
asBlend = path;
if (hecl::IsPathBlend(asBlend))
2015-09-30 17:40:21 -07:00
{
hecl::blender::BlendType type = hecl::blender::GetBlendType(asBlend.getAbsolutePath());
2018-04-01 21:27:24 -07:00
if (type != hecl::blender::BlendType::None)
return cookPass < 0 ||
(cookPass == 0 && type == hecl::blender::BlendType::Mesh) || // CMDL only
(cookPass == 1 && type != hecl::blender::BlendType::Mesh); // Non-CMDL only
return false;
2015-09-30 17:40:21 -07:00
}
2018-04-01 21:27:24 -07:00
/* Non-CMDLs shall not pass */
if (cookPass == 0)
return false;
if (hecl::IsPathPNG(path))
2015-09-30 17:40:21 -07:00
{
return true;
}
2016-03-04 15:04:53 -08:00
else if (hecl::IsPathYAML(path))
2015-09-30 17:40:21 -07:00
{
2016-08-21 20:47:48 -07:00
athena::io::FileReader reader(path.getAbsolutePath());
bool retval = validateYAMLDNAType(reader);
2015-09-30 17:40:21 -07:00
return retval;
}
2016-09-18 16:47:48 -07:00
else if (IsPathAudioGroup(path))
{
return true;
}
else if (IsPathSong(path))
{
return true;
}
2015-07-22 12:05:18 -07:00
return false;
2015-07-01 16:50:39 -07:00
}
const hecl::Database::DataSpecEntry* SpecBase::overrideDataSpec(const hecl::ProjectPath& path,
const hecl::Database::DataSpecEntry* oldEntry,
2017-12-29 00:08:12 -08:00
hecl::blender::Token& btok) const
{
if (!checkPathPrefix(path))
return nullptr;
2016-09-23 11:56:42 -07:00
hecl::ProjectPath asBlend;
if (path.getPathType() == hecl::ProjectPath::Type::Glob)
2018-10-14 13:16:21 -07:00
asBlend = path.getWithExtension(_SYS_STR(".blend"), true);
2016-09-23 11:56:42 -07:00
else
asBlend = path;
if (hecl::IsPathBlend(asBlend))
{
2018-10-14 13:16:21 -07:00
if (hecl::StringUtils::EndsWith(path.getAuxInfo(), _SYS_STR(".CSKR")) ||
hecl::StringUtils::EndsWith(path.getAuxInfo(), _SYS_STR(".ANIM")))
2016-08-31 16:31:12 -07:00
return oldEntry;
hecl::blender::BlendType type = hecl::blender::GetBlendType(asBlend.getAbsolutePath());
if (type == hecl::blender::BlendType::None)
{
2018-10-14 13:16:21 -07:00
Log.report(logvisor::Error, _SYS_STR("unable to cook '%s'"),
2017-11-12 22:19:18 -08:00
path.getAbsolutePath().data());
return nullptr;
}
2017-12-29 00:08:12 -08:00
if (type == hecl::blender::BlendType::Mesh ||
type == hecl::blender::BlendType::Area)
return oldEntry;
}
else if (hecl::IsPathPNG(path))
{
return oldEntry;
}
return &getOriginalSpec();
}
2016-03-04 15:04:53 -08:00
void SpecBase::doCook(const hecl::ProjectPath& path, const hecl::ProjectPath& cookedPath,
2017-12-29 00:08:12 -08:00
bool fast, hecl::blender::Token& btok, FCookProgress progress)
2015-07-01 16:50:39 -07:00
{
2016-12-24 17:36:42 -08:00
cookedPath.makeDirChain(false);
2016-04-09 21:49:02 -07:00
DataSpec::g_curSpec.reset(this);
2016-09-23 11:56:42 -07:00
hecl::ProjectPath asBlend;
if (path.getPathType() == hecl::ProjectPath::Type::Glob)
2018-10-14 13:16:21 -07:00
asBlend = path.getWithExtension(_SYS_STR(".blend"), true);
2016-09-23 11:56:42 -07:00
else
asBlend = path;
if (hecl::IsPathBlend(asBlend))
2015-10-03 22:08:56 -07:00
{
2017-12-29 00:08:12 -08:00
hecl::blender::Connection& conn = btok.getBlenderConnection();
2016-09-23 11:56:42 -07:00
if (!conn.openBlend(asBlend))
2015-10-03 22:08:56 -07:00
return;
2015-10-06 18:17:17 -07:00
switch (conn.getBlendType())
{
2017-12-29 00:08:12 -08:00
case hecl::blender::BlendType::Mesh:
2015-10-03 22:08:56 -07:00
{
2017-12-29 00:08:12 -08:00
hecl::blender::DataStream ds = conn.beginData();
2016-04-09 21:49:02 -07:00
cookMesh(cookedPath, path, ds, fast, btok, progress);
2015-10-06 18:17:17 -07:00
break;
2015-10-03 22:08:56 -07:00
}
2017-12-29 00:08:12 -08:00
case hecl::blender::BlendType::ColMesh:
2017-10-16 22:51:53 -07:00
{
2017-12-29 00:08:12 -08:00
hecl::blender::DataStream ds = conn.beginData();
2017-10-16 22:51:53 -07:00
cookColMesh(cookedPath, path, ds, fast, btok, progress);
break;
}
2018-02-25 00:23:27 -08:00
case hecl::blender::BlendType::PathMesh:
{
hecl::blender::DataStream ds = conn.beginData();
cookPathMesh(cookedPath, path, ds, fast, btok, progress);
break;
}
2017-12-29 00:08:12 -08:00
case hecl::blender::BlendType::Actor:
2015-10-06 18:17:17 -07:00
{
2017-12-29 00:08:12 -08:00
hecl::blender::DataStream ds = conn.beginData();
2016-04-09 21:49:02 -07:00
cookActor(cookedPath, path, ds, fast, btok, progress);
2015-10-06 18:17:17 -07:00
break;
}
2017-12-29 00:08:12 -08:00
case hecl::blender::BlendType::Area:
2015-10-06 18:17:17 -07:00
{
2017-12-29 00:08:12 -08:00
hecl::blender::DataStream ds = conn.beginData();
2016-04-09 21:49:02 -07:00
cookArea(cookedPath, path, ds, fast, btok, progress);
2015-10-06 18:17:17 -07:00
break;
}
2017-12-29 00:08:12 -08:00
case hecl::blender::BlendType::World:
{
2017-12-29 00:08:12 -08:00
hecl::blender::DataStream ds = conn.beginData();
cookWorld(cookedPath, path, ds, fast, btok, progress);
break;
}
2017-12-29 00:08:12 -08:00
case hecl::blender::BlendType::Frame:
{
2017-12-29 00:08:12 -08:00
hecl::blender::DataStream ds = conn.beginData();
cookGuiFrame(cookedPath, path, ds, btok, progress);
break;
}
2017-12-29 00:08:12 -08:00
case hecl::blender::BlendType::MapArea:
2017-03-19 22:09:53 -07:00
{
2017-12-29 00:08:12 -08:00
hecl::blender::DataStream ds = conn.beginData();
2017-03-19 22:09:53 -07:00
cookMapArea(cookedPath, path, ds, btok, progress);
break;
}
2017-12-29 00:08:12 -08:00
case hecl::blender::BlendType::MapUniverse:
2017-03-19 22:09:53 -07:00
{
2017-12-29 00:08:12 -08:00
hecl::blender::DataStream ds = conn.beginData();
2017-03-19 22:09:53 -07:00
cookMapUniverse(cookedPath, path, ds, btok, progress);
break;
}
2015-10-06 18:17:17 -07:00
default: break;
}
}
else if (hecl::IsPathPNG(path))
{
if (m_pc)
TXTR::CookPC(path, cookedPath);
else
TXTR::Cook(path, cookedPath);
}
2016-03-04 15:04:53 -08:00
else if (hecl::IsPathYAML(path))
2015-10-06 18:17:17 -07:00
{
2016-08-21 20:47:48 -07:00
athena::io::FileReader reader(path.getAbsolutePath());
cookYAML(cookedPath, path, reader, progress);
2015-10-03 22:08:56 -07:00
}
2016-09-18 16:47:48 -07:00
else if (IsPathAudioGroup(path))
{
cookAudioGroup(cookedPath, path, progress);
}
else if (IsPathSong(path))
{
cookSong(cookedPath, path, progress);
}
2015-07-01 16:50:39 -07:00
}
void SpecBase::flattenDependenciesBlend(const hecl::ProjectPath& in,
std::vector<hecl::ProjectPath>& pathsOut,
hecl::blender::Token& btok,
int charIdx)
{
hecl::blender::Connection& conn = btok.getBlenderConnection();
if (!conn.openBlend(in))
return;
switch (conn.getBlendType())
{
case hecl::blender::BlendType::Mesh:
{
hecl::blender::DataStream ds = conn.beginData();
std::vector<hecl::ProjectPath> texs = ds.getTextures();
for (const hecl::ProjectPath& tex : texs)
pathsOut.push_back(tex);
break;
}
case hecl::blender::BlendType::Actor:
{
2018-10-14 13:16:21 -07:00
hecl::ProjectPath asGlob = in.getWithExtension(_SYS_STR(".*"), true);
hecl::blender::DataStream ds = conn.beginData();
hecl::blender::Actor actor = ds.compileActorCharacterOnly();
2018-04-01 21:27:24 -07:00
auto actNames = ds.getActionNames();
ds.close();
auto doSubtype = [&](Actor::Subtype& sub)
{
if (sub.armature >= 0)
{
2018-04-01 21:27:24 -07:00
if (hecl::IsPathBlend(sub.mesh))
{
flattenDependenciesBlend(sub.mesh, pathsOut, btok);
pathsOut.push_back(sub.mesh);
}
hecl::SystemStringConv chSysName(sub.name);
2018-10-14 13:16:21 -07:00
pathsOut.push_back(asGlob.ensureAuxInfo(hecl::SystemString(chSysName.sys_str()) + _SYS_STR(".CSKR")));
const auto& arm = actor.armatures[sub.armature];
hecl::SystemStringConv armSysName(arm.name);
2018-10-14 13:16:21 -07:00
pathsOut.push_back(asGlob.ensureAuxInfo(hecl::SystemString(armSysName.sys_str()) + _SYS_STR(".CINF")));
for (const auto& overlay : sub.overlayMeshes)
{
hecl::SystemStringConv ovelaySys(overlay.first);
2018-04-01 21:27:24 -07:00
if (hecl::IsPathBlend(overlay.second))
{
flattenDependenciesBlend(overlay.second, pathsOut, btok);
pathsOut.push_back(overlay.second);
}
2018-10-14 13:16:21 -07:00
pathsOut.push_back(asGlob.ensureAuxInfo(hecl::SystemString(chSysName.sys_str()) + _SYS_STR('.') +
ovelaySys.c_str() + _SYS_STR(".CSKR")));
}
}
};
if (charIdx < 0)
for (auto& sub : actor.subtypes)
doSubtype(sub);
else if (charIdx < actor.subtypes.size())
doSubtype(actor.subtypes[charIdx]);
for (const Actor::Attachment& att : actor.attachments)
{
if (hecl::IsPathBlend(att.mesh))
{
flattenDependenciesBlend(att.mesh, pathsOut, btok);
pathsOut.push_back(att.mesh);
}
hecl::SystemStringConv chSysName(att.name);
2018-10-14 13:16:21 -07:00
pathsOut.push_back(asGlob.ensureAuxInfo(hecl::SystemString(_SYS_STR("ATTACH.")) +
chSysName.c_str() + _SYS_STR(".CSKR")));
if (att.armature >= 0)
{
const auto& arm = actor.armatures[att.armature];
hecl::SystemStringConv armSysName(arm.name);
2018-10-14 13:16:21 -07:00
pathsOut.push_back(asGlob.ensureAuxInfo(hecl::SystemString(armSysName.sys_str()) + _SYS_STR(".CINF")));
}
}
for (const auto& act : actNames)
{
hecl::SystemStringConv actSysName(act);
2018-10-14 13:16:21 -07:00
pathsOut.push_back(asGlob.ensureAuxInfo(hecl::SystemString(actSysName.sys_str()) + _SYS_STR(".ANIM")));
hecl::ProjectPath evntPath = asGlob.ensureAuxInfo(hecl::SystemStringView{}).getWithExtension(
hecl::SysFormat(_SYS_STR(".%s.evnt.yaml"), actSysName.c_str()).c_str(), true);
if (evntPath.isFile())
pathsOut.push_back(evntPath);
}
2018-10-14 13:16:21 -07:00
hecl::ProjectPath yamlPath = asGlob.getWithExtension(_SYS_STR(".yaml"), true);
if (yamlPath.isFile())
{
athena::io::FileReader reader(yamlPath.getAbsolutePath());
flattenDependenciesANCSYAML(reader, pathsOut, charIdx);
}
pathsOut.push_back(asGlob);
return;
}
case hecl::blender::BlendType::Area:
{
hecl::blender::DataStream ds = conn.beginData();
std::vector<hecl::ProjectPath> texs = ds.getTextures();
for (const hecl::ProjectPath& tex : texs)
pathsOut.push_back(tex);
break;
}
default: break;
}
}
2016-10-02 15:41:36 -07:00
void SpecBase::flattenDependencies(const hecl::ProjectPath& path,
std::vector<hecl::ProjectPath>& pathsOut,
hecl::blender::Token& btok,
int charIdx)
2015-07-01 16:50:39 -07:00
{
2016-10-02 15:41:36 -07:00
DataSpec::g_curSpec.reset(this);
2017-10-28 00:08:48 -07:00
g_ThreadBlenderToken.reset(&btok);
2016-10-02 15:41:36 -07:00
hecl::ProjectPath asBlend;
if (path.getPathType() == hecl::ProjectPath::Type::Glob)
2018-10-14 13:16:21 -07:00
asBlend = path.getWithExtension(_SYS_STR(".blend"), true);
2016-10-02 15:41:36 -07:00
else
asBlend = path;
if (hecl::IsPathBlend(asBlend))
{
flattenDependenciesBlend(asBlend, pathsOut, btok, charIdx);
2016-10-02 15:41:36 -07:00
}
else if (hecl::IsPathYAML(path))
{
athena::io::FileReader reader(path.getAbsolutePath());
flattenDependenciesYAML(reader, pathsOut);
}
pathsOut.push_back(path);
}
void SpecBase::flattenDependencies(const UniqueID32& id, std::vector<hecl::ProjectPath>& pathsOut, int charIdx)
2016-10-02 15:41:36 -07:00
{
hecl::ProjectPath path = UniqueIDBridge::TranslatePakIdToPath(id);
if (path)
flattenDependencies(path, pathsOut, *g_ThreadBlenderToken.get(), charIdx);
2016-10-02 15:41:36 -07:00
}
void SpecBase::flattenDependencies(const UniqueID64& id, std::vector<hecl::ProjectPath>& pathsOut, int charIdx)
2016-10-02 15:41:36 -07:00
{
hecl::ProjectPath path = UniqueIDBridge::TranslatePakIdToPath(id);
if (path)
flattenDependencies(path, pathsOut, *g_ThreadBlenderToken.get(), charIdx);
2015-07-01 16:50:39 -07:00
}
bool SpecBase::canPackage(const hecl::ProjectPath& path)
2015-07-01 16:50:39 -07:00
{
auto components = path.getPathComponents();
if (components.size() <= 1)
return false;
2017-10-27 03:10:32 -07:00
return path.isFile() || path.isDirectory();
2015-07-01 16:50:39 -07:00
}
void SpecBase::recursiveBuildResourceList(std::vector<urde::SObjectTag>& listOut,
2017-10-25 22:37:46 -07:00
std::unordered_set<urde::SObjectTag>& addedTags,
const hecl::ProjectPath& path,
2017-12-29 00:08:12 -08:00
hecl::blender::Token& btok)
2015-07-01 16:50:39 -07:00
{
hecl::DirectoryEnumerator dEnum(path.getAbsolutePath(),
hecl::DirectoryEnumerator::Mode::DirsThenFilesSorted, false, false, true);
for (const auto& ent : dEnum)
{
hecl::ProjectPath childPath(path, ent.m_name);
if (ent.m_isDir)
2017-10-25 22:37:46 -07:00
{
2018-10-14 13:16:21 -07:00
if (hecl::ProjectPath(childPath, _SYS_STR("!project.yaml")).isFile() &&
hecl::ProjectPath(childPath, _SYS_STR("!pool.yaml")).isFile())
{
/* Handle AudioGroup case */
if (urde::SObjectTag tag = tagFromPath(childPath, btok))
{
if (addedTags.find(tag) != addedTags.end())
continue;
addedTags.insert(tag);
listOut.push_back(tag);
}
continue;
}
2017-10-25 22:37:46 -07:00
recursiveBuildResourceList(listOut, addedTags, childPath, btok);
}
else
{
std::vector<hecl::ProjectPath> subPaths;
flattenDependencies(childPath, subPaths, btok);
for (const auto& subPath : subPaths)
{
if (urde::SObjectTag tag = tagFromPath(subPath, btok))
{
if (addedTags.find(tag) != addedTags.end())
continue;
addedTags.insert(tag);
listOut.push_back(tag);
}
}
}
}
}
void SpecBase::copyBuildListData(std::vector<std::tuple<size_t, size_t, bool>>& fileIndex,
const std::vector<urde::SObjectTag>& buildList,
const hecl::Database::DataSpecEntry* entry,
2018-03-23 14:56:17 -07:00
bool fast, const hecl::MultiProgressPrinter& progress,
2018-04-01 21:27:24 -07:00
athena::io::FileWriter& pakOut,
const std::unordered_map<urde::CAssetId, std::vector<uint8_t>>& mlvlData)
{
fileIndex.reserve(buildList.size());
2018-03-23 14:56:17 -07:00
int loadIdx = 0;
for (const auto& tag : buildList)
{
2018-10-14 13:16:21 -07:00
hecl::SystemString str = hecl::SysFormat(_SYS_STR("Copying %.4") FMT_CSTR_SYS " %08X",
2018-03-23 14:56:17 -07:00
tag.type.getChars(), (unsigned int)tag.id.Value());
progress.print(str.c_str(), nullptr, ++loadIdx / float(buildList.size()));
fileIndex.emplace_back();
auto& thisIdx = fileIndex.back();
2018-04-01 21:27:24 -07:00
if (tag.type == FOURCC('MLVL'))
{
auto search = mlvlData.find(tag.id);
if (search == mlvlData.end())
2018-10-14 13:16:21 -07:00
Log.report(logvisor::Fatal, _SYS_STR("Unable to find MLVL %08X"), tag.id.Value());
2018-04-01 21:27:24 -07:00
std::get<0>(thisIdx) = pakOut.position();
std::get<1>(thisIdx) = ROUND_UP_32(search->second.size());
std::get<2>(thisIdx) = false;
pakOut.writeUBytes(search->second.data(), search->second.size());
for (atUint64 i = search->second.size() ; i < std::get<1>(thisIdx) ; ++i)
pakOut.writeUByte(0xff);
continue;
}
hecl::ProjectPath path = pathFromTag(tag);
hecl::ProjectPath cooked = getCookedPath(path, true);
athena::io::FileReader r(cooked.getAbsolutePath());
if (r.hasError())
2018-10-14 13:16:21 -07:00
Log.report(logvisor::Fatal, _SYS_STR("Unable to open resource %s"), cooked.getRelativePath().data());
atUint64 size = r.length();
auto data = r.readUBytes(size);
auto compData = compressPakData(tag, data.get(), size);
if (compData.first)
{
std::get<0>(thisIdx) = pakOut.position();
std::get<1>(thisIdx) = ROUND_UP_32(compData.second + 4);
std::get<2>(thisIdx) = true;
pakOut.writeUint32Big(atUint32(size));
pakOut.writeUBytes(compData.first.get(), compData.second);
for (atUint64 i = compData.second + 4 ; i < std::get<1>(thisIdx) ; ++i)
pakOut.writeUByte(0xff);
}
else
{
std::get<0>(thisIdx) = pakOut.position();
std::get<1>(thisIdx) = ROUND_UP_32(size);
std::get<2>(thisIdx) = false;
pakOut.writeUBytes(data.get(), size);
for (atUint64 i = size ; i < std::get<1>(thisIdx) ; ++i)
pakOut.writeUByte(0xff);
}
}
2018-03-23 14:56:17 -07:00
progress.startNewLine();
}
void SpecBase::doPackage(const hecl::ProjectPath& path, const hecl::Database::DataSpecEntry* entry,
2018-03-23 14:56:17 -07:00
bool fast, hecl::blender::Token& btok, const hecl::MultiProgressPrinter& progress,
hecl::ClientProcess* cp)
{
/* Prepare complete resource index */
if (!m_backgroundRunning && m_tagToPath.empty())
beginBackgroundIndex();
waitForIndexComplete();
/* Name pak based on root-relative components */
2018-10-14 13:16:21 -07:00
auto components = path.getWithExtension(_SYS_STR(""), true).getPathComponents();
if (components.size() <= 1)
return;
hecl::ProjectPath outPath;
2018-10-14 13:16:21 -07:00
if (hecl::ProjectPath(m_project.getProjectWorkingPath(), _SYS_STR("out/files/") + components[0]).isDirectory())
outPath.assign(m_project.getProjectWorkingPath(),
2018-10-14 13:16:21 -07:00
_SYS_STR("out/files/") + components[0] + _SYS_STR("/") + components[1] + entry->m_pakExt.data());
else
outPath.assign(m_project.getProjectWorkingPath(),
2018-10-14 13:16:21 -07:00
_SYS_STR("out/files/") + components[1] + entry->m_pakExt.data());
outPath.makeDirChain(false);
/* Output file */
2017-11-25 19:04:25 -08:00
athena::io::FileWriter pakOut(outPath.getAbsolutePath());
std::vector<urde::SObjectTag> buildList;
atUint64 resTableOffset = 0;
2018-04-01 21:27:24 -07:00
std::unordered_map<urde::CAssetId, std::vector<uint8_t>> mlvlData;
if (path.getPathType() == hecl::ProjectPath::Type::File &&
2018-10-14 13:16:21 -07:00
!hecl::StrCmp(path.getLastComponent().data(), _SYS_STR("!world.blend"))) /* World PAK */
{
/* Force-cook MLVL and write resource list structure */
2018-03-23 14:56:17 -07:00
m_project.cookPath(path, progress, false, true, fast, entry, cp);
if (cp)
cp->waitUntilComplete();
progress.startNewLine();
hecl::ProjectPath cooked = getCookedPath(path, true);
2018-04-01 21:27:24 -07:00
buildWorldPakList(path, cooked, btok, pakOut, buildList, resTableOffset, mlvlData);
if (int64_t rem = pakOut.position() % 32)
for (int64_t i=0 ; i<32-rem ; ++i)
pakOut.writeUByte(0xff);
}
else if (path.getPathType() == hecl::ProjectPath::Type::Directory) /* General PAK */
{
/* Build resource list */
2017-10-25 22:37:46 -07:00
std::unordered_set<urde::SObjectTag> addedTags;
recursiveBuildResourceList(buildList, addedTags, path, btok);
std::vector<std::pair<urde::SObjectTag, std::string>> nameList;
/* Build name list */
for (const auto& item : buildList)
{
2018-04-01 21:27:24 -07:00
auto search = m_catalogTagToNames.find(item);
if (search != m_catalogTagToNames.end())
for (const auto& name : search->second)
nameList.emplace_back(item, name);
2017-10-27 03:10:32 -07:00
}
/* Write resource list structure */
buildPakList(btok, pakOut, buildList, nameList, resTableOffset);
if (int64_t rem = pakOut.position() % 32)
for (int64_t i=0 ; i<32-rem ; ++i)
pakOut.writeUByte(0xff);
}
else if (path.getPathType() == hecl::ProjectPath::Type::File) /* One-file General PAK */
{
/* Build resource list */
std::vector<hecl::ProjectPath> subPaths;
flattenDependencies(path, subPaths, btok);
std::unordered_set<urde::SObjectTag> addedTags;
std::vector<std::pair<urde::SObjectTag, std::string>> nameList;
for (const auto& subPath : subPaths)
{
if (urde::SObjectTag tag = tagFromPath(subPath, btok))
{
if (addedTags.find(tag) != addedTags.end())
continue;
addedTags.insert(tag);
buildList.push_back(tag);
}
}
/* Build name list */
for (const auto& item : buildList)
{
2018-04-01 21:27:24 -07:00
auto search = m_catalogTagToNames.find(item);
if (search != m_catalogTagToNames.end())
for (const auto& name : search->second)
nameList.emplace_back(item, name);
}
/* Write resource list structure */
buildPakList(btok, pakOut, buildList, nameList, resTableOffset);
if (int64_t rem = pakOut.position() % 32)
for (int64_t i=0 ; i<32-rem ; ++i)
pakOut.writeUByte(0xff);
}
/* Async cook resource list if using ClientProcess */
if (cp)
{
2018-10-14 13:16:21 -07:00
Log.report(logvisor::Info, _SYS_STR("Validating resources"));
2018-03-23 14:56:17 -07:00
progress.setMainIndeterminate(true);
2018-04-01 21:27:24 -07:00
for (int i=0 ; i<entry->m_numCookPasses ; ++i)
{
2018-04-01 21:27:24 -07:00
std::unordered_set<urde::SObjectTag> addedTags;
addedTags.reserve(buildList.size());
for (auto& tag : buildList)
{
2018-04-01 21:27:24 -07:00
if (addedTags.find(tag) != addedTags.end())
continue;
addedTags.insert(tag);
hecl::ProjectPath depPath = pathFromTag(tag);
if (!depPath)
{
2018-10-14 13:16:21 -07:00
Log.report(logvisor::Fatal, _SYS_STR("Unable to resolve %.4s %08X"),
2018-04-01 21:27:24 -07:00
tag.type.getChars(), tag.id.Value());
}
m_project.cookPath(depPath, progress, false, false, fast, entry, cp, i);
}
}
2018-03-23 14:56:17 -07:00
progress.setMainIndeterminate(false);
cp->waitUntilComplete();
2018-03-23 14:56:17 -07:00
progress.startNewLine();
}
/* Write resource data and build file index */
std::vector<std::tuple<size_t, size_t, bool>> fileIndex;
2018-10-14 13:16:21 -07:00
Log.report(logvisor::Info, _SYS_STR("Copying data into %s"), outPath.getRelativePath().data());
2018-04-01 21:27:24 -07:00
copyBuildListData(fileIndex, buildList, entry, fast, progress, pakOut, mlvlData);
/* Write file index */
writePakFileIndex(pakOut, buildList, fileIndex, resTableOffset);
pakOut.close();
}
2018-05-25 20:07:29 -07:00
void SpecBase::interruptCook()
{
cancelBackgroundIndex();
}
hecl::ProjectPath SpecBase::getCookedPath(const hecl::ProjectPath& working, bool pcTarget) const
{
const hecl::Database::DataSpecEntry* spec = &getOriginalSpec();
if (pcTarget)
2017-12-29 00:08:12 -08:00
spec = overrideDataSpec(working, getDataSpecEntry(), hecl::blender::SharedBlenderToken);
if (!spec)
return {};
return working.getCookedPath(*spec);
2015-07-01 16:50:39 -07:00
}
2017-05-31 22:34:24 -07:00
static void PNGErr(png_structp png, png_const_charp msg)
{
Log.report(logvisor::Error, msg);
}
static void PNGWarn(png_structp png, png_const_charp msg)
{
Log.report(logvisor::Warning, msg);
}
static inline uint8_t Convert4To8(uint8_t v)
{
/* Swizzle bits: 00001234 -> 12341234 */
return (v << 4) | v;
}
void SpecBase::extractRandomStaticEntropy(const uint8_t* buf, const hecl::ProjectPath& noAramPath)
2017-05-31 22:34:24 -07:00
{
2018-10-14 13:16:21 -07:00
hecl::ProjectPath entropyPath(noAramPath, _SYS_STR("RandomStaticEntropy.png"));
hecl::ProjectPath catalogPath(noAramPath, _SYS_STR("!catalog.yaml"));
2017-05-31 22:34:24 -07:00
2018-10-14 13:16:21 -07:00
if (FILE* fp = hecl::Fopen(catalogPath.getAbsolutePath().data(), _SYS_STR("a")))
2017-05-31 22:34:24 -07:00
{
2017-11-12 22:19:18 -08:00
fprintf(fp, "RandomStaticEntropy: %s\n", entropyPath.getRelativePathUTF8().data());
2017-05-31 22:34:24 -07:00
fclose(fp);
}
2018-10-14 13:16:21 -07:00
FILE* fp = hecl::Fopen(entropyPath.getAbsolutePath().data(), _SYS_STR("wb"));
2017-05-31 22:34:24 -07:00
if (!fp)
{
Log.report(logvisor::Error,
2018-10-14 13:16:21 -07:00
_SYS_STR("Unable to open '%s' for writing"),
2017-11-12 22:19:18 -08:00
entropyPath.getAbsolutePath().data());
2017-05-31 22:34:24 -07:00
return;
}
png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, PNGErr, PNGWarn);
png_init_io(png, fp);
png_infop info = png_create_info_struct(png);
png_text textStruct = {};
textStruct.key = png_charp("urde_nomip");
png_set_text(png, info, &textStruct, 1);
png_set_IHDR(png, info, 1024, 512, 8,
PNG_COLOR_TYPE_GRAY_ALPHA, PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
png_write_info(png, info);
std::unique_ptr<uint8_t[]> rowbuf(new uint8_t[1024*2]);
for (int y=0 ; y<512 ; ++y)
{
for (int x=0 ; x<1024 ; ++x)
{
uint8_t texel = buf[y*1024+x];
rowbuf[x*2] = Convert4To8(texel >> 4 & 0xf);
rowbuf[x*2+1] = Convert4To8(texel & 0xf);
}
png_write_row(png, rowbuf.get());
}
png_write_end(png, info);
png_write_flush(png);
png_destroy_write_struct(&png, &info);
fclose(fp);
}
void SpecBase::clearTagCache()
{
m_tagToPath.clear();
m_pathToTag.clear();
m_catalogNameToTag.clear();
2018-04-01 21:27:24 -07:00
m_catalogTagToNames.clear();
}
hecl::ProjectPath SpecBase::pathFromTag(const urde::SObjectTag& tag) const
{
std::unique_lock<std::mutex> lk(const_cast<SpecBase&>(*this).m_backgroundIndexMutex);
auto search = m_tagToPath.find(tag);
if (search != m_tagToPath.cend())
return search->second;
return {};
}
urde::SObjectTag SpecBase::tagFromPath(const hecl::ProjectPath& path,
2017-12-29 00:08:12 -08:00
hecl::blender::Token& btok) const
{
auto search = m_pathToTag.find(path.hash());
if (search != m_pathToTag.cend())
return search->second;
return buildTagFromPath(path, btok);
}
bool SpecBase::waitForTagReady(const urde::SObjectTag& tag, const hecl::ProjectPath*& pathOut)
{
std::unique_lock<std::mutex> lk(m_backgroundIndexMutex);
auto search = m_tagToPath.find(tag);
if (search == m_tagToPath.end())
{
if (m_backgroundRunning)
{
while (m_backgroundRunning)
{
lk.unlock();
std::this_thread::sleep_for(std::chrono::milliseconds(2));
lk.lock();
search = m_tagToPath.find(tag);
if (search != m_tagToPath.end())
break;
}
if (search == m_tagToPath.end())
return false;
}
else
return false;
}
lk.unlock();
pathOut = &search->second;
return true;
}
2017-11-12 22:19:18 -08:00
const urde::SObjectTag* SpecBase::getResourceIdByName(std::string_view name) const
{
2017-11-12 22:19:18 -08:00
std::string lower(name);
std::transform(lower.cbegin(), lower.cend(), lower.begin(), tolower);
std::unique_lock<std::mutex> lk(const_cast<SpecBase&>(*this).m_backgroundIndexMutex);
auto search = m_catalogNameToTag.find(lower);
if (search == m_catalogNameToTag.end())
{
if (m_backgroundRunning)
{
while (m_backgroundRunning)
{
lk.unlock();
std::this_thread::sleep_for(std::chrono::milliseconds(2));
lk.lock();
search = m_catalogNameToTag.find(lower);
if (search != m_catalogNameToTag.end())
break;
}
if (search == m_catalogNameToTag.end())
return nullptr;
}
else
return nullptr;
}
return &search->second;
}
FourCC SpecBase::getResourceTypeById(urde::CAssetId id) const
{
if (!id.IsValid())
return {};
std::unique_lock<std::mutex> lk(const_cast<SpecBase&>(*this).m_backgroundIndexMutex);
urde::SObjectTag searchTag = {FourCC(), id};
auto search = m_tagToPath.find(searchTag);
if (search == m_tagToPath.end())
{
if (m_backgroundRunning)
{
while (m_backgroundRunning)
{
lk.unlock();
std::this_thread::sleep_for(std::chrono::milliseconds(2));
lk.lock();
search = m_tagToPath.find(searchTag);
if (search != m_tagToPath.end())
break;
}
if (search == m_tagToPath.end())
return {};
}
else
return {};
}
return search->first.type;
}
void SpecBase::enumerateResources(const std::function<bool(const urde::SObjectTag&)>& lambda) const
{
waitForIndexComplete();
for (const auto& pair : m_tagToPath)
{
if (!lambda(pair.first))
break;
}
}
void SpecBase::enumerateNamedResources(
2017-11-12 22:19:18 -08:00
const std::function<bool(std::string_view, const urde::SObjectTag&)>& lambda) const
{
waitForIndexComplete();
for (const auto& pair : m_catalogNameToTag)
{
if (!lambda(pair.first, pair.second))
break;
}
}
static void WriteTag(athena::io::YAMLDocWriter& cacheWriter,
const urde::SObjectTag& pathTag, const hecl::ProjectPath& path)
{
char idStr[9];
snprintf(idStr, 9, "%08X", uint32_t(pathTag.id.Value()));
if (auto v = cacheWriter.enterSubVector(idStr))
{
cacheWriter.writeString(nullptr, pathTag.type.toString().c_str());
cacheWriter.writeString(nullptr, path.getAuxInfo().size() ?
2017-11-12 22:19:18 -08:00
(std::string(path.getRelativePathUTF8()) + '|' + path.getAuxInfoUTF8().data()) :
path.getRelativePathUTF8());
}
}
static void WriteNameTag(athena::io::YAMLDocWriter& nameWriter,
const urde::SObjectTag& pathTag,
2017-11-12 22:19:18 -08:00
std::string_view name)
{
char idStr[9];
snprintf(idStr, 9, "%08X", uint32_t(pathTag.id.Value()));
2017-11-12 22:19:18 -08:00
nameWriter.writeString(name.data(), idStr);
}
void SpecBase::readCatalog(const hecl::ProjectPath& catalogPath,
athena::io::YAMLDocWriter& nameWriter)
{
athena::io::FileReader freader(catalogPath.getAbsolutePath());
if (!freader.isOpen())
return;
athena::io::YAMLDocReader reader;
bool res = reader.parse(&freader);
if (!res)
return;
const athena::io::YAMLNode* root = reader.getRootNode();
for (const auto& p : root->m_mapChildren)
{
/* Hash as lowercase since lookup is case-insensitive */
std::string pLower = p.first;
std::transform(pLower.cbegin(), pLower.cend(), pLower.begin(), tolower);
/* Avoid redundant filesystem access for re-caches */
if (m_catalogNameToTag.find(pLower) != m_catalogNameToTag.cend())
continue;
athena::io::YAMLNode& node = *p.second;
hecl::ProjectPath path;
if (node.m_type == YAML_SCALAR_NODE)
{
path = hecl::ProjectPath(m_project.getProjectWorkingPath(), node.m_scalarString);
}
else if (node.m_type == YAML_SEQUENCE_NODE)
{
if (node.m_seqChildren.size() >= 2)
path = hecl::ProjectPath(m_project.getProjectWorkingPath(), node.m_seqChildren[0]->m_scalarString).
ensureAuxInfo(node.m_seqChildren[1]->m_scalarString);
else if (node.m_seqChildren.size() == 1)
path = hecl::ProjectPath(m_project.getProjectWorkingPath(), node.m_seqChildren[0]->m_scalarString);
}
if (path.isNone())
continue;
urde::SObjectTag pathTag = tagFromPath(path, m_backgroundBlender);
if (pathTag)
{
std::unique_lock<std::mutex> lk(m_backgroundIndexMutex);
m_catalogNameToTag[pLower] = pathTag;
2018-04-01 21:27:24 -07:00
m_catalogTagToNames[pathTag].insert(p.first);
WriteNameTag(nameWriter, pathTag, p.first);
#if 0
fprintf(stderr, "%s %s %08X\n",
p.first.c_str(),
pathTag.type.toString().c_str(), uint32_t(pathTag.id));
#endif
}
}
}
void SpecBase::backgroundIndexRecursiveCatalogs(const hecl::ProjectPath& dir,
athena::io::YAMLDocWriter& nameWriter,
int level)
{
hecl::DirectoryEnumerator dEnum(dir.getAbsolutePath(),
hecl::DirectoryEnumerator::Mode::DirsThenFilesSorted,
false, false, true);
/* Enumerate all items */
for (const hecl::DirectoryEnumerator::Entry& ent : dEnum)
{
hecl::ProjectPath path(dir, ent.m_name);
if (ent.m_isDir && level < 1)
backgroundIndexRecursiveCatalogs(path, nameWriter, level + 1);
else
{
if (!path.isFile())
continue;
/* Read catalog.yaml for .pak directory if exists */
2018-10-14 13:16:21 -07:00
if (level == 1 && !ent.m_name.compare(_SYS_STR("!catalog.yaml")))
{
readCatalog(path, nameWriter);
continue;
}
}
/* bail if cancelled by client */
if (!m_backgroundRunning)
break;
}
}
#if DUMP_CACHE_FILL
static void DumpCacheAdd(const urde::SObjectTag& pathTag, const hecl::ProjectPath& path)
{
fprintf(stderr, "%s %08X %s\n",
pathTag.type.toString().c_str(), uint32_t(pathTag.id.Value()),
2017-11-12 22:19:18 -08:00
path.getRelativePathUTF8().data());
}
#endif
bool SpecBase::addFileToIndex(const hecl::ProjectPath& path,
athena::io::YAMLDocWriter& cacheWriter)
{
/* Avoid redundant filesystem access for re-caches */
if (m_pathToTag.find(path.hash()) != m_pathToTag.cend())
return true;
/* Try as glob */
2018-10-14 13:16:21 -07:00
hecl::ProjectPath asGlob = path.getWithExtension(_SYS_STR(".*"), true);
if (m_pathToTag.find(asGlob.hash()) != m_pathToTag.cend())
return true;
/* Classify intermediate into tag */
urde::SObjectTag pathTag = buildTagFromPath(path, m_backgroundBlender);
if (pathTag)
{
std::unique_lock<std::mutex> lk(m_backgroundIndexMutex);
bool useGlob = false;
/* Special multi-resource intermediates */
if (pathTag.type == SBIG('ANCS'))
{
2017-12-29 00:08:12 -08:00
hecl::blender::Connection& conn = m_backgroundBlender.getBlenderConnection();
if (!conn.openBlend(path) || conn.getBlendType() != hecl::blender::BlendType::Actor)
return false;
/* Transform tag to glob */
pathTag = {SBIG('ANCS'), asGlob.hash().val32()};
useGlob = true;
2017-12-29 00:08:12 -08:00
hecl::blender::DataStream ds = conn.beginData();
std::vector<std::string> armatureNames = ds.getArmatureNames();
std::vector<std::string> subtypeNames = ds.getSubtypeNames();
std::vector<std::string> actionNames = ds.getActionNames();
for (const std::string& arm : armatureNames)
{
2017-11-12 22:19:18 -08:00
hecl::SystemStringConv sysStr(arm);
2018-10-14 13:16:21 -07:00
hecl::ProjectPath subPath = asGlob.ensureAuxInfo(hecl::SystemString(sysStr.sys_str()) + _SYS_STR(".CINF"));
urde::SObjectTag pathTag = buildTagFromPath(subPath, m_backgroundBlender);
m_tagToPath[pathTag] = subPath;
m_pathToTag[subPath.hash()] = pathTag;
WriteTag(cacheWriter, pathTag, subPath);
#if DUMP_CACHE_FILL
DumpCacheAdd(pathTag, subPath);
#endif
}
for (const std::string& sub : subtypeNames)
{
2017-11-12 22:19:18 -08:00
hecl::SystemStringConv sysStr(sub);
2018-10-14 13:16:21 -07:00
hecl::ProjectPath subPath = asGlob.ensureAuxInfo(hecl::SystemString(sysStr.sys_str()) + _SYS_STR(".CSKR"));
urde::SObjectTag pathTag = buildTagFromPath(subPath, m_backgroundBlender);
m_tagToPath[pathTag] = subPath;
m_pathToTag[subPath.hash()] = pathTag;
WriteTag(cacheWriter, pathTag, subPath);
#if DUMP_CACHE_FILL
DumpCacheAdd(pathTag, subPath);
#endif
std::vector<std::string> overlayNames = ds.getSubtypeOverlayNames(sub);
for (const auto& overlay : overlayNames)
{
2017-11-12 22:19:18 -08:00
hecl::SystemStringConv overlaySys(overlay);
2018-10-14 13:16:21 -07:00
hecl::ProjectPath subPath = asGlob.ensureAuxInfo(hecl::SystemString(sysStr.sys_str()) + _SYS_STR('.') +
overlaySys.c_str() + _SYS_STR(".CSKR"));
urde::SObjectTag pathTag = buildTagFromPath(subPath, m_backgroundBlender);
m_tagToPath[pathTag] = subPath;
m_pathToTag[subPath.hash()] = pathTag;
WriteTag(cacheWriter, pathTag, subPath);
#if DUMP_CACHE_FILL
DumpCacheAdd(pathTag, subPath);
#endif
}
}
std::vector<std::string> attachmentNames = ds.getAttachmentNames();
for (const auto& attachment : attachmentNames)
{
hecl::SystemStringConv attachmentSys(attachment);
2018-10-14 13:16:21 -07:00
hecl::ProjectPath subPath = asGlob.ensureAuxInfo(hecl::SystemString(_SYS_STR("ATTACH.")) +
attachmentSys.c_str() + _SYS_STR(".CSKR"));
urde::SObjectTag pathTag = buildTagFromPath(subPath, m_backgroundBlender);
m_tagToPath[pathTag] = subPath;
m_pathToTag[subPath.hash()] = pathTag;
WriteTag(cacheWriter, pathTag, subPath);
#if DUMP_CACHE_FILL
DumpCacheAdd(pathTag, subPath);
#endif
}
for (const std::string& act : actionNames)
{
2017-11-12 22:19:18 -08:00
hecl::SystemStringConv sysStr(act);
2018-10-14 13:16:21 -07:00
hecl::ProjectPath subPath = asGlob.ensureAuxInfo(hecl::SystemString(sysStr.sys_str()) + _SYS_STR(".ANIM"));
urde::SObjectTag pathTag = buildTagFromPath(subPath, m_backgroundBlender);
m_tagToPath[pathTag] = subPath;
m_pathToTag[subPath.hash()] = pathTag;
WriteTag(cacheWriter, pathTag, subPath);
#if DUMP_CACHE_FILL
DumpCacheAdd(pathTag, subPath);
#endif
}
}
else if (pathTag.type == SBIG('MLVL'))
{
/* Transform tag to glob */
pathTag = {SBIG('MLVL'), asGlob.hash().val32()};
useGlob = true;
2018-10-14 13:16:21 -07:00
hecl::ProjectPath subPath = asGlob.ensureAuxInfo(_SYS_STR("MAPW"));
urde::SObjectTag pathTag = buildTagFromPath(subPath, m_backgroundBlender);
m_tagToPath[pathTag] = subPath;
m_pathToTag[subPath.hash()] = pathTag;
WriteTag(cacheWriter, pathTag, subPath);
#if DUMP_CACHE_FILL
DumpCacheAdd(pathTag, subPath);
#endif
2018-10-14 13:16:21 -07:00
subPath = asGlob.ensureAuxInfo(_SYS_STR("SAVW"));
pathTag = buildTagFromPath(subPath, m_backgroundBlender);
m_tagToPath[pathTag] = subPath;
m_pathToTag[subPath.hash()] = pathTag;
WriteTag(cacheWriter, pathTag, subPath);
#if DUMP_CACHE_FILL
DumpCacheAdd(pathTag, subPath);
#endif
}
/* Cache in-memory */
const hecl::ProjectPath& usePath = useGlob ? asGlob : path;
m_tagToPath[pathTag] = usePath;
m_pathToTag[usePath.hash()] = pathTag;
WriteTag(cacheWriter, pathTag, usePath);
#if DUMP_CACHE_FILL
DumpCacheAdd(pathTag, usePath);
#endif
}
return true;
}
void SpecBase::backgroundIndexRecursiveProc(const hecl::ProjectPath& dir,
athena::io::YAMLDocWriter& cacheWriter,
athena::io::YAMLDocWriter& nameWriter,
int level)
{
hecl::DirectoryEnumerator dEnum(dir.getAbsolutePath(),
hecl::DirectoryEnumerator::Mode::DirsThenFilesSorted,
false, false, true);
/* Enumerate all items */
for (const hecl::DirectoryEnumerator::Entry& ent : dEnum)
{
2018-05-25 20:07:29 -07:00
/* bail if cancelled by client */
if (!m_backgroundRunning)
break;
hecl::ProjectPath path(dir, ent.m_name);
if (ent.m_isDir)
{
/* Index AGSC here */
if (hecl::ProjectPath(path, "!project.yaml").isFile() &&
hecl::ProjectPath(path, "!pool.yaml").isFile())
{
urde::SObjectTag pathTag(SBIG('AGSC'), path.hash().val32());
m_tagToPath[pathTag] = path;
m_pathToTag[path.hash()] = pathTag;
WriteTag(cacheWriter, pathTag, path);
}
else
{
backgroundIndexRecursiveProc(path, cacheWriter, nameWriter, level + 1);
}
}
else
{
if (!path.isFile())
continue;
/* Read catalog.yaml for .pak directory if exists */
2018-10-14 13:16:21 -07:00
if (level == 1 && !ent.m_name.compare(_SYS_STR("!catalog.yaml")))
{
readCatalog(path, nameWriter);
continue;
}
/* Index the regular file */
addFileToIndex(path, cacheWriter);
}
}
}
void SpecBase::backgroundIndexProc()
{
2018-06-01 17:03:31 -07:00
logvisor::RegisterThreadName("Resource Index");
2018-10-14 13:16:21 -07:00
hecl::ProjectPath tagCachePath(m_project.getProjectCookedPath(getOriginalSpec()), _SYS_STR("tag_cache.yaml"));
hecl::ProjectPath nameCachePath(m_project.getProjectCookedPath(getOriginalSpec()), _SYS_STR("name_cache.yaml"));
hecl::ProjectPath specRoot(m_project.getProjectWorkingPath(), getOriginalSpec().m_name);
/* Cache will be overwritten with validated entries afterwards */
athena::io::YAMLDocWriter cacheWriter(nullptr);
athena::io::YAMLDocWriter nameWriter(nullptr);
/* Read in tag cache */
if (tagCachePath.isFile())
{
athena::io::FileReader reader(tagCachePath.getAbsolutePath());
if (reader.isOpen())
{
2018-10-14 13:16:21 -07:00
Log.report(logvisor::Info, _SYS_STR("Cache index of '%s' loading"), getOriginalSpec().m_name.data());
athena::io::YAMLDocReader cacheReader;
if (cacheReader.parse(&reader))
{
std::unique_lock<std::mutex> lk(m_backgroundIndexMutex);
size_t tagCount = cacheReader.getRootNode()->m_mapChildren.size();
m_tagToPath.reserve(tagCount);
m_pathToTag.reserve(tagCount);
size_t loadIdx = 0;
for (const auto& child : cacheReader.getRootNode()->m_mapChildren)
{
2018-05-25 20:07:29 -07:00
if (!m_backgroundRunning)
return;
const athena::io::YAMLNode& node = *child.second;
unsigned long id = strtoul(child.first.c_str(), nullptr, 16);
hecl::FourCC type(node.m_seqChildren.at(0)->m_scalarString.c_str());
hecl::ProjectPath path(m_project.getProjectWorkingPath(),
node.m_seqChildren.at(1)->m_scalarString);
if (path.isFileOrGlob())
{
urde::SObjectTag pathTag(type, id);
m_tagToPath[pathTag] = path;
m_pathToTag[path.hash()] = pathTag;
WriteTag(cacheWriter, pathTag, path);
}
2018-03-23 14:56:17 -07:00
++loadIdx;
if (!(loadIdx % 100))
fprintf(stderr, "\r %" PRISize " / %" PRISize, loadIdx, tagCount);
}
2018-03-23 14:56:17 -07:00
fprintf(stderr, "\r %" PRISize " / %" PRISize "\n", loadIdx, tagCount);
}
2018-10-14 13:16:21 -07:00
Log.report(logvisor::Info, _SYS_STR("Cache index of '%s' loaded; %d tags"),
2017-11-13 19:36:36 -08:00
getOriginalSpec().m_name.data(), m_tagToPath.size());
if (nameCachePath.isFile())
{
/* Read in name cache */
2018-10-14 13:16:21 -07:00
Log.report(logvisor::Info, _SYS_STR("Name index of '%s' loading"), getOriginalSpec().m_name.data());
athena::io::FileReader nreader(nameCachePath.getAbsolutePath());
athena::io::YAMLDocReader nameReader;
if (nameReader.parse(&nreader))
{
std::unique_lock<std::mutex> lk(m_backgroundIndexMutex);
m_catalogNameToTag.reserve(nameReader.getRootNode()->m_mapChildren.size());
2018-04-01 21:27:24 -07:00
m_catalogTagToNames.reserve(nameReader.getRootNode()->m_mapChildren.size());
for (const auto& child : nameReader.getRootNode()->m_mapChildren)
{
unsigned long id = strtoul(child.second->m_scalarString.c_str(), nullptr, 16);
auto search = m_tagToPath.find(urde::SObjectTag(FourCC(), uint32_t(id)));
if (search != m_tagToPath.cend())
{
std::string chLower = child.first;
std::transform(chLower.cbegin(), chLower.cend(), chLower.begin(), tolower);
m_catalogNameToTag[chLower] = search->first;
2018-04-01 21:27:24 -07:00
m_catalogTagToNames[search->first].insert(child.first);
WriteNameTag(nameWriter, search->first, child.first);
}
}
}
2018-10-14 13:16:21 -07:00
Log.report(logvisor::Info, _SYS_STR("Name index of '%s' loaded; %d names"),
2017-11-13 19:36:36 -08:00
getOriginalSpec().m_name.data(), m_catalogNameToTag.size());
}
}
}
/* Add special original IDs resource if exists (not name-cached to disk) */
hecl::ProjectPath oidsPath(specRoot, "!original_ids.yaml");
urde::SObjectTag oidsTag = buildTagFromPath(oidsPath, m_backgroundBlender);
if (oidsTag)
{
m_catalogNameToTag["mp1originalids"] = oidsTag;
2018-04-01 21:27:24 -07:00
m_catalogTagToNames[oidsTag].insert("MP1OriginalIDs");
}
2018-10-14 13:16:21 -07:00
Log.report(logvisor::Info, _SYS_STR("Background index of '%s' started"), getOriginalSpec().m_name.data());
backgroundIndexRecursiveProc(specRoot, cacheWriter, nameWriter, 0);
tagCachePath.makeDirChain(false);
athena::io::FileWriter twriter(tagCachePath.getAbsolutePath());
cacheWriter.finish(&twriter);
athena::io::FileWriter nwriter(nameCachePath.getAbsolutePath());
nameWriter.finish(&nwriter);
m_backgroundBlender.shutdown();
2018-10-14 13:16:21 -07:00
Log.report(logvisor::Info, _SYS_STR("Background index of '%s' complete; %d tags, %d names"),
2017-11-13 19:36:36 -08:00
getOriginalSpec().m_name.data(), m_tagToPath.size(), m_catalogNameToTag.size());
m_backgroundRunning = false;
}
void SpecBase::cancelBackgroundIndex()
{
m_backgroundRunning = false;
if (m_backgroundIndexTh.joinable())
m_backgroundIndexTh.join();
}
void SpecBase::beginBackgroundIndex()
{
cancelBackgroundIndex();
clearTagCache();
m_backgroundRunning = true;
m_backgroundIndexTh = std::thread(std::bind(&SpecBase::backgroundIndexProc, this));
}
void SpecBase::waitForIndexComplete() const
{
std::unique_lock<std::mutex> lk(const_cast<SpecBase&>(*this).m_backgroundIndexMutex);
while (m_backgroundRunning)
{
lk.unlock();
std::this_thread::sleep_for(std::chrono::milliseconds(2));
lk.lock();
}
}
2015-07-01 16:50:39 -07:00
}