📄 win_main.c
字号:
/*
===========================================================================
Copyright (C) 1999-2005 Id Software, Inc.
This file is part of Quake III Arena source code.
Quake III Arena source code 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.
Quake III Arena source code 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 Foobar; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
===========================================================================
*/
// win_main.c
#include "../client/client.h"
#include "../qcommon/qcommon.h"
#include "win_local.h"
#include "resource.h"
#include <errno.h>
#include <float.h>
#include <fcntl.h>
#include <stdio.h>
#include <direct.h>
#include <io.h>
#include <conio.h>
#define CD_BASEDIR "quake3"
#define CD_EXE "quake3.exe"
#define CD_BASEDIR_LINUX "bin\\x86\\glibc-2.1"
#define CD_EXE_LINUX "quake3"
#define MEM_THRESHOLD 96*1024*1024
static char sys_cmdline[MAX_STRING_CHARS];
// define this to use alternate spanking method
// I found out that the regular way doesn't work on my box for some reason
// see the associated spank.sh script
#define ALT_SPANK
#ifdef ALT_SPANK
#include <stdio.h>
#include <sys\stat.h>
int fh = 0;
void Spk_Open(char *name)
{
fh = open( name, O_TRUNC | O_CREAT | O_WRONLY, S_IREAD | S_IWRITE );
};
void Spk_Close()
{
if (!fh)
return;
close( fh );
fh = 0;
}
void Spk_Printf (const char *text, ...)
{
va_list argptr;
char buf[32768];
if (!fh)
return;
va_start (argptr,text);
vsprintf (buf, text, argptr);
write(fh, buf, strlen(buf));
_commit(fh);
va_end (argptr);
};
#endif
/*
==================
Sys_LowPhysicalMemory()
==================
*/
qboolean Sys_LowPhysicalMemory() {
MEMORYSTATUS stat;
GlobalMemoryStatus (&stat);
return (stat.dwTotalPhys <= MEM_THRESHOLD) ? qtrue : qfalse;
}
/*
==================
Sys_BeginProfiling
==================
*/
void Sys_BeginProfiling( void ) {
// this is just used on the mac build
}
/*
=============
Sys_Error
Show the early console as an error dialog
=============
*/
void QDECL Sys_Error( const char *error, ... ) {
va_list argptr;
char text[4096];
MSG msg;
va_start (argptr, error);
vsprintf (text, error, argptr);
va_end (argptr);
Conbuf_AppendText( text );
Conbuf_AppendText( "\n" );
Sys_SetErrorText( text );
Sys_ShowConsole( 1, qtrue );
timeEndPeriod( 1 );
IN_Shutdown();
// wait for the user to quit
while ( 1 ) {
if (!GetMessage (&msg, NULL, 0, 0))
Com_Quit_f ();
TranslateMessage (&msg);
DispatchMessage (&msg);
}
Sys_DestroyConsole();
exit (1);
}
/*
==============
Sys_Quit
==============
*/
void Sys_Quit( void ) {
timeEndPeriod( 1 );
IN_Shutdown();
Sys_DestroyConsole();
exit (0);
}
/*
==============
Sys_Print
==============
*/
void Sys_Print( const char *msg ) {
Conbuf_AppendText( msg );
}
/*
==============
Sys_Mkdir
==============
*/
void Sys_Mkdir( const char *path ) {
_mkdir (path);
}
/*
==============
Sys_Cwd
==============
*/
char *Sys_Cwd( void ) {
static char cwd[MAX_OSPATH];
_getcwd( cwd, sizeof( cwd ) - 1 );
cwd[MAX_OSPATH-1] = 0;
return cwd;
}
/*
==============
Sys_DefaultCDPath
==============
*/
char *Sys_DefaultCDPath( void ) {
return "";
}
/*
==============
Sys_DefaultBasePath
==============
*/
char *Sys_DefaultBasePath( void ) {
return Sys_Cwd();
}
/*
==============================================================
DIRECTORY SCANNING
==============================================================
*/
#define MAX_FOUND_FILES 0x1000
void Sys_ListFilteredFiles( const char *basedir, char *subdirs, char *filter, char **list, int *numfiles ) {
char search[MAX_OSPATH], newsubdirs[MAX_OSPATH];
char filename[MAX_OSPATH];
int findhandle;
struct _finddata_t findinfo;
if ( *numfiles >= MAX_FOUND_FILES - 1 ) {
return;
}
if (strlen(subdirs)) {
Com_sprintf( search, sizeof(search), "%s\\%s\\*", basedir, subdirs );
}
else {
Com_sprintf( search, sizeof(search), "%s\\*", basedir );
}
findhandle = _findfirst (search, &findinfo);
if (findhandle == -1) {
return;
}
do {
if (findinfo.attrib & _A_SUBDIR) {
if (Q_stricmp(findinfo.name, ".") && Q_stricmp(findinfo.name, "..")) {
if (strlen(subdirs)) {
Com_sprintf( newsubdirs, sizeof(newsubdirs), "%s\\%s", subdirs, findinfo.name);
}
else {
Com_sprintf( newsubdirs, sizeof(newsubdirs), "%s", findinfo.name);
}
Sys_ListFilteredFiles( basedir, newsubdirs, filter, list, numfiles );
}
}
if ( *numfiles >= MAX_FOUND_FILES - 1 ) {
break;
}
Com_sprintf( filename, sizeof(filename), "%s\\%s", subdirs, findinfo.name );
if (!Com_FilterPath( filter, filename, qfalse ))
continue;
list[ *numfiles ] = CopyString( filename );
(*numfiles)++;
} while ( _findnext (findhandle, &findinfo) != -1 );
_findclose (findhandle);
}
static qboolean strgtr(const char *s0, const char *s1) {
int l0, l1, i;
l0 = strlen(s0);
l1 = strlen(s1);
if (l1<l0) {
l0 = l1;
}
for(i=0;i<l0;i++) {
if (s1[i] > s0[i]) {
return qtrue;
}
if (s1[i] < s0[i]) {
return qfalse;
}
}
return qfalse;
}
char **Sys_ListFiles( const char *directory, const char *extension, char *filter, int *numfiles, qboolean wantsubs ) {
char search[MAX_OSPATH];
int nfiles;
char **listCopy;
char *list[MAX_FOUND_FILES];
struct _finddata_t findinfo;
int findhandle;
int flag;
int i;
if (filter) {
nfiles = 0;
Sys_ListFilteredFiles( directory, "", filter, list, &nfiles );
list[ nfiles ] = 0;
*numfiles = nfiles;
if (!nfiles)
return NULL;
listCopy = Z_Malloc( ( nfiles + 1 ) * sizeof( *listCopy ) );
for ( i = 0 ; i < nfiles ; i++ ) {
listCopy[i] = list[i];
}
listCopy[i] = NULL;
return listCopy;
}
if ( !extension) {
extension = "";
}
// passing a slash as extension will find directories
if ( extension[0] == '/' && extension[1] == 0 ) {
extension = "";
flag = 0;
} else {
flag = _A_SUBDIR;
}
Com_sprintf( search, sizeof(search), "%s\\*%s", directory, extension );
// search
nfiles = 0;
findhandle = _findfirst (search, &findinfo);
if (findhandle == -1) {
*numfiles = 0;
return NULL;
}
do {
if ( (!wantsubs && flag ^ ( findinfo.attrib & _A_SUBDIR )) || (wantsubs && findinfo.attrib & _A_SUBDIR) ) {
if ( nfiles == MAX_FOUND_FILES - 1 ) {
break;
}
list[ nfiles ] = CopyString( findinfo.name );
nfiles++;
}
} while ( _findnext (findhandle, &findinfo) != -1 );
list[ nfiles ] = 0;
_findclose (findhandle);
// return a copy of the list
*numfiles = nfiles;
if ( !nfiles ) {
return NULL;
}
listCopy = Z_Malloc( ( nfiles + 1 ) * sizeof( *listCopy ) );
for ( i = 0 ; i < nfiles ; i++ ) {
listCopy[i] = list[i];
}
listCopy[i] = NULL;
do {
flag = 0;
for(i=1; i<nfiles; i++) {
if (strgtr(listCopy[i-1], listCopy[i])) {
char *temp = listCopy[i];
listCopy[i] = listCopy[i-1];
listCopy[i-1] = temp;
flag = 1;
}
}
} while(flag);
return listCopy;
}
void Sys_FreeFileList( char **list ) {
int i;
if ( !list ) {
return;
}
for ( i = 0 ; list[i] ; i++ ) {
Z_Free( list[i] );
}
Z_Free( list );
}
//========================================================
/*
================
Sys_ScanForCD
Search all the drives to see if there is a valid CD to grab
the cddir from
================
*/
qboolean Sys_ScanForCD( void ) {
static char cddir[MAX_OSPATH];
char drive[4];
FILE *f;
char test[MAX_OSPATH];
#if 0
// don't override a cdpath on the command line
if ( strstr( sys_cmdline, "cdpath" ) ) {
return;
}
#endif
drive[0] = 'c';
drive[1] = ':';
drive[2] = '\\';
drive[3] = 0;
// scan the drives
for ( drive[0] = 'c' ; drive[0] <= 'z' ; drive[0]++ ) {
if ( GetDriveType (drive) != DRIVE_CDROM ) {
continue;
}
sprintf (cddir, "%s%s", drive, CD_BASEDIR);
sprintf (test, "%s\\%s", cddir, CD_EXE);
f = fopen( test, "r" );
if ( f ) {
fclose (f);
return qtrue;
} else {
sprintf(cddir, "%s%s", drive, CD_BASEDIR_LINUX);
sprintf(test, "%s\\%s", cddir, CD_EXE_LINUX);
f = fopen( test, "r" );
if ( f ) {
fclose (f);
return qtrue;
}
}
}
return qfalse;
}
/*
================
Sys_CheckCD
Return true if the proper CD is in the drive
================
*/
qboolean Sys_CheckCD( void ) {
// FIXME: mission pack
return qtrue;
//return Sys_ScanForCD();
}
/*
================
Sys_GetClipboardData
================
*/
char *Sys_GetClipboardData( void ) {
char *data = NULL;
char *cliptext;
if ( OpenClipboard( NULL ) != 0 ) {
HANDLE hClipboardData;
if ( ( hClipboardData = GetClipboardData( CF_TEXT ) ) != 0 ) {
if ( ( cliptext = GlobalLock( hClipboardData ) ) != 0 ) {
data = Z_Malloc( GlobalSize( hClipboardData ) + 1 );
Q_strncpyz( data, cliptext, GlobalSize( hClipboardData ) );
GlobalUnlock( hClipboardData );
strtok( data, "\n\r\b" );
}
}
CloseClipboard();
}
return data;
}
/*
========================================================================
LOAD/UNLOAD DLL
========================================================================
*/
/*
=================
Sys_UnloadDll
=================
*/
void Sys_UnloadDll( void *dllHandle ) {
if ( !dllHandle ) {
return;
}
if ( !FreeLibrary( dllHandle ) ) {
Com_Error (ERR_FATAL, "Sys_UnloadDll FreeLibrary failed");
}
}
/*
=================
Sys_LoadDll
Used to load a development dll instead of a virtual machine
TTimo: added some verbosity in debug
=================
*/
extern char *FS_BuildOSPath( const char *base, const char *game, const char *qpath );
// fqpath param added 7/20/02 by T.Ray - Sys_LoadDll is only called in vm.c at this time
// fqpath will be empty if dll not loaded, otherwise will hold fully qualified path of dll module loaded
// fqpath buffersize must be at least MAX_QPATH+1 bytes long
void * QDECL Sys_LoadDll( const char *name, char *fqpath , int (QDECL **entryPoint)(int, ...),
int (QDECL *systemcalls)(int, ...) ) {
static int lastWarning = 0;
HINSTANCE libHandle;
void (QDECL *dllEntry)( int (QDECL *syscallptr)(int, ...) );
char *basepath;
char *cdpath;
char *gamedir;
char *fn;
#ifdef NDEBUG
int timestamp;
int ret;
#endif
char filename[MAX_QPATH];
*fqpath = 0 ; // added 7/20/02 by T.Ray
Com_sprintf( filename, sizeof( filename ), "%sx86.dll", name );
#ifdef NDEBUG
timestamp = Sys_Milliseconds();
if( ((timestamp - lastWarning) > (5 * 60000)) && !Cvar_VariableIntegerValue( "dedicated" )
&& !Cvar_VariableIntegerValue( "com_blindlyLoadDLLs" ) ) {
if (FS_FileExists(filename)) {
lastWarning = timestamp;
ret = MessageBoxEx( NULL, "You are about to load a .DLL executable that\n"
"has not been verified for use with Quake III Arena.\n"
"This type of file can compromise the security of\n"
"your computer.\n\n"
"Select 'OK' if you choose to load it anyway.",
"Security Warning", MB_OKCANCEL | MB_ICONEXCLAMATION | MB_DEFBUTTON2 | MB_TOPMOST | MB_SETFOREGROUND,
MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ) );
if( ret != IDOK ) {
return NULL;
}
}
}
#endif
#ifndef NDEBUG
libHandle = LoadLibrary( filename );
if (libHandle)
Com_Printf("LoadLibrary '%s' ok\n", filename);
else
Com_Printf("LoadLibrary '%s' failed\n", filename);
if ( !libHandle ) {
#endif
basepath = Cvar_VariableString( "fs_basepath" );
cdpath = Cvar_VariableString( "fs_cdpath" );
gamedir = Cvar_VariableString( "fs_game" );
fn = FS_BuildOSPath( basepath, gamedir, filename );
libHandle = LoadLibrary( fn );
#ifndef NDEBUG
if (libHandle)
Com_Printf("LoadLibrary '%s' ok\n", fn);
else
Com_Printf("LoadLibrary '%s' failed\n", fn);
#endif
if ( !libHandle ) {
if( cdpath[0] ) {
fn = FS_BuildOSPath( cdpath, gamedir, filename );
libHandle = LoadLibrary( fn );
#ifndef NDEBUG
if (libHandle)
Com_Printf("LoadLibrary '%s' ok\n", fn);
else
Com_Printf("LoadLibrary '%s' failed\n", fn);
#endif
}
if ( !libHandle ) {
return NULL;
}
}
#ifndef NDEBUG
}
#endif
dllEntry = ( void (QDECL *)( int (QDECL *)( int, ... ) ) )GetProcAddress( libHandle, "dllEntry" );
*entryPoint = (int (QDECL *)(int,...))GetProcAddress( libHandle, "vmMain" );
if ( !*entryPoint || !dllEntry ) {
FreeLibrary( libHandle );
return NULL;
}
dllEntry( systemcalls );
if ( libHandle ) Q_strncpyz ( fqpath , filename , MAX_QPATH ) ; // added 7/20/02 by T.Ray
return libHandle;
}
/*
========================================================================
BACKGROUND FILE STREAMING
========================================================================
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -