From 6ad27355dfe4f50916169dc29bf06bcd4add0558 Mon Sep 17 00:00:00 2001 From: cloudwalk Date: Tue, 20 Apr 2021 13:24:10 +0000 Subject: [PATCH] prvm: Deduplicate console command handler git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@13127 d7cf8633-e32d-0410-b094-e92efae38249 --- csprogs.c | 18 +----------------- prvm_cmds.c | 28 ++++++++++++++++++++++++++++ prvm_cmds.h | 1 + svvm_cmds.c | 21 +-------------------- 4 files changed, 31 insertions(+), 37 deletions(-) diff --git a/csprogs.c b/csprogs.c index 80666f5e..3c90ff13 100644 --- a/csprogs.c +++ b/csprogs.c @@ -512,23 +512,7 @@ qbool CL_VM_UpdateView (double frametime) qbool CL_VM_ConsoleCommand (const char *text) { prvm_prog_t *prog = CLVM_prog; - int restorevm_tempstringsbuf_cursize; - qbool r = false; - if(!cl.csqc_loaded) - return false; - CSQC_BEGIN - if (PRVM_clientfunction(CSQC_ConsoleCommand)) - { - PRVM_clientglobalfloat(time) = cl.time; - PRVM_clientglobaledict(self) = cl.csqc_server2csqcentitynumber[cl.playerentity]; - restorevm_tempstringsbuf_cursize = prog->tempstringsbuf.cursize; - PRVM_G_INT(OFS_PARM0) = PRVM_SetTempString(prog, text); - prog->ExecuteProgram(prog, PRVM_clientfunction(CSQC_ConsoleCommand), "QC function CSQC_ConsoleCommand is missing"); - prog->tempstringsbuf.cursize = restorevm_tempstringsbuf_cursize; - r = CSQC_RETURNVAL != 0; - } - CSQC_END - return r; + return PRVM_ConsoleCommand(prog, text, &prog->funcoffsets.CSQC_ConsoleCommand, false, cl.csqc_server2csqcentitynumber[cl.playerentity], cl.time, cl.csqc_loaded, "QC function CSQC_ConsoleCommand is missing"); } qbool CL_VM_Parse_TempEntity (void) diff --git a/prvm_cmds.c b/prvm_cmds.c index 67ccb3f3..1711d33c 100644 --- a/prvm_cmds.c +++ b/prvm_cmds.c @@ -58,6 +58,34 @@ void VM_CheckEmptyString(prvm_prog_t *prog, const char *s) prog->error_cmd("%s: Bad string", prog->name); } +qbool PRVM_ConsoleCommand (prvm_prog_t *prog, const char *text, int *func, qbool preserve_self, int curself, double ptime, qbool prog_loaded, const char *error_message) +{ + int restorevm_tempstringsbuf_cursize; + int save_self; + qbool r = false; + + if(!prog_loaded) + return false; + + if(func) + { + if(preserve_self) + save_self = PRVM_gameglobaledict(self); + if(ptime) + PRVM_gameglobalfloat(time) = ptime; + PRVM_gameglobaledict(self) = curself; + restorevm_tempstringsbuf_cursize = prog->tempstringsbuf.cursize; + PRVM_G_INT(OFS_PARM0) = PRVM_SetTempString(prog, text); + prog->ExecuteProgram(prog, *func, error_message); + prog->tempstringsbuf.cursize = restorevm_tempstringsbuf_cursize; + if(preserve_self) + PRVM_gameglobaledict(self) = save_self; + r = (int) PRVM_G_FLOAT(OFS_RETURN) != 0; + } + + return r; +} + void VM_GenerateFrameGroupBlend(prvm_prog_t *prog, framegroupblend_t *framegroupblend, const prvm_edict_t *ed) { // self.frame is the interpolation target (new frame) diff --git a/prvm_cmds.h b/prvm_cmds.h index c3f182c2..c3c83b2f 100644 --- a/prvm_cmds.h +++ b/prvm_cmds.h @@ -217,6 +217,7 @@ float getserverlistindexforkey(string key) // general functions void VM_CheckEmptyString (prvm_prog_t *prog, const char *s); void VM_VarString(prvm_prog_t *prog, int first, char *out, int outlength); +qbool PRVM_ConsoleCommand (prvm_prog_t *prog, const char *text, int *func, qbool preserve_self, int curself, double ptime, qbool prog_loaded, const char *error_message); prvm_stringbuffer_t *BufStr_FindCreateReplace (prvm_prog_t *prog, int bufindex, int flags, const char *format); void BufStr_Set(prvm_prog_t *prog, prvm_stringbuffer_t *stringbuffer, int strindex, const char *str); void BufStr_Del(prvm_prog_t *prog, prvm_stringbuffer_t *stringbuffer); diff --git a/svvm_cmds.c b/svvm_cmds.c index bece8d54..8de1d444 100644 --- a/svvm_cmds.c +++ b/svvm_cmds.c @@ -2853,26 +2853,7 @@ static void VM_SV_pointparticles(prvm_prog_t *prog) qbool SV_VM_ConsoleCommand (const char *text) { prvm_prog_t *prog = SVVM_prog; - int restorevm_tempstringsbuf_cursize; - int save_self; - qbool r = false; - - if(!sv.active || !prog || !prog->loaded) - return false; - - if (PRVM_serverfunction(ConsoleCmd)) - { - save_self = PRVM_serverglobaledict(self); - PRVM_serverglobalfloat(time) = sv.time; - restorevm_tempstringsbuf_cursize = prog->tempstringsbuf.cursize; - PRVM_serverglobaledict(self) = PRVM_EDICT_TO_PROG(sv.world.prog->edicts); - PRVM_G_INT(OFS_PARM0) = PRVM_SetTempString(prog, text); - prog->ExecuteProgram(prog, PRVM_serverfunction(ConsoleCmd), "QC function ConsoleCmd is missing"); - prog->tempstringsbuf.cursize = restorevm_tempstringsbuf_cursize; - PRVM_serverglobaledict(self) = save_self; - r = (int) PRVM_G_FLOAT(OFS_RETURN) != 0; - } - return r; + return PRVM_ConsoleCommand(prog, text, &prog->funcoffsets.ConsoleCmd, true, PRVM_EDICT_TO_PROG(sv.world.prog->edicts), sv.time, !(!sv.active || !prog || !prog->loaded), "QC function ConsoleCmd is missing"); } // #352 void(string cmdname) registercommand (EXT_CSQC) -- 2.39.2