2 * Copyright (C) Volition, Inc. 1999. All rights reserved.
4 * All source code herein is the property of Volition, Inc. You may not sell
5 * or otherwise commercially exploit the source or things you created based on
10 * $Logfile: /Freespace2/code/Cfilearchiver/CfileArchiver.cpp $
15 * Program to create an archive file for use with cfile stuff
18 * Revision 1.6 2004/12/15 04:10:45 taylor
19 * outwnd_unix.cpp from fs2_open for logging to file in debug mode
20 * fixes for default function values
21 * always use vm_* functions for sanity sake
22 * make cfilearchiver 64-bit compatible
23 * fix crash on exit from double free()
24 * fix crash on startup from extra long GL extension string in debug
26 * Revision 1.5 2003/05/04 04:49:48 taylor
27 * improve error handling, instructions
29 * Revision 1.4 2003/01/30 20:01:46 relnev
30 * ported (Taylor Richards)
32 * Revision 1.3 2002/06/09 04:41:15 relnev
33 * added copyright header
35 * Revision 1.2 2002/05/07 03:16:43 theoddone33
36 * The Great Newline Fix
38 * Revision 1.1.1.1 2002/05/03 03:28:08 root
42 * 2 10/23/98 6:15p Dave
58 #include <sys/types.h>
63 static int data_error;
66 unsigned int Total_size=16; // Start with size of header
67 unsigned int Num_files =0;
69 FILE * fp_out_hdr = NULL;
71 typedef struct vp_header {
78 //vp_header Vp_header;
80 char archive_dat[1024];
81 char archive_hdr[1024];
83 #define BLOCK_SIZE (1024*1024)
84 #define VERSION_NUMBER 2;
86 char tmp_data[BLOCK_SIZE]; // 1 MB
90 fseek(fp_out, 0, SEEK_SET);
91 fwrite("VPVP", 1, 4, fp_out);
92 int ver = VERSION_NUMBER;
93 fwrite(&ver, 1, 4, fp_out);
94 fwrite(&Total_size, 1, 4, fp_out);
95 fwrite(&Num_files, 1, 4, fp_out);
98 int write_index(char *hf, char *df)
100 FILE *h = fopen(hf, "rb");
102 FILE *d = fopen(df, "a+b");
104 for (unsigned int i=0;i<Num_files;i++) {
105 fread(tmp_data, 32+4+4+4, 1, h);
106 fwrite(tmp_data, 32+4+4+4, 1, d);
113 void pack_file( char *filespec, char *filename, int filesize, fs_time_t time_write )
117 if ( strstr( filename, ".vp" )) {
118 // Don't pack yourself!!
122 if ( strstr( filename, ".hdr" )) {
123 // Don't pack yourself!!
127 if ( filesize == 0 ) {
128 // Don't pack 0 length files, screws up directory structure!
132 memset( path, 0, sizeof(path));
133 strcpy( path, filename );
134 if ( strlen(filename)>31 ) {
135 printf( "Filename '%s' too long\n", filename );
138 fwrite( &Total_size, 1, 4, fp_out_hdr );
139 fwrite( &filesize, 1, 4, fp_out_hdr );
140 fwrite( &path, 1, 32, fp_out_hdr );
141 fwrite( &time_write, 1, sizeof(fs_time_t), fp_out_hdr);
143 Total_size += filesize;
146 printf( "Packing %s\\%s...", filespec, filename );
149 sprintf( path, "%s\\%s", filespec, filename );
151 printf( "Packing %s/%s...", filespec, filename );
154 sprintf( path, "%s/%s", filespec, filename );
157 FILE *fp = fopen( path, "rb" );
160 printf( "Error opening '%s'\n", path );
164 int nbytes, nbytes_read=0;
167 nbytes = fread( tmp_data, 1, BLOCK_SIZE, fp );
169 fwrite( tmp_data, 1, nbytes, fp_out );
170 nbytes_read += nbytes;
173 } while( nbytes > 0 );
177 printf( " %d bytes\n", nbytes_read );
180 // This function adds a directory marker to the header file
181 void add_directory( char * dirname)
184 char *pathptr = path;
187 strcpy(path, dirname);
188 fwrite(&Total_size, 1, 4, fp_out_hdr);
190 fwrite(&i, 1, 4, fp_out_hdr);
191 // strip out any directories that this dir is a subdir of
193 while ((tmpptr = strchr(pathptr, '\\')) != NULL) {
195 while ((tmpptr = strchr(pathptr, '/')) != NULL) {
199 fwrite(pathptr, 1, 32, fp_out_hdr);
200 fwrite(&i, 1, 4, fp_out_hdr); // timestamp = 0
204 void pack_directory( char * filespec)
217 last_slash = strrchr(filespec, '\\');
219 strcpy(dir_name, last_slash+1);
221 strcpy(dir_name, filespec);
224 if ( !SDL_strcasecmp(dir_name, "voice") ) {
232 // strip trailing '/'
233 ts = filespec+(strlen(filespec)-1);
234 while(*ts == '/' && ts > filespec)
237 strcpy( tmp1, filespec );
239 add_directory(filespec);
240 strcat( tmp1, "/*.*" );
242 strcpy( tmp1, filespec );
244 add_directory(filespec);
245 strcat( tmp1, "\\*.*" );
248 printf( "In dir '%s'\n", tmp1 );
251 find_handle = _findfirst( tmp1, &find );
252 if( find_handle != -1 ) {
253 if ( find.attrib & _A_SUBDIR ) {
254 if (strcmp( "..", find.name) && strcmp( ".", find.name)) {
255 strcpy( tmp, filespec );
257 strcat( tmp, find.name );
261 pack_file( filespec, find.name, find.size, (fs_time_t)find.time_write );
264 while( !_findnext( find_handle, &find ) ) {
265 if ( find.attrib & _A_SUBDIR ) {
266 if (strcmp( "..", find.name) && strcmp( ".", find.name)) {
267 strcpy( tmp, filespec );
269 strcat( tmp, find.name );
274 pack_file( filespec, find.name, find.size, find.time_write );
282 dirp = opendir (filespec);
284 while ((dir = readdir(dirp)) != NULL) {
287 snprintf(fn, MAX_PATH-1, "%s/%s", filespec, dir->d_name);
291 if (stat(fn, &buf) == -1) {
295 if ( (strcmp(dir->d_name, ".") == 0) || (strcmp(dir->d_name, "..") == 0) ) {
299 if (S_ISDIR(buf.st_mode)) {
300 strcpy( tmp, filespec );
302 strcat( tmp, dir->d_name );
305 pack_file( filespec, dir->d_name, buf.st_size, buf.st_mtime );
310 printf("Error: Source directory does not exist!\n");
317 int verify_directory( char *filespec )
322 // strip trailing '/'
323 ts = filespec+(strlen(filespec)-1);
324 while(*ts == '/' && ts > filespec)
327 // make sure last directory is named "data", ignoring case
328 dd = filespec+(strlen(filespec)-4);
329 if ( SDL_strcasecmp( dd, "data" ) )
335 void print_instructions()
337 printf( "Creates a vp archive out of a FreeSpace data tree.\n\n" );
338 printf( "Usage: cfilearchiver archive_name src_dir\n");
339 printf( "Example: cfilearchiver freespace /tmp/freespace/data\n\n");
340 printf( "Directory structure options:\n" );
341 printf( " Effects (.ani .pcx .neb .tga)\n" );
342 printf( " Fonts (.vf)\n" );
343 printf( " Hud (.ani .pcx .tga\n" );
344 printf( " Interface (.pcx .ani .tga)\n" );
345 printf( " Maps (.pcx .ani .tga)\n" );
346 printf( " Missions (.ntl .ssv), FS1(.fsm .fsc), FS2(.fs2 .fc2)\n" );
347 printf( " Models (.pof)\n" );
348 printf( " Music (.wav)\n" );
349 printf( " Sounds/8b22k (.wav)\n" );
350 printf( " Sounds/16b11k (.wav)\n" );
351 printf( " Tables (.tbl)\n" );
352 printf( " Voice/Briefing (.wav)\n" );
353 printf( " Voice/Command briefings (.wav)\n" );
354 printf( " Voice/Debriefing (.wav)\n" );
355 printf( " Voice/Personas (.wav)\n" );
356 printf( " Voice/Special (.wav)\n" );
357 printf( " Voice/Training (.wav)\n" );
362 int main(int argc, char *argv[] )
369 printf( "Usage: %s archive_name src_dir\n", argv[0] );
370 printf( "Example: %s freespace c:\\freespace\\data\n", argv[0] );
371 printf( "Creates an archive named freespace out of the\nfreespace data tree\n" );
372 printf( "Press any key to exit...\n" );
376 print_instructions();
380 strcpy( archive, argv[1] );
381 p = strchr( archive, '.' );
382 if (p) *p = 0; // remove extension
384 strcpy( archive_dat, archive );
385 strcat( archive_dat, ".vp" );
387 strcpy( archive_hdr, archive );
388 strcat( archive_hdr, ".hdr" );
390 fp_out = fopen( archive_dat, "wb" );
392 printf( "Couldn't open '%s'!\n", archive_dat );
394 printf( "Press any key to exit...\n" );
402 fp_out_hdr = fopen( archive_hdr, "wb" );
404 printf( "Couldn't open '%s'!\n", archive_hdr );
406 printf( "Press any key to exit...\n" );
414 if ( verify_directory( argv[2] ) != 0 ) {
415 printf("Warning! Last directory must be named \"data\" (not case sensitive)\n");
421 pack_directory( argv[2] );
423 // in case the directory doesn't exist
432 printf( "Data files written, appending index...\n" );
434 if (!write_index(archive_hdr, archive_dat)) {
435 printf("Error appending index!\n");
437 printf("Press any key to exit...\n");
443 printf( "%d total KB.\n", Total_size/1024 );