Quake II RTX doxygen  1.0 dev
system.c File Reference
#include "client.h"
#include "common/cvar.h"
#include "common/field.h"
#include "common/prompt.h"
#include <mmsystem.h>

Go to the source code of this file.

Enumerations

enum  should_exit_t { SE_NOT, SE_YES, SE_FULL }
 

Functions

void Sys_Error (const char *error,...)
 
void Sys_Quit (void)
 
void Sys_DebugBreak (void)
 
unsigned Sys_Milliseconds (void)
 
void Sys_AddDefaultConfig (void)
 
void Sys_Sleep (int msec)
 
qboolean Sys_IsDir (const char *path)
 
qboolean Sys_IsFile (const char *path)
 
void Sys_Init (void)
 
void Sys_FreeLibrary (void *handle)
 
voidSys_LoadLibrary (const char *path, const char *sym, void **handle)
 
voidSys_GetProcAddress (void *handle, const char *sym)
 
static time_t file_time_to_unix (FILETIME *f)
 
static voidcopy_info (const char *name, const LPWIN32_FIND_DATAA data)
 
void Sys_ListFiles_r (const char *path, const char *filter, unsigned flags, size_t baselen, int *count_p, void **files, int depth)
 
static BOOL fix_current_directory (void)
 
static int Sys_Main (int argc, char **argv)
 
int main (int argc, char **argv)
 

Variables

HINSTANCE hGlobalInstance
 
static char currentDirectory [MAX_OSPATH]
 
static volatile should_exit_t shouldExit
 
static volatile qboolean errorEntered
 
cvar_t * sys_basedir
 
cvar_t * sys_libdir
 
cvar_t * sys_homedir
 
cvar_t * sys_forcegamelib
 

Enumeration Type Documentation

◆ should_exit_t

Enumerator
SE_NOT 
SE_YES 
SE_FULL 

Definition at line 41 of file system.c.

