Skip to content

Commit

Permalink
Merged pull request "Sync with Q2PRO: Extended game API": #434
Browse files Browse the repository at this point in the history
  • Loading branch information
apanteleev committed Feb 19, 2025
2 parents e82ba8c + 3c04d51 commit d14b0cf
Show file tree
Hide file tree
Showing 9 changed files with 232 additions and 107 deletions.
51 changes: 1 addition & 50 deletions inc/common/files.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,61 +22,12 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "common/cmd.h"
#include "common/error.h"
#include "common/zone.h"
#include "shared/files.h"

#define MIN_LISTED_FILES 1024
#define MAX_LISTED_FILES 250000000
#define MAX_LISTED_DEPTH 8

typedef struct file_info_s {
int64_t size;
time_t ctime;
time_t mtime;
char name[1];
} file_info_t;

// bits 0 - 1, enum
#define FS_MODE_APPEND 0x00000000
#define FS_MODE_READ 0x00000001
#define FS_MODE_WRITE 0x00000002
#define FS_MODE_RDWR 0x00000003
#define FS_MODE_MASK 0x00000003

// bits 2 - 3, enum
#define FS_BUF_DEFAULT 0x00000000
#define FS_BUF_FULL 0x00000004
#define FS_BUF_LINE 0x00000008
#define FS_BUF_NONE 0x0000000c
#define FS_BUF_MASK 0x0000000c

// bits 4 - 5, enum
#define FS_TYPE_ANY 0x00000000
#define FS_TYPE_REAL 0x00000010
#define FS_TYPE_PAK 0x00000020
#define FS_TYPE_RESERVED 0x00000030
#define FS_TYPE_MASK 0x00000030

// bits 6 - 7, flag
#define FS_PATH_ANY 0x00000000
#define FS_PATH_BASE 0x00000040
#define FS_PATH_GAME 0x00000080
#define FS_PATH_MASK 0x000000c0

// bits 8 - 13, flag
#define FS_SEARCH_BYFILTER 0x00000100
#define FS_SEARCH_SAVEPATH 0x00000200
#define FS_SEARCH_EXTRAINFO 0x00000400
#define FS_SEARCH_STRIPEXT 0x00000800
#define FS_SEARCH_DIRSONLY 0x00001000
#define FS_SEARCH_RECURSIVE 0x00002000
#define FS_SEARCH_MASK 0x00003f00

// bits 8 - 12, flag
#define FS_FLAG_GZIP 0x00000100 // transparently (de)compress with gzip
#define FS_FLAG_EXCL 0x00000200 // create the file, fail if already exists
#define FS_FLAG_TEXT 0x00000400 // open in text mode if from disk
#define FS_FLAG_DEFLATE 0x00000800 // if compressed in .pkz, read raw deflate data, fail otherwise
#define FS_FLAG_LOADFILE 0x00001000 // open non-unique handle, must be closed very quickly

//
// Limit the maximum file size FS_LoadFile can handle, as a protection from
// malicious paks causing memory exhaustion.
Expand Down
1 change: 1 addition & 0 deletions inc/server/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ unsigned SV_Frame(unsigned msec);
void SV_SetConsoleTitle(void);
#endif
//void SV_ConsoleOutput(const char *msg);
void SV_RestartFilesystem(void);

#if USE_MVD_CLIENT && USE_CLIENT
bool MVD_GetDemoStatus(float *progress, bool *paused, int *framenum);
Expand Down
69 changes: 69 additions & 0 deletions inc/shared/files.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
Copyright (C) 2023 Andrey Nazarov
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 the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#pragma once

typedef struct {
int64_t size;
int64_t ctime;
int64_t mtime;
char name[1];
} file_info_t;

// file opening mode
#define FS_MODE_READ 0x00000000
#define FS_MODE_WRITE 0x00000001
#define FS_MODE_APPEND 0x00000002
#define FS_MODE_RDWR 0x00000003 // similar to FS_MODE_APPEND, but does not create the file
#define FS_MODE_MASK 0x00000003

// output buffering mode
#define FS_BUF_DEFAULT 0x00000000 // default mode (normally fully buffered)
#define FS_BUF_FULL 0x00000004 // fully buffered
#define FS_BUF_LINE 0x00000008 // line buffered
#define FS_BUF_NONE 0x0000000c // unbuffered
#define FS_BUF_MASK 0x0000000c

// where to open file from
#define FS_TYPE_ANY 0x00000000 // open from anywhere
#define FS_TYPE_REAL 0x00000010 // open from disk only
#define FS_TYPE_PAK 0x00000020 // open from pack only
#define FS_TYPE_MASK 0x00000030

