1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10  *
11  * See the GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
16  *
17  *
18  */
19 
20 #ifndef __VFS_H__
21 #define __VFS_H__
22 
23 
24 // FIXME: probably we can move some of that to .c files.
25 
26 
27 //=================================
28 // Quake filesystem
29 //=================================
30 extern hashtable_t *filesystemhash;
31 extern int fs_hash_dups;
32 extern int fs_hash_files;
33 
34 typedef struct
35 {
36 	struct searchpath_s *	search;
37 	int						index;
38 	char					rawname[MAX_OSPATH];
39 	int						offset;
40 	int						len;
41 } flocation_t;
42 
43 // FS_FLocateFile return type.
44 typedef enum
45 {
46 	FSLFRT_IFFOUND,			// return true if file found, false if not found.
47 	FSLFRT_LENGTH,			// return file length if found, -1 if not found.
48 	FSLFRT_DEPTH_OSONLY,	// return depth (no paks), 0x7fffffff if not found.
49 	FSLFRT_DEPTH_ANYPATH	// return depth, 0x7fffffff if not found.
50 } FSLF_ReturnType_e;
51 
52 typedef enum
53 {
54 	VFSERR_NONE,
55 	VFSERR_EOF
56 } vfserrno_t;
57 
58 typedef struct vfsfile_s
59 {
60 	int (*ReadBytes) (struct vfsfile_s *file, void *buffer, int bytestoread, vfserrno_t *err);
61 	int (*WriteBytes) (struct vfsfile_s *file, const void *buffer, int bytestowrite);
62 	int (*Seek) (struct vfsfile_s *file, unsigned long pos, int whence);	// Returns 0 on sucess, -1 otherwise
63 	unsigned long (*Tell) (struct vfsfile_s *file);
64 	unsigned long (*GetLen) (struct vfsfile_s *file);	// Could give some lag
65 	void (*Close) (struct vfsfile_s *file);
66 	void (*Flush) (struct vfsfile_s *file);
67 	qbool seekingisabadplan;
68 	qbool copyprotected;							// File found was in a pak
69 } vfsfile_t;
70 
71 typedef struct
72 {
73 	void	(*PrintPath)(void *handle);
74 	void	(*ClosePath)(void *handle);
75 	void	(*BuildHash)(void *handle);
76 	// true if found (hashedresult can be NULL)
77 	// note that if rawfile and offset are set, many Com_FileOpens will
78 	// read the raw file otherwise ReadFile will be called instead.
79 	qbool   (*FindFile)(void *handle, flocation_t *loc, const char *name, void *hashedresult);
80 	// reads the entire file
81 	void	(*ReadFile)(void *handle, flocation_t *loc, char *buffer);
82 
83 	int		(*EnumerateFiles)(void *handle, char *match, int (*func)(char *, int, void *), void *parm);
84 
85 	// returns a handle to a new pak/path
86 	void	*(*OpenNew)(vfsfile_t *file, const char *desc);
87 
88 	int		(*GeneratePureCRC) (void *handle, int seed, int usepure);
89 
90 	vfsfile_t *(*OpenVFS)(void *handle, flocation_t *loc, char *mode);
91 } searchpathfuncs_t;
92 
93 typedef struct searchpath_s
94 {
95 	searchpathfuncs_t *funcs;
96 	qbool copyprotected;	// don't allow downloads from here.
97 	qbool istemporary;
98 	void *handle;
99 
100 	struct searchpath_s *next;
101 
102 } searchpath_t;
103 
104 // mostly analogs for stdio functions
105 void 			VFS_CLOSE  (struct vfsfile_s *vf);
106 unsigned long	VFS_TELL   (struct vfsfile_s *vf);
107 unsigned long	VFS_GETLEN (struct vfsfile_s *vf);
108 int				VFS_SEEK   (struct vfsfile_s *vf, unsigned long pos, int whence);
109 int				VFS_READ   (struct vfsfile_s *vf, void *buffer, int bytestoread, vfserrno_t *err);
110 int				VFS_WRITE  (struct vfsfile_s *vf, const void *buffer, int bytestowrite);
111 void			VFS_FLUSH  (struct vfsfile_s *vf);
112 // return null terminated string
113 char		   *VFS_GETS   (struct vfsfile_s *vf, char *buffer, int buflen);
114 qbool			VFS_COPYPROTECTED(struct vfsfile_s *vf);
115 
116 typedef enum
117 {
118 	FS_NONE_OS, // FIXME: probably must be removed, as not so secure...
119 				// Opened with OS functions (no paks).
120 				// filename.
121 
122 	FS_GAME_OS, // Opened with OS functions (no paks).
123 				// fs_basedir/fs_gamedirfile/filename.
124 
125 	FS_GAME,	// Searched on path as filename, including packs.
126 
127 	FS_BASE_OS,	// Opened with OS functions (no paks).
128 				// fs_basedir/filename.
129 
130 	FS_ANY		// That slightly evil, derived from ezquake.
131 				// 1) FS_GAME.
132 				// 2) FS_NONE_OS.
133 } relativeto_t;
134 
135 void FS_FlushFSHash(void);
136 
137 vfsfile_t *FS_OpenVFS(const char *filename, char *mode, relativeto_t relativeto);
138 int FS_FLocateFile(const char *filename, FSLF_ReturnType_e returntype, flocation_t *loc);
139 
140 //=================================
141 // STDIO Files (OS)
142 //=================================
143 
144 vfsfile_t *FS_OpenTemp(void);
145 vfsfile_t *VFSOS_Open(char *osname, char *mode);
146 
147 extern searchpathfuncs_t osfilefuncs;
148 
149 //====================
150 // PACK (*pak) Support
151 //====================
152 
153 extern searchpathfuncs_t packfilefuncs;
154 
155 #endif /* __VFS_H__ */
156