metaforce/DataSpec/SpecBase.cpp

473 lines
15 KiB
C++
Raw Normal View History

2015-11-27 22:21:19 +00:00
#if _WIN32
#define _CRT_RAND_S
#include <stdlib.h>
#endif
2015-07-01 23:50:39 +00:00
#include "SpecBase.hpp"
#include "Blender/BlenderSupport.hpp"
2016-09-18 23:47:48 +00:00
#include "hecl/Blender/BlenderConnection.hpp"
2015-11-10 02:07:15 +00:00
#include "DNACommon/DNACommon.hpp"
#include "DNACommon/TXTR.hpp"
2015-07-01 23:50:39 +00:00
2015-11-22 05:16:40 +00:00
#include <time.h>
2017-06-01 05:34:24 +00:00
#include <png.h>
2015-11-22 05:16:40 +00:00
2016-02-13 09:02:47 +00:00
namespace DataSpec
2015-07-01 23:50:39 +00:00
{
2016-03-04 23:04:53 +00:00
static logvisor::Module Log("urde::SpecBase");
2016-03-04 23:04:53 +00:00
static const hecl::SystemChar* MomErr[] =
2015-11-22 05:16:40 +00:00
{
_S("Your metroid is in another castle"),
_S("HECL is experiencing a PTSD attack"),
_S("Unable to freeze metroids"),
_S("Ridley ate your homework"),
2017-02-05 06:20:31 +00:00
_S("Expected 0 maternal symbolisms, found 2147483647"),
2015-11-22 05:16:40 +00:00
_S("Contradictive narratives unsupported"),
_S("Wiimote profile \"NES + Zapper\" not recognized"),
_S("Unable to find Waldo"),
2016-02-20 08:53:06 +00:00
_S("Expected Ridley, found furby"),
_S("Adam has not authorized this, please do not bug the developers"),
_S("Lady returned objection"),
_S("Unterminated plot thread 'Deleter' detected")
2015-11-22 05:16:40 +00: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")
{
DataSpec::UniqueIDBridge::setThreadProject(m_project);
}
2015-11-10 02:07:15 +00:00
static const hecl::SystemString regNONE = _S("");
static const hecl::SystemString regE = _S("NTSC");
static const hecl::SystemString regJ = _S("NTSC-J");
static const hecl::SystemString regP = _S("PAL");
void SpecBase::setThreadProject()
{
UniqueIDBridge::setThreadProject(m_project);
}
2015-10-04 05:08:56 +00:00
bool SpecBase::canExtract(const ExtractPassInfo& info, std::vector<ExtractReport>& reps)
2015-07-01 23:50:39 +00:00
{
2016-03-04 23:04:53 +00:00
m_disc = nod::OpenDiscFromImage(info.srcpath.c_str(), m_isWii);
2015-07-16 01:57:34 +00:00
if (!m_disc)
2015-07-02 20:41:40 +00:00
return false;
2016-01-20 03:18:30 +00:00
const char* gameID = m_disc->getHeader().m_gameID;
2015-07-02 20:41:40 +00:00
2015-11-22 05:16:40 +00:00
if (!memcmp(gameID, "R3O", 3))
{
unsigned int t = time(nullptr);
2015-11-27 22:21:19 +00:00
#if _WIN32
rand_s(&t);
2016-02-20 08:53:06 +00:00
int r = t % MomErrCount;
2015-11-27 22:21:19 +00:00
#else
2016-02-20 08:53:06 +00:00
int r = rand_r(&t) % MomErrCount;
2015-11-27 22:21:19 +00:00
#endif
2016-03-04 23:04:53 +00:00
Log.report(logvisor::Fatal, MomErr[r]);
2015-11-22 05:16:40 +00:00
}
m_standalone = true;
if (m_isWii && !memcmp(gameID, "R3M", 3))
m_standalone = false;
2015-07-01 23:50:39 +00:00
if (m_standalone && !checkStandaloneID(gameID))
2015-07-10 05:28:08 +00:00
return false;
2016-01-20 03:18:30 +00:00
char region = m_disc->getHeader().m_gameID[3];
2016-03-04 23:04:53 +00:00
const hecl::SystemString* regstr = &regNONE;
2015-07-12 18:07:58 +00:00
switch (region)
{
case 'E':
regstr = &regE;
break;
case 'J':
regstr = &regJ;
break;
case 'P':
regstr = &regP;
break;
}
if (m_standalone)
2015-08-05 21:46:07 +00:00
return checkFromStandaloneDisc(*m_disc, *regstr, info.extractArgs, reps);
2015-07-10 05:28:08 +00:00
else
2015-08-05 21:46:07 +00:00
return checkFromTrilogyDisc(*m_disc, *regstr, info.extractArgs, reps);
2015-07-01 23:50:39 +00:00
}
2015-09-30 06:23:40 +00:00
void SpecBase::doExtract(const ExtractPassInfo& info, FProgress progress)
2015-07-01 23:50:39 +00:00
{
2016-04-10 04:49:02 +00:00
DataSpec::g_curSpec.reset(this);
2015-08-05 21:46:07 +00:00
if (!Blender::BuildMasterShader(m_masterShader))
2016-03-04 23:04:53 +00:00
Log.report(logvisor::Fatal, "Unable to build master shader blend");
if (m_isWii)
{
/* Extract root files for repacking later */
2016-03-05 00:03:41 +00:00
hecl::ProjectPath outDir(m_project.getProjectWorkingPath(), _S("out"));
outDir.makeDirChain(true);
2016-03-04 23:04:53 +00:00
nod::ExtractionContext ctx = {true, info.force, nullptr};
2015-09-29 03:49:31 +00:00
if (!m_standalone)
{
2015-09-02 22:00:40 +00:00
progress(_S("Trilogy Files"), _S(""), 1, 0.0);
2016-03-04 23:04:53 +00:00
nod::Partition* data = m_disc->getDataPartition();
const nod::Node& root = data->getFSTRoot();
for (const nod::Node& child : root)
if (child.getKind() == nod::Node::Kind::File)
child.extractToDirectory(outDir.getAbsolutePath(), ctx);
2015-09-02 22:00:40 +00:00
progress(_S("Trilogy Files"), _S(""), 1, 1.0);
}
}
2015-08-05 21:46:07 +00:00
extractFromDisc(*m_disc, info.force, progress);
2015-07-01 23:50:39 +00:00
}
2016-09-18 23:47:48 +00:00
static bool IsPathAudioGroup(const hecl::ProjectPath& path)
{
if (path.getPathType() != hecl::ProjectPath::Type::Glob ||
!path.getWithExtension(_S(".pool"), true).isFile() ||
!path.getWithExtension(_S(".proj"), true).isFile() ||
!path.getWithExtension(_S(".sdir"), true).isFile() ||
!path.getWithExtension(_S(".samp"), true).isFile())
return false;
return true;
}
static bool IsPathSong(const hecl::ProjectPath& path)
{
if (path.getPathType() != hecl::ProjectPath::Type::Glob ||
!path.getWithExtension(_S(".mid"), true).isFile() ||
!path.getWithExtension(_S(".yaml"), true).isFile())
return false;
return true;
}
bool SpecBase::canCook(const hecl::ProjectPath& path, hecl::BlenderToken& btok)
2015-07-01 23:50:39 +00:00
{
2015-10-04 05:08:56 +00:00
if (!checkPathPrefix(path))
return false;
2016-09-23 18:56:42 +00:00
hecl::ProjectPath asBlend;
if (path.getPathType() == hecl::ProjectPath::Type::Glob)
asBlend = path.getWithExtension(_S(".blend"), true);
else
asBlend = path;
if (hecl::IsPathBlend(asBlend))
2015-10-01 00:40:21 +00:00
{
hecl::BlenderConnection& conn = btok.getBlenderConnection();
2016-09-23 18:56:42 +00:00
if (!conn.openBlend(asBlend))
2015-10-01 00:40:21 +00:00
return false;
2016-03-04 23:04:53 +00:00
if (conn.getBlendType() != hecl::BlenderConnection::BlendType::None)
2015-10-01 00:40:21 +00:00
return true;
}
2016-03-04 23:04:53 +00:00
else if (hecl::IsPathPNG(path))
2015-10-01 00:40:21 +00:00
{
return true;
}
2016-03-04 23:04:53 +00:00
else if (hecl::IsPathYAML(path))
2015-10-01 00:40:21 +00:00
{
2016-08-22 03:47:48 +00:00
athena::io::FileReader reader(path.getAbsolutePath());
bool retval = validateYAMLDNAType(reader);
2015-10-01 00:40:21 +00:00
return retval;
}
2016-09-18 23:47:48 +00:00
else if (IsPathAudioGroup(path))
{
return true;
}
else if (IsPathSong(path))
{
return true;
}
2015-07-22 19:05:18 +00:00
return false;
2015-07-01 23:50:39 +00:00
}
const hecl::Database::DataSpecEntry* SpecBase::overrideDataSpec(const hecl::ProjectPath& path,
const hecl::Database::DataSpecEntry* oldEntry,
2016-10-01 23:20:20 +00:00
hecl::BlenderToken& btok) const
{
if (!checkPathPrefix(path))
return nullptr;
2016-09-23 18:56:42 +00:00
hecl::ProjectPath asBlend;
if (path.getPathType() == hecl::ProjectPath::Type::Glob)
asBlend = path.getWithExtension(_S(".blend"), true);
else
asBlend = path;
if (hecl::IsPathBlend(asBlend))
{
if (hecl::StringUtils::EndsWith(path.getAuxInfo(), _S(".CSKR")) ||
hecl::StringUtils::EndsWith(path.getAuxInfo(), _S(".ANIM")))
2016-08-31 23:31:12 +00:00
return oldEntry;
hecl::BlenderConnection& conn = btok.getBlenderConnection();
2016-09-23 18:56:42 +00:00
if (!conn.openBlend(asBlend))
{
Log.report(logvisor::Error, _S("unable to cook '%s'"),
path.getAbsolutePath().c_str());
return nullptr;
}
hecl::BlenderConnection::BlendType type = conn.getBlendType();
if (type == hecl::BlenderConnection::BlendType::Mesh ||
type == hecl::BlenderConnection::BlendType::Area)
return oldEntry;
}
else if (hecl::IsPathPNG(path))
{
return oldEntry;
}
return &getOriginalSpec();
}
2016-03-04 23:04:53 +00:00
void SpecBase::doCook(const hecl::ProjectPath& path, const hecl::ProjectPath& cookedPath,
bool fast, hecl::BlenderToken& btok, FCookProgress progress)
2015-07-01 23:50:39 +00:00
{
2016-12-25 01:36:42 +00:00
cookedPath.makeDirChain(false);
2016-04-10 04:49:02 +00:00
DataSpec::g_curSpec.reset(this);
2016-09-23 18:56:42 +00:00
hecl::ProjectPath asBlend;
if (path.getPathType() == hecl::ProjectPath::Type::Glob)
asBlend = path.getWithExtension(_S(".blend"), true);
else
asBlend = path;
if (hecl::IsPathBlend(asBlend))
2015-10-04 05:08:56 +00:00
{
hecl::BlenderConnection& conn = btok.getBlenderConnection();
2016-09-23 18:56:42 +00:00
if (!conn.openBlend(asBlend))
2015-10-04 05:08:56 +00:00
return;
2015-10-07 01:17:17 +00:00
switch (conn.getBlendType())
{
2016-03-04 23:04:53 +00:00
case hecl::BlenderConnection::BlendType::Mesh:
2015-10-04 05:08:56 +00:00
{
2016-03-04 23:04:53 +00:00
hecl::BlenderConnection::DataStream ds = conn.beginData();
2016-04-10 04:49:02 +00:00
cookMesh(cookedPath, path, ds, fast, btok, progress);
2015-10-07 01:17:17 +00:00
break;
2015-10-04 05:08:56 +00:00
}
2016-03-04 23:04:53 +00:00
case hecl::BlenderConnection::BlendType::Actor:
2015-10-07 01:17:17 +00:00
{
2016-03-04 23:04:53 +00:00
hecl::BlenderConnection::DataStream ds = conn.beginData();
2016-04-10 04:49:02 +00:00
cookActor(cookedPath, path, ds, fast, btok, progress);
2015-10-07 01:17:17 +00:00
break;
}
2016-03-04 23:04:53 +00:00
case hecl::BlenderConnection::BlendType::Area:
2015-10-07 01:17:17 +00:00
{
2016-03-04 23:04:53 +00:00
hecl::BlenderConnection::DataStream ds = conn.beginData();
2016-04-10 04:49:02 +00:00
cookArea(cookedPath, path, ds, fast, btok, progress);
2015-10-07 01:17:17 +00:00
break;
}
case hecl::BlenderConnection::BlendType::World:
{
hecl::BlenderConnection::DataStream ds = conn.beginData();
cookWorld(cookedPath, path, ds, fast, btok, progress);
break;
}
case hecl::BlenderConnection::BlendType::Frame:
{
hecl::BlenderConnection::DataStream ds = conn.beginData();
cookGuiFrame(cookedPath, path, ds, btok, progress);
break;
}
2017-03-20 05:09:53 +00:00
case hecl::BlenderConnection::BlendType::MapArea:
{
hecl::BlenderConnection::DataStream ds = conn.beginData();
cookMapArea(cookedPath, path, ds, btok, progress);
break;
}
case hecl::BlenderConnection::BlendType::MapUniverse:
{
hecl::BlenderConnection::DataStream ds = conn.beginData();
cookMapUniverse(cookedPath, path, ds, btok, progress);
break;
}
2015-10-07 01:17:17 +00:00
default: break;
}
}
else if (hecl::IsPathPNG(path))
{
if (m_pc)
TXTR::CookPC(path, cookedPath);
else
TXTR::Cook(path, cookedPath);
}
2016-03-04 23:04:53 +00:00
else if (hecl::IsPathYAML(path))
2015-10-07 01:17:17 +00:00
{
2016-08-22 03:47:48 +00:00
athena::io::FileReader reader(path.getAbsolutePath());
cookYAML(cookedPath, path, reader, progress);
2015-10-04 05:08:56 +00:00
}
2016-09-18 23:47:48 +00:00
else if (IsPathAudioGroup(path))
{
cookAudioGroup(cookedPath, path, progress);
}
else if (IsPathSong(path))
{
cookSong(cookedPath, path, progress);
}
2015-07-01 23:50:39 +00:00
}
2016-10-02 22:41:36 +00:00
void SpecBase::flattenDependencies(const hecl::ProjectPath& path,
std::vector<hecl::ProjectPath>& pathsOut,
hecl::BlenderToken& btok)
2015-07-01 23:50:39 +00:00
{
2016-10-02 22:41:36 +00:00
DataSpec::g_curSpec.reset(this);
hecl::ProjectPath asBlend;
if (path.getPathType() == hecl::ProjectPath::Type::Glob)
asBlend = path.getWithExtension(_S(".blend"), true);
else
asBlend = path;
if (hecl::IsPathBlend(asBlend))
{
hecl::BlenderConnection& conn = btok.getBlenderConnection();
if (!conn.openBlend(asBlend))
return;
switch (conn.getBlendType())
{
case hecl::BlenderConnection::BlendType::Mesh:
{
hecl::BlenderConnection::DataStream ds = conn.beginData();
std::vector<hecl::ProjectPath> texs = ds.getTextures();
for (const hecl::ProjectPath& tex : texs)
pathsOut.push_back(tex);
break;
}
case hecl::BlenderConnection::BlendType::Actor:
{
hecl::BlenderConnection::DataStream ds = conn.beginData();
2016-10-08 03:41:08 +00:00
hecl::BlenderConnection::DataStream::Actor actor = ds.compileActorCharacterOnly();
2016-10-02 22:41:36 +00:00
for (auto& sub : actor.subtypes)
{
if (sub.armature >= 0)
{
pathsOut.push_back(sub.mesh);
hecl::SystemStringView chSysName(sub.name);
pathsOut.push_back(path.ensureAuxInfo(chSysName.sys_str() + _S(".CSKR")));
const auto& arm = actor.armatures[sub.armature];
hecl::SystemStringView armSysName(arm.name);
pathsOut.push_back(path.ensureAuxInfo(armSysName.sys_str() + _S(".CINF")));
if (sub.overlayMeshes.size())
{
pathsOut.push_back(sub.overlayMeshes[0].second);
pathsOut.push_back(path.ensureAuxInfo(chSysName.sys_str() + _S(".over.CSKR")));
}
}
}
break;
}
case hecl::BlenderConnection::BlendType::Area:
{
hecl::BlenderConnection::DataStream ds = conn.beginData();
std::vector<hecl::ProjectPath> texs = ds.getTextures();
for (const hecl::ProjectPath& tex : texs)
pathsOut.push_back(tex);
break;
}
default: break;
}
}
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)
{
hecl::ProjectPath path = UniqueIDBridge::TranslatePakIdToPath(id);
if (path)
2016-12-25 01:36:42 +00:00
flattenDependencies(path, pathsOut, *g_ThreadBlenderToken.get());
2016-10-02 22:41:36 +00:00
}
void SpecBase::flattenDependencies(const UniqueID64& id, std::vector<hecl::ProjectPath>& pathsOut)
{
hecl::ProjectPath path = UniqueIDBridge::TranslatePakIdToPath(id);
if (path)
2016-12-25 01:36:42 +00:00
flattenDependencies(path, pathsOut, *g_ThreadBlenderToken.get());
2015-07-01 23:50:39 +00:00
}
2016-10-02 22:41:36 +00:00
bool SpecBase::canPackage(const PackagePassInfo& info)
2015-07-01 23:50:39 +00:00
{
2016-10-02 22:41:36 +00:00
return false;
2015-07-01 23:50:39 +00:00
}
2015-08-05 21:46:07 +00:00
void SpecBase::doPackage(const PackagePassInfo& info)
2015-07-01 23:50:39 +00:00
{
}
2017-06-01 05:34:24 +00: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)
{
hecl::ProjectPath entropyPath(noAramPath, _S("RandomStaticEntropy.png"));
hecl::ProjectPath catalogPath(noAramPath, _S("!catalog.yaml"));
if (FILE* fp = hecl::Fopen(catalogPath.getAbsolutePath().c_str(), _S("a")))
{
fprintf(fp, "RandomStaticEntropy: %s\n", entropyPath.getRelativePathUTF8().c_str());
fclose(fp);
}
FILE* fp = hecl::Fopen(entropyPath.getAbsolutePath().c_str(), _S("wb"));
if (!fp)
{
Log.report(logvisor::Error,
_S("Unable to open '%s' for writing"),
entropyPath.getAbsolutePath().c_str());
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);
}
2015-07-01 23:50:39 +00:00
}