// where to look for a file
#define FS_PATH_ANY 0x00000000 // look in any search paths
#define FS_PATH_BASE 0x00000040 // look in base search paths
#define FS_PATH_GAME 0x00000080 // look in game search paths
#define FS_PATH_MASK 0x000000c0

// search mode for ListFiles()
#define FS_SEARCH_BYFILTER 0x00000100 // wildcard search instead of extension search
#define FS_SEARCH_SAVEPATH 0x00000200 // preserve file path
#define FS_SEARCH_EXTRAINFO 0x00000400 // return file_info_t *, not char *
#define FS_SEARCH_STRIPEXT 0x00000800 // strip file extension
#define FS_SEARCH_DIRSONLY 0x00001000 // search only directories (can't be mixed with other flags)
#define FS_SEARCH_RECURSIVE 0x00002000 // recursive search (implied by FS_SEARCH_BYFILTER)
#define FS_SEARCH_MASK 0x0000ff00

// misc flags for OpenFile()
#define FS_FLAG_GZIP 0x00000100 // transparently (de)compress with gzip
#define FS_FLAG_EXCL 0x00000200 // create the file, fail if already exists
#define FS_FLAG_TEXT 0x00000400 // open in text mode if from disk
#define FS_FLAG_DEFLATE 0x00000800 // if compressed, read raw deflate data, fail otherwise
#define FS_FLAG_LOADFILE 0x00001000 // open non-unique handle, must be closed very quickly
#define FS_FLAG_MASK 0x0000ff00
47 changes: 47 additions & 0 deletions inc/shared/game.h
Original file line number Diff line number Diff line change
Expand Up @@ -240,4 +240,51 @@ typedef struct {
int max_edicts;
} game_export_t;

typedef game_export_t *(*game_entry_t)(game_import_t *);

//===============================================================

/*
* GetExtendedGameAPI() is guaranteed to be called after GetGameAPI() and
* before ge->Init().
*
* Unlike GetGameAPI(), passed game_import_ex_t * is valid as long as game
* library is loaded. Pointed to structure should be considered unknown length
* and must not be copied locally.
*
* New fields can be safely added at the end of game_import_ex_t and
* game_export_ex_t structures, provided GAME_API_VERSION_EX is also bumped.
*/

#define GAME_API_VERSION_EX 1

typedef struct {
int apiversion;

int64_t (*OpenFile)(const char *path, qhandle_t *f, unsigned mode); // returns file length
int (*CloseFile)(qhandle_t f);
int (*LoadFile)(const char *path, void **buffer, unsigned flags, unsigned tag);

int (*ReadFile)(void *buffer, size_t len, qhandle_t f);
int (*WriteFile)(const void *buffer, size_t len, qhandle_t f);
int (*FlushFile)(qhandle_t f);
int64_t (*TellFile)(qhandle_t f);
int (*SeekFile)(qhandle_t f, int64_t offset, int whence);
int (*ReadLine)(qhandle_t f, char *buffer, size_t size);

void **(*ListFiles)(const char *path, const char *filter, unsigned flags, int *count_p);
void (*FreeFileList)(void **list);

const char *(*ErrorString)(int error);
void *(*TagRealloc)(void *ptr, size_t size);
} game_import_ex_t;

typedef struct {
int apiversion;

void (*RestartFilesystem)(void); // called when fs_restart is issued
} game_export_ex_t;

typedef const game_export_ex_t *(*game_entry_ex_t)(const game_import_ex_t *);

#endif // GAME_H
2 changes: 1 addition & 1 deletion src/client/ui/demos.c
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ static void BuildName(const file_info_t *info, char **cache)

// format date
len = 0;
if ((tm = localtime(&info->mtime)) != NULL) {
if ((tm = localtime(&(time_t){info->mtime})) != NULL) {
if (tm->tm_year == m_demos.year) {
len = strftime(date, sizeof(date), "%b %d %H:%M", tm);
} else {
Expand Down
3 changes: 3 additions & 0 deletions src/common/files.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "common/intreadwrite.h"
#include "system/system.h"
#include "client/client.h"
#include "server/server.h"
#include "format/pak.h"

#include <fcntl.h>
Expand Down Expand Up @@ -3586,6 +3587,8 @@ void FS_Restart(bool total)

setup_game_paths();

SV_RestartFilesystem();

FS_Path_f();

Com_Printf("----------------------\n");
Expand Down
Loading

0 comments on commit d14b0cf

Please sign in to comment.