# HG changeset patch # User Teemu Piippo # Date 1427963158 -10800 # Node ID 9ac1042beadc5ed9d398946c364f628ebb7b5b38 # Parent cc8920320184df54859557acedf179092c274978 - commit ldproject stuff diff -r cc8920320184 -r 9ac1042beadc src/ldproject.cpp --- a/src/ldproject.cpp Thu Mar 19 15:24:02 2015 +0200 +++ b/src/ldproject.cpp Thu Apr 02 11:25:58 2015 +0300 @@ -28,8 +28,132 @@ LDProjectPtr LDProject::LoadFromFile (const QString& filename) { - LDProjectPtr proj = LDProject::NewProject(); - LDProjectLoader (proj); + QVector> referenceNames; + std::function loadProject; + std::function loadDocument; + + loadProject = [&](const QString& filename) -> LDProjectPtr + { + archive* arc = archive_read_new(); + archive_read_support_filter_all (arc); + archive_read_support_format_zip (arc); + int result = archive_read_open_filename (arc, filename.toLocal8Bit().constData(), 0x4000); + + if (result != ARCHIVE_OK) + { + fprint (stderr, "unable to open argh.pk3 (%1)\n", archive_error_string (arc)); + return LDProjectPtr(); + } + + for (archive_entry* arcent; archive_read_next_header(arc, &arcent) == ARCHIVE_OK;) + { + QString pathname = archive_entry_pathname (arcent); + QVector buffer; + buffer.resize (archive_entry_size (arcent)); + int size = archive_read_data(arc, buffer.data(), buffer.size()); + + if (size >= 0) + { + if (pathname.startsWith ("dat/")) + loadDocument (pathname.right (4), QByteArray (buffer.constData(), buffer.size())); + } + else + fprint (stderr, "Unable to read %1: %2", pathname, archive_error_string (arc)); + } + + if ((result = archive_read_free(arc)) != ARCHIVE_OK) + { + fprint (stderr, "unable to close argh.pk3\n"); + return LDProjectPtr(); + } + + return LDProjectPtr(); + }; + + loadDocument = [&](const QString &name, const QByteArray &data) -> void + { + QDataStream ds (&data, QIODevice::ReadOnly); + ds.setVersion (QDataStream::Qt_4_8); + + quint16 version; + ds << version; + + if (version > CurrentBinaryVersion) + return; // too new + + qint8 header; + quint32 color; + LDDocumentPtr doc = LDDocument::createNew(); + doc->setName (name); + LDObjectPtr obj; + Vertex vertex; + Matrix matrix; + + while ((ds << header) != -1) + { + switch (header) + { + case 0: + { + QString message; + ds >> message; + doc->addObject (LDSpawn (message)); + } + break; + + case 1: + { + LDSubfilePtr ref = LDSpawn(); + QString name; + ds >> color; + ds >> vertex; + + for (int i = 0; i < 9; ++i) + ds >> matrix[i]; + + ds >> name; + ref->setColor (LDColor::fromIndex (color)); + ref->setPosition (vertex); + ref->setTransform (matrix); + + // We leave the fileInfo null for now, references are resolved during + // post-process. If this object references a document that we'll parse + // later, trying to find it now would yield null. + referenceNames.append ({ref, name}); + } + break; + + case 2: + obj = LDSpawn(); + goto polyobject; + + case 3: + obj = LDSpawn(); + goto polyobject; + + case 4: + obj = LDSpawn(); + goto polyobject; + + case 5: + obj = LDSpawn(); + polyobject: + ds >> color; + obj->setColor (LDColor::fromIndex (color)); + + for (int i = 0; i < obj->numVertices(); ++i) + { + ds >> vertex; + obj->setVertex (i, vertex); + } + + doc->addObject (obj); + break; + } + } + }; + + return loadProject (filename); } struct Xyz @@ -54,134 +178,12 @@ struct LDProjectLoader { - QVector> referenceNames; - LDProjectLoader (LDProjectPtr proj); + LDProjectPtr loadProject(); void loadDocument (const QString &name, const QByteArray &data); }; -LDProjectLoader::LDProjectLoader (LDProjectPtr proj) -{ - archive* arc = archive_read_new(); - archive_read_support_filter_all (arc); - archive_read_support_format_zip (arc); - int result = archive_read_open_filename (arc, filename.toLocal8Bit().constData(), 0x4000); - - if (result != ARCHIVE_OK) - { - fprint (stderr, "unable to open argh.pk3 (%1)\n", archive_error_string (arc)); - return LDProjectPtr(); - } - - for (archive_entry* arcent; archive_read_next_header(arc, &arcent) == ARCHIVE_OK;) - { - QString pathname = archive_entry_pathname (arcent); - QVector buffer; - buffer.resize (archive_entry_size (arcent)); - int size = archive_read_data(arc, buffer.data(), buffer.size()); - - if (size >= 0) - { - if (pathname.startsWith ("dat/")) - loadDocument (pathname.right (4), QByteArray (buffer.constData(), buffer.size())); - } - else - fprint (stderr, "Unable to read %1: %2", pathname, archive_error_string (arc)); - } - - if ((result = archive_read_free(arc)) != ARCHIVE_OK) - { - fprint (stderr, "unable to close argh.pk3\n"); - return LDProjectPtr(); - } - - return LDProjectPtr(); -} - -void LDProjectLoader::loadDocument (const QString &name, const QByteArray &data) -{ - QDataStream ds (&data, QIODevice::ReadOnly); - ds.setVersion (QDataStream::Qt_4_8); - - quint16 version; - ds << version; - - if (version > CurrentBinaryVersion) - return; // too new - - qint8 header; - quint32 color; - LDDocumentPtr doc = LDDocument::createNew(); - doc->setName (name); - LDObjectPtr obj; - Vertex vertex; - Matrix matrix; - - while ((ds << header) != -1) - { - switch (header) - { - case 0: - { - QString message; - ds >> message; - doc->addObject (LDSpawn (message)); - } - break; - - case 1: - { - LDSubfilePtr ref = LDSpawn(); - QString name; - ds >> color; - ds >> vertex; - - for (int i = 0; i < 9; ++i) - ds >> matrix[i]; - - ds >> name; - ref->setColor (LDColor::fromIndex (color)); - ref->setPosition (vertex); - ref->setTransform (matrix); - - // We leave the fileInfo null for now, references are resolved during post-process. - // If this object references a document that we'll parse later, finding it would - // yield null now. - referenceNames.append ({ref, name}); - } - break; - - case 2: - obj = LDSpawn(); - goto polyobject; - - case 3: - obj = LDSpawn(); - goto polyobject; - - case 4: - obj = LDSpawn(); - goto polyobject; - - case 5: - obj = LDSpawn(); - polyobject: - ds >> color; - obj->setColor (LDColor::fromIndex (color)); - - for (int i = 0; i < obj->numVertices(); ++i) - { - ds >> vertex; - obj->setVertex (i, vertex); - } - - doc->addObject (obj); - break; - } - } -} - struct ArchiveEntry { archive_entry* entry;