This repository has been archived on 2022-06-27. You can view files and clone it, but cannot push or open issues or pull requests.
Xash3DArchive/engine/common/host.c

867 lines
19 KiB
C
Raw Normal View History

2007-06-21 22:00:00 +02:00
//=======================================================================
// Copyright XashXT Group 2007 <20>
// host.c - dedicated and shared host
//=======================================================================
2008-06-09 22:00:00 +02:00
#include "common.h"
2010-10-07 22:00:00 +02:00
#include "netchan.h"
2010-12-16 22:00:00 +01:00
#include "protocol.h"
2010-08-12 22:00:00 +02:00
#include "cm_local.h"
2011-03-12 22:00:00 +01:00
#include "mathlib.h"
2008-08-04 22:00:00 +02:00
#include "input.h"
2007-06-21 22:00:00 +02:00
2011-03-12 22:00:00 +01:00
static const char *show_credits = "\n\n\n\n\tCopyright XashXT Group %s <20>\n\t\
All Rights Reserved\n\n\t Visit www.xash.ru\n";
typedef void (*pfnChangeGame)( const char *progname );
pfnChangeGame pChangeGame = NULL;
HINSTANCE hCurrent; // hinstance of current .dll
2007-09-03 22:00:00 +02:00
host_parm_t host; // host parms
2011-03-12 22:00:00 +01:00
sysinfo_t SI;
2007-06-21 22:00:00 +02:00
2010-10-22 22:00:00 +02:00
convar_t *host_serverstate;
convar_t *host_gameloaded;
convar_t *host_limitlocal;
convar_t *host_cheats;
convar_t *host_maxfps;
convar_t *host_framerate;
2011-02-11 22:00:00 +01:00
convar_t *con_gamemaps;
2007-11-17 22:00:00 +01:00
2011-03-12 22:00:00 +01:00
static int num_decals;
2008-07-31 22:00:00 +02:00
// these cvars will be duplicated on each client across network
2010-12-02 22:00:00 +01:00
int Host_ServerState( void )
{
return Cvar_VariableInteger( "host_serverstate" );
}
2008-07-06 22:00:00 +02:00
2009-09-28 22:00:00 +02:00
int Host_CompareFileTime( long ft1, long ft2 )
{
if( ft1 < ft2 )
{
return -1;
}
else if( ft1 > ft2 )
{
return 1;
}
return 0;
}
2009-10-13 22:00:00 +02:00
void Host_ShutdownServer( void )
{
if( !SV_Active()) return;
2011-03-09 22:00:00 +01:00
Q_strncpy( host.finalmsg, "Server was killed\n", MAX_STRING );
2009-10-13 22:00:00 +02:00
SV_Shutdown( false );
}
2010-03-25 22:00:00 +01:00
/*
================
Host_NewGame
================
*/
2010-10-26 22:00:00 +02:00
qboolean Host_NewGame( const char *mapName, qboolean loadGame )
2010-03-25 22:00:00 +01:00
{
2010-10-26 22:00:00 +02:00
qboolean iRet;
2010-03-25 22:00:00 +01:00
iRet = SV_NewGame( mapName, loadGame );
return iRet;
}
2009-10-13 22:00:00 +02:00
/*
================
Host_EndGame
================
*/
void Host_EndGame( const char *message, ... )
{
va_list argptr;
2010-07-26 22:00:00 +02:00
static char string[MAX_SYSPATH];
2009-10-13 22:00:00 +02:00
va_start( argptr, message );
2011-03-09 22:00:00 +01:00
Q_vsprintf( string, message, argptr );
2009-10-13 22:00:00 +02:00
va_end( argptr );
MsgDev( D_INFO, "Host_EndGame: %s\n", string );
if( SV_Active())
{
2011-03-09 22:00:00 +01:00
Q_snprintf( host.finalmsg, sizeof( host.finalmsg ), "Host_EndGame: %s\n", string );
2009-10-13 22:00:00 +02:00
SV_Shutdown( false );
}
if( host.type == HOST_DEDICATED )
Sys_Break( "Host_EndGame: %s\n", string ); // dedicated servers exit
2009-11-03 22:00:00 +01:00
if( CL_NextDemo( ));
else CL_Disconnect();
2009-10-13 22:00:00 +02:00
Host_AbortCurrentFrame ();
}
2007-09-10 22:00:00 +02:00
/*
================
Host_AbortCurrentFrame
aborts the current host frame and goes on with the next one
================
*/
void Host_AbortCurrentFrame( void )
{
2008-05-18 22:00:00 +02:00
longjmp( host.abortframe, 1 );
2007-09-10 22:00:00 +02:00
}
2007-11-14 22:00:00 +01:00
/*
==================
Host_SetServerState
==================
*/
void Host_SetServerState( int state )
{
2010-10-22 22:00:00 +02:00
Cvar_FullSet( "host_serverstate", va( "%i", state ), CVAR_INIT );
2007-11-14 22:00:00 +01:00
}
2011-03-12 22:00:00 +01:00
void Host_NewInstance( const char *name, const char *finalmsg )
{
if( !pChangeGame ) return;
host.change_game = true;
Q_strncpy( host.finalmsg, finalmsg, sizeof( host.finalmsg ));
pChangeGame( name );
}
2010-03-25 22:00:00 +01:00
/*
=================
Host_ChangeGame_f
Change game modification
=================
*/
2008-08-04 22:00:00 +02:00
void Host_ChangeGame_f( void )
{
int i;
if( Cmd_Argc() != 2 )
{
2009-09-10 22:00:00 +02:00
Msg( "Usage: game <directory>\n" );
2008-08-04 22:00:00 +02:00
return;
}
// validate gamedir
2011-03-12 22:00:00 +01:00
for( i = 0; i < SI.numgames; i++ )
2008-08-04 22:00:00 +02:00
{
2011-03-12 22:00:00 +01:00
if( !Q_stricmp( SI.games[i]->gamefolder, Cmd_Argv( 1 )))
2008-08-04 22:00:00 +02:00
break;
}
2011-03-12 22:00:00 +01:00
if( i == SI.numgames )
{
Msg( "%s not exist\n", Cmd_Argv( 1 ));
}
2011-03-09 22:00:00 +01:00
else if( !Q_stricmp( GI->gamefolder, Cmd_Argv( 1 )))
2011-03-12 22:00:00 +01:00
{
2009-09-10 22:00:00 +02:00
Msg( "%s already active\n", Cmd_Argv( 1 ));
2011-03-12 22:00:00 +01:00
}
else
{
Host_NewInstance( Cmd_Argv( 1 ), va( "change game to '%s'", SI.games[i]->title ));
}
2008-08-04 22:00:00 +02:00
}
2011-03-08 22:00:00 +01:00
/*
===============
Host_Exec_f
===============
*/
void Host_Exec_f( void )
{
string cfgpath;
size_t len;
char *f;
if( Cmd_Argc() != 2 )
{
Msg( "Usage: exec <filename>\n" );
return;
}
2011-03-09 22:00:00 +01:00
Q_strncpy( cfgpath, Cmd_Argv( 1 ), sizeof( cfgpath ));
2011-03-08 22:00:00 +01:00
FS_DefaultExtension( cfgpath, ".cfg" ); // append as default
f = FS_LoadFile( cfgpath, &len, false );
if( !f )
{
MsgDev( D_NOTE, "couldn't exec %s\n", Cmd_Argv( 1 ));
return;
}
MsgDev( D_INFO, "execing %s\n", Cmd_Argv( 1 ));
Cbuf_InsertText( f );
Mem_Free( f );
}
2011-03-09 22:00:00 +01:00
/*
===============
Host_MemStats_f
===============
*/
void Host_MemStats_f( void )
{
switch( Cmd_Argc( ))
{
case 1:
Mem_PrintList( 1<<30 );
Mem_PrintStats();
break;
case 2:
Mem_PrintList( Q_atoi( Cmd_Argv( 1 )) * 1024 );
Mem_PrintStats();
break;
default:
Msg( "Usage: memlist <all>\n" );
break;
}
}
2008-08-10 22:00:00 +02:00
void Host_Minimize_f( void )
{
if( host.hWnd ) ShowWindow( host.hWnd, SW_MINIMIZE );
}
2010-10-26 22:00:00 +02:00
qboolean Host_IsLocalGame( void )
2010-06-20 22:00:00 +02:00
{
if( CL_Active() && SV_Active() && CL_GetMaxClients() == 1 )
return true;
return false;
}
2010-10-28 22:00:00 +02:00
/*
=================
Host_RegisterDecal
=================
*/
qboolean Host_RegisterDecal( const char *name )
{
char shortname[CS_SIZE];
int i;
if( !name || !name[0] )
return 0;
FS_FileBase( name, shortname );
for( i = 1; i < MAX_DECALS && host.draw_decals[i][0]; i++ )
{
2011-03-09 22:00:00 +01:00
if( !Q_stricmp( host.draw_decals[i], shortname ))
2010-10-28 22:00:00 +02:00
return true;
}
if( i == MAX_DECALS )
{
MsgDev( D_ERROR, "Host_RegisterDecal: MAX_DECALS limit exceeded\n" );
return false;
}
// register new decal
2011-03-09 22:00:00 +01:00
Q_strncpy( host.draw_decals[i], shortname, sizeof( host.draw_decals[i] ));
2010-10-28 22:00:00 +02:00
num_decals++;
return true;
}
/*
=================
Host_InitDecals
=================
*/
void Host_InitDecals( void )
{
search_t *t;
int i;
2011-03-10 22:00:00 +01:00
Q_memset( host.draw_decals, 0, sizeof( host.draw_decals ));
2010-10-28 22:00:00 +02:00
num_decals = 0;
// lookup all decals in decals.wad
2011-03-08 22:00:00 +01:00
t = FS_Search( "decals.wad/*.*", true, false );
2010-10-28 22:00:00 +02:00
for( i = 0; t && i < t->numfilenames; i++ )
{
if( !Host_RegisterDecal( t->filenames[i] ))
break;
}
if( t ) Mem_Free( t );
2010-11-15 22:00:00 +01:00
MsgDev( D_NOTE, "InitDecals: %i decals\n", num_decals );
2010-10-28 22:00:00 +02:00
}
2009-09-20 22:00:00 +02:00
/*
=================
Host_InitEvents
=================
*/
void Host_InitEvents( void )
{
2011-03-10 22:00:00 +01:00
Q_memset( host.events, 0, sizeof( host.events ));
2009-09-20 22:00:00 +02:00
host.events_head = 0;
host.events_tail = 0;
}
/*
=================
Host_PushEvent
=================
*/
void Host_PushEvent( sys_event_t *event )
{
sys_event_t *ev;
2010-10-26 22:00:00 +02:00
static qboolean overflow = false;
2009-09-20 22:00:00 +02:00
2009-11-23 22:00:00 +01:00
ev = &host.events[host.events_head & (MAX_SYSEVENTS-1)];
2009-09-20 22:00:00 +02:00
2009-11-23 22:00:00 +01:00
if( host.events_head - host.events_tail >= MAX_SYSEVENTS )
2009-09-20 22:00:00 +02:00
{
if( !overflow )
{
MsgDev( D_WARN, "Host_PushEvent overflow\n" );
overflow = true;
}
if( ev->data ) Mem_Free( ev->data );
host.events_tail++;
}
else overflow = false;
*ev = *event;
host.events_head++;
}
/*
=================
Host_GetEvent
=================
*/
sys_event_t Host_GetEvent( void )
{
if( host.events_head > host.events_tail )
{
host.events_tail++;
2009-11-23 22:00:00 +01:00
return host.events[(host.events_tail - 1) & (MAX_SYSEVENTS-1)];
2009-09-20 22:00:00 +02:00
}
return Sys_GetEvent();
}
2008-07-06 22:00:00 +02:00
/*
=================
Host_EventLoop
2010-10-09 22:00:00 +02:00
Returns false while events is out
2008-07-06 22:00:00 +02:00
=================
*/
2010-10-09 22:00:00 +02:00
void Host_EventLoop( void )
2008-07-06 22:00:00 +02:00
{
sys_event_t ev;
while( 1 )
{
2009-02-03 22:00:00 +01:00
ev = Sys_GetEvent();
switch( ev.type )
2008-07-06 22:00:00 +02:00
{
case SE_NONE:
2010-10-09 22:00:00 +02:00
Cbuf_Execute();
return;
2008-07-06 22:00:00 +02:00
case SE_KEY:
2010-10-09 22:00:00 +02:00
Key_Event( ev.value[0], ev.value[1] );
2008-07-06 22:00:00 +02:00
break;
case SE_CHAR:
CL_CharEvent( ev.value[0] );
break;
case SE_MOUSE:
2009-02-03 22:00:00 +01:00
CL_MouseEvent( ev.value[0], ev.value[1] );
2008-07-06 22:00:00 +02:00
break;
case SE_CONSOLE:
2009-06-22 22:00:00 +02:00
Cbuf_AddText( va( "%s\n", ev.data ));
2008-07-12 22:00:00 +02:00
break;
2008-07-06 22:00:00 +02:00
default:
2010-06-20 22:00:00 +02:00
MsgDev( D_ERROR, "Host_EventLoop: bad event type %i", ev.type );
2010-10-09 22:00:00 +02:00
return;
2008-07-06 22:00:00 +02:00
}
if( ev.data ) Mem_Free( ev.data );
}
}
2010-07-23 22:00:00 +02:00
/*
2010-10-09 22:00:00 +02:00
===================
Host_FilterTime
Returns false if the time is too short to run a frame
===================
2010-07-23 22:00:00 +02:00
*/
2010-10-26 22:00:00 +02:00
qboolean Host_FilterTime( float time )
2010-07-23 22:00:00 +02:00
{
2010-10-09 22:00:00 +02:00
static double oldtime;
float fps;
2010-06-20 22:00:00 +02:00
2010-10-09 22:00:00 +02:00
host.realtime += time;
2010-06-20 22:00:00 +02:00
2010-10-09 22:00:00 +02:00
// dedicated's tic_rate regulates server frame rate. Don't apply fps filter here.
fps = host_maxfps->value;
if( fps != 0 )
2010-07-23 22:00:00 +02:00
{
2010-10-09 22:00:00 +02:00
float minframetime;
// limit fps to withing tolerable range
fps = bound( MIN_FPS, fps, MAX_FPS );
minframetime = 1.0f / fps;
if(( host.realtime - oldtime ) < minframetime )
{
// framerate is too high
return false;
}
2009-09-20 22:00:00 +02:00
}
2010-10-09 22:00:00 +02:00
host.frametime = host.realtime - oldtime;
host.realframetime = bound( MIN_FRAMETIME, host.frametime, MAX_FRAMETIME );
oldtime = host.realtime;
if( host_framerate->value > 0 && ( Host_IsLocalGame() || CL_IsPlaybackDemo() ))
2009-09-20 22:00:00 +02:00
{
2010-10-09 22:00:00 +02:00
float fps = host_framerate->value;
if( fps > 1 ) fps = 1.0f / fps;
host.frametime = fps;
2009-09-20 22:00:00 +02:00
}
else
2010-10-09 22:00:00 +02:00
{ // don't allow really long or short frames
host.frametime = bound( MIN_FRAMETIME, host.frametime, MAX_FRAMETIME );
2009-09-20 22:00:00 +02:00
}
2010-10-09 22:00:00 +02:00
return true;
2009-09-16 22:00:00 +02:00
}
2007-06-21 22:00:00 +02:00
/*
=================
Host_Frame
=================
*/
2010-10-09 22:00:00 +02:00
void Host_Frame( float time )
2008-07-06 22:00:00 +02:00
{
2008-12-20 22:00:00 +01:00
if( setjmp( host.abortframe ))
2008-07-06 22:00:00 +02:00
return;
2010-10-09 22:00:00 +02:00
Host_InputFrame (); // input frame
Host_EventLoop();
2010-07-23 22:00:00 +02:00
2010-10-09 22:00:00 +02:00
// decide the simulation time
if( !Host_FilterTime( time ))
2010-07-23 22:00:00 +02:00
return;
2010-10-09 22:00:00 +02:00
Host_ServerFrame (); // server frame
Host_ClientFrame (); // client frame
2008-07-06 22:00:00 +02:00
host.framecount++;
}
2010-10-09 22:00:00 +02:00
2007-11-14 22:00:00 +01:00
/*
================
Host_Print
Handles cursor positioning, line wrapping, etc
All console printing must go through this in order to be logged to disk
If no console is visible, the text will appear at the top of the game window
================
*/
void Host_Print( const char *txt )
{
2008-07-12 22:00:00 +02:00
if( host.rd.target )
2007-11-14 22:00:00 +01:00
{
2011-03-09 22:00:00 +01:00
if(( Q_strlen( txt ) + Q_strlen( host.rd.buffer )) > ( host.rd.buffersize - 1 ))
2007-11-14 22:00:00 +01:00
{
2008-07-12 22:00:00 +02:00
if( host.rd.flush )
2007-11-14 22:00:00 +01:00
{
2008-07-12 22:00:00 +02:00
host.rd.flush( host.rd.address, host.rd.target, host.rd.buffer );
2007-11-14 22:00:00 +01:00
*host.rd.buffer = 0;
}
}
2011-03-09 22:00:00 +01:00
Q_strcat( host.rd.buffer, txt );
2007-11-14 22:00:00 +01:00
return;
}
Con_Print( txt ); // echo to client console
}
2007-09-10 22:00:00 +02:00
/*
=================
Host_Error
=================
*/
void Host_Error( const char *error, ... )
{
2010-07-26 22:00:00 +02:00
static char hosterror1[MAX_SYSPATH];
static char hosterror2[MAX_SYSPATH];
2010-10-26 22:00:00 +02:00
static qboolean recursive = false;
2007-09-10 22:00:00 +02:00
va_list argptr;
2011-03-19 22:00:00 +01:00
if( host.mouse_visible )
{
// hide mouse
while( ShowCursor( false ) >= 0 );
host.mouse_visible = false;
}
2007-09-10 22:00:00 +02:00
va_start( argptr, error );
2011-03-09 22:00:00 +01:00
Q_vsprintf( hosterror1, error, argptr );
2007-09-10 22:00:00 +02:00
va_end( argptr );
2011-03-09 22:00:00 +01:00
CL_WriteMessageHistory (); // before Q_error call
2010-03-27 22:00:00 +01:00
2011-02-25 22:00:00 +01:00
if( host.framecount < 3 )
2008-11-04 22:00:00 +01:00
{
2011-03-12 22:00:00 +01:00
Sys_Error( "Host_InitError: %s", hosterror1 );
2008-11-04 22:00:00 +01:00
}
2008-11-05 22:00:00 +01:00
else if( host.framecount == host.errorframe )
{
2011-03-12 22:00:00 +01:00
Sys_Error( "Host_MultiError: %s", hosterror2 );
2008-11-05 22:00:00 +01:00
return;
}
2011-02-25 22:00:00 +01:00
else
{
Msg( "Host_Error: %s", hosterror1 );
}
// host is shutting down. don't invoke infinite loop
if( host.state == HOST_SHUTDOWN ) return;
2007-09-10 22:00:00 +02:00
2008-08-04 22:00:00 +02:00
if( recursive )
2007-09-10 22:00:00 +02:00
{
2008-11-06 22:00:00 +01:00
Msg( "Host_RecursiveError: %s", hosterror2 );
2011-03-12 22:00:00 +01:00
Sys_Error( hosterror1 );
2007-11-21 22:00:00 +01:00
return; // don't multiple executes
2007-09-10 22:00:00 +02:00
}
2007-11-21 22:00:00 +01:00
2007-09-10 22:00:00 +02:00
recursive = true;
2011-03-09 22:00:00 +01:00
Q_strncpy( hosterror2, hosterror1, MAX_SYSPATH );
2008-11-06 22:00:00 +01:00
host.errorframe = host.framecount; // to avoid multply calls per frame
2011-03-09 22:00:00 +01:00
Q_sprintf( host.finalmsg, "Server crashed: %s\n", hosterror1 );
2007-09-10 22:00:00 +02:00
2007-11-21 22:00:00 +01:00
SV_Shutdown( false );
2007-09-10 22:00:00 +02:00
CL_Drop(); // drop clients
recursive = false;
Host_AbortCurrentFrame();
2007-09-22 22:00:00 +02:00
host.state = HOST_ERROR;
2007-09-10 22:00:00 +02:00
}
void Host_Error_f( void )
{
2010-02-18 22:00:00 +01:00
const char *error = Cmd_Argv( 1 );
if( !*error ) error = "Invoked host error";
Host_Error( "%s\n", error );
}
void Sys_Error_f( void )
{
const char *error = Cmd_Argv( 1 );
if( !*error ) error = "Invoked sys error";
2011-03-12 22:00:00 +01:00
Sys_Error( "%s\n", error );
2007-11-17 22:00:00 +01:00
}
2010-03-27 22:00:00 +01:00
void Net_Error_f( void )
{
2011-03-09 22:00:00 +01:00
Q_strncpy( host.finalmsg, Cmd_Argv( 1 ), sizeof( host.finalmsg ));
2010-03-27 22:00:00 +01:00
SV_ForceError();
}
2007-11-17 22:00:00 +01:00
/*
=================
Host_Crash_f
=================
*/
2009-10-02 22:00:00 +02:00
static void Host_Crash_f( void )
2007-11-17 22:00:00 +01:00
{
*(int *)0 = 0xffffffff;
}
2011-03-12 22:00:00 +01:00
void Host_InitCommon( const char *progname, qboolean bChangeGame )
2008-07-06 22:00:00 +02:00
{
2011-03-12 22:00:00 +01:00
MEMORYSTATUS lpBuffer;
char dev_level[4];
char szTemp[MAX_SYSPATH];
2011-03-09 22:00:00 +01:00
2011-03-12 22:00:00 +01:00
lpBuffer.dwLength = sizeof( MEMORYSTATUS );
GlobalMemoryStatus( &lpBuffer );
2008-07-06 22:00:00 +02:00
2011-03-12 22:00:00 +01:00
host.oldFilter = SetUnhandledExceptionFilter( Sys_Crash );
2010-11-28 22:00:00 +01:00
host.hInst = GetModuleHandle( NULL );
2011-03-12 22:00:00 +01:00
host.change_game = bChangeGame;
host.state = HOST_INIT; // initialzation started
host.developer = 0;
CRT_Init(); // init some CRT functions
// some commands may turn engine into infinity loop,
// e.g. xash.exe +game xash -game xash
// so we clearing all cmd_args, but leave dbg states as well
Sys_ParseCommandLine( GetCommandLine( ));
SetErrorMode( SEM_FAILCRITICALERRORS ); // no abort/retry/fail errors
2011-03-09 22:00:00 +01:00
host.mempool = Mem_AllocPool( "Zone Engine" );
2010-11-28 22:00:00 +01:00
2011-03-12 22:00:00 +01:00
if( Sys_CheckParm( "-console" )) host.developer = 1;
if( Sys_CheckParm( "-dev" ))
{
if( Sys_GetParmFromCmdLine( "-dev", dev_level ))
{
if( Q_isdigit( dev_level ))
host.developer = abs( Q_atoi( dev_level ));
else host.developer++; // -dev == 1, -dev -console == 2
}
else host.developer++; // -dev == 1, -dev -console == 2
}
host.type = HOST_NORMAL; // predict state
host.con_showalways = true;
// we can specified custom name, from Sys_NewInstance
if( GetModuleFileName( NULL, szTemp, sizeof( szTemp )) && !host.change_game )
FS_FileBase( szTemp, SI.ModuleName );
if( SI.ModuleName[0] == '#' ) host.type = HOST_DEDICATED;
// determine host type
if( progname[0] == '#' )
{
Q_strncpy( SI.ModuleName, progname + 1, sizeof( SI.ModuleName ));
host.type = HOST_DEDICATED;
}
else Q_strncpy( SI.ModuleName, progname, sizeof( SI.ModuleName ));
if( host.type == HOST_DEDICATED )
{
// check for duplicate dedicated server
host.hMutex = CreateMutex( NULL, 0, "Xash Dedicated Server" );
if( !host.hMutex )
{
MSGBOX( "Dedicated server already running" );
Sys_Quit();
return;
}
2011-03-13 22:00:00 +01:00
Sys_MergeCommandLine( GetCommandLine( ));
2011-03-12 22:00:00 +01:00
CloseHandle( host.hMutex );
host.hMutex = CreateSemaphore( NULL, 0, 1, "Xash Dedicated Server" );
if( host.developer < 3 ) host.developer = 3; // otherwise we see empty console
host.change_game = false;
}
else
{
// don't show console as default
if( host.developer < D_WARN ) host.con_showalways = false;
}
if( GetModuleFileName( hCurrent, szTemp, sizeof( szTemp )))
FS_FileBase( szTemp, szTemp );
2011-03-13 22:00:00 +01:00
// protect to rename xash.dll
if( Q_stricmp( szTemp, "xash" ) && Com_RandomLong( 0, 1 ))
2011-03-12 22:00:00 +01:00
{
host.type = HOST_CREDITS;
host.con_showalways = true;
Con_CreateConsole();
Sys_Break( show_credits, szTemp );
}
Con_CreateConsole();
// first text message into console or log
MsgDev( D_NOTE, "Sys_LoadLibrary: Loading xash.dll - ok\n" );
2011-03-09 22:00:00 +01:00
// startup cmds and cvars subsystem
Cmd_Init();
Cvar_Init();
2011-03-08 22:00:00 +01:00
2011-03-09 22:00:00 +01:00
// share developer level across all dlls
2011-03-11 22:00:00 +01:00
Q_snprintf( dev_level, sizeof( dev_level ), "%i", host.developer );
2011-03-09 22:00:00 +01:00
Cvar_Get( "developer", dev_level, CVAR_INIT, "current developer level" );
Cmd_AddCommand( "exec", Host_Exec_f, "execute a script file" );
Cmd_AddCommand( "memlist", Host_MemStats_f, "prints memory pool information" );
2011-03-12 22:00:00 +01:00
2011-03-08 22:00:00 +01:00
FS_Init();
2011-02-28 22:00:00 +01:00
Image_Init();
Sound_Init();
2009-09-13 22:00:00 +02:00
FS_LoadGameInfo( NULL );
2011-03-13 22:00:00 +01:00
Q_strncpy( host.gamefolder, GI->gamefolder, sizeof( host.gamefolder ));
2011-02-28 22:00:00 +01:00
2010-10-28 22:00:00 +02:00
Host_InitEvents();
2011-03-13 22:00:00 +01:00
HPAK_Init();
2010-10-28 22:00:00 +02:00
2008-07-06 22:00:00 +02:00
IN_Init();
2011-03-12 22:00:00 +01:00
Key_Init();
2008-07-06 22:00:00 +02:00
}
void Host_FreeCommon( void )
{
2011-02-28 22:00:00 +01:00
Image_Shutdown();
Sound_Shutdown();
2010-10-07 22:00:00 +02:00
Netchan_Shutdown();
2011-03-08 22:00:00 +01:00
FS_Shutdown();
2011-03-09 22:00:00 +01:00
Mem_FreePool( &host.mempool );
2008-07-06 22:00:00 +02:00
}
2007-11-17 22:00:00 +01:00
/*
=================
2011-03-12 22:00:00 +01:00
Host_Main
2007-11-17 22:00:00 +01:00
=================
*/
2011-03-12 22:00:00 +01:00
int EXPORT Host_Main( const char *progname, int bChangeGame, pfnChangeGame func )
2007-11-17 22:00:00 +01:00
{
2011-03-12 22:00:00 +01:00
static double oldtime, newtime;
2007-11-17 22:00:00 +01:00
2011-03-12 22:00:00 +01:00
pChangeGame = func;
2011-03-10 22:00:00 +01:00
2011-03-12 22:00:00 +01:00
Host_InitCommon( progname, bChangeGame );
2007-11-17 22:00:00 +01:00
// init commands and vars
2010-03-25 22:00:00 +01:00
if( host.developer >= 3 )
2007-11-17 22:00:00 +01:00
{
2010-02-18 22:00:00 +01:00
Cmd_AddCommand ( "sys_error", Sys_Error_f, "just throw a fatal error to test shutdown procedures");
Cmd_AddCommand ( "host_error", Host_Error_f, "just throw a host error to test shutdown procedures");
Cmd_AddCommand ( "crash", Host_Crash_f, "a way to force a bus error for development reasons");
2010-03-27 22:00:00 +01:00
Cmd_AddCommand ( "net_error", Net_Error_f, "send network bad message from random place");
2007-11-17 22:00:00 +01:00
}
2008-07-17 22:00:00 +02:00
2010-07-29 22:00:00 +02:00
host_cheats = Cvar_Get( "sv_cheats", "0", CVAR_LATCH, "allow cheat variables to enable" );
2010-06-20 22:00:00 +02:00
host_maxfps = Cvar_Get( "fps_max", "72", CVAR_ARCHIVE, "host fps upper limit" );
2009-02-03 22:00:00 +01:00
host_framerate = Cvar_Get( "host_framerate", "0", 0, "locks frame timing to this value in seconds" );
2010-10-22 22:00:00 +02:00
host_serverstate = Cvar_Get( "host_serverstate", "0", CVAR_INIT, "displays current server state" );
host_gameloaded = Cvar_Get( "host_gameloaded", "0", CVAR_INIT, "inidcates a loaded game.dll" );
2010-10-14 22:00:00 +02:00
host_limitlocal = Cvar_Get( "host_limitlocal", "0", 0, "apply cl_cmdrate and rate to loopback connection" );
2011-03-09 22:00:00 +01:00
con_gamemaps = Cvar_Get( "con_gamemaps", "1", CVAR_ARCHIVE, "when true show only maps in game folder" );
2007-11-17 22:00:00 +01:00
2010-04-17 22:00:00 +02:00
// content control
2010-12-27 22:00:00 +01:00
Cvar_Get( "violence_hgibs", "1", CVAR_ARCHIVE, "show human gib entities" );
Cvar_Get( "violence_agibs", "1", CVAR_ARCHIVE, "show alien gib entities" );
Cvar_Get( "violence_hblood", "1", CVAR_ARCHIVE, "draw human blood" );
Cvar_Get( "violence_ablood", "1", CVAR_ARCHIVE, "draw alien blood" );
2010-11-20 22:00:00 +01:00
2010-07-29 22:00:00 +02:00
if( host.type != HOST_DEDICATED )
{
// when we in developer-mode automatically turn cheats on
2010-10-31 22:00:00 +01:00
if( host.developer > 1 ) Cvar_SetFloat( "sv_cheats", 1.0f );
2010-12-27 22:00:00 +01:00
Cbuf_AddText( "exec video.cfg\n" );
2010-07-29 22:00:00 +02:00
}
2010-04-17 22:00:00 +02:00
2010-11-21 22:00:00 +01:00
Mod_Init();
2007-11-17 22:00:00 +01:00
NET_Init();
Netchan_Init();
2008-07-11 22:00:00 +02:00
2007-11-17 22:00:00 +01:00
SV_Init();
CL_Init();
2008-07-06 22:00:00 +02:00
2009-07-12 22:00:00 +02:00
if( host.type == HOST_DEDICATED )
{
2009-09-10 22:00:00 +02:00
Cmd_AddCommand( "quit", Sys_Quit, "quit the game" );
Cmd_AddCommand( "exit", Sys_Quit, "quit the game" );
2010-07-30 22:00:00 +02:00
// dedicated servers using settings from server.cfg file
Cbuf_AddText( va( "exec %s\n", Cvar_VariableString( "servercfgfile" )));
Cbuf_Execute();
Cbuf_AddText( va( "map %s\n", Cvar_VariableString( "defaultmap" )));
2009-07-12 22:00:00 +02:00
}
2009-10-02 22:00:00 +02:00
else
{
Cmd_AddCommand( "minimize", Host_Minimize_f, "minimize main window to tray" );
2010-11-26 22:00:00 +01:00
Cbuf_AddText( "exec config.cfg\n" );
2009-10-02 22:00:00 +02:00
}
2010-06-18 22:00:00 +02:00
// allow to change game from the console
2011-03-21 22:00:00 +01:00
if( pChangeGame != NULL )
{
Cmd_AddCommand( "game", Host_ChangeGame_f, "change game" );
Cvar_Get( "host_allow_changegame", "1", CVAR_READ_ONLY, "allows to change games" );
}
else
{
Cvar_Get( "host_allow_changegame", "0", CVAR_READ_ONLY, "allows to change games" );
}
2010-06-18 22:00:00 +02:00
2008-11-04 22:00:00 +01:00
host.errorframe = 0;
2010-01-30 22:00:00 +01:00
Cbuf_Execute();
2010-10-09 22:00:00 +02:00
SCR_CheckStartupVids(); // must be last
2011-03-09 22:00:00 +01:00
// post initializations
switch( host.type )
{
case HOST_NORMAL:
2011-03-12 22:00:00 +01:00
Con_ShowConsole( false ); // hide console
2011-03-09 22:00:00 +01:00
// execute startup config and cmdline
2011-03-12 22:00:00 +01:00
Cbuf_AddText( va( "exec %s.rc\n", SI.ModuleName ));
2011-03-09 22:00:00 +01:00
case HOST_DEDICATED:
Cbuf_Execute();
// if stuffcmds wasn't run, then init.rc is probably missing, use default
if( !host.stuffcmdsrun ) Cbuf_ExecuteText( EXEC_NOW, "stuffcmds\n" );
break;
}
2011-03-12 22:00:00 +01:00
host.change_game = false; // done
2011-03-09 22:00:00 +01:00
Cmd_RemoveCommand( "setr" ); // remove potentially backdoor for change render settings
Cmd_RemoveCommand( "setgl" );
2010-10-09 22:00:00 +02:00
2011-02-26 22:00:00 +01:00
// we need to execute it again here
2011-03-03 22:00:00 +01:00
Cmd_ExecuteString( "exec config.cfg\n" );
2010-10-09 22:00:00 +02:00
oldtime = Sys_DoubleTime();
2007-11-17 22:00:00 +01:00
// main window message loop
2011-03-12 22:00:00 +01:00
while( 1 )
2007-11-17 22:00:00 +01:00
{
2010-10-09 22:00:00 +02:00
newtime = Sys_DoubleTime ();
Host_Frame( newtime - oldtime );
oldtime = newtime;
2007-11-17 22:00:00 +01:00
}
2011-03-12 22:00:00 +01:00
// never reached
return 0;
2007-11-17 22:00:00 +01:00
}
/*
=================
Host_Shutdown
=================
*/
2011-03-12 22:00:00 +01:00
void EXPORT Host_Shutdown( void )
2007-11-17 22:00:00 +01:00
{
2011-03-12 22:00:00 +01:00
if( host.shutdown_issued ) return;
host.shutdown_issued = true;
2009-09-28 22:00:00 +02:00
2011-03-12 22:00:00 +01:00
if( host.state != HOST_ERR_FATAL ) host.state = HOST_SHUTDOWN; // prepare host to normal shutdown
if( !host.change_game ) Q_strncpy( host.finalmsg, "Server shutdown\n", sizeof( host.finalmsg ));
2008-06-06 22:00:00 +02:00
2011-03-15 22:00:00 +01:00
if( host.type == HOST_NORMAL )
Host_WriteConfig();
2010-06-16 22:00:00 +02:00
SV_Shutdown( false );
CL_Shutdown();
2010-12-13 22:00:00 +01:00
Mod_Shutdown();
2010-02-28 22:00:00 +01:00
NET_Shutdown();
2007-11-17 22:00:00 +01:00
Host_FreeCommon();
2011-03-12 22:00:00 +01:00
Con_DestroyConsole();
// restore filter
if( host.oldFilter ) SetUnhandledExceptionFilter( host.oldFilter );
2010-10-26 22:00:00 +02:00
}
// main DLL entry point
BOOL WINAPI DllMain( HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved )
{
2011-03-12 22:00:00 +01:00
hCurrent = hinstDLL;
2010-10-26 22:00:00 +02:00
return TRUE;
2007-06-21 22:00:00 +02:00
}