]> git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - host.c
Consolidate FS_Init and associated code
[xonotic/darkplaces.git] / host.c
diff --git a/host.c b/host.c
index f19f950c6e258dfbb6fd2cfb4e3c05d09fbbb170..301ea8ea8b5efb87e63b317a2ba41f8c1bbb1deb 100644 (file)
--- a/host.c
+++ b/host.c
@@ -44,21 +44,10 @@ Memory is cleared / released when a server or client begins, not when they end.
 
 */
 
-// how many frames have occurred
-// (checked by Host_Error and Host_SaveConfig_f)
-int host_framecount = 0;
-// LadyHavoc: set when quit is executed
-qboolean host_shuttingdown = false;
-
-// the accumulated mainloop time since application started (with filtering), without any slowmo or clamping
-double realtime;
-// the main loop wall time for this frame
-double host_dirtytime;
-
 // current client
 client_t *host_client;
 
-jmp_buf host_abortframe;
+host_t host;
 
 // pretend frames take this amount of time (in seconds), 0 = realtime
 cvar_t host_framerate = {CVAR_CLIENT | CVAR_SERVER, "host_framerate","0", "locks frame timing to this value in seconds, 0.05 is 20fps for example, note that this can easily run too fast, use cl_maxfps if you want to limit your framerate instead, or sys_ticrate to limit server speed"};
@@ -104,7 +93,7 @@ void Host_AbortCurrentFrame(void)
        // in case we were previously nice, make us mean again
        Sys_MakeProcessMean();
 
