| 1 | /* |
| 2 | Copyright (C) 2006 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 | if (buffer != NULL) |
| 34 | { |
| 35 | delete[] buffer; |
| 36 | } |
| 37 | |
| 38 | if (output != NULL) |
| 39 | { |
| 40 | delete[] output; |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | void countFiles(char *dirName) |
| 45 | { |
| 46 | DIR *dirp, *dirp2; |
| 47 | dirent *dfile; |
| 48 | dirp = opendir(dirName); |
| 49 | char filename[1024]; |
| 50 | |
| 51 | while ((dfile = readdir(dirp))) |
| 52 | { |
| 53 | if (dfile->d_name[0] == '.') |
| 54 | { |
| 55 | continue; |
| 56 | } |
| 57 | |
| 58 | sprintf(filename, "%s/%s", dirName, dfile->d_name); |
| 59 | |
| 60 | if (strlen(filename) > PAK_MAX_FILENAME - 1) |
| 61 | { |
| 62 | printf("\nERROR - '%s' exceeds maximum defined file length of %d\n", filename, PAK_MAX_FILENAME); |
| 63 | exit(1); |
| 64 | } |
| 65 | |
| 66 | dirp2 = opendir(filename); |
| 67 | |
| 68 | if (dirp2) |
| 69 | { |
| 70 | closedir(dirp2); |
| 71 | countFiles(filename); |
| 72 | } |
| 73 | else |
| 74 | { |
| 75 | totalFiles++; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | closedir(dirp); |
| 80 | |
| 81 | fileData = new FileData[totalFiles]; |
| 82 | } |
| 83 | |
| 84 | void recurseDirectory(char *dirName) |
| 85 | { |
| 86 | DIR *dirp, *dirp2; |
| 87 | dirent *dfile; |
| 88 | gzFile fp; |
| 89 | FILE *infile; |
| 90 | char filename[1024]; |
| 91 | const char *f; |
| 92 | |
| 93 | uLongf cSize = 0; |
| 94 | uLongf fSize = 0; |
| 95 | |
| 96 | dirp = opendir(dirName); |
| 97 | |
| 98 | if (dirp == NULL) |
| 99 | { |
| 100 | printf("%s: Directory does not exist or is not accessable\n", dirName); |
| 101 | return; |
| 102 | } |
| 103 | |
| 104 | float percentage; |
| 105 | long filesize; |
| 106 | |
| 107 | while ((dfile = readdir(dirp))) |
| 108 | { |
| 109 | if (dfile->d_name[0] == '.') |
| 110 | { |
| 111 | continue; |
| 112 | } |
| 113 | |
| 114 | sprintf(filename, "%s/%s", dirName, dfile->d_name); |
| 115 | |
| 116 | dirp2 = opendir(filename); |
| 117 | |
| 118 | if (dirp2) |
| 119 | { |
| 120 | closedir(dirp2); |
| 121 | recurseDirectory(filename); |
| 122 | } |
| 123 | else |
| 124 | { |
| 125 | infile = fopen(filename, "rb"); |
| 126 | if (!infile) |
| 127 | { |
| 128 | printf("Couldn't open %s for reading!\n", filename); |
| 129 | closedir(dirp); |
| 130 | gzclose(pak); |
| 131 | exit(1); |
| 132 | } |
| 133 | |
| 134 | fseek(infile, SEEK_SET, SEEK_END); |
| 135 | |
| 136 | filesize = ftell(infile); |
| 137 | |
| 138 | fclose(infile); |
| 139 | |
| 140 | if (buffer != NULL) |
| 141 | { |
| 142 | delete[] buffer; |
| 143 | buffer = NULL; |
| 144 | } |
| 145 | |
| 146 | buffer = new unsigned char[filesize]; |
| 147 | |
| 148 | if (output != NULL) |
| 149 | { |
| 150 | delete[] output; |
| 151 | output = NULL; |
| 152 | } |
| 153 | |
| 154 | output = new unsigned char[(int)(filesize * 1.01) + 12]; |
| 155 | |
| 156 | fp = gzopen(filename, "rb"); |
| 157 | |
| 158 | if (!fp) |
| 159 | { |
| 160 | printf("Couldn't open %s for reading!\n", filename); |
| 161 | closedir(dirp); |
| 162 | gzclose(pak); |
| 163 | exit(1); |
| 164 | } |
| 165 | else |
| 166 | { |
| 167 | fSize = gzread(fp, buffer, filesize); |
| 168 | gzclose(fp); |
| 169 | |
| 170 | cSize = (uLongf)((fSize * 1.01) + 12); |
| 171 | compress2(output, &cSize, buffer, fSize, 9); |
| 172 | |
| 173 | f = filename; |
| 174 | |
| 175 | fileData[files].set(f, fSize, cSize, ftell(pak)); |
| 176 | |
| 177 | fwrite(output, 1, cSize, pak); |
| 178 | |
| 179 | files++; |
| 180 | |
| 181 | percentage = files; |
| 182 | percentage /= totalFiles; |
| 183 | percentage *= 100; |
| 184 | |
| 185 | printf("\b\b\b\b%3.0f%%", percentage); |
| 186 | fflush(stdout); |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | closedir(dirp); |
| 192 | |
| 193 | dirs++; |
| 194 | } |
| 195 | |
| 196 | int main(int argc, char *argv[]) |
| 197 | { |
| 198 | if (argc < 3) |
| 199 | { |
| 200 | printf("Usage : pak <directory names> <outputname>\n"); |
| 201 | printf("Example : pak data music gfx sound data.pak\n"); |
| 202 | exit(1); |
| 203 | } |
| 204 | |
| 205 | pak = fopen(argv[argc - 1], "wb"); |
| 206 | |
| 207 | for (int i = 1 ; i < (argc - 1) ; i++) |
| 208 | { |
| 209 | countFiles(argv[i]); |
| 210 | } |
| 211 | |
| 212 | printf("Paking...000%%"); |
| 213 | fflush(stdout); |
| 214 | |
| 215 | output = NULL; |
| 216 | buffer = NULL; |
| 217 | |
| 218 | atexit(cleanup); |
| 219 | |
| 220 | for (int i = 1 ; i < (argc - 1) ; i++) |
| 221 | { |
| 222 | recurseDirectory(argv[i]); |
| 223 | } |
| 224 | |
| 225 | unsigned int pos = ftell(pak); |
| 226 | |
| 227 | for (int i = 0 ; i < files ; i++) |
| 228 | { |
| 229 | if (fileData[i].fSize == 0) |
| 230 | { |
| 231 | break; |
| 232 | } |
| 233 | |
| 234 | fwrite(&fileData[i], sizeof(FileData), 1, pak); |
| 235 | } |
| 236 | |
| 237 | unsigned int numberOfFiles = totalFiles; |
| 238 | |
| 239 | pos = SDL_SwapLE32(pos); |
| 240 | numberOfFiles = SDL_SwapLE32(numberOfFiles); |
| 241 | |
| 242 | fwrite(&pos, sizeof(unsigned int), 1, pak); |
| 243 | fwrite(&numberOfFiles, sizeof(unsigned int), 1, pak); |
| 244 | |
| 245 | fclose(pak); |
| 246 | |
| 247 | printf("\nPak: All Done. Added %d files\n", numberOfFiles); |
| 248 | |
| 249 | return 0; |
| 250 | } |