0
|
1 /*
|
|
2 * PACKed - PACKfile EDitor
|
|
3 * Programmed and designed by Matti 'ccr' Hamalainen
|
|
4 * (C) Copyright 2011 Tecnic Software productions (TNSP)
|
|
5 */
|
|
6 #include "dmlib.h"
|
|
7 #include "dmargs.h"
|
|
8 #include "dmpack.h"
|
|
9 #include "dmpackutil.h"
|
|
10 #include <errno.h>
|
|
11
|
|
12 #define SET_MAX_FILES (4096)
|
|
13 #define SET_DEFAULT_PACK "data.pak"
|
|
14
|
|
15 enum
|
|
16 {
|
|
17 CMD_NONE = 0,
|
|
18 CMD_CREATE,
|
|
19 CMD_ADD,
|
|
20 CMD_LIST,
|
|
21 CMD_EXTRACT
|
|
22 } DCOMMAND;
|
|
23
|
|
24 enum
|
|
25 {
|
|
26 PACK_EXTRACTED = 0x0001,
|
|
27 };
|
|
28
|
|
29 int nsrcFilenames = 0;
|
|
30 char * srcFilenames[SET_MAX_FILES];
|
|
31 char * optPackFilename = NULL;
|
|
32 BOOL optCompress = TRUE;
|
|
33 int optCommand = CMD_NONE;
|
|
34 int optDefResFlags = 0;
|
|
35
|
|
36
|
|
37 static DMOptArg optList[] =
|
|
38 {
|
|
39 { 0, '?', "help", "Show this help", OPT_NONE },
|
|
40 { 1, 'p', "pack", "Set pack filename (default: " SET_DEFAULT_PACK ")", OPT_ARGREQ },
|
|
41 { 2, 'c', "create", "Create and add files to PACK", OPT_NONE },
|
|
42 { 3, 'a', "add", "Add files to PACK", OPT_NONE },
|
|
43 { 4, 'l', "list", "List files in PACK", OPT_NONE },
|
|
44 { 5, 'e', "extract", "Extract files from PACK", OPT_NONE },
|
|
45 { 6, 'n', "nocompress", "No compression", OPT_NONE },
|
|
46 { 7, 'v', "verbose", "Increase verbosity", OPT_NONE },
|
|
47 { 8, 'f', "resflags", "Set default resource flags (-f 0xff)", OPT_ARGREQ },
|
|
48 };
|
|
49
|
|
50 static const int optListN = sizeof(optList) / sizeof(optList[0]);
|
|
51
|
|
52
|
|
53 void argShowHelp()
|
|
54 {
|
|
55 dmPrintBanner(stdout, dmProgName, "[options] <packfile> [filename[s]]");
|
|
56 dmArgsPrintHelp(stdout, optList, optListN);
|
|
57 fprintf(stdout,
|
|
58 "\n"
|
|
59 "Examples:\n"
|
|
60 "$ %s -p test.pak -l -- list files in test.pak\n"
|
|
61 "$ %s -a foobar.jpg -- add foobar.jpg in " SET_DEFAULT_PACK "\n"
|
|
62 "$ %s -x foobar.jpg -- extract foobar.jpg from " SET_DEFAULT_PACK "\n",
|
|
63 dmProgName, dmProgName, dmProgName);
|
|
64 }
|
|
65
|
|
66
|
|
67 BOOL argHandleOpt(const int optN, char *optArg, char *currArg)
|
|
68 {
|
|
69 (void) optArg;
|
|
70 switch (optN)
|
|
71 {
|
|
72 case 0:
|
|
73 argShowHelp();
|
|
74 exit(0);
|
|
75 break;
|
|
76
|
|
77 case 1:
|
|
78 optPackFilename = optArg;
|
|
79 break;
|
|
80 case 2:
|
|
81 optCommand = CMD_CREATE;
|
|
82 break;
|
|
83 case 3:
|
|
84 optCommand = CMD_ADD;
|
|
85 break;
|
|
86 case 4:
|
|
87 optCommand = CMD_LIST;
|
|
88 break;
|
|
89 case 5:
|
|
90 optCommand = CMD_EXTRACT;
|
|
91 break;
|
|
92
|
|
93 case 6:
|
|
94 optCompress = FALSE;
|
|
95 break;
|
|
96
|
|
97 case 7:
|
|
98 dmVerbosity++;
|
|
99 break;
|
|
100
|
|
101 case 8:
|
|
102 {
|
|
103 int i;
|
|
104 if (!dmGetIntVal(optArg, &i))
|
|
105 {
|
|
106 dmError("Invalid flags value '%s'.\n", optArg);
|
|
107 return FALSE;
|
|
108 }
|
|
109 optDefResFlags = i;
|
|
110 }
|
|
111 break;
|
|
112
|
|
113 default:
|
|
114 dmError("Unknown argument '%s'.\n", currArg);
|
|
115 return FALSE;
|
|
116 }
|
|
117
|
|
118 return TRUE;
|
|
119 }
|
|
120
|
|
121
|
|
122 BOOL argHandleFile(char *currArg)
|
|
123 {
|
|
124 if (nsrcFilenames < SET_MAX_FILES)
|
|
125 {
|
|
126 srcFilenames[nsrcFilenames] = currArg;
|
|
127 nsrcFilenames++;
|
|
128 }
|
|
129 else
|
|
130 {
|
|
131 dmError("Maximum number of input files (%i) exceeded!\n",
|
|
132 SET_MAX_FILES);
|
|
133 return FALSE;
|
|
134 }
|
|
135 return TRUE;
|
|
136 }
|
|
137
|
|
138
|
|
139 /* Compare a string to a pattern. Case-SENSITIVE version.
|
|
140 * The matching pattern can consist of any normal characters plus
|
|
141 * wildcards ? and *. "?" matches any character and "*" matches
|
|
142 * any number of characters.
|
|
143 */
|
|
144 BOOL dm_strmatch(const char *str, const char *pattern)
|
|
145 {
|
|
146 BOOL didMatch = TRUE, isAnyMode = FALSE, isEnd = FALSE;
|
|
147 const char *tmpPattern = NULL;
|
|
148
|
|
149 // Check given pattern and string
|
|
150 if (str == NULL || pattern == NULL)
|
|
151 return FALSE;
|
|
152
|
|
153 // Start comparision
|
|
154 do {
|
|
155 didMatch = FALSE;
|
|
156 switch (*pattern)
|
|
157 {
|
|
158 case '?':
|
|
159 // Any single character matches
|
|
160 if (*str)
|
|
161 {
|
|
162 didMatch = TRUE;
|
|
163 pattern++;
|
|
164 str++;
|
|
165 }
|
|
166 break;
|
|
167
|
|
168 case '*':
|
|
169 didMatch = TRUE;
|
|
170 pattern++;
|
|
171 if (!*pattern)
|
|
172 isEnd = TRUE;
|
|
173 isAnyMode = TRUE;
|
|
174 tmpPattern = pattern;
|
|
175 break;
|
|
176
|
|
177 case 0:
|
|
178 if (isAnyMode)
|
|
179 {
|
|
180 if (*str)
|
|
181 str++;
|
|
182 else
|
|
183 isEnd = TRUE;
|
|
184 }
|
|
185 else
|
|
186 {
|
|
187 if (*str)
|
|
188 {
|
|
189 if (tmpPattern)
|
|
190 {
|
|
191 isAnyMode = TRUE;
|
|
192 pattern = tmpPattern;
|
|
193 }
|
|
194 else
|
|
195 didMatch = FALSE;
|
|
196 }
|
|
197 else
|
|
198 isEnd = TRUE;
|
|
199 }
|
|
200 break;
|
|
201 default:
|
|
202 if (isAnyMode)
|
|
203 {
|
|
204 if (*pattern == *str)
|
|
205 {
|
|
206 isAnyMode = FALSE;
|
|
207 didMatch = TRUE;
|
|
208 }
|
|
209 else
|
|
210 {
|
|
211 if (*str)
|
|
212 {
|
|
213 didMatch = TRUE;
|
|
214 str++;
|
|
215 }
|
|
216 }
|
|
217 }
|
|
218 else
|
|
219 {
|
|
220 if (*pattern == *str)
|
|
221 {
|
|
222 didMatch = TRUE;
|
|
223 if (*pattern)
|
|
224 pattern++;
|
|
225 if (*str)
|
|
226 str++;
|
|
227 }
|
|
228 else
|
|
229 {
|
|
230 if (tmpPattern)
|
|
231 {
|
|
232 didMatch = TRUE;
|
|
233 isAnyMode = TRUE;
|
|
234 pattern = tmpPattern;
|
|
235 }
|
|
236 }
|
|
237 }
|
|
238
|
|
239 if (!*str && !*pattern)
|
|
240 isEnd = TRUE;
|
|
241 break;
|
|
242
|
|
243 } // switch
|
|
244
|
|
245 } while (didMatch && !isEnd);
|
|
246
|
|
247 return didMatch;
|
|
248 }
|
|
249
|
|
250
|
|
251 int dmAddFileToPack(DMPackFile *pack, const char *filename, int compression, int resFlags)
|
|
252 {
|
|
253 DMPackEntry *node;
|
|
254 int res = dm_pack_add_file(pack, filename, compression, resFlags, &node);
|
|
255
|
|
256 if (res != DMERR_OK)
|
|
257 {
|
|
258 dmPrint(1, "%-32s [ERROR:%i]\n",
|
|
259 filename, res);
|
|
260 }
|
|
261 else
|
|
262 {
|
|
263 dmPrint(1, "%-32s ['%s', s=%d, c=%d, o=%ld, f=0x%04x]\n",
|
|
264 filename, node->filename,
|
|
265 node->size, node->length, node->offset,
|
|
266 node->resFlags);
|
|
267 }
|
|
268
|
|
269 return res;
|
|
270 }
|
|
271
|
|
272
|
|
273 int main(int argc, char *argv[])
|
|
274 {
|
|
275 int i, res = 0;
|
|
276 DMPackFile *pack = NULL;
|
|
277
|
|
278 stderr = stdout;
|
|
279
|
|
280 // Parse arguments
|
|
281 dmInitProg("packed", "Pack File Editor", "0.4", NULL, NULL);
|
|
282 dmVerbosity = 1;
|
|
283
|
|
284 if (!dmArgsProcess(argc, argv, optList, optListN,
|
|
285 argHandleOpt, argHandleFile, TRUE))
|
|
286 exit(1);
|
|
287
|
|
288 // Check PACK filename
|
|
289 if (optPackFilename == NULL)
|
|
290 optPackFilename = SET_DEFAULT_PACK;
|
|
291
|
|
292 if (optCommand == CMD_NONE)
|
|
293 {
|
|
294 argShowHelp();
|
|
295 dmError("Nothing to do.\n");
|
|
296 exit(0);
|
|
297 return 0;
|
|
298 }
|
|
299
|
|
300 dmMsg(1, "Processing %s ...\n", optPackFilename);
|
|
301
|
|
302 // Execute command
|
|
303 switch (optCommand)
|
|
304 {
|
|
305 case CMD_CREATE:
|
|
306 case CMD_ADD:
|
|
307 switch (optCommand)
|
|
308 {
|
|
309 case CMD_CREATE:
|
|
310 dmMsg(1, "Creating new PACK\n");
|
|
311 res = dm_pack_create(optPackFilename, &pack);
|
|
312 break;
|
|
313
|
|
314 case CMD_ADD:
|
|
315 dmMsg(1, "Opening existing PACK\n");
|
|
316 res = dm_pack_open(optPackFilename, &pack, FALSE);
|
|
317 break;
|
|
318 }
|
|
319
|
|
320 // Add files into PACK
|
|
321 if (res == DMERR_OK)
|
|
322 {
|
|
323 dmMsg(1, "Adding %d files...\n", nsrcFilenames);
|
|
324
|
|
325 for (i = 0; i < nsrcFilenames; i++)
|
|
326 {
|
|
327 // Handle resource definition files
|
|
328 if (srcFilenames[i][0] == '@')
|
|
329 {
|
|
330 }
|
|
331 else
|
|
332 {
|
|
333 dmAddFileToPack(pack, srcFilenames[i], optCompress, optDefResFlags);
|
|
334 }
|
|
335 }
|
|
336
|
|
337 dmMsg(1, "w=%i\n", dm_pack_write(pack));
|
|
338 dmMsg(1, "c=%i\n", dm_pack_close(pack));
|
|
339 }
|
|
340 else
|
|
341 {
|
|
342 dmError("Could not open packfile, error #%i: %s\n", res,
|
|
343 dmErrorStr(res));
|
|
344 }
|
|
345 break;
|
|
346
|
|
347 case CMD_LIST:
|
|
348 // List files in PACK
|
|
349 res = dm_pack_open(optPackFilename, &pack, TRUE);
|
|
350 if (res == DMERR_OK)
|
|
351 {
|
|
352 DMPackEntry *node;
|
|
353 for (i = 0, node = pack->entries; node; i++)
|
|
354 node = node->next;
|
|
355 dmMsg(1, "%d files total\n", i);
|
|
356
|
|
357 for (node = pack->entries; node != NULL; node = node->next)
|
|
358 {
|
|
359 BOOL match;
|
|
360
|
|
361 // Check for matches
|
|
362 if (nsrcFilenames > 0)
|
|
363 {
|
|
364 match = FALSE;
|
|
365 for (i = 0; i < nsrcFilenames && !match; i++)
|
|
366 {
|
|
367 match = dm_strmatch(node->filename, srcFilenames[i]);
|
|
368 }
|
|
369 }
|
|
370 else
|
|
371 match = TRUE;
|
|
372
|
|
373 if (match)
|
|
374 {
|
|
375 // Print one entry
|
|
376 dmPrint(0, "Extracting: %-32s [siz=%d, cmp=%d, offs=0x%08x, flags=0x%04x]\n",
|
|
377 node->filename, node->size, node->length,
|
|
378 node->offset, node->resFlags);
|
|
379 }
|
|
380 }
|
|
381
|
|
382 dmMsg(1, "c=%i\n", dm_pack_close(pack));
|
|
383 }
|
|
384 else
|
|
385 dmError("Could not open packfile, error #%i: %s\n", res,
|
|
386 dmErrorStr(res));
|
|
387 break;
|
|
388
|
|
389 case CMD_EXTRACT:
|
|
390 // Extract files from PACK
|
|
391 res = dm_pack_open(optPackFilename, &pack, TRUE);
|
|
392 if (res == DMERR_OK)
|
|
393 {
|
|
394 DMPackEntry *node;
|
|
395 FILE *resFile = fopen(DMRES_RES_FILE, "w");
|
|
396 if (resFile == NULL)
|
|
397 {
|
|
398 dmError("Could not create resource output file '%s' #%i: %s\n",
|
|
399 DMRES_RES_FILE, errno, strerror(errno));
|
|
400 }
|
|
401
|
|
402 for (node = pack->entries; node != NULL; node = node->next)
|
|
403 {
|
|
404 BOOL match;
|
|
405
|
|
406 // Check for matches
|
|
407 if (nsrcFilenames > 0)
|
|
408 {
|
|
409 match = FALSE;
|
|
410 for (i = 0; (i < nsrcFilenames) && !match; i++)
|
|
411 {
|
|
412 match = dm_strmatch(node->filename, srcFilenames[i]);
|
|
413 }
|
|
414 }
|
|
415 else
|
|
416 match = TRUE;
|
|
417
|
|
418 if (match && (node->privFlags & PACK_EXTRACTED) == 0)
|
|
419 {
|
|
420 // Mark as done
|
|
421 node->privFlags |= PACK_EXTRACTED;
|
|
422
|
|
423 // Print one entry
|
|
424 dmPrint(0, "Extracting: %-32s [siz=%d, cmp=%d, offs=0x%08x, flags=0x%04x]\n",
|
|
425 node->filename, node->size, node->length,
|
|
426 node->offset, node->resFlags);
|
|
427
|
|
428 dm_pack_extract_file(pack, node);
|
|
429
|
|
430 if (resFile != NULL)
|
|
431 {
|
|
432 fprintf(resFile,
|
|
433 "%s|%04x", node->filename, node->resFlags);
|
|
434 }
|
|
435 }
|
|
436 }
|
|
437
|
|
438 dmMsg(1, "c=%i\n", dm_pack_close(pack));
|
|
439
|
|
440 if (resFile != NULL)
|
|
441 fclose(resFile);
|
|
442 }
|
|
443 else
|
|
444 dmError("Could not open packfile, error #%i: %s\n", res,
|
|
445 dmErrorStr(res));
|
|
446 break;
|
|
447
|
|
448 }
|
|
449
|
|
450 return 0;
|
|
451 }
|