2007-06-21 22:00:00 +02:00
|
|
|
|
//=======================================================================
|
|
|
|
|
// Copyright XashXT Group 2007 <20>
|
|
|
|
|
// host.c - dedicated and shared host
|
|
|
|
|
//=======================================================================
|
|
|
|
|
|
|
|
|
|
#include <setjmp.h>
|
|
|
|
|
#include <windows.h>
|
|
|
|
|
#include <dsound.h>
|
|
|
|
|
#include "engine.h"
|
|
|
|
|
|
2007-09-06 22:00:00 +02:00
|
|
|
|
platform_exp_t *pi; // fundamental callbacks
|
2007-09-03 22:00:00 +02:00
|
|
|
|
host_parm_t host; // host parms
|
2007-09-11 22:00:00 +02:00
|
|
|
|
stdinout_api_t std;
|
2007-06-21 22:00:00 +02:00
|
|
|
|
|
|
|
|
|
byte *zonepool;
|
|
|
|
|
int ActiveApp;
|
2007-09-06 22:00:00 +02:00
|
|
|
|
bool Minimized;
|
2007-09-11 22:00:00 +02:00
|
|
|
|
char *buildstring = __TIME__ " " __DATE__;
|
2007-09-02 22:00:00 +02:00
|
|
|
|
|
2007-06-21 22:00:00 +02:00
|
|
|
|
void Key_Init (void);
|
|
|
|
|
void SCR_EndLoadingPlaque (void);
|
|
|
|
|
|
|
|
|
|
HINSTANCE global_hInstance;
|
2007-09-11 22:00:00 +02:00
|
|
|
|
dll_info_t platform_dll = { "platform.dll", NULL, "CreateAPI", NULL, NULL, true, PLATFORM_API_VERSION, sizeof(platform_exp_t) };
|
2007-06-21 22:00:00 +02:00
|
|
|
|
|
|
|
|
|
cvar_t *timescale;
|
|
|
|
|
cvar_t *fixedtime;
|
|
|
|
|
cvar_t *showtrace;
|
|
|
|
|
|
|
|
|
|
void Host_InitPlatform( char *funcname, int argc, char **argv )
|
|
|
|
|
{
|
2007-09-11 22:00:00 +02:00
|
|
|
|
static stdinout_api_t pistd;
|
|
|
|
|
platform_t CreatePlat;
|
2007-06-21 22:00:00 +02:00
|
|
|
|
|
2007-06-29 22:00:00 +02:00
|
|
|
|
//make callbacks
|
2007-07-28 22:00:00 +02:00
|
|
|
|
pistd.printf = Msg;
|
|
|
|
|
pistd.dprintf = MsgDev;
|
2007-08-17 22:00:00 +02:00
|
|
|
|
pistd.wprintf = MsgWarn;
|
2007-07-28 22:00:00 +02:00
|
|
|
|
pistd.error = Sys_Error;
|
2007-06-21 22:00:00 +02:00
|
|
|
|
|
2007-09-11 22:00:00 +02:00
|
|
|
|
Sys_LoadLibrary( &platform_dll );
|
2007-06-21 22:00:00 +02:00
|
|
|
|
|
2007-09-11 22:00:00 +02:00
|
|
|
|
CreatePlat = (void *)platform_dll.main;
|
|
|
|
|
pi = CreatePlat( &pistd );
|
2007-06-21 22:00:00 +02:00
|
|
|
|
|
2007-09-10 22:00:00 +02:00
|
|
|
|
// initialize our platform :)
|
2007-08-13 22:00:00 +02:00
|
|
|
|
pi->Init( argc, argv );
|
2007-06-21 22:00:00 +02:00
|
|
|
|
|
2007-06-29 22:00:00 +02:00
|
|
|
|
//TODO: init basedir here
|
|
|
|
|
pi->LoadGameInfo("gameinfo.txt");
|
2007-06-21 22:00:00 +02:00
|
|
|
|
zonepool = Mem_AllocPool("Zone Engine");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Host_FreePlatform( void )
|
|
|
|
|
{
|
2007-09-11 22:00:00 +02:00
|
|
|
|
if(platform_dll.link)
|
2007-06-21 22:00:00 +02:00
|
|
|
|
{
|
|
|
|
|
Mem_FreePool( &zonepool );
|
2007-06-29 22:00:00 +02:00
|
|
|
|
pi->Shutdown();
|
2007-09-11 22:00:00 +02:00
|
|
|
|
Sys_FreeLibrary( &platform_dll );
|
2007-06-21 22:00:00 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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 )
|
|
|
|
|
{
|
|
|
|
|
longjmp(host.abortframe, 1);
|
|
|
|
|
}
|
|
|
|
|
|
2007-06-21 22:00:00 +02:00
|
|
|
|
/*
|
|
|
|
|
=================
|
|
|
|
|
Host_Init
|
|
|
|
|
=================
|
|
|
|
|
*/
|
|
|
|
|
void Host_Init (char *funcname, int argc, char **argv)
|
|
|
|
|
{
|
|
|
|
|
char *s;
|
|
|
|
|
|
2007-09-03 22:00:00 +02:00
|
|
|
|
host.state = HOST_INIT; //initialzation started
|
|
|
|
|
|
2007-06-21 22:00:00 +02:00
|
|
|
|
global_hInstance = (HINSTANCE)GetModuleHandle( NULL );
|
2007-08-13 22:00:00 +02:00
|
|
|
|
|
2007-09-03 22:00:00 +02:00
|
|
|
|
if(!strcmp(funcname, "host_dedicated")) host.type = HOST_DEDICATED;
|
|
|
|
|
else if(!strcmp(funcname, "host_shared")) host.type = HOST_NORMAL;
|
|
|
|
|
else host.type = HOST_OFFLINE; // launcher can loading engine for some reasons
|
2007-06-21 22:00:00 +02:00
|
|
|
|
|
2007-09-03 22:00:00 +02:00
|
|
|
|
COM_InitArgv (argc, argv); // init host.debug & host.developer here
|
|
|
|
|
Host_InitPlatform( funcname, argc, argv );
|
2007-06-21 22:00:00 +02:00
|
|
|
|
|
2007-09-03 22:00:00 +02:00
|
|
|
|
MsgDev(D_INFO, "------- Loading bin/engine.dll [%g] -------\n", ENGINE_VERSION );
|
|
|
|
|
|
2007-09-06 22:00:00 +02:00
|
|
|
|
Cbuf_Init ();
|
|
|
|
|
Cmd_Init ();
|
|
|
|
|
Cvar_Init ();
|
|
|
|
|
Key_Init ();
|
2007-09-02 22:00:00 +02:00
|
|
|
|
|
2007-06-21 22:00:00 +02:00
|
|
|
|
// we need to add the early commands twice, because
|
|
|
|
|
// a basedir or cddir needs to be set before execing
|
|
|
|
|
// config files, but we want other parms to override
|
|
|
|
|
// the settings of the config files
|
|
|
|
|
Cbuf_AddEarlyCommands (false);
|
|
|
|
|
Cbuf_Execute ();
|
|
|
|
|
|
|
|
|
|
Cbuf_AddText ("exec default.cfg\n");
|
|
|
|
|
Cbuf_AddText ("exec config.cfg\n");
|
|
|
|
|
Cbuf_AddEarlyCommands (true);
|
|
|
|
|
Cbuf_Execute ();
|
|
|
|
|
|
|
|
|
|
// init commands and vars
|
2007-09-10 22:00:00 +02:00
|
|
|
|
Cmd_AddCommand ("error", Host_Error_f);
|
2007-06-21 22:00:00 +02:00
|
|
|
|
|
|
|
|
|
host_speeds = Cvar_Get ("host_speeds", "0", 0);
|
2007-09-06 22:00:00 +02:00
|
|
|
|
log_stats = Cvar_Get ("log_stats", "0", 0);
|
|
|
|
|
developer = Cvar_Get ("developer", "0", 0);
|
2007-06-21 22:00:00 +02:00
|
|
|
|
timescale = Cvar_Get ("timescale", "1", 0);
|
|
|
|
|
fixedtime = Cvar_Get ("fixedtime", "0", 0);
|
|
|
|
|
showtrace = Cvar_Get ("showtrace", "0", 0);
|
2007-09-06 22:00:00 +02:00
|
|
|
|
if(host.type == HOST_DEDICATED) dedicated = Cvar_Get ("dedicated", "1", CVAR_NOSET);
|
|
|
|
|
else dedicated = Cvar_Get ("dedicated", "0", CVAR_NOSET);
|
2007-06-21 22:00:00 +02:00
|
|
|
|
|
|
|
|
|
s = va("%4.2f %s %s %s", VERSION, "x86", __DATE__, BUILDSTRING);
|
|
|
|
|
Cvar_Get ("version", s, CVAR_SERVERINFO|CVAR_NOSET);
|
|
|
|
|
|
2007-09-06 22:00:00 +02:00
|
|
|
|
if (dedicated->value) Cmd_AddCommand ("quit", Com_Quit);
|
2007-09-10 22:00:00 +02:00
|
|
|
|
|
2007-06-21 22:00:00 +02:00
|
|
|
|
NET_Init ();
|
|
|
|
|
Netchan_Init ();
|
|
|
|
|
|
|
|
|
|
SV_Init();
|
|
|
|
|
CL_Init();
|
|
|
|
|
|
|
|
|
|
// add + commands from command line
|
2007-09-06 22:00:00 +02:00
|
|
|
|
if (!Cbuf_AddLateCommands ())
|
2007-06-21 22:00:00 +02:00
|
|
|
|
{
|
|
|
|
|
// if the user didn't give any commands, run default action
|
2007-09-06 22:00:00 +02:00
|
|
|
|
if (!dedicated->value) Cbuf_AddText ("d1\n");
|
2007-06-21 22:00:00 +02:00
|
|
|
|
else Cbuf_AddText ("dedicated_start\n");
|
|
|
|
|
Cbuf_Execute ();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{ // the user asked for something explicit
|
|
|
|
|
// so drop the loading plaque
|
|
|
|
|
SCR_EndLoadingPlaque ();
|
|
|
|
|
}
|
2007-09-09 22:00:00 +02:00
|
|
|
|
Sys_DoubleTime(); // initialize timer
|
2007-06-21 22:00:00 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
=================
|
|
|
|
|
Host_Frame
|
|
|
|
|
=================
|
|
|
|
|
*/
|
|
|
|
|
void Host_Frame (double time)
|
|
|
|
|
{
|
2007-09-10 22:00:00 +02:00
|
|
|
|
char *s;
|
2007-09-06 22:00:00 +02:00
|
|
|
|
static double time_before, time_between, time_after;
|
2007-06-21 22:00:00 +02:00
|
|
|
|
|
2007-09-10 22:00:00 +02:00
|
|
|
|
if (setjmp(host.abortframe)) return;
|
2007-06-21 22:00:00 +02:00
|
|
|
|
|
2007-09-06 22:00:00 +02:00
|
|
|
|
if ( log_stats->modified )
|
|
|
|
|
{
|
|
|
|
|
log_stats->modified = false;
|
|
|
|
|
if ( log_stats->value )
|
|
|
|
|
{
|
|
|
|
|
if ( log_stats_file )
|
|
|
|
|
{
|
|
|
|
|
FS_Close( log_stats_file );
|
|
|
|
|
log_stats_file = 0;
|
|
|
|
|
}
|
|
|
|
|
log_stats_file = FS_Open( "stats.log", "w" );
|
|
|
|
|
if ( log_stats_file )
|
|
|
|
|
FS_Printf( log_stats_file, "entities,dlights,parts,frame time\n" );
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if ( log_stats_file )
|
|
|
|
|
{
|
|
|
|
|
FS_Close( log_stats_file );
|
|
|
|
|
log_stats_file = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2007-06-21 22:00:00 +02:00
|
|
|
|
if (showtrace->value)
|
|
|
|
|
{
|
|
|
|
|
extern int c_traces, c_brush_traces;
|
|
|
|
|
extern int c_pointcontents;
|
|
|
|
|
|
2007-07-28 22:00:00 +02:00
|
|
|
|
Msg ("%4i traces %4i points\n", c_traces, c_pointcontents);
|
2007-06-21 22:00:00 +02:00
|
|
|
|
c_traces = 0;
|
|
|
|
|
c_brush_traces = 0;
|
|
|
|
|
c_pointcontents = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2007-09-11 22:00:00 +02:00
|
|
|
|
|
|
|
|
|
rand(); // keep the random time dependent
|
|
|
|
|
|
2007-06-21 22:00:00 +02:00
|
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
s = Sys_ConsoleInput ();
|
2007-09-11 22:00:00 +02:00
|
|
|
|
if(s) Cbuf_AddText (va("%s\n",s));
|
2007-06-21 22:00:00 +02:00
|
|
|
|
} while (s);
|
|
|
|
|
Cbuf_Execute ();
|
2007-09-06 22:00:00 +02:00
|
|
|
|
|
|
|
|
|
if (host_speeds->value) time_before = Sys_DoubleTime();
|
|
|
|
|
|
2007-06-21 22:00:00 +02:00
|
|
|
|
SV_Frame (time);
|
2007-09-06 22:00:00 +02:00
|
|
|
|
|
|
|
|
|
if (host_speeds->value) time_between = Sys_DoubleTime();
|
|
|
|
|
|
2007-06-21 22:00:00 +02:00
|
|
|
|
CL_Frame (time);
|
|
|
|
|
|
2007-09-06 22:00:00 +02:00
|
|
|
|
if (host_speeds->value) time_after = Sys_DoubleTime();
|
|
|
|
|
if (host_speeds->value)
|
|
|
|
|
{
|
|
|
|
|
double all, sv, gm, cl, rf;
|
|
|
|
|
|
|
|
|
|
all = time_after - time_before;
|
|
|
|
|
sv = time_between - time_before;
|
|
|
|
|
cl = time_after - time_between;
|
|
|
|
|
gm = time_after_game - time_before_game;
|
|
|
|
|
rf = time_after_ref - time_before_ref;
|
|
|
|
|
sv -= gm;
|
|
|
|
|
cl -= rf;
|
|
|
|
|
Msg ("all:%.3f sv:%.3f gm:%.3f cl:%.3f rf:%.3f\n", all, sv, gm, cl, rf);
|
|
|
|
|
}
|
2007-09-07 22:00:00 +02:00
|
|
|
|
host.framecount++;
|
2007-06-21 22:00:00 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
=================
|
|
|
|
|
Host_Main
|
|
|
|
|
=================
|
|
|
|
|
*/
|
|
|
|
|
void Host_Main( void )
|
|
|
|
|
{
|
2007-09-11 22:00:00 +02:00
|
|
|
|
MSG msg;
|
2007-09-09 22:00:00 +02:00
|
|
|
|
static double time, oldtime, newtime;
|
2007-06-21 22:00:00 +02:00
|
|
|
|
|
2007-09-09 22:00:00 +02:00
|
|
|
|
oldtime = host.realtime;
|
2007-06-21 22:00:00 +02:00
|
|
|
|
|
|
|
|
|
// main window message loop
|
2007-09-06 22:00:00 +02:00
|
|
|
|
while (1)
|
2007-06-21 22:00:00 +02:00
|
|
|
|
{
|
|
|
|
|
// if at a full screen console, don't update unless needed
|
2007-09-06 22:00:00 +02:00
|
|
|
|
if (Minimized || (dedicated && dedicated->value) )
|
2007-06-21 22:00:00 +02:00
|
|
|
|
{
|
2007-09-06 22:00:00 +02:00
|
|
|
|
Sleep (1);
|
2007-06-21 22:00:00 +02:00
|
|
|
|
}
|
2007-09-11 22:00:00 +02:00
|
|
|
|
|
|
|
|
|
while (PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE))
|
|
|
|
|
{
|
|
|
|
|
if (!GetMessage (&msg, NULL, 0, 0)) Com_Quit ();
|
|
|
|
|
host.sv_timer = msg.time;
|
|
|
|
|
TranslateMessage (&msg);
|
|
|
|
|
DispatchMessage (&msg);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
do
|
2007-09-05 22:00:00 +02:00
|
|
|
|
{
|
2007-09-07 22:00:00 +02:00
|
|
|
|
newtime = Sys_DoubleTime();
|
2007-09-09 22:00:00 +02:00
|
|
|
|
time = newtime - oldtime;
|
2007-09-05 22:00:00 +02:00
|
|
|
|
|
2007-09-09 22:00:00 +02:00
|
|
|
|
} while (time < 0.001);
|
2007-06-21 22:00:00 +02:00
|
|
|
|
|
2007-09-09 22:00:00 +02:00
|
|
|
|
Host_Frame (time); // engine frame
|
2007-06-21 22:00:00 +02:00
|
|
|
|
oldtime = newtime;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
=================
|
|
|
|
|
Host_Shutdown
|
|
|
|
|
=================
|
|
|
|
|
*/
|
|
|
|
|
void Host_Free (void)
|
|
|
|
|
{
|
2007-09-07 22:00:00 +02:00
|
|
|
|
SV_Shutdown ("Server shutdown\n", false);
|
2007-06-21 22:00:00 +02:00
|
|
|
|
CL_Shutdown ();
|
|
|
|
|
Host_FreePlatform();
|
2007-09-10 22:00:00 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
=================
|
|
|
|
|
Host_Error
|
|
|
|
|
=================
|
|
|
|
|
*/
|
|
|
|
|
void Host_Error( const char *error, ... )
|
|
|
|
|
{
|
|
|
|
|
static char hosterror1[MAX_INPUTLINE];
|
|
|
|
|
static char hosterror2[MAX_INPUTLINE];
|
|
|
|
|
static bool recursive = false;
|
|
|
|
|
va_list argptr;
|
|
|
|
|
|
|
|
|
|
va_start( argptr, error );
|
|
|
|
|
vsprintf( hosterror1, error, argptr );
|
|
|
|
|
va_end( argptr );
|
|
|
|
|
|
|
|
|
|
if (host.framecount < 3 || host.state == HOST_SHUTDOWN)
|
|
|
|
|
Sys_Error ("%s", hosterror1 );
|
|
|
|
|
else Msg("Host_Error: %s", hosterror1);
|
|
|
|
|
|
|
|
|
|
if(recursive)
|
|
|
|
|
{
|
|
|
|
|
Msg("Host_Error: recursive %s", hosterror2);
|
|
|
|
|
Sys_Error ("%s", hosterror1);
|
|
|
|
|
}
|
|
|
|
|
recursive = true;
|
|
|
|
|
strlcpy(hosterror2, hosterror1, sizeof(hosterror2));
|
|
|
|
|
|
|
|
|
|
SV_Shutdown (va("Server crashed: %s", hosterror1), false);
|
|
|
|
|
CL_Drop(); // drop clients
|
|
|
|
|
|
|
|
|
|
recursive = false;
|
|
|
|
|
Host_AbortCurrentFrame();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Host_Error_f( void )
|
|
|
|
|
{
|
|
|
|
|
Host_Error( "%s\n", Cmd_Argv(1));
|
2007-06-21 22:00:00 +02:00
|
|
|
|
}
|