2022-07-11 00:16:20 -04:00
|
|
|
#include "interleaf.h"
|
|
|
|
|
2022-07-17 19:25:06 -04:00
|
|
|
#include <cmath>
|
2022-07-11 04:48:20 -04:00
|
|
|
#include <iostream>
|
2022-07-18 03:27:00 -04:00
|
|
|
#include <sstream>
|
2022-07-11 04:48:20 -04:00
|
|
|
|
|
|
|
#include "object.h"
|
2022-07-17 19:25:06 -04:00
|
|
|
#include "othertypes.h"
|
2022-07-17 21:51:16 -04:00
|
|
|
#include "sitypes.h"
|
|
|
|
#include "util.h"
|
2022-07-11 04:48:20 -04:00
|
|
|
|
2022-07-11 00:16:20 -04:00
|
|
|
namespace si {
|
|
|
|
|
2022-07-18 03:27:00 -04:00
|
|
|
static const uint32_t kMinimumChunkSize = 8;
|
|
|
|
|
2022-07-11 00:16:20 -04:00
|
|
|
Interleaf::Interleaf()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-07-17 21:51:16 -04:00
|
|
|
void Interleaf::Clear()
|
|
|
|
{
|
2022-07-18 03:27:00 -04:00
|
|
|
m_Info.clear();
|
2022-07-17 21:51:16 -04:00
|
|
|
m_BufferSize = 0;
|
2022-07-18 05:12:22 -04:00
|
|
|
m_JoiningProgress = 0;
|
|
|
|
m_JoiningSize = 0;
|
2022-07-18 03:27:00 -04:00
|
|
|
m_ObjectIDTable.clear();
|
|
|
|
m_ObjectList.clear();
|
2022-07-17 21:51:16 -04:00
|
|
|
DeleteChildren();
|
|
|
|
}
|
|
|
|
|
2022-07-18 03:27:00 -04:00
|
|
|
Interleaf::Error Interleaf::Read(const char *f)
|
2022-07-17 21:51:16 -04:00
|
|
|
{
|
|
|
|
std::ifstream is(f);
|
|
|
|
if (!is.is_open() || !is.good()) {
|
2022-07-18 03:27:00 -04:00
|
|
|
return ERROR_IO;
|
2022-07-17 21:51:16 -04:00
|
|
|
}
|
|
|
|
return Read(is);
|
|
|
|
}
|
|
|
|
|
2022-07-18 03:27:00 -04:00
|
|
|
Interleaf::Error Interleaf::Write(const char *f) const
|
2022-07-17 21:51:16 -04:00
|
|
|
{
|
2022-07-18 03:27:00 -04:00
|
|
|
std::ofstream os(f);
|
|
|
|
if (!os.is_open() || !os.good()) {
|
|
|
|
return ERROR_IO;
|
2022-07-17 21:51:16 -04:00
|
|
|
}
|
2022-07-18 03:27:00 -04:00
|
|
|
return Write(os);
|
2022-07-17 21:51:16 -04:00
|
|
|
}
|
|
|
|
|
2022-07-18 03:27:00 -04:00
|
|
|
#ifdef _WIN32
|
|
|
|
Interleaf::Error Interleaf::Read(const wchar_t *f)
|
2022-07-17 21:51:16 -04:00
|
|
|
{
|
2022-07-18 12:49:27 -04:00
|
|
|
std::ifstream is(f);
|
2022-07-18 03:27:00 -04:00
|
|
|
if (!is.is_open() || !is.good()) {
|
2022-07-18 12:49:27 -04:00
|
|
|
return ERROR_IO;
|
2022-07-17 21:51:16 -04:00
|
|
|
}
|
2022-07-18 03:27:00 -04:00
|
|
|
return Read(is);
|
2022-07-17 21:51:16 -04:00
|
|
|
}
|
|
|
|
|
2022-07-18 03:27:00 -04:00
|
|
|
Interleaf::Error Interleaf::Write(const wchar_t *f) const
|
2022-07-17 21:51:16 -04:00
|
|
|
{
|
2022-07-18 12:49:27 -04:00
|
|
|
std::ofstream os(f);
|
2022-07-17 21:51:16 -04:00
|
|
|
if (!os.is_open() || !os.good()) {
|
2022-07-18 12:49:27 -04:00
|
|
|
return ERROR_IO;
|
2022-07-17 21:51:16 -04:00
|
|
|
}
|
|
|
|
return Write(os);
|
|
|
|
}
|
2022-07-18 03:27:00 -04:00
|
|
|
#endif
|
2022-07-17 21:51:16 -04:00
|
|
|
|
2022-07-18 03:27:00 -04:00
|
|
|
Interleaf::Error Interleaf::ReadChunk(Core *parent, std::istream &is, Info *info)
|
2022-07-17 21:51:16 -04:00
|
|
|
{
|
|
|
|
uint32_t offset = is.tellg();
|
|
|
|
uint32_t id = ReadU32(is);
|
|
|
|
uint32_t size = ReadU32(is);
|
|
|
|
uint32_t end = uint32_t(is.tellg()) + size;
|
|
|
|
|
2022-07-18 03:27:00 -04:00
|
|
|
info->SetType(id);
|
|
|
|
info->SetOffset(offset);
|
|
|
|
info->SetSize(size);
|
2022-07-17 21:51:16 -04:00
|
|
|
|
2022-07-18 03:27:00 -04:00
|
|
|
std::stringstream desc;
|
|
|
|
|
|
|
|
switch (static_cast<RIFF::Type>(id)) {
|
|
|
|
case RIFF::RIFF_:
|
2022-07-17 21:51:16 -04:00
|
|
|
{
|
|
|
|
// Require RIFF type to be OMNI
|
|
|
|
uint32_t riff_type = ReadU32(is);
|
|
|
|
if (riff_type != RIFF::OMNI) {
|
2022-07-18 03:27:00 -04:00
|
|
|
return ERROR_INVALID_INPUT;
|
2022-07-17 21:51:16 -04:00
|
|
|
}
|
2022-07-18 03:27:00 -04:00
|
|
|
|
|
|
|
desc << "Type: " << RIFF::PrintU32AsString(riff_type);
|
2022-07-17 21:51:16 -04:00
|
|
|
break;
|
|
|
|
}
|
2022-07-18 03:27:00 -04:00
|
|
|
case RIFF::MxHd:
|
2022-07-17 21:51:16 -04:00
|
|
|
{
|
|
|
|
m_Version = ReadU32(is);
|
2022-07-18 03:27:00 -04:00
|
|
|
desc << "Version: " << m_Version << std::endl;
|
2022-07-17 21:51:16 -04:00
|
|
|
|
|
|
|
m_BufferSize = ReadU32(is);
|
2022-07-18 03:27:00 -04:00
|
|
|
desc << "Buffer Size: 0x" << std::hex << m_BufferSize;
|
2022-07-17 21:51:16 -04:00
|
|
|
|
2022-07-18 03:27:00 -04:00
|
|
|
if (m_Version == 0x00020002) {
|
2022-07-17 21:51:16 -04:00
|
|
|
m_BufferCount = ReadU32(is);
|
2022-07-18 03:27:00 -04:00
|
|
|
desc << std::endl << "Buffer Count: " << std::dec << m_BufferCount << std::endl;
|
2022-07-17 21:51:16 -04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2022-07-18 03:27:00 -04:00
|
|
|
case RIFF::pad_:
|
2022-07-17 21:51:16 -04:00
|
|
|
is.seekg(size, std::ios::cur);
|
|
|
|
break;
|
2022-07-18 03:27:00 -04:00
|
|
|
case RIFF::MxOf:
|
2022-07-17 21:51:16 -04:00
|
|
|
{
|
2022-07-18 03:27:00 -04:00
|
|
|
uint32_t offset_count = ReadU32(is);
|
|
|
|
|
|
|
|
desc << "Count: " << offset_count;
|
|
|
|
|
|
|
|
uint32_t real_count = (size - sizeof(uint32_t)) / sizeof(uint32_t);
|
|
|
|
m_ObjectList.resize(real_count);
|
|
|
|
for (uint32_t i = 0; i < real_count; i++) {
|
|
|
|
Object *o = new Object();
|
|
|
|
parent->AppendChild(o);
|
|
|
|
|
|
|
|
uint32_t choffset = ReadU32(is);
|
|
|
|
m_ObjectList[i] = choffset;
|
|
|
|
desc << std::endl << i << ": 0x" << std::hex << choffset;
|
2022-07-17 21:51:16 -04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2022-07-18 03:27:00 -04:00
|
|
|
case RIFF::LIST:
|
2022-07-17 21:51:16 -04:00
|
|
|
{
|
|
|
|
uint32_t list_type = ReadU32(is);
|
2022-07-18 03:27:00 -04:00
|
|
|
desc << "Type: " << RIFF::PrintU32AsString(list_type) << std::endl;
|
2022-07-17 21:51:16 -04:00
|
|
|
uint32_t list_count = 0;
|
2022-07-18 03:27:00 -04:00
|
|
|
if (list_type == RIFF::MxCh) {
|
2022-07-17 21:51:16 -04:00
|
|
|
list_count = ReadU32(is);
|
2022-07-18 03:27:00 -04:00
|
|
|
desc << "Count: " << list_count << std::endl;
|
2022-07-17 21:51:16 -04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2022-07-18 03:27:00 -04:00
|
|
|
case RIFF::MxSt:
|
|
|
|
case RIFF::MxDa:
|
|
|
|
case RIFF::WAVE:
|
|
|
|
case RIFF::fmt_:
|
|
|
|
case RIFF::data:
|
|
|
|
case RIFF::OMNI:
|
2022-07-17 21:51:16 -04:00
|
|
|
// Types with no data
|
|
|
|
break;
|
2022-07-18 03:27:00 -04:00
|
|
|
case RIFF::MxOb:
|
2022-07-17 21:51:16 -04:00
|
|
|
{
|
2022-07-18 03:27:00 -04:00
|
|
|
Object *o = NULL;
|
|
|
|
|
|
|
|
for (size_t i=0; i<m_ObjectList.size(); i++) {
|
|
|
|
if (m_ObjectList[i] == offset-kMinimumChunkSize) {
|
|
|
|
o = static_cast<Object*>(GetChildAt(i));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!o) {
|
|
|
|
o = new Object();
|
|
|
|
parent->AppendChild(o);
|
|
|
|
}
|
|
|
|
|
|
|
|
ReadObject(is, o, desc);
|
|
|
|
|
|
|
|
info->SetObjectID(o->id());
|
|
|
|
|
|
|
|
m_ObjectIDTable[o->id()] = o;
|
|
|
|
|
2022-07-17 21:51:16 -04:00
|
|
|
parent = o;
|
|
|
|
break;
|
|
|
|
}
|
2022-07-18 03:27:00 -04:00
|
|
|
case RIFF::MxCh:
|
2022-07-17 21:51:16 -04:00
|
|
|
{
|
|
|
|
uint16_t flags = ReadU16(is);
|
2022-07-18 03:27:00 -04:00
|
|
|
desc << "Flags: 0x" << std::hex << flags << std::endl;
|
|
|
|
|
2022-07-17 21:51:16 -04:00
|
|
|
uint32_t object = ReadU32(is);
|
2022-07-18 03:27:00 -04:00
|
|
|
desc << "Object: " << std::dec << object << std::endl;
|
|
|
|
|
2022-07-17 21:51:16 -04:00
|
|
|
uint32_t time = ReadU32(is);
|
2022-07-18 03:27:00 -04:00
|
|
|
desc << "Time: " << time << std::endl;
|
|
|
|
|
2022-07-17 21:51:16 -04:00
|
|
|
uint32_t data_sz = ReadU32(is);
|
2022-07-18 03:27:00 -04:00
|
|
|
desc << "Size: " << data_sz << std::endl;
|
|
|
|
|
2022-07-17 21:51:16 -04:00
|
|
|
bytearray data = ReadBytes(is, size - MxCh::HEADER_SIZE);
|
|
|
|
|
2022-07-18 03:27:00 -04:00
|
|
|
info->SetObjectID(object);
|
|
|
|
info->SetData(data);
|
|
|
|
|
|
|
|
if (!(flags & MxCh::FLAG_END)) {
|
|
|
|
Object *o = m_ObjectIDTable.at(object);
|
|
|
|
if (!o) {
|
|
|
|
return ERROR_INVALID_INPUT;
|
|
|
|
}
|
|
|
|
|
2022-07-18 05:12:22 -04:00
|
|
|
if (flags & MxCh::FLAG_SPLIT && m_JoiningSize > 0) {
|
2022-07-18 03:27:00 -04:00
|
|
|
o->data_.back().append(data);
|
2022-07-18 05:12:22 -04:00
|
|
|
|
|
|
|
m_JoiningProgress += data.size();
|
|
|
|
if (m_JoiningProgress == m_JoiningSize) {
|
|
|
|
m_JoiningProgress = 0;
|
|
|
|
m_JoiningSize = 0;
|
|
|
|
}
|
2022-07-18 03:27:00 -04:00
|
|
|
} else {
|
|
|
|
o->data_.push_back(data);
|
2022-07-18 05:12:22 -04:00
|
|
|
|
|
|
|
if (flags & MxCh::FLAG_SPLIT) {
|
|
|
|
m_JoiningProgress = data.size();
|
|
|
|
m_JoiningSize = data_sz;
|
|
|
|
}
|
2022-07-18 03:27:00 -04:00
|
|
|
}
|
|
|
|
break;
|
2022-07-17 21:51:16 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assume any remaining data is this chunk's children
|
2022-07-18 03:27:00 -04:00
|
|
|
while (is.good() && (size_t(is.tellg()) + kMinimumChunkSize) < end) {
|
2022-07-17 21:51:16 -04:00
|
|
|
// Check alignment, if there's not enough room to for another segment, skip ahead
|
|
|
|
if (m_BufferSize > 0) {
|
|
|
|
uint32_t offset_in_buffer = is.tellg()%m_BufferSize;
|
2022-07-18 03:27:00 -04:00
|
|
|
if (offset_in_buffer + kMinimumChunkSize > m_BufferSize) {
|
2022-07-17 21:51:16 -04:00
|
|
|
is.seekg(m_BufferSize-offset_in_buffer, std::ios::cur);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read next child
|
2022-07-18 03:27:00 -04:00
|
|
|
Info *subinfo = new Info();
|
|
|
|
info->AppendChild(subinfo);
|
|
|
|
Error e = ReadChunk(parent, is, subinfo);
|
|
|
|
if (e != ERROR_SUCCESS) {
|
|
|
|
return e;
|
2022-07-17 21:51:16 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-18 03:27:00 -04:00
|
|
|
info->SetDescription(desc.str());
|
|
|
|
|
2022-07-17 21:51:16 -04:00
|
|
|
if (is.tellg() < end) {
|
|
|
|
is.seekg(end, std::ios::beg);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (size%2 == 1) {
|
|
|
|
is.seekg(1, std::ios::cur);
|
|
|
|
}
|
|
|
|
|
2022-07-18 03:27:00 -04:00
|
|
|
return ERROR_SUCCESS;
|
2022-07-17 21:51:16 -04:00
|
|
|
}
|
|
|
|
|
2022-07-18 03:27:00 -04:00
|
|
|
Object *Interleaf::ReadObject(std::istream &is, Object *o, std::stringstream &desc)
|
2022-07-17 21:51:16 -04:00
|
|
|
{
|
|
|
|
o->type_ = static_cast<MxOb::Type>(ReadU16(is));
|
2022-07-18 03:27:00 -04:00
|
|
|
desc << "Type: " << o->type_ << std::endl;
|
2022-07-17 21:51:16 -04:00
|
|
|
o->presenter_ = ReadString(is);
|
2022-07-18 03:27:00 -04:00
|
|
|
desc << "Presenter: " << o->presenter_ << std::endl;
|
2022-07-17 21:51:16 -04:00
|
|
|
o->unknown1_ = ReadU32(is);
|
2022-07-18 03:27:00 -04:00
|
|
|
desc << "Unknown1: " << o->unknown1_ << std::endl;
|
2022-07-17 21:51:16 -04:00
|
|
|
o->name_ = ReadString(is);
|
2022-07-18 03:27:00 -04:00
|
|
|
desc << "Name: " << o->name_ << std::endl;
|
2022-07-17 21:51:16 -04:00
|
|
|
o->id_ = ReadU32(is);
|
2022-07-18 03:27:00 -04:00
|
|
|
desc << "ID: " << o->id_ << std::endl;
|
2022-07-17 21:51:16 -04:00
|
|
|
o->flags_ = ReadU32(is);
|
2022-07-18 03:27:00 -04:00
|
|
|
desc << "Flags: " << o->flags_ << std::endl;
|
2022-07-17 21:51:16 -04:00
|
|
|
o->unknown4_ = ReadU32(is);
|
2022-07-18 03:27:00 -04:00
|
|
|
desc << "Unknown4: " << o->unknown4_ << std::endl;
|
2022-07-17 21:51:16 -04:00
|
|
|
o->duration_ = ReadU32(is);
|
2022-07-18 03:27:00 -04:00
|
|
|
desc << "Duration: " << o->duration_ << std::endl;
|
2022-07-17 21:51:16 -04:00
|
|
|
o->loops_ = ReadU32(is);
|
2022-07-18 03:27:00 -04:00
|
|
|
desc << "Loops: " << o->loops_ << std::endl;
|
2022-07-17 21:51:16 -04:00
|
|
|
o->position_ = ReadVector3(is);
|
2022-07-18 03:27:00 -04:00
|
|
|
desc << "Position: " << o->position_.x << " " << o->position_.y << " " << o->position_.z << std::endl;
|
2022-07-17 21:51:16 -04:00
|
|
|
o->direction_ = ReadVector3(is);
|
2022-07-18 03:27:00 -04:00
|
|
|
desc << "Direction: " << o->direction_.x << " " << o->direction_.y << " " << o->direction_.z << std::endl;
|
2022-07-17 21:51:16 -04:00
|
|
|
o->up_ = ReadVector3(is);
|
2022-07-18 03:27:00 -04:00
|
|
|
desc << "Up: " << o->up_.x << " " << o->up_.y << " " << o->up_.z << std::endl;
|
2022-07-17 21:51:16 -04:00
|
|
|
|
|
|
|
uint16_t extra_sz = ReadU16(is);
|
2022-07-18 03:27:00 -04:00
|
|
|
desc << "Extra Size: " << extra_sz << std::endl;
|
2022-07-17 21:51:16 -04:00
|
|
|
o->extra_ = ReadBytes(is, extra_sz);
|
|
|
|
|
|
|
|
if (o->type_ != MxOb::Presenter && o->type_ != MxOb::World) {
|
|
|
|
o->filename_ = ReadString(is);
|
2022-07-18 03:27:00 -04:00
|
|
|
desc << "Filename: " << o->filename_ << std::endl;
|
2022-07-17 21:51:16 -04:00
|
|
|
o->unknown26_ = ReadU32(is);
|
2022-07-18 03:27:00 -04:00
|
|
|
desc << "Unknown26: " << o->unknown26_ << std::endl;
|
2022-07-17 21:51:16 -04:00
|
|
|
o->unknown27_ = ReadU32(is);
|
2022-07-18 03:27:00 -04:00
|
|
|
desc << "Unknown27: " << o->unknown27_ << std::endl;
|
2022-07-17 21:51:16 -04:00
|
|
|
o->unknown28_ = ReadU32(is);
|
2022-07-18 03:27:00 -04:00
|
|
|
desc << "Unknown28: " << o->unknown28_ << std::endl;
|
2022-07-17 21:51:16 -04:00
|
|
|
o->filetype_ = static_cast<MxOb::FileType>(ReadU32(is));
|
2022-07-18 03:27:00 -04:00
|
|
|
desc << "File Type: " << RIFF::PrintU32AsString(o->filetype_) << std::endl;
|
2022-07-17 21:51:16 -04:00
|
|
|
o->unknown29_ = ReadU32(is);
|
2022-07-18 03:27:00 -04:00
|
|
|
desc << "Unknown29: " << o->unknown29_ << std::endl;
|
2022-07-17 21:51:16 -04:00
|
|
|
o->unknown30_ = ReadU32(is);
|
2022-07-18 03:27:00 -04:00
|
|
|
desc << "Unknown30: " << o->unknown30_ << std::endl;
|
2022-07-17 21:51:16 -04:00
|
|
|
|
|
|
|
if (o->filetype_ == MxOb::WAV) {
|
|
|
|
o->unknown31_ = ReadU32(is);
|
2022-07-18 03:27:00 -04:00
|
|
|
desc << "Unknown31: " << o->unknown31_ << std::endl;
|
2022-07-17 21:51:16 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
2022-07-18 03:27:00 -04:00
|
|
|
Interleaf::Error Interleaf::Read(std::istream &is)
|
2022-07-17 21:51:16 -04:00
|
|
|
{
|
|
|
|
Clear();
|
2022-07-18 03:27:00 -04:00
|
|
|
return ReadChunk(this, is, &m_Info);
|
2022-07-17 21:51:16 -04:00
|
|
|
}
|
|
|
|
|
2022-07-18 03:27:00 -04:00
|
|
|
Interleaf::Error Interleaf::Write(std::ostream &os) const
|
2022-07-11 00:16:20 -04:00
|
|
|
{
|
2022-07-18 03:27:00 -04:00
|
|
|
if (m_BufferSize == 0) {
|
|
|
|
LogError() << "Buffer size must be set to write" << std::endl;
|
|
|
|
return ERROR_INVALID_BUFFER_SIZE;
|
2022-07-11 00:16:20 -04:00
|
|
|
}
|
|
|
|
|
2022-07-18 03:27:00 -04:00
|
|
|
RIFF::Chk riff = RIFF::BeginChunk(os, RIFF::RIFF_);
|
|
|
|
WriteU32(os, RIFF::OMNI);
|
2022-07-11 00:16:20 -04:00
|
|
|
|
2022-07-18 03:27:00 -04:00
|
|
|
std::ios::pos_type offset_table_pos;
|
2022-07-11 00:16:20 -04:00
|
|
|
|
2022-07-18 03:27:00 -04:00
|
|
|
{
|
|
|
|
// MxHd
|
|
|
|
RIFF::Chk mxhd = RIFF::BeginChunk(os, RIFF::MxHd);
|
2022-07-11 00:16:20 -04:00
|
|
|
|
2022-07-18 03:27:00 -04:00
|
|
|
WriteU32(os, m_Version);
|
|
|
|
WriteU32(os, m_BufferSize);
|
2022-07-11 00:16:20 -04:00
|
|
|
|
2022-07-18 03:27:00 -04:00
|
|
|
if (m_Version == 0x00020002) {
|
|
|
|
WriteU32(os, m_BufferCount);
|
2022-07-11 04:48:20 -04:00
|
|
|
}
|
2022-07-18 03:27:00 -04:00
|
|
|
|
|
|
|
RIFF::EndChunk(os, mxhd);
|
2022-07-11 04:48:20 -04:00
|
|
|
}
|
|
|
|
|
2022-07-18 03:27:00 -04:00
|
|
|
{
|
|
|
|
// MxOf
|
|
|
|
RIFF::Chk mxof = RIFF::BeginChunk(os, RIFF::MxOf);
|
2022-07-11 04:48:20 -04:00
|
|
|
|
2022-07-18 03:27:00 -04:00
|
|
|
WriteU32(os, GetChildCount());
|
|
|
|
|
|
|
|
offset_table_pos = os.tellp();
|
|
|
|
|
|
|
|
for (size_t i = 0; i < GetChildCount(); i++) {
|
|
|
|
WriteU32(os, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
RIFF::EndChunk(os, mxof);
|
2022-07-11 17:19:36 -04:00
|
|
|
}
|
|
|
|
|
2022-07-18 03:27:00 -04:00
|
|
|
{
|
|
|
|
// LIST
|
|
|
|
RIFF::Chk list_mxst = RIFF::BeginChunk(os, RIFF::LIST);
|
2022-07-11 17:19:36 -04:00
|
|
|
|
2022-07-18 03:27:00 -04:00
|
|
|
WriteU32(os, RIFF::MxSt);
|
2022-07-11 17:19:36 -04:00
|
|
|
|
2022-07-18 03:27:00 -04:00
|
|
|
for (size_t i = 0; i < GetChildCount(); i++) {
|
|
|
|
Object *child = static_cast<Object*>(GetChildAt(i));
|
2022-07-11 17:19:36 -04:00
|
|
|
|
2022-07-18 03:27:00 -04:00
|
|
|
uint32_t mxst_offset = os.tellp();
|
2022-07-11 17:19:36 -04:00
|
|
|
|
2022-07-18 03:27:00 -04:00
|
|
|
os.seekp(size_t(offset_table_pos) + i * sizeof(uint32_t));
|
|
|
|
WriteU32(os, mxst_offset);
|
|
|
|
os.seekp(mxst_offset);
|
2022-07-11 17:19:36 -04:00
|
|
|
|
2022-07-18 03:27:00 -04:00
|
|
|
// MxSt
|
|
|
|
RIFF::Chk mxst = RIFF::BeginChunk(os, RIFF::MxSt);
|
2022-07-11 17:19:36 -04:00
|
|
|
|
2022-07-18 03:27:00 -04:00
|
|
|
{
|
|
|
|
// MxOb
|
|
|
|
WriteObject(os, child);
|
|
|
|
}
|
2022-07-11 04:48:20 -04:00
|
|
|
|
2022-07-18 03:27:00 -04:00
|
|
|
{
|
|
|
|
// LIST
|
|
|
|
RIFF::Chk list_mxda = RIFF::BeginChunk(os, RIFF::LIST);
|
2022-07-11 04:48:20 -04:00
|
|
|
|
2022-07-18 03:27:00 -04:00
|
|
|
WriteU32(os, RIFF::MxDa);
|
2022-07-11 04:48:20 -04:00
|
|
|
|
2022-07-18 03:27:00 -04:00
|
|
|
// First, interleave headers
|
|
|
|
std::vector<Object*> objects;
|
|
|
|
objects.reserve(child->GetChildCount() + 1);
|
|
|
|
objects.push_back(child);
|
|
|
|
for (size_t j=0; j<child->GetChildCount(); j++) {
|
|
|
|
objects.push_back(static_cast<Object*>(child->GetChildAt(j)));
|
2022-07-11 17:19:36 -04:00
|
|
|
}
|
2022-07-18 03:27:00 -04:00
|
|
|
|
|
|
|
InterleaveObjects(os, objects);
|
|
|
|
|
|
|
|
RIFF::EndChunk(os, list_mxda);
|
2022-07-11 04:48:20 -04:00
|
|
|
}
|
2022-07-18 03:27:00 -04:00
|
|
|
|
|
|
|
RIFF::EndChunk(os, mxst);
|
2022-07-11 04:48:20 -04:00
|
|
|
}
|
|
|
|
|
2022-07-18 03:27:00 -04:00
|
|
|
// Fill remainder with padding
|
|
|
|
if (os.tellp()%m_BufferSize != 0) {
|
|
|
|
uint32_t current_buf = os.tellp() / m_BufferSize;
|
|
|
|
uint32_t target_sz = (current_buf + 1) * m_BufferSize;
|
|
|
|
|
|
|
|
WritePadding(os, target_sz - os.tellp());
|
2022-07-11 00:16:20 -04:00
|
|
|
}
|
2022-07-18 03:27:00 -04:00
|
|
|
|
|
|
|
RIFF::EndChunk(os, list_mxst);
|
2022-07-11 00:16:20 -04:00
|
|
|
}
|
|
|
|
|
2022-07-18 03:27:00 -04:00
|
|
|
RIFF::EndChunk(os, riff);
|
|
|
|
|
|
|
|
return ERROR_SUCCESS;
|
2022-07-11 00:16:20 -04:00
|
|
|
}
|
|
|
|
|
2022-07-18 03:27:00 -04:00
|
|
|
void Interleaf::WriteObject(std::ostream &os, const Object *o) const
|
2022-07-17 19:25:06 -04:00
|
|
|
{
|
2022-07-18 03:27:00 -04:00
|
|
|
RIFF::Chk mxob = RIFF::BeginChunk(os, RIFF::MxOb);
|
|
|
|
|
|
|
|
WriteU16(os, o->type_);
|
|
|
|
WriteString(os, o->presenter_);
|
|
|
|
WriteU32(os, o->unknown1_);
|
|
|
|
WriteString(os, o->name_);
|
|
|
|
WriteU32(os, o->id_);
|
|
|
|
WriteU32(os, o->flags_);
|
|
|
|
WriteU32(os, o->unknown4_);
|
|
|
|
WriteU32(os, o->duration_);
|
|
|
|
WriteU32(os, o->loops_);
|
|
|
|
WriteVector3(os, o->position_);
|
|
|
|
WriteVector3(os, o->direction_);
|
|
|
|
WriteVector3(os, o->up_);
|
|
|
|
|
|
|
|
WriteU16(os, o->extra_.size());
|
|
|
|
WriteBytes(os, o->extra_);
|
|
|
|
|
|
|
|
if (o->type_ != MxOb::Presenter && o->type_ != MxOb::World) {
|
|
|
|
WriteString(os, o->filename_);
|
|
|
|
WriteU32(os, o->unknown26_);
|
|
|
|
WriteU32(os, o->unknown27_);
|
|
|
|
WriteU32(os, o->unknown28_);
|
|
|
|
WriteU32(os, o->filetype_);
|
|
|
|
WriteU32(os, o->unknown29_);
|
|
|
|
WriteU32(os, o->unknown30_);
|
|
|
|
|
|
|
|
if (o->filetype_ == MxOb::WAV) {
|
|
|
|
WriteU32(os, o->unknown31_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (o->HasChildren()) {
|
|
|
|
// Child list
|
|
|
|
RIFF::Chk list_mxch = RIFF::BeginChunk(os, RIFF::LIST);
|
|
|
|
|
|
|
|
WriteU32(os, RIFF::MxCh);
|
|
|
|
WriteU32(os, o->GetChildCount());
|
|
|
|
|
|
|
|
for (size_t i = 0; i < o->GetChildCount(); i++) {
|
|
|
|
WriteObject(os, static_cast<Object*>(o->GetChildAt(i)));
|
|
|
|
}
|
|
|
|
|
|
|
|
RIFF::EndChunk(os, list_mxch);
|
2022-07-17 19:25:06 -04:00
|
|
|
}
|
|
|
|
|
2022-07-18 03:27:00 -04:00
|
|
|
RIFF::EndChunk(os, mxob);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ChunkStatus
|
|
|
|
{
|
2022-07-17 19:25:06 -04:00
|
|
|
Object *object;
|
|
|
|
size_t index;
|
|
|
|
uint32_t time;
|
|
|
|
};
|
|
|
|
|
2022-07-18 05:12:22 -04:00
|
|
|
bool HasChildrenThatNeedPriority(Object *parent, uint32_t parent_time, const std::vector<ChunkStatus> &other_jobs)
|
|
|
|
{
|
|
|
|
for (size_t i=0; i<other_jobs.size(); i++) {
|
|
|
|
if (parent->ContainsChild(other_jobs.at(i).object)
|
|
|
|
&& other_jobs.at(i).time <= parent_time) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-07-18 03:27:00 -04:00
|
|
|
void Interleaf::InterleaveObjects(std::ostream &os, const std::vector<Object *> &objects) const
|
2022-07-17 19:25:06 -04:00
|
|
|
{
|
2022-07-18 03:27:00 -04:00
|
|
|
std::vector<ChunkStatus> status(objects.size());
|
|
|
|
|
|
|
|
// Set up status vector
|
|
|
|
for (size_t i=0; i<objects.size(); i++) {
|
|
|
|
status[i].object = objects.at(i);
|
|
|
|
status[i].index = 0;
|
|
|
|
status[i].time = 0;
|
2022-07-17 19:25:06 -04:00
|
|
|
}
|
|
|
|
|
2022-07-18 03:27:00 -04:00
|
|
|
// First, interleave headers
|
|
|
|
for (size_t i=0; i<status.size(); i++) {
|
|
|
|
ChunkStatus &s = status[i];
|
|
|
|
Object *o = s.object;
|
|
|
|
if (!o->data().empty()) {
|
|
|
|
WriteSubChunk(os, 0, o->id(), 0xFFFFFFFF, o->data().front());
|
|
|
|
s.index++;
|
2022-07-17 19:25:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-18 03:27:00 -04:00
|
|
|
// Next, interleave the rest based on time
|
2022-07-17 19:25:06 -04:00
|
|
|
while (true) {
|
|
|
|
// Find next chunk
|
2022-07-18 05:12:22 -04:00
|
|
|
std::vector<ChunkStatus>::iterator s = status.begin();
|
|
|
|
if (s == status.end()) {
|
|
|
|
break;
|
|
|
|
}
|
2022-07-17 19:25:06 -04:00
|
|
|
|
2022-07-18 05:12:22 -04:00
|
|
|
while (HasChildrenThatNeedPriority(s->object, s->time, status)) {
|
|
|
|
s++;
|
|
|
|
}
|
2022-07-17 19:25:06 -04:00
|
|
|
|
2022-07-18 05:12:22 -04:00
|
|
|
if (s == status.end()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<ChunkStatus>::iterator it = s;
|
|
|
|
it++;
|
|
|
|
for (; it!=status.end(); it++) {
|
2022-07-17 19:25:06 -04:00
|
|
|
// Find earliest chunk to write
|
2022-07-18 05:12:22 -04:00
|
|
|
if (it->time < s->time && !HasChildrenThatNeedPriority(it->object, it->time, status)) {
|
|
|
|
s = it;
|
2022-07-17 19:25:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-18 05:12:22 -04:00
|
|
|
if (s->index == s->object->data_.size()) {
|
|
|
|
WriteSubChunk(os, MxCh::FLAG_END, s->object->id(), s->time);
|
|
|
|
status.erase(s);
|
|
|
|
continue;
|
2022-07-17 19:25:06 -04:00
|
|
|
}
|
|
|
|
|
2022-07-18 03:27:00 -04:00
|
|
|
Object *obj = s->object;
|
|
|
|
const bytearray &data = obj->data().at(s->index);
|
2022-07-17 19:25:06 -04:00
|
|
|
|
2022-07-18 03:27:00 -04:00
|
|
|
WriteSubChunk(os, 0, obj->id(), s->time, data);
|
2022-07-17 19:25:06 -04:00
|
|
|
|
2022-07-18 03:27:00 -04:00
|
|
|
s->index++;
|
2022-07-17 19:25:06 -04:00
|
|
|
|
|
|
|
// Increment time
|
2022-07-18 03:27:00 -04:00
|
|
|
switch (obj->filetype()) {
|
2022-07-17 19:25:06 -04:00
|
|
|
case MxOb::WAV:
|
|
|
|
{
|
2022-07-18 03:27:00 -04:00
|
|
|
const WAVFmt *fmt = obj->GetFileHeader().cast<WAVFmt>();
|
|
|
|
s->time += round(double(data.size() * 1000) / (fmt->BitsPerSample/8) / fmt->Channels / fmt->SampleRate);
|
2022-07-17 19:25:06 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case MxOb::SMK:
|
|
|
|
{
|
2022-07-18 03:27:00 -04:00
|
|
|
int32_t frame_rate = obj->GetFileHeader().cast<SMK2>()->FrameRate;
|
2022-07-17 19:25:06 -04:00
|
|
|
int32_t fps;
|
|
|
|
if (frame_rate > 0) {
|
|
|
|
fps = 1000/frame_rate;
|
|
|
|
} else if (frame_rate < 0) {
|
|
|
|
fps = 100000/-frame_rate;
|
|
|
|
} else {
|
|
|
|
fps = 10;
|
|
|
|
}
|
2022-07-18 03:27:00 -04:00
|
|
|
s->time += 1000/fps;
|
2022-07-17 19:25:06 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case MxOb::FLC:
|
2022-07-18 03:27:00 -04:00
|
|
|
s->time += obj->GetFileHeader().cast<FLIC>()->speed;
|
2022-07-17 19:25:06 -04:00
|
|
|
break;
|
|
|
|
case MxOb::STL:
|
|
|
|
case MxOb::OBJ:
|
|
|
|
// Unaffected by time
|
|
|
|
break;
|
|
|
|
}
|
2022-07-18 03:27:00 -04:00
|
|
|
|
2022-07-18 05:12:22 -04:00
|
|
|
// Update parent time too
|
|
|
|
for (size_t i=0; i<status.size(); i++) {
|
|
|
|
ChunkStatus &p = status.at(i);
|
|
|
|
if (p.object != obj) {
|
|
|
|
if (p.object->ContainsChild(obj)) {
|
|
|
|
p.time = std::max(p.time, s->time);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-07-18 03:27:00 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Interleaf::WriteSubChunk(std::ostream &os, uint16_t flags, uint32_t object, uint32_t time, const bytearray &data) const
|
|
|
|
{
|
2022-07-18 05:12:22 -04:00
|
|
|
static const uint32_t total_hdr = MxCh::HEADER_SIZE + kMinimumChunkSize;
|
|
|
|
|
|
|
|
uint32_t data_offset = 0;
|
|
|
|
|
|
|
|
while (data_offset < data.size() || data.size() == 0) {
|
|
|
|
uint32_t data_sz = data.size() - data_offset;
|
2022-07-18 03:27:00 -04:00
|
|
|
|
2022-07-18 05:12:22 -04:00
|
|
|
// Calculate whether this chunk will overrun the buffer
|
|
|
|
uint32_t start_buffer = os.tellp() / m_BufferSize;
|
|
|
|
uint32_t stop_buffer = (uint32_t(os.tellp()) - 1 + data_sz + total_hdr) / m_BufferSize;
|
2022-07-17 19:25:06 -04:00
|
|
|
|
2022-07-18 05:12:22 -04:00
|
|
|
size_t max_chunk = data_sz;
|
|
|
|
|
|
|
|
if (start_buffer != stop_buffer) {
|
|
|
|
size_t remaining = ((start_buffer + 1) * m_BufferSize) - os.tellp();
|
|
|
|
max_chunk = remaining - total_hdr;
|
|
|
|
|
|
|
|
if (!(flags & MxCh::FLAG_SPLIT)) {
|
|
|
|
if (remaining < 9882) {
|
|
|
|
// This chunk won't fit in our buffer alignment. We must make a decision to either insert
|
|
|
|
// padding or split the clip.
|
|
|
|
WritePadding(os, remaining);
|
|
|
|
|
|
|
|
// Do loop over again
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
flags |= MxCh::FLAG_SPLIT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bytearray chunk = data.mid(data_offset, max_chunk);
|
|
|
|
WriteSubChunkInternal(os, flags, object, time, data_sz, chunk);
|
|
|
|
data_offset += chunk.size();
|
|
|
|
|
|
|
|
if (data.size() == 0) {
|
|
|
|
break;
|
|
|
|
}
|
2022-07-17 19:25:06 -04:00
|
|
|
}
|
2022-07-18 05:12:22 -04:00
|
|
|
}
|
2022-07-17 19:25:06 -04:00
|
|
|
|
2022-07-18 05:12:22 -04:00
|
|
|
void Interleaf::WriteSubChunkInternal(std::ostream &os, uint16_t flags, uint32_t object, uint32_t time, uint32_t data_sz, const bytearray &data) const
|
|
|
|
{
|
2022-07-18 03:27:00 -04:00
|
|
|
RIFF::Chk mxch = RIFF::BeginChunk(os, RIFF::MxCh);
|
|
|
|
|
|
|
|
WriteU16(os, flags);
|
|
|
|
WriteU32(os, object);
|
|
|
|
WriteU32(os, time);
|
2022-07-18 05:12:22 -04:00
|
|
|
WriteU32(os, data_sz);
|
2022-07-18 03:27:00 -04:00
|
|
|
WriteBytes(os, data);
|
|
|
|
|
|
|
|
RIFF::EndChunk(os, mxch);
|
2022-07-17 19:25:06 -04:00
|
|
|
}
|
|
|
|
|
2022-07-18 03:27:00 -04:00
|
|
|
void Interleaf::WritePadding(std::ostream &os, uint32_t size) const
|
2022-07-17 19:25:06 -04:00
|
|
|
{
|
2022-07-18 03:27:00 -04:00
|
|
|
if (size < kMinimumChunkSize) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
size -= kMinimumChunkSize;
|
|
|
|
|
|
|
|
WriteU32(os, RIFF::pad_);
|
|
|
|
WriteU32(os, size);
|
|
|
|
|
|
|
|
bytearray b(size);
|
|
|
|
b.fill(0xCD);
|
|
|
|
WriteBytes(os, b);
|
|
|
|
}
|
2022-07-17 19:25:06 -04:00
|
|
|
|
2022-07-11 00:16:20 -04:00
|
|
|
}
|