Ticket #8559: sword2_clue.c

File sword2_clue.c, 10.5 KB (added by eriktorbjorn, 16 years ago)

The actual "CLUster Explorer"

Line 
1/* clue - Extremely primitive "CLUster Explorer" for Broken Sword 2
2 * Copyright (C) 2006 The ScummVM project
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
17.
18 *
19 * $URL$
20 * $Id$
21 *
22 */
23
24#include <gtk/gtk.h>
25
26#include "util.h"
27
28/* The following cluster files are - to some extent - understood:
29 *
30 * carib1.clu
31 * 247 ANIMATION_FILE resources
32 * 7 SCREEN_FILE resources
33 * 11 WALK_GRID_FILE resources
34 * 7 RUN_LIST resources
35 * 24 WAV_FILE resources
36 * carib2.clu
37 * 133 ANIMATION_FILE resources
38 * 11 SCREEN_FILE resources
39 * 13 WALK_GRID_FILE resources
40 * 11 RUN_LIST resources
41 * 30 WAV_FILE resources
42 * carib3.clu
43 * 174 ANIMATION_FILE resources
44 * 5 SCREEN_FILE resources
45 * 4 WALK_GRID_FILE resources
46 * 5 RUN_LIST resources
47 * 31 WAV_FILE resources
48 * docks.clu
49 * 216 ANIMATION_FILE resources
50 * 4 SCREEN_FILE resources
51 * 3 WALK_GRID_FILE resources
52 * 4 RUN_LIST resources
53 * 47 WAV_FILE resources
54 * 1 PALETTE_FILE resource
55 * general.clu
56 * 62 ANIMATION_FILE resources
57 * 7 SCREEN_FILE resources
58 * 1 WALK_GRID_FILE resource
59 * 89 MOUSE_FILE resources
60 * 1 WAV_FILE resource
61 * 159 ICON_FILE resources
62 * 1 PALETTE_FILE resource
63 * jungle.clu
64 * 137 ANIMATION_FILE resources
65 * 4 SCREEN_FILE resources
66 * 6 WALK_GRID_FILE resources
67 * 4 RUN_LIST resources
68 * 10 WAV_FILE resources
69 * paris.clu
70 * 223 ANIMATION_FILE resources
71 * 9 SCREEN_FILE resources
72 * 8 WALK_GRID_FILE resources
73 * 9 RUN_LIST resources
74 * 44 WAV_FILE resources
75 * players.clu
76 * 143 ANIMATION_FILE resources
77 * 2 RUN_LIST resources
78 * pyramid1.clu
79 * 189 ANIMATION_FILE resources
80 * 4 SCREEN_FILE resources
81 * 10 WALK_GRID_FILE resources
82 * 4 RUN_LIST resources
83 * 14 WAV_FILE resources
84 * 30 PALETTE_FILE resources
85 * pyramid2.clu
86 * 124 ANIMATION_FILE resources
87 * 6 SCREEN_FILE resources
88 * 5 WALK_GRID_FILE resources
89 * 6 RUN_LIST resources
90 * 35 WAV_FILE resources
91 * 130 PALETTE_FILE resources
92 * quaramon.clu
93 * 288 ANIMATION_FILE resources
94 * 5 SCREEN_FILE resources
95 * 16 WALK_GRID_FILE resources
96 * 5 RUN_LIST resources
97 * 23 WAV_FILE resources
98 * scripts.clu
99 * 905 GAME_OBJECT resources
100 * 1 GLOBAL_VAR_FILE resource
101 * 68 SCREEN_MANAGER resources
102 * text.clu
103 * 112 TEXT_FILE resources
104 * warehous.clu
105 * 174 ANIMATION_FILE resources
106 * 5 SCREEN_FILE resources
107 * 7 WALK_GRID_FILE resources
108 * 5 RUN_LIST resources
109 * 41 WAV_FILE resources
110 *
111 * The following files are NOT understood:
112 *
113 * credits.clu - This is a text file, with some formatting information
114 * font.clu - Credits font?
115 * music1.clu - See compress_sword2.c
116 * music2.clu - See compress_sword2.c
117 * speech1.clu - See compress_sword2.c
118 * speech2.clu - See compress_sword2.c
119 */
120
121enum {
122 NAME_COLUMN,
123 SIZE_COLUMN,
124 TYPE_COLUMN,
125 POSITION_COLUMN,
126 LENGTH_COLUMN,
127 N_COLUMNS
128};
129
130enum {
131 ANIMATION_FILE = 1,
132 SCREEN_FILE = 2,
133 GAME_OBJECT = 3,
134 WALK_GRID_FILE = 4,
135 GLOBAL_VAR_FILE = 5,
136 PARALLAX_FILE_null = 6,
137 RUN_LIST = 7,
138 TEXT_FILE = 8,
139 SCREEN_MANAGER = 9,
140 MOUSE_FILE = 10,
141 WAV_FILE = 11,
142 ICON_FILE = 12,
143 PALETTE_FILE = 13
144};
145
146void open_screen_file(char *filename, uint32 pos, uint32 len) {
147 g_message("Would open a screen file here");
148}
149
150const gchar *getType(byte type) {
151 switch (type) {
152 case ANIMATION_FILE:
153 return "Animation";
154 case SCREEN_FILE:
155 return "Screen";
156 case GAME_OBJECT:
157 return "Object";
158 case WALK_GRID_FILE:
159 return "Walk Grid";
160 case GLOBAL_VAR_FILE:
161 return "Global Variables";
162 case PARALLAX_FILE_null:
163 return "Parallax File (null";
164 case RUN_LIST:
165 return "Run List";
166 case TEXT_FILE:
167 return "Text File";
168 case SCREEN_MANAGER:
169 return "Screen Manager";
170 case MOUSE_FILE:
171 return "Mouse File";
172 case WAV_FILE:
173 return "WAV File";
174 case ICON_FILE:
175 return "Icon";
176 case PALETTE_FILE:
177 return "Palette";
178 default:
179 return "<unknown>";
180 }
181}
182
183void main_window_destroy_cb(GtkWidget *widget, gpointer data) {
184 gtk_main_quit();
185}
186
187gboolean tree_view_button_cb(GtkTreeView *view, GdkEventButton *event, gpointer data) {
188 gchar *filename = (gchar *)data;
189 GtkTreeSelection *selection;
190 GtkTreeIter iter;
191 GtkTreeModel *model;
192 gchar *name;
193 gint type, pos, len;
194
195 if (event->type != GDK_2BUTTON_PRESS || event->button != 1)
196 return FALSE;
197
198 selection = gtk_tree_view_get_selection(view);
199 if (!gtk_tree_selection_get_selected(selection, &model, &iter))
200 return FALSE;
201
202 gtk_tree_model_get(model, &iter,
203 NAME_COLUMN, &name,
204 TYPE_COLUMN, &type,
205 POSITION_COLUMN, &pos,
206 LENGTH_COLUMN, &len,
207 -1);
208
209 switch (type) {
210 case SCREEN_FILE:
211 open_screen_file(filename, pos, len);
212 return TRUE;
213
214 case ANIMATION_FILE:
215 case GAME_OBJECT:
216 case WALK_GRID_FILE:
217 case GLOBAL_VAR_FILE:
218 case PARALLAX_FILE_null:
219 case RUN_LIST:
220 case TEXT_FILE:
221 case SCREEN_MANAGER:
222 case MOUSE_FILE:
223 case WAV_FILE:
224 case ICON_FILE:
225 case PALETTE_FILE:
226 g_message("'%s' is in '%s' at position %d, length %d", name, filename, pos, len);
227 return TRUE;
228 default:
229 if (type != -1)
230 g_message("Unknown resource type: %d", type);
231 return FALSE;
232 }
233}
234
235gint compare_items(GtkTreeModel *model, GtkTreeIter *a, GtkTreeIter *b, gpointer user_data) {
236 char *name1, *name2;
237
238 gtk_tree_model_get(model, a, NAME_COLUMN, &name1, -1);
239 gtk_tree_model_get(model, b, NAME_COLUMN, &name2, -1);
240
241 return strcmp(name1, name2);
242}
243
244gchar *make_size(uint32 len) {
245 static gchar size[10];
246
247 if (len >= 0x100000)
248 sprintf(size, "%0.1fM", (double)len / 0x100000);
249 else if (len >= 0x400)
250 sprintf(size, "%dK", len / 0x400);
251 else
252 sprintf(size, "%d", len);
253
254 return size;
255}
256
257int main(int argc, char *argv[]) {
258 GtkWidget *main_window, *scroll;
259 GtkWidget *treeview;
260 GtkTreeViewColumn *column;
261 GtkCellRenderer *name_renderer, *size_renderer;
262 GtkTreeStore *store;
263 GtkTreeIter categories[14];
264 GValue value = { 0, };
265 gint offset;
266 uint32 res_counts[14];
267 uint32 res_sizes[14];
268 int i;
269
270 FILE *in;
271 uint32 index_pos;
272 uint32 pos, len;
273
274 gtk_init(&argc, &argv);
275
276 if (argc != 2) {
277 printf("Usage: %s filename\n", argv[0]);
278 return EXIT_FAILURE;
279 }
280
281 in = fopen(argv[1], "rb");
282 if (!in) {
283 printf("Couldn't open %s for reading\n", argv[1]);
284 return EXIT_FAILURE;
285 }
286
287 /* Create the main window, scrollable in both directions */
288
289 main_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
290 gtk_window_set_title(GTK_WINDOW(main_window), "CLUster Explorer");
291 gtk_window_set_default_size(GTK_WINDOW(main_window), 400, 400);
292
293 g_signal_connect(G_OBJECT(main_window), "destroy", G_CALLBACK(main_window_destroy_cb), NULL);
294
295 scroll = gtk_scrolled_window_new(NULL, NULL);
296 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scroll), GTK_SHADOW_ETCHED_IN);
297 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
298
299 /* Create the tree view */
300
301 for (i = 0; i < ARRAYSIZE(res_counts); i++) {
302 res_counts[i] = 0;
303 res_sizes[i] = 0;
304 }
305
306 store = gtk_tree_store_new(N_COLUMNS,
307 G_TYPE_STRING,
308 G_TYPE_STRING,
309 G_TYPE_INT,
310 G_TYPE_INT,
311 G_TYPE_INT);
312
313 gtk_tree_sortable_set_default_sort_func(GTK_TREE_SORTABLE(store), compare_items, NULL, NULL);
314 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(store), GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID, GTK_SORT_ASCENDING);
315
316 index_pos = readUint32LE(in);
317 fseek(in, index_pos, SEEK_SET);
318
319 for (;;) {
320 GtkTreeIter iter;
321 byte type;
322 gchar *utf8_name;
323 gchar name[34];
324 gchar *size;
325
326 pos = readUint32LE(in);
327 len = readUint32LE(in);
328
329 if (pos == EOF || len == EOF)
330 break;
331
332 size = make_size(len);
333
334 index_pos = ftell(in);
335
336 fseek(in, pos, SEEK_SET);
337
338 type = readByte(in);
339 readByte(in); /* compType */
340 readUint32LE(in); /* compSize */
341 readUint32LE(in); /* decompSize */
342 fread(name, sizeof(name), 1, in);
343
344 /*
345 * We need to convert from Latin-1 to UTF-8. Otherwise the text
346 * "CAFÉ" won't be displayed properly.
347 */
348
349 utf8_name = g_convert(name, -1, "UTF-8", "ISO-8859-1", NULL, NULL, NULL);
350
351 if (!res_counts[type]) {
352 gtk_tree_store_append(store, &categories[type], NULL);
353 gtk_tree_store_set(store, &categories[type],
354 NAME_COLUMN, getType(type),
355 SIZE_COLUMN, "",
356 TYPE_COLUMN, -1,
357 POSITION_COLUMN, -1,
358 LENGTH_COLUMN, -1,
359 -1);
360 }
361
362 res_counts[type]++;
363 res_sizes[type] += len;
364 gtk_tree_store_append(store, &iter, &categories[type]);
365 gtk_tree_store_set(store, &iter,
366 NAME_COLUMN, utf8_name,
367 SIZE_COLUMN, size,
368 TYPE_COLUMN, type,
369 POSITION_COLUMN, pos,
370 LENGTH_COLUMN, len);
371
372 fseek(in, index_pos, SEEK_SET);
373 }
374
375 fclose(in);
376
377 for (i = 0; i < ARRAYSIZE(res_counts); i++) {
378 if (res_counts[i]) {
379 gchar size[80];
380
381 sprintf(size, "%s [%d]", make_size(res_sizes[i]), res_counts[i]);
382 gtk_tree_store_set(store, &categories[i],
383 SIZE_COLUMN, size,
384 -1);
385 }
386 }
387
388 treeview = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
389 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(treeview), TRUE);
390
391 g_signal_connect(G_OBJECT(treeview), "button-press-event", G_CALLBACK(tree_view_button_cb), argv[1]);
392
393 /* The view now holds a reference. We can get rid of our own. */
394 g_object_unref(G_OBJECT(store));
395
396 name_renderer = gtk_cell_renderer_text_new();
397 size_renderer = gtk_cell_renderer_text_new();
398
399 g_value_init(&value, G_TYPE_FLOAT);
400 g_value_set_float(&value, 1.0);
401 g_object_set_property(G_OBJECT(size_renderer), "xalign", &value);
402
403 gtk_tree_view_insert_column_with_attributes(
404 GTK_TREE_VIEW(treeview), -1, "Name", name_renderer,
405 "text", NAME_COLUMN,
406 NULL);
407
408 offset = gtk_tree_view_insert_column_with_attributes(
409 GTK_TREE_VIEW(treeview), -1, "Size", size_renderer,
410 "text", SIZE_COLUMN,
411 NULL);
412 column = gtk_tree_view_get_column(GTK_TREE_VIEW(treeview), offset - 1);
413 gtk_tree_view_column_set_alignment(column, 1.0);
414
415 gtk_container_add(GTK_CONTAINER(scroll), GTK_WIDGET(treeview));
416 gtk_container_add(GTK_CONTAINER(main_window), scroll);
417 gtk_widget_show_all(GTK_WIDGET(main_window));
418 gtk_main();
419
420 return EXIT_SUCCESS;
421}