1 //-----------------------------------------------------------------------------
2 // Gestion de fichiers
3 //-----------------------------------------------------------------------------
4 
5 #ifndef __FILES_H__
6 #define __FILES_H__
7 
8 #include "cake.h"
9 #include "consts.h"
10 #include <stdio.h>
11 
12 #ifdef WIN32
13   #include <io.h>
14 #else
15   #include <dirent.h>
16   #include <time.h>
17 
18   /**
19    * Following stuff was taken from io.h
20    */
21   #ifndef _FSIZE_T_DEFINED
22     typedef unsigned _fsize_t; /**< Could be 64 bits for Win32 */
23     #define _FSIZE_T_DEFINED
24   #endif
25 
26   struct _finddata_t
27   {
28     unsigned    attrib;
29     time_t      time_create;    /**< -1 for FAT file systems */
30     time_t      time_access;    /**< -1 for FAT file systems */
31     time_t      time_write;
32     _fsize_t    size;
33     char        name[260];
34   };
35 #endif
36 
37 #define MAX_FILEPATH    1024
38 #define FILE_NAME     256
39 
40 typedef struct PackedFile_s
41 {
42   char      name[1024];     /**< @test Static allocation */
43   int       offset;
44   int       size;
45 } PackedFile;
46 
47 typedef struct Pack_s
48 {
49   char *    name;
50   int     nFiles;
51   PackedFile *files;
52   Pack_s *  next;
53 } Pack;
54 
55 //-----------------------------------------------------------------------------
56 
57 extern char* UnZip(FILE *h);
58 
59 #ifdef WIN32
60   #pragma pack( push, before_ZIPStructs )
61   #pragma pack(1)
62 #else
63   #pragma pack(1)
64 #endif
65 
66 #define ZIPHeaderSig  0x04034b50
67 typedef struct
68 {
69   LONG  Signature;       //  (0x04034b50)
70   WORD  Version;
71   WORD  Flags;
72   WORD  Method;
73   LONG  LastMod;
74   LONG  CRC32;
75   LONG  CompressedSize;
76   LONG  UnCompressedSize;
77   WORD  FileNameLength;
78   WORD  ExtraLength;
79 } ZIPHeader;
80 
81 #define ZIPCtrlHeaderSig  0x02014b50
82 typedef struct
83 {
84   LONG  Signature;       //  (0x02014b50)
85   WORD  VersionMade;
86   WORD  VersionNeeded;
87   WORD  Flags;
88   WORD  Method;
89   LONG  LastMod;
90   LONG  CRC32;
91   LONG  CompressedSize;
92   LONG  UnCompressedSize;
93   WORD  FileNameLength;
94   WORD  ExtraLength;
95   WORD  CommentLength;
96   WORD  StartDisk;
97   WORD  IniternalAttribs;
98   LONG  ExternalAttribs;
99   LONG  Offset;
100 } ZIPCtrlHeader;
101 
102 #define ZIPEndSig     0x06054b50
103 typedef struct
104 {
105   LONG  Signature;       //  (0x06054b50)
106   WORD  DiskNumber;
107   WORD  StartDiskNumber;
108   WORD  FilesOnDisk;
109   WORD  Files;
110   LONG  Size;
111   LONG  Offset;
112   WORD  CommentLength;
113 } ZIPEnd;
114 
115 #ifdef WIN32
116   #pragma pack( pop, before_ZIPStructs )
117 #else
118   #pragma pack()
119 #endif
120 
121 char * f_Name(char *path);
122 void f_StripName(char *path);
123 char * f_Extension(char *name);
124 void f_StripExtension (char *path);
125 
126 /**
127  * Display in console the content of pak files.
128  * FileEnvironnment must be created and initialized.
129  */
130 void getPAKContent(char *pak_name = NULL,
131            char *file_type = ".*",
132            char *grep = NULL);
133 
134 /**
135  * Write a list of all maps available with levelshot in console.
136  * FileEnvironnment must be created and initialized.
137  */
138 void logPreviews(void);
139 
140 /**
141  * Checks if a file exists, inside or outside any package.
142  * The file is searched from current path.
143  * @param file_name the file name to search
144  * @param pak_search an integer value that indicates if the function must look in pak files
145  * @param strip_extension the function must strip the extension for search (note
146  *        that it as only effect with files searched in pak files)
147  * @return an integer (0 or 1) that indicates if file exists (1) or not (0)
148  */
149 int FileExist(char *file_name, int pak_search = 1, int strip_extension = 1);
150 
151 inline void f_CorrectSlashInName(char *name);
152 
153 //-----------------------------------------------------------------------------
154 // SearchFile
155 //-----------------------------------------------------------------------------
156 
157 /**
158  * Typical string list.
159  */
160 typedef struct _s
161 {
162   char path[PATH_LENGTH];
163   _s *next;
164 } search_path;
165 
166 /**
167  * Searchs for a filenames given a pattern.
168  */
169 class SearchFile
170 {
171   public:
172     SearchFile(char *pattern="*");
173     ~SearchFile(void);
174 
175     char *FirstFile(void);
176     char *NextFile(void);
177 
178     char *FirstFile(const char *path);
179     char *NextFile(const char *path);
180 
181   private:
182     #ifdef WIN32
183       int handle;           /**< io handle */
184     #else
185       DIR *handle;
186     #endif
187     char *pattern;
188     char *temp_pat;         /**< last pattern used. */
189     search_path *last;        /**< last path searched. */
190     char lastpath[MAX_FILEPATH];  /**< this will save the name of the last path until f_FindNext is called. */
191 };
192 
193 //-----------------------------------------------------------------------------
194 //  FileEnvironment
195 //-----------------------------------------------------------------------------
196 /**
197  * Global file acces functions
198  */
199 class FileEnvironment
200 {
201   public:
202     static void Init(void);
203     static void Shut(void);
204     static void AddPath(const char *path);
205     static void LoadPacks(void);
206     static search_path *Search;
207     static char main_path[MAX_FILEPATH];
208 
209     static void dumpEnvironment(void);
210 };
211 
212 //-----------------------------------------------------------------------------
213 // VFile
214 //-----------------------------------------------------------------------------
215 /**
216  * Read-only virtual file
217  */
218 class VFile : public FileEnvironment
219 {
220   public:
221     VFile(const char *name, bool pak_search=true, const char *pakname=NULL);
222     ~VFile(void);
223     char  fname[FILE_NAME];
224     BYTE *  mem;
225     int   size;
226     int   error;
227 };
228 
229 //-----------------------------------------------------------------------------
230 // LumpFile
231 //-----------------------------------------------------------------------------
232 
233 /**
234  * Read-only virtual file with lump header and parsing support (tipical q1,
235  * q2 or q3 files).
236  */
237 class LumpFile : public VFile
238 {
239   public:
240     LumpFile(const char *name, int id, int ver, const int num_lumps);
241     ~LumpFile(void);
242 
243     int ReadLump(int Lump, void** mem, size_t elem);
244 
245   protected:
246 
247     struct header
248     {
249       int id, ver;
250       struct { int fileofs, filelen; } lump[];  // is that allowed on most compilers ???
251     } *head;
252 };
253 
254 //-----------------------------------------------------------------------------
255 // Config File
256 //-----------------------------------------------------------------------------
257 class ConfigFile : public VFile
258 {
259   public:
260     ConfigFile(char *name);
261 };
262 
263 //-----------------------------------------------------------------------------
264 // Shader loading
265 //-----------------------------------------------------------------------------
266 
267 #if SHADER_USE_REFERENCES
268   /** Structure that stores the informations about shaders. */
269   typedef struct _shader_ref_list
270   {
271     char shader_name[SHADER_NAME_MAX_LENGTH];
272     char file_name[SHADER_NAME_MAX_LENGTH];
273     char pak_name[SHADER_NAME_MAX_LENGTH];
274     int offset;           /**< Position of the beginning of the
275                      *   shader. First char should be '{' */
276     _shader_ref_list *next;
277   } shader_ref_list;
278   shader_ref_list *RefForShader(const char *shader);
279 #else
280   /** Structure that stores the name of all shader files. */
281   typedef struct _shaders_file_list
282   {
283     char file_name[SHADER_NAME_MAX_LENGTH];
284     _shaders_file_list *next;
285   } shaders_file_list;
286   char *GetFirstShadersFile(void);
287   char *NextShadersFile(void);
288 #endif
289 
290 void InitializeShaders();
291 void DestroyShaderList(void);
292 
293 //-----------------------------------------------------------------------------
294 // Bitmap file writer
295 //-----------------------------------------------------------------------------
296 // Copyright (C) 2000 Nicholas Anderson
297 // This program is free software; you can redistribute it and/or
298 // modify it under the terms of the GNU General Public License
299 // as published by the Free Software Foundation; either version 2
300 // of the License, or (at your option) any later version.
301 // This program is distributed in the hope that it will be useful,
302 // but WITHOUT ANY WARRANTY; without even the implied warranty of
303 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
304 // GNU General Public License for more details.
305 // You should have received a copy of the GNU General Public License
306 // along with this program; if not, write to the Free Software
307 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
308 
309 typedef unsigned char BYTE;
310 typedef short int SHORT;
311 typedef int LONG;
312 typedef unsigned short int WORD;
313 typedef unsigned int DWORD;
314 
315 typedef struct TEXTURE_BMP{
316   int w, h;
317   unsigned char *data;
318 } TEXTURE_BMP;
319 
320 typedef struct _Win3xBitmapHeader
321 {
322   WORD  ImageFileType;
323   DWORD FileSize;
324   WORD  Reserved1;
325   WORD  Reserved2;
326   DWORD ImageDataOffset;
327 
328 } WIN3XHEAD;
329 
330 typedef struct _Win3xBitmapInfoHeader
331 {
332   DWORD HeaderSize;
333   LONG  ImageWidth;
334   LONG  ImageHeight;
335   WORD  NumberOfImagePlanes;
336   WORD  BitsPerPixel;
337   DWORD CompressionMethod;
338   DWORD SizeOfBitmap;
339   LONG  HorzResolution;
340   LONG  VertResolution;
341   DWORD NumColorsUsed;
342   DWORD NumSignificantColors;
343 
344 } WIN3XINFOHEAD;
345 
346 typedef struct _Win3xPixelData
347 {
348   BYTE r;
349   BYTE g;
350   BYTE b;
351 } PAL;
352 int WriteBitmapFile(char *filename, int width, int height, int bpp, unsigned char *imageData);
353 
354 #endif  /* __FILES_H__ */
355