-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathContentTableViewer.cpp
453 lines (400 loc) · 15.3 KB
/
ContentTableViewer.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
#include "ContentTableViewer.h"
#include <stdint.h>
#include "libInfinite/contentUtils.h"
// display modes for the 16-byte type field
#define TYPE_GUID "guid"
#define TYPE_HEXL "hexl"
#define TYPE_HEXB "hexb"
#define TYPE_PYTHON "py"
ContentTableViewer::ContentTableViewer() {
// this code is pretty much the same for all table/list viewers. Only the columns have to be changed
set_orientation(Gtk::ORIENTATION_HORIZONTAL);
Gtk::TreeModel::ColumnRecord record;
record.add(TypeColumn);
record.add(TypeIDColumn);
record.add(IsExternalColumn);
record.add(RefColumn);
record.add(RefSizeColumn);
record.add(ParentColumn);
record.add(FieldOffsetColumn);
record.add(IndexColumn);
store = Gtk::TreeStore::create(record);
view = new Gtk::TreeView(store);
TypeViewColumn = new Gtk::TreeViewColumn("Type",TypeColumn);
TypeIDViewColumn = new Gtk::TreeViewColumn("Type ID",TypeIDColumn);
IsExternalViewColumn= new Gtk::TreeViewColumn("Is External",IsExternalColumn);
RefViewColumn = new Gtk::TreeViewColumn("Reference Index",RefColumn);
RefSizeViewColumn = new Gtk::TreeViewColumn("Reference Size",RefSizeColumn);
ParentViewColumn = new Gtk::TreeViewColumn("Parent Index",ParentColumn);
FieldOffsetViewColumn = new Gtk::TreeViewColumn("Field Offset",FieldOffsetColumn);
TypeViewColumn->set_resizable(true);
TypeViewColumn->set_min_width(10);
TypeIDViewColumn->set_resizable(true);
TypeIDViewColumn->set_min_width(10);
IsExternalViewColumn->set_resizable(true);
IsExternalViewColumn->set_min_width(10);
RefViewColumn->set_resizable(true);
RefViewColumn->set_min_width(10);
RefSizeViewColumn->set_resizable(true);
RefSizeViewColumn->set_min_width(10);
ParentViewColumn->set_resizable(true);
ParentViewColumn->set_min_width(10);
FieldOffsetViewColumn->set_resizable(true);
FieldOffsetViewColumn->set_min_width(10);
TypeViewColumn->set_property("editable", true);
//TypeViewColumn->set_renderer(typeRenderer, TypeColumn);
view->append_column(*TypeViewColumn);
view->append_column(*TypeIDViewColumn);
view->append_column(*IsExternalViewColumn);
view->append_column(*RefViewColumn);
view->append_column(*RefSizeViewColumn);
view->append_column(*ParentViewColumn);
view->append_column(*FieldOffsetViewColumn);
view->set_hexpand(true);
view->set_vexpand(true);
view->show();
view->set_show_expanders(true);
view->signal_row_activated().connect([this](Gtk::TreePath path, Gtk::TreeViewColumn* column){
Gtk::TreeModel::Row row = *this->view->get_selection()->get_selected();
int idx = row.get_value(IndexColumn);
int ref = item->contentTable.entries[idx].ref;
if(ref == 0xffffffff){
return;
}
showDataCallback(ref);
});
scroller = new Gtk::ScrolledWindow();
scroller->add(*view);
scroller->show();
scroller->set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
viewFrame = new Gtk::Frame();
viewFrame->show();
add(*viewFrame);
viewFrame->add(*scroller);
item = nullptr;
// end easily copyable code
settingsBox = new Gtk::Box(Gtk::ORIENTATION_VERTICAL);
settingsBox->show();
add(*settingsBox);
settingsLabel = new Gtk::Label("Viewer Settings");
settingsLabel->show();
settingsBox->add(*settingsLabel);
settingsSeparator = new Gtk::Separator(Gtk::ORIENTATION_HORIZONTAL);
settingsSeparator->show();
settingsBox->add(*settingsSeparator);
typeDisplayModeLabel = new Gtk::Label("Type Display Mode:");
typeDisplayModeLabel->show();
typeDisplayModeLabel->set_alignment(Gtk::ALIGN_START, Gtk::ALIGN_CENTER);
settingsBox->add(*typeDisplayModeLabel);
typeDisplayModeBox = new Gtk::ComboBoxText();
typeDisplayModeBox->show();
typeDisplayModeBox->append(TYPE_HEXL, "Hex String - Little Endian");
typeDisplayModeBox->append(TYPE_HEXB, "Hex String - Big Endian");
typeDisplayModeBox->append(TYPE_PYTHON, "Python byte-literal");
typeDisplayModeBox->set_active_id(TYPE_PYTHON);
typeMode = MODE_PYTHON;
typeDisplayModeBox->signal_changed().connect([this] {
std::string active = typeDisplayModeBox->get_active_id();
if(active == TYPE_GUID){
typeMode = MODE_GUID;
} else if(active == TYPE_HEXL){
typeMode = MODE_HEXL;
} else if(active == TYPE_HEXB){
typeMode = MODE_HEXB;
} else if(active == TYPE_PYTHON){
typeMode = MODE_PYTHON;
}
// after changing the mode, the Store needs to be updated, as there is currently no custom CellRenderer or cell_data_func used, although that would be a cleaner way to do it
fillStore();
});
settingsBox->add(*typeDisplayModeBox);
StartOffsetCheckButton.set_label("from Start of File");
TagOffsetCheckButton.set_label("from Start of Tag Data");
TagOffsetCheckButton.join_group(StartOffsetCheckButton);
StartOffsetCheckButton.show();
TagOffsetCheckButton.show();
settingsBox->add(StartOffsetCheckButton);
settingsBox->add(TagOffsetCheckButton);
Label0x.set_label("0x");
OffsetBox.set_orientation(Gtk::ORIENTATION_HORIZONTAL);
Label0x.show();
OffsetBox.add(Label0x);
OffsetEntry.show();
OffsetBox.add(OffsetEntry);
OffsetBox.show();
settingsBox->add(OffsetBox);
SearchOffsetButton.set_label("Search");
settingsBox->add(SearchOffsetButton);
SearchOffsetButton.show();
SearchOffsetButton.signal_clicked().connect([this]{
uint32_t off = std::stoul(OffsetEntry.get_text(), nullptr, 16);
for(auto it = store->children().begin(); it != store->children().end(); it++){
bool fromStart = StartOffsetCheckButton.get_active();
int idx = it->get_value(IndexColumn);
ContentTableEntry* cte = &item->contentTable.entries[idx];
if(cte->ref == -1){
continue;
}
DataTableEntry* dte = &item->dataTable.entries[cte->ref];
uint32_t eoff = dte->offset;
if(fromStart){
eoff = item->getDataBlockOffset(dte);
}
uint32_t maxoff = eoff + dte->size;
if(eoff <= off && off <= maxoff){
//found it!
Gtk::TreeModel::Row row = *it;
Gtk::TreeModel::Path p = store->get_path(it);
view->expand_to_path(p);
view->scroll_to_row(p);
view->get_selection()->unselect_all();
view->get_selection()->select(row);
return;
}
if(findOffset(off, it)){
return;
}
}
});
view->add_events(Gdk::BUTTON_PRESS_MASK);
view->signal_button_press_event().connect([this] (GdkEventButton* button){
if(button->button == GDK_BUTTON_SECONDARY){
std::string type = view->get_selection()->get_selected()->get_value(TypeColumn);
auto clip = Gtk::Clipboard::get();
clip->set_text(type);
clip->store();
}
return false;
},false);
}
ContentTableViewer::~ContentTableViewer(){
delete typeDisplayModeBox;
delete typeDisplayModeLabel;
delete settingsSeparator;
delete settingsLabel;
delete settingsBox;
delete viewFrame;
delete scroller;
delete TypeViewColumn;
delete RefViewColumn;
delete RefSizeViewColumn;
delete ParentViewColumn;
delete view;
//deleteTree();
}
void ContentTableViewer::setShowDataCallback(std::function<void(int)> callback){
showDataCallback = callback;
}
/*std::string pythonByteLiteral(uint8_t byte){
if(byte >= 32 && byte < 127){
// printable ASCII-character
if(byte == 39){
// single quotes, since I'm putting single quotes around the literal, these have to be escaped
return std::string("\\'");
}
// all other characters can just be used regularly
return std::string(1,byte);
}
// all non-printable characters or invalid ASCII values have to be escaped
char tmp[5];
snprintf(tmp,5,"\\x%02x",byte);
return std::string(tmp);
}*/
std::string uint8HexString(uint8_t byte){
char tmp[3];
snprintf(tmp,3,"%02X",byte);
return std::string(tmp);
}
std::string ContentTableViewer::getTypeString(TypeGUID type){
std::string out;
switch(typeMode){
case MODE_GUID:
break;
case MODE_HEXB:
out = "";
for(int i = 1; i >= 0; i--){
out += uint8HexString((uint8_t)(type.data[i] >> 56)) + " "; // byte 7
out += uint8HexString((uint8_t)(type.data[i] >> 48)) + " "; // byte 6
out += uint8HexString((uint8_t)(type.data[i] >> 40)) + " "; // byte 5
out += uint8HexString((uint8_t)(type.data[i] >> 32)) + " "; // byte 4
out += uint8HexString((uint8_t)(type.data[i] >> 24)) + " "; // byte 3
out += uint8HexString((uint8_t)(type.data[i] >> 16)) + " "; // byte 2
out += uint8HexString((uint8_t)(type.data[i] >> 8)) + " "; // byte 1
out += uint8HexString((uint8_t)type.data[i]) + " "; // byte 0
}
return out;
case MODE_HEXL:
out = "";
for(int i = 0; i < 2; i++){
out += uint8HexString((uint8_t)type.data[i]) + " "; // byte 0
out += uint8HexString((uint8_t)(type.data[i] >> 8)) + " "; // byte 1
out += uint8HexString((uint8_t)(type.data[i] >> 16)) + " "; // byte 2
out += uint8HexString((uint8_t)(type.data[i] >> 24)) + " "; // byte 3
out += uint8HexString((uint8_t)(type.data[i] >> 32)) + " "; // byte 4
out += uint8HexString((uint8_t)(type.data[i] >> 40)) + " "; // byte 5
out += uint8HexString((uint8_t)(type.data[i] >> 48)) + " "; // byte 6
out += uint8HexString((uint8_t)(type.data[i] >> 56)) + " "; // byte 7
}
return out;
case MODE_PYTHON:
out = "b'";
for(int i = 0; i < 2; i++){
// the TypeGUID consists of two 64-bit integers to store all 16 bytes
// since the shifting is different for each of the 8 bytes per int, looping over each byte would be a bit more complicated than necessary
out += pythonByteLiteral((uint8_t)type.data[i]); // byte 0
out += pythonByteLiteral((uint8_t)(type.data[i] >> 8)); // byte 1
out += pythonByteLiteral((uint8_t)(type.data[i] >> 16)); // byte 2
out += pythonByteLiteral((uint8_t)(type.data[i] >> 24)); // byte 3
out += pythonByteLiteral((uint8_t)(type.data[i] >> 32)); // byte 4
out += pythonByteLiteral((uint8_t)(type.data[i] >> 40)); // byte 5
out += pythonByteLiteral((uint8_t)(type.data[i] >> 48)); // byte 6
out += pythonByteLiteral((uint8_t)(type.data[i] >> 56)); // byte 7
}
return out + "'";
}
return std::string("Internal Error!");
}
void ContentTableViewer::setItem(Item* item){
this->item = item;
//deleteTree();
if(item == nullptr){
store->clear();
printf("null\n");
return;
}
//generateTree(true); //TODO: add this setting
fillStore();
}
// these two functions are mostly identical, except that the non-recursive version creates top-level entries into the tree store
// combining them should be possible
void ContentTableViewer::fillStore(){
view->set_model(Glib::RefPtr<Gtk::TreeStore>());
store->clear();
for(int i = 0; i < item->contentTable.rootEntries.size(); i++){
// every top-level entry
Gtk::TreeModel::iterator iter = store->append();
iter->set_value(TypeColumn, getTypeString(item->contentTable.rootEntries[i]->type));
iter->set_value(TypeIDColumn, std::to_string(item->contentTable.rootEntries[i]->type_id));
iter->set_value(IsExternalColumn, std::to_string(item->contentTable.rootEntries[i]->is_external));
iter->set_value(RefColumn, uint32ToHexString(item->contentTable.rootEntries[i]->ref));
if(item->contentTable.rootEntries[i]->ref != 0xffffffff){
iter->set_value(RefSizeColumn, uint32ToHexString(item->dataTable.entries[item->contentTable.rootEntries[i]->ref].size));
} else {
iter->set_value(RefSizeColumn, std::string("-"));
}
iter->set_value(ParentColumn, uint32ToHexString(item->contentTable.rootEntries[i]->parent));
iter->set_value(FieldOffsetColumn, uint32ToHexString(item->contentTable.rootEntries[i]->field_offset));
iter->set_value(IndexColumn, item->contentTable.rootEntries[i]->idx);
// fill in the children of this entry
for(int c = 0; c < item->contentTable.rootEntries[i]->children.size(); c++){
fillStoreRecursive(item->contentTable.rootEntries[i]->children[c], iter);
}
}
view->set_model(store);
}
void ContentTableViewer::fillStoreRecursive(ContentTableEntry* entry, Gtk::TreeStore::iterator iter){
// create the row for this entry
Gtk::TreeModel::iterator this_it = store->append(iter->children());
this_it->set_value(TypeColumn, getTypeString(entry->type));
this_it->set_value(TypeIDColumn, std::to_string(entry->type_id));
this_it->set_value(IsExternalColumn, std::to_string(entry->is_external));
this_it->set_value(RefColumn, uint32ToHexString(entry->ref));
if(entry->ref != 0xffffffff){
this_it->set_value(RefSizeColumn, uint32ToHexString(item->dataTable.entries[entry->ref].size));
} else {
this_it->set_value(RefSizeColumn, std::string("-"));
}
this_it->set_value(ParentColumn, uint32ToHexString(entry->parent));
this_it->set_value(FieldOffsetColumn, uint32ToHexString(entry->field_offset));
this_it->set_value(IndexColumn, entry->idx);
// fill in the children, if there are any
for(int i = 0; i < entry->children.size(); i++){
fillStoreRecursive(entry->children[i], this_it);
}
}
bool ContentTableViewer::findOffset(uint32_t off,Gtk::TreeStore::iterator iter){
bool fromStart = StartOffsetCheckButton.get_active();
for(auto it = iter->children().begin(); it != iter->children().end(); it++){
int idx = it->get_value(IndexColumn);
ContentTableEntry* cte = &item->contentTable.entries[idx];
if(cte->ref == -1){
continue;
}
DataTableEntry* dte = &item->dataTable.entries[cte->ref];
uint32_t eoff = dte->offset;
if(fromStart){
eoff = item->getDataBlockOffset(dte);
}
uint32_t maxoff = eoff + dte->size;
if(eoff <= off && off <= maxoff){
//found it!
Gtk::TreeModel::Row row = *it;
Gtk::TreeModel::Path p = store->get_path(it);
view->expand_to_path(p);
view->scroll_to_row(p);
view->get_selection()->unselect_all();
view->get_selection()->select(row);
return true;
}
if(findOffset(off, it)){
return true;
}
}
return false;
}
void ContentTableViewer::deleteTree(){
for(int i = 0; i < tree.size(); i++){
for(int c = 0; c < tree[i]->children.size(); c++){
deleteTreeRecursive(tree[i]->children[c]);
}
delete tree[i];
}
tree.clear(); // clear the vector so that there are no invalid pointers
}
void ContentTableViewer::deleteTreeRecursive(TreeEntry* entry){
for(int c = 0; c < entry->children.size(); c++){
deleteTreeRecursive(entry->children[c]);
}
// all children are deleted now, so this entry can now be deleted as well
delete entry;
}
void ContentTableViewer::generateTree(bool tree){
if(!tree){
// don't actually generate a tree, just create a list
//TODO: add this
}
std::vector<TreeEntry*> tmpVec;
// allocate some memory
tmpVec.reserve(item->contentTable.entries.size());
// create a TableEntry for every Entry
for(int i = 0; i < item->contentTable.entries.size(); i++){
// every entry
ContentTableEntry* cte = &item->contentTable.entries[i];
TreeEntry* tmp = new TreeEntry;
tmp->idx = i;
for(int c = 0; c < cte->children.size(); c++){
}
tmpVec.emplace_back(tmp);
}
// iterate over the entries again, but this time, build the tree by looking at the parent index
// this might be possible in one loop if parent entries are always defined before child entries
// but it's safer this way without proper documentation on the format
for(int i = 0; i < item->contentTable.entries.size(); i++){
if(item->contentTable.entries[i].parent != 0xFFFFFFFF){
// should have a valid parent
tmpVec[i]->hasParent = true;
// add this entry to the parents children vector
tmpVec[item->contentTable.entries[i].parent]->children.emplace_back(tmpVec[i]);
} else {
tmpVec[i]->hasParent = false;
}
}
// the tree is now built, but not really usable yet, as the top-level entries haven't been added to the final vector yet
// to find them, iterate over everything a third time
for(int i = 0; i < tmpVec.size(); i++){
if(!tmpVec[i]->hasParent){
// this entry didn't get added to any parent in the last loop, so it's a top level entry
this->tree.emplace_back(tmpVec[i]);
}
}
}