metaforce/DataSpec/DNAMP1/MLVL.cpp

425 lines
17 KiB
C++
Raw Normal View History

2016-10-01 23:20:20 +00:00
#include "MLVL.hpp"
#include "SCLY.hpp"
2016-10-02 22:41:36 +00:00
#include "SAVW.hpp"
#include "SCAN.hpp"
#include "ScriptObjects/MemoryRelay.hpp"
#include "ScriptObjects/SpecialFunction.hpp"
#include "ScriptObjects/DoorArea.hpp"
#include "Runtime/RetroTypes.hpp"
2017-01-24 07:41:33 +00:00
#include "Runtime/World/ScriptObjectSupport.hpp"
2016-10-01 23:20:20 +00:00
namespace DataSpec
{
namespace DNAMP1
{
2017-01-31 11:21:45 +00:00
bool MLVL::Extract(const SpecBase& dataSpec, PAKEntryReadStream& rs, const hecl::ProjectPath& outPath,
PAKRouter<PAKBridge>& pakRouter, const PAK::Entry& entry, bool force, hecl::BlenderToken& btok,
2016-10-01 23:20:20 +00:00
std::function<void(const hecl::SystemChar*)> fileChanged)
{
MLVL mlvl;
mlvl.read(rs);
2017-01-31 11:21:45 +00:00
const nod::Node* node;
const typename PAKRouter<PAKBridge>::EntryType* texEntry = pakRouter.lookupEntry(mlvl.saveWorldId, &node);
hecl::ProjectPath savwPath = pakRouter.getWorking(texEntry);
SAVW savw;
if (!savwPath.isNone())
{
savwPath.makeDirChain(false);
PAKEntryReadStream rs = texEntry->beginReadStream(*node);
savw.read(rs);
}
2016-10-02 22:41:36 +00:00
2017-01-31 11:21:45 +00:00
atUint32 areaIdx = 0;
2016-10-02 22:41:36 +00:00
for (const MLVL::Area& area : mlvl.areas)
{
hecl::ProjectPath areaDir = pakRouter.getWorking(area.areaMREAId).getParentPath();
2017-01-31 11:21:45 +00:00
{
athena::io::FileWriter fw(hecl::ProjectPath(areaDir, _S("!memoryid.yaml")).getAbsolutePath());
athena::io::YAMLDocWriter w(nullptr);
w.writeUint32("memoryid", area.areaId);
w.finish(&fw);
}
{
athena::io::FileWriter fw(hecl::ProjectPath(areaDir, _S("!memoryrelays.yaml")).getAbsolutePath());
athena::io::YAMLDocWriter w(nullptr);
std::vector<atUint32> relayIds;
for (const atUint32& relay : savw.relays)
{
atUint16 aIdx = ((relay >> 16) & 0x3ff);
if (aIdx == areaIdx && std::find(relayIds.begin(), relayIds.end(), relay) == relayIds.end())
relayIds.push_back(relay);
}
w.enumerate<atUint32>("memrelays", relayIds);
w.finish(&fw);
}
areaIdx++;
2016-10-02 22:41:36 +00:00
}
2016-10-01 23:20:20 +00:00
athena::io::FileWriter writer(outPath.getWithExtension(_S(".yaml"), true).getAbsolutePath());
mlvl.toYAMLStream(writer, static_cast<YAMLWriteMemberFn>(&MLVL::writeMeta));
hecl::BlenderConnection& conn = btok.getBlenderConnection();
2017-01-31 11:21:45 +00:00
return DNAMLVL::ReadMLVLToBlender(conn, mlvl, outPath, pakRouter, entry, force, fileChanged);
2016-10-01 23:20:20 +00:00
}
2017-01-31 11:21:45 +00:00
bool MLVL::Cook(const hecl::ProjectPath& outPath, const hecl::ProjectPath& inPath, const World& wld,
hecl::BlenderToken& btok)
2016-10-01 23:20:20 +00:00
{
MLVL mlvl = {};
athena::io::FileReader reader(inPath.getWithExtension(_S(".yaml"), true).getAbsolutePath());
mlvl.fromYAMLStream(reader, static_cast<YAMLReadMemberFn>(&MLVL::readMeta));
mlvl.magic = 0xDEAFBABE;
mlvl.version = 0x11;
2016-10-08 03:41:08 +00:00
hecl::ProjectPath namePath(inPath.getParentPath(), _S("!name.yaml"));
if (namePath.isFile())
mlvl.worldNameId = namePath;
hecl::ProjectPath savwPath = inPath.ensureAuxInfo(_S("SAVW"));
2016-10-02 22:41:36 +00:00
mlvl.saveWorldId = savwPath;
2016-10-08 03:41:08 +00:00
hecl::ProjectPath mapwPath = inPath.ensureAuxInfo(_S("MAPW"));
2016-10-02 22:41:36 +00:00
mlvl.worldMap = mapwPath;
std::vector<urde::SObjectTag> mapaTags;
mapaTags.reserve(wld.areas.size());
SAVW savw = {};
savw.header.magic = 0xC001D00D;
savw.header.version = 0x3;
2016-12-26 07:58:44 +00:00
std::unordered_set<UniqueID32> addedScans;
2016-10-01 23:20:20 +00:00
size_t areaIdx = 0;
2016-10-02 22:41:36 +00:00
size_t nameOffset = 0;
2016-10-01 23:20:20 +00:00
for (const World::Area& area : wld.areas)
{
if (area.path.getPathType() != hecl::ProjectPath::Type::Directory)
continue;
2016-10-02 22:41:36 +00:00
hecl::ProjectPath areaPath(area.path, _S("/!area.blend"));
if (!areaPath.isFile())
continue;
2017-01-31 11:21:45 +00:00
hecl::ProjectPath memRelayPath(area.path, _S("/!memoryrelays.yaml"));
std::vector<atUint32> memRelays;
if (memRelayPath.isFile())
{
athena::io::FileReader fr(memRelayPath.getAbsolutePath());
athena::io::YAMLDocReader r;
if (r.parse(&fr))
r.enumerate<atUint32>("memrelays", memRelays);
}
savw.relays.insert(savw.relays.end(), memRelays.begin(), memRelays.end());
/* Bare minimum we'll need exactly the same number of links as relays */
std::vector<MemRelayLink> memRelayLinks(memRelays.size());
hecl::DirectoryEnumerator dEnum(area.path.getAbsolutePath(), hecl::DirectoryEnumerator::Mode::DirsSorted);
2016-10-01 23:20:20 +00:00
bool areaInit = false;
2016-10-02 22:41:36 +00:00
size_t layerIdx = 0;
2016-10-01 23:20:20 +00:00
for (const hecl::DirectoryEnumerator::Entry& e : dEnum)
{
hecl::SystemString layerName;
hecl::SystemChar* endCh = nullptr;
hecl::StrToUl(e.m_name.c_str(), &endCh, 0);
if (!endCh)
2016-10-02 22:41:36 +00:00
layerName = hecl::StringUtils::TrimWhitespace(e.m_name);
2016-10-01 23:20:20 +00:00
else
layerName = hecl::StringUtils::TrimWhitespace(hecl::SystemString(endCh));
hecl::ProjectPath objectsPath(area.path, e.m_name + _S("/!objects.yaml"));
if (objectsPath.isNone())
continue;
SCLY::ScriptLayer layer;
{
athena::io::FileReader freader(objectsPath.getAbsolutePath());
if (!freader.isOpen())
continue;
if (!BigYAML::ValidateFromYAMLStream<DNAMP1::SCLY::ScriptLayer>(freader))
continue;
athena::io::YAMLDocReader reader;
if (!reader.parse(&freader))
continue;
layer.read(reader);
}
2016-10-02 22:41:36 +00:00
/* Set active flag state */
2016-10-01 23:20:20 +00:00
hecl::ProjectPath defActivePath(area.path, e.m_name + _S("/!defaultactive"));
bool active = defActivePath.isNone() ? false : true;
if (!areaInit)
{
2016-10-02 22:41:36 +00:00
/* Finish last area */
2016-10-08 03:41:08 +00:00
mlvl.finishLastArea();
2016-10-02 22:41:36 +00:00
/* Area map */
hecl::ProjectPath mapPath(area.path, _S("/!map.blend"));
if (mapPath.isFile())
2016-12-25 01:36:42 +00:00
mapaTags.push_back(g_curSpec->BuildTagFromPath(mapPath, btok));
2016-10-02 22:41:36 +00:00
/* Populate area record */
2016-10-01 23:20:20 +00:00
mlvl.areas.emplace_back();
MLVL::Area& areaOut = mlvl.areas.back();
2016-10-02 22:41:36 +00:00
hecl::ProjectPath namePath(area.path, _S("/!name.yaml"));
if (namePath.isFile())
areaOut.areaNameId = namePath;
areaOut.transformMtx[0] = area.transform[0];
areaOut.transformMtx[1] = area.transform[1];
areaOut.transformMtx[2] = area.transform[2];
areaOut.aabb[0] = area.aabb[0];
areaOut.aabb[1] = area.aabb[1];
areaOut.areaMREAId = areaPath;
areaOut.areaId = 0xffffffff;
hecl::ProjectPath memIdPath(area.path, _S("/!memoryid.yaml"));
2016-10-08 03:41:08 +00:00
if (memIdPath.isFile())
2016-10-02 22:41:36 +00:00
{
athena::io::FileReader fr(memIdPath.getAbsolutePath());
athena::io::YAMLDocReader r;
if (r.parse(&fr))
areaOut.areaId = r.readUint32("memoryid");
}
/* Attached Areas and Docks */
{
std::unordered_set<uint32_t> addedAreas;
areaOut.dockCount = area.docks.size();
for (const World::Area::Dock& dock : area.docks)
{
areaOut.docks.emplace_back();
MLVL::Area::Dock& dockOut = areaOut.docks.back();
2016-10-08 03:41:08 +00:00
if (dock.targetArea != -1 && dock.targetDock != -1)
{
dockOut.endpointCount = 1;
dockOut.endpoints.emplace_back();
MLVL::Area::Dock::Endpoint& ep = dockOut.endpoints.back();
ep.areaIdx = dock.targetArea;
ep.dockIdx = dock.targetDock;
if (addedAreas.find(dock.targetArea) == addedAreas.cend())
{
addedAreas.insert(dock.targetArea);
areaOut.attachedAreas.push_back(dock.targetArea);
}
}
else
{
dockOut.endpointCount = 0;
}
2016-10-02 22:41:36 +00:00
dockOut.planeVertCount = 4;
dockOut.planeVerts.push_back(dock.verts[0]);
dockOut.planeVerts.push_back(dock.verts[1]);
dockOut.planeVerts.push_back(dock.verts[2]);
dockOut.planeVerts.push_back(dock.verts[3]);
}
areaOut.attachedAreaCount = areaOut.attachedAreas.size();
}
/* Layer flags */
mlvl.layerFlags.emplace_back();
mlvl.layerFlags.back().layerCount = 0;
mlvl.layerFlags.back().flags = ~0;
/* Layer name offset */
mlvl.layerNameOffsets.push_back(nameOffset);
2016-10-01 23:20:20 +00:00
areaInit = true;
}
2016-10-02 22:41:36 +00:00
MLVL::Area& areaOut = mlvl.areas.back();
areaOut.depLayers.push_back(areaOut.deps.size());
2017-01-31 11:21:45 +00:00
2016-10-02 22:41:36 +00:00
/* Gather memory relays, scans, and dependencies */
{
2016-12-25 01:36:42 +00:00
g_ThreadBlenderToken.reset(&btok);
2016-10-02 22:41:36 +00:00
std::vector<hecl::ProjectPath> depPaths;
std::vector<Scan> scans;
for (std::unique_ptr<IScriptObject>& obj : layer.objects)
{
2017-01-24 07:41:33 +00:00
if (obj->type == int(urde::EScriptObjectType::MemoryRelay))
2016-10-02 22:41:36 +00:00
{
MemoryRelay& memRelay = static_cast<MemoryRelay&>(*obj);
for (IScriptObject::Connection& conn : memRelay.connections)
{
2017-01-31 11:21:45 +00:00
MemRelayLink linkOut;
2016-10-02 22:41:36 +00:00
linkOut.memRelayId = memRelay.id;
linkOut.targetId = conn.target;
linkOut.msg = conn.msg;
linkOut.active = memRelay.active;
2017-01-31 11:21:45 +00:00
auto iter = std::find(memRelays.begin(), memRelays.end(), linkOut.memRelayId);
if (iter == memRelays.end())
{
/* We must have a new relay, let's track it */
memRelayLinks.push_back(linkOut);
savw.relays.push_back(memRelay.id);
memRelays.push_back(memRelay.id);
}
else /* Lets insert this in it's appropriate location, target order doesn't matter */
{
atUint32 idx = iter - memRelays.begin();
if (idx >= memRelayLinks.size())
memRelayLinks.push_back(linkOut);
else
memRelayLinks.insert(memRelayLinks.begin() + idx, linkOut);
}
2016-10-02 22:41:36 +00:00
}
}
2017-01-24 07:41:33 +00:00
else if (obj->type == int(urde::EScriptObjectType::SpecialFunction))
2016-10-02 22:41:36 +00:00
{
SpecialFunction& specialFunc = static_cast<SpecialFunction&>(*obj);
2016-12-25 01:36:42 +00:00
if (specialFunc.function == ESpecialFunctionType::CinematicSkip)
2016-10-02 22:41:36 +00:00
savw.skippableCutscenes.push_back(specialFunc.id);
2016-12-25 01:36:42 +00:00
else if (specialFunc.function == ESpecialFunctionType::ScriptLayerController)
2016-10-02 22:41:36 +00:00
{
savw.layers.emplace_back();
SAVWCommon::Layer& layer = savw.layers.back();
layer.areaId = specialFunc.layerSwitch.area;
layer.layer = specialFunc.layerSwitch.layerIdx;
}
}
2017-01-24 07:41:33 +00:00
else if (obj->type == int(urde::EScriptObjectType::Door))
2016-10-02 22:41:36 +00:00
{
DoorArea& doorArea = static_cast<DoorArea&>(*obj);
savw.doors.push_back(doorArea.id);
}
obj->gatherDependencies(depPaths);
obj->gatherScans(scans);
}
/* Cull duplicate paths and add typed hash to list */
std::unordered_set<hecl::Hash> addedPaths;
for (const hecl::ProjectPath& path : depPaths)
{
if (addedPaths.find(path.hash()) == addedPaths.cend())
{
addedPaths.insert(path.hash());
2016-12-25 01:36:42 +00:00
urde::SObjectTag tag = g_curSpec->BuildTagFromPath(path, btok);
2016-10-02 22:41:36 +00:00
areaOut.deps.emplace_back(tag.id, tag.type);
}
}
/* Cull duplicate scans and add to list */
for (const Scan& scan : scans)
{
2016-10-08 03:41:08 +00:00
if (!scan.scanId)
continue;
2016-10-02 22:41:36 +00:00
if (addedScans.find(scan.scanId) == addedScans.cend())
{
addedScans.insert(scan.scanId);
hecl::ProjectPath scanPath = UniqueIDBridge::TranslatePakIdToPath(scan.scanId);
savw.scans.emplace_back();
Scan& scanOut = savw.scans.back();
scanOut.scanId = scan.scanId;
scanOut.category = SAVWCommon::EScanCategory(SCAN::GetCategory(scanPath));
}
}
}
hecl::SystemUTF8View layerU8(layerName);
mlvl.layerNames.push_back(layerU8.str());
nameOffset += layerName.size() + 1;
MLVL::LayerFlags& thisLayFlags = mlvl.layerFlags.back();
++thisLayFlags.layerCount;
if (!active)
thisLayFlags.flags &= ~(1 << layerIdx);
++layerIdx;
2016-10-01 23:20:20 +00:00
}
2016-10-08 03:41:08 +00:00
2017-01-31 11:21:45 +00:00
/* Append Memory Relays */
mlvl.memRelayLinks.insert(mlvl.memRelayLinks.end(), memRelayLinks.begin(), memRelayLinks.end());
2016-10-08 03:41:08 +00:00
/* Cull duplicate area paths and add typed hash to list */
2017-01-19 09:03:43 +00:00
auto& conn = btok.getBlenderConnection();
2016-10-08 03:41:08 +00:00
if (conn.openBlend(areaPath))
{
MLVL::Area& areaOut = mlvl.areas.back();
areaOut.depLayers.push_back(areaOut.deps.size());
auto ds = conn.beginData();
std::vector<hecl::ProjectPath> texs = ds.getTextures();
ds.close();
std::unordered_set<hecl::Hash> addedPaths;
for (const hecl::ProjectPath& path : texs)
{
if (addedPaths.find(path.hash()) == addedPaths.cend())
{
addedPaths.insert(path.hash());
2016-12-25 01:36:42 +00:00
urde::SObjectTag tag = g_curSpec->BuildTagFromPath(path, btok);
2016-10-08 03:41:08 +00:00
areaOut.deps.emplace_back(tag.id, tag.type);
}
}
2016-12-25 01:36:42 +00:00
urde::SObjectTag tag = g_curSpec->BuildTagFromPath(areaPath, btok);
2016-10-08 03:41:08 +00:00
areaOut.deps.emplace_back(tag.id, tag.type);
}
2016-10-01 23:20:20 +00:00
++areaIdx;
}
2016-10-02 22:41:36 +00:00
2016-10-08 03:41:08 +00:00
/* Finish last area */
mlvl.finishLastArea();
2016-10-01 23:20:20 +00:00
mlvl.memRelayLinkCount = mlvl.memRelayLinks.size();
mlvl.areaCount = mlvl.areas.size();
2016-10-02 22:41:36 +00:00
mlvl.layerFlagCount = mlvl.layerFlags.size();
mlvl.layerNameCount = mlvl.layerNames.size();
mlvl.layerNameOffsetCount = mlvl.layerNameOffsets.size();
/* Write out */
{
athena::io::FileWriter fo(outPath.getAbsolutePath());
mlvl.write(fo);
}
/* Write out MAPW */
{
2017-01-31 11:21:45 +00:00
hecl::ProjectPath mapwCooked = mapwPath.getCookedPath(*g_curSpec->overrideDataSpec(mapwPath, nullptr, btok));
2016-10-08 03:41:08 +00:00
mapwCooked.makeDirChain(false);
2016-10-02 22:41:36 +00:00
athena::io::FileWriter fo(mapwCooked.getAbsolutePath());
fo.writeUint32Big(0xDEADF00D);
fo.writeUint32Big(1);
fo.writeUint32Big(mapaTags.size());
for (const urde::SObjectTag& mapa : mapaTags)
fo.writeUint32Big(mapa.id);
}
/* Write out SAVW */
{
savw.header.areaCount = mlvl.areaCount;
savw.skippableCutsceneCount = savw.skippableCutscenes.size();
savw.relayCount = savw.relays.size();
savw.layerCount = savw.layers.size();
savw.doorCount = savw.doors.size();
savw.scanCount = savw.scans.size();
2017-01-31 11:21:45 +00:00
hecl::ProjectPath savwCooked = savwPath.getCookedPath(*g_curSpec->overrideDataSpec(savwPath, nullptr, btok));
2016-10-08 03:41:08 +00:00
savwCooked.makeDirChain(false);
2016-10-02 22:41:36 +00:00
athena::io::FileWriter fo(savwCooked.getAbsolutePath());
savw.write(fo);
2017-02-06 03:32:06 +00:00
int64_t rem = fo.position() % 32;
if (rem)
for (int64_t i=0 ; i<32-rem ; ++i)
fo.writeBytes((atInt8*)"\xff", 1);
2016-10-02 22:41:36 +00:00
}
2016-10-01 23:20:20 +00:00
2016-10-02 22:41:36 +00:00
return true;
2016-10-01 23:20:20 +00:00
}
}
}