From f1572007b4abc4cba2a834095c84e731562926b8 Mon Sep 17 00:00:00 2001 From: cloudwalk Date: Tue, 29 Sep 2020 19:19:09 +0000 Subject: [PATCH] sv_save: Add new hooks to reduce client code in server savegame code. git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@12974 d7cf8633-e32d-0410-b094-e92efae38249 --- cl_main.c | 6 ++++++ host.h | 2 ++ server.h | 2 ++ sv_main.c | 36 ++++++++++++++++++++++++++++++++++++ sv_save.c | 19 ++----------------- 5 files changed, 48 insertions(+), 17 deletions(-) diff --git a/cl_main.c b/cl_main.c index 69f8e622..3f24ce7b 100644 --- a/cl_main.c +++ b/cl_main.c @@ -572,6 +572,11 @@ static void CL_EstablishConnection_Local(void) CL_EstablishConnection("local:1", -2); } +static qbool CL_Intermission(void) +{ + return cl.intermission; +} + /* ============== CL_PrintEntities_f @@ -3055,5 +3060,6 @@ void CL_Init (void) CL_Video_Init(); host.hook.ConnectLocal = CL_EstablishConnection_Local; + host.hook.CL_Intermission = CL_Intermission; } } diff --git a/host.h b/host.h index 4d0ee339..5b6f51d2 100644 --- a/host.h +++ b/host.h @@ -29,6 +29,8 @@ typedef struct host_s struct { void (*ConnectLocal)(void); + qbool (*CL_Intermission)(void); // Quake compatibility + qbool (*SV_CanSave)(void); // Quake compatibility } hook; } host_t; diff --git a/server.h b/server.h index 60c5b757..0df1b40a 100644 --- a/server.h +++ b/server.h @@ -515,6 +515,8 @@ void SV_Init (void); double SV_Frame(double time); void SV_Shutdown(void); +int SV_IsLocalGame(void); + void SV_StartParticle (vec3_t org, vec3_t dir, int color, int count); void SV_StartEffect (vec3_t org, int modelindex, int startframe, int framecount, int framerate); void SV_StartSound (prvm_edict_t *entity, int channel, const char *sample, int volume, float attenuation, qbool reliable, float speed); diff --git a/sv_main.c b/sv_main.c index 3bedf609..867c0bdf 100644 --- a/sv_main.c +++ b/sv_main.c @@ -432,6 +432,30 @@ static void SV_AreaStats_f(cmd_state_t *cmd) World_PrintAreaStats(&sv.world, "server"); } +static qbool SV_CanSave(void) +{ + prvm_prog_t *prog = SVVM_prog; + if(SV_IsLocalGame() == 1) + { + // singleplayer checks + if (!(svs.clients[0].active && PRVM_serveredictfloat(svs.clients[0].edict, deadflag))) + { + Con_Print("Can't savegame with a dead player\n"); + return false; + } + + if(!host.hook.CL_Intermission || !host.hook.CL_Intermission()) + { + Con_Print("Can't save in intermission.\n"); + return false; + } + } + else + Con_Print(CON_WARN "Warning: saving a multiplayer game may have strange results when restored (to properly resume, all players must join in the same player slots and then the game can be reloaded).\n"); + return true; + +} + /* =============== SV_Init @@ -632,6 +656,7 @@ void SV_Init (void) Cvar_RegisterVariable (&sv_mapformat_is_quake3); SV_InitOperatorCommands(); + host.hook.SV_CanSave = SV_CanSave; sv_mempool = Mem_AllocPool("server", 0, NULL); } @@ -1989,6 +2014,17 @@ void SV_SpawnServer (const char *map) // SV_UnlockThreadMutex(); } +/* + * Returns number of slots if we're a listen server. + * Returns 0 if we're a dedicated server. + */ +int SV_IsLocalGame(void) +{ + if (sv.active && LHNETADDRESS_GetAddressType(&host_client->netconnection->peeraddress) == LHNETADDRESSTYPE_LOOP) + return svs.maxclients; + return 0; +} + /* ================== SV_Shutdown diff --git a/sv_save.c b/sv_save.c index 3c7940b8..a08e33a5 100644 --- a/sv_save.c +++ b/sv_save.c @@ -185,7 +185,6 @@ void SV_Savegame_f(cmd_state_t *cmd) { prvm_prog_t *prog = SVVM_prog; char name[MAX_QPATH]; - qbool deadflag = false; if (!sv.active) { @@ -193,25 +192,11 @@ void SV_Savegame_f(cmd_state_t *cmd) return; } - deadflag = cl.islocalgame && svs.clients[0].active && PRVM_serveredictfloat(svs.clients[0].edict, deadflag); - - if (cl.islocalgame) + if(host.hook.SV_CanSave) { - // singleplayer checks - if (cl.intermission) - { - Con_Print("Can't save in intermission.\n"); - return; - } - - if (deadflag) - { - Con_Print("Can't savegame with a dead player\n"); + if(!host.hook.SV_CanSave()) return; - } } - else - Con_Print(CON_WARN "Warning: saving a multiplayer game may have strange results when restored (to properly resume, all players must join in the same player slots and then the game can be reloaded).\n"); if (Cmd_Argc(cmd) != 2) { -- 2.39.2