metaforce/DataSpec/DNAMP3/MREA.cpp

293 lines
9.3 KiB
C++
Raw Normal View History

2016-03-04 23:04:53 +00:00
#include <athena/FileWriter.hpp>
2015-09-20 06:09:27 +00:00
#include "MREA.hpp"
#include "../DNAMP2/DeafBabe.hpp"
2015-09-28 05:19:31 +00:00
#include "../DNACommon/BabeDead.hpp"
2015-09-20 06:09:27 +00:00
2016-02-13 09:02:47 +00:00
namespace DataSpec
2015-09-20 06:09:27 +00:00
{
namespace DNAMP3
{
2016-03-04 23:04:53 +00:00
MREA::StreamReader::StreamReader(athena::io::IStreamReader& source,
2015-09-20 06:09:27 +00:00
atUint32 blkCount, atUint32 secIdxCount)
: DNAMP2::MREA::StreamReader(source)
{
m_blkCount = blkCount;
m_blockInfos.reserve(blkCount);
2015-10-12 04:41:28 +00:00
for (atUint32 i=0 ; i<blkCount ; ++i)
2015-09-20 06:09:27 +00:00
{
m_blockInfos.emplace_back();
BlockInfo& info = m_blockInfos.back();
info.read(source);
m_totalDecompLen += info.decompSize;
}
source.seekAlign32();
m_secIdxs.reserve(secIdxCount);
2015-10-12 04:41:28 +00:00
for (atUint32 i=0 ; i<secIdxCount ; ++i)
2015-09-20 06:09:27 +00:00
{
m_secIdxs.emplace_back();
std::pair<DNAFourCC, atUint32>& idx = m_secIdxs.back();
idx.first.read(source);
idx.second = source.readUint32Big();
}
source.seekAlign32();
m_blkBase = source.position();
nextBlock();
}
2016-03-04 23:04:53 +00:00
void MREA::StreamReader::writeSecIdxs(athena::io::IStreamWriter& writer) const
2015-09-20 06:09:27 +00:00
{
for (const std::pair<DNAFourCC, atUint32>& idx : m_secIdxs)
{
idx.first.write(writer);
writer.writeUint32Big(idx.second);
}
}
2016-03-04 23:04:53 +00:00
void MREA::ReadBabeDeadToBlender_3(hecl::BlenderConnection::PyOutStream& os,
athena::io::IStreamReader& rs)
2015-09-20 06:09:27 +00:00
{
atUint32 bdMagic = rs.readUint32Big();
if (bdMagic != 0xBABEDEAD)
2016-03-04 23:04:53 +00:00
Log.report(logvisor::Fatal, "invalid BABEDEAD magic");
2015-09-20 06:09:27 +00:00
os << "bpy.context.scene.render.engine = 'CYCLES'\n"
"bpy.context.scene.world.use_nodes = True\n"
"bpy.context.scene.render.engine = 'BLENDER_GAME'\n"
"bg_node = bpy.context.scene.world.node_tree.nodes['Background']\n";
for (atUint32 s=0 ; s<4 ; ++s)
{
atUint32 lightCount = rs.readUint32Big();
for (atUint32 l=0 ; l<lightCount ; ++l)
{
BabeDeadLight light;
light.read(rs);
2015-09-28 05:19:31 +00:00
ReadBabeDeadLightToBlender(os, light, s, l);
2015-09-20 06:09:27 +00:00
}
}
}
bool MREA::Extract(const SpecBase& dataSpec,
PAKEntryReadStream& rs,
2016-03-04 23:04:53 +00:00
const hecl::ProjectPath& outPath,
2015-09-20 06:09:27 +00:00
PAKRouter<PAKBridge>& pakRouter,
const PAK::Entry& entry,
2015-10-26 02:31:09 +00:00
bool force,
2016-03-04 23:04:53 +00:00
std::function<void(const hecl::SystemChar*)>)
2015-09-20 06:09:27 +00:00
{
using RigPair = std::pair<CSKR*, CINF*>;
RigPair dummy(nullptr, nullptr);
hecl::ProjectPath mreaPath;
if (pakRouter.isShared())
/* Rename MREA for consistency */
mreaPath = hecl::ProjectPath(outPath.getParentPath(), _S("!area.blend"));
else
/* We're not in a world pak, so lets keep the original name */
mreaPath = outPath;
2016-03-04 23:04:53 +00:00
if (!force && mreaPath.getPathType() == hecl::ProjectPath::Type::File)
2015-10-26 02:31:09 +00:00
return true;
2015-09-20 06:09:27 +00:00
/* Do extract */
Header head;
head.read(rs);
rs.seekAlign32();
/* MREA decompression stream */
StreamReader drs(rs, head.compressedBlockCount, head.secIndexCount);
2016-03-04 23:04:53 +00:00
athena::io::FileWriter mreaDecompOut(pakRouter.getCooked(&entry).getWithExtension(_S(".decomp")).getAbsolutePath());
2015-09-20 06:09:27 +00:00
head.write(mreaDecompOut);
mreaDecompOut.seekAlign32();
drs.writeDecompInfos(mreaDecompOut);
mreaDecompOut.seekAlign32();
drs.writeSecIdxs(mreaDecompOut);
mreaDecompOut.seekAlign32();
atUint64 decompLen = drs.length();
mreaDecompOut.writeBytes(drs.readBytes(decompLen).get(), decompLen);
mreaDecompOut.close();
2016-03-04 23:04:53 +00:00
drs.seek(0, athena::Begin);
2015-09-20 06:09:27 +00:00
/* Start up blender connection */
2016-03-04 23:04:53 +00:00
hecl::BlenderConnection& conn = hecl::BlenderConnection::SharedConnection();
if (!conn.createBlend(mreaPath, hecl::BlenderConnection::BlendType::Area))
2015-09-20 06:09:27 +00:00
return false;
/* Open Py Stream and read sections */
2016-03-04 23:04:53 +00:00
hecl::BlenderConnection::PyOutStream os = conn.beginPythonOut(true);
2015-09-20 06:09:27 +00:00
os.format("import bpy\n"
"import bmesh\n"
"from mathutils import Vector\n"
"\n"
2015-10-01 00:40:21 +00:00
"bpy.context.scene.name = '%s'\n",
2015-09-20 06:09:27 +00:00
pakRouter.getBestEntryName(entry).c_str());
DNACMDL::InitGeomBlenderContext(os, dataSpec.getMasterShaderPath());
MaterialSet::RegisterMaterialProps(os);
os << "# Clear Scene\n"
"for ob in bpy.data.objects:\n"
" bpy.context.scene.objects.unlink(ob)\n"
" bpy.data.objects.remove(ob)\n"
"bpy.types.Lamp.retro_layer = bpy.props.IntProperty(name='Retro: Light Layer')\n"
2015-09-28 06:16:41 +00:00
"bpy.types.Lamp.retro_origtype = bpy.props.IntProperty(name='Retro: Original Type')\n"
2015-09-20 06:09:27 +00:00
"\n";
/* One shared material set for all meshes */
os << "# Materials\n"
"materials = []\n"
"\n";
MaterialSet matSet;
atUint64 secStart = drs.position();
matSet.read(drs);
matSet.readToBlender(os, pakRouter, entry, 0);
2016-03-04 23:04:53 +00:00
drs.seek(secStart + head.secSizes[0], athena::Begin);
2015-09-20 06:09:27 +00:00
std::vector<DNACMDL::VertexAttributes> vertAttribs;
DNACMDL::GetVertexAttributes(matSet, vertAttribs);
/* Read mesh info */
atUint32 curSec = 1;
std::vector<atUint32> surfaceCounts;
surfaceCounts.reserve(head.meshCount);
2015-10-12 04:41:28 +00:00
for (atUint32 m=0 ; m<head.meshCount ; ++m)
2015-09-20 06:09:27 +00:00
{
2015-09-23 01:15:11 +00:00
/* Mesh header */
MeshHeader mHeader;
secStart = drs.position();
mHeader.read(drs);
2016-03-04 23:04:53 +00:00
drs.seek(secStart + head.secSizes[curSec++], athena::Begin);
2015-09-20 06:09:27 +00:00
2015-09-23 01:15:11 +00:00
/* Surface count from here */
secStart = drs.position();
surfaceCounts.push_back(drs.readUint32Big());
2016-03-04 23:04:53 +00:00
drs.seek(secStart + head.secSizes[curSec++], athena::Begin);
2015-09-20 06:09:27 +00:00
2015-09-23 01:15:11 +00:00
/* Seek through AROT-relation sections */
2016-03-04 23:04:53 +00:00
drs.seek(head.secSizes[curSec++], athena::Current);
drs.seek(head.secSizes[curSec++], athena::Current);
2015-09-20 06:09:27 +00:00
}
2015-09-23 01:15:11 +00:00
/* Skip though WOBJs */
auto secIdxIt = drs.beginSecIdxs();
while (secIdxIt->first == FOURCC('WOBJ'))
++secIdxIt;
2015-09-20 06:09:27 +00:00
/* Skip AROT */
if (secIdxIt->first == FOURCC('ROCT'))
{
2016-03-04 23:04:53 +00:00
drs.seek(head.secSizes[curSec++], athena::Current);
2015-09-20 06:09:27 +00:00
++secIdxIt;
}
/* Skip AABB */
if (secIdxIt->first == FOURCC('AABB'))
{
2016-03-04 23:04:53 +00:00
drs.seek(head.secSizes[curSec++], athena::Current);
2015-09-20 06:09:27 +00:00
++secIdxIt;
}
/* Now the meshes themselves */
if (secIdxIt->first == FOURCC('GPUD'))
{
2015-10-12 04:41:28 +00:00
for (atUint32 m=0 ; m<head.meshCount ; ++m)
2015-09-20 06:09:27 +00:00
{
2015-09-26 03:12:08 +00:00
curSec += DNACMDL::ReadGeomSectionsToBlender<PAKRouter<PAKBridge>, MaterialSet, RigPair, DNACMDL::SurfaceHeader_3>
2015-09-20 06:09:27 +00:00
(os, drs, pakRouter, entry, dummy, true,
false, vertAttribs, m, head.secCount, 0, &head.secSizes[curSec], surfaceCounts[m]);
}
++secIdxIt;
}
2015-09-23 01:15:11 +00:00
/* Skip DEPS */
if (secIdxIt->first == FOURCC('DEPS'))
{
2016-03-04 23:04:53 +00:00
drs.seek(head.secSizes[curSec++], athena::Current);
2015-09-23 01:15:11 +00:00
++secIdxIt;
}
2015-09-20 06:09:27 +00:00
2015-09-23 01:15:11 +00:00
/* Skip SOBJ (SCLY) */
if (secIdxIt->first == FOURCC('SOBJ'))
{
2015-10-12 04:41:28 +00:00
for (atUint32 l=0 ; l<head.sclyLayerCount ; ++l)
2016-03-04 23:04:53 +00:00
drs.seek(head.secSizes[curSec++], athena::Current);
2015-09-23 01:15:11 +00:00
++secIdxIt;
}
2015-09-20 06:09:27 +00:00
2015-09-23 01:15:11 +00:00
/* Skip SGEN */
if (secIdxIt->first == FOURCC('SGEN'))
{
2016-03-04 23:04:53 +00:00
drs.seek(head.secSizes[curSec++], athena::Current);
2015-09-23 01:15:11 +00:00
++secIdxIt;
}
2015-09-20 06:09:27 +00:00
2015-09-23 01:15:11 +00:00
/* Read Collision Meshes */
if (secIdxIt->first == FOURCC('COLI'))
{
DNAMP2::DeafBabe collision;
secStart = drs.position();
collision.read(drs);
DNAMP2::DeafBabe::BlenderInit(os);
collision.sendToBlender(os);
2016-03-04 23:04:53 +00:00
drs.seek(secStart + head.secSizes[curSec++], athena::Begin);
2015-09-23 01:15:11 +00:00
++secIdxIt;
}
2015-09-20 06:09:27 +00:00
/* Read BABEDEAD Lights as Cycles emissives */
2015-09-23 01:15:11 +00:00
if (secIdxIt->first == FOURCC('LITE'))
{
secStart = drs.position();
ReadBabeDeadToBlender_3(os, drs);
2016-03-04 23:04:53 +00:00
drs.seek(secStart + head.secSizes[curSec++], athena::Begin);
2015-09-23 01:15:11 +00:00
++secIdxIt;
}
2015-09-20 06:09:27 +00:00
/* Origins to center of mass */
os << "bpy.context.scene.layers[1] = True\n"
"bpy.ops.object.select_by_type(type='MESH')\n"
"bpy.ops.object.origin_set(type='ORIGIN_CENTER_OF_MASS')\n"
"bpy.ops.object.select_all(action='DESELECT')\n"
"bpy.context.scene.layers[1] = False\n";
os.centerView();
2015-09-20 06:09:27 +00:00
os.close();
return conn.saveBlend();
}
bool MREA::ExtractLayerDeps(PAKEntryReadStream& rs, PAKBridge::Level::Area& areaOut)
2015-09-23 06:35:07 +00:00
{
/* Do extract */
Header head;
head.read(rs);
rs.seekAlign32();
/* MREA decompression stream */
StreamReader drs(rs, head.compressedBlockCount, head.secIndexCount);
for (const std::pair<DNAFourCC, atUint32>& idx : drs.m_secIdxs)
{
if (idx.first == FOURCC('DEPS'))
{
2016-03-04 23:04:53 +00:00
drs.seek(head.getSecOffset(idx.second), athena::Begin);
2015-09-23 06:35:07 +00:00
DEPS deps;
deps.read(drs);
unsigned r=0;
for (unsigned l=1 ; l<deps.depLayerCount ; ++l)
{
2015-09-24 03:10:59 +00:00
if (l > areaOut.layers.size())
break;
PAKBridge::Level::Area::Layer& layer = areaOut.layers.at(l-1);
2015-09-23 06:35:07 +00:00
layer.resources.reserve(deps.depLayers[l] - r);
for (; r<deps.depLayers[l] ; ++r)
layer.resources.emplace(deps.deps[r].id);
}
2015-10-26 02:31:09 +00:00
areaOut.resources.reserve(deps.depCount - r + 2);
2015-09-23 06:35:07 +00:00
for (; r<deps.depCount ; ++r)
areaOut.resources.emplace(deps.deps[r].id);
return true;
}
}
return false;
}
2015-09-20 06:09:27 +00:00
}
}