2 Copyright (C) 2004-2011 Parallel Realities
3 Copyright (C) 2011-2015 Perpendicular Dimensions
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 See the GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
43 void Pak::showPakErrorAndExit()
45 printf("\nFatal Error: The Blob Wars PAK file was either not found or was not accessable.\n");
46 printf("(If you compiled from source did you forget to run make install?)\n");
47 printf("The path to the file was expected to be,\n\n");
48 printf(PAKFULLPATH"\n\n");
49 printf("Please try running the game again. If problems persist either reinstall the game or check,\n\n");
50 printf("http://www.parallelrealities.co.uk/blobWars.php\n\n");
51 printf("for updates.\n\n");
55 void Pak::setPakFile(const char *pakFilename)
58 strlcpy(this->pakFilename, pakFilename, sizeof this->pakFilename);
60 debug(("Pak : Filename set to %s\n", pakFilename));
62 FILE *pak = fopen(pakFilename, "rb");
66 showPakErrorAndExit();
69 fseek(pak, (-sizeof(Uint32)) * 2, SEEK_END);
70 if (fread(&listPos, sizeof(Uint32), 1, pak) != 1)
73 showPakErrorAndExit();
75 if (fread(&numberOfFiles, sizeof(Uint32), 1, pak) != 1)
78 showPakErrorAndExit();
81 debug(("Pak : File list resides at %d\n", (int)listPos));
82 debug(("Pak : Number of files are %d\n", (int)numberOfFiles));
84 fd = new FileData[numberOfFiles];
86 fseek(pak, listPos, SEEK_SET);
90 for (unsigned int i = 0 ; i < numberOfFiles ; i++)
92 result = fread(&fd[i], sizeof(FileData), 1, pak);
97 showPakErrorAndExit();
100 debug(("Read FileData #%d / %d : %s\n", (i + 1), numberOfFiles, fd[i].filename));
108 bool Pak::unpack(const char *filename, unsigned char **buffer)
110 debug(("Pak : Unpacking %s...\n", filename));
114 for (unsigned int i = 0 ; i < numberOfFiles ; i++)
116 if (strcmp(filename, fd[i].filename) == 0)
118 currentFile = &fd[i];
123 if (currentFile == NULL)
128 FILE *pak = fopen(pakFilename, "rb");
131 showPakErrorAndExit();
134 fseek(pak, currentFile->location, SEEK_SET);
139 input = new unsigned char[(int)(currentFile->cSize * 1.01) + 12];
140 *buffer = new unsigned char[currentFile->fSize + 1];
142 if (fread(input, 1, currentFile->cSize, pak) != currentFile->cSize)
145 showPakErrorAndExit();
148 uLongf fSize = (uLongf)currentFile->fSize;
150 uncompress(*buffer, &fSize, input, currentFile->cSize);
151 (*buffer)[currentFile->fSize] = 0;
158 debug(("Pak : Unpack %s...Done\n", filename));
163 bool Pak::fileExists(const char *filename)
165 for (unsigned int i = 0 ; i < numberOfFiles ; i++)
167 if (strcmp(fd[i].filename, filename) == 0)
176 unsigned int Pak::getUncompressedSize() const
178 return (unsigned int)currentFile->fSize;