metaforce/DataSpec/DNAMP1/CSNG.cpp

103 lines
3.1 KiB
C++
Raw Normal View History

2016-09-18 23:47:48 +00:00
#include "CSNG.hpp"
#include "amuse/SongConverter.hpp"
2017-12-29 08:08:12 +00:00
namespace DataSpec::DNAMP1
2016-09-18 23:47:48 +00:00
{
bool CSNG::Extract(PAKEntryReadStream& rs, const hecl::ProjectPath& outPath)
{
2018-10-14 20:16:21 +00:00
hecl::ProjectPath midPath = outPath.getWithExtension(_SYS_STR(".mid"), true);
hecl::ProjectPath yamlPath = outPath.getWithExtension(_SYS_STR(".yaml"), true);
2016-09-18 23:47:48 +00:00
Header head;
head.read(rs);
{
athena::io::YAMLDocWriter dw("CSNG");
dw.writeUint32("midiSetupId", head.midiSetupId);
dw.writeUint32("songGroupId", head.songGroupId);
2017-02-12 23:56:03 +00:00
if (auto rec = dw.enterSubRecord("agscId"))
head.agscId.write(dw);
2016-09-18 23:47:48 +00:00
athena::io::FileWriter w(yamlPath.getAbsolutePath());
if (w.hasError())
return false;
dw.finish(&w);
}
{
auto sng = rs.readUBytes(head.sngLength);
int version;
bool isBig;
auto midi = amuse::SongConverter::SongToMIDI(sng.get(), version, isBig);
athena::io::FileWriter w(midPath.getAbsolutePath());
if (w.hasError())
return false;
w.writeUBytes(midi.data(), midi.size());
}
/* Update !songs.yaml for Amuse editor */
2018-10-14 20:16:21 +00:00
hecl::ProjectPath audGrp(outPath.getParentPath().getParentPath(), _SYS_STR("AudioGrp"));
audGrp.makeDirChain(true);
2018-10-14 20:16:21 +00:00
hecl::ProjectPath songsPath(audGrp, _SYS_STR("!songs.yaml"));
std::experimental::optional<athena::io::FileReader> r;
if (songsPath.isFile())
r.emplace(songsPath.getAbsolutePath());
athena::io::YAMLDocWriter ydw("amuse::Songs", r ? &*r : nullptr);
r = std::experimental::nullopt;
char id[16];
snprintf(id, 16, "%04X", head.midiSetupId);
ydw.writeString(id, hecl::Format("../MidiData/%s", midPath.getLastComponentUTF8().data()));
athena::io::FileWriter w(songsPath.getAbsolutePath());
ydw.finish(&w);
2016-09-18 23:47:48 +00:00
return true;
}
bool CSNG::Cook(const hecl::ProjectPath& inPath, const hecl::ProjectPath& outPath)
{
2018-10-14 20:16:21 +00:00
hecl::ProjectPath midPath = inPath.getWithExtension(_SYS_STR(".mid"), true);
hecl::ProjectPath yamlPath = inPath.getWithExtension(_SYS_STR(".yaml"), true);
2016-09-18 23:47:48 +00:00
std::vector<uint8_t> sngData;
{
athena::io::FileReader midR(midPath.getAbsolutePath());
if (midR.hasError())
return false;
uint32_t midLen = midR.length();
std::vector<uint8_t> midData;
midData.resize(midLen);
midR.readUBytesToBuf(midData.data(), midLen);
sngData = amuse::SongConverter::MIDIToSong(midData, 1, true);
}
athena::io::FileWriter w(outPath.getAbsolutePath());
if (w.hasError())
return false;
{
athena::io::FileReader yamlR(yamlPath.getAbsolutePath());
if (yamlR.hasError())
return false;
athena::io::YAMLDocReader dr;
if (!dr.parse(&yamlR))
return false;
Header head;
head.midiSetupId = dr.readUint32("midiSetupId");
head.songGroupId = dr.readUint32("songGroupId");
2017-02-12 23:56:03 +00:00
if (auto rec = dr.enterSubRecord("agscId"))
head.agscId.read(dr);
2016-09-18 23:47:48 +00:00
head.sngLength = sngData.size();
head.write(w);
}
w.writeUBytes(sngData.data(), sngData.size());
return true;
}
}