]> git.mxchange.org Git - quix0rs-blobwars.git/blob - src/pak.cpp
d70fe571864cc15227742e257cd782486f9b5567
[quix0rs-blobwars.git] / src / pak.cpp
1 /*
2 Copyright (C) 2005 Parallel Realities
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.
12
13 See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19 */
20
21 #include "pak.h"
22
23 FILE *pak;
24 int dirs = 0, files = 0;
25 int totalFiles = 0;
26 Bytef *buffer;
27 Bytef *output;
28
29 FileData *fileData = NULL;
30
31 void cleanup()
32 {
33         delete[] buffer;
34         delete[] output;
35 }
36
37 void countFiles(const char *dirName)
38 {
39         DIR *dirp, *dirp2;
40         dirent *dfile;
41         dirp = opendir(dirName);
42         char filename[1024];
43
44         while ((dfile = readdir(dirp)))
45         {
46                 if (dfile->d_name[0] == '.')
47                 {
48                         continue;
49                 }
50
51                 snprintf(filename, sizeof filename, "%s/%s", dirName, dfile->d_name);
52                 
53                 if (strlen(filename) > PAK_MAX_FILENAME - 1)
54                 {
55                         printf("\nERROR - '%s' exceeds maximum defined file length of %d\n", filename, PAK_MAX_FILENAME);
56                         exit(1);
57                 }
58
59                 dirp2 = opendir(filename);
60
61                 if (dirp2)
62                 {
63                         closedir(dirp2);
64                         countFiles(filename);
65                 }
66                 else
67                 {
68                         totalFiles++;
69                 }
70         }
71
72         closedir(dirp);
73         
74         fileData = new FileData[totalFiles];
75 }
76
77 void recurseDirectory(const char *dirName)
78 {
79         DIR *dirp, *dirp2;
80         dirent *dfile;
81         gzFile fp;
82         FILE *infile;
83         char filename[1024];
84
85         uLongf cSize = 0;
86         uLongf fSize = 0;
87
88         dirp = opendir(dirName);
89
90         if (dirp == NULL)
91         {
92                 printf("%s: Directory does not exist or is not accessable\n", dirName);
93                 return;
94         }
95         
96         float percentage;
97         long filesize;
98
99         while ((dfile = readdir(dirp)))
100         {
101                 if (dfile->d_name[0] == '.')
102                 {
103                         continue;
104                 }
105
106                 snprintf(filename, sizeof filename, "%s/%s", dirName, dfile->d_name);
107
108                 dirp2 = opendir(filename);
109
110                 if (dirp2)
111                 {
112                         closedir(dirp2);
113                         recurseDirectory(filename);
114                 }
115                 else
116                 {
117                         infile = fopen(filename, "rb");
118                         if (!infile)
119                         {
120                                 printf("Couldn't open %s for reading!\n", filename);
121                                 closedir(dirp);
122                                 gzclose(pak);
123                                 exit(1);
124                         }
125                         
126                         fseek(infile, SEEK_SET, SEEK_END);
127                         
128                         filesize = ftell(infile);
129                         
130                         fclose(infile);
131                         
132                         delete[] buffer;
133                         buffer = new unsigned char[filesize];
134                         
135                         delete[] output;
136                         output = new unsigned char[(int)(filesize * 1.01) + 12];
137                         
138                         fp = gzopen(filename, "rb");
139
140                         if (!fp)
141                         {
142                                 printf("Couldn't open %s for reading!\n", filename);
143                                 closedir(dirp);
144                                 gzclose(pak);
145                                 exit(1);
146                         }
147                         else
148                         {
149                                 fSize = gzread(fp, buffer, filesize);
150                                 gzclose(fp);
151
152                                 cSize = (uLongf)((fSize * 1.01) + 12);
153                                 compress2(output, &cSize, buffer, fSize, 9);
154                                 
155                                 fileData[files].set(filename, fSize, cSize, ftell(pak));
156
157                                 fwrite(output, 1, cSize, pak);
158
159                                 files++;
160                                 
161                                 percentage = files;
162                                 percentage /= totalFiles;
163                                 percentage *= 100;
164
165                                 printf("\b\b\b\b%3.0f%%", percentage);
166                                 fflush(stdout);
167                         }
168                 }
169         }
170
171         closedir(dirp);
172
173         dirs++;
174 }
175
176 int main(int argc, char *argv[])
177 {
178         if (argc < 3)
179         {
180                 printf("Usage   : pak <directory names> <outputname>\n");
181                 printf("Example : pak data music gfx sound data.pak\n");
182                 exit(1);
183         }
184
185         pak = fopen(argv[argc - 1], "wb");
186         
187         for (int i = 1 ; i < (argc - 1) ; i++)
188         {
189                 countFiles(argv[i]);
190         }
191                 
192         printf("Paking...000%%");
193         fflush(stdout);
194         
195         output = NULL;
196         buffer = NULL;
197         
198         atexit(cleanup);
199
200         for (int i = 1 ; i < (argc - 1) ; i++)
201         {
202                 recurseDirectory(argv[i]);
203         }
204
205         unsigned int pos = ftell(pak);
206
207         for (int i = 0 ; i < files ; i++)
208         {
209                 if (fileData[i].fSize == 0)
210                 {
211                         break;
212                 }
213
214                 fwrite(&fileData[i], sizeof(FileData), 1, pak);
215         }
216         
217         unsigned int numberOfFiles = totalFiles;
218
219         fwrite(&pos, sizeof(unsigned int), 1, pak);
220         fwrite(&numberOfFiles, sizeof(unsigned int), 1, pak);
221
222         fclose(pak);
223
224         printf("\nPak: All Done. Added %d files\n", numberOfFiles);
225
226         return 0;
227 }