1 /*
2 	rua_qfs.c
3 
4 	CSQC file builtins
5 
6 	Copyright (C) 1996-1997  Id Software, Inc.
7 
8 	This program is free software; you can redistribute it and/or
9 	modify it under the terms of the GNU General Public License
10 	as published by the Free Software Foundation; either version 2
11 	of the License, or (at your option) any later version.
12 
13 	This program is distributed in the hope that it will be useful,
14 	but WITHOUT ANY WARRANTY; without even the implied warranty of
15 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16 
17 	See the GNU General Public License for more details.
18 
19 	You should have received a copy of the GNU General Public License
20 	along with this program; if not, write to:
21 
22 		Free Software Foundation, Inc.
23 		59 Temple Place - Suite 330
24 		Boston, MA  02111-1307, USA
25 
26 */
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30 
31 #include <stdlib.h>
32 #ifdef HAVE_STRING_H
33 # include <string.h>
34 #endif
35 #ifdef HAVE_STRINGS_H
36 # include <strings.h>
37 #endif
38 
39 #include "QF/progs.h"
40 #include "QF/quakefs.h"
41 #include "QF/va.h"
42 #include "QF/zone.h"
43 
44 #include "rua_internal.h"
45 
46 typedef struct {
47 	int         count;
48 	pointer_t   list;
49 } qfslist_t;
50 
51 static void
check_buffer(progs_t * pr,pr_type_t * buf,int count,const char * name)52 check_buffer (progs_t *pr, pr_type_t *buf, int count, const char *name)
53 {
54 	int         len;
55 
56 	len = (count + sizeof (pr_type_t) - 1) / sizeof (pr_type_t);
57 	if (buf < pr->pr_globals || buf + len > pr->pr_globals + pr->globals_size)
58 		PR_RunError (pr, "%s: bad buffer", name);
59 }
60 
61 
62 static void
bi_QFS_Open(progs_t * pr)63 bi_QFS_Open (progs_t *pr)
64 {
65 	QFile      *file;
66 	const char *path = P_GSTRING (pr, 0);
67 	const char *mode = P_GSTRING (pr, 1);
68 
69 	if (!(file = QFS_Open (path, mode))) {
70 		R_INT (pr) = 0;
71 		return;
72 	}
73 	if (!(R_INT (pr) = QFile_AllocHandle (pr, file)))
74 		Qclose (file);
75 }
76 
77 static void
bi_QFS_WOpen(progs_t * pr)78 bi_QFS_WOpen (progs_t *pr)
79 {
80 	QFile      *file;
81 	const char *path = P_GSTRING (pr, 0);
82 	int         zip = P_INT (pr, 1);
83 
84 	if (!(file = QFS_WOpen (path, zip))) {
85 		R_INT (pr) = 0;
86 		return;
87 	}
88 	if (!(R_INT (pr) = QFile_AllocHandle (pr, file)))
89 		Qclose (file);
90 }
91 
92 static void
bi_QFS_Rename(progs_t * pr)93 bi_QFS_Rename (progs_t *pr)
94 {
95 	const char *old = P_GSTRING (pr, 0);
96 	const char *new = P_GSTRING (pr, 1);
97 
98 	R_INT (pr) = QFS_Rename (old, new);
99 }
100 
101 static void
bi_QFS_LoadFile(progs_t * pr)102 bi_QFS_LoadFile (progs_t *pr)
103 {
104 	const char *filename = P_GSTRING (pr, 0);
105 	QFile      *file;
106 	int         size;
107 	void       *buffer;
108 
109 	QFS_FOpenFile (filename, &file);
110 	if (!file) {
111 		RETURN_POINTER (pr, 0);
112 		return;
113 	}
114 	size = Qfilesize (file);
115 	buffer = PR_Zone_Malloc (pr, (size + 3) & ~3);
116 	if (!buffer) {
117 		Qclose (file);
118 		RETURN_POINTER (pr, 0);
119 		return;
120 	}
121 	Qread (file, buffer, size);
122 	Qclose (file);
123 	RETURN_POINTER (pr, buffer);
124 }
125 
126 static void
bi_QFS_OpenFile(progs_t * pr)127 bi_QFS_OpenFile (progs_t *pr)
128 {
129 	QFile      *file;
130 	const char *filename = P_GSTRING (pr, 0);
131 
132 	QFS_FOpenFile (filename, &file);
133 	if (!file) {
134 		R_INT (pr) = 0;
135 		return;
136 	}
137 	if (!(R_INT (pr) = QFile_AllocHandle (pr, file)))
138 		Qclose (file);
139 }
140 
141 static void
bi_QFS_WriteFile(progs_t * pr)142 bi_QFS_WriteFile (progs_t *pr)
143 {
144 	const char *filename = P_GSTRING (pr, 0);
145 	pr_type_t  *buf = P_GPOINTER (pr, 1);
146 	int         count = P_INT (pr, 2);
147 
148 	check_buffer (pr, buf, count, "QFS_WriteFile");
149 	QFS_WriteFile (va ("%s/%s", qfs_gamedir->dir.def, filename), buf, count);
150 }
151 
152 static void
bi_QFS_Filelist(progs_t * pr)153 bi_QFS_Filelist (progs_t *pr)
154 {
155 	filelist_t *filelist = QFS_FilelistNew ();
156 	qfslist_t  *list;
157 	string_t   *strings;
158 	int         i;
159 
160 	QFS_FilelistFill (filelist, P_GSTRING (pr, 0), P_GSTRING (pr, 1),
161 					  P_INT (pr, 2));
162 
163 	list = PR_Zone_Malloc (pr, sizeof (list) + filelist->count * 4);
164 	list->count = filelist->count;
165 	strings = (string_t *) (list + 1);
166 	list->list = PR_SetPointer (pr, strings);
167 	for (i = 0; i < filelist->count; i++)
168 		strings[i] = PR_SetDynamicString (pr, filelist->list[i]);
169 	RETURN_POINTER (pr, list);
170 }
171 
172 static void
bi_QFS_FilelistFree(progs_t * pr)173 bi_QFS_FilelistFree (progs_t *pr)
174 {
175 	qfslist_t  *list = &P_STRUCT (pr, qfslist_t, 0);
176 	string_t   *strings = &G_STRUCT (pr, string_t, list->list);
177 	int         i;
178 
179 	for (i = 0; i < list->count; i++)
180 		PR_FreeString (pr, strings[i]);
181 	PR_Zone_Free (pr, list);
182 }
183 
184 static builtin_t builtins[] = {
185 	{"QFS_Open",			bi_QFS_Open,			-1},
186 	{"QFS_WOpen",			bi_QFS_WOpen,			-1},
187 	{"QFS_Rename",			bi_QFS_Rename,			-1},
188 	{"QFS_LoadFile",		bi_QFS_LoadFile,		-1},
189 	{"QFS_OpenFile",		bi_QFS_OpenFile,		-1},
190 	{"QFS_WriteFile",		bi_QFS_WriteFile,		-1},
191 	{"QFS_Filelist",		bi_QFS_Filelist,		-1},
192 	{"QFS_FilelistFree",	bi_QFS_FilelistFree,	-1},
193 	{0}
194 };
195 
196 void
RUA_QFS_Init(progs_t * pr,int secure)197 RUA_QFS_Init (progs_t *pr, int secure)
198 {
199 	PR_RegisterBuiltins (pr, builtins);
200 }
201