-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.cpp
293 lines (230 loc) · 7.7 KB
/
search.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
extern "C" {
#include <s3dat_ext.h>
}
#include <dirent.h>
#include <iostream>
#include <QStandardItemModel>
#include <QFileSystemModel>
#include <QApplication>
#include <QPushButton>
#include <QSplitter>
#include <QTreeView>
#include <QLabel>
#include <QDebug>
#include <QHeaderView>
s3util_exception_t* ex;
std::vector<s3dat_t*> s3dat_handles;
QStandardItemModel model(0, 5);
QStandardItem* newItem(QString name) {
QStandardItem* item = new QStandardItem(name);
item->setEditable(false);
return item;
}
QPixmap ref_to_pixmap(s3dat_ref_t* ref) {
uint32_t width = s3dat_width(ref);
uint32_t height = s3dat_height(ref);
int s = 1;
if(width < 200 && height < 200) s = 4;
uint32_t bmp_width = width*s;
uint32_t bmp_height = height*s;
s3util_color_t* original_color = s3dat_bmpdata(ref);
uint8_t data[bmp_height][bmp_width][4];
for(uint32_t x = 0;x != width;x++) {
for(uint32_t y = 0;y != height;y++) {
for(uint32_t xoff = 0;xoff != s;xoff++) {
for(uint32_t yoff = 0;yoff != s;yoff++) {
data[y*s+xoff][x*s+yoff][0] = original_color[y*width+x].red;
data[y*s+xoff][x*s+yoff][1] = original_color[y*width+x].green;
data[y*s+xoff][x*s+yoff][2] = original_color[y*width+x].blue;
data[y*s+xoff][x*s+yoff][3] = original_color[y*width+x].alpha;
}
}
}
}
return QPixmap::fromImage(QImage((uint8_t*)data, bmp_width, bmp_height, QImage::Format_RGBA8888));
}
QList<QStandardItem*>* refItem(uint16_t i, s3dat_ref_t* ref) {
QList<QStandardItem*>* props = new QList<QStandardItem*>();
if(ref != NULL) {
props->append(newItem(QString::number(i)));
if(s3dat_is_bitmap(ref)) {
props->append(newItem(QString::number(s3dat_width(ref))));
props->append(newItem(QString::number(s3dat_height(ref))));
} else {
props->append(newItem(""));
props->append(newItem(""));
}
if(s3dat_is_animation(ref)) {
props->append(newItem(QString::number(s3dat_anilen(ref))));
} else {
props->append(newItem(""));
}
if(s3dat_is_string(ref)) {
props->append(newItem(QString::fromUtf8(s3dat_strdata(ref))));
} else {
props->append(newItem(""));
}
}
return props;
}
void fillComplexTree(QString name, s3dat_content_type type, s3dat_t* handle, QStandardItem* fileitem) {
QStandardItem* root = newItem(name);
uint16_t len = s3dat_indexlen(handle, type);
for(uint16_t i = 0;i != len;i++) {
uint32_t seqlen = s3dat_seqlen(handle, i, type);
QStandardItem* seqroot = newItem(QString::number(i));
for(uint32_t seq = 0;seq != seqlen;seq++) {
s3dat_ref_t* ref = NULL;
if(type == s3dat_settler) ref = s3dat_extract_settler(handle, i, seq, &ex);
else if(type == s3dat_torso) ref = s3dat_extract_torso(handle, i, seq, &ex);
else if(type == s3dat_shadow) ref = s3dat_extract_shadow(handle, i, seq, &ex);
else if(type == s3dat_string) ref = s3dat_extract_string(handle, i, seq, &ex);
s3util_catch_exception(&ex);
if(ref != NULL) {
seqroot->appendRow(*refItem(seq, ref));
s3dat_unref(ref);
} else {
printf("%i:%i\n", i, seq);
}
}
root->appendRow(seqroot);
}
fileitem->appendRow(root);
}
void fillSimpleTree(QString name, s3dat_content_type type, s3dat_t* handle, QStandardItem* fileitem) {
QStandardItem* root = newItem(name);
uint16_t len = s3dat_indexlen(handle, type);
for(uint16_t i = 0;i != len;i++) {
s3dat_ref_t* ref = NULL;
if(type == s3dat_gui) ref = s3dat_extract_gui(handle, i, &ex);
else if(type == s3dat_landscape) ref = s3dat_extract_landscape(handle, i, &ex);
else if(type == s3dat_animation) ref = s3dat_extract_animation(handle, i, &ex);
else if(type == s3dat_palette) ref = s3dat_extract_palette(handle, i, &ex);
s3util_catch_exception(&ex);
root->appendRow(*refItem(i, ref));
if(ref != NULL) s3dat_unref(ref);
}
fileitem->appendRow(root);
}
class Counter : public QObject
{
public:
Counter() { m_value = 0; }
int value() const { return m_value; }
public slots:
void setValue(int value);
signals:
void valueChanged(int newValue);
private:
int m_value;
};
class S3DATCallback : public QObject {
public:
S3DATCallback();
public slots:
void onselect(const QModelIndex& index);
};
S3DATCallback::S3DATCallback() {}
QLabel* preview;
void S3DATCallback::onselect(const QModelIndex& index) {
QModelIndex flevel = index.parent();
QModelIndex slevel = flevel.parent();
QModelIndex tlevel = slevel.parent();
s3dat_ref_t* ref = NULL;
if(!flevel.isValid() && !index.child(0, 0).isValid()) {
QStandardItem* file = model.item(index.row());
s3dat_t* datfile = s3dat_handles.at(index.row());
fillComplexTree("settlers", s3dat_settler, datfile, file);
fillComplexTree("torsos", s3dat_torso, datfile, file);
fillComplexTree("shadows", s3dat_shadow, datfile, file);
fillComplexTree("strings", s3dat_string, datfile, file);
fillSimpleTree("guis", s3dat_gui, datfile, file);
fillSimpleTree("landscapes", s3dat_landscape, datfile, file);
fillSimpleTree("animations", s3dat_animation, datfile, file);
fillSimpleTree("palettes", s3dat_palette, datfile, file);
std::cout << "main " << index.row() << std::endl;
return;
}
if(!slevel.isValid()) return;
if(tlevel.isValid()) {
switch(slevel.row()) {
case 0:
ref = s3dat_extract_settler(s3dat_handles.at(tlevel.row()), flevel.row(), index.row(), &ex);
break;
case 1:
ref = s3dat_extract_torso(s3dat_handles.at(tlevel.row()), flevel.row(), index.row(), &ex);
break;
case 2:
ref = s3dat_extract_shadow(s3dat_handles.at(tlevel.row()), flevel.row(), index.row(), &ex);
break;
default:
break;
}
} else {
if(flevel.row()<=3) return;
switch(flevel.row()) {
case 4:
ref = s3dat_extract_gui(s3dat_handles[slevel.row()], index.row(), &ex);
break;
case 5:
ref = s3dat_extract_landscape(s3dat_handles[slevel.row()], index.row(), &ex);
break;
case 6:
ref = s3dat_extract_animation(s3dat_handles[slevel.row()], index.row(), &ex);
break;
case 7:
ref = s3dat_extract_palette(s3dat_handles[slevel.row()], index.row(), &ex);
break;
}
}
if(!s3util_catch_exception(&ex) || ref == NULL) return;
if(s3dat_is_bitmap(ref)) {
preview->setPixmap(ref_to_pixmap(ref));
}
s3dat_unref(ref);
}
int main(int argc, char** argv) {
DIR* dir = opendir("GFX");
if(dir == NULL) {
std::cout << "GFX not found!" << std::endl;
return 1;
}
QApplication app(argc, argv);
QSplitter splitter;
QTreeView treeview(&splitter);
treeview.setSelectionBehavior(QAbstractItemView::SelectRows);
treeview.setModel(&model);
struct dirent* dirent;
while((dirent = readdir(dir)) != NULL) {
std::string name = "GFX/";
name += dirent->d_name;
std::cout << name << std::endl;
if(strlen(dirent->d_name) == 25 && strncmp(dirent->d_name+1, "iedler3", 7) == 0) {
s3dat_t* datfile = s3dat_new_malloc();
s3dat_readfile_name(datfile, (char*)name.c_str(), &ex);
s3util_catch_exception(&ex);
s3dat_add_utf8_encoding(datfile, &ex);
s3dat_add_cache(datfile, &ex);
s3util_catch_exception(&ex);
QString id;
id.append(dirent->d_name[9]);
id.append(dirent->d_name[10]);
QStandardItem* file = newItem(id);
model.appendRow(file);
s3dat_handles.push_back(datfile);
}
}
model.setHorizontalHeaderItem(0, new QStandardItem("id"));
model.setHorizontalHeaderItem(1, new QStandardItem("width"));
model.setHorizontalHeaderItem(2, new QStandardItem("height"));
model.setHorizontalHeaderItem(3, new QStandardItem("length"));
model.setHorizontalHeaderItem(4, new QStandardItem("string"));
preview = new QLabel(&splitter);
S3DATCallback* callback = new S3DATCallback();
splitter.show();
QObject::connect(treeview.selectionModel(), &QItemSelectionModel::currentChanged, callback, &S3DATCallback::onselect);
int return_var = app.exec();
std::for_each(s3dat_handles.begin(), s3dat_handles.end(), s3dat_delete);
delete preview;
return return_var;
}