]> git.mxchange.org Git - quix0rs-blobwars.git/blob - src/CPak.cpp
Update copyright statements.
[quix0rs-blobwars.git] / src / CPak.cpp
1 /*
2 Copyright (C) 2004-2011 Parallel Realities
3 Copyright (C) 2011-2015 Perpendicular Dimensions
4
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.
9
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.
13
14 See the GNU General Public License for more details.
15
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.
19
20 */
21
22 #include "headers.h"
23
24 Pak::Pak()
25 {
26         input = NULL;
27         fd = NULL;
28         
29         numberOfFiles = 0;
30         listPos = 0;
31         currentFile = NULL;
32
33         pakFilename[0] = 0;
34         filename[0] = 0;
35 }
36
37 Pak::~Pak()
38 {
39         delete[] input;
40         delete[] fd;
41 }
42
43 void Pak::showPakErrorAndExit()
44 {
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");
52         exit(1);
53 }
54
55 void Pak::setPakFile(const char *pakFilename)
56 {
57         #if USEPAK
58         strlcpy(this->pakFilename, pakFilename, sizeof this->pakFilename);
59         
60         debug(("Pak : Filename set to %s\n", pakFilename));
61
62         FILE *pak = fopen(pakFilename, "rb");
63
64         if (!pak)
65         {
66                 showPakErrorAndExit();
67         }
68
69         fseek(pak, (-sizeof(Uint32)) * 2, SEEK_END);
70         if (fread(&listPos, sizeof(Uint32), 1, pak) != 1)
71         {
72                 fclose(pak);
73                 showPakErrorAndExit();
74         }
75         if (fread(&numberOfFiles, sizeof(Uint32), 1, pak) != 1)
76         {
77                 fclose(pak);
78                 showPakErrorAndExit();
79         }
80         
81         debug(("Pak : File list resides at %d\n", (int)listPos));
82         debug(("Pak : Number of files are %d\n", (int)numberOfFiles));
83         
84         fd = new FileData[numberOfFiles];
85         
86         fseek(pak, listPos, SEEK_SET);
87         
88         int result = 0;
89         
90         for (unsigned int i = 0 ; i < numberOfFiles ; i++)
91         {
92                 result = fread(&fd[i], sizeof(FileData), 1, pak);
93                 
94                 if (!result)
95                 {
96                         fclose(pak);
97                         showPakErrorAndExit();
98                 }
99                 
100                 debug(("Read FileData #%d / %d : %s\n", (i + 1), numberOfFiles, fd[i].filename));
101         }
102         
103         fclose(pak);
104         
105         #endif
106 }
107
108 bool Pak::unpack(const char *filename, unsigned char **buffer)
109 {
110         debug(("Pak : Unpacking %s...\n", filename));
111         
112         currentFile = NULL;
113         
114         for (unsigned int i = 0 ; i < numberOfFiles ; i++)
115         {
116                 if (strcmp(filename, fd[i].filename) == 0)
117                 {
118                         currentFile = &fd[i];
119                         break;
120                 }
121         }
122         
123         if (currentFile == NULL)
124         {
125                 return false;
126         }
127         
128         FILE *pak = fopen(pakFilename, "rb");
129         if (!pak)
130         {
131                 showPakErrorAndExit();
132         }
133         
134         fseek(pak, currentFile->location, SEEK_SET);
135
136         delete[] input;
137         input = NULL;
138         
139         input = new unsigned char[(int)(currentFile->cSize * 1.01) + 12];
140         *buffer = new unsigned char[currentFile->fSize + 1];
141
142         if (fread(input, 1, currentFile->cSize, pak) != currentFile->cSize)
143         {
144                 fclose(pak);
145                 showPakErrorAndExit();
146         }
147         
148         uLongf fSize = (uLongf)currentFile->fSize;
149         
150         uncompress(*buffer, &fSize, input, currentFile->cSize);
151         (*buffer)[currentFile->fSize] = 0;
152
153         fclose(pak);
154         
155         delete[] input;
156         input = NULL;
157         
158         debug(("Pak : Unpack %s...Done\n", filename));
159
160         return true;
161 }
162
163 bool Pak::fileExists(const char *filename)
164 {
165         for (unsigned int i = 0 ; i < numberOfFiles ; i++)
166         {
167                 if (strcmp(fd[i].filename, filename) == 0)
168                 {
169                         return true;
170                 }
171         }
172         
173         return false;
174 }
175
176 unsigned int Pak::getUncompressedSize() const
177 {
178         return (unsigned int)currentFile->fSize;
179 }