| 29 if (!fp) |
29 if (!fp) |
| 30 return LDProjectPtr(); |
30 return LDProjectPtr(); |
| 31 |
31 |
| 32 archive* arc = archive_read_new(); |
32 archive* arc = archive_read_new(); |
| 33 archive_read_support_filter_all (arc); |
33 archive_read_support_filter_all (arc); |
| 34 archive_read_support_format_all (arc); |
34 archive_read_support_format_zip (arc); |
| 35 // archive_read_support_format_zip (arc); |
|
| 36 archive_entry* arcent; |
|
| 37 int result = archive_read_open_filename (arc, filename.toLocal8Bit().constData(), 0x4000); |
35 int result = archive_read_open_filename (arc, filename.toLocal8Bit().constData(), 0x4000); |
| 38 |
36 |
| 39 if (result != ARCHIVE_OK) |
37 if (result != ARCHIVE_OK) |
| 40 { |
38 { |
| 41 fprint (fp, "unable to open argh.pk3 (%1)\n", archive_error_string (arc)); |
39 fprint (fp, "unable to open argh.pk3 (%1)\n", archive_error_string (arc)); |
| 42 return LDProjectPtr(); |
40 return LDProjectPtr(); |
| 43 } |
41 } |
| 44 |
42 |
| 45 while (archive_read_next_header(arc, &arcent) == ARCHIVE_OK) |
43 for (archive_entry* arcent; archive_read_next_header(arc, &arcent) == ARCHIVE_OK;) |
| 46 { |
44 { |
| 47 QString pathname = archive_entry_pathname (arcent); |
45 QString pathname = archive_entry_pathname (arcent); |
| 48 char buffer[1024]; |
46 QVector<char> buffer; |
| 49 int size = archive_read_data(arc, buffer, sizeof buffer); |
47 buffer.resize (archive_entry_size (arcent)); |
| |
48 int size = archive_read_data(arc, buffer.data(), buffer.size()); |
| 50 |
49 |
| 51 if (size < 0) |
50 if (size >= 0) |
| 52 fprint (fp, "Error reading entry %1: %2", pathname, archive_error_string (arc)); |
51 { |
| |
52 if (pathname.startsWith ("dat/")) |
| |
53 loadBinaryDocument (pathname.right (4), QByteArray (buffer.constData(), buffer.size())); |
| |
54 } |
| 53 else |
55 else |
| 54 fprint (fp, "%1 contains: %2 (%3 bytes)\n", pathname, static_cast<const char*>(buffer), size); |
56 fprint (fp, "Unable to read %1: %2", pathname, archive_error_string (arc)); |
| 55 } |
57 } |
| 56 |
58 |
| 57 if ((result = archive_read_free(arc)) != ARCHIVE_OK) |
59 if ((result = archive_read_free(arc)) != ARCHIVE_OK) |
| 58 { |
60 { |
| 59 fprint (fp, "unable to close argh.pk3\n"); |
61 fprint (fp, "unable to close argh.pk3\n"); |
| 60 return LDProjectPtr(); |
62 return LDProjectPtr(); |
| 61 } |
63 } |
| 62 |
64 |
| 63 return LDProjectPtr(); |
65 return LDProjectPtr(); |
| |
66 } |
| |
67 |
| |
68 #include "ldDocument.h" |
| |
69 void LDProject::loadBinaryDocument(const QString &name, const QByteArray &data) |
| |
70 { |
| |
71 QDataStream ds (&data, QIODevice::ReadOnly); |
| |
72 ds.setVersion (QDataStream::Qt_4_8); |
| |
73 enum { CurrentVersion = 0 }; |
| |
74 |
| |
75 quint16 version; |
| |
76 ds << version; |
| |
77 |
| |
78 if (version > CurrentVersion) |
| |
79 return; // too new |
| |
80 |
| |
81 qint8 header; |
| |
82 quint32 color; |
| |
83 LDDocumentPtr doc = LDDocument::createNew(); |
| |
84 LDObjectPtr obj; |
| |
85 struct XYZ { double x, y, z; Vertex toVertex() const { return Vertex (x,y,z); }}; |
| |
86 XYZ verts[4]; |
| |
87 |
| |
88 while ((ds << header) != -1) |
| |
89 { |
| |
90 switch (header) |
| |
91 { |
| |
92 case 0: |
| |
93 { |
| |
94 QString message; |
| |
95 ds >> message; |
| |
96 doc->addObject (LDSpawn<LDComment> (message)); |
| |
97 } |
| |
98 break; |
| |
99 |
| |
100 case 2: |
| |
101 obj = LDSpawn<LDLine>(); |
| |
102 goto polyobject; |
| |
103 |
| |
104 case 3: |
| |
105 obj = LDSpawn<LDTriangle>(); |
| |
106 goto polyobject; |
| |
107 |
| |
108 case 4: |
| |
109 obj = LDSpawn<LDQuad>(); |
| |
110 goto polyobject; |
| |
111 |
| |
112 case 5: |
| |
113 obj = LDSpawn<LDCondLine>(); |
| |
114 polyobject: |
| |
115 ds >> color; |
| |
116 for (int i = 0; i < obj->numVertices(); ++i) |
| |
117 { |
| |
118 XYZ v; |
| |
119 ds >> v.x >> v.y >> v.z; |
| |
120 obj->setVertex (i, Vertex (v.x, v.y, v.z)); |
| |
121 } |
| |
122 |
| |
123 doc->addObject (obj); |
| |
124 break; |
| |
125 } |
| |
126 } |
| |
127 |
| 64 } |
128 } |
| 65 |
129 |
| 66 LDProjectPtr LDProject::NewProject() |
130 LDProjectPtr LDProject::NewProject() |
| 67 { |
131 { |
| 68 return LDProjectPtr (new LDProject()); |
132 return LDProjectPtr (new LDProject()); |