2003-02-28 05:35:27 +00:00
|
|
|
/*
|
2004-04-20 22:52:09 +00:00
|
|
|
rua_qfs.c
|
2003-02-28 05:35:27 +00:00
|
|
|
|
|
|
|
CSQC file builtins
|
|
|
|
|
|
|
|
Copyright (C) 1996-1997 Id Software, Inc.
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
|
|
|
|
See the GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to:
|
|
|
|
|
|
|
|
Free Software Foundation, Inc.
|
|
|
|
59 Temple Place - Suite 330
|
|
|
|
Boston, MA 02111-1307, USA
|
|
|
|
|
|
|
|
*/
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#ifdef HAVE_STRING_H
|
|
|
|
# include <string.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STRINGS_H
|
|
|
|
# include <strings.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "QF/progs.h"
|
|
|
|
#include "QF/quakefs.h"
|
|
|
|
#include "QF/va.h"
|
|
|
|
#include "QF/zone.h"
|
|
|
|
|
2004-01-16 07:03:58 +00:00
|
|
|
#include "rua_internal.h"
|
|
|
|
|
2004-04-20 22:52:09 +00:00
|
|
|
typedef struct {
|
|
|
|
int count;
|
|
|
|
pointer_t list;
|
|
|
|
} qfslist_t;
|
|
|
|
|
2003-02-28 05:35:27 +00:00
|
|
|
static void
|
|
|
|
check_buffer (progs_t *pr, pr_type_t *buf, int count, const char *name)
|
|
|
|
{
|
|
|
|
int len;
|
|
|
|
|
|
|
|
len = (count + sizeof (pr_type_t) - 1) / sizeof (pr_type_t);
|
|
|
|
if (buf < pr->pr_globals || buf + len > pr->pr_globals + pr->globals_size)
|
|
|
|
PR_RunError (pr, "%s: bad buffer", name);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-12-20 12:08:57 +00:00
|
|
|
static void
|
|
|
|
bi_QFS_Open (progs_t *pr)
|
|
|
|
{
|
|
|
|
QFile *file;
|
|
|
|
const char *path = P_GSTRING (pr, 0);
|
|
|
|
const char *mode = P_GSTRING (pr, 1);
|
|
|
|
|
|
|
|
if (!(file = QFS_Open (path, mode))) {
|
|
|
|
R_INT (pr) = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!(R_INT (pr) = QFile_AllocHandle (pr, file)))
|
|
|
|
Qclose (file);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
bi_QFS_WOpen (progs_t *pr)
|
|
|
|
{
|
|
|
|
QFile *file;
|
|
|
|
const char *path = P_GSTRING (pr, 0);
|
|
|
|
int zip = P_INT (pr, 1);
|
|
|
|
|
|
|
|
if (!(file = QFS_WOpen (path, zip))) {
|
|
|
|
R_INT (pr) = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!(R_INT (pr) = QFile_AllocHandle (pr, file)))
|
|
|
|
Qclose (file);
|
|
|
|
}
|
|
|
|
|
2003-02-28 05:35:27 +00:00
|
|
|
static void
|
|
|
|
bi_QFS_Rename (progs_t *pr)
|
|
|
|
{
|
2003-04-22 18:20:15 +00:00
|
|
|
const char *old = P_GSTRING (pr, 0);
|
|
|
|
const char *new = P_GSTRING (pr, 1);
|
2003-02-28 05:35:27 +00:00
|
|
|
|
|
|
|
R_INT (pr) = QFS_Rename (old, new);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
bi_QFS_LoadFile (progs_t *pr)
|
|
|
|
{
|
2003-04-22 18:20:15 +00:00
|
|
|
const char *filename = P_GSTRING (pr, 0);
|
2003-02-28 05:35:27 +00:00
|
|
|
QFile *file;
|
|
|
|
int size;
|
|
|
|
void *buffer;
|
|
|
|
|
2014-01-23 02:57:57 +00:00
|
|
|
file = QFS_FOpenFile (filename);
|
2003-02-28 05:35:27 +00:00
|
|
|
if (!file) {
|
|
|
|
RETURN_POINTER (pr, 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
size = Qfilesize (file);
|
|
|
|
buffer = PR_Zone_Malloc (pr, (size + 3) & ~3);
|
|
|
|
if (!buffer) {
|
|
|
|
Qclose (file);
|
|
|
|
RETURN_POINTER (pr, 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Qread (file, buffer, size);
|
|
|
|
Qclose (file);
|
|
|
|
RETURN_POINTER (pr, buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
bi_QFS_OpenFile (progs_t *pr)
|
|
|
|
{
|
2004-11-11 07:57:00 +00:00
|
|
|
QFile *file;
|
2003-04-22 18:20:15 +00:00
|
|
|
const char *filename = P_GSTRING (pr, 0);
|
2003-02-28 05:35:27 +00:00
|
|
|
|
2014-01-23 02:57:57 +00:00
|
|
|
file = QFS_FOpenFile (filename);
|
2004-11-11 07:57:00 +00:00
|
|
|
if (!file) {
|
|
|
|
R_INT (pr) = 0;
|
2003-02-28 05:35:27 +00:00
|
|
|
return;
|
|
|
|
}
|
2004-11-11 07:57:00 +00:00
|
|
|
if (!(R_INT (pr) = QFile_AllocHandle (pr, file)))
|
|
|
|
Qclose (file);
|
2003-02-28 05:35:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
bi_QFS_WriteFile (progs_t *pr)
|
|
|
|
{
|
2003-04-22 18:20:15 +00:00
|
|
|
const char *filename = P_GSTRING (pr, 0);
|
|
|
|
pr_type_t *buf = P_GPOINTER (pr, 1);
|
2003-02-28 05:35:27 +00:00
|
|
|
int count = P_INT (pr, 2);
|
|
|
|
|
|
|
|
check_buffer (pr, buf, count, "QFS_WriteFile");
|
2003-05-23 17:17:01 +00:00
|
|
|
QFS_WriteFile (va ("%s/%s", qfs_gamedir->dir.def, filename), buf, count);
|
2003-02-28 05:35:27 +00:00
|
|
|
}
|
|
|
|
|
2004-04-20 22:52:09 +00:00
|
|
|
static void
|
|
|
|
bi_QFS_Filelist (progs_t *pr)
|
|
|
|
{
|
|
|
|
filelist_t *filelist = QFS_FilelistNew ();
|
|
|
|
qfslist_t *list;
|
|
|
|
string_t *strings;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
QFS_FilelistFill (filelist, P_GSTRING (pr, 0), P_GSTRING (pr, 1),
|
|
|
|
P_INT (pr, 2));
|
|
|
|
|
|
|
|
list = PR_Zone_Malloc (pr, sizeof (list) + filelist->count * 4);
|
|
|
|
list->count = filelist->count;
|
2010-01-13 06:29:01 +00:00
|
|
|
strings = (string_t *) (list + 1);
|
2004-11-11 07:57:00 +00:00
|
|
|
list->list = PR_SetPointer (pr, strings);
|
2004-04-20 22:52:09 +00:00
|
|
|
for (i = 0; i < filelist->count; i++)
|
2010-01-13 06:29:01 +00:00
|
|
|
strings[i] = PR_SetDynamicString (pr, filelist->list[i]);
|
2004-04-20 22:52:09 +00:00
|
|
|
RETURN_POINTER (pr, list);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
bi_QFS_FilelistFree (progs_t *pr)
|
|
|
|
{
|
2010-01-13 06:29:01 +00:00
|
|
|
qfslist_t *list = &P_STRUCT (pr, qfslist_t, 0);
|
|
|
|
string_t *strings = &G_STRUCT (pr, string_t, list->list);
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < list->count; i++)
|
|
|
|
PR_FreeString (pr, strings[i]);
|
|
|
|
PR_Zone_Free (pr, list);
|
2004-04-20 22:52:09 +00:00
|
|
|
}
|
|
|
|
|
2004-01-06 05:51:09 +00:00
|
|
|
static builtin_t builtins[] = {
|
2006-12-20 12:08:57 +00:00
|
|
|
{"QFS_Open", bi_QFS_Open, -1},
|
|
|
|
{"QFS_WOpen", bi_QFS_WOpen, -1},
|
2004-04-20 22:52:09 +00:00
|
|
|
{"QFS_Rename", bi_QFS_Rename, -1},
|
|
|
|
{"QFS_LoadFile", bi_QFS_LoadFile, -1},
|
|
|
|
{"QFS_OpenFile", bi_QFS_OpenFile, -1},
|
|
|
|
{"QFS_WriteFile", bi_QFS_WriteFile, -1},
|
|
|
|
{"QFS_Filelist", bi_QFS_Filelist, -1},
|
|
|
|
{"QFS_FilelistFree", bi_QFS_FilelistFree, -1},
|
2004-01-06 05:51:09 +00:00
|
|
|
{0}
|
|
|
|
};
|
|
|
|
|
2003-02-28 05:35:27 +00:00
|
|
|
void
|
2004-01-16 07:03:58 +00:00
|
|
|
RUA_QFS_Init (progs_t *pr, int secure)
|
2003-02-28 05:35:27 +00:00
|
|
|
{
|
2004-01-06 05:51:09 +00:00
|
|
|
PR_RegisterBuiltins (pr, builtins);
|
2003-02-28 05:35:27 +00:00
|
|
|
}
|