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.4 2003/01/30 20:01:46 relnev
19 * ported (Taylor Richards)
21 * Revision 1.3 2002/06/09 04:41:15 relnev
22 * added copyright header
24 * Revision 1.2 2002/05/07 03:16:43 theoddone33
25 * The Great Newline Fix
27 * Revision 1.1.1.1 2002/05/03 03:28:08 root
31 * 2 10/23/98 6:15p Dave
47 #include <sys/types.h>
52 unsigned int Total_size=16; // Start with size of header
53 unsigned int Num_files =0;
55 FILE * fp_out_hdr = NULL;
57 typedef struct vp_header {
64 //vp_header Vp_header;
66 char archive_dat[1024];
67 char archive_hdr[1024];
69 #define BLOCK_SIZE (1024*1024)
70 #define VERSION_NUMBER 2;
72 char tmp_data[BLOCK_SIZE]; // 1 MB
76 fseek(fp_out, 0, SEEK_SET);
77 fwrite("VPVP", 1, 4, fp_out);
78 int ver = VERSION_NUMBER;
79 fwrite(&ver, 1, 4, fp_out);
80 fwrite(&Total_size, 1, 4, fp_out);
81 fwrite(&Num_files, 1, 4, fp_out);
84 int write_index(char *hf, char *df)
86 FILE *h = fopen(hf, "rb");
88 FILE *d = fopen(df, "a+b");
90 for (unsigned int i=0;i<Num_files;i++) {
91 fread(tmp_data, 32+4+4+4, 1, h);
92 fwrite(tmp_data, 32+4+4+4, 1, d);
99 void pack_file( char *filespec, char *filename, int filesize, time_t time_write )
103 if ( strstr( filename, ".vp" )) {
104 // Don't pack yourself!!
108 if ( strstr( filename, ".hdr" )) {
109 // Don't pack yourself!!
113 if ( filesize == 0 ) {
114 // Don't pack 0 length files, screws up directory structure!
118 memset( path, 0, sizeof(path));
119 strcpy( path, filename );
120 if ( strlen(filename)>31 ) {
121 printf( "Filename '%s' too long\n", filename );
124 fwrite( &Total_size, 1, 4, fp_out_hdr );
125 fwrite( &filesize, 1, 4, fp_out_hdr );
126 fwrite( &path, 1, 32, fp_out_hdr );
127 fwrite( &time_write, 1, sizeof(time_t), fp_out_hdr);
129 Total_size += filesize;
132 printf( "Packing %s\\%s...", filespec, filename );
135 sprintf( path, "%s\\%s", filespec, filename );
137 printf( "Packing %s/%s...", filespec, filename );
140 sprintf( path, "%s/%s", filespec, filename );
143 FILE *fp = fopen( path, "rb" );
146 printf( "Error opening '%s'\n", path );
150 int nbytes, nbytes_read=0;
153 nbytes = fread( tmp_data, 1, BLOCK_SIZE, fp );
155 fwrite( tmp_data, 1, nbytes, fp_out );
156 nbytes_read += nbytes;
159 } while( nbytes > 0 );
163 printf( " %d bytes\n", nbytes_read );
166 // This function adds a directory marker to the header file
167 void add_directory( char * dirname)
170 char *pathptr = path;
173 strcpy(path, dirname);
174 fwrite(&Total_size, 1, 4, fp_out_hdr);
176 fwrite(&i, 1, 4, fp_out_hdr);
177 // strip out any directories that this dir is a subdir of
179 while ((tmpptr = strchr(pathptr, '\\')) != NULL) {
181 while ((tmpptr = strchr(pathptr, '/')) != NULL) {
185 fwrite(pathptr, 1, 32, fp_out_hdr);
186 fwrite(&i, 1, 4, fp_out_hdr); // timestamp = 0
190 void pack_directory( char * filespec)
203 last_slash = strrchr(filespec, '\\');
205 strcpy(dir_name, last_slash+1);
207 strcpy(dir_name, filespec);
210 if ( !stricmp(dir_name, "voice") ) {
215 strcpy( tmp1, filespec );
217 // space for more unfinished things here
220 add_directory(filespec);
221 strcat( tmp1, "/*.*" );
223 add_directory(filespec);
224 strcat( tmp1, "\\*.*" );
227 printf( "In dir '%s'\n", tmp1 );
230 find_handle = _findfirst( tmp1, &find );
231 if( find_handle != -1 ) {
232 if ( find.attrib & _A_SUBDIR ) {
233 if (strcmp( "..", find.name) && strcmp( ".", find.name)) {
234 strcpy( tmp, filespec );
236 strcat( tmp, find.name );
240 pack_file( filespec, find.name, find.size, find.time_write );
243 while( !_findnext( find_handle, &find ) ) {
244 if ( find.attrib & _A_SUBDIR ) {
245 if (strcmp( "..", find.name) && strcmp( ".", find.name)) {
246 strcpy( tmp, filespec );
248 strcat( tmp, find.name );
253 pack_file( filespec, find.name, find.size, find.time_write );
261 dirp = opendir (filespec);
263 while ((dir = readdir(dirp)) != NULL) {
266 snprintf(fn, MAX_PATH-1, "%s/%s", filespec, dir->d_name);
270 if (stat(fn, &buf) == -1) {
274 if ( (strcmp(dir->d_name, ".") == 0) || (strcmp(dir->d_name, "..") == 0) ) {
278 if (S_ISDIR(buf.st_mode)) {
279 strcpy( tmp, filespec );
281 strcat( tmp, dir->d_name );
284 pack_file( filespec, dir->d_name, buf.st_size, buf.st_mtime );
295 int main(int argc, char *argv[] )
302 printf( "Usage: %s archive_name src_dir\n", argv[0] );
303 printf( "Example: %s freespace c:\\freespace\\data\n", argv[0] );
304 printf( "Creates an archive named freespace out of the\nfreespace data tree\n" );
305 printf( "Press any key to exit...\n" );
308 printf( "Creates a vp archive out of a FreeSpace data tree.\n\n" );
309 printf( "Usage: %s archive_name src_dir\n", argv[0] );
310 printf( "Example: %s freespace /tmp/freespace/data\n", argv[0] );
311 printf( "NOTE: last directory must be named \"data\", no trailing \"/\"\n\n" );
312 printf( "Directory structure options:\n" );
313 printf( " Effects (.ani .pcx .neb .tga)\n" );
314 printf( " Fonts (.vf)\n" );
315 printf( " Hud (.ani .pcx .tga\n" );
316 printf( " Interface (.pcx .ani .tga)\n" );
317 printf( " Maps (.pcx .ani .tga)\n" );
318 printf( " Missions (.ntl .ssv), FS1(.fsm .fsc), FS2(.fs2 .fc2)\n" );
319 printf( " Models (.pof)\n" );
320 printf( " Music (.wav)\n" );
321 printf( " Sounds/8b22k (.wav)\n" );
322 printf( " Sounds/16b11k (.wav)\n" );
323 printf( " Tables (.tbl)\n" );
324 printf( " Voice/Briefing (.wav)\n" );
325 printf( " Voice/Command briefings (.wav)\n" );
326 printf( " Voice/Debriefing (.wav)\n" );
327 printf( " Voice/Personas (.wav)\n" );
328 printf( " Voice/Special (.wav)\n" );
329 printf( " Voice/Training (.wav)\n" );
334 strcpy( archive, argv[1] );
335 p = strchr( archive, '.' );
336 if (p) *p = 0; // remove extension
338 strcpy( archive_dat, archive );
339 strcat( archive_dat, ".vp" );
341 strcpy( archive_hdr, archive );
342 strcat( archive_hdr, ".hdr" );
344 fp_out = fopen( archive_dat, "wb" );
346 printf( "Couldn't open '%s'!\n", archive_dat );
348 printf( "Press any key to exit...\n" );
354 fp_out_hdr = fopen( archive_hdr, "wb" );
356 printf( "Couldn't open '%s'!\n", archive_hdr );
358 printf( "Press any key to exit...\n" );
366 pack_directory( argv[2] );
373 printf( "Data files written, appending index...\n" );
375 if (!write_index(archive_hdr, archive_dat)) {
376 printf("Error appending index!\n");
378 printf("Press any key to exit...\n");
384 printf( "%d total KB.\n", Total_size/1024 );