-       longjmp (host_abortframe, 1);
+       longjmp (host.abortframe, 1);
 }
 
 /*
@@ -133,7 +122,7 @@ void Host_Error (const char *error, ...)
 
        // LadyHavoc: if crashing very early, or currently shutting down, do
        // Sys_Error instead
-       if (host_framecount < 3 || host_shuttingdown)
+       if (host.framecount < 3 || host.state == host_shutdown)
                Sys_Error ("Host_Error: %s", hosterrorstring1);
 
        if (hosterror)
@@ -190,8 +179,8 @@ static void Host_ServerOptions (void)
        {
                cls.state = ca_dedicated;
                // check for -dedicated specifying how many players
-               if (i && i + 1 < com_argc && atoi (com_argv[i+1]) >= 1)
-                       svs.maxclients = atoi (com_argv[i+1]);
+               if (i && i + 1 < sys.argc && atoi (sys.argv[i+1]) >= 1)
+                       svs.maxclients = atoi (sys.argv[i+1]);
                if (COM_CheckParm ("-listen"))
                        Con_Printf ("Only one of -dedicated or -listen can be specified\n");
                // default sv_public on for dedicated servers (often hosted by serious administrators), off for listen servers (often hosted by clueless users)
@@ -205,8 +194,8 @@ static void Host_ServerOptions (void)
                if (i)
                {
                        // default players unless specified
-                       if (i + 1 < com_argc && atoi (com_argv[i+1]) >= 1)
-                               svs.maxclients = atoi (com_argv[i+1]);
+                       if (i + 1 < sys.argc && atoi (sys.argv[i+1]) >= 1)
+                               svs.maxclients = atoi (sys.argv[i+1]);
                }
                else
                {
@@ -235,11 +224,8 @@ extern cvar_t sv_writepicture_quality;
 extern cvar_t r_texture_jpeg_fastpicmip;
 static void Host_InitLocal (void)
 {
-       Cmd_AddCommand(&cmd_client, "saveconfig", Host_SaveConfig_f, "save settings to config.cfg (or a specified filename) immediately (also automatic when quitting)");
-       Cmd_AddCommand(&cmd_client, "loadconfig", Host_LoadConfig_f, "reset everything and reload configs");
-       Cmd_AddCommand(&cmd_server, "saveconfig", Host_SaveConfig_f, "save settings to config.cfg (or a specified filename) immediately (also automatic when quitting)");
-       Cmd_AddCommand(&cmd_server, "loadconfig", Host_LoadConfig_f, "reset everything and reload configs");
-
+       Cmd_AddCommand(CMD_SHARED, "saveconfig", Host_SaveConfig_f, "save settings to config.cfg (or a specified filename) immediately (also automatic when quitting)");
+       Cmd_AddCommand(CMD_SHARED, "loadconfig", Host_LoadConfig_f, "reset everything and reload configs");
        Cvar_RegisterVariable (&cl_maxphysicsframesperserverframe);
        Cvar_RegisterVariable (&host_framerate);
        Cvar_RegisterVariable (&host_speeds);
@@ -285,7 +271,7 @@ static void Host_SaveConfig_to(const char *file)
 // dedicated servers initialize the host but don't parse and set the
 // config.cfg cvars
        // LadyHavoc: don't save a config if it crashed in startup
-       if (host_framecount >= 3 && cls.state != ca_dedicated && !COM_CheckParm("-benchmark") && !COM_CheckParm("-capturedemo"))
+       if (host.framecount >= 3 && cls.state != ca_dedicated && !COM_CheckParm("-benchmark") && !COM_CheckParm("-capturedemo"))
        {
                f = FS_OpenRealFile(file, "wb", false);
                if (!f)
@@ -328,6 +314,7 @@ static void Host_AddConfigText(cmd_state_t *cmd)
                Cbuf_InsertText(cmd, "alias startmap_sp \"map start\"\nalias startmap_dm \"map start\"\nexec teu.rc\n");
        else
                Cbuf_InsertText(cmd, "alias startmap_sp \"map start\"\nalias startmap_dm \"map start\"\nexec " STARTCONFIGFILENAME "\n");
+       Cbuf_Execute(cmd);
 }
 
 /*
@@ -671,7 +658,7 @@ void Host_Main(void)
        double time2 = 0;
        double time3 = 0;
        double cl_timer = 0, sv_timer = 0;
-       double clframetime, deltacleantime, olddirtytime, dirtytime;
+       double clframetime, time, oldtime, newtime;
        double wait;
        int pass1, pass2, pass3, i;
        char vabuf[1024];
@@ -679,40 +666,41 @@ void Host_Main(void)
 
        Host_Init();
 
-       realtime = 0;
-       host_dirtytime = Sys_DirtyTime();
-       for (;;)
+       host.realtime = 0;
+       host.dirtytime = Sys_DirtyTime();
+
+       while(host.state != host_shutdown)
        {
-               if (setjmp(host_abortframe))
+               if (setjmp(host.abortframe))
                {
                        SCR_ClearLoadingScreen(false);
                        continue;                       // something bad happened, or the server disconnected
                }
 
-               olddirtytime = host_dirtytime;
-               dirtytime = Sys_DirtyTime();
-               deltacleantime = dirtytime - olddirtytime;
-               if (deltacleantime < 0)
+               oldtime = host.dirtytime;
+               newtime = Sys_DirtyTime();
+               time = newtime - oldtime;
+               if (time < 0)
                {
                        // warn if it's significant
-                       if (deltacleantime < -0.01)
-                               Con_Warnf("Host_Mingled: time stepped backwards (went from %f to %f, difference %f)\n", olddirtytime, dirtytime, deltacleantime);
-                       deltacleantime = 0;
+                       if (time < -0.01)
+                               Con_Warnf("Host_Mingled: time stepped backwards (went from %f to %f, difference %f)\n", oldtime, newtime, time);
+                       time = 0;
                }
-               else if (deltacleantime >= 1800)
+               else if (time >= 1800)
                {
-                       Con_Warnf("Host_Mingled: time stepped forward (went from %f to %f, difference %f)\n", olddirtytime, dirtytime, deltacleantime);
-                       deltacleantime = 0;
+                       Con_Warnf("Host_Mingled: time stepped forward (went from %f to %f, difference %f)\n", oldtime, newtime, time);
+                       time = 0;
                }
-               realtime += deltacleantime;
-               host_dirtytime = dirtytime;
+               host.realtime += time;
+               host.dirtytime = newtime;
 
-               cl_timer += deltacleantime;
-               sv_timer += deltacleantime;
+               cl_timer += time;
+               sv_timer += time;
 
                if (!svs.threaded)
                {
-                       svs.perf_acc_realtime += deltacleantime;
+                       svs.perf_acc_realtime += time;
 
                        // Look for clients who have spawned
                        playing = false;
@@ -743,8 +731,6 @@ void Host_Main(void)
                        }
                }
 
-               if (slowmo.value < 0.00001 && slowmo.value != 0)
-                       Cvar_SetValueQuick(&slowmo, 0);
                if (host_framerate.value < 0.00001 && host_framerate.value != 0)
                        Cvar_SetValueQuick(&host_framerate, 0);
 
@@ -773,25 +759,16 @@ void Host_Main(void)
                // check for commands typed to the host
                Host_GetConsoleCommands();
 
-               // when a server is running we only execute console commands on server frames
-               // (this mainly allows frikbot .way config files to work properly by staying in sync with the server qc)
-               // otherwise we execute them on client frames
-               if (sv.active ? sv_timer > 0 : cl_timer > 0)
-               {
-                       // process console commands
-//                     R_TimeReport("preconsole");
-                       CL_VM_PreventInformationLeaks();
-                       Cbuf_Frame(&cmd_clientfromserver);
-                       Cbuf_Frame(&cmd_client);
-                       Cbuf_Frame(&cmd_server);
-
-                       if(sv.active)
-                       {
-                               Cbuf_Frame(&cmd_serverfromclient);
-                       }
+               // process console commands
+//             R_TimeReport("preconsole");
+               CL_VM_PreventInformationLeaks();
+               Cbuf_Frame(&cmd_client);
+               Cbuf_Frame(&cmd_server);
 
-//                     R_TimeReport("console");
-               }
+               if(sv.active)
+                       Cbuf_Frame(&cmd_serverfromclient);
+
+//             R_TimeReport("console");
 
                //Con_Printf("%6.0f %6.0f\n", cl_timer * 1000000.0, sv_timer * 1000000.0);
 
@@ -879,14 +856,14 @@ void Host_Main(void)
                                framelimit = cl_maxphysicsframesperserverframe.integer;
                                aborttime = Sys_DirtyTime() + 0.1;
                        }
-                       if(slowmo.value > 0 && slowmo.value < 1)
-                               advancetime = min(advancetime, 0.1 / slowmo.value);
+                       if(host_timescale.value > 0 && host_timescale.value < 1)
+                               advancetime = min(advancetime, 0.1 / host_timescale.value);
                        else
                                advancetime = min(advancetime, 0.1);
 
                        if(advancetime > 0)
                        {
-                               offset = Sys_DirtyTime() - dirtytime;if (offset < 0 || offset >= 1800) offset = 0;
+                               offset = Sys_DirtyTime() - newtime;if (offset < 0 || offset >= 1800) offset = 0;
                                offset += sv_timer;
                                ++svs.perf_acc_offset_samples;
                                svs.perf_acc_offset += offset;
@@ -897,7 +874,7 @@ void Host_Main(void)
 
                        // only advance time if not paused
                        // the game also pauses in singleplayer when menu or console is used
-                       sv.frametime = advancetime * slowmo.value;
+                       sv.frametime = advancetime * host_timescale.value;
                        if (host_framerate.value)
                                sv.frametime = host_framerate.value;
                        if (sv.paused || (cl.islocalgame && (key_dest != key_game || key_consoleactive || cl.csqc_paused)))
@@ -920,8 +897,8 @@ void Host_Main(void)
                        // send all messages to the clients
                        SV_SendClientMessages();
 
-                       if (sv.paused == 1 && realtime > sv.pausedstart && sv.pausedstart > 0) {
-                               prog->globals.fp[OFS_PARM0] = realtime - sv.pausedstart;
+                       if (sv.paused == 1 && host.realtime > sv.pausedstart && sv.pausedstart > 0) {
+                               prog->globals.fp[OFS_PARM0] = host.realtime - sv.pausedstart;
                                PRVM_serverglobalfloat(time) = sv.time;
                                prog->ExecuteProgram(prog, PRVM_serverfunction(SV_PausedTic), "QC function SV_PausedTic is missing");
                        }
@@ -948,6 +925,7 @@ void Host_Main(void)
                        R_TimeReport("---");
                        Collision_Cache_NewFrame();
                        R_TimeReport("photoncache");
+#ifdef CONFIG_VIDEO_CAPTURE
                        // decide the simulation time
                        if (cls.capturevideo.active)
                        {
@@ -961,6 +939,10 @@ void Host_Main(void)
                                }
                        }
                        else if (vid_activewindow && cl_maxfps.value >= 1 && !cls.timedemo)
+
+#else
+                       if (vid_activewindow && cl_maxfps.value >= 1 && !cls.timedemo)
+#endif
                        {
                                clframetime = cl.realframetime = max(cl_timer, 1.0 / cl_maxfps.value);
                                // when running slow, we need to sleep to keep input responsive
@@ -978,7 +960,7 @@ void Host_Main(void)
                        // scale playback speed of demos by slowmo cvar
                        if (cls.demoplayback)
                        {
-                               clframetime *= slowmo.value;
+                               clframetime *= host_timescale.value;
                                // if demo playback is paused, don't advance time at all
                                if (cls.demopaused)
                                        clframetime = 0;
@@ -1080,8 +1062,10 @@ void Host_Main(void)
                        sv_timer = 0;
                }
 
-               host_framecount++;
+               host.framecount++;
        }
+
+       Sys_Quit(0);
 }
 
 //============================================================================
@@ -1108,17 +1092,21 @@ static qboolean locksession_run = false;
 static void Host_InitSession(void)
 {
        int i;
+       char *buf;
        Cvar_RegisterVariable(&sessionid);
        Cvar_RegisterVariable(&locksession);
 
        // load the session ID into the read-only cvar
-       if ((i = COM_CheckParm("-sessionid")) && (i + 1 < com_argc))
+       if ((i = COM_CheckParm("-sessionid")) && (i + 1 < sys.argc))
        {
-               char vabuf[1024];
-               if(com_argv[i+1][0] == '.')
-                       Cvar_SetQuick(&sessionid, com_argv[i+1]);
+               if(sys.argv[i+1][0] == '.')
+                       Cvar_SetQuick(&sessionid, sys.argv[i+1]);
                else
-                       Cvar_SetQuick(&sessionid, va(vabuf, sizeof(vabuf), ".%s", com_argv[i+1]));
+               {
+                       buf = (char *)Z_Malloc(strlen(sys.argv[i+1]) + 2);
+                       dpsnprintf(buf, sizeof(buf), ".%s", sys.argv[i+1]);
+                       Cvar_SetQuick(&sessionid, buf);
+               }
        }
 }
 void Host_LockSession(void)
@@ -1162,8 +1150,6 @@ void Host_UnlockSession(void)
        }
 }
 
-extern hook_t *csqc_concmd;
-
 /*
 ====================
 Host_Init
@@ -1174,9 +1160,10 @@ static void Host_Init (void)
        int i;
        const char* os;
        char vabuf[1024];
-       qboolean dedicated_server = COM_CheckParm("-dedicated") || !cl_available;
        cmd_state_t *cmd = &cmd_client;
 
+       host.state = host_init;
+
        if (COM_CheckParm("-profilegameonly"))
                Sys_AllowProfiling(false);
 
@@ -1226,13 +1213,9 @@ static void Host_Init (void)
        // initialize console command/cvar/alias/command execution systems
        Cmd_Init();
 
-       Cmd_Init_Commands(dedicated_server);
-
        // initialize memory subsystem cvars/commands
        Memory_Init_Commands();
 
-       Hook_Init();
-       csqc_concmd = Hook_Register(csqc_concmd,CL_VM_ConsoleCommand,1);
        // initialize console and logging and its cvars/commands
        Con_Init();
 
@@ -1241,16 +1224,12 @@ static void Host_Init (void)
        Curl_Init_Commands();
        Sys_Init_Commands();
        COM_Init_Commands();
-       FS_Init_Commands();
 
        // initialize console window (only used by sys_win.c)
        Sys_InitConsole();
 
-       // initialize the self-pack (must be before COM_InitGameType as it may add command line options)
-       FS_Init_SelfPack();
-
-       // detect gamemode from commandline options or executable name
-       COM_InitGameType();
+       // initialize filesystem (including fs_basedir, fs_gamedir, -game, scr_screenshot_name)
+       FS_Init();
 
        // construct a version string for the corner of the console
        os = DP_OS_NAME;
@@ -1263,9 +1242,6 @@ static void Host_Init (void)
        // initialize ixtable
        Mathlib_Init();
 
-       // initialize filesystem (including fs_basedir, fs_gamedir, -game, scr_screenshot_name)
-       FS_Init();
-
        // register the cvars for session locking
        Host_InitSession();
 
@@ -1275,8 +1251,6 @@ static void Host_Init (void)
 
        NetConn_Init();
        Curl_Init();
-       //PR_Init();
-       //PR_Cmd_Init();
        PRVM_Init();
        Mod_Init();
        World_Init();
@@ -1299,60 +1273,62 @@ static void Host_Init (void)
        // without crashing the whole game, so this should just be a short-time solution
 
        // here comes the not so critical stuff
-       if (setjmp(host_abortframe)) {
+       if (setjmp(host.abortframe)) {
                return;
        }
 
        Host_AddConfigText(cmd);
-       Cbuf_Execute(cmd);
 
-       // if stuffcmds wasn't run, then quake.rc is probably missing, use default
-       if (!host_stuffcmdsrun)
+       Host_StartVideo();
+
+       // if quake.rc is missing, use default
+       if (!FS_FileExists("quake.rc"))
        {
-               Cbuf_AddText(cmd, "exec default.cfg\nexec " CONFIGFILENAME "\nexec autoexec.cfg\nstuffcmds\n");
+               Cbuf_InsertText(cmd, "exec default.cfg\nexec " CONFIGFILENAME "\nexec autoexec.cfg\n");
                Cbuf_Execute(cmd);
        }
 
+       host.state = host_active;
+
+       // run stuffcmds now, deferred previously because it can crash if a server starts that early
+       Cbuf_AddText(cmd,"stuffcmds\n");
+       Cbuf_Execute(cmd);
+
        Log_Start();
-       
+
        // put up the loading image so the user doesn't stare at a black screen...
        SCR_BeginLoadingPlaque(true);
-
-#ifdef CONFIG_MENU
-       if (cls.state != ca_dedicated)
-       {
-               MR_Init();
-       }
-#endif
-
+       
        // check for special benchmark mode
 // COMMANDLINEOPTION: Client: -benchmark <demoname> runs a timedemo and quits, results of any timedemo can be found in gamedir/benchmark.log (for example id1/benchmark.log)
        i = COM_CheckParm("-benchmark");
-       if (i && i + 1 < com_argc)
+       if (i && i + 1 < sys.argc)
        if (!sv.active && !cls.demoplayback && !cls.connect_trying)
        {
-               Cbuf_AddText(&cmd_client, va(vabuf, sizeof(vabuf), "timedemo %s\n", com_argv[i + 1]));
+               Cbuf_AddText(&cmd_client, va(vabuf, sizeof(vabuf), "timedemo %s\n", sys.argv[i + 1]));
                Cbuf_Execute(&cmd_client);
        }
 
        // check for special demo mode
 // COMMANDLINEOPTION: Client: -demo <demoname> runs a playdemo and quits
        i = COM_CheckParm("-demo");
-       if (i && i + 1 < com_argc)
+       if (i && i + 1 < sys.argc)
        if (!sv.active && !cls.demoplayback && !cls.connect_trying)
        {
-               Cbuf_AddText(&cmd_client, va(vabuf, sizeof(vabuf), "playdemo %s\n", com_argv[i + 1]));
+               Cbuf_AddText(&cmd_client, va(vabuf, sizeof(vabuf), "playdemo %s\n", sys.argv[i + 1]));
                Cbuf_Execute(&cmd_client);
        }
 
+#ifdef CONFIG_VIDEO_CAPTURE
 // COMMANDLINEOPTION: Client: -capturedemo <demoname> captures a playdemo and quits
        i = COM_CheckParm("-capturedemo");
-       if (i && i + 1 < com_argc)
+       if (i && i + 1 < sys.argc)
        if (!sv.active && !cls.demoplayback && !cls.connect_trying)
        {
-               Cbuf_AddText(&cmd_client, va(vabuf, sizeof(vabuf), "playdemo %s\ncl_capturevideo 1\n", com_argv[i + 1]));
+               Cbuf_AddText(&cmd_client, va(vabuf, sizeof(vabuf), "playdemo %s\ncl_capturevideo 1\n", sys.argv[i + 1]));
                Cbuf_Execute(&cmd_client);
        }
+#endif
 
        if (cls.state == ca_dedicated || COM_CheckParm("-listen"))
        if (!sv.active && !cls.demoplayback && !cls.connect_trying)
@@ -1371,8 +1347,6 @@ static void Host_Init (void)
 
        Con_DPrint("========Initialized=========\n");
 
-       //Host_StartVideo();
-
        if (cls.state != ca_dedicated)
                SV_StartThread();
 }
@@ -1395,7 +1369,7 @@ void Host_Shutdown(void)
                Con_Print("recursive shutdown\n");
                return;
        }
-       if (setjmp(host_abortframe))
+       if (setjmp(host.abortframe))
        {
                Con_Print("aborted the quitting frame?!?\n");
                return;
@@ -1434,7 +1408,6 @@ void Host_Shutdown(void)
        S_Terminate ();
        Curl_Shutdown ();
        NetConn_Shutdown ();
-       //PR_Shutdown ();
 
        if (cls.state != ca_dedicated)
        {