]> git.mxchange.org Git - quix0rs-blobwars.git/blob - src/CPak.cpp
Don't link pak tool with SDL.
[quix0rs-blobwars.git] / src / CPak.cpp
1 /*
2 Copyright (C) 2004-2011 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 "headers.h"
22
23 Pak::Pak()
24 {
25         input = NULL;
26         fd = NULL;
27         
28         numberOfFiles = 0;
29         listPos = 0;
30         currentFile = NULL;
31
32         pakFilename[0] = 0;
33         filename[0] = 0;
34 }
35
36 Pak::~Pak()
37 {
38         delete[] input;
39         delete[] fd;
40 }
41
42 void Pak::showPakErrorAndExit()
43 {
44         printf("\nFatal Error: The Blob Wars PAK file was either not found or was not accessable.\n");
45         printf("(If you compiled from source did you forget to run make install?)\n");
46         printf("The path to the file was expected to be,\n\n");
47         printf(PAKFULLPATH"\n\n");
48         printf("Please try running the game again. If problems persist either reinstall the game or check,\n\n");
49         printf("http://www.parallelrealities.co.uk/blobWars.php\n\n");
50         printf("for updates.\n\n");
51         exit(1);
52 }
53
54 void Pak::setPakFile(const char *pakFilename)
55 {
56         #if USEPAK
57         strlcpy(this->pakFilename, pakFilename, sizeof this->pakFilename);
58         
59         debug(("Pak : Filename set to %s\n", pakFilename));
60
61         FILE *pak = fopen(pakFilename, "rb");
62
63         if (!pak)
64         {
65                 showPakErrorAndExit();
66         }
67
68         fseek(pak, (-sizeof(Uint32)) * 2, SEEK_END);
69         if (fread(&listPos, sizeof(Uint32), 1, pak) != 1)
70         {
71                 fclose(pak);
72                 showPakErrorAndExit();
73         }
74         if (fread(&numberOfFiles, sizeof(Uint32), 1, pak) != 1)
75         {
76                 fclose(pak);
77                 showPakErrorAndExit();
78         }
79         
80         debug(("Pak : File list resides at %d\n", (int)listPos));
81         debug(("Pak : Number of files are %d\n", (int)numberOfFiles));
82         
83         fd = new FileData[numberOfFiles];
84         
85         fseek(pak, listPos, SEEK_SET);
86         
87         int result = 0;
88         
89         for (unsigned int i = 0 ; i < numberOfFiles ; i++)
90         {
91                 result = fread(&fd[i], sizeof(FileData), 1, pak);
92                 
93                 if (!result)
94                 {
95                         fclose(pak);
96                         showPakErrorAndExit();
97                 }
98                 
99                 debug(("Read FileData #%d / %d : %s\n", (i + 1), numberOfFiles, fd[i].filename));
100         }
101         
102         fclose(pak);
103         
104         #endif
105 }
106
107 bool Pak::unpack(const char *filename, unsigned char **buffer)
108 {
109         debug(("Pak : Unpacking %s...\n", filename));
110         
111         currentFile = NULL;
112         
113         for (unsigned int i = 0 ; i < numberOfFiles ; i++)
114         {
115                 if (strcmp(filename, fd[i].filename) == 0)
116                 {
117                         currentFile = &fd[i];
118                         break;
119                 }
120         }
121         
122         if (currentFile == NULL)
123         {
124                 return false;
125         }
126         
127         FILE *pak = fopen(pakFilename, "rb");
128         if (!pak)
129         {
130                 showPakErrorAndExit();
131         }
132         
133         fseek(pak, currentFile->location, SEEK_SET);
134
135         delete[] input;
136         input = NULL;
137         
138         input = new unsigned char[(int)(currentFile->cSize * 1.01) + 12];
139         *buffer = new unsigned char[currentFile->fSize + 1];
140
141         if (fread(input, 1, currentFile->cSize, pak) != currentFile->cSize)
142         {
143                 fclose(pak);
144                 showPakErrorAndExit();
145         }
146         
147         uLongf fSize = (uLongf)currentFile->fSize;
148         
149         uncompress(*buffer, &fSize, input, currentFile->cSize);
150         (*buffer)[currentFile->fSize] = 0;
151
152         fclose(pak);
153         
154         delete[] input;
155         input = NULL;
156         
157         debug(("Pak : Unpack %s...Done\n", filename));
158
159         return true;
160 }
161
162 bool Pak::fileExists(const char *filename)
163 {
164         for (unsigned int i = 0 ; i < numberOfFiles ; i++)
165         {
166                 if (strcmp(fd[i].filename, filename) == 0)
167                 {
168                         return true;
169                 }
170         }
171         
172         return false;
173 }
174
175 unsigned int Pak::getUncompressedSize() const
176 {
177         return (unsigned int)currentFile->fSize;
178 }