41  {
42  SE_NOT,
43  SE_YES,
44  SE_FULL

Function Documentation

◆ copy_info()

static void* copy_info ( const char *  name,
const LPWIN32_FIND_DATAA  data 
)
static

Definition at line 850 of file system.c.

851 {
852  time_t ctime = file_time_to_unix(&data->ftCreationTime);
853  time_t mtime = file_time_to_unix(&data->ftLastWriteTime);
854 
855  return FS_CopyInfo(name, data->nFileSizeLow, ctime, mtime);
856 }

Referenced by Sys_ListFiles_r().

◆ file_time_to_unix()

static time_t file_time_to_unix ( FILETIME *  f)
inlinestatic

Definition at line 844 of file system.c.

845 {
846  ULARGE_INTEGER u = *(ULARGE_INTEGER *)f;
847  return (time_t)((u.QuadPart - 116444736000000000ULL) / 10000000);
848 }

Referenced by copy_info().

◆ fix_current_directory()

static BOOL fix_current_directory ( void  )
static

Definition at line 1006 of file system.c.

1007 {
1008  char *p;
1009 
1010  if (!GetModuleFileNameA(NULL, currentDirectory, sizeof(currentDirectory) - 1)) {
1011  return FALSE;
1012  }
1013 
1014  if ((p = strrchr(currentDirectory, '\\')) != NULL) {
1015  *p = 0;
1016  }
1017 
1018 #ifndef UNDER_CE
1019  if (!SetCurrentDirectoryA(currentDirectory)) {
1020  return FALSE;
1021  }
1022 #endif
1023 
1024  return TRUE;
1025 }

Referenced by Sys_Main().

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 1174 of file system.c.

1175 {
1176 #if USE_WINSVC
1177  int i;
1178 #endif
1179 
1180  hGlobalInstance = GetModuleHandle(NULL);
1181 
1182 #if USE_WINSVC
1183  for (i = 1; i < argc; i++) {
1184  if (!strcmp(argv[i], "-service")) {
1185  argv[i] = NULL;
1186  sys_argc = argc;
1187  sys_argv = argv;
1188  if (StartServiceCtrlDispatcher(serviceTable)) {
1189  return 0;
1190  }
1191  if (GetLastError() == ERROR_FAILED_SERVICE_CONTROLLER_CONNECT) {
1192  break; // fall back to normal server startup
1193  }
1194  return 1;
1195  }
1196  }
1197 #endif
1198 
1199  return Sys_Main(argc, argv);
1200 }

◆ Sys_AddDefaultConfig()

void Sys_AddDefaultConfig ( void  )

Definition at line 649 of file system.c.

650 {
651 }

Referenced by Qcommon_Init().

◆ Sys_DebugBreak()

void Sys_DebugBreak ( void  )

Definition at line 639 of file system.c.

640 {
641  DebugBreak();
642 }

Referenced by Com_Error().

◆ Sys_Error()

void Sys_Error ( const char *  error,
  ... 
)

Definition at line 574 of file system.c.

575 {
576  va_list argptr;
577  char text[MAXERRORMSG];
578 
579  va_start(argptr, error);
580  Q_vsnprintf(text, sizeof(text), error, argptr);
581  va_end(argptr);
582 
583  errorEntered = qtrue;
584 
585 #if USE_CLIENT
586  VID_Shutdown();
587 #endif
588 
589 #if USE_SYSCON
590  Sys_SetConsoleColor(COLOR_RED);
591  Sys_Printf("********************\n"
592  "FATAL: %s\n"
593  "********************\n", text);
594  Sys_SetConsoleColor(COLOR_NONE);
595 #endif
596 
597 #if USE_WINSVC
598  if (!statusHandle)
599 #endif
600  {
601 #if USE_SYSCON
602  if (gotConsole) {
603  Sleep(INFINITE);
604  }
605 #endif
606  MessageBoxA(NULL, text, PRODUCT " Fatal Error", MB_ICONERROR | MB_OK);
607  }
608 
609  exit(1);
610 }

Referenced by Com_Error(), Qcommon_Init(), and Sys_Init().

◆ Sys_FreeLibrary()

void Sys_FreeLibrary ( void handle)

Definition at line 787 of file system.c.

788 {
789  if (handle && !FreeLibrary(handle)) {
790  Com_Error(ERR_FATAL, "FreeLibrary failed on %p", handle);
791  }
792 }

Referenced by QAL_Shutdown(), and SV_ShutdownGameProgs().

◆ Sys_GetProcAddress()

void* Sys_GetProcAddress ( void handle,
const char *  sym 
)

Definition at line 824 of file system.c.

825 {
826  void *entry;
827 
828  entry = GetProcAddress(handle, sym);
829  if (!entry)
830  Com_SetLastError(va("GetProcAddress(%s) failed with error %lu",
831  sym, GetLastError()));
832 
833  return entry;
834 }

◆ Sys_Init()

void Sys_Init ( void  )

Definition at line 695 of file system.c.

696 {
697  OSVERSIONINFO vinfo;
698 #ifndef _WIN64
699  HMODULE module;
700  BOOL (WINAPI * pSetProcessDEPPolicy)(DWORD);
701 #endif
702  cvar_t *var = NULL;
703 
704  timeBeginPeriod(1);
705 
706  // check windows version
707  vinfo.dwOSVersionInfoSize = sizeof(vinfo);
708 #pragma warning(push)
709 #pragma warning(disable: 4996) // warning C4996: 'GetVersionExA': was declared deprecated
710  if (!GetVersionEx(&vinfo)) {
711  Sys_Error("Couldn't get OS info");
712  }
713 #pragma warning(pop)
714  if (vinfo.dwPlatformId != VER_PLATFORM_WIN32_NT) {
715  Sys_Error(PRODUCT " requires Windows NT");
716  }
717  if (vinfo.dwMajorVersion < 5) {
718  Sys_Error(PRODUCT " requires Windows 2000 or greater");
719  }
720 
721  // basedir <path>
722  // allows the game to run from outside the data tree
723  sys_basedir = Cvar_Get("basedir", currentDirectory, CVAR_NOSET);
724  sys_libdir = Cvar_Get("libdir", currentDirectory, CVAR_NOSET);
725 
726  // homedir <path>
727  // specifies per-user writable directory for demos, screenshots, etc
728  sys_homedir = Cvar_Get("homedir", "", CVAR_NOSET);
729 
730  sys_forcegamelib = Cvar_Get("sys_forcegamelib", "", CVAR_NOSET);
731 
732 #if USE_WINSVC
733  Cmd_AddCommand("installservice", Sys_InstallService_f);
734  Cmd_AddCommand("deleteservice", Sys_DeleteService_f);
735 #endif
736 
737 #if USE_SYSCON
738  houtput = GetStdHandle(STD_OUTPUT_HANDLE);
739 #if USE_CLIENT
740  sys_viewlog = Cvar_Get("sys_viewlog", "0", CVAR_NOSET);
741 
742  if (dedicated->integer || sys_viewlog->integer)
743 #endif
744  Sys_ConsoleInit();
745 #endif // USE_SYSCON
746 
747 #if USE_DBGHELP
748  var = Cvar_Get("sys_disablecrashdump", "0", CVAR_NOSET);
749 
750  // install our exception filter
751  if (!var->integer) {
752  mainProcessThread = GetCurrentThread();
753  prevExceptionFilter = SetUnhandledExceptionFilter(
755  }
756 #endif
757 
758 #ifndef _WIN64
759  module = GetModuleHandle("kernel32.dll");
760  if (module) {
761  pSetProcessDEPPolicy = (PVOID)GetProcAddress(module,
762  "SetProcessDEPPolicy");
763  if (pSetProcessDEPPolicy) {
764  var = Cvar_Get("sys_disabledep", "0", CVAR_NOSET);
765 
766  // opt-in or opt-out for DEP
767  if (!var->integer) {
768  pSetProcessDEPPolicy(
771  } else if (var->integer == 2) {
772  pSetProcessDEPPolicy(0);
773  }
774  }
775  }
776 #endif
777 }

Referenced by Qcommon_Init().

◆ Sys_IsDir()

qboolean Sys_IsDir ( const char *  path)

Definition at line 659 of file system.c.

660 {
661  WCHAR wpath[MAX_OSPATH] = { 0 };
662  MultiByteToWideChar(CP_UTF8, 0, path, -1, wpath, MAX_OSPATH);
663 
664  DWORD fileAttributes = GetFileAttributesW(wpath);
665  if (fileAttributes == INVALID_FILE_ATTRIBUTES)
666  {
667  return qfalse;
668  }
669 
670  return (fileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
671 }

Referenced by OGG_InitTrackList().

◆ Sys_IsFile()

qboolean Sys_IsFile ( const char *  path)

Definition at line 674 of file system.c.

675 {
676  WCHAR wpath[MAX_OSPATH] = { 0 };
677  MultiByteToWideChar(CP_UTF8, 0, path, -1, wpath, MAX_OSPATH);
678 
679  DWORD fileAttributes = GetFileAttributesW(wpath);
680  if (fileAttributes == INVALID_FILE_ATTRIBUTES)
681  {
682  return qfalse;
683  }
684 
685  // I guess the assumption that if it's not a file or device
686  // then it's a directory is good enough for us?
687  return (fileAttributes & (FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_DEVICE)) == 0;
688 }

Referenced by OGG_InitTrackList().

◆ Sys_ListFiles_r()

void Sys_ListFiles_r ( const char *  path,
const char *  filter,
unsigned  flags,
size_t  baselen,
int count_p,
void **  files,
int  depth 
)

Definition at line 868 of file system.c.

875 {
876  WIN32_FIND_DATAA data;
877  HANDLE handle;
878  char fullpath[MAX_OSPATH], *name;
879  size_t pathlen, len;
880  unsigned mask;
881  void *info;
882 
883  // optimize single extension search
884  if (!(flags & FS_SEARCH_BYFILTER) &&
885  filter && !strchr(filter, ';')) {
886  if (*filter == '.') {
887  filter++;
888  }
889  len = Q_concat(fullpath, sizeof(fullpath),
890  path, "\\*.", filter, NULL);
891  filter = NULL; // do not check it later
892  } else {
893  len = Q_concat(fullpath, sizeof(fullpath),
894  path, "\\*", NULL);
895  }
896 
897  if (len >= sizeof(fullpath)) {
898  return;
899  }
900 
901  // format path to windows style
902  // done on the first run only
903  if (!depth) {
904  FS_ReplaceSeparators(fullpath, '\\');
905  }
906 
907  handle = FindFirstFileA(fullpath, &data);
908  if (handle == INVALID_HANDLE_VALUE) {
909  return;
910  }
911 
912  // make it point right after the slash
913  pathlen = strlen(path) + 1;
914 
915  do {
916  if (!strcmp(data.cFileName, ".") ||
917  !strcmp(data.cFileName, "..")) {
918  continue; // ignore special entries
919  }
920 
921  // construct full path
922  len = strlen(data.cFileName);
923  if (pathlen + len >= sizeof(fullpath)) {
924  continue;
925  }
926 
927  memcpy(fullpath + pathlen, data.cFileName, len + 1);
928 
929  if (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
930  mask = FS_SEARCH_DIRSONLY;
931  } else {
932  mask = 0;
933  }
934 
935  // pattern search implies recursive search
936  if ((flags & FS_SEARCH_BYFILTER) && mask &&
937  depth < MAX_LISTED_DEPTH) {
938  Sys_ListFiles_r(fullpath, filter, flags, baselen,
939  count_p, files, depth + 1);
940 
941  // re-check count
942  if (*count_p >= MAX_LISTED_FILES) {
943  break;
944  }
945  }
946 
947  // check type
948  if ((flags & FS_SEARCH_DIRSONLY) != mask) {
949  continue;
950  }
951 
952  // check filter
953  if (filter) {
954  if (flags & FS_SEARCH_BYFILTER) {
955  if (!FS_WildCmp(filter, fullpath + baselen)) {
956  continue;
957  }
958  } else {
959  if (!FS_ExtCmp(filter, data.cFileName)) {
960  continue;
961  }
962  }
963  }
964 
965  // strip path
966  if (flags & FS_SEARCH_SAVEPATH) {
967  name = fullpath + baselen;
968  } else {
969  name = data.cFileName;
970  }
971 
972  // reformat it back to quake filesystem style
973  FS_ReplaceSeparators(name, '/');
974 
975  // strip extension
976  if (flags & FS_SEARCH_STRIPEXT) {
977  *COM_FileExtension(name) = 0;
978 
979  if (!*name) {
980  continue;
981  }
982  }
983 
984  // copy info off
985  if (flags & FS_SEARCH_EXTRAINFO) {
986  info = copy_info(name, &data);
987  } else {
988  info = FS_CopyString(name);
989  }
990 
991  files[(*count_p)++] = info;
992  } while (*count_p < MAX_LISTED_FILES &&
993  FindNextFileA(handle, &data) != FALSE);
994 
995  FindClose(handle);
996 }

Referenced by FS_ListFiles(), and q_printf().

◆ Sys_LoadLibrary()

void* Sys_LoadLibrary ( const char *  path,
const char *  sym,
void **  handle 
)

Definition at line 794 of file system.c.

795 {
796  HMODULE module;
797  void *entry;
798 
799  *handle = NULL;
800 
801  module = LoadLibraryA(path);
802  if (!module) {
803  Com_SetLastError(va("%s: LoadLibrary failed with error %lu",
804  path, GetLastError()));
805  return NULL;
806  }
807 
808  if (sym) {
809  entry = GetProcAddress(module, sym);
810  if (!entry) {
811  Com_SetLastError(va("%s: GetProcAddress(%s) failed with error %lu",
812  path, sym, GetLastError()));
813  FreeLibrary(module);
814  return NULL;
815  }
816  } else {
817  entry = NULL;
818  }
819 
820  *handle = module;
821  return entry;
822 }

Referenced by _SV_LoadGameLibrary(), and QAL_Init().

◆ Sys_Main()

static int Sys_Main ( int  argc,
char **  argv 
)
static

Definition at line 1034 of file system.c.

1035 {
1036  // fix current directory to point to the basedir
1037  if (!fix_current_directory()) {
1038  return 1;
1039  }
1040 
1041 #if (_MSC_VER >= 1400)
1042  // work around strftime given invalid format string
1043  // killing the whole fucking process :((
1044  _set_invalid_parameter_handler(msvcrt_sucks);
1045 #endif
1046 
1047  Qcommon_Init(argc, argv);
1048 
1049  // main program loop
1050  while (1) {
1051  Qcommon_Frame();
1052  if (shouldExit) {
1053 #if USE_WINSVC
1054  if (shouldExit == SE_FULL)
1055 #endif
1056  Com_Quit(NULL, ERR_DISCONNECT);
1057  break;
1058  }
1059  }
1060 
1061  // may get here when our service stops
1062  return 0;
1063 }

Referenced by main().

◆ Sys_Milliseconds()

◆ Sys_Quit()

void Sys_Quit ( void  )

Definition at line 619 of file system.c.

620 {
621  timeEndPeriod(1);
622 
623 #if USE_CLIENT
624 #if USE_SYSCON
625  if (dedicated && dedicated->integer) {
626  FreeConsole();
627  }
628 #endif
629 #elif USE_WINSVC
630  if (statusHandle && !shouldExit) {
631  shouldExit = SE_YES;
632  Com_AbortFrame();
633  }
634 #endif
635 
636  exit(0);
637 }

Referenced by Com_Quit().

◆ Sys_Sleep()

void Sys_Sleep ( int  msec)

Definition at line 653 of file system.c.

654 {
655  Sleep(msec);
656 }

Referenced by NET_Sleep().

Variable Documentation

◆ currentDirectory

char currentDirectory[MAX_OSPATH]
static

Definition at line 35 of file system.c.

Referenced by fix_current_directory(), and Sys_Init().

◆ errorEntered

volatile qboolean errorEntered
static

Definition at line 48 of file system.c.

Referenced by Sys_Error().

◆ hGlobalInstance

HINSTANCE hGlobalInstance

◆ shouldExit

volatile should_exit_t shouldExit
static

Definition at line 47 of file system.c.

Referenced by Sys_Main(), and Sys_Quit().

◆ sys_basedir

cvar_t* sys_basedir

Definition at line 50 of file system.c.

Referenced by FS_Restart(), open_file_write(), setup_base_paths(), setup_game_paths(), and Sys_Init().

◆ sys_forcegamelib

cvar_t* sys_forcegamelib

Definition at line 53 of file system.c.

Referenced by SV_InitGameProgs(), and Sys_Init().

◆ sys_homedir

cvar_t* sys_homedir

Definition at line 52 of file system.c.

Referenced by open_file_write(), setup_game_paths(), and Sys_Init().

◆ sys_libdir

cvar_t* sys_libdir

Definition at line 51 of file system.c.

Referenced by SV_LoadGameLibrary(), and Sys_Init().

handle
static void * handle
Definition: dynamic.c:52
FS_CopyInfo
file_info_t * FS_CopyInfo(const char *name, size_t size, time_t ctime, time_t mtime)
Definition: files.c:2599
file_time_to_unix
static time_t file_time_to_unix(FILETIME *f)
Definition: system.c:844
Sys_ListFiles_r
void Sys_ListFiles_r(const char *path, const char *filter, unsigned flags, size_t baselen, int *count_p, void **files, int depth)
Definition: system.c:868
Com_AbortFrame
void Com_AbortFrame(void)
Definition: common.c:595
Qcommon_Frame
void Qcommon_Frame(void)
Definition: common.c:1078
FS_ExtCmp
qboolean FS_ExtCmp(const char *ext, const char *name)
Definition: files.c:2649
Cmd_AddCommand
void Cmd_AddCommand(const char *name, xcommand_t function)
Definition: cmd.c:1562
sys_homedir
cvar_t * sys_homedir
Definition: system.c:52
Cvar_Get
cvar_t * Cvar_Get(const char *var_name, const char *var_value, int flags)
Definition: cvar.c:257
sys_libdir
cvar_t * sys_libdir
Definition: system.c:51
FS_ReplaceSeparators
char * FS_ReplaceSeparators(char *s, int separator)
Definition: files.c:235
shouldExit
static volatile should_exit_t shouldExit
Definition: system.c:47
Sys_ExceptionFilter
LONG WINAPI Sys_ExceptionFilter(LPEXCEPTION_POINTERS exceptionInfo)
Definition: debug.c:180
Q_vsnprintf
size_t Q_vsnprintf(char *dest, size_t size, const char *fmt, va_list argptr)
Definition: shared.c:791
FindNextFileA
BOOL WINAPI FindNextFileA(HANDLE handle, LPWIN32_FIND_DATAA data)
Definition: ascii.c:22
Sys_Main
static int Sys_Main(int argc, char **argv)
Definition: system.c:1034
Sys_Error
void Sys_Error(const char *error,...)
Definition: system.c:574
BOOL
BOOL(WINAPI *qwglSetPixelFormat)(HDC
fix_current_directory
static BOOL fix_current_directory(void)
Definition: system.c:1006
should_exit_t
should_exit_t
Definition: system.c:41
hGlobalInstance
HINSTANCE hGlobalInstance
Definition: system.c:28
Com_Error
void Com_Error(error_type_t type, const char *fmt,...)
Definition: g_main.c:258
dedicated
cvar_t * dedicated
Definition: g_main.c:46
errorEntered
static volatile qboolean errorEntered
Definition: system.c:48
currentDirectory
static char currentDirectory[MAX_OSPATH]
Definition: system.c:35
va
char * va(const char *format,...)
Definition: shared.c:429
PROCESS_DEP_ENABLE
#define PROCESS_DEP_ENABLE
Definition: client.h:46
VID_Shutdown
void VID_Shutdown(void)
Definition: glimp.c:54
FS_WildCmp
qboolean FS_WildCmp(const char *filter, const char *string)
Definition: files.c:2636
SE_FULL
@ SE_FULL
Definition: system.c:44
copy_info
static void * copy_info(const char *name, const LPWIN32_FIND_DATAA data)
Definition: system.c:850
FindFirstFileA
HANDLE WINAPI FindFirstFileA(LPCSTR path, LPWIN32_FIND_DATAA data)
Definition: ascii.c:3
SE_YES
@ SE_YES
Definition: system.c:43
GetModuleFileNameA
DWORD WINAPI GetModuleFileNameA(HMODULE hModule, LPSTR lpFileName, DWORD nSize)
Definition: ascii.c:84
sys_forcegamelib
cvar_t * sys_forcegamelib
Definition: system.c:53
Com_Quit
void Com_Quit(const char *reason, error_type_t type)
Definition: common.c:609
MessageBoxA
int WINAPI MessageBoxA(HWND hWnd, LPCSTR lpText, LPCSTR lpCaption, UINT uType)
Definition: ascii.c:47
Com_SetLastError
void Com_SetLastError(const char *msg)
Definition: common.c:382
Qcommon_Init
void Qcommon_Init(int argc, char **argv)
Definition: common.c:893
sys_basedir
cvar_t * sys_basedir
Definition: system.c:50
SE_NOT
@ SE_NOT
Definition: system.c:42
Q_concat
size_t Q_concat(char *dest, size_t size,...)
Definition: shared.c:758
COM_FileExtension
char * COM_FileExtension(const char *in)
Definition: shared.c:199
PROCESS_DEP_DISABLE_ATL_THUNK_EMULATION
#define PROCESS_DEP_DISABLE_ATL_THUNK_EMULATION
Definition: client.h:49
LoadLibraryA
HINSTANCE WINAPI LoadLibraryA(LPCSTR path)
Definition: ascii.